connect.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h> /* <netinet/tcp.h> may need it */
  27. #endif
  28. #ifdef HAVE_SYS_UN_H
  29. #include <sys/un.h> /* for sockaddr_un */
  30. #endif
  31. #ifdef HAVE_LINUX_TCP_H
  32. #include <linux/tcp.h>
  33. #elif defined(HAVE_NETINET_TCP_H)
  34. #include <netinet/tcp.h>
  35. #endif
  36. #ifdef HAVE_SYS_IOCTL_H
  37. #include <sys/ioctl.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_FCNTL_H
  43. #include <fcntl.h>
  44. #endif
  45. #ifdef HAVE_ARPA_INET_H
  46. #include <arpa/inet.h>
  47. #endif
  48. #ifdef __VMS
  49. #include <in.h>
  50. #include <inet.h>
  51. #endif
  52. #include "urldata.h"
  53. #include "sendf.h"
  54. #include "if2ip.h"
  55. #include "strerror.h"
  56. #include "cfilters.h"
  57. #include "connect.h"
  58. #include "cf-haproxy.h"
  59. #include "cf-https-connect.h"
  60. #include "cf-socket.h"
  61. #include "select.h"
  62. #include "url.h" /* for Curl_safefree() */
  63. #include "multiif.h"
  64. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  65. #include "inet_ntop.h"
  66. #include "inet_pton.h"
  67. #include "vtls/vtls.h" /* for vtsl cfilters */
  68. #include "progress.h"
  69. #include "warnless.h"
  70. #include "conncache.h"
  71. #include "multihandle.h"
  72. #include "share.h"
  73. #include "version_win32.h"
  74. #include "vquic/vquic.h" /* for quic cfilters */
  75. #include "http_proxy.h"
  76. #include "socks.h"
  77. /* The last 3 #include files should be in this order */
  78. #include "curl_printf.h"
  79. #include "curl_memory.h"
  80. #include "memdebug.h"
  81. /*
  82. * Curl_timeleft() returns the amount of milliseconds left allowed for the
  83. * transfer/connection. If the value is 0, there's no timeout (ie there's
  84. * infinite time left). If the value is negative, the timeout time has already
  85. * elapsed.
  86. *
  87. * If 'nowp' is non-NULL, it points to the current time.
  88. * 'duringconnect' is FALSE if not during a connect, as then of course the
  89. * connect timeout is not taken into account!
  90. *
  91. * @unittest: 1303
  92. */
  93. #define TIMEOUT_CONNECT 1
  94. #define TIMEOUT_MAXTIME 2
  95. timediff_t Curl_timeleft(struct Curl_easy *data,
  96. struct curltime *nowp,
  97. bool duringconnect)
  98. {
  99. unsigned int timeout_set = 0;
  100. timediff_t connect_timeout_ms = 0;
  101. timediff_t maxtime_timeout_ms = 0;
  102. timediff_t timeout_ms = 0;
  103. struct curltime now;
  104. /* The duration of a connect and the total transfer are calculated from two
  105. different time-stamps. It can end up with the total timeout being reached
  106. before the connect timeout expires and we must acknowledge whichever
  107. timeout that is reached first. The total timeout is set per entire
  108. operation, while the connect timeout is set per connect. */
  109. if(data->set.timeout > 0) {
  110. timeout_set = TIMEOUT_MAXTIME;
  111. maxtime_timeout_ms = data->set.timeout;
  112. }
  113. if(duringconnect) {
  114. timeout_set |= TIMEOUT_CONNECT;
  115. connect_timeout_ms = (data->set.connecttimeout > 0) ?
  116. data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT;
  117. }
  118. if(!timeout_set)
  119. /* no timeout */
  120. return 0;
  121. if(!nowp) {
  122. now = Curl_now();
  123. nowp = &now;
  124. }
  125. if(timeout_set & TIMEOUT_MAXTIME) {
  126. maxtime_timeout_ms -= Curl_timediff(*nowp, data->progress.t_startop);
  127. timeout_ms = maxtime_timeout_ms;
  128. }
  129. if(timeout_set & TIMEOUT_CONNECT) {
  130. connect_timeout_ms -= Curl_timediff(*nowp, data->progress.t_startsingle);
  131. if(!(timeout_set & TIMEOUT_MAXTIME) ||
  132. (connect_timeout_ms < maxtime_timeout_ms))
  133. timeout_ms = connect_timeout_ms;
  134. }
  135. if(!timeout_ms)
  136. /* avoid returning 0 as that means no timeout! */
  137. return -1;
  138. return timeout_ms;
  139. }
  140. /* Copies connection info into the transfer handle to make it available when
  141. the transfer handle is no longer associated with the connection. */
  142. void Curl_persistconninfo(struct Curl_easy *data, struct connectdata *conn,
  143. char *local_ip, int local_port)
  144. {
  145. memcpy(data->info.conn_primary_ip, conn->primary_ip, MAX_IPADR_LEN);
  146. if(local_ip && local_ip[0])
  147. memcpy(data->info.conn_local_ip, local_ip, MAX_IPADR_LEN);
  148. else
  149. data->info.conn_local_ip[0] = 0;
  150. data->info.conn_scheme = conn->handler->scheme;
  151. /* conn_protocol can only provide "old" protocols */
  152. data->info.conn_protocol = (conn->handler->protocol) & CURLPROTO_MASK;
  153. data->info.conn_primary_port = conn->port;
  154. data->info.conn_remote_port = conn->remote_port;
  155. data->info.conn_local_port = local_port;
  156. }
  157. static const struct Curl_addrinfo *
  158. addr_first_match(const struct Curl_addrinfo *addr, int family)
  159. {
  160. while(addr) {
  161. if(addr->ai_family == family)
  162. return addr;
  163. addr = addr->ai_next;
  164. }
  165. return NULL;
  166. }
  167. static const struct Curl_addrinfo *
  168. addr_next_match(const struct Curl_addrinfo *addr, int family)
  169. {
  170. while(addr && addr->ai_next) {
  171. addr = addr->ai_next;
  172. if(addr->ai_family == family)
  173. return addr;
  174. }
  175. return NULL;
  176. }
  177. /* retrieves ip address and port from a sockaddr structure.
  178. note it calls Curl_inet_ntop which sets errno on fail, not SOCKERRNO. */
  179. bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
  180. char *addr, int *port)
  181. {
  182. struct sockaddr_in *si = NULL;
  183. #ifdef ENABLE_IPV6
  184. struct sockaddr_in6 *si6 = NULL;
  185. #endif
  186. #if (defined(HAVE_SYS_UN_H) || defined(WIN32_SOCKADDR_UN)) && defined(AF_UNIX)
  187. struct sockaddr_un *su = NULL;
  188. #else
  189. (void)salen;
  190. #endif
  191. switch(sa->sa_family) {
  192. case AF_INET:
  193. si = (struct sockaddr_in *)(void *) sa;
  194. if(Curl_inet_ntop(sa->sa_family, &si->sin_addr,
  195. addr, MAX_IPADR_LEN)) {
  196. unsigned short us_port = ntohs(si->sin_port);
  197. *port = us_port;
  198. return TRUE;
  199. }
  200. break;
  201. #ifdef ENABLE_IPV6
  202. case AF_INET6:
  203. si6 = (struct sockaddr_in6 *)(void *) sa;
  204. if(Curl_inet_ntop(sa->sa_family, &si6->sin6_addr,
  205. addr, MAX_IPADR_LEN)) {
  206. unsigned short us_port = ntohs(si6->sin6_port);
  207. *port = us_port;
  208. return TRUE;
  209. }
  210. break;
  211. #endif
  212. #if (defined(HAVE_SYS_UN_H) || defined(WIN32_SOCKADDR_UN)) && defined(AF_UNIX)
  213. case AF_UNIX:
  214. if(salen > (curl_socklen_t)sizeof(CURL_SA_FAMILY_T)) {
  215. su = (struct sockaddr_un*)sa;
  216. msnprintf(addr, MAX_IPADR_LEN, "%s", su->sun_path);
  217. }
  218. else
  219. addr[0] = 0; /* socket with no name */
  220. *port = 0;
  221. return TRUE;
  222. #endif
  223. default:
  224. break;
  225. }
  226. addr[0] = '\0';
  227. *port = 0;
  228. errno = EAFNOSUPPORT;
  229. return FALSE;
  230. }
  231. struct connfind {
  232. curl_off_t id_tofind;
  233. struct connectdata *found;
  234. };
  235. static int conn_is_conn(struct Curl_easy *data,
  236. struct connectdata *conn, void *param)
  237. {
  238. struct connfind *f = (struct connfind *)param;
  239. (void)data;
  240. if(conn->connection_id == f->id_tofind) {
  241. f->found = conn;
  242. return 1;
  243. }
  244. return 0;
  245. }
  246. /*
  247. * Used to extract socket and connectdata struct for the most recent
  248. * transfer on the given Curl_easy.
  249. *
  250. * The returned socket will be CURL_SOCKET_BAD in case of failure!
  251. */
  252. curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
  253. struct connectdata **connp)
  254. {
  255. DEBUGASSERT(data);
  256. /* this works for an easy handle:
  257. * - that has been used for curl_easy_perform()
  258. * - that is associated with a multi handle, and whose connection
  259. * was detached with CURLOPT_CONNECT_ONLY
  260. */
  261. if((data->state.lastconnect_id != -1) && (data->multi_easy || data->multi)) {
  262. struct connectdata *c;
  263. struct connfind find;
  264. find.id_tofind = data->state.lastconnect_id;
  265. find.found = NULL;
  266. Curl_conncache_foreach(data,
  267. data->share && (data->share->specifier
  268. & (1<< CURL_LOCK_DATA_CONNECT))?
  269. &data->share->conn_cache:
  270. data->multi_easy?
  271. &data->multi_easy->conn_cache:
  272. &data->multi->conn_cache, &find, conn_is_conn);
  273. if(!find.found) {
  274. data->state.lastconnect_id = -1;
  275. return CURL_SOCKET_BAD;
  276. }
  277. c = find.found;
  278. if(connp)
  279. /* only store this if the caller cares for it */
  280. *connp = c;
  281. return c->sock[FIRSTSOCKET];
  282. }
  283. return CURL_SOCKET_BAD;
  284. }
  285. /*
  286. * Curl_conncontrol() marks streams or connection for closure.
  287. */
  288. void Curl_conncontrol(struct connectdata *conn,
  289. int ctrl /* see defines in header */
  290. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  291. , const char *reason
  292. #endif
  293. )
  294. {
  295. /* close if a connection, or a stream that isn't multiplexed. */
  296. /* This function will be called both before and after this connection is
  297. associated with a transfer. */
  298. bool closeit, is_multiplex;
  299. DEBUGASSERT(conn);
  300. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  301. (void)reason; /* useful for debugging */
  302. #endif
  303. is_multiplex = Curl_conn_is_multiplex(conn, FIRSTSOCKET);
  304. closeit = (ctrl == CONNCTRL_CONNECTION) ||
  305. ((ctrl == CONNCTRL_STREAM) && !is_multiplex);
  306. if((ctrl == CONNCTRL_STREAM) && is_multiplex)
  307. ; /* stream signal on multiplex conn never affects close state */
  308. else if((bit)closeit != conn->bits.close) {
  309. conn->bits.close = closeit; /* the only place in the source code that
  310. should assign this bit */
  311. }
  312. }
  313. /**
  314. * job walking the matching addr infos, creating a sub-cfilter with the
  315. * provided method `cf_create` and running setup/connect on it.
  316. */
  317. struct eyeballer {
  318. const char *name;
  319. const struct Curl_addrinfo *addr; /* List of addresses to try, not owned */
  320. int ai_family; /* matching address family only */
  321. cf_ip_connect_create *cf_create; /* for creating cf */
  322. struct Curl_cfilter *cf; /* current sub-cfilter connecting */
  323. struct eyeballer *primary; /* eyeballer this one is backup for */
  324. timediff_t delay_ms; /* delay until start */
  325. struct curltime started; /* start of current attempt */
  326. timediff_t timeoutms; /* timeout for current attempt */
  327. expire_id timeout_id; /* ID for Curl_expire() */
  328. CURLcode result;
  329. int error;
  330. BIT(has_started); /* attempts have started */
  331. BIT(is_done); /* out of addresses/time */
  332. BIT(connected); /* cf has connected */
  333. };
  334. typedef enum {
  335. SCFST_INIT,
  336. SCFST_WAITING,
  337. SCFST_DONE
  338. } cf_connect_state;
  339. struct cf_he_ctx {
  340. int transport;
  341. cf_ip_connect_create *cf_create;
  342. const struct Curl_dns_entry *remotehost;
  343. cf_connect_state state;
  344. struct eyeballer *baller[2];
  345. struct eyeballer *winner;
  346. struct curltime started;
  347. };
  348. static CURLcode eyeballer_new(struct eyeballer **pballer,
  349. cf_ip_connect_create *cf_create,
  350. const struct Curl_addrinfo *addr,
  351. int ai_family,
  352. struct eyeballer *primary,
  353. timediff_t delay_ms,
  354. timediff_t timeout_ms,
  355. expire_id timeout_id)
  356. {
  357. struct eyeballer *baller;
  358. *pballer = NULL;
  359. baller = calloc(1, sizeof(*baller) + 1000);
  360. if(!baller)
  361. return CURLE_OUT_OF_MEMORY;
  362. baller->name = ((ai_family == AF_INET)? "ipv4" : (
  363. #ifdef ENABLE_IPV6
  364. (ai_family == AF_INET6)? "ipv6" :
  365. #endif
  366. "ip"));
  367. baller->cf_create = cf_create;
  368. baller->addr = addr;
  369. baller->ai_family = ai_family;
  370. baller->primary = primary;
  371. baller->delay_ms = delay_ms;
  372. baller->timeoutms = addr_next_match(baller->addr, baller->ai_family)?
  373. timeout_ms / 2 : timeout_ms;
  374. baller->timeout_id = timeout_id;
  375. baller->result = CURLE_COULDNT_CONNECT;
  376. *pballer = baller;
  377. return CURLE_OK;
  378. }
  379. static void baller_close(struct eyeballer *baller,
  380. struct Curl_easy *data)
  381. {
  382. if(baller && baller->cf) {
  383. Curl_conn_cf_discard_chain(&baller->cf, data);
  384. }
  385. }
  386. static void baller_free(struct eyeballer *baller,
  387. struct Curl_easy *data)
  388. {
  389. if(baller) {
  390. baller_close(baller, data);
  391. free(baller);
  392. }
  393. }
  394. static void baller_next_addr(struct eyeballer *baller)
  395. {
  396. baller->addr = addr_next_match(baller->addr, baller->ai_family);
  397. }
  398. /*
  399. * Initiate a connect attempt walk.
  400. *
  401. * Note that even on connect fail it returns CURLE_OK, but with 'sock' set to
  402. * CURL_SOCKET_BAD. Other errors will however return proper errors.
  403. */
  404. static void baller_initiate(struct Curl_cfilter *cf,
  405. struct Curl_easy *data,
  406. struct eyeballer *baller)
  407. {
  408. struct cf_he_ctx *ctx = cf->ctx;
  409. struct Curl_cfilter *cf_prev = baller->cf;
  410. struct Curl_cfilter *wcf;
  411. CURLcode result;
  412. /* Don't close a previous cfilter yet to ensure that the next IP's
  413. socket gets a different file descriptor, which can prevent bugs when
  414. the curl_multi_socket_action interface is used with certain select()
  415. replacements such as kqueue. */
  416. result = baller->cf_create(&baller->cf, data, cf->conn, baller->addr,
  417. ctx->transport);
  418. if(result)
  419. goto out;
  420. /* the new filter might have sub-filters */
  421. for(wcf = baller->cf; wcf; wcf = wcf->next) {
  422. wcf->conn = cf->conn;
  423. wcf->sockindex = cf->sockindex;
  424. }
  425. if(addr_next_match(baller->addr, baller->ai_family)) {
  426. Curl_expire(data, baller->timeoutms, baller->timeout_id);
  427. }
  428. out:
  429. if(result) {
  430. DEBUGF(LOG_CF(data, cf, "%s failed", baller->name));
  431. baller_close(baller, data);
  432. }
  433. if(cf_prev)
  434. Curl_conn_cf_discard_chain(&cf_prev, data);
  435. baller->result = result;
  436. }
  437. /**
  438. * Start a connection attempt on the current baller address.
  439. * Will return CURLE_OK on the first address where a socket
  440. * could be created and the non-blocking connect started.
  441. * Returns error when all remaining addresses have been tried.
  442. */
  443. static CURLcode baller_start(struct Curl_cfilter *cf,
  444. struct Curl_easy *data,
  445. struct eyeballer *baller,
  446. timediff_t timeoutms)
  447. {
  448. baller->error = 0;
  449. baller->connected = FALSE;
  450. baller->has_started = TRUE;
  451. while(baller->addr) {
  452. baller->started = Curl_now();
  453. baller->timeoutms = addr_next_match(baller->addr, baller->ai_family) ?
  454. timeoutms / 2 : timeoutms;
  455. baller_initiate(cf, data, baller);
  456. if(!baller->result)
  457. break;
  458. baller_next_addr(baller);
  459. }
  460. if(!baller->addr) {
  461. baller->is_done = TRUE;
  462. }
  463. return baller->result;
  464. }
  465. /* Used within the multi interface. Try next IP address, returns error if no
  466. more address exists or error */
  467. static CURLcode baller_start_next(struct Curl_cfilter *cf,
  468. struct Curl_easy *data,
  469. struct eyeballer *baller,
  470. timediff_t timeoutms)
  471. {
  472. if(cf->sockindex == FIRSTSOCKET) {
  473. baller_next_addr(baller);
  474. baller_start(cf, data, baller, timeoutms);
  475. }
  476. else {
  477. baller->error = 0;
  478. baller->connected = FALSE;
  479. baller->has_started = TRUE;
  480. baller->is_done = TRUE;
  481. baller->result = CURLE_COULDNT_CONNECT;
  482. }
  483. return baller->result;
  484. }
  485. static CURLcode baller_connect(struct Curl_cfilter *cf,
  486. struct Curl_easy *data,
  487. struct eyeballer *baller,
  488. struct curltime *now,
  489. bool *connected)
  490. {
  491. (void)cf;
  492. *connected = baller->connected;
  493. if(!baller->result && !*connected) {
  494. /* evaluate again */
  495. baller->result = Curl_conn_cf_connect(baller->cf, data, 0, connected);
  496. if(!baller->result) {
  497. if(*connected) {
  498. baller->connected = TRUE;
  499. baller->is_done = TRUE;
  500. }
  501. else if(Curl_timediff(*now, baller->started) >= baller->timeoutms) {
  502. infof(data, "%s connect timeout after %" CURL_FORMAT_TIMEDIFF_T
  503. "ms, move on!", baller->name, baller->timeoutms);
  504. #if defined(ETIMEDOUT)
  505. baller->error = ETIMEDOUT;
  506. #endif
  507. baller->result = CURLE_OPERATION_TIMEDOUT;
  508. }
  509. }
  510. }
  511. return baller->result;
  512. }
  513. /*
  514. * is_connected() checks if the socket has connected.
  515. */
  516. static CURLcode is_connected(struct Curl_cfilter *cf,
  517. struct Curl_easy *data,
  518. bool *connected)
  519. {
  520. struct cf_he_ctx *ctx = cf->ctx;
  521. struct connectdata *conn = cf->conn;
  522. CURLcode result;
  523. struct curltime now;
  524. size_t i;
  525. int ongoing, not_started;
  526. const char *hostname;
  527. /* Check if any of the conn->tempsock we use for establishing connections
  528. * succeeded and, if so, close any ongoing other ones.
  529. * Transfer the successful conn->tempsock to conn->sock[sockindex]
  530. * and set conn->tempsock to CURL_SOCKET_BAD.
  531. * If transport is QUIC, we need to shutdown the ongoing 'other'
  532. * cot ballers in a QUIC appropriate way. */
  533. evaluate:
  534. *connected = FALSE; /* a very negative world view is best */
  535. now = Curl_now();
  536. ongoing = not_started = 0;
  537. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  538. struct eyeballer *baller = ctx->baller[i];
  539. if(!baller || baller->is_done)
  540. continue;
  541. if(!baller->has_started) {
  542. ++not_started;
  543. continue;
  544. }
  545. baller->result = baller_connect(cf, data, baller, &now, connected);
  546. DEBUGF(LOG_CF(data, cf, "%s connect -> %d, connected=%d",
  547. baller->name, baller->result, *connected));
  548. if(!baller->result) {
  549. if(*connected) {
  550. /* connected, declare the winner */
  551. ctx->winner = baller;
  552. ctx->baller[i] = NULL;
  553. break;
  554. }
  555. else { /* still waiting */
  556. ++ongoing;
  557. }
  558. }
  559. else if(!baller->is_done) {
  560. /* The baller failed to connect, start its next attempt */
  561. if(baller->error) {
  562. data->state.os_errno = baller->error;
  563. SET_SOCKERRNO(baller->error);
  564. }
  565. baller_start_next(cf, data, baller, Curl_timeleft(data, &now, TRUE));
  566. if(baller->is_done) {
  567. DEBUGF(LOG_CF(data, cf, "%s done", baller->name));
  568. }
  569. else {
  570. /* next attempt was started */
  571. DEBUGF(LOG_CF(data, cf, "%s trying next", baller->name));
  572. ++ongoing;
  573. }
  574. }
  575. }
  576. if(ctx->winner) {
  577. *connected = TRUE;
  578. return CURLE_OK;
  579. }
  580. /* Nothing connected, check the time before we might
  581. * start new ballers or return ok. */
  582. if((ongoing || not_started) && Curl_timeleft(data, &now, TRUE) < 0) {
  583. failf(data, "Connection timeout after %ld ms",
  584. Curl_timediff(now, data->progress.t_startsingle));
  585. return CURLE_OPERATION_TIMEDOUT;
  586. }
  587. /* Check if we have any waiting ballers to start now. */
  588. if(not_started > 0) {
  589. int added = 0;
  590. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  591. struct eyeballer *baller = ctx->baller[i];
  592. if(!baller || baller->has_started)
  593. continue;
  594. /* We start its primary baller has failed to connect or if
  595. * its start delay_ms have expired */
  596. if((baller->primary && baller->primary->is_done) ||
  597. Curl_timediff(now, ctx->started) >= baller->delay_ms) {
  598. baller_start(cf, data, baller, Curl_timeleft(data, &now, TRUE));
  599. if(baller->is_done) {
  600. DEBUGF(LOG_CF(data, cf, "%s done", baller->name));
  601. }
  602. else {
  603. DEBUGF(LOG_CF(data, cf, "%s starting (timeout=%"
  604. CURL_FORMAT_TIMEDIFF_T "ms)",
  605. baller->name, baller->timeoutms));
  606. ++ongoing;
  607. ++added;
  608. }
  609. }
  610. }
  611. if(added > 0)
  612. goto evaluate;
  613. }
  614. if(ongoing > 0) {
  615. /* We are still trying, return for more waiting */
  616. *connected = FALSE;
  617. return CURLE_OK;
  618. }
  619. /* all ballers have failed to connect. */
  620. DEBUGF(LOG_CF(data, cf, "all eyeballers failed"));
  621. result = CURLE_COULDNT_CONNECT;
  622. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  623. struct eyeballer *baller = ctx->baller[i];
  624. DEBUGF(LOG_CF(data, cf, "%s assess started=%d, result=%d",
  625. baller?baller->name:NULL,
  626. baller?baller->has_started:0,
  627. baller?baller->result:0));
  628. if(baller && baller->has_started && baller->result) {
  629. result = baller->result;
  630. break;
  631. }
  632. }
  633. #ifndef CURL_DISABLE_PROXY
  634. if(conn->bits.socksproxy)
  635. hostname = conn->socks_proxy.host.name;
  636. else if(conn->bits.httpproxy)
  637. hostname = conn->http_proxy.host.name;
  638. else
  639. #endif
  640. if(conn->bits.conn_to_host)
  641. hostname = conn->conn_to_host.name;
  642. else
  643. hostname = conn->host.name;
  644. failf(data, "Failed to connect to %s port %u after "
  645. "%" CURL_FORMAT_TIMEDIFF_T " ms: %s",
  646. hostname, conn->port,
  647. Curl_timediff(now, data->progress.t_startsingle),
  648. curl_easy_strerror(result));
  649. #ifdef WSAETIMEDOUT
  650. if(WSAETIMEDOUT == data->state.os_errno)
  651. result = CURLE_OPERATION_TIMEDOUT;
  652. #elif defined(ETIMEDOUT)
  653. if(ETIMEDOUT == data->state.os_errno)
  654. result = CURLE_OPERATION_TIMEDOUT;
  655. #endif
  656. return result;
  657. }
  658. /*
  659. * Connect to the given host with timeout, proxy or remote doesn't matter.
  660. * There might be more than one IP address to try out.
  661. */
  662. static CURLcode start_connect(struct Curl_cfilter *cf,
  663. struct Curl_easy *data,
  664. const struct Curl_dns_entry *remotehost)
  665. {
  666. struct cf_he_ctx *ctx = cf->ctx;
  667. struct connectdata *conn = cf->conn;
  668. CURLcode result = CURLE_COULDNT_CONNECT;
  669. int ai_family0, ai_family1;
  670. timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  671. const struct Curl_addrinfo *addr0, *addr1;
  672. if(timeout_ms < 0) {
  673. /* a precaution, no need to continue if time already is up */
  674. failf(data, "Connection time-out");
  675. return CURLE_OPERATION_TIMEDOUT;
  676. }
  677. ctx->started = Curl_now();
  678. /* remotehost->addr is the list of addresses from the resolver, each
  679. * with an address family. The list has at least one entry, possibly
  680. * many more.
  681. * We try at most 2 at a time, until we either get a connection or
  682. * run out of addresses to try. Since likelihood of success is tied
  683. * to the address family (e.g. IPV6 might not work at all ), we want
  684. * the 2 connect attempt ballers to try different families, if possible.
  685. *
  686. */
  687. if(conn->ip_version == CURL_IPRESOLVE_WHATEVER) {
  688. /* any IP version is allowed */
  689. ai_family0 = remotehost->addr?
  690. remotehost->addr->ai_family : 0;
  691. #ifdef ENABLE_IPV6
  692. ai_family1 = ai_family0 == AF_INET6 ?
  693. AF_INET : AF_INET6;
  694. #else
  695. ai_family1 = AF_UNSPEC;
  696. #endif
  697. }
  698. else {
  699. /* only one IP version is allowed */
  700. ai_family0 = (conn->ip_version == CURL_IPRESOLVE_V4) ?
  701. AF_INET :
  702. #ifdef ENABLE_IPV6
  703. AF_INET6;
  704. #else
  705. AF_UNSPEC;
  706. #endif
  707. ai_family1 = AF_UNSPEC;
  708. }
  709. /* Get the first address in the list that matches the family,
  710. * this might give NULL, if we do not have any matches. */
  711. addr0 = addr_first_match(remotehost->addr, ai_family0);
  712. addr1 = addr_first_match(remotehost->addr, ai_family1);
  713. if(!addr0 && addr1) {
  714. /* switch around, so a single baller always uses addr0 */
  715. addr0 = addr1;
  716. ai_family0 = ai_family1;
  717. addr1 = NULL;
  718. }
  719. /* We found no address that matches our criteria, we cannot connect */
  720. if(!addr0) {
  721. return CURLE_COULDNT_CONNECT;
  722. }
  723. memset(ctx->baller, 0, sizeof(ctx->baller));
  724. result = eyeballer_new(&ctx->baller[0], ctx->cf_create, addr0, ai_family0,
  725. NULL, 0, /* no primary/delay, start now */
  726. timeout_ms, EXPIRE_DNS_PER_NAME);
  727. if(result)
  728. return result;
  729. DEBUGF(LOG_CF(data, cf, "created %s (timeout %"
  730. CURL_FORMAT_TIMEDIFF_T "ms)",
  731. ctx->baller[0]->name, ctx->baller[0]->timeoutms));
  732. if(addr1) {
  733. /* second one gets a delayed start */
  734. result = eyeballer_new(&ctx->baller[1], ctx->cf_create, addr1, ai_family1,
  735. ctx->baller[0], /* wait on that to fail */
  736. /* or start this delayed */
  737. data->set.happy_eyeballs_timeout,
  738. timeout_ms, EXPIRE_DNS_PER_NAME2);
  739. if(result)
  740. return result;
  741. DEBUGF(LOG_CF(data, cf, "created %s (timeout %"
  742. CURL_FORMAT_TIMEDIFF_T "ms)",
  743. ctx->baller[1]->name, ctx->baller[1]->timeoutms));
  744. }
  745. Curl_expire(data, data->set.happy_eyeballs_timeout,
  746. EXPIRE_HAPPY_EYEBALLS);
  747. return CURLE_OK;
  748. }
  749. static void cf_he_ctx_clear(struct Curl_cfilter *cf, struct Curl_easy *data)
  750. {
  751. struct cf_he_ctx *ctx = cf->ctx;
  752. size_t i;
  753. DEBUGASSERT(ctx);
  754. DEBUGASSERT(data);
  755. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  756. baller_free(ctx->baller[i], data);
  757. ctx->baller[i] = NULL;
  758. }
  759. baller_free(ctx->winner, data);
  760. ctx->winner = NULL;
  761. }
  762. static int cf_he_get_select_socks(struct Curl_cfilter *cf,
  763. struct Curl_easy *data,
  764. curl_socket_t *socks)
  765. {
  766. struct cf_he_ctx *ctx = cf->ctx;
  767. size_t i, s;
  768. int wrc, rc = GETSOCK_BLANK;
  769. curl_socket_t wsocks[MAX_SOCKSPEREASYHANDLE];
  770. if(cf->connected)
  771. return cf->next->cft->get_select_socks(cf->next, data, socks);
  772. for(i = s = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  773. struct eyeballer *baller = ctx->baller[i];
  774. if(!baller || !baller->cf)
  775. continue;
  776. wrc = Curl_conn_cf_get_select_socks(baller->cf, data, wsocks);
  777. if(wrc) {
  778. /* TODO: we assume we get at most one socket back */
  779. socks[s] = wsocks[0];
  780. if(wrc & GETSOCK_WRITESOCK(0))
  781. rc |= GETSOCK_WRITESOCK(s);
  782. if(wrc & GETSOCK_READSOCK(0))
  783. rc |= GETSOCK_READSOCK(s);
  784. s++;
  785. }
  786. }
  787. return rc;
  788. }
  789. static CURLcode cf_he_connect(struct Curl_cfilter *cf,
  790. struct Curl_easy *data,
  791. bool blocking, bool *done)
  792. {
  793. struct cf_he_ctx *ctx = cf->ctx;
  794. CURLcode result = CURLE_OK;
  795. if(cf->connected) {
  796. *done = TRUE;
  797. return CURLE_OK;
  798. }
  799. (void)blocking; /* TODO: do we want to support this? */
  800. DEBUGASSERT(ctx);
  801. *done = FALSE;
  802. switch(ctx->state) {
  803. case SCFST_INIT:
  804. DEBUGASSERT(CURL_SOCKET_BAD == Curl_conn_cf_get_socket(cf, data));
  805. DEBUGASSERT(!cf->connected);
  806. result = start_connect(cf, data, ctx->remotehost);
  807. if(result)
  808. return result;
  809. ctx->state = SCFST_WAITING;
  810. /* FALLTHROUGH */
  811. case SCFST_WAITING:
  812. result = is_connected(cf, data, done);
  813. if(!result && *done) {
  814. DEBUGASSERT(ctx->winner);
  815. DEBUGASSERT(ctx->winner->cf);
  816. DEBUGASSERT(ctx->winner->cf->connected);
  817. /* we have a winner. Install and activate it.
  818. * close/free all others. */
  819. ctx->state = SCFST_DONE;
  820. cf->connected = TRUE;
  821. cf->next = ctx->winner->cf;
  822. ctx->winner->cf = NULL;
  823. cf_he_ctx_clear(cf, data);
  824. Curl_conn_cf_cntrl(cf->next, data, TRUE,
  825. CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
  826. if(cf->conn->handler->protocol & PROTO_FAMILY_SSH)
  827. Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
  828. Curl_verboseconnect(data, cf->conn);
  829. data->info.numconnects++; /* to track the # of connections made */
  830. }
  831. break;
  832. case SCFST_DONE:
  833. *done = TRUE;
  834. break;
  835. }
  836. return result;
  837. }
  838. static void cf_he_close(struct Curl_cfilter *cf,
  839. struct Curl_easy *data)
  840. {
  841. struct cf_he_ctx *ctx = cf->ctx;
  842. DEBUGF(LOG_CF(data, cf, "close"));
  843. cf_he_ctx_clear(cf, data);
  844. cf->connected = FALSE;
  845. ctx->state = SCFST_INIT;
  846. if(cf->next) {
  847. cf->next->cft->do_close(cf->next, data);
  848. Curl_conn_cf_discard_chain(&cf->next, data);
  849. }
  850. }
  851. static bool cf_he_data_pending(struct Curl_cfilter *cf,
  852. const struct Curl_easy *data)
  853. {
  854. struct cf_he_ctx *ctx = cf->ctx;
  855. size_t i;
  856. if(cf->connected)
  857. return cf->next->cft->has_data_pending(cf->next, data);
  858. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  859. struct eyeballer *baller = ctx->baller[i];
  860. if(!baller || !baller->cf)
  861. continue;
  862. if(baller->cf->cft->has_data_pending(baller->cf, data))
  863. return TRUE;
  864. }
  865. return FALSE;
  866. }
  867. static struct curltime get_max_baller_time(struct Curl_cfilter *cf,
  868. struct Curl_easy *data,
  869. int query)
  870. {
  871. struct cf_he_ctx *ctx = cf->ctx;
  872. struct curltime t, tmax;
  873. size_t i;
  874. memset(&tmax, 0, sizeof(tmax));
  875. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  876. struct eyeballer *baller = ctx->baller[i];
  877. memset(&t, 0, sizeof(t));
  878. if(baller && baller->cf &&
  879. !baller->cf->cft->query(baller->cf, data, query, NULL, &t)) {
  880. if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
  881. tmax = t;
  882. }
  883. }
  884. return tmax;
  885. }
  886. static CURLcode cf_he_query(struct Curl_cfilter *cf,
  887. struct Curl_easy *data,
  888. int query, int *pres1, void *pres2)
  889. {
  890. struct cf_he_ctx *ctx = cf->ctx;
  891. if(!cf->connected) {
  892. switch(query) {
  893. case CF_QUERY_CONNECT_REPLY_MS: {
  894. int reply_ms = -1;
  895. size_t i;
  896. for(i = 0; i < sizeof(ctx->baller)/sizeof(ctx->baller[0]); i++) {
  897. struct eyeballer *baller = ctx->baller[i];
  898. int breply_ms;
  899. if(baller && baller->cf &&
  900. !baller->cf->cft->query(baller->cf, data, query,
  901. &breply_ms, NULL)) {
  902. if(breply_ms >= 0 && (reply_ms < 0 || breply_ms < reply_ms))
  903. reply_ms = breply_ms;
  904. }
  905. }
  906. *pres1 = reply_ms;
  907. DEBUGF(LOG_CF(data, cf, "query connect reply: %dms", *pres1));
  908. return CURLE_OK;
  909. }
  910. case CF_QUERY_TIMER_CONNECT: {
  911. struct curltime *when = pres2;
  912. *when = get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
  913. return CURLE_OK;
  914. }
  915. case CF_QUERY_TIMER_APPCONNECT: {
  916. struct curltime *when = pres2;
  917. *when = get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
  918. return CURLE_OK;
  919. }
  920. default:
  921. break;
  922. }
  923. }
  924. return cf->next?
  925. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  926. CURLE_UNKNOWN_OPTION;
  927. }
  928. static void cf_he_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  929. {
  930. struct cf_he_ctx *ctx = cf->ctx;
  931. DEBUGF(LOG_CF(data, cf, "destroy"));
  932. if(ctx) {
  933. cf_he_ctx_clear(cf, data);
  934. }
  935. /* release any resources held in state */
  936. Curl_safefree(ctx);
  937. }
  938. struct Curl_cftype Curl_cft_happy_eyeballs = {
  939. "HAPPY-EYEBALLS",
  940. 0,
  941. CURL_LOG_DEFAULT,
  942. cf_he_destroy,
  943. cf_he_connect,
  944. cf_he_close,
  945. Curl_cf_def_get_host,
  946. cf_he_get_select_socks,
  947. cf_he_data_pending,
  948. Curl_cf_def_send,
  949. Curl_cf_def_recv,
  950. Curl_cf_def_cntrl,
  951. Curl_cf_def_conn_is_alive,
  952. Curl_cf_def_conn_keep_alive,
  953. cf_he_query,
  954. };
  955. /**
  956. * Create a happy eyeball connection filter that uses the, once resolved,
  957. * address information to connect on ip families based on connection
  958. * configuration.
  959. * @param pcf output, the created cfilter
  960. * @param data easy handle used in creation
  961. * @param conn connection the filter is created for
  962. * @param cf_create method to create the sub-filters performing the
  963. * actual connects.
  964. */
  965. static CURLcode
  966. cf_happy_eyeballs_create(struct Curl_cfilter **pcf,
  967. struct Curl_easy *data,
  968. struct connectdata *conn,
  969. cf_ip_connect_create *cf_create,
  970. const struct Curl_dns_entry *remotehost,
  971. int transport)
  972. {
  973. struct cf_he_ctx *ctx = NULL;
  974. CURLcode result;
  975. (void)data;
  976. (void)conn;
  977. *pcf = NULL;
  978. ctx = calloc(sizeof(*ctx), 1);
  979. if(!ctx) {
  980. result = CURLE_OUT_OF_MEMORY;
  981. goto out;
  982. }
  983. ctx->transport = transport;
  984. ctx->cf_create = cf_create;
  985. ctx->remotehost = remotehost;
  986. result = Curl_cf_create(pcf, &Curl_cft_happy_eyeballs, ctx);
  987. out:
  988. if(result) {
  989. Curl_safefree(*pcf);
  990. Curl_safefree(ctx);
  991. }
  992. return result;
  993. }
  994. struct transport_provider {
  995. int transport;
  996. cf_ip_connect_create *cf_create;
  997. };
  998. static
  999. #ifndef DEBUGBUILD
  1000. const
  1001. #endif
  1002. struct transport_provider transport_providers[] = {
  1003. { TRNSPRT_TCP, Curl_cf_tcp_create },
  1004. #ifdef ENABLE_QUIC
  1005. { TRNSPRT_QUIC, Curl_cf_quic_create },
  1006. #endif
  1007. { TRNSPRT_UDP, Curl_cf_udp_create },
  1008. { TRNSPRT_UNIX, Curl_cf_unix_create },
  1009. };
  1010. #ifndef ARRAYSIZE
  1011. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  1012. #endif
  1013. static cf_ip_connect_create *get_cf_create(int transport)
  1014. {
  1015. size_t i;
  1016. for(i = 0; i < ARRAYSIZE(transport_providers); ++i) {
  1017. if(transport == transport_providers[i].transport)
  1018. return transport_providers[i].cf_create;
  1019. }
  1020. return NULL;
  1021. }
  1022. static CURLcode cf_he_insert_after(struct Curl_cfilter *cf_at,
  1023. struct Curl_easy *data,
  1024. const struct Curl_dns_entry *remotehost,
  1025. int transport)
  1026. {
  1027. cf_ip_connect_create *cf_create;
  1028. struct Curl_cfilter *cf;
  1029. CURLcode result;
  1030. /* Need to be first */
  1031. DEBUGASSERT(cf_at);
  1032. cf_create = get_cf_create(transport);
  1033. if(!cf_create) {
  1034. DEBUGF(LOG_CF(data, cf_at, "unsupported transport type %d", transport));
  1035. return CURLE_UNSUPPORTED_PROTOCOL;
  1036. }
  1037. result = cf_happy_eyeballs_create(&cf, data, cf_at->conn,
  1038. cf_create, remotehost,
  1039. transport);
  1040. if(result)
  1041. return result;
  1042. Curl_conn_cf_insert_after(cf_at, cf);
  1043. return CURLE_OK;
  1044. }
  1045. typedef enum {
  1046. CF_SETUP_INIT,
  1047. CF_SETUP_CNNCT_EYEBALLS,
  1048. CF_SETUP_CNNCT_SOCKS,
  1049. CF_SETUP_CNNCT_HTTP_PROXY,
  1050. CF_SETUP_CNNCT_HAPROXY,
  1051. CF_SETUP_CNNCT_SSL,
  1052. CF_SETUP_DONE
  1053. } cf_setup_state;
  1054. struct cf_setup_ctx {
  1055. cf_setup_state state;
  1056. const struct Curl_dns_entry *remotehost;
  1057. int ssl_mode;
  1058. int transport;
  1059. };
  1060. static CURLcode cf_setup_connect(struct Curl_cfilter *cf,
  1061. struct Curl_easy *data,
  1062. bool blocking, bool *done)
  1063. {
  1064. struct cf_setup_ctx *ctx = cf->ctx;
  1065. CURLcode result = CURLE_OK;
  1066. if(cf->connected) {
  1067. *done = TRUE;
  1068. return CURLE_OK;
  1069. }
  1070. /* connect current sub-chain */
  1071. connect_sub_chain:
  1072. if(cf->next && !cf->next->connected) {
  1073. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  1074. if(result || !*done)
  1075. return result;
  1076. }
  1077. if(ctx->state < CF_SETUP_CNNCT_EYEBALLS) {
  1078. result = cf_he_insert_after(cf, data, ctx->remotehost, ctx->transport);
  1079. if(result)
  1080. return result;
  1081. ctx->state = CF_SETUP_CNNCT_EYEBALLS;
  1082. if(!cf->next || !cf->next->connected)
  1083. goto connect_sub_chain;
  1084. }
  1085. /* sub-chain connected, do we need to add more? */
  1086. #ifndef CURL_DISABLE_PROXY
  1087. if(ctx->state < CF_SETUP_CNNCT_SOCKS && cf->conn->bits.socksproxy) {
  1088. result = Curl_cf_socks_proxy_insert_after(cf, data);
  1089. if(result)
  1090. return result;
  1091. ctx->state = CF_SETUP_CNNCT_SOCKS;
  1092. if(!cf->next || !cf->next->connected)
  1093. goto connect_sub_chain;
  1094. }
  1095. if(ctx->state < CF_SETUP_CNNCT_HTTP_PROXY && cf->conn->bits.httpproxy) {
  1096. #ifdef USE_SSL
  1097. if(IS_HTTPS_PROXY(cf->conn->http_proxy.proxytype)
  1098. && !Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
  1099. result = Curl_cf_ssl_proxy_insert_after(cf, data);
  1100. if(result)
  1101. return result;
  1102. }
  1103. #endif /* USE_SSL */
  1104. #if !defined(CURL_DISABLE_HTTP)
  1105. if(cf->conn->bits.tunnel_proxy) {
  1106. result = Curl_cf_http_proxy_insert_after(cf, data);
  1107. if(result)
  1108. return result;
  1109. }
  1110. #endif /* !CURL_DISABLE_HTTP */
  1111. ctx->state = CF_SETUP_CNNCT_HTTP_PROXY;
  1112. if(!cf->next || !cf->next->connected)
  1113. goto connect_sub_chain;
  1114. }
  1115. #endif /* !CURL_DISABLE_PROXY */
  1116. if(ctx->state < CF_SETUP_CNNCT_HAPROXY) {
  1117. #if !defined(CURL_DISABLE_PROXY)
  1118. if(data->set.haproxyprotocol) {
  1119. if(Curl_conn_is_ssl(cf->conn, cf->sockindex)) {
  1120. failf(data, "haproxy protocol not support with SSL "
  1121. "encryption in place (QUIC?)");
  1122. return CURLE_UNSUPPORTED_PROTOCOL;
  1123. }
  1124. result = Curl_cf_haproxy_insert_after(cf, data);
  1125. if(result)
  1126. return result;
  1127. }
  1128. #endif /* !CURL_DISABLE_PROXY */
  1129. ctx->state = CF_SETUP_CNNCT_HAPROXY;
  1130. if(!cf->next || !cf->next->connected)
  1131. goto connect_sub_chain;
  1132. }
  1133. if(ctx->state < CF_SETUP_CNNCT_SSL) {
  1134. #ifdef USE_SSL
  1135. if((ctx->ssl_mode == CURL_CF_SSL_ENABLE
  1136. || (ctx->ssl_mode != CURL_CF_SSL_DISABLE
  1137. && cf->conn->handler->flags & PROTOPT_SSL)) /* we want SSL */
  1138. && !Curl_conn_is_ssl(cf->conn, cf->sockindex)) { /* it is missing */
  1139. result = Curl_cf_ssl_insert_after(cf, data);
  1140. if(result)
  1141. return result;
  1142. }
  1143. #endif /* USE_SSL */
  1144. ctx->state = CF_SETUP_CNNCT_SSL;
  1145. if(!cf->next || !cf->next->connected)
  1146. goto connect_sub_chain;
  1147. }
  1148. ctx->state = CF_SETUP_DONE;
  1149. cf->connected = TRUE;
  1150. *done = TRUE;
  1151. return CURLE_OK;
  1152. }
  1153. static void cf_setup_close(struct Curl_cfilter *cf,
  1154. struct Curl_easy *data)
  1155. {
  1156. struct cf_setup_ctx *ctx = cf->ctx;
  1157. DEBUGF(LOG_CF(data, cf, "close"));
  1158. cf->connected = FALSE;
  1159. ctx->state = CF_SETUP_INIT;
  1160. if(cf->next) {
  1161. cf->next->cft->do_close(cf->next, data);
  1162. Curl_conn_cf_discard_chain(&cf->next, data);
  1163. }
  1164. }
  1165. static void cf_setup_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1166. {
  1167. struct cf_setup_ctx *ctx = cf->ctx;
  1168. (void)data;
  1169. DEBUGF(LOG_CF(data, cf, "destroy"));
  1170. Curl_safefree(ctx);
  1171. }
  1172. struct Curl_cftype Curl_cft_setup = {
  1173. "SETUP",
  1174. 0,
  1175. CURL_LOG_DEFAULT,
  1176. cf_setup_destroy,
  1177. cf_setup_connect,
  1178. cf_setup_close,
  1179. Curl_cf_def_get_host,
  1180. Curl_cf_def_get_select_socks,
  1181. Curl_cf_def_data_pending,
  1182. Curl_cf_def_send,
  1183. Curl_cf_def_recv,
  1184. Curl_cf_def_cntrl,
  1185. Curl_cf_def_conn_is_alive,
  1186. Curl_cf_def_conn_keep_alive,
  1187. Curl_cf_def_query,
  1188. };
  1189. static CURLcode cf_setup_create(struct Curl_cfilter **pcf,
  1190. struct Curl_easy *data,
  1191. const struct Curl_dns_entry *remotehost,
  1192. int transport,
  1193. int ssl_mode)
  1194. {
  1195. struct Curl_cfilter *cf = NULL;
  1196. struct cf_setup_ctx *ctx;
  1197. CURLcode result = CURLE_OK;
  1198. (void)data;
  1199. ctx = calloc(sizeof(*ctx), 1);
  1200. if(!ctx) {
  1201. result = CURLE_OUT_OF_MEMORY;
  1202. goto out;
  1203. }
  1204. ctx->state = CF_SETUP_INIT;
  1205. ctx->remotehost = remotehost;
  1206. ctx->ssl_mode = ssl_mode;
  1207. ctx->transport = transport;
  1208. result = Curl_cf_create(&cf, &Curl_cft_setup, ctx);
  1209. if(result)
  1210. goto out;
  1211. ctx = NULL;
  1212. out:
  1213. *pcf = result? NULL : cf;
  1214. free(ctx);
  1215. return result;
  1216. }
  1217. static CURLcode cf_setup_add(struct Curl_easy *data,
  1218. struct connectdata *conn,
  1219. int sockindex,
  1220. const struct Curl_dns_entry *remotehost,
  1221. int transport,
  1222. int ssl_mode)
  1223. {
  1224. struct Curl_cfilter *cf;
  1225. CURLcode result = CURLE_OK;
  1226. DEBUGASSERT(data);
  1227. result = cf_setup_create(&cf, data, remotehost, transport, ssl_mode);
  1228. if(result)
  1229. goto out;
  1230. Curl_conn_cf_add(data, conn, sockindex, cf);
  1231. out:
  1232. return result;
  1233. }
  1234. #ifdef DEBUGBUILD
  1235. /* used by unit2600.c */
  1236. void Curl_debug_set_transport_provider(int transport,
  1237. cf_ip_connect_create *cf_create)
  1238. {
  1239. size_t i;
  1240. for(i = 0; i < ARRAYSIZE(transport_providers); ++i) {
  1241. if(transport == transport_providers[i].transport) {
  1242. transport_providers[i].cf_create = cf_create;
  1243. return;
  1244. }
  1245. }
  1246. }
  1247. #endif /* DEBUGBUILD */
  1248. CURLcode Curl_cf_setup_insert_after(struct Curl_cfilter *cf_at,
  1249. struct Curl_easy *data,
  1250. const struct Curl_dns_entry *remotehost,
  1251. int transport,
  1252. int ssl_mode)
  1253. {
  1254. struct Curl_cfilter *cf;
  1255. CURLcode result;
  1256. DEBUGASSERT(data);
  1257. result = cf_setup_create(&cf, data, remotehost, transport, ssl_mode);
  1258. if(result)
  1259. goto out;
  1260. Curl_conn_cf_insert_after(cf_at, cf);
  1261. out:
  1262. return result;
  1263. }
  1264. CURLcode Curl_conn_setup(struct Curl_easy *data,
  1265. struct connectdata *conn,
  1266. int sockindex,
  1267. const struct Curl_dns_entry *remotehost,
  1268. int ssl_mode)
  1269. {
  1270. CURLcode result = CURLE_OK;
  1271. DEBUGASSERT(data);
  1272. DEBUGASSERT(conn->handler);
  1273. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  1274. if(!conn->cfilter[sockindex] &&
  1275. conn->handler->protocol == CURLPROTO_HTTPS) {
  1276. DEBUGASSERT(ssl_mode != CURL_CF_SSL_DISABLE);
  1277. result = Curl_cf_https_setup(data, conn, sockindex, remotehost);
  1278. if(result)
  1279. goto out;
  1280. }
  1281. #endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */
  1282. /* Still no cfilter set, apply default. */
  1283. if(!conn->cfilter[sockindex]) {
  1284. result = cf_setup_add(data, conn, sockindex, remotehost,
  1285. conn->transport, ssl_mode);
  1286. if(result)
  1287. goto out;
  1288. }
  1289. DEBUGASSERT(conn->cfilter[sockindex]);
  1290. out:
  1291. return result;
  1292. }