connect.c 43 KB

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