cf-https-connect.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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. #ifndef CURL_DISABLE_HTTP
  26. #include "urldata.h"
  27. #include "curl_trc.h"
  28. #include "cfilters.h"
  29. #include "connect.h"
  30. #include "hostip.h"
  31. #include "multiif.h"
  32. #include "cf-https-connect.h"
  33. #include "http2.h"
  34. #include "progress.h"
  35. #include "select.h"
  36. #include "vquic/vquic.h"
  37. typedef enum {
  38. CF_HC_INIT,
  39. CF_HC_CONNECT,
  40. CF_HC_SUCCESS,
  41. CF_HC_FAILURE
  42. } cf_hc_state;
  43. struct cf_hc_baller {
  44. const char *name;
  45. struct Curl_cfilter *cf;
  46. CURLcode result;
  47. struct curltime started;
  48. int reply_ms;
  49. uint8_t transport;
  50. enum alpnid alpn_id;
  51. BIT(shutdown);
  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->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->cf && (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. static bool cf_hc_baller_needs_flush(struct cf_hc_baller *b,
  86. struct Curl_easy *data)
  87. {
  88. return b->cf && !b->result && Curl_conn_cf_needs_flush(b->cf, data);
  89. }
  90. static CURLcode cf_hc_baller_cntrl(struct cf_hc_baller *b,
  91. struct Curl_easy *data,
  92. int event, int arg1, void *arg2)
  93. {
  94. if(b->cf && !b->result)
  95. return Curl_conn_cf_cntrl(b->cf, data, FALSE, event, arg1, arg2);
  96. return CURLE_OK;
  97. }
  98. struct cf_hc_ctx {
  99. cf_hc_state state;
  100. struct curltime started; /* when connect started */
  101. CURLcode result; /* overall result */
  102. struct cf_hc_baller ballers[2];
  103. size_t baller_count;
  104. timediff_t soft_eyeballs_timeout_ms;
  105. timediff_t hard_eyeballs_timeout_ms;
  106. };
  107. static void cf_hc_baller_assign(struct cf_hc_baller *b,
  108. enum alpnid alpn_id,
  109. uint8_t def_transport)
  110. {
  111. b->alpn_id = alpn_id;
  112. b->transport = def_transport;
  113. switch(b->alpn_id) {
  114. case ALPN_h3:
  115. b->name = "h3";
  116. b->transport = TRNSPRT_QUIC;
  117. break;
  118. case ALPN_h2:
  119. b->name = "h2";
  120. break;
  121. case ALPN_h1:
  122. b->name = "h1";
  123. break;
  124. default:
  125. b->result = CURLE_FAILED_INIT;
  126. break;
  127. }
  128. }
  129. static void cf_hc_baller_init(struct cf_hc_baller *b,
  130. struct Curl_cfilter *cf,
  131. struct Curl_easy *data,
  132. uint8_t transport)
  133. {
  134. struct Curl_cfilter *save = cf->next;
  135. cf->next = NULL;
  136. b->started = *Curl_pgrs_now(data);
  137. switch(b->alpn_id) {
  138. case ALPN_h3:
  139. transport = TRNSPRT_QUIC;
  140. break;
  141. default:
  142. break;
  143. }
  144. if(!b->result)
  145. b->result = Curl_cf_setup_insert_after(cf, data, transport,
  146. CURL_CF_SSL_ENABLE);
  147. b->cf = cf->next;
  148. cf->next = save;
  149. }
  150. static CURLcode cf_hc_baller_connect(struct cf_hc_baller *b,
  151. struct Curl_cfilter *cf,
  152. struct Curl_easy *data,
  153. bool *done)
  154. {
  155. struct Curl_cfilter *save = cf->next;
  156. cf->next = b->cf;
  157. b->result = Curl_conn_cf_connect(cf->next, data, done);
  158. b->cf = cf->next; /* it might mutate */
  159. cf->next = save;
  160. return b->result;
  161. }
  162. static void cf_hc_reset(struct Curl_cfilter *cf, struct Curl_easy *data)
  163. {
  164. struct cf_hc_ctx *ctx = cf->ctx;
  165. size_t i;
  166. if(ctx) {
  167. for(i = 0; i < ctx->baller_count; ++i)
  168. cf_hc_baller_reset(&ctx->ballers[i], data);
  169. ctx->state = CF_HC_INIT;
  170. ctx->result = CURLE_OK;
  171. ctx->hard_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout;
  172. ctx->soft_eyeballs_timeout_ms = data->set.happy_eyeballs_timeout / 4;
  173. }
  174. }
  175. static CURLcode baller_connected(struct Curl_cfilter *cf,
  176. struct Curl_easy *data,
  177. struct cf_hc_baller *winner)
  178. {
  179. struct cf_hc_ctx *ctx = cf->ctx;
  180. CURLcode result = CURLE_OK;
  181. int reply_ms;
  182. size_t i;
  183. DEBUGASSERT(winner->cf);
  184. for(i = 0; i < ctx->baller_count; ++i)
  185. if(winner != &ctx->ballers[i])
  186. cf_hc_baller_reset(&ctx->ballers[i], data);
  187. reply_ms = cf_hc_baller_reply_ms(winner, data);
  188. if(reply_ms >= 0)
  189. CURL_TRC_CF(data, cf, "connect+handshake %s: %dms, 1st data: %dms",
  190. winner->name,
  191. (int)curlx_ptimediff_ms(Curl_pgrs_now(data),
  192. &winner->started), reply_ms);
  193. else
  194. CURL_TRC_CF(data, cf, "deferred handshake %s: %dms",
  195. winner->name, (int)curlx_ptimediff_ms(Curl_pgrs_now(data),
  196. &winner->started));
  197. /* install the winning filter below this one. */
  198. cf->next = winner->cf;
  199. winner->cf = NULL;
  200. #ifdef USE_NGHTTP2
  201. {
  202. /* Using nghttp2, we add the filter "below" us, so when the conn
  203. * closes, we tear it down for a fresh reconnect */
  204. const char *alpn = Curl_conn_cf_get_alpn_negotiated(cf->next, data);
  205. if(alpn && !strcmp("h2", alpn)) {
  206. result = Curl_http2_switch_at(cf, data);
  207. if(result) {
  208. ctx->state = CF_HC_FAILURE;
  209. ctx->result = result;
  210. return result;
  211. }
  212. }
  213. }
  214. #endif
  215. ctx->state = CF_HC_SUCCESS;
  216. cf->connected = TRUE;
  217. return result;
  218. }
  219. static bool time_to_start_next(struct Curl_cfilter *cf,
  220. struct Curl_easy *data,
  221. size_t idx, struct curltime now)
  222. {
  223. struct cf_hc_ctx *ctx = cf->ctx;
  224. timediff_t elapsed_ms;
  225. size_t i;
  226. if(idx >= ctx->baller_count)
  227. return FALSE;
  228. if(cf_hc_baller_has_started(&ctx->ballers[idx]))
  229. return FALSE;
  230. for(i = 0; i < idx; i++) {
  231. if(!ctx->ballers[i].result)
  232. break;
  233. }
  234. if(i == idx) {
  235. CURL_TRC_CF(data, cf, "all previous attempts failed, starting %s",
  236. ctx->ballers[idx].name);
  237. return TRUE;
  238. }
  239. elapsed_ms = curlx_ptimediff_ms(&now, &ctx->started);
  240. if(elapsed_ms >= ctx->hard_eyeballs_timeout_ms) {
  241. CURL_TRC_CF(data, cf, "hard timeout of %" FMT_TIMEDIFF_T "ms reached, "
  242. "starting %s",
  243. ctx->hard_eyeballs_timeout_ms, ctx->ballers[idx].name);
  244. return TRUE;
  245. }
  246. if((idx > 0) && (elapsed_ms >= ctx->soft_eyeballs_timeout_ms)) {
  247. if(cf_hc_baller_reply_ms(&ctx->ballers[idx - 1], data) < 0) {
  248. CURL_TRC_CF(data, cf, "soft timeout of %" FMT_TIMEDIFF_T "ms reached, "
  249. "%s has not seen any data, starting %s",
  250. ctx->soft_eyeballs_timeout_ms,
  251. ctx->ballers[idx - 1].name, ctx->ballers[idx].name);
  252. return TRUE;
  253. }
  254. /* set the effective hard timeout again */
  255. Curl_expire(data, ctx->hard_eyeballs_timeout_ms - elapsed_ms,
  256. EXPIRE_ALPN_EYEBALLS);
  257. }
  258. return FALSE;
  259. }
  260. static CURLcode cf_hc_connect(struct Curl_cfilter *cf,
  261. struct Curl_easy *data,
  262. bool *done)
  263. {
  264. struct cf_hc_ctx *ctx = cf->ctx;
  265. CURLcode result = CURLE_OK;
  266. size_t i, failed_ballers;
  267. if(cf->connected) {
  268. *done = TRUE;
  269. return CURLE_OK;
  270. }
  271. *done = FALSE;
  272. switch(ctx->state) {
  273. case CF_HC_INIT:
  274. DEBUGASSERT(!cf->next);
  275. for(i = 0; i < ctx->baller_count; i++)
  276. DEBUGASSERT(!ctx->ballers[i].cf);
  277. CURL_TRC_CF(data, cf, "connect, init");
  278. ctx->started = *Curl_pgrs_now(data);
  279. cf_hc_baller_init(&ctx->ballers[0], cf, data, ctx->ballers[0].transport);
  280. if(ctx->baller_count > 1) {
  281. Curl_expire(data, ctx->soft_eyeballs_timeout_ms, EXPIRE_ALPN_EYEBALLS);
  282. CURL_TRC_CF(data, cf, "set next attempt to start in %" FMT_TIMEDIFF_T
  283. "ms", ctx->soft_eyeballs_timeout_ms);
  284. }
  285. ctx->state = CF_HC_CONNECT;
  286. FALLTHROUGH();
  287. case CF_HC_CONNECT:
  288. if(cf_hc_baller_is_active(&ctx->ballers[0])) {
  289. result = cf_hc_baller_connect(&ctx->ballers[0], cf, data, done);
  290. if(!result && *done) {
  291. result = baller_connected(cf, data, &ctx->ballers[0]);
  292. goto out;
  293. }
  294. }
  295. if(time_to_start_next(cf, data, 1, *Curl_pgrs_now(data))) {
  296. cf_hc_baller_init(&ctx->ballers[1], cf, data, ctx->ballers[1].transport);
  297. }
  298. if((ctx->baller_count > 1) && cf_hc_baller_is_active(&ctx->ballers[1])) {
  299. CURL_TRC_CF(data, cf, "connect, check %s", ctx->ballers[1].name);
  300. result = cf_hc_baller_connect(&ctx->ballers[1], cf, data, done);
  301. if(!result && *done) {
  302. result = baller_connected(cf, data, &ctx->ballers[1]);
  303. goto out;
  304. }
  305. }
  306. failed_ballers = 0;
  307. for(i = 0; i < ctx->baller_count; i++) {
  308. if(ctx->ballers[i].result)
  309. ++failed_ballers;
  310. }
  311. if(failed_ballers == ctx->baller_count) {
  312. /* all have failed. we give up */
  313. CURL_TRC_CF(data, cf, "connect, all attempts failed");
  314. for(i = 0; i < ctx->baller_count; i++) {
  315. if(ctx->ballers[i].result) {
  316. result = ctx->ballers[i].result;
  317. break;
  318. }
  319. }
  320. ctx->state = CF_HC_FAILURE;
  321. goto out;
  322. }
  323. result = CURLE_OK;
  324. *done = FALSE;
  325. break;
  326. case CF_HC_FAILURE:
  327. result = ctx->result;
  328. cf->connected = FALSE;
  329. *done = FALSE;
  330. break;
  331. case CF_HC_SUCCESS:
  332. result = CURLE_OK;
  333. cf->connected = TRUE;
  334. *done = TRUE;
  335. break;
  336. }
  337. out:
  338. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  339. return result;
  340. }
  341. static CURLcode cf_hc_shutdown(struct Curl_cfilter *cf,
  342. struct Curl_easy *data, bool *done)
  343. {
  344. struct cf_hc_ctx *ctx = cf->ctx;
  345. size_t i;
  346. CURLcode result = CURLE_OK;
  347. DEBUGASSERT(data);
  348. if(cf->connected) {
  349. *done = TRUE;
  350. return CURLE_OK;
  351. }
  352. /* shutdown all ballers that have not done so already. If one fails,
  353. * continue shutting down others until all are shutdown. */
  354. for(i = 0; i < ctx->baller_count; i++) {
  355. struct cf_hc_baller *b = &ctx->ballers[i];
  356. bool bdone = FALSE;
  357. if(!cf_hc_baller_is_active(b) || b->shutdown)
  358. continue;
  359. b->result = b->cf->cft->do_shutdown(b->cf, data, &bdone);
  360. if(b->result || bdone)
  361. b->shutdown = TRUE; /* treat a failed shutdown as done */
  362. }
  363. *done = TRUE;
  364. for(i = 0; i < ctx->baller_count; i++) {
  365. if(!ctx->ballers[i].shutdown)
  366. *done = FALSE;
  367. }
  368. if(*done) {
  369. for(i = 0; i < ctx->baller_count; i++) {
  370. if(ctx->ballers[i].result)
  371. result = ctx->ballers[i].result;
  372. }
  373. }
  374. CURL_TRC_CF(data, cf, "shutdown -> %d, done=%d", result, *done);
  375. return result;
  376. }
  377. static CURLcode cf_hc_adjust_pollset(struct Curl_cfilter *cf,
  378. struct Curl_easy *data,
  379. struct easy_pollset *ps)
  380. {
  381. CURLcode result = CURLE_OK;
  382. if(!cf->connected) {
  383. struct cf_hc_ctx *ctx = cf->ctx;
  384. size_t i;
  385. for(i = 0; (i < ctx->baller_count) && !result; i++) {
  386. struct cf_hc_baller *b = &ctx->ballers[i];
  387. if(!cf_hc_baller_is_active(b))
  388. continue;
  389. result = Curl_conn_cf_adjust_pollset(b->cf, data, ps);
  390. }
  391. CURL_TRC_CF(data, cf, "adjust_pollset -> %d, %d socks", result, ps->n);
  392. }
  393. return result;
  394. }
  395. static bool cf_hc_data_pending(struct Curl_cfilter *cf,
  396. const struct Curl_easy *data)
  397. {
  398. struct cf_hc_ctx *ctx = cf->ctx;
  399. size_t i;
  400. if(cf->connected)
  401. return cf->next->cft->has_data_pending(cf->next, data);
  402. for(i = 0; i < ctx->baller_count; i++)
  403. if(cf_hc_baller_data_pending(&ctx->ballers[i], data))
  404. return TRUE;
  405. return FALSE;
  406. }
  407. static struct curltime cf_get_max_baller_time(struct Curl_cfilter *cf,
  408. struct Curl_easy *data,
  409. int query)
  410. {
  411. struct cf_hc_ctx *ctx = cf->ctx;
  412. struct curltime t, tmax;
  413. size_t i;
  414. memset(&tmax, 0, sizeof(tmax));
  415. for(i = 0; i < ctx->baller_count; i++) {
  416. struct Curl_cfilter *cfb = ctx->ballers[i].cf;
  417. memset(&t, 0, sizeof(t));
  418. if(cfb && !cfb->cft->query(cfb, data, query, NULL, &t)) {
  419. if((t.tv_sec || t.tv_usec) && curlx_ptimediff_us(&t, &tmax) > 0)
  420. tmax = t;
  421. }
  422. }
  423. return tmax;
  424. }
  425. static CURLcode cf_hc_query(struct Curl_cfilter *cf,
  426. struct Curl_easy *data,
  427. int query, int *pres1, void *pres2)
  428. {
  429. struct cf_hc_ctx *ctx = cf->ctx;
  430. size_t i;
  431. if(!cf->connected) {
  432. switch(query) {
  433. case CF_QUERY_TIMER_CONNECT: {
  434. struct curltime *when = pres2;
  435. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_CONNECT);
  436. return CURLE_OK;
  437. }
  438. case CF_QUERY_TIMER_APPCONNECT: {
  439. struct curltime *when = pres2;
  440. *when = cf_get_max_baller_time(cf, data, CF_QUERY_TIMER_APPCONNECT);
  441. return CURLE_OK;
  442. }
  443. case CF_QUERY_NEED_FLUSH: {
  444. for(i = 0; i < ctx->baller_count; i++)
  445. if(cf_hc_baller_needs_flush(&ctx->ballers[i], data)) {
  446. *pres1 = TRUE;
  447. return CURLE_OK;
  448. }
  449. break;
  450. }
  451. default:
  452. break;
  453. }
  454. }
  455. return cf->next ?
  456. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  457. CURLE_UNKNOWN_OPTION;
  458. }
  459. static CURLcode cf_hc_cntrl(struct Curl_cfilter *cf,
  460. struct Curl_easy *data,
  461. int event, int arg1, void *arg2)
  462. {
  463. struct cf_hc_ctx *ctx = cf->ctx;
  464. CURLcode result = CURLE_OK;
  465. size_t i;
  466. if(!cf->connected) {
  467. for(i = 0; i < ctx->baller_count; i++) {
  468. result = cf_hc_baller_cntrl(&ctx->ballers[i], data, event, arg1, arg2);
  469. if(result && (result != CURLE_AGAIN))
  470. goto out;
  471. }
  472. result = CURLE_OK;
  473. }
  474. out:
  475. return result;
  476. }
  477. static void cf_hc_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  478. {
  479. CURL_TRC_CF(data, cf, "close");
  480. cf_hc_reset(cf, data);
  481. cf->connected = FALSE;
  482. if(cf->next) {
  483. cf->next->cft->do_close(cf->next, data);
  484. Curl_conn_cf_discard_chain(&cf->next, data);
  485. }
  486. }
  487. static void cf_hc_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  488. {
  489. struct cf_hc_ctx *ctx = cf->ctx;
  490. (void)data;
  491. CURL_TRC_CF(data, cf, "destroy");
  492. cf_hc_reset(cf, data);
  493. Curl_safefree(ctx);
  494. }
  495. struct Curl_cftype Curl_cft_http_connect = {
  496. "HTTPS-CONNECT",
  497. 0,
  498. CURL_LOG_LVL_NONE,
  499. cf_hc_destroy,
  500. cf_hc_connect,
  501. cf_hc_close,
  502. cf_hc_shutdown,
  503. cf_hc_adjust_pollset,
  504. cf_hc_data_pending,
  505. Curl_cf_def_send,
  506. Curl_cf_def_recv,
  507. cf_hc_cntrl,
  508. Curl_cf_def_conn_is_alive,
  509. Curl_cf_def_conn_keep_alive,
  510. cf_hc_query,
  511. };
  512. static CURLcode cf_hc_create(struct Curl_cfilter **pcf,
  513. struct Curl_easy *data,
  514. enum alpnid *alpnids, size_t alpn_count,
  515. uint8_t def_transport)
  516. {
  517. struct Curl_cfilter *cf = NULL;
  518. struct cf_hc_ctx *ctx;
  519. CURLcode result = CURLE_OK;
  520. size_t i;
  521. ctx = curlx_calloc(1, sizeof(*ctx));
  522. if(!ctx) {
  523. result = CURLE_OUT_OF_MEMORY;
  524. goto out;
  525. }
  526. DEBUGASSERT(alpnids);
  527. DEBUGASSERT(alpn_count);
  528. DEBUGASSERT(alpn_count <= CURL_ARRAYSIZE(ctx->ballers));
  529. if(!alpn_count || (alpn_count > CURL_ARRAYSIZE(ctx->ballers))) {
  530. failf(data, "https-connect filter create with unsupported %zu ALPN ids",
  531. alpn_count);
  532. result = CURLE_FAILED_INIT;
  533. goto out;
  534. }
  535. for(i = 0; i < alpn_count; ++i)
  536. cf_hc_baller_assign(&ctx->ballers[i], alpnids[i], def_transport);
  537. for(; i < CURL_ARRAYSIZE(ctx->ballers); ++i)
  538. ctx->ballers[i].alpn_id = ALPN_none;
  539. ctx->baller_count = alpn_count;
  540. result = Curl_cf_create(&cf, &Curl_cft_http_connect, ctx);
  541. if(result)
  542. goto out;
  543. ctx = NULL;
  544. cf_hc_reset(cf, data);
  545. out:
  546. *pcf = result ? NULL : cf;
  547. curlx_free(ctx);
  548. return result;
  549. }
  550. static CURLcode cf_http_connect_add(struct Curl_easy *data,
  551. struct connectdata *conn,
  552. int sockindex,
  553. enum alpnid *alpn_ids, size_t alpn_count,
  554. uint8_t def_transport)
  555. {
  556. struct Curl_cfilter *cf;
  557. CURLcode result = CURLE_OK;
  558. DEBUGASSERT(data);
  559. result = cf_hc_create(&cf, data, alpn_ids, alpn_count, def_transport);
  560. if(result)
  561. goto out;
  562. Curl_conn_cf_add(data, conn, sockindex, cf);
  563. out:
  564. return result;
  565. }
  566. static bool cf_https_alpns_contain(enum alpnid id,
  567. enum alpnid *list, size_t len)
  568. {
  569. size_t i;
  570. for(i = 0; i < len; ++i) {
  571. if(id == list[i])
  572. return TRUE;
  573. }
  574. return FALSE;
  575. }
  576. CURLcode Curl_cf_https_setup(struct Curl_easy *data,
  577. struct connectdata *conn,
  578. int sockindex)
  579. {
  580. enum alpnid alpn_ids[2];
  581. size_t alpn_count = 0;
  582. CURLcode result = CURLE_OK;
  583. struct Curl_cfilter cf_fake, *cf = NULL;
  584. (void)sockindex;
  585. /* we want to log for the filter before we create it, fake it. */
  586. memset(&cf_fake, 0, sizeof(cf_fake));
  587. cf_fake.cft = &Curl_cft_http_connect;
  588. cf = &cf_fake;
  589. if(conn->bits.tls_enable_alpn) {
  590. #ifdef USE_HTTPSRR
  591. /* Is there an HTTPSRR use its ALPNs here.
  592. * We are here after having selected a connection to a host+port and
  593. * can no longer change that. Any HTTPSRR advice for other hosts and ports
  594. * we need to ignore. */
  595. struct Curl_dns_entry *dns = data->state.dns[sockindex];
  596. struct Curl_https_rrinfo *rr = dns ? dns->hinfo : NULL;
  597. if(rr && !rr->no_def_alpn && /* ALPNs are defaults */
  598. (!rr->target || /* for same host */
  599. !rr->target[0] ||
  600. (rr->target[0] == '.' &&
  601. !rr->target[1])) &&
  602. (rr->port < 0 || /* for same port */
  603. rr->port == conn->remote_port)) {
  604. size_t i;
  605. for(i = 0; i < CURL_ARRAYSIZE(rr->alpns) &&
  606. alpn_count < CURL_ARRAYSIZE(alpn_ids); ++i) {
  607. enum alpnid alpn = rr->alpns[i];
  608. if(cf_https_alpns_contain(alpn, alpn_ids, alpn_count))
  609. continue;
  610. switch(alpn) {
  611. case ALPN_h3:
  612. if(Curl_conn_may_http3(data, conn, conn->transport_wanted))
  613. break; /* not possible */
  614. if(data->state.http_neg.allowed & CURL_HTTP_V3x) {
  615. CURL_TRC_CF(data, cf, "adding h3 via HTTPS-RR");
  616. alpn_ids[alpn_count++] = alpn;
  617. }
  618. break;
  619. case ALPN_h2:
  620. if(data->state.http_neg.allowed & CURL_HTTP_V2x) {
  621. CURL_TRC_CF(data, cf, "adding h2 via HTTPS-RR");
  622. alpn_ids[alpn_count++] = alpn;
  623. }
  624. break;
  625. case ALPN_h1:
  626. if(data->state.http_neg.allowed & CURL_HTTP_V1x) {
  627. CURL_TRC_CF(data, cf, "adding h1 via HTTPS-RR");
  628. alpn_ids[alpn_count++] = alpn;
  629. }
  630. break;
  631. default: /* ignore */
  632. break;
  633. }
  634. }
  635. }
  636. #endif
  637. /* Add preferred HTTP version ALPN first */
  638. if(data->state.http_neg.preferred &&
  639. (alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
  640. (data->state.http_neg.preferred & data->state.http_neg.allowed)) {
  641. enum alpnid alpn_pref = ALPN_none;
  642. switch(data->state.http_neg.preferred) {
  643. case CURL_HTTP_V3x:
  644. if(!Curl_conn_may_http3(data, conn, conn->transport_wanted))
  645. alpn_pref = ALPN_h3;
  646. break;
  647. case CURL_HTTP_V2x:
  648. alpn_pref = ALPN_h2;
  649. break;
  650. case CURL_HTTP_V1x:
  651. alpn_pref = ALPN_h1;
  652. break;
  653. default:
  654. break;
  655. }
  656. if(alpn_pref &&
  657. !cf_https_alpns_contain(alpn_pref, alpn_ids, alpn_count)) {
  658. alpn_ids[alpn_count++] = alpn_pref;
  659. }
  660. }
  661. if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
  662. (data->state.http_neg.wanted & CURL_HTTP_V3x) &&
  663. !cf_https_alpns_contain(ALPN_h3, alpn_ids, alpn_count)) {
  664. result = Curl_conn_may_http3(data, conn, conn->transport_wanted);
  665. if(!result) {
  666. CURL_TRC_CF(data, cf, "adding wanted h3");
  667. alpn_ids[alpn_count++] = ALPN_h3;
  668. }
  669. else if(data->state.http_neg.wanted == CURL_HTTP_V3x)
  670. goto out; /* only h3 allowed, not possible, error out */
  671. }
  672. if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
  673. (data->state.http_neg.wanted & CURL_HTTP_V2x) &&
  674. !cf_https_alpns_contain(ALPN_h2, alpn_ids, alpn_count)) {
  675. CURL_TRC_CF(data, cf, "adding wanted h2");
  676. alpn_ids[alpn_count++] = ALPN_h2;
  677. }
  678. else if((alpn_count < CURL_ARRAYSIZE(alpn_ids)) &&
  679. (data->state.http_neg.wanted & CURL_HTTP_V1x) &&
  680. !cf_https_alpns_contain(ALPN_h1, alpn_ids, alpn_count)) {
  681. CURL_TRC_CF(data, cf, "adding wanted h1");
  682. alpn_ids[alpn_count++] = ALPN_h1;
  683. }
  684. }
  685. /* If we identified ALPNs to use, install our filter. Otherwise,
  686. * install nothing, so our call will use a default connect setup. */
  687. if(alpn_count) {
  688. result = cf_http_connect_add(data, conn, sockindex,
  689. alpn_ids, alpn_count,
  690. conn->transport_wanted);
  691. }
  692. out:
  693. return result;
  694. }
  695. #endif /* !CURL_DISABLE_HTTP */