connect.c 42 KB

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