connect.c 40 KB

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