connect.c 41 KB

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