2
0

curl_quiche.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  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 USE_QUICHE
  26. #include <quiche.h>
  27. #include <openssl/err.h>
  28. #include <openssl/ssl.h>
  29. #include "bufq.h"
  30. #include "hash.h"
  31. #include "urldata.h"
  32. #include "cfilters.h"
  33. #include "cf-socket.h"
  34. #include "sendf.h"
  35. #include "strdup.h"
  36. #include "rand.h"
  37. #include "strcase.h"
  38. #include "multiif.h"
  39. #include "connect.h"
  40. #include "progress.h"
  41. #include "strerror.h"
  42. #include "http1.h"
  43. #include "vquic.h"
  44. #include "vquic_int.h"
  45. #include "vquic-tls.h"
  46. #include "curl_quiche.h"
  47. #include "transfer.h"
  48. #include "inet_pton.h"
  49. #include "vtls/openssl.h"
  50. #include "vtls/keylog.h"
  51. #include "vtls/vtls.h"
  52. /* The last 3 #include files should be in this order */
  53. #include "curl_printf.h"
  54. #include "curl_memory.h"
  55. #include "memdebug.h"
  56. /* HTTP/3 error values defined in RFC 9114, ch. 8.1 */
  57. #define CURL_H3_NO_ERROR (0x0100)
  58. #define QUIC_MAX_STREAMS (100)
  59. #define H3_STREAM_WINDOW_SIZE (128 * 1024)
  60. #define H3_STREAM_CHUNK_SIZE (16 * 1024)
  61. /* The pool keeps spares around and half of a full stream windows
  62. * seems good. More does not seem to improve performance.
  63. * The benefit of the pool is that stream buffer to not keep
  64. * spares. So memory consumption goes down when streams run empty,
  65. * have a large upload done, etc. */
  66. #define H3_STREAM_POOL_SPARES \
  67. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
  68. /* Receive and Send max number of chunks just follows from the
  69. * chunk size and window size */
  70. #define H3_STREAM_RECV_CHUNKS \
  71. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  72. #define H3_STREAM_SEND_CHUNKS \
  73. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  74. /*
  75. * Store quiche version info in this buffer.
  76. */
  77. void Curl_quiche_ver(char *p, size_t len)
  78. {
  79. (void)msnprintf(p, len, "quiche/%s", quiche_version());
  80. }
  81. struct cf_quiche_ctx {
  82. struct cf_quic_ctx q;
  83. struct ssl_peer peer;
  84. struct curl_tls_ctx tls;
  85. quiche_conn *qconn;
  86. quiche_config *cfg;
  87. quiche_h3_conn *h3c;
  88. quiche_h3_config *h3config;
  89. uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
  90. struct curltime started_at; /* time the current attempt started */
  91. struct curltime handshake_at; /* time connect handshake finished */
  92. struct curltime reconnect_at; /* time the next attempt should start */
  93. struct bufc_pool stream_bufcp; /* chunk pool for streams */
  94. struct Curl_hash streams; /* hash `data->id` to `stream_ctx` */
  95. curl_off_t data_recvd;
  96. BIT(goaway); /* got GOAWAY from server */
  97. BIT(x509_store_setup); /* if x509 store has been set up */
  98. };
  99. #ifdef DEBUG_QUICHE
  100. static void quiche_debug_log(const char *line, void *argp)
  101. {
  102. (void)argp;
  103. fprintf(stderr, "%s\n", line);
  104. }
  105. #endif
  106. static void cf_quiche_ctx_clear(struct cf_quiche_ctx *ctx)
  107. {
  108. if(ctx) {
  109. if(ctx->h3c)
  110. quiche_h3_conn_free(ctx->h3c);
  111. if(ctx->h3config)
  112. quiche_h3_config_free(ctx->h3config);
  113. if(ctx->qconn)
  114. quiche_conn_free(ctx->qconn);
  115. if(ctx->cfg)
  116. quiche_config_free(ctx->cfg);
  117. /* quiche just freed it */
  118. ctx->tls.ossl.ssl = NULL;
  119. Curl_vquic_tls_cleanup(&ctx->tls);
  120. Curl_ssl_peer_cleanup(&ctx->peer);
  121. vquic_ctx_free(&ctx->q);
  122. Curl_bufcp_free(&ctx->stream_bufcp);
  123. Curl_hash_clean(&ctx->streams);
  124. Curl_hash_destroy(&ctx->streams);
  125. memset(ctx, 0, sizeof(*ctx));
  126. }
  127. }
  128. static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
  129. struct Curl_easy *data);
  130. /**
  131. * All about the H3 internals of a stream
  132. */
  133. struct stream_ctx {
  134. curl_uint64_t id; /* HTTP/3 protocol stream identifier */
  135. struct bufq recvbuf; /* h3 response */
  136. struct h1_req_parser h1; /* h1 request parsing */
  137. curl_uint64_t error3; /* HTTP/3 stream error code */
  138. curl_off_t upload_left; /* number of request bytes left to upload */
  139. BIT(opened); /* TRUE after stream has been opened */
  140. BIT(closed); /* TRUE on stream close */
  141. BIT(reset); /* TRUE on stream reset */
  142. BIT(send_closed); /* stream is locally closed */
  143. BIT(resp_hds_complete); /* final response has been received */
  144. BIT(resp_got_header); /* TRUE when h3 stream has recvd some HEADER */
  145. BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
  146. };
  147. #define H3_STREAM_CTX(ctx,data) ((struct stream_ctx *)(\
  148. data? Curl_hash_offt_get(&(ctx)->streams, (data)->id) : NULL))
  149. static void h3_stream_ctx_free(struct stream_ctx *stream)
  150. {
  151. Curl_bufq_free(&stream->recvbuf);
  152. Curl_h1_req_parse_free(&stream->h1);
  153. free(stream);
  154. }
  155. static void h3_stream_hash_free(void *stream)
  156. {
  157. DEBUGASSERT(stream);
  158. h3_stream_ctx_free((struct stream_ctx *)stream);
  159. }
  160. static void check_resumes(struct Curl_cfilter *cf,
  161. struct Curl_easy *data)
  162. {
  163. struct cf_quiche_ctx *ctx = cf->ctx;
  164. struct Curl_easy *sdata;
  165. struct stream_ctx *stream;
  166. DEBUGASSERT(data->multi);
  167. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  168. if(sdata->conn == data->conn) {
  169. stream = H3_STREAM_CTX(ctx, sdata);
  170. if(stream && stream->quic_flow_blocked) {
  171. stream->quic_flow_blocked = FALSE;
  172. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  173. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] unblock", stream->id);
  174. }
  175. }
  176. }
  177. }
  178. static CURLcode h3_data_setup(struct Curl_cfilter *cf,
  179. struct Curl_easy *data)
  180. {
  181. struct cf_quiche_ctx *ctx = cf->ctx;
  182. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  183. if(stream)
  184. return CURLE_OK;
  185. stream = calloc(1, sizeof(*stream));
  186. if(!stream)
  187. return CURLE_OUT_OF_MEMORY;
  188. stream->id = -1;
  189. Curl_bufq_initp(&stream->recvbuf, &ctx->stream_bufcp,
  190. H3_STREAM_RECV_CHUNKS, BUFQ_OPT_SOFT_LIMIT);
  191. Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
  192. if(!Curl_hash_offt_set(&ctx->streams, data->id, stream)) {
  193. h3_stream_ctx_free(stream);
  194. return CURLE_OUT_OF_MEMORY;
  195. }
  196. return CURLE_OK;
  197. }
  198. static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
  199. {
  200. struct cf_quiche_ctx *ctx = cf->ctx;
  201. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  202. CURLcode result;
  203. (void)cf;
  204. if(stream) {
  205. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] easy handle is done", stream->id);
  206. if(ctx->qconn && !stream->closed) {
  207. quiche_conn_stream_shutdown(ctx->qconn, stream->id,
  208. QUICHE_SHUTDOWN_READ, CURL_H3_NO_ERROR);
  209. if(!stream->send_closed) {
  210. quiche_conn_stream_shutdown(ctx->qconn, stream->id,
  211. QUICHE_SHUTDOWN_WRITE, CURL_H3_NO_ERROR);
  212. stream->send_closed = TRUE;
  213. }
  214. stream->closed = TRUE;
  215. result = cf_flush_egress(cf, data);
  216. if(result)
  217. CURL_TRC_CF(data, cf, "data_done, flush egress -> %d", result);
  218. }
  219. Curl_hash_offt_remove(&ctx->streams, data->id);
  220. }
  221. }
  222. static void drain_stream(struct Curl_cfilter *cf,
  223. struct Curl_easy *data)
  224. {
  225. struct cf_quiche_ctx *ctx = cf->ctx;
  226. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  227. unsigned char bits;
  228. (void)cf;
  229. bits = CURL_CSELECT_IN;
  230. if(stream && !stream->send_closed && stream->upload_left)
  231. bits |= CURL_CSELECT_OUT;
  232. if(data->state.select_bits != bits) {
  233. data->state.select_bits = bits;
  234. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  235. }
  236. }
  237. static struct Curl_easy *get_stream_easy(struct Curl_cfilter *cf,
  238. struct Curl_easy *data,
  239. curl_uint64_t stream_id,
  240. struct stream_ctx **pstream)
  241. {
  242. struct cf_quiche_ctx *ctx = cf->ctx;
  243. struct Curl_easy *sdata;
  244. struct stream_ctx *stream;
  245. (void)cf;
  246. stream = H3_STREAM_CTX(ctx, data);
  247. if(stream && stream->id == stream_id) {
  248. *pstream = stream;
  249. return data;
  250. }
  251. else {
  252. DEBUGASSERT(data->multi);
  253. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  254. if(sdata->conn != data->conn)
  255. continue;
  256. stream = H3_STREAM_CTX(ctx, sdata);
  257. if(stream && stream->id == stream_id) {
  258. *pstream = stream;
  259. return sdata;
  260. }
  261. }
  262. }
  263. *pstream = NULL;
  264. return NULL;
  265. }
  266. static void cf_quiche_expire_conn_closed(struct Curl_cfilter *cf,
  267. struct Curl_easy *data)
  268. {
  269. struct Curl_easy *sdata;
  270. DEBUGASSERT(data->multi);
  271. CURL_TRC_CF(data, cf, "conn closed, expire all transfers");
  272. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  273. if(sdata == data || sdata->conn != data->conn)
  274. continue;
  275. CURL_TRC_CF(sdata, cf, "conn closed, expire transfer");
  276. Curl_expire(sdata, 0, EXPIRE_RUN_NOW);
  277. }
  278. }
  279. /*
  280. * write_resp_raw() copies response data in raw format to the `data`'s
  281. * receive buffer. If not enough space is available, it appends to the
  282. * `data`'s overflow buffer.
  283. */
  284. static CURLcode write_resp_raw(struct Curl_cfilter *cf,
  285. struct Curl_easy *data,
  286. const void *mem, size_t memlen)
  287. {
  288. struct cf_quiche_ctx *ctx = cf->ctx;
  289. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  290. CURLcode result = CURLE_OK;
  291. ssize_t nwritten;
  292. (void)cf;
  293. if(!stream)
  294. return CURLE_RECV_ERROR;
  295. nwritten = Curl_bufq_write(&stream->recvbuf, mem, memlen, &result);
  296. if(nwritten < 0)
  297. return result;
  298. if((size_t)nwritten < memlen) {
  299. /* This MUST not happen. Our recbuf is dimensioned to hold the
  300. * full max_stream_window and then some for this very reason. */
  301. DEBUGASSERT(0);
  302. return CURLE_RECV_ERROR;
  303. }
  304. return result;
  305. }
  306. struct cb_ctx {
  307. struct Curl_cfilter *cf;
  308. struct Curl_easy *data;
  309. };
  310. static int cb_each_header(uint8_t *name, size_t name_len,
  311. uint8_t *value, size_t value_len,
  312. void *argp)
  313. {
  314. struct cb_ctx *x = argp;
  315. struct cf_quiche_ctx *ctx = x->cf->ctx;
  316. struct stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
  317. CURLcode result;
  318. if(!stream)
  319. return CURLE_OK;
  320. if((name_len == 7) && !strncmp(HTTP_PSEUDO_STATUS, (char *)name, 7)) {
  321. CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRIu64 "] status: %.*s",
  322. stream->id, (int)value_len, value);
  323. result = write_resp_raw(x->cf, x->data, "HTTP/3 ", sizeof("HTTP/3 ") - 1);
  324. if(!result)
  325. result = write_resp_raw(x->cf, x->data, value, value_len);
  326. if(!result)
  327. result = write_resp_raw(x->cf, x->data, " \r\n", 3);
  328. }
  329. else {
  330. CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRIu64 "] header: %.*s: %.*s",
  331. stream->id, (int)name_len, name,
  332. (int)value_len, value);
  333. result = write_resp_raw(x->cf, x->data, name, name_len);
  334. if(!result)
  335. result = write_resp_raw(x->cf, x->data, ": ", 2);
  336. if(!result)
  337. result = write_resp_raw(x->cf, x->data, value, value_len);
  338. if(!result)
  339. result = write_resp_raw(x->cf, x->data, "\r\n", 2);
  340. }
  341. if(result) {
  342. CURL_TRC_CF(x->data, x->cf, "[%"CURL_PRIu64"] on header error %d",
  343. stream->id, result);
  344. }
  345. return result;
  346. }
  347. static ssize_t stream_resp_read(void *reader_ctx,
  348. unsigned char *buf, size_t len,
  349. CURLcode *err)
  350. {
  351. struct cb_ctx *x = reader_ctx;
  352. struct cf_quiche_ctx *ctx = x->cf->ctx;
  353. struct stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
  354. ssize_t nread;
  355. if(!stream) {
  356. *err = CURLE_RECV_ERROR;
  357. return -1;
  358. }
  359. nread = quiche_h3_recv_body(ctx->h3c, ctx->qconn, stream->id,
  360. buf, len);
  361. if(nread >= 0) {
  362. *err = CURLE_OK;
  363. return nread;
  364. }
  365. else {
  366. *err = CURLE_AGAIN;
  367. return -1;
  368. }
  369. }
  370. static CURLcode cf_recv_body(struct Curl_cfilter *cf,
  371. struct Curl_easy *data)
  372. {
  373. struct cf_quiche_ctx *ctx = cf->ctx;
  374. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  375. ssize_t nwritten;
  376. struct cb_ctx cb_ctx;
  377. CURLcode result = CURLE_OK;
  378. if(!stream)
  379. return CURLE_RECV_ERROR;
  380. if(!stream->resp_hds_complete) {
  381. result = write_resp_raw(cf, data, "\r\n", 2);
  382. if(result)
  383. return result;
  384. stream->resp_hds_complete = TRUE;
  385. }
  386. cb_ctx.cf = cf;
  387. cb_ctx.data = data;
  388. nwritten = Curl_bufq_slurp(&stream->recvbuf,
  389. stream_resp_read, &cb_ctx, &result);
  390. if(nwritten < 0 && result != CURLE_AGAIN) {
  391. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] recv_body error %zd",
  392. stream->id, nwritten);
  393. failf(data, "Error %d in HTTP/3 response body for stream[%"CURL_PRIu64"]",
  394. result, stream->id);
  395. stream->closed = TRUE;
  396. stream->reset = TRUE;
  397. stream->send_closed = TRUE;
  398. streamclose(cf->conn, "Reset of stream");
  399. return result;
  400. }
  401. return CURLE_OK;
  402. }
  403. #ifdef DEBUGBUILD
  404. static const char *cf_ev_name(quiche_h3_event *ev)
  405. {
  406. switch(quiche_h3_event_type(ev)) {
  407. case QUICHE_H3_EVENT_HEADERS:
  408. return "HEADERS";
  409. case QUICHE_H3_EVENT_DATA:
  410. return "DATA";
  411. case QUICHE_H3_EVENT_RESET:
  412. return "RESET";
  413. case QUICHE_H3_EVENT_FINISHED:
  414. return "FINISHED";
  415. case QUICHE_H3_EVENT_GOAWAY:
  416. return "GOAWAY";
  417. default:
  418. return "Unknown";
  419. }
  420. }
  421. #else
  422. #define cf_ev_name(x) ""
  423. #endif
  424. static CURLcode h3_process_event(struct Curl_cfilter *cf,
  425. struct Curl_easy *data,
  426. struct stream_ctx *stream,
  427. quiche_h3_event *ev)
  428. {
  429. struct cb_ctx cb_ctx;
  430. CURLcode result = CURLE_OK;
  431. int rc;
  432. if(!stream)
  433. return CURLE_OK;
  434. switch(quiche_h3_event_type(ev)) {
  435. case QUICHE_H3_EVENT_HEADERS:
  436. stream->resp_got_header = TRUE;
  437. cb_ctx.cf = cf;
  438. cb_ctx.data = data;
  439. rc = quiche_h3_event_for_each_header(ev, cb_each_header, &cb_ctx);
  440. if(rc) {
  441. failf(data, "Error %d in HTTP/3 response header for stream[%"
  442. CURL_PRIu64"]", rc, stream->id);
  443. return CURLE_RECV_ERROR;
  444. }
  445. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] <- [HEADERS]", stream->id);
  446. break;
  447. case QUICHE_H3_EVENT_DATA:
  448. if(!stream->closed) {
  449. result = cf_recv_body(cf, data);
  450. }
  451. break;
  452. case QUICHE_H3_EVENT_RESET:
  453. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] RESET", stream->id);
  454. stream->closed = TRUE;
  455. stream->reset = TRUE;
  456. stream->send_closed = TRUE;
  457. streamclose(cf->conn, "Reset of stream");
  458. break;
  459. case QUICHE_H3_EVENT_FINISHED:
  460. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] CLOSED", stream->id);
  461. if(!stream->resp_hds_complete) {
  462. result = write_resp_raw(cf, data, "\r\n", 2);
  463. if(result)
  464. return result;
  465. stream->resp_hds_complete = TRUE;
  466. }
  467. stream->closed = TRUE;
  468. streamclose(cf->conn, "End of stream");
  469. break;
  470. case QUICHE_H3_EVENT_GOAWAY:
  471. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] <- [GOAWAY]", stream->id);
  472. break;
  473. default:
  474. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] recv, unhandled event %d",
  475. stream->id, quiche_h3_event_type(ev));
  476. break;
  477. }
  478. return result;
  479. }
  480. static CURLcode cf_poll_events(struct Curl_cfilter *cf,
  481. struct Curl_easy *data)
  482. {
  483. struct cf_quiche_ctx *ctx = cf->ctx;
  484. struct stream_ctx *stream = NULL;
  485. struct Curl_easy *sdata;
  486. quiche_h3_event *ev;
  487. CURLcode result;
  488. /* Take in the events and distribute them to the transfers. */
  489. while(ctx->h3c) {
  490. curl_int64_t stream3_id = quiche_h3_conn_poll(ctx->h3c, ctx->qconn, &ev);
  491. if(stream3_id == QUICHE_H3_ERR_DONE) {
  492. break;
  493. }
  494. else if(stream3_id < 0) {
  495. CURL_TRC_CF(data, cf, "error poll: %"CURL_PRId64, stream3_id);
  496. return CURLE_HTTP3;
  497. }
  498. sdata = get_stream_easy(cf, data, stream3_id, &stream);
  499. if(!sdata || !stream) {
  500. CURL_TRC_CF(data, cf, "discard event %s for unknown [%"CURL_PRId64"]",
  501. cf_ev_name(ev), stream3_id);
  502. }
  503. else {
  504. result = h3_process_event(cf, sdata, stream, ev);
  505. drain_stream(cf, sdata);
  506. if(result) {
  507. CURL_TRC_CF(data, cf, "error processing event %s "
  508. "for [%"CURL_PRIu64"] -> %d", cf_ev_name(ev),
  509. stream3_id, result);
  510. if(data == sdata) {
  511. /* Only report this error to the caller if it is about the
  512. * transfer we were called with. Otherwise we fail a transfer
  513. * due to a problem in another one. */
  514. quiche_h3_event_free(ev);
  515. return result;
  516. }
  517. }
  518. quiche_h3_event_free(ev);
  519. }
  520. }
  521. return CURLE_OK;
  522. }
  523. struct recv_ctx {
  524. struct Curl_cfilter *cf;
  525. struct Curl_easy *data;
  526. int pkts;
  527. };
  528. static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
  529. struct sockaddr_storage *remote_addr,
  530. socklen_t remote_addrlen, int ecn,
  531. void *userp)
  532. {
  533. struct recv_ctx *r = userp;
  534. struct cf_quiche_ctx *ctx = r->cf->ctx;
  535. quiche_recv_info recv_info;
  536. ssize_t nread;
  537. (void)ecn;
  538. ++r->pkts;
  539. recv_info.to = (struct sockaddr *)&ctx->q.local_addr;
  540. recv_info.to_len = ctx->q.local_addrlen;
  541. recv_info.from = (struct sockaddr *)remote_addr;
  542. recv_info.from_len = remote_addrlen;
  543. nread = quiche_conn_recv(ctx->qconn, (unsigned char *)pkt, pktlen,
  544. &recv_info);
  545. if(nread < 0) {
  546. if(QUICHE_ERR_DONE == nread) {
  547. if(quiche_conn_is_draining(ctx->qconn)) {
  548. CURL_TRC_CF(r->data, r->cf, "ingress, connection is draining");
  549. return CURLE_RECV_ERROR;
  550. }
  551. if(quiche_conn_is_closed(ctx->qconn)) {
  552. CURL_TRC_CF(r->data, r->cf, "ingress, connection is closed");
  553. return CURLE_RECV_ERROR;
  554. }
  555. CURL_TRC_CF(r->data, r->cf, "ingress, quiche is DONE");
  556. return CURLE_OK;
  557. }
  558. else if(QUICHE_ERR_TLS_FAIL == nread) {
  559. long verify_ok = SSL_get_verify_result(ctx->tls.ossl.ssl);
  560. if(verify_ok != X509_V_OK) {
  561. failf(r->data, "SSL certificate problem: %s",
  562. X509_verify_cert_error_string(verify_ok));
  563. return CURLE_PEER_FAILED_VERIFICATION;
  564. }
  565. }
  566. else {
  567. failf(r->data, "quiche_conn_recv() == %zd", nread);
  568. return CURLE_RECV_ERROR;
  569. }
  570. }
  571. else if((size_t)nread < pktlen) {
  572. CURL_TRC_CF(r->data, r->cf, "ingress, quiche only read %zd/%zu bytes",
  573. nread, pktlen);
  574. }
  575. return CURLE_OK;
  576. }
  577. static CURLcode cf_process_ingress(struct Curl_cfilter *cf,
  578. struct Curl_easy *data)
  579. {
  580. struct cf_quiche_ctx *ctx = cf->ctx;
  581. struct recv_ctx rctx;
  582. CURLcode result;
  583. DEBUGASSERT(ctx->qconn);
  584. result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
  585. if(result)
  586. return result;
  587. rctx.cf = cf;
  588. rctx.data = data;
  589. rctx.pkts = 0;
  590. result = vquic_recv_packets(cf, data, &ctx->q, 1000, recv_pkt, &rctx);
  591. if(result)
  592. return result;
  593. if(rctx.pkts > 0) {
  594. /* quiche digested ingress packets. It might have opened flow control
  595. * windows again. */
  596. check_resumes(cf, data);
  597. }
  598. return cf_poll_events(cf, data);
  599. }
  600. struct read_ctx {
  601. struct Curl_cfilter *cf;
  602. struct Curl_easy *data;
  603. quiche_send_info send_info;
  604. };
  605. static ssize_t read_pkt_to_send(void *userp,
  606. unsigned char *buf, size_t buflen,
  607. CURLcode *err)
  608. {
  609. struct read_ctx *x = userp;
  610. struct cf_quiche_ctx *ctx = x->cf->ctx;
  611. ssize_t nwritten;
  612. nwritten = quiche_conn_send(ctx->qconn, buf, buflen, &x->send_info);
  613. if(nwritten == QUICHE_ERR_DONE) {
  614. *err = CURLE_AGAIN;
  615. return -1;
  616. }
  617. if(nwritten < 0) {
  618. failf(x->data, "quiche_conn_send returned %zd", nwritten);
  619. *err = CURLE_SEND_ERROR;
  620. return -1;
  621. }
  622. *err = CURLE_OK;
  623. return nwritten;
  624. }
  625. /*
  626. * flush_egress drains the buffers and sends off data.
  627. * Calls failf() on errors.
  628. */
  629. static CURLcode cf_flush_egress(struct Curl_cfilter *cf,
  630. struct Curl_easy *data)
  631. {
  632. struct cf_quiche_ctx *ctx = cf->ctx;
  633. ssize_t nread;
  634. CURLcode result;
  635. curl_int64_t expiry_ns;
  636. curl_int64_t timeout_ns;
  637. struct read_ctx readx;
  638. size_t pkt_count, gsolen;
  639. expiry_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
  640. if(!expiry_ns) {
  641. quiche_conn_on_timeout(ctx->qconn);
  642. if(quiche_conn_is_closed(ctx->qconn)) {
  643. if(quiche_conn_is_timed_out(ctx->qconn))
  644. failf(data, "connection closed by idle timeout");
  645. else
  646. failf(data, "connection closed by server");
  647. /* Connection timed out, expire all transfers belonging to it
  648. * as will not get any more POLL events here. */
  649. cf_quiche_expire_conn_closed(cf, data);
  650. return CURLE_SEND_ERROR;
  651. }
  652. }
  653. result = vquic_flush(cf, data, &ctx->q);
  654. if(result) {
  655. if(result == CURLE_AGAIN) {
  656. Curl_expire(data, 1, EXPIRE_QUIC);
  657. return CURLE_OK;
  658. }
  659. return result;
  660. }
  661. readx.cf = cf;
  662. readx.data = data;
  663. memset(&readx.send_info, 0, sizeof(readx.send_info));
  664. pkt_count = 0;
  665. gsolen = quiche_conn_max_send_udp_payload_size(ctx->qconn);
  666. for(;;) {
  667. /* add the next packet to send, if any, to our buffer */
  668. nread = Curl_bufq_sipn(&ctx->q.sendbuf, 0,
  669. read_pkt_to_send, &readx, &result);
  670. if(nread < 0) {
  671. if(result != CURLE_AGAIN)
  672. return result;
  673. /* Nothing more to add, flush and leave */
  674. result = vquic_send(cf, data, &ctx->q, gsolen);
  675. if(result) {
  676. if(result == CURLE_AGAIN) {
  677. Curl_expire(data, 1, EXPIRE_QUIC);
  678. return CURLE_OK;
  679. }
  680. return result;
  681. }
  682. goto out;
  683. }
  684. ++pkt_count;
  685. if((size_t)nread < gsolen || pkt_count >= MAX_PKT_BURST) {
  686. result = vquic_send(cf, data, &ctx->q, gsolen);
  687. if(result) {
  688. if(result == CURLE_AGAIN) {
  689. Curl_expire(data, 1, EXPIRE_QUIC);
  690. return CURLE_OK;
  691. }
  692. goto out;
  693. }
  694. pkt_count = 0;
  695. }
  696. }
  697. out:
  698. timeout_ns = quiche_conn_timeout_as_nanos(ctx->qconn);
  699. if(timeout_ns % 1000000)
  700. timeout_ns += 1000000;
  701. /* expire resolution is milliseconds */
  702. Curl_expire(data, (timeout_ns / 1000000), EXPIRE_QUIC);
  703. return result;
  704. }
  705. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  706. struct Curl_easy *data,
  707. CURLcode *err)
  708. {
  709. struct cf_quiche_ctx *ctx = cf->ctx;
  710. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  711. ssize_t nread = -1;
  712. DEBUGASSERT(stream);
  713. if(stream->reset) {
  714. failf(data,
  715. "HTTP/3 stream %" CURL_PRIu64 " reset by server", stream->id);
  716. *err = data->req.bytecount? CURLE_PARTIAL_FILE : CURLE_HTTP3;
  717. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_recv, was reset -> %d",
  718. stream->id, *err);
  719. }
  720. else if(!stream->resp_got_header) {
  721. failf(data,
  722. "HTTP/3 stream %" CURL_PRIu64 " was closed cleanly, but before "
  723. "getting all response header fields, treated as error",
  724. stream->id);
  725. /* *err = CURLE_PARTIAL_FILE; */
  726. *err = CURLE_HTTP3;
  727. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_recv, closed incomplete"
  728. " -> %d", stream->id, *err);
  729. }
  730. else {
  731. *err = CURLE_OK;
  732. nread = 0;
  733. }
  734. return nread;
  735. }
  736. static ssize_t cf_quiche_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  737. char *buf, size_t len, CURLcode *err)
  738. {
  739. struct cf_quiche_ctx *ctx = cf->ctx;
  740. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  741. ssize_t nread = -1;
  742. CURLcode result;
  743. vquic_ctx_update_time(&ctx->q);
  744. if(!stream) {
  745. *err = CURLE_RECV_ERROR;
  746. return -1;
  747. }
  748. if(!Curl_bufq_is_empty(&stream->recvbuf)) {
  749. nread = Curl_bufq_read(&stream->recvbuf,
  750. (unsigned char *)buf, len, err);
  751. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] read recvbuf(len=%zu) "
  752. "-> %zd, %d", stream->id, len, nread, *err);
  753. if(nread < 0)
  754. goto out;
  755. }
  756. if(cf_process_ingress(cf, data)) {
  757. CURL_TRC_CF(data, cf, "cf_recv, error on ingress");
  758. *err = CURLE_RECV_ERROR;
  759. nread = -1;
  760. goto out;
  761. }
  762. /* recvbuf had nothing before, maybe after progressing ingress? */
  763. if(nread < 0 && !Curl_bufq_is_empty(&stream->recvbuf)) {
  764. nread = Curl_bufq_read(&stream->recvbuf,
  765. (unsigned char *)buf, len, err);
  766. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] read recvbuf(len=%zu) "
  767. "-> %zd, %d", stream->id, len, nread, *err);
  768. if(nread < 0)
  769. goto out;
  770. }
  771. if(nread > 0) {
  772. if(stream->closed)
  773. drain_stream(cf, data);
  774. }
  775. else {
  776. if(stream->closed) {
  777. nread = recv_closed_stream(cf, data, err);
  778. goto out;
  779. }
  780. else if(quiche_conn_is_draining(ctx->qconn)) {
  781. failf(data, "QUIC connection is draining");
  782. *err = CURLE_HTTP3;
  783. nread = -1;
  784. goto out;
  785. }
  786. *err = CURLE_AGAIN;
  787. nread = -1;
  788. }
  789. out:
  790. result = cf_flush_egress(cf, data);
  791. if(result) {
  792. CURL_TRC_CF(data, cf, "cf_recv, flush egress failed");
  793. *err = result;
  794. nread = -1;
  795. }
  796. if(nread > 0)
  797. ctx->data_recvd += nread;
  798. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] cf_recv(total=%"
  799. CURL_FORMAT_CURL_OFF_T ") -> %zd, %d",
  800. stream->id, ctx->data_recvd, nread, *err);
  801. return nread;
  802. }
  803. /* Index where :authority header field will appear in request header
  804. field list. */
  805. #define AUTHORITY_DST_IDX 3
  806. static ssize_t h3_open_stream(struct Curl_cfilter *cf,
  807. struct Curl_easy *data,
  808. const void *buf, size_t len,
  809. CURLcode *err)
  810. {
  811. struct cf_quiche_ctx *ctx = cf->ctx;
  812. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  813. size_t nheader, i;
  814. curl_int64_t stream3_id;
  815. struct dynhds h2_headers;
  816. quiche_h3_header *nva = NULL;
  817. ssize_t nwritten;
  818. if(!stream) {
  819. *err = h3_data_setup(cf, data);
  820. if(*err) {
  821. return -1;
  822. }
  823. stream = H3_STREAM_CTX(ctx, data);
  824. DEBUGASSERT(stream);
  825. }
  826. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  827. DEBUGASSERT(stream);
  828. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  829. if(nwritten < 0)
  830. goto out;
  831. if(!stream->h1.done) {
  832. /* need more data */
  833. goto out;
  834. }
  835. DEBUGASSERT(stream->h1.req);
  836. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  837. if(*err) {
  838. nwritten = -1;
  839. goto out;
  840. }
  841. /* no longer needed */
  842. Curl_h1_req_parse_free(&stream->h1);
  843. nheader = Curl_dynhds_count(&h2_headers);
  844. nva = malloc(sizeof(quiche_h3_header) * nheader);
  845. if(!nva) {
  846. *err = CURLE_OUT_OF_MEMORY;
  847. nwritten = -1;
  848. goto out;
  849. }
  850. for(i = 0; i < nheader; ++i) {
  851. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  852. nva[i].name = (unsigned char *)e->name;
  853. nva[i].name_len = e->namelen;
  854. nva[i].value = (unsigned char *)e->value;
  855. nva[i].value_len = e->valuelen;
  856. }
  857. switch(data->state.httpreq) {
  858. case HTTPREQ_POST:
  859. case HTTPREQ_POST_FORM:
  860. case HTTPREQ_POST_MIME:
  861. case HTTPREQ_PUT:
  862. if(data->state.infilesize != -1)
  863. stream->upload_left = data->state.infilesize;
  864. else
  865. /* data sending without specifying the data amount up front */
  866. stream->upload_left = -1; /* unknown */
  867. break;
  868. default:
  869. stream->upload_left = 0; /* no request body */
  870. break;
  871. }
  872. if(stream->upload_left == 0)
  873. stream->send_closed = TRUE;
  874. stream3_id = quiche_h3_send_request(ctx->h3c, ctx->qconn, nva, nheader,
  875. stream->send_closed);
  876. if(stream3_id < 0) {
  877. if(QUICHE_H3_ERR_STREAM_BLOCKED == stream3_id) {
  878. /* quiche seems to report this error if the connection window is
  879. * exhausted. Which happens frequently and intermittent. */
  880. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] blocked", stream->id);
  881. stream->quic_flow_blocked = TRUE;
  882. *err = CURLE_AGAIN;
  883. nwritten = -1;
  884. goto out;
  885. }
  886. else {
  887. CURL_TRC_CF(data, cf, "send_request(%s) -> %" CURL_PRIu64,
  888. data->state.url, stream3_id);
  889. }
  890. *err = CURLE_SEND_ERROR;
  891. nwritten = -1;
  892. goto out;
  893. }
  894. DEBUGASSERT(!stream->opened);
  895. *err = CURLE_OK;
  896. stream->id = stream3_id;
  897. stream->opened = TRUE;
  898. stream->closed = FALSE;
  899. stream->reset = FALSE;
  900. if(Curl_trc_is_verbose(data)) {
  901. infof(data, "[HTTP/3] [%" CURL_PRIu64 "] OPENED stream for %s",
  902. stream->id, data->state.url);
  903. for(i = 0; i < nheader; ++i) {
  904. infof(data, "[HTTP/3] [%" CURL_PRIu64 "] [%.*s: %.*s]", stream->id,
  905. (int)nva[i].name_len, nva[i].name,
  906. (int)nva[i].value_len, nva[i].value);
  907. }
  908. }
  909. out:
  910. free(nva);
  911. Curl_dynhds_free(&h2_headers);
  912. return nwritten;
  913. }
  914. static ssize_t cf_quiche_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  915. const void *buf, size_t len, CURLcode *err)
  916. {
  917. struct cf_quiche_ctx *ctx = cf->ctx;
  918. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  919. CURLcode result;
  920. ssize_t nwritten;
  921. vquic_ctx_update_time(&ctx->q);
  922. *err = cf_process_ingress(cf, data);
  923. if(*err) {
  924. nwritten = -1;
  925. goto out;
  926. }
  927. if(!stream || !stream->opened) {
  928. nwritten = h3_open_stream(cf, data, buf, len, err);
  929. if(nwritten < 0)
  930. goto out;
  931. stream = H3_STREAM_CTX(ctx, data);
  932. }
  933. else if(stream->closed) {
  934. if(stream->resp_hds_complete) {
  935. /* sending request body on a stream that has been closed by the
  936. * server. If the server has send us a final response, we should
  937. * silently discard the send data.
  938. * This happens for example on redirects where the server, instead
  939. * of reading the full request body just closed the stream after
  940. * sending the 30x response.
  941. * This is sort of a race: had the transfer loop called recv first,
  942. * it would see the response and stop/discard sending on its own- */
  943. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] discarding data"
  944. "on closed stream with response", stream->id);
  945. *err = CURLE_OK;
  946. nwritten = (ssize_t)len;
  947. goto out;
  948. }
  949. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
  950. "-> stream closed", stream->id, len);
  951. *err = CURLE_HTTP3;
  952. nwritten = -1;
  953. goto out;
  954. }
  955. else {
  956. bool eof = (stream->upload_left >= 0 &&
  957. (curl_off_t)len >= stream->upload_left);
  958. nwritten = quiche_h3_send_body(ctx->h3c, ctx->qconn, stream->id,
  959. (uint8_t *)buf, len, eof);
  960. if(nwritten == QUICHE_H3_ERR_DONE || (nwritten == 0 && len > 0)) {
  961. /* TODO: we seem to be blocked on flow control and should HOLD
  962. * sending. But when do we open again? */
  963. if(!quiche_conn_stream_writable(ctx->qconn, stream->id, len)) {
  964. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
  965. "-> window exhausted", stream->id, len);
  966. stream->quic_flow_blocked = TRUE;
  967. }
  968. *err = CURLE_AGAIN;
  969. nwritten = -1;
  970. goto out;
  971. }
  972. else if(nwritten == QUICHE_H3_TRANSPORT_ERR_INVALID_STREAM_STATE) {
  973. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
  974. "-> invalid stream state", stream->id, len);
  975. *err = CURLE_HTTP3;
  976. nwritten = -1;
  977. goto out;
  978. }
  979. else if(nwritten == QUICHE_H3_TRANSPORT_ERR_FINAL_SIZE) {
  980. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
  981. "-> exceeds size", stream->id, len);
  982. *err = CURLE_SEND_ERROR;
  983. nwritten = -1;
  984. goto out;
  985. }
  986. else if(nwritten < 0) {
  987. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send_body(len=%zu) "
  988. "-> quiche err %zd", stream->id, len, nwritten);
  989. *err = CURLE_SEND_ERROR;
  990. nwritten = -1;
  991. goto out;
  992. }
  993. else {
  994. /* quiche accepted all or at least a part of the buf */
  995. if(stream->upload_left > 0) {
  996. stream->upload_left = (nwritten < stream->upload_left)?
  997. (stream->upload_left - nwritten) : 0;
  998. }
  999. if(stream->upload_left == 0)
  1000. stream->send_closed = TRUE;
  1001. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] send body(len=%zu, "
  1002. "left=%" CURL_FORMAT_CURL_OFF_T ") -> %zd",
  1003. stream->id, len, stream->upload_left, nwritten);
  1004. *err = CURLE_OK;
  1005. }
  1006. }
  1007. out:
  1008. result = cf_flush_egress(cf, data);
  1009. if(result) {
  1010. *err = result;
  1011. nwritten = -1;
  1012. }
  1013. CURL_TRC_CF(data, cf, "[%" CURL_PRIu64 "] cf_send(len=%zu) -> %zd, %d",
  1014. stream? stream->id : -1, len, nwritten, *err);
  1015. return nwritten;
  1016. }
  1017. static bool stream_is_writeable(struct Curl_cfilter *cf,
  1018. struct Curl_easy *data)
  1019. {
  1020. struct cf_quiche_ctx *ctx = cf->ctx;
  1021. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1022. return stream && (quiche_conn_stream_writable(
  1023. ctx->qconn, (curl_uint64_t)stream->id, 1) > 0);
  1024. }
  1025. static void cf_quiche_adjust_pollset(struct Curl_cfilter *cf,
  1026. struct Curl_easy *data,
  1027. struct easy_pollset *ps)
  1028. {
  1029. struct cf_quiche_ctx *ctx = cf->ctx;
  1030. bool want_recv, want_send;
  1031. if(!ctx->qconn)
  1032. return;
  1033. Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
  1034. if(want_recv || want_send) {
  1035. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1036. bool c_exhaust, s_exhaust;
  1037. c_exhaust = FALSE; /* Have not found any call in quiche that tells
  1038. us if the connection itself is blocked */
  1039. s_exhaust = want_send && stream && stream->opened &&
  1040. (stream->quic_flow_blocked || !stream_is_writeable(cf, data));
  1041. want_recv = (want_recv || c_exhaust || s_exhaust);
  1042. want_send = (!s_exhaust && want_send) ||
  1043. !Curl_bufq_is_empty(&ctx->q.sendbuf);
  1044. Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
  1045. }
  1046. }
  1047. /*
  1048. * Called from transfer.c:data_pending to know if we should keep looping
  1049. * to receive more data from the connection.
  1050. */
  1051. static bool cf_quiche_data_pending(struct Curl_cfilter *cf,
  1052. const struct Curl_easy *data)
  1053. {
  1054. struct cf_quiche_ctx *ctx = cf->ctx;
  1055. const struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1056. (void)cf;
  1057. return stream && !Curl_bufq_is_empty(&stream->recvbuf);
  1058. }
  1059. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  1060. struct Curl_easy *data,
  1061. bool pause)
  1062. {
  1063. /* TODO: there seems right now no API in quiche to shrink/enlarge
  1064. * the streams windows. As we do in HTTP/2. */
  1065. if(!pause) {
  1066. drain_stream(cf, data);
  1067. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1068. }
  1069. return CURLE_OK;
  1070. }
  1071. static CURLcode cf_quiche_data_event(struct Curl_cfilter *cf,
  1072. struct Curl_easy *data,
  1073. int event, int arg1, void *arg2)
  1074. {
  1075. struct cf_quiche_ctx *ctx = cf->ctx;
  1076. CURLcode result = CURLE_OK;
  1077. (void)arg1;
  1078. (void)arg2;
  1079. switch(event) {
  1080. case CF_CTRL_DATA_SETUP:
  1081. break;
  1082. case CF_CTRL_DATA_PAUSE:
  1083. result = h3_data_pause(cf, data, (arg1 != 0));
  1084. break;
  1085. case CF_CTRL_DATA_DETACH:
  1086. h3_data_done(cf, data);
  1087. break;
  1088. case CF_CTRL_DATA_DONE:
  1089. h3_data_done(cf, data);
  1090. break;
  1091. case CF_CTRL_DATA_DONE_SEND: {
  1092. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1093. if(stream && !stream->send_closed) {
  1094. unsigned char body[1];
  1095. ssize_t sent;
  1096. stream->send_closed = TRUE;
  1097. stream->upload_left = 0;
  1098. body[0] = 'X';
  1099. sent = cf_quiche_send(cf, data, body, 0, &result);
  1100. CURL_TRC_CF(data, cf, "[%"CURL_PRIu64"] DONE_SEND -> %zd, %d",
  1101. stream->id, sent, result);
  1102. }
  1103. break;
  1104. }
  1105. case CF_CTRL_DATA_IDLE: {
  1106. struct stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1107. if(stream && !stream->closed) {
  1108. result = cf_flush_egress(cf, data);
  1109. if(result)
  1110. CURL_TRC_CF(data, cf, "data idle, flush egress -> %d", result);
  1111. }
  1112. break;
  1113. }
  1114. default:
  1115. break;
  1116. }
  1117. return result;
  1118. }
  1119. static CURLcode cf_connect_start(struct Curl_cfilter *cf,
  1120. struct Curl_easy *data)
  1121. {
  1122. struct cf_quiche_ctx *ctx = cf->ctx;
  1123. int rv;
  1124. CURLcode result;
  1125. const struct Curl_sockaddr_ex *sockaddr;
  1126. DEBUGASSERT(ctx->q.sockfd != CURL_SOCKET_BAD);
  1127. #ifdef DEBUG_QUICHE
  1128. /* initialize debug log callback only once */
  1129. static int debug_log_init = 0;
  1130. if(!debug_log_init) {
  1131. quiche_enable_debug_logging(quiche_debug_log, NULL);
  1132. debug_log_init = 1;
  1133. }
  1134. #endif
  1135. Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
  1136. H3_STREAM_POOL_SPARES);
  1137. Curl_hash_offt_init(&ctx->streams, 63, h3_stream_hash_free);
  1138. ctx->data_recvd = 0;
  1139. result = vquic_ctx_init(&ctx->q);
  1140. if(result)
  1141. return result;
  1142. result = Curl_ssl_peer_init(&ctx->peer, cf, TRNSPRT_QUIC);
  1143. if(result)
  1144. return result;
  1145. ctx->cfg = quiche_config_new(QUICHE_PROTOCOL_VERSION);
  1146. if(!ctx->cfg) {
  1147. failf(data, "can't create quiche config");
  1148. return CURLE_FAILED_INIT;
  1149. }
  1150. quiche_config_enable_pacing(ctx->cfg, false);
  1151. quiche_config_set_max_idle_timeout(ctx->cfg, CURL_QUIC_MAX_IDLE_MS);
  1152. quiche_config_set_initial_max_data(ctx->cfg, (1 * 1024 * 1024)
  1153. /* (QUIC_MAX_STREAMS/2) * H3_STREAM_WINDOW_SIZE */);
  1154. quiche_config_set_initial_max_streams_bidi(ctx->cfg, QUIC_MAX_STREAMS);
  1155. quiche_config_set_initial_max_streams_uni(ctx->cfg, QUIC_MAX_STREAMS);
  1156. quiche_config_set_initial_max_stream_data_bidi_local(ctx->cfg,
  1157. H3_STREAM_WINDOW_SIZE);
  1158. quiche_config_set_initial_max_stream_data_bidi_remote(ctx->cfg,
  1159. H3_STREAM_WINDOW_SIZE);
  1160. quiche_config_set_initial_max_stream_data_uni(ctx->cfg,
  1161. H3_STREAM_WINDOW_SIZE);
  1162. quiche_config_set_disable_active_migration(ctx->cfg, TRUE);
  1163. quiche_config_set_max_connection_window(ctx->cfg,
  1164. 10 * QUIC_MAX_STREAMS * H3_STREAM_WINDOW_SIZE);
  1165. quiche_config_set_max_stream_window(ctx->cfg, 10 * H3_STREAM_WINDOW_SIZE);
  1166. quiche_config_set_application_protos(ctx->cfg,
  1167. (uint8_t *)
  1168. QUICHE_H3_APPLICATION_PROTOCOL,
  1169. sizeof(QUICHE_H3_APPLICATION_PROTOCOL)
  1170. - 1);
  1171. result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
  1172. QUICHE_H3_APPLICATION_PROTOCOL,
  1173. sizeof(QUICHE_H3_APPLICATION_PROTOCOL) - 1,
  1174. NULL, NULL, cf);
  1175. if(result)
  1176. return result;
  1177. result = Curl_rand(data, ctx->scid, sizeof(ctx->scid));
  1178. if(result)
  1179. return result;
  1180. Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL);
  1181. ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
  1182. rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
  1183. &ctx->q.local_addrlen);
  1184. if(rv == -1)
  1185. return CURLE_QUIC_CONNECT_ERROR;
  1186. ctx->qconn = quiche_conn_new_with_tls((const uint8_t *)ctx->scid,
  1187. sizeof(ctx->scid), NULL, 0,
  1188. (struct sockaddr *)&ctx->q.local_addr,
  1189. ctx->q.local_addrlen,
  1190. &sockaddr->sa_addr, sockaddr->addrlen,
  1191. ctx->cfg, ctx->tls.ossl.ssl, false);
  1192. if(!ctx->qconn) {
  1193. failf(data, "can't create quiche connection");
  1194. return CURLE_OUT_OF_MEMORY;
  1195. }
  1196. /* Known to not work on Windows */
  1197. #if !defined(_WIN32) && defined(HAVE_QUICHE_CONN_SET_QLOG_FD)
  1198. {
  1199. int qfd;
  1200. (void)Curl_qlogdir(data, ctx->scid, sizeof(ctx->scid), &qfd);
  1201. if(qfd != -1)
  1202. quiche_conn_set_qlog_fd(ctx->qconn, qfd,
  1203. "qlog title", "curl qlog");
  1204. }
  1205. #endif
  1206. result = cf_flush_egress(cf, data);
  1207. if(result)
  1208. return result;
  1209. {
  1210. unsigned char alpn_protocols[] = QUICHE_H3_APPLICATION_PROTOCOL;
  1211. unsigned alpn_len, offset = 0;
  1212. /* Replace each ALPN length prefix by a comma. */
  1213. while(offset < sizeof(alpn_protocols) - 1) {
  1214. alpn_len = alpn_protocols[offset];
  1215. alpn_protocols[offset] = ',';
  1216. offset += 1 + alpn_len;
  1217. }
  1218. CURL_TRC_CF(data, cf, "Sent QUIC client Initial, ALPN: %s",
  1219. alpn_protocols + 1);
  1220. }
  1221. return CURLE_OK;
  1222. }
  1223. static CURLcode cf_quiche_verify_peer(struct Curl_cfilter *cf,
  1224. struct Curl_easy *data)
  1225. {
  1226. struct cf_quiche_ctx *ctx = cf->ctx;
  1227. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1228. cf->conn->httpversion = 30;
  1229. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1230. return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
  1231. }
  1232. static CURLcode cf_quiche_connect(struct Curl_cfilter *cf,
  1233. struct Curl_easy *data,
  1234. bool blocking, bool *done)
  1235. {
  1236. struct cf_quiche_ctx *ctx = cf->ctx;
  1237. CURLcode result = CURLE_OK;
  1238. if(cf->connected) {
  1239. *done = TRUE;
  1240. return CURLE_OK;
  1241. }
  1242. /* Connect the UDP filter first */
  1243. if(!cf->next->connected) {
  1244. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  1245. if(result || !*done)
  1246. return result;
  1247. }
  1248. *done = FALSE;
  1249. vquic_ctx_update_time(&ctx->q);
  1250. if(ctx->reconnect_at.tv_sec &&
  1251. Curl_timediff(ctx->q.last_op, ctx->reconnect_at) < 0) {
  1252. /* Not time yet to attempt the next connect */
  1253. CURL_TRC_CF(data, cf, "waiting for reconnect time");
  1254. goto out;
  1255. }
  1256. if(!ctx->qconn) {
  1257. result = cf_connect_start(cf, data);
  1258. if(result)
  1259. goto out;
  1260. ctx->started_at = ctx->q.last_op;
  1261. result = cf_flush_egress(cf, data);
  1262. /* we do not expect to be able to recv anything yet */
  1263. goto out;
  1264. }
  1265. result = cf_process_ingress(cf, data);
  1266. if(result)
  1267. goto out;
  1268. result = cf_flush_egress(cf, data);
  1269. if(result)
  1270. goto out;
  1271. if(quiche_conn_is_established(ctx->qconn)) {
  1272. ctx->handshake_at = ctx->q.last_op;
  1273. CURL_TRC_CF(data, cf, "handshake complete after %dms",
  1274. (int)Curl_timediff(ctx->handshake_at, ctx->started_at));
  1275. result = cf_quiche_verify_peer(cf, data);
  1276. if(!result) {
  1277. CURL_TRC_CF(data, cf, "peer verified");
  1278. ctx->h3config = quiche_h3_config_new();
  1279. if(!ctx->h3config) {
  1280. result = CURLE_OUT_OF_MEMORY;
  1281. goto out;
  1282. }
  1283. /* Create a new HTTP/3 connection on the QUIC connection. */
  1284. ctx->h3c = quiche_h3_conn_new_with_transport(ctx->qconn, ctx->h3config);
  1285. if(!ctx->h3c) {
  1286. result = CURLE_OUT_OF_MEMORY;
  1287. goto out;
  1288. }
  1289. cf->connected = TRUE;
  1290. cf->conn->alpn = CURL_HTTP_VERSION_3;
  1291. *done = TRUE;
  1292. connkeep(cf->conn, "HTTP/3 default");
  1293. }
  1294. }
  1295. else if(quiche_conn_is_draining(ctx->qconn)) {
  1296. /* When a QUIC server instance is shutting down, it may send us a
  1297. * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
  1298. * state. The CONNECT may work in the near future again. Indicate
  1299. * that as a "weird" reply. */
  1300. result = CURLE_WEIRD_SERVER_REPLY;
  1301. }
  1302. out:
  1303. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1304. if(result && result != CURLE_AGAIN) {
  1305. struct ip_quadruple ip;
  1306. Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
  1307. infof(data, "connect to %s port %u failed: %s",
  1308. ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
  1309. }
  1310. #endif
  1311. return result;
  1312. }
  1313. static void cf_quiche_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1314. {
  1315. struct cf_quiche_ctx *ctx = cf->ctx;
  1316. if(ctx) {
  1317. if(ctx->qconn) {
  1318. vquic_ctx_update_time(&ctx->q);
  1319. (void)quiche_conn_close(ctx->qconn, TRUE, 0, NULL, 0);
  1320. /* flushing the egress is not a failsafe way to deliver all the
  1321. outstanding packets, but we also don't want to get stuck here... */
  1322. (void)cf_flush_egress(cf, data);
  1323. }
  1324. cf_quiche_ctx_clear(ctx);
  1325. }
  1326. }
  1327. static void cf_quiche_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1328. {
  1329. struct cf_quiche_ctx *ctx = cf->ctx;
  1330. (void)data;
  1331. cf_quiche_ctx_clear(ctx);
  1332. free(ctx);
  1333. cf->ctx = NULL;
  1334. }
  1335. static CURLcode cf_quiche_query(struct Curl_cfilter *cf,
  1336. struct Curl_easy *data,
  1337. int query, int *pres1, void *pres2)
  1338. {
  1339. struct cf_quiche_ctx *ctx = cf->ctx;
  1340. switch(query) {
  1341. case CF_QUERY_MAX_CONCURRENT: {
  1342. curl_uint64_t max_streams = CONN_INUSE(cf->conn);
  1343. if(!ctx->goaway) {
  1344. max_streams += quiche_conn_peer_streams_left_bidi(ctx->qconn);
  1345. }
  1346. *pres1 = (max_streams > INT_MAX)? INT_MAX : (int)max_streams;
  1347. CURL_TRC_CF(data, cf, "query conn[%" CURL_FORMAT_CURL_OFF_T "]: "
  1348. "MAX_CONCURRENT -> %d (%zu in use)",
  1349. cf->conn->connection_id, *pres1, CONN_INUSE(cf->conn));
  1350. return CURLE_OK;
  1351. }
  1352. case CF_QUERY_CONNECT_REPLY_MS:
  1353. if(ctx->q.got_first_byte) {
  1354. timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
  1355. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  1356. }
  1357. else
  1358. *pres1 = -1;
  1359. return CURLE_OK;
  1360. case CF_QUERY_TIMER_CONNECT: {
  1361. struct curltime *when = pres2;
  1362. if(ctx->q.got_first_byte)
  1363. *when = ctx->q.first_byte_at;
  1364. return CURLE_OK;
  1365. }
  1366. case CF_QUERY_TIMER_APPCONNECT: {
  1367. struct curltime *when = pres2;
  1368. if(cf->connected)
  1369. *when = ctx->handshake_at;
  1370. return CURLE_OK;
  1371. }
  1372. default:
  1373. break;
  1374. }
  1375. return cf->next?
  1376. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1377. CURLE_UNKNOWN_OPTION;
  1378. }
  1379. static bool cf_quiche_conn_is_alive(struct Curl_cfilter *cf,
  1380. struct Curl_easy *data,
  1381. bool *input_pending)
  1382. {
  1383. struct cf_quiche_ctx *ctx = cf->ctx;
  1384. bool alive = TRUE;
  1385. *input_pending = FALSE;
  1386. if(!ctx->qconn)
  1387. return FALSE;
  1388. if(quiche_conn_is_closed(ctx->qconn)) {
  1389. if(quiche_conn_is_timed_out(ctx->qconn))
  1390. CURL_TRC_CF(data, cf, "connection was closed due to idle timeout");
  1391. else
  1392. CURL_TRC_CF(data, cf, "connection is closed");
  1393. return FALSE;
  1394. }
  1395. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  1396. return FALSE;
  1397. if(*input_pending) {
  1398. /* This happens before we've sent off a request and the connection is
  1399. not in use by any other transfer, there shouldn't be any data here,
  1400. only "protocol frames" */
  1401. *input_pending = FALSE;
  1402. if(cf_process_ingress(cf, data))
  1403. alive = FALSE;
  1404. else {
  1405. alive = TRUE;
  1406. }
  1407. }
  1408. return alive;
  1409. }
  1410. struct Curl_cftype Curl_cft_http3 = {
  1411. "HTTP/3",
  1412. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  1413. 0,
  1414. cf_quiche_destroy,
  1415. cf_quiche_connect,
  1416. cf_quiche_close,
  1417. Curl_cf_def_get_host,
  1418. cf_quiche_adjust_pollset,
  1419. cf_quiche_data_pending,
  1420. cf_quiche_send,
  1421. cf_quiche_recv,
  1422. cf_quiche_data_event,
  1423. cf_quiche_conn_is_alive,
  1424. Curl_cf_def_conn_keep_alive,
  1425. cf_quiche_query,
  1426. };
  1427. CURLcode Curl_cf_quiche_create(struct Curl_cfilter **pcf,
  1428. struct Curl_easy *data,
  1429. struct connectdata *conn,
  1430. const struct Curl_addrinfo *ai)
  1431. {
  1432. struct cf_quiche_ctx *ctx = NULL;
  1433. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  1434. CURLcode result;
  1435. (void)data;
  1436. (void)conn;
  1437. ctx = calloc(1, sizeof(*ctx));
  1438. if(!ctx) {
  1439. result = CURLE_OUT_OF_MEMORY;
  1440. goto out;
  1441. }
  1442. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  1443. if(result)
  1444. goto out;
  1445. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  1446. if(result)
  1447. goto out;
  1448. udp_cf->conn = cf->conn;
  1449. udp_cf->sockindex = cf->sockindex;
  1450. cf->next = udp_cf;
  1451. out:
  1452. *pcf = (!result)? cf : NULL;
  1453. if(result) {
  1454. if(udp_cf)
  1455. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  1456. Curl_safefree(cf);
  1457. Curl_safefree(ctx);
  1458. }
  1459. return result;
  1460. }
  1461. bool Curl_conn_is_quiche(const struct Curl_easy *data,
  1462. const struct connectdata *conn,
  1463. int sockindex)
  1464. {
  1465. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  1466. (void)data;
  1467. for(; cf; cf = cf->next) {
  1468. if(cf->cft == &Curl_cft_http3)
  1469. return TRUE;
  1470. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  1471. return FALSE;
  1472. }
  1473. return FALSE;
  1474. }
  1475. #endif