curl_quiche.c 49 KB

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