2
0

cf-https-connect.c 16 KB

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