curl_quiche.c 46 KB

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