cf-https-connect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. 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. CURL_TRC_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. default:
  169. infof(data, "using HTTP/1.x");
  170. break;
  171. }
  172. ctx->state = CF_HC_SUCCESS;
  173. cf->connected = TRUE;
  174. Curl_conn_cf_cntrl(cf->next, data, TRUE,
  175. CF_CTRL_CONN_INFO_UPDATE, 0, NULL);
  176. return result;
  177. }
  178. static bool time_to_start_h21(struct Curl_cfilter *cf,
  179. struct Curl_easy *data,
  180. struct curltime now)
  181. {
  182. struct cf_hc_ctx *ctx = cf->ctx;
  183. timediff_t elapsed_ms;
  184. if(!ctx->h21_baller.enabled || cf_hc_baller_has_started(&ctx->h21_baller))
  185. return FALSE;
  186. if(!ctx->h3_baller.enabled || !cf_hc_baller_is_active(&ctx->h3_baller))
  187. return TRUE;
  188. elapsed_ms = Curl_timediff(now, ctx->started);
  189. if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
  190. CURL_TRC_CF(data, cf, "hard timeout of %dms reached, starting h21",
  191. ctx->hard_eyeballs_timeout_ms);
  192. return TRUE;
  193. }
  194. if(elapsed_ms >= ctx->soft_eyeballs_timeout_ms) {
  195. if(cf_hc_baller_reply_ms(&ctx->h3_baller, data) < 0) {
  196. CURL_TRC_CF(data, cf, "soft timeout of %dms reached, h3 has not "
  197. "seen any data, starting h21",
  198. ctx->soft_eyeballs_timeout_ms);
  199. return TRUE;
  200. }
  201. /* set the effective hard timeout again */
  202. Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
  203. EXPIRE_ALPN_EYEBALLS);
  204. }
  205. return FALSE;
  206. }
  207. static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
  208. struct Curl_easy *data,
  209. bool blocking, bool *done)
  210. {
  211. struct cf_hc_ctx *ctx = cf->ctx;
  212. struct curltime now;
  213. CURLcode result = CURLE_OK;
  214. (void)blocking;
  215. if(cf->connected) {
  216. *done = TRUE;
  217. return CURLE_OK;
  218. }
  219. *done = FALSE;
  220. now = Curl_now();
  221. switch(ctx->state) {
  222. case CF_HC_INIT:
  223. DEBUGASSERT(!ctx->h3_baller.cf);
  224. DEBUGASSERT(!ctx->h21_baller.cf);
  225. DEBUGASSERT(!cf->next);
  226. CURL_TRC_CF(data, cf, "connect, init");
  227. ctx->started = now;
  228. if(ctx->h3_baller.enabled) {
  229. cf_hc_baller_init(&ctx->h3_baller, cf, data, "h3", TRNSPRT_QUIC);
  230. if(ctx->h21_baller.enabled)
  231. Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
  232. }
  233. else if(ctx->h21_baller.enabled)
  234. cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
  235. cf->conn->transport);
  236. ctx->state = CF_HC_CONNECT;
  237. FALLTHROUGH();
  238. case CF_HC_CONNECT:
  239. if(cf_hc_baller_is_active(&ctx->h3_baller)) {
  240. result = cf_hc_baller_connect(&ctx->h3_baller, cf, data, done);
  241. if(!result && *done) {
  242. result = baller_connected(cf, data, &ctx->h3_baller);
  243. goto out;
  244. }
  245. }
  246. if(time_to_start_h21(cf, data, now)) {
  247. cf_hc_baller_init(&ctx->h21_baller, cf, data, "h21",
  248. cf->conn->transport);
  249. }
  250. if(cf_hc_baller_is_active(&ctx->h21_baller)) {
  251. CURL_TRC_CF(data, cf, "connect, check h21");
  252. result = cf_hc_baller_connect(&ctx->h21_baller, cf, data, done);
  253. if(!result && *done) {
  254. result = baller_connected(cf, data, &ctx->h21_baller);
  255. goto out;
  256. }
  257. }
  258. if((!ctx->h3_baller.enabled || ctx->h3_baller.result) &&
  259. (!ctx->h21_baller.enabled || ctx->h21_baller.result)) {
  260. /* both failed or disabled. we give up */
  261. CURL_TRC_CF(data, cf, "connect, all failed");
  262. result = ctx->result = ctx->h3_baller.enabled?
  263. ctx->h3_baller.result : ctx->h21_baller.result;
  264. ctx->state = CF_HC_FAILURE;
  265. goto out;
  266. }
  267. result = CURLE_OK;
  268. *done = FALSE;
  269. break;
  270. case CF_HC_FAILURE:
  271. result = ctx->result;
  272. cf->connected = FALSE;
  273. *done = FALSE;
  274. break;
  275. case CF_HC_SUCCESS:
  276. result = CURLE_OK;
  277. cf->connected = TRUE;
  278. *done = TRUE;
  279. break;
  280. }
  281. out:
  282. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  283. return result;
  284. }
  285. static void cf_hc_adjust_pollset(struct Curl_cfilter *cf,
  286. struct Curl_easy *data,
  287. struct easy_pollset *ps)
  288. {
  289. if(!cf->connected) {
  290. struct cf_hc_ctx *ctx = cf->ctx;
  291. struct cf_hc_baller *ballers[2];
  292. size_t i;
  293. ballers[0] = &ctx->h3_baller;
  294. ballers[1] = &ctx->h21_baller;
  295. for(i = 0; i < sizeof(ballers)/sizeof(ballers[0]); i++) {
  296. struct cf_hc_baller *b = ballers[i];
  297. if(!cf_hc_baller_is_active(b))
  298. continue;
  299. Curl_conn_cf_adjust_pollset(b->cf, data, ps);
  300. }
  301. CURL_TRC_CF(data, cf, "adjust_pollset -> %d socks", ps->num);
  302. }
  303. }
  304. static bool cf_hc_data_pending(struct Curl_cfilter *cf,
  305. const struct Curl_easy *data)
  306. {
  307. struct cf_hc_ctx *ctx = cf->ctx;
  308. if(cf->connected)
  309. return cf->next->cft->has_data_pending(cf->next, data);
  310. CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
  311. return cf_hc_baller_data_pending(&ctx->h3_baller, data)
  312. || cf_hc_baller_data_pending(&ctx->h21_baller, data);
  313. }
  314. static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
  315. struct Curl_easy *data,
  316. int query)
  317. {
  318. struct cf_hc_ctx *ctx = cf->ctx;
  319. struct Curl_cfilter *cfb;
  320. struct curltime t, tmax;
  321. memset(&tmax, 0, sizeof(tmax));
  322. memset(&t, 0, sizeof(t));
  323. cfb = ctx->h21_baller.enabled? ctx->h21_baller.cf : NULL;
  324. if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
  325. if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
  326. tmax = t;
  327. }
  328. memset(&t, 0, sizeof(t));
  329. cfb = ctx->h3_baller.enabled? ctx->h3_baller.cf : NULL;
  330. if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
  331. if((t.tv_sec || t.tv_usec) && Curl_timediff_us(t, tmax) > 0)
  332. tmax = t;
  333. }
  334. return tmax;
  335. }
  336. static CURLcode cf_hc_query(struct Curl_cfilter *cf,
  337. struct Curl_easy *data,
  338. int query, int *pres1, void *pres2)
  339. {
  340. if(!cf->connected) {
  341. switch(query) {
  342. case CF_QUERY_TIMER_CONNECT: {
  343. struct curltime *when = pres2;
  344. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
  345. return CURLE_OK;
  346. }
  347. case CF_QUERY_TIMER_APPCONNECT: {
  348. struct curltime *when = pres2;
  349. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
  350. return CURLE_OK;
  351. }
  352. default:
  353. break;
  354. }
  355. }
  356. return cf->next?
  357. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  358. CURLE_UNKNOWN_OPTION;
  359. }
  360. static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  361. {
  362. CURL_TRC_CF(data, cf, "close");
  363. cf_hc_reset(cf, data);
  364. cf->connected = FALSE;
  365. if(cf->next) {
  366. cf->next->cft->do_close(cf->next, data);
  367. Curl_conn_cf_discard_chain(&cf->next, data);
  368. }
  369. }
  370. static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  371. {
  372. struct cf_hc_ctx *ctx = cf->ctx;
  373. (void)data;
  374. CURL_TRC_CF(data, cf, "destroy");
  375. cf_hc_reset(cf, data);
  376. Curl_safefree(ctx);
  377. }
  378. struct Curl_cftype Curl_cft_http_connect = {
  379. "HTTPS-CONNECT",
  380. 0,
  381. CURL_LOG_LVL_NONE,
  382. cf_hc_destroy,
  383. cf_hc_connect,
  384. cf_hc_close,
  385. Curl_cf_def_get_host,
  386. cf_hc_adjust_pollset,
  387. cf_hc_data_pending,
  388. Curl_cf_def_send,
  389. Curl_cf_def_recv,
  390. Curl_cf_def_cntrl,
  391. Curl_cf_def_conn_is_alive,
  392. Curl_cf_def_conn_keep_alive,
  393. cf_hc_query,
  394. };
  395. static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
  396. struct Curl_easy *data,
  397. const struct Curl_dns_entry *remotehost,
  398. bool try_h3, bool try_h21)
  399. {
  400. struct Curl_cfilter *cf = NULL;
  401. struct cf_hc_ctx *ctx;
  402. CURLcode result = CURLE_OK;
  403. (void)data;
  404. ctx = calloc(1, sizeof(*ctx));
  405. if(!ctx) {
  406. result = CURLE_OUT_OF_MEMORY;
  407. goto out;
  408. }
  409. ctx->remotehost = remotehost;
  410. ctx->h3_baller.enabled = try_h3;
  411. ctx->h21_baller.enabled = try_h21;
  412. result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
  413. if(result)
  414. goto out;
  415. ctx = NULL;
  416. cf_hc_reset(cf, data);
  417. out:
  418. *pcf = result? NULL : cf;
  419. free(ctx);
  420. return result;
  421. }
  422. static CURLcode cf_http_connect_add(struct Curl_easy *data,
  423. struct connectdata *conn,
  424. int sockindex,
  425. const struct Curl_dns_entry *remotehost,
  426. bool try_h3, bool try_h21)
  427. {
  428. struct Curl_cfilter *cf;
  429. CURLcode result = CURLE_OK;
  430. DEBUGASSERT(data);
  431. result = cf_hc_create(&cf, data, remotehost, try_h3, try_h21);
  432. if(result)
  433. goto out;
  434. Curl_conn_cf_add(data, conn, sockindex, cf);
  435. out:
  436. return result;
  437. }
  438. CURLcode Curl_cf_https_setup(struct Curl_easy *data,
  439. struct connectdata *conn,
  440. int sockindex,
  441. const struct Curl_dns_entry *remotehost)
  442. {
  443. bool try_h3 = FALSE, try_h21 = TRUE; /* defaults, for now */
  444. CURLcode result = CURLE_OK;
  445. (void)sockindex;
  446. (void)remotehost;
  447. if(!conn->bits.tls_enable_alpn)
  448. goto out;
  449. if(data->state.httpwant == CURL_HTTP_VERSION_3ONLY) {
  450. result = Curl_conn_may_http3(data, conn);
  451. if(result) /* can't do it */
  452. goto out;
  453. try_h3 = TRUE;
  454. try_h21 = FALSE;
  455. }
  456. else if(data->state.httpwant >= CURL_HTTP_VERSION_3) {
  457. /* We assume that silently not even trying H3 is ok here */
  458. /* TODO: should we fail instead? */
  459. try_h3 = (Curl_conn_may_http3(data, conn) == CURLE_OK);
  460. try_h21 = TRUE;
  461. }
  462. result = cf_http_connect_add(data, conn, sockindex, remotehost,
  463. try_h3, try_h21);
  464. out:
  465. return result;
  466. }
  467. #endif /* !defined(CURL_DISABLE_HTTP) && !defined(USE_HYPER) */