curl_quiche.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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 seems good.
  62. * More does not seem to improve performance. The benefit of the pool is that
  63. * stream buffer to not keep spares. Memory consumption goes down when streams
  64. * run empty, have a large upload done, etc. */
  65. #define H3_STREAM_POOL_SPARES \
  66. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
  67. /* Receive and Send max number of chunks just follows from the
  68. * chunk size and window size */
  69. #define H3_STREAM_RECV_CHUNKS \
  70. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  71. #define H3_STREAM_SEND_CHUNKS \
  72. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  73. /*
  74. * Store quiche version info in this buffer.
  75. */
  76. void Curl_quiche_ver(char *p, size_t len)
  77. {
  78. (void)msnprintf(p, len, "quiche/%s", quiche_version());
  79. }
  80. struct cf_quiche_ctx {
  81. struct cf_quic_ctx q;
  82. struct ssl_peer peer;
  83. struct curl_tls_ctx tls;
  84. quiche_conn *qconn;
  85. quiche_config *cfg;
  86. quiche_h3_conn *h3c;
  87. quiche_h3_config *h3config;
  88. uint8_t scid[QUICHE_MAX_CONN_ID_LEN];
  89. struct curltime started_at; /* time the current attempt started */
  90. struct curltime handshake_at; /* time connect handshake finished */
  91. struct curltime reconnect_at; /* time the next attempt should start */
  92. struct bufc_pool stream_bufcp; /* chunk pool for streams */
  93. struct Curl_hash streams; /* hash `data->id` to `stream_ctx` */
  94. curl_off_t data_recvd;
  95. BIT(goaway); /* got GOAWAY from server */
  96. BIT(x509_store_setup); /* if x509 store has been set up */
  97. BIT(shutdown_started); /* queued shutdown packets */
  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 : (uint64_t)~0, 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, "cannot 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, "cannot 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 CURLcode cf_quiche_shutdown(struct Curl_cfilter *cf,
  1314. struct Curl_easy *data, bool *done)
  1315. {
  1316. struct cf_quiche_ctx *ctx = cf->ctx;
  1317. CURLcode result = CURLE_OK;
  1318. if(cf->shutdown || !ctx || !ctx->qconn) {
  1319. *done = TRUE;
  1320. return CURLE_OK;
  1321. }
  1322. *done = FALSE;
  1323. if(!ctx->shutdown_started) {
  1324. int err;
  1325. ctx->shutdown_started = TRUE;
  1326. vquic_ctx_update_time(&ctx->q);
  1327. err = quiche_conn_close(ctx->qconn, TRUE, 0, NULL, 0);
  1328. if(err) {
  1329. CURL_TRC_CF(data, cf, "error %d adding shutdown packet, "
  1330. "aborting shutdown", err);
  1331. result = CURLE_SEND_ERROR;
  1332. goto out;
  1333. }
  1334. }
  1335. if(!Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1336. CURL_TRC_CF(data, cf, "shutdown, flushing sendbuf");
  1337. result = cf_flush_egress(cf, data);
  1338. if(result)
  1339. goto out;
  1340. }
  1341. if(Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1342. /* sent everything, quiche does not seem to support a graceful
  1343. * shutdown waiting for a reply, so ware done. */
  1344. CURL_TRC_CF(data, cf, "shutdown completely sent off, done");
  1345. *done = TRUE;
  1346. }
  1347. else {
  1348. CURL_TRC_CF(data, cf, "shutdown sending blocked");
  1349. }
  1350. out:
  1351. return result;
  1352. }
  1353. static void cf_quiche_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1354. {
  1355. struct cf_quiche_ctx *ctx = cf->ctx;
  1356. if(ctx) {
  1357. bool done;
  1358. (void)cf_quiche_shutdown(cf, data, &done);
  1359. cf_quiche_ctx_clear(ctx);
  1360. }
  1361. }
  1362. static void cf_quiche_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1363. {
  1364. struct cf_quiche_ctx *ctx = cf->ctx;
  1365. (void)data;
  1366. cf_quiche_ctx_clear(ctx);
  1367. free(ctx);
  1368. cf->ctx = NULL;
  1369. }
  1370. static CURLcode cf_quiche_query(struct Curl_cfilter *cf,
  1371. struct Curl_easy *data,
  1372. int query, int *pres1, void *pres2)
  1373. {
  1374. struct cf_quiche_ctx *ctx = cf->ctx;
  1375. switch(query) {
  1376. case CF_QUERY_MAX_CONCURRENT: {
  1377. curl_uint64_t max_streams = CONN_INUSE(cf->conn);
  1378. if(!ctx->goaway) {
  1379. max_streams += quiche_conn_peer_streams_left_bidi(ctx->qconn);
  1380. }
  1381. *pres1 = (max_streams > INT_MAX)? INT_MAX : (int)max_streams;
  1382. CURL_TRC_CF(data, cf, "query conn[%" CURL_FORMAT_CURL_OFF_T "]: "
  1383. "MAX_CONCURRENT -> %d (%zu in use)",
  1384. cf->conn->connection_id, *pres1, CONN_INUSE(cf->conn));
  1385. return CURLE_OK;
  1386. }
  1387. case CF_QUERY_CONNECT_REPLY_MS:
  1388. if(ctx->q.got_first_byte) {
  1389. timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
  1390. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  1391. }
  1392. else
  1393. *pres1 = -1;
  1394. return CURLE_OK;
  1395. case CF_QUERY_TIMER_CONNECT: {
  1396. struct curltime *when = pres2;
  1397. if(ctx->q.got_first_byte)
  1398. *when = ctx->q.first_byte_at;
  1399. return CURLE_OK;
  1400. }
  1401. case CF_QUERY_TIMER_APPCONNECT: {
  1402. struct curltime *when = pres2;
  1403. if(cf->connected)
  1404. *when = ctx->handshake_at;
  1405. return CURLE_OK;
  1406. }
  1407. default:
  1408. break;
  1409. }
  1410. return cf->next?
  1411. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1412. CURLE_UNKNOWN_OPTION;
  1413. }
  1414. static bool cf_quiche_conn_is_alive(struct Curl_cfilter *cf,
  1415. struct Curl_easy *data,
  1416. bool *input_pending)
  1417. {
  1418. struct cf_quiche_ctx *ctx = cf->ctx;
  1419. bool alive = TRUE;
  1420. *input_pending = FALSE;
  1421. if(!ctx->qconn)
  1422. return FALSE;
  1423. if(quiche_conn_is_closed(ctx->qconn)) {
  1424. if(quiche_conn_is_timed_out(ctx->qconn))
  1425. CURL_TRC_CF(data, cf, "connection was closed due to idle timeout");
  1426. else
  1427. CURL_TRC_CF(data, cf, "connection is closed");
  1428. return FALSE;
  1429. }
  1430. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  1431. return FALSE;
  1432. if(*input_pending) {
  1433. /* This happens before we have sent off a request and the connection is
  1434. not in use by any other transfer, there should not be any data here,
  1435. only "protocol frames" */
  1436. *input_pending = FALSE;
  1437. if(cf_process_ingress(cf, data))
  1438. alive = FALSE;
  1439. else {
  1440. alive = TRUE;
  1441. }
  1442. }
  1443. return alive;
  1444. }
  1445. struct Curl_cftype Curl_cft_http3 = {
  1446. "HTTP/3",
  1447. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  1448. 0,
  1449. cf_quiche_destroy,
  1450. cf_quiche_connect,
  1451. cf_quiche_close,
  1452. cf_quiche_shutdown,
  1453. Curl_cf_def_get_host,
  1454. cf_quiche_adjust_pollset,
  1455. cf_quiche_data_pending,
  1456. cf_quiche_send,
  1457. cf_quiche_recv,
  1458. cf_quiche_data_event,
  1459. cf_quiche_conn_is_alive,
  1460. Curl_cf_def_conn_keep_alive,
  1461. cf_quiche_query,
  1462. };
  1463. CURLcode Curl_cf_quiche_create(struct Curl_cfilter **pcf,
  1464. struct Curl_easy *data,
  1465. struct connectdata *conn,
  1466. const struct Curl_addrinfo *ai)
  1467. {
  1468. struct cf_quiche_ctx *ctx = NULL;
  1469. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  1470. CURLcode result;
  1471. (void)data;
  1472. (void)conn;
  1473. ctx = calloc(1, sizeof(*ctx));
  1474. if(!ctx) {
  1475. result = CURLE_OUT_OF_MEMORY;
  1476. goto out;
  1477. }
  1478. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  1479. if(result)
  1480. goto out;
  1481. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  1482. if(result)
  1483. goto out;
  1484. udp_cf->conn = cf->conn;
  1485. udp_cf->sockindex = cf->sockindex;
  1486. cf->next = udp_cf;
  1487. out:
  1488. *pcf = (!result)? cf : NULL;
  1489. if(result) {
  1490. if(udp_cf)
  1491. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  1492. Curl_safefree(cf);
  1493. Curl_safefree(ctx);
  1494. }
  1495. return result;
  1496. }
  1497. bool Curl_conn_is_quiche(const struct Curl_easy *data,
  1498. const struct connectdata *conn,
  1499. int sockindex)
  1500. {
  1501. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  1502. (void)data;
  1503. for(; cf; cf = cf->next) {
  1504. if(cf->cft == &Curl_cft_http3)
  1505. return TRUE;
  1506. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  1507. return FALSE;
  1508. }
  1509. return FALSE;
  1510. }
  1511. #endif