2
0

connect.c 41 KB

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