2
0

connect.c 45 KB

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