2
0

connect.c 44 KB

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