curl_quiche.c 47 KB

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