cf-https-connect.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. #if !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER)
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "curl_trc.h"
  29. #include "cfilters.h"
  30. #include "connect.h"
  31. #include "multiif.h"
  32. #include "cf-https-connect.h"
  33. #include "http2.h"
  34. #include "vquic/vquic.h"
  35. /* The last 3 #include files should be in this order */
  36. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. typedef enum {
  40. CF_HC_INIT,
  41. CF_HC_CONNECT,
  42. CF_HC_SUCCESS,
  43. CF_HC_FAILURE
  44. } cf_hc_state;
  45. struct cf_hc_baller {
  46. const char *name;
  47. struct Curl_cfilter *cf;
  48. CURLcode result;
  49. struct curltime started;
  50. int reply_ms;
  51. BIT(enabled);
  52. BIT(shutdown);
  53. };
  54. static void cf_hc_baller_reset(struct cf_hc_baller *b,
  55. struct Curl_easy *data)
  56. {
  57. if(b->cf) {
  58. Curl_conn_cf_close(b->cf, data);
  59. Curl_conn_cf_discard_chain(&b->cf, data);
  60. b->cf = NULL;
  61. }
  62. b->result = CURLE_OK;
  63. b->reply_ms = -1;
  64. }
  65. static bool cf_hc_baller_is_active(struct cf_hc_baller *b)
  66. {
  67. return b->enabled && b->cf && !b->result;
  68. }
  69. static bool cf_hc_baller_has_started(struct cf_hc_baller *b)
  70. {
  71. return !!b->cf;
  72. }
  73. static int cf_hc_baller_reply_ms(struct cf_hc_baller *b,
  74. struct Curl_easy *data)
  75. {
  76. if(b->reply_ms < 0)
  77. b->cf->cft->query(b->cf, data, CF_QUERY_CONNECT_REPLY_MS,
  78. &b->reply_ms, NULL);
  79. return b->reply_ms;
  80. }
  81. static bool cf_hc_baller_data_pending(struct cf_hc_baller *b,
  82. const struct Curl_easy *data)
  83. {
  84. return b->cf && !b->result && b->cf->cft->has_data_pending(b->cf, data);
  85. }
  86. struct cf_hc_ctx {
  87. cf_hc_state state;
  88. const struct Curl_dns_entry *remotehost;
  89. struct curltime started; /* when connect started */
  90. CURLcode result; /* overall result */
  91. struct cf_hc_baller h3_baller;
  92. struct cf_hc_baller h21_baller;
  93. unsigned int soft_eyeballs_timeout_ms;
  94. unsigned int hard_eyeballs_timeout_ms;
  95. };
  96. static void cf_hc_baller_init(struct cf_hc_baller *b,
  97. struct Curl_cfilter *cf,
  98. struct Curl_easy *data,
  99. const char *name,
  100. int transport)
  101. {
  102. struct cf_hc_ctx *ctx = cf->ctx;
  103. struct Curl_cfilter *save = cf->next;
  104. b->name = name;
  105. cf->next = NULL;
  106. b->started = Curl_now();
  107. b->result = Curl_cf_setup_insert_after(cf, data, ctx->remotehost,
  108. transport, CURL_CF_SSL_ENABLE);
  109. b->cf = cf->next;
  110. cf->next = save;
  111. }
  112. static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
  113. struct Curl_cfilter *cf,
  114. struct Curl_easy *data,
  115. bool *done)
  116. {
  117. struct Curl_cfilter *save = cf->next;
  118. cf->next = b->cf;
  119. b->result = Curl_conn_cf_connect(cf->next, data, FALSE, done);
  120. b->cf = cf->next; /* it might mutate */
  121. cf->next = save;
  122. return b->result;
  123. }
  124. static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
  125. {
  126. struct cf_hc_ctx *ctx = cf->ctx;
  127. if(ctx) {
  128. cf_hc_baller_reset(&ctx->h3_baller, data);
  129. cf_hc_baller_reset(&ctx->h21_baller, data);
  130. ctx->state = CF_HC_INIT;
  131. ctx->result = CURLE_OK;
  132. ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
  133. ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 2;
  134. }
  135. }
  136. static CURLcode baller_connected(struct Curl_cfilter *cf,
  137. struct Curl_easy *data,
  138. struct cf_hc_baller *winner)
  139. {
  140. struct cf_hc_ctx *ctx = cf->ctx;
  141. CURLcode result = CURLE_OK;
  142. DEBUGASSERT(winner->cf);
  143. if(winner != &ctx->h3_baller)
  144. cf_hc_baller_reset(&ctx->h3_baller, data);
  145. if(winner != &ctx->h21_baller)
  146. cf_hc_baller_reset(&ctx->h21_baller, data);
  147. CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
  148. winner->name, (int)Curl_timediff(Curl_now(), winner->started),
  149. cf_hc_baller_reply_ms(winner, data));
  150. cf->next = winner->cf;
  151. winner->cf = NULL;
  152. switch(cf->conn->alpn) {
  153. case CURL_HTTP_VERSION_3:
  154. infof(data, "using HTTP/3");
  155. break;
  156. case CURL_HTTP_VERSION_2:
  157. #ifdef USE_NGHTTP2
  158. /* Using nghttp2, we add the filter "below" us, so when the conn
  159. * closes, we tear it down for a fresh reconnect */
  160. result = Curl_http2_switch_at(cf, data);
  161. if(result) {
  162. ctx->state = CF_HC_FAILURE;
  163. ctx->result = result;
  164. return result;
  165. }
  166. #endif
  167. infof(data, "using HTTP/2");
  168. break;
  169. default:
  170. infof(data, "using HTTP/1.x");
  171. break;
  172. }
  173. ctx->state = CF_HC_SUCCESS;
  174. cf->connected = TRUE;
  175. Curl_conn_cf_cntrl(cf->next, data, TRUE,
  176. CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
  177. return result;
  178. }
  179. static bool time_to_start_h21(struct Curl_cfilter *cf,
  180. struct Curl_easy *data,
  181. struct curltime now)
  182. {
  183. struct cf_hc_ctx *ctx = cf->ctx;
  184. timediff_t elapsed_ms;
  185. if(!ctx->h21_baller.enabled || cf_hc_baller_has_started(&ctx->h21_baller))
  186. return FALSE;
  187. if(!ctx->h3_baller.enabled || !cf_hc_baller_is_active(&ctx->h3_baller))
  188. return TRUE;
  189. elapsed_ms = Curl_timediff(now, ctx->started);
  190. if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
  191. CURL_TRC_CF(data, cf, "hard timeout of %dms reached, starting h21",
  192. ctx->hard_eyeballs_timeout_ms);
  193. return TRUE;
  194. }
  195. if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
  196. if(cf_hc_baller_reply_ms(&ctx->h3_baller, data) < 0) {
  197. CURL_TRC_CF(data, cf, "soft timeout of %dms reached, h3 has not "
  198. "seen any data, starting h21",
  199. ctx->soft_eyeballs_timeout_ms);
  200. return TRUE;
  201. }
  202. /* set the effective hard timeout again */
  203. Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
  204. EXPIRE_ALPN_EYEBALLS);
  205. }
  206. return FALSE;
  207. }
  208. static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
  209. struct Curl_easy *data,
  210. bool blocking, bool *done)
  211. {
  212. struct cf_hc_ctx *ctx = cf->ctx;
  213. struct curltime now;
  214. CURLcode result = CURLE_OK;
  215. (void)blocking;
  216. if(cf->connected) {
  217. *done = TRUE;
  218. return CURLE_OK;
  219. }
  220. *done = FALSE;
  221. now = Curl_now();
  222. switch(ctx->state) {
  223. case CF_HC_INIT:
  224. DEBUGASSERT(!ctx->h3_baller.cf);
  225. DEBUGASSERT(!ctx->h21_baller.cf);
  226. DEBUGASSERT(!cf->next);
  227. CURL_TRC_CF(data, cf, "connect, init");
  228. ctx->started = now;
  229. if(ctx->h3_baller.enabled) {
  230. cf_hc_baller_init(&ctx->h3_baller, cf, data, "h3", TRNSPRT_QUIC);
  231. if(ctx->h21_baller.enabled)
  232. Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
  233. }
  234. else if(ctx->h21_baller.enabled)
  235. cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
  236. cf->conn->transport);
  237. ctx->state = CF_HC_CONNECT;
  238. FALLTHROUGH();
  239. case CF_HC_CONNECT:
  240. if(cf_hc_baller_is_active(&ctx->h3_baller)) {
  241. result = cf_hc_baller_connect(&ctx->h3_baller, cf, data, done);
  242. if(!result && *done) {
  243. result = baller_connected(cf, data, &ctx->h3_baller);
  244. goto out;
  245. }
  246. }
  247. if(time_to_start_h21(cf, data, now)) {
  248. cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
  249. cf->conn->transport);
  250. }
  251. if(cf_hc_baller_is_active(&ctx->h21_baller)) {
  252. CURL_TRC_CF(data, cf, "connect, check h21");
  253. result = cf_hc_baller_connect(&ctx->h21_baller, cf, data, done);
  254. if(!result && *done) {
  255. result = baller_connected(cf, data, &ctx->h21_baller);
  256. goto out;
  257. }
  258. }
  259. if((!ctx->h3_baller.enabled || ctx->h3_baller.result) &&
  260. (!ctx->h21_baller.enabled || ctx->h21_baller.result)) {
  261. /* both failed or disabled. we give up */
  262. CURL_TRC_CF(data, cf, "connect, all failed");
  263. result = ctx->result = ctx->h3_baller.enabled?
  264. ctx->h3_baller.result : ctx->h21_baller.result;
  265. ctx->state = CF_HC_FAILURE;
  266. goto out;
  267. }
  268. result = CURLE_OK;
  269. *done = FALSE;
  270. break;
  271. case CF_HC_FAILURE:
  272. result = ctx->result;
  273. cf->connected = FALSE;
  274. *done = FALSE;
  275. break;
  276. case CF_HC_SUCCESS:
  277. result = CURLE_OK;
  278. cf->connected = TRUE;
  279. *done = TRUE;
  280. break;
  281. }
  282. out:
  283. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  284. return result;
  285. }
  286. static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
  287. struct Curl_easy *data, bool *done)
  288. {
  289. struct cf_hc_ctx *ctx = cf->ctx;
  290. struct cf_hc_baller *ballers[2];
  291. size_t i;
  292. CURLcode result = CURLE_OK;
  293. DEBUGASSERT(data);
  294. if(cf->connected) {
  295. *done = TRUE;
  296. return CURLE_OK;
  297. }
  298. /* shutdown all ballers that have not done so already. If one fails,
  299. * continue shutting down others until all are shutdown. */
  300. ballers[0] = &ctx->h3_baller;
  301. ballers[1] = &ctx->h21_baller;
  302. for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
  303. struct cf_hc_baller *b = ballers[i];
  304. bool bdone = FALSE;
  305. if(!cf_hc_baller_is_active(b) || b->shutdown)
  306. continue;
  307. b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
  308. if(b->result || bdone)
  309. b->shutdown = TRUE; /* treat a failed shutdown as done */
  310. }
  311. *done = TRUE;
  312. for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
  313. if(ballers[i] && !ballers[i]->shutdown)
  314. *done = FALSE;
  315. }
  316. if(*done) {
  317. for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
  318. if(ballers[i] && ballers[i]->result)
  319. result = ballers[i]->result;
  320. }
  321. }
  322. CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done);
  323. return result;
  324. }
  325. static void cf_hc_adjust_pollset(struct Curl_cfilter *cf,
  326. struct Curl_easy *data,
  327. struct easy_pollset *ps)
  328. {
  329. if(!cf->connected) {
  330. struct cf_hc_ctx *ctx = cf->ctx;
  331. struct cf_hc_baller *ballers[2];
  332. size_t i;
  333. ballers[0] = &ctx->h3_baller;
  334. ballers[1] = &ctx->h21_baller;
  335. for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
  336. struct cf_hc_baller *b = ballers[i];
  337. if(!cf_hc_baller_is_active(b))
  338. continue;
  339. Curl_conn_cf_adjust_pollset(b->cf, data, ps);
  340. }
  341. CURL_TRC_CF(data, cf, "adjust_pollset -> %d socks", ps->num);
  342. }
  343. }
  344. static bool cf_hc_data_pending(struct Curl_cfilter *cf,
  345. const struct Curl_easy *data)
  346. {
  347. struct cf_hc_ctx *ctx = cf->ctx;
  348. if(cf->connected)
  349. return cf->next->cft->has_data_pending(cf->next, data);
  350. CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
  351. return cf_hc_baller_data_pending(&ctx->h3_baller, data)
  352. || cf_hc_baller_data_pending(&ctx->h21_baller, data);
  353. }
  354. static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
  355. struct Curl_easy *data,
  356. int query)
  357. {
  358. struct cf_hc_ctx *ctx = cf->ctx;
  359. struct Curl_cfilter *cfb;
  360. struct curltime t, tmax;
  361. memset(&tmax, 0, sizeof(tmax));
  362. memset(&t, 0, sizeof(t));
  363. cfb = ctx->h21_baller.enabled? ctx->h21_baller.cf : NULL;
  364. if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
  365. if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
  366. tmax = t;
  367. }
  368. memset(&t, 0, sizeof(t));
  369. cfb = ctx->h3_baller.enabled? ctx->h3_baller.cf : NULL;
  370. if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
  371. if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
  372. tmax = t;
  373. }
  374. return tmax;
  375. }
  376. static CURLcode cf_hc_query(struct Curl_cfilter *cf,
  377. struct Curl_easy *data,
  378. int query, int *pres1, void *pres2)
  379. {
  380. if(!cf->connected) {
  381. switch(query) {
  382. case CF_QUERY_TIMER_CONNECT: {
  383. struct curltime *when = pres2;
  384. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
  385. return CURLE_OK;
  386. }
  387. case CF_QUERY_TIMER_APPCONNECT: {
  388. struct curltime *when = pres2;
  389. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
  390. return CURLE_OK;
  391. }
  392. default:
  393. break;
  394. }
  395. }
  396. return cf->next?
  397. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  398. CURLE_UNKNOWN_OPTION;
  399. }
  400. static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  401. {
  402. CURL_TRC_CF(data, cf, "close");
  403. cf_hc_reset(cf, data);
  404. cf->connected = FALSE;
  405. if(cf->next) {
  406. cf->next->cft->do_close(cf->next, data);
  407. Curl_conn_cf_discard_chain(&cf->next, data);
  408. }
  409. }
  410. static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  411. {
  412. struct cf_hc_ctx *ctx = cf->ctx;
  413. (void)data;
  414. CURL_TRC_CF(data, cf, "destroy");
  415. cf_hc_reset(cf, data);
  416. Curl_safefree(ctx);
  417. }
  418. struct Curl_cftype Curl_cft_http_connect = {
  419. "HTTPS-CONNECT",
  420. 0,
  421. CURL_LOG_LVL_NONE,
  422. cf_hc_destroy,
  423. cf_hc_connect,
  424. cf_hc_close,
  425. cf_hc_shutdown,
  426. Curl_cf_def_get_host,
  427. cf_hc_adjust_pollset,
  428. cf_hc_data_pending,
  429. Curl_cf_def_send,
  430. Curl_cf_def_recv,
  431. Curl_cf_def_cntrl,
  432. Curl_cf_def_conn_is_alive,
  433. Curl_cf_def_conn_keep_alive,
  434. cf_hc_query,
  435. };
  436. static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
  437. struct Curl_easy *data,
  438. const struct Curl_dns_entry *remotehost,
  439. bool try_h3, bool try_h21)
  440. {
  441. struct Curl_cfilter *cf = NULL;
  442. struct cf_hc_ctx *ctx;
  443. CURLcode result = CURLE_OK;
  444. (void)data;
  445. ctx = calloc(1, sizeof(*ctx));
  446. if(!ctx) {
  447. result = CURLE_OUT_OF_MEMORY;
  448. goto out;
  449. }
  450. ctx->remotehost = remotehost;
  451. ctx->h3_baller.enabled = try_h3;
  452. ctx->h21_baller.enabled = try_h21;
  453. result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
  454. if(result)
  455. goto out;
  456. ctx = NULL;
  457. cf_hc_reset(cf, data);
  458. out:
  459. *pcf = result? NULL : cf;
  460. free(ctx);
  461. return result;
  462. }
  463. static CURLcode cf_http_connect_add(struct Curl_easy *data,
  464. struct connectdata *conn,
  465. int sockindex,
  466. const struct Curl_dns_entry *remotehost,
  467. bool try_h3, bool try_h21)
  468. {
  469. struct Curl_cfilter *cf;
  470. CURLcode result = CURLE_OK;
  471. DEBUGASSERT(data);
  472. result = cf_hc_create(&cf, data, remotehost, try_h3, try_h21);
  473. if(result)
  474. goto out;
  475. Curl_conn_cf_add(data, conn, sockindex, cf);
  476. out:
  477. return result;
  478. }
  479. CURLcode Curl_cf_https_setup(struct Curl_easy *data,
  480. struct connectdata *conn,
  481. int sockindex,
  482. const struct Curl_dns_entry *remotehost)
  483. {
  484. bool try_h3 = FALSE, try_h21 = TRUE; /* defaults, for now */
  485. CURLcode result = CURLE_OK;
  486. (void)sockindex;
  487. (void)remotehost;
  488. if(!conn->bits.tls_enable_alpn)
  489. goto out;
  490. if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
  491. result = Curl_conn_may_http3(data, conn);
  492. if(result) /* cannot do it */
  493. goto out;
  494. try_h3 = TRUE;
  495. try_h21 = FALSE;
  496. }
  497. else if(data->state.httpwant >= CURL_HTTP_VERSION_3) {
  498. /* We assume that silently not even trying H3 is ok here */
  499. /* TODO: should we fail instead? */
  500. try_h3 = (Curl_conn_may_http3(data, conn) == CURLE_OK);
  501. try_h21 = TRUE;
  502. }
  503. result = cf_http_connect_add(data, conn, sockindex, remotehost,
  504. try_h3, try_h21);
  505. out:
  506. return result;
  507. }
  508. #endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */