curl_ngtcp2.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  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. #if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
  26. #include <ngtcp2/ngtcp2.h>
  27. #include <nghttp3/nghttp3.h>
  28. #ifdef USE_OPENSSL
  29. #include <openssl/err.h>
  30. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  31. #include <ngtcp2/ngtcp2_crypto_boringssl.h>
  32. #else
  33. #include <ngtcp2/ngtcp2_crypto_quictls.h>
  34. #endif
  35. #include "vtls/openssl.h"
  36. #elif defined(USE_GNUTLS)
  37. #include <ngtcp2/ngtcp2_crypto_gnutls.h>
  38. #include "vtls/gtls.h"
  39. #elif defined(USE_WOLFSSL)
  40. #include <ngtcp2/ngtcp2_crypto_wolfssl.h>
  41. #endif
  42. #include "urldata.h"
  43. #include "hash.h"
  44. #include "sendf.h"
  45. #include "strdup.h"
  46. #include "rand.h"
  47. #include "multiif.h"
  48. #include "strcase.h"
  49. #include "cfilters.h"
  50. #include "cf-socket.h"
  51. #include "connect.h"
  52. #include "progress.h"
  53. #include "strerror.h"
  54. #include "dynbuf.h"
  55. #include "http1.h"
  56. #include "select.h"
  57. #include "inet_pton.h"
  58. #include "transfer.h"
  59. #include "vquic.h"
  60. #include "vquic_int.h"
  61. #include "vquic-tls.h"
  62. #include "vtls/keylog.h"
  63. #include "vtls/vtls.h"
  64. #include "curl_ngtcp2.h"
  65. #include "warnless.h"
  66. /* The last 3 #include files should be in this order */
  67. #include "curl_printf.h"
  68. #include "curl_memory.h"
  69. #include "memdebug.h"
  70. #define QUIC_MAX_STREAMS (256*1024)
  71. #define QUIC_MAX_DATA (1*1024*1024)
  72. #define QUIC_HANDSHAKE_TIMEOUT (10*NGTCP2_SECONDS)
  73. /* A stream window is the maximum amount we need to buffer for
  74. * each active transfer. We use HTTP/3 flow control and only ACK
  75. * when we take things out of the buffer.
  76. * Chunk size is large enough to take a full DATA frame */
  77. #define H3_STREAM_WINDOW_SIZE (128 * 1024)
  78. #define H3_STREAM_CHUNK_SIZE (16 * 1024)
  79. /* The pool keeps spares around and half of a full stream windows
  80. * seems good. More does not seem to improve performance.
  81. * The benefit of the pool is that stream buffer to not keep
  82. * spares. So memory consumption goes down when streams run empty,
  83. * have a large upload done, etc. */
  84. #define H3_STREAM_POOL_SPARES \
  85. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE ) / 2
  86. /* Receive and Send max number of chunks just follows from the
  87. * chunk size and window size */
  88. #define H3_STREAM_RECV_CHUNKS \
  89. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  90. #define H3_STREAM_SEND_CHUNKS \
  91. (H3_STREAM_WINDOW_SIZE / H3_STREAM_CHUNK_SIZE)
  92. /*
  93. * Store ngtcp2 version info in this buffer.
  94. */
  95. void Curl_ngtcp2_ver(char *p, size_t len)
  96. {
  97. const ngtcp2_info *ng2 = ngtcp2_version(0);
  98. const nghttp3_info *ht3 = nghttp3_version(0);
  99. (void)msnprintf(p, len, "ngtcp2/%s nghttp3/%s",
  100. ng2->version_str, ht3->version_str);
  101. }
  102. struct cf_ngtcp2_ctx {
  103. struct cf_quic_ctx q;
  104. struct ssl_peer peer;
  105. struct curl_tls_ctx tls;
  106. ngtcp2_path connected_path;
  107. ngtcp2_conn *qconn;
  108. ngtcp2_cid dcid;
  109. ngtcp2_cid scid;
  110. uint32_t version;
  111. ngtcp2_settings settings;
  112. ngtcp2_transport_params transport_params;
  113. ngtcp2_ccerr last_error;
  114. ngtcp2_crypto_conn_ref conn_ref;
  115. struct cf_call_data call_data;
  116. nghttp3_conn *h3conn;
  117. nghttp3_settings h3settings;
  118. struct curltime started_at; /* time the current attempt started */
  119. struct curltime handshake_at; /* time connect handshake finished */
  120. struct curltime reconnect_at; /* time the next attempt should start */
  121. struct bufc_pool stream_bufcp; /* chunk pool for streams */
  122. struct dynbuf scratch; /* temp buffer for header construction */
  123. struct Curl_hash streams; /* hash `data->id` to `h3_stream_ctx` */
  124. size_t max_stream_window; /* max flow window for one stream */
  125. uint64_t max_idle_ms; /* max idle time for QUIC connection */
  126. int qlogfd;
  127. };
  128. /* How to access `call_data` from a cf_ngtcp2 filter */
  129. #undef CF_CTX_CALL_DATA
  130. #define CF_CTX_CALL_DATA(cf) \
  131. ((struct cf_ngtcp2_ctx *)(cf)->ctx)->call_data
  132. /**
  133. * All about the H3 internals of a stream
  134. */
  135. struct h3_stream_ctx {
  136. curl_int64_t id; /* HTTP/3 protocol identifier */
  137. struct bufq sendbuf; /* h3 request body */
  138. struct h1_req_parser h1; /* h1 request parsing */
  139. size_t sendbuf_len_in_flight; /* sendbuf amount "in flight" */
  140. size_t upload_blocked_len; /* the amount written last and EGAINed */
  141. curl_uint64_t error3; /* HTTP/3 stream error code */
  142. curl_off_t upload_left; /* number of request bytes left to upload */
  143. int status_code; /* HTTP status code */
  144. CURLcode xfer_result; /* result from xfer_resp_write(_hd) */
  145. bool resp_hds_complete; /* we have a complete, final response */
  146. bool closed; /* TRUE on stream close */
  147. bool reset; /* TRUE on stream reset */
  148. bool send_closed; /* stream is local closed */
  149. BIT(quic_flow_blocked); /* stream is blocked by QUIC flow control */
  150. };
  151. #define H3_STREAM_CTX(ctx,data) ((struct h3_stream_ctx *)(\
  152. data? Curl_hash_offt_get(&(ctx)->streams, (data)->id) : NULL))
  153. static void h3_stream_ctx_free(struct h3_stream_ctx *stream)
  154. {
  155. Curl_bufq_free(&stream->sendbuf);
  156. Curl_h1_req_parse_free(&stream->h1);
  157. free(stream);
  158. }
  159. static void h3_stream_hash_free(void *stream)
  160. {
  161. DEBUGASSERT(stream);
  162. h3_stream_ctx_free((struct h3_stream_ctx *)stream);
  163. }
  164. static CURLcode h3_data_setup(struct Curl_cfilter *cf,
  165. struct Curl_easy *data)
  166. {
  167. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  168. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  169. if(!data || !data->req.p.http) {
  170. failf(data, "initialization failure, transfer not http initialized");
  171. return CURLE_FAILED_INIT;
  172. }
  173. if(stream)
  174. return CURLE_OK;
  175. stream = calloc(1, sizeof(*stream));
  176. if(!stream)
  177. return CURLE_OUT_OF_MEMORY;
  178. stream->id = -1;
  179. /* on send, we control how much we put into the buffer */
  180. Curl_bufq_initp(&stream->sendbuf, &ctx->stream_bufcp,
  181. H3_STREAM_SEND_CHUNKS, BUFQ_OPT_NONE);
  182. stream->sendbuf_len_in_flight = 0;
  183. Curl_h1_req_parse_init(&stream->h1, H1_PARSE_DEFAULT_MAX_LINE_LEN);
  184. if(!Curl_hash_offt_set(&ctx->streams, data->id, stream)) {
  185. h3_stream_ctx_free(stream);
  186. return CURLE_OUT_OF_MEMORY;
  187. }
  188. return CURLE_OK;
  189. }
  190. static void h3_data_done(struct Curl_cfilter *cf, struct Curl_easy *data)
  191. {
  192. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  193. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  194. (void)cf;
  195. if(stream) {
  196. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] easy handle is done",
  197. stream->id);
  198. if(ctx->h3conn && !stream->closed) {
  199. nghttp3_conn_shutdown_stream_read(ctx->h3conn, stream->id);
  200. nghttp3_conn_close_stream(ctx->h3conn, stream->id,
  201. NGHTTP3_H3_REQUEST_CANCELLED);
  202. nghttp3_conn_set_stream_user_data(ctx->h3conn, stream->id, NULL);
  203. ngtcp2_conn_set_stream_user_data(ctx->qconn, stream->id, NULL);
  204. stream->closed = TRUE;
  205. }
  206. Curl_hash_offt_remove(&ctx->streams, data->id);
  207. }
  208. }
  209. static struct Curl_easy *get_stream_easy(struct Curl_cfilter *cf,
  210. struct Curl_easy *data,
  211. int64_t stream_id,
  212. struct h3_stream_ctx **pstream)
  213. {
  214. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  215. struct Curl_easy *sdata;
  216. struct h3_stream_ctx *stream;
  217. (void)cf;
  218. stream = H3_STREAM_CTX(ctx, data);
  219. if(stream && stream->id == stream_id) {
  220. *pstream = stream;
  221. return data;
  222. }
  223. else {
  224. DEBUGASSERT(data->multi);
  225. for(sdata = data->multi->easyp; sdata; sdata = sdata->next) {
  226. if(sdata->conn != data->conn)
  227. continue;
  228. stream = H3_STREAM_CTX(ctx, sdata);
  229. if(stream && stream->id == stream_id) {
  230. *pstream = stream;
  231. return sdata;
  232. }
  233. }
  234. }
  235. *pstream = NULL;
  236. return NULL;
  237. }
  238. static void h3_drain_stream(struct Curl_cfilter *cf,
  239. struct Curl_easy *data)
  240. {
  241. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  242. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  243. unsigned char bits;
  244. (void)cf;
  245. bits = CURL_CSELECT_IN;
  246. if(stream && stream->upload_left && !stream->send_closed)
  247. bits |= CURL_CSELECT_OUT;
  248. if(data->state.select_bits != bits) {
  249. data->state.select_bits = bits;
  250. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  251. }
  252. }
  253. /* ngtcp2 default congestion controller does not perform pacing. Limit
  254. the maximum packet burst to MAX_PKT_BURST packets. */
  255. #define MAX_PKT_BURST 10
  256. struct pkt_io_ctx {
  257. struct Curl_cfilter *cf;
  258. struct Curl_easy *data;
  259. ngtcp2_tstamp ts;
  260. size_t pkt_count;
  261. ngtcp2_path_storage ps;
  262. };
  263. static void pktx_update_time(struct pkt_io_ctx *pktx,
  264. struct Curl_cfilter *cf)
  265. {
  266. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  267. vquic_ctx_update_time(&ctx->q);
  268. pktx->ts = ctx->q.last_op.tv_sec * NGTCP2_SECONDS +
  269. ctx->q.last_op.tv_usec * NGTCP2_MICROSECONDS;
  270. }
  271. static void pktx_init(struct pkt_io_ctx *pktx,
  272. struct Curl_cfilter *cf,
  273. struct Curl_easy *data)
  274. {
  275. pktx->cf = cf;
  276. pktx->data = data;
  277. pktx->pkt_count = 0;
  278. ngtcp2_path_storage_zero(&pktx->ps);
  279. pktx_update_time(pktx, cf);
  280. }
  281. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  282. struct Curl_easy *data,
  283. struct pkt_io_ctx *pktx);
  284. static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
  285. struct Curl_easy *data,
  286. struct pkt_io_ctx *pktx);
  287. static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
  288. uint64_t datalen, void *user_data,
  289. void *stream_user_data);
  290. static ngtcp2_conn *get_conn(ngtcp2_crypto_conn_ref *conn_ref)
  291. {
  292. struct Curl_cfilter *cf = conn_ref->user_data;
  293. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  294. return ctx->qconn;
  295. }
  296. #ifdef DEBUG_NGTCP2
  297. static void quic_printf(void *user_data, const char *fmt, ...)
  298. {
  299. struct Curl_cfilter *cf = user_data;
  300. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  301. (void)ctx; /* TODO: need an easy handle to infof() message */
  302. va_list ap;
  303. va_start(ap, fmt);
  304. vfprintf(stderr, fmt, ap);
  305. va_end(ap);
  306. fprintf(stderr, "\n");
  307. }
  308. #endif
  309. static void qlog_callback(void *user_data, uint32_t flags,
  310. const void *data, size_t datalen)
  311. {
  312. struct Curl_cfilter *cf = user_data;
  313. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  314. (void)flags;
  315. if(ctx->qlogfd != -1) {
  316. ssize_t rc = write(ctx->qlogfd, data, datalen);
  317. if(rc == -1) {
  318. /* on write error, stop further write attempts */
  319. close(ctx->qlogfd);
  320. ctx->qlogfd = -1;
  321. }
  322. }
  323. }
  324. static void quic_settings(struct cf_ngtcp2_ctx *ctx,
  325. struct Curl_easy *data,
  326. struct pkt_io_ctx *pktx)
  327. {
  328. ngtcp2_settings *s = &ctx->settings;
  329. ngtcp2_transport_params *t = &ctx->transport_params;
  330. ngtcp2_settings_default(s);
  331. ngtcp2_transport_params_default(t);
  332. #ifdef DEBUG_NGTCP2
  333. s->log_printf = quic_printf;
  334. #else
  335. s->log_printf = NULL;
  336. #endif
  337. (void)data;
  338. s->initial_ts = pktx->ts;
  339. s->handshake_timeout = QUIC_HANDSHAKE_TIMEOUT;
  340. s->max_window = 100 * ctx->max_stream_window;
  341. s->max_stream_window = ctx->max_stream_window;
  342. t->initial_max_data = 10 * ctx->max_stream_window;
  343. t->initial_max_stream_data_bidi_local = ctx->max_stream_window;
  344. t->initial_max_stream_data_bidi_remote = ctx->max_stream_window;
  345. t->initial_max_stream_data_uni = ctx->max_stream_window;
  346. t->initial_max_streams_bidi = QUIC_MAX_STREAMS;
  347. t->initial_max_streams_uni = QUIC_MAX_STREAMS;
  348. t->max_idle_timeout = (ctx->max_idle_ms * NGTCP2_MILLISECONDS);
  349. if(ctx->qlogfd != -1) {
  350. s->qlog_write = qlog_callback;
  351. }
  352. }
  353. static int init_ngh3_conn(struct Curl_cfilter *cf);
  354. static int cb_handshake_completed(ngtcp2_conn *tconn, void *user_data)
  355. {
  356. (void)user_data;
  357. (void)tconn;
  358. return 0;
  359. }
  360. static int cb_recv_stream_data(ngtcp2_conn *tconn, uint32_t flags,
  361. int64_t sid, uint64_t offset,
  362. const uint8_t *buf, size_t buflen,
  363. void *user_data, void *stream_user_data)
  364. {
  365. struct Curl_cfilter *cf = user_data;
  366. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  367. curl_int64_t stream_id = (curl_int64_t)sid;
  368. nghttp3_ssize nconsumed;
  369. int fin = (flags & NGTCP2_STREAM_DATA_FLAG_FIN) ? 1 : 0;
  370. struct Curl_easy *data = stream_user_data;
  371. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  372. (void)offset;
  373. (void)data;
  374. nconsumed =
  375. nghttp3_conn_read_stream(ctx->h3conn, stream_id, buf, buflen, fin);
  376. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read_stream(len=%zu) -> %zd",
  377. stream_id, buflen, nconsumed);
  378. if(nconsumed < 0) {
  379. /* consume all bytes */
  380. ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, buflen);
  381. ngtcp2_conn_extend_max_offset(tconn, buflen);
  382. if(!data || (stream && stream->reset) ||
  383. NGHTTP3_ERR_H3_STREAM_CREATION_ERROR == (int)nconsumed) {
  384. struct Curl_easy *cdata = CF_DATA_CURRENT(cf);
  385. CURL_TRC_CF(cdata, cf, "[%" CURL_PRId64 "] discard data for stream %s",
  386. stream_id, (data && stream)? "reset" : "unknown");
  387. return 0;
  388. }
  389. ngtcp2_ccerr_set_application_error(
  390. &ctx->last_error,
  391. nghttp3_err_infer_quic_app_error_code((int)nconsumed), NULL, 0);
  392. return NGTCP2_ERR_CALLBACK_FAILURE;
  393. }
  394. /* number of bytes inside buflen which consists of framing overhead
  395. * including QPACK HEADERS. In other words, it does not consume payload of
  396. * DATA frame. */
  397. ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, nconsumed);
  398. ngtcp2_conn_extend_max_offset(tconn, nconsumed);
  399. return 0;
  400. }
  401. static int
  402. cb_acked_stream_data_offset(ngtcp2_conn *tconn, int64_t stream_id,
  403. uint64_t offset, uint64_t datalen, void *user_data,
  404. void *stream_user_data)
  405. {
  406. struct Curl_cfilter *cf = user_data;
  407. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  408. int rv;
  409. (void)stream_id;
  410. (void)tconn;
  411. (void)offset;
  412. (void)datalen;
  413. (void)stream_user_data;
  414. rv = nghttp3_conn_add_ack_offset(ctx->h3conn, stream_id, datalen);
  415. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  416. return NGTCP2_ERR_CALLBACK_FAILURE;
  417. }
  418. return 0;
  419. }
  420. static int cb_stream_close(ngtcp2_conn *tconn, uint32_t flags,
  421. int64_t sid, uint64_t app_error_code,
  422. void *user_data, void *stream_user_data)
  423. {
  424. struct Curl_cfilter *cf = user_data;
  425. struct Curl_easy *data = stream_user_data;
  426. curl_int64_t stream_id = (curl_int64_t)sid;
  427. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  428. int rv;
  429. (void)tconn;
  430. (void)data;
  431. /* stream is closed... */
  432. if(!(flags & NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET)) {
  433. app_error_code = NGHTTP3_H3_NO_ERROR;
  434. }
  435. rv = nghttp3_conn_close_stream(ctx->h3conn, stream_id, app_error_code);
  436. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] quic close(err=%"
  437. CURL_PRIu64 ") -> %d", stream_id, (curl_uint64_t)app_error_code,
  438. rv);
  439. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  440. ngtcp2_ccerr_set_application_error(
  441. &ctx->last_error, nghttp3_err_infer_quic_app_error_code(rv), NULL, 0);
  442. return NGTCP2_ERR_CALLBACK_FAILURE;
  443. }
  444. return 0;
  445. }
  446. static int cb_stream_reset(ngtcp2_conn *tconn, int64_t sid,
  447. uint64_t final_size, uint64_t app_error_code,
  448. void *user_data, void *stream_user_data)
  449. {
  450. struct Curl_cfilter *cf = user_data;
  451. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  452. curl_int64_t stream_id = (curl_int64_t)sid;
  453. struct Curl_easy *data = stream_user_data;
  454. int rv;
  455. (void)tconn;
  456. (void)final_size;
  457. (void)app_error_code;
  458. (void)data;
  459. rv = nghttp3_conn_shutdown_stream_read(ctx->h3conn, stream_id);
  460. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] reset -> %d", stream_id, rv);
  461. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  462. return NGTCP2_ERR_CALLBACK_FAILURE;
  463. }
  464. return 0;
  465. }
  466. static int cb_stream_stop_sending(ngtcp2_conn *tconn, int64_t stream_id,
  467. uint64_t app_error_code, void *user_data,
  468. void *stream_user_data)
  469. {
  470. struct Curl_cfilter *cf = user_data;
  471. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  472. int rv;
  473. (void)tconn;
  474. (void)app_error_code;
  475. (void)stream_user_data;
  476. rv = nghttp3_conn_shutdown_stream_read(ctx->h3conn, stream_id);
  477. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  478. return NGTCP2_ERR_CALLBACK_FAILURE;
  479. }
  480. return 0;
  481. }
  482. static int cb_extend_max_local_streams_bidi(ngtcp2_conn *tconn,
  483. uint64_t max_streams,
  484. void *user_data)
  485. {
  486. (void)tconn;
  487. (void)max_streams;
  488. (void)user_data;
  489. return 0;
  490. }
  491. static int cb_extend_max_stream_data(ngtcp2_conn *tconn, int64_t sid,
  492. uint64_t max_data, void *user_data,
  493. void *stream_user_data)
  494. {
  495. struct Curl_cfilter *cf = user_data;
  496. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  497. curl_int64_t stream_id = (curl_int64_t)sid;
  498. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  499. struct Curl_easy *s_data;
  500. struct h3_stream_ctx *stream;
  501. int rv;
  502. (void)tconn;
  503. (void)max_data;
  504. (void)stream_user_data;
  505. rv = nghttp3_conn_unblock_stream(ctx->h3conn, stream_id);
  506. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  507. return NGTCP2_ERR_CALLBACK_FAILURE;
  508. }
  509. s_data = get_stream_easy(cf, data, stream_id, &stream);
  510. if(s_data && stream && stream->quic_flow_blocked) {
  511. CURL_TRC_CF(s_data, cf, "[%" CURL_PRId64 "] unblock quic flow",
  512. stream_id);
  513. stream->quic_flow_blocked = FALSE;
  514. h3_drain_stream(cf, s_data);
  515. }
  516. return 0;
  517. }
  518. static void cb_rand(uint8_t *dest, size_t destlen,
  519. const ngtcp2_rand_ctx *rand_ctx)
  520. {
  521. CURLcode result;
  522. (void)rand_ctx;
  523. result = Curl_rand(NULL, dest, destlen);
  524. if(result) {
  525. /* cb_rand is only used for non-cryptographic context. If Curl_rand
  526. failed, just fill 0 and call it *random*. */
  527. memset(dest, 0, destlen);
  528. }
  529. }
  530. static int cb_get_new_connection_id(ngtcp2_conn *tconn, ngtcp2_cid *cid,
  531. uint8_t *token, size_t cidlen,
  532. void *user_data)
  533. {
  534. CURLcode result;
  535. (void)tconn;
  536. (void)user_data;
  537. result = Curl_rand(NULL, cid->data, cidlen);
  538. if(result)
  539. return NGTCP2_ERR_CALLBACK_FAILURE;
  540. cid->datalen = cidlen;
  541. result = Curl_rand(NULL, token, NGTCP2_STATELESS_RESET_TOKENLEN);
  542. if(result)
  543. return NGTCP2_ERR_CALLBACK_FAILURE;
  544. return 0;
  545. }
  546. static int cb_recv_rx_key(ngtcp2_conn *tconn, ngtcp2_encryption_level level,
  547. void *user_data)
  548. {
  549. struct Curl_cfilter *cf = user_data;
  550. (void)tconn;
  551. if(level != NGTCP2_ENCRYPTION_LEVEL_1RTT) {
  552. return 0;
  553. }
  554. if(init_ngh3_conn(cf) != CURLE_OK) {
  555. return NGTCP2_ERR_CALLBACK_FAILURE;
  556. }
  557. return 0;
  558. }
  559. static ngtcp2_callbacks ng_callbacks = {
  560. ngtcp2_crypto_client_initial_cb,
  561. NULL, /* recv_client_initial */
  562. ngtcp2_crypto_recv_crypto_data_cb,
  563. cb_handshake_completed,
  564. NULL, /* recv_version_negotiation */
  565. ngtcp2_crypto_encrypt_cb,
  566. ngtcp2_crypto_decrypt_cb,
  567. ngtcp2_crypto_hp_mask_cb,
  568. cb_recv_stream_data,
  569. cb_acked_stream_data_offset,
  570. NULL, /* stream_open */
  571. cb_stream_close,
  572. NULL, /* recv_stateless_reset */
  573. ngtcp2_crypto_recv_retry_cb,
  574. cb_extend_max_local_streams_bidi,
  575. NULL, /* extend_max_local_streams_uni */
  576. cb_rand,
  577. cb_get_new_connection_id,
  578. NULL, /* remove_connection_id */
  579. ngtcp2_crypto_update_key_cb, /* update_key */
  580. NULL, /* path_validation */
  581. NULL, /* select_preferred_addr */
  582. cb_stream_reset,
  583. NULL, /* extend_max_remote_streams_bidi */
  584. NULL, /* extend_max_remote_streams_uni */
  585. cb_extend_max_stream_data,
  586. NULL, /* dcid_status */
  587. NULL, /* handshake_confirmed */
  588. NULL, /* recv_new_token */
  589. ngtcp2_crypto_delete_crypto_aead_ctx_cb,
  590. ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
  591. NULL, /* recv_datagram */
  592. NULL, /* ack_datagram */
  593. NULL, /* lost_datagram */
  594. ngtcp2_crypto_get_path_challenge_data_cb,
  595. cb_stream_stop_sending,
  596. NULL, /* version_negotiation */
  597. cb_recv_rx_key,
  598. NULL, /* recv_tx_key */
  599. NULL, /* early_data_rejected */
  600. };
  601. /**
  602. * Connection maintenance like timeouts on packet ACKs etc. are done by us, not
  603. * the OS like for TCP. POLL events on the socket therefore are not
  604. * sufficient.
  605. * ngtcp2 tells us when it wants to be invoked again. We handle that via
  606. * the `Curl_expire()` mechanisms.
  607. */
  608. static CURLcode check_and_set_expiry(struct Curl_cfilter *cf,
  609. struct Curl_easy *data,
  610. struct pkt_io_ctx *pktx)
  611. {
  612. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  613. struct pkt_io_ctx local_pktx;
  614. ngtcp2_tstamp expiry;
  615. if(!pktx) {
  616. pktx_init(&local_pktx, cf, data);
  617. pktx = &local_pktx;
  618. }
  619. else {
  620. pktx_update_time(pktx, cf);
  621. }
  622. expiry = ngtcp2_conn_get_expiry(ctx->qconn);
  623. if(expiry != UINT64_MAX) {
  624. if(expiry <= pktx->ts) {
  625. CURLcode result;
  626. int rv = ngtcp2_conn_handle_expiry(ctx->qconn, pktx->ts);
  627. if(rv) {
  628. failf(data, "ngtcp2_conn_handle_expiry returned error: %s",
  629. ngtcp2_strerror(rv));
  630. ngtcp2_ccerr_set_liberr(&ctx->last_error, rv, NULL, 0);
  631. return CURLE_SEND_ERROR;
  632. }
  633. result = cf_progress_ingress(cf, data, pktx);
  634. if(result)
  635. return result;
  636. result = cf_progress_egress(cf, data, pktx);
  637. if(result)
  638. return result;
  639. /* ask again, things might have changed */
  640. expiry = ngtcp2_conn_get_expiry(ctx->qconn);
  641. }
  642. if(expiry > pktx->ts) {
  643. ngtcp2_duration timeout = expiry - pktx->ts;
  644. if(timeout % NGTCP2_MILLISECONDS) {
  645. timeout += NGTCP2_MILLISECONDS;
  646. }
  647. Curl_expire(data, timeout / NGTCP2_MILLISECONDS, EXPIRE_QUIC);
  648. }
  649. }
  650. return CURLE_OK;
  651. }
  652. static void cf_ngtcp2_adjust_pollset(struct Curl_cfilter *cf,
  653. struct Curl_easy *data,
  654. struct easy_pollset *ps)
  655. {
  656. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  657. bool want_recv, want_send;
  658. if(!ctx->qconn)
  659. return;
  660. Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
  661. if(want_recv || want_send) {
  662. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  663. struct cf_call_data save;
  664. bool c_exhaust, s_exhaust;
  665. CF_DATA_SAVE(save, cf, data);
  666. c_exhaust = want_send && (!ngtcp2_conn_get_cwnd_left(ctx->qconn) ||
  667. !ngtcp2_conn_get_max_data_left(ctx->qconn));
  668. s_exhaust = want_send && stream && stream->id >= 0 &&
  669. stream->quic_flow_blocked;
  670. want_recv = (want_recv || c_exhaust || s_exhaust);
  671. want_send = (!s_exhaust && want_send) ||
  672. !Curl_bufq_is_empty(&ctx->q.sendbuf);
  673. Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
  674. CF_DATA_RESTORE(cf, save);
  675. }
  676. }
  677. static int cb_h3_stream_close(nghttp3_conn *conn, int64_t sid,
  678. uint64_t app_error_code, void *user_data,
  679. void *stream_user_data)
  680. {
  681. struct Curl_cfilter *cf = user_data;
  682. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  683. struct Curl_easy *data = stream_user_data;
  684. curl_int64_t stream_id = (curl_int64_t)sid;
  685. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  686. (void)conn;
  687. (void)stream_id;
  688. /* we might be called by nghttp3 after we already cleaned up */
  689. if(!stream)
  690. return 0;
  691. stream->closed = TRUE;
  692. stream->error3 = (curl_uint64_t)app_error_code;
  693. if(stream->error3 != NGHTTP3_H3_NO_ERROR) {
  694. stream->reset = TRUE;
  695. stream->send_closed = TRUE;
  696. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] RESET: error %" CURL_PRIu64,
  697. stream->id, stream->error3);
  698. }
  699. else {
  700. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] CLOSED", stream->id);
  701. }
  702. h3_drain_stream(cf, data);
  703. return 0;
  704. }
  705. static void h3_xfer_write_resp_hd(struct Curl_cfilter *cf,
  706. struct Curl_easy *data,
  707. struct h3_stream_ctx *stream,
  708. const char *buf, size_t blen, bool eos)
  709. {
  710. /* If we already encountered an error, skip further writes */
  711. if(!stream->xfer_result) {
  712. stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
  713. if(stream->xfer_result)
  714. CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu "
  715. "bytes of headers", stream->id, stream->xfer_result, blen);
  716. }
  717. }
  718. static void h3_xfer_write_resp(struct Curl_cfilter *cf,
  719. struct Curl_easy *data,
  720. struct h3_stream_ctx *stream,
  721. const char *buf, size_t blen, bool eos)
  722. {
  723. /* If we already encountered an error, skip further writes */
  724. if(!stream->xfer_result)
  725. stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
  726. /* If the transfer write is errored, we do not want any more data */
  727. if(stream->xfer_result) {
  728. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  729. CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu bytes "
  730. "of data, cancelling stream",
  731. stream->id, stream->xfer_result, blen);
  732. nghttp3_conn_close_stream(ctx->h3conn, stream->id,
  733. NGHTTP3_H3_REQUEST_CANCELLED);
  734. }
  735. }
  736. static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream3_id,
  737. const uint8_t *buf, size_t blen,
  738. void *user_data, void *stream_user_data)
  739. {
  740. struct Curl_cfilter *cf = user_data;
  741. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  742. struct Curl_easy *data = stream_user_data;
  743. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  744. (void)conn;
  745. (void)stream3_id;
  746. if(!stream)
  747. return NGHTTP3_ERR_CALLBACK_FAILURE;
  748. h3_xfer_write_resp(cf, data, stream, (char *)buf, blen, FALSE);
  749. if(blen) {
  750. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] ACK %zu bytes of DATA",
  751. stream->id, blen);
  752. ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream->id, blen);
  753. ngtcp2_conn_extend_max_offset(ctx->qconn, blen);
  754. }
  755. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] DATA len=%zu", stream->id, blen);
  756. return 0;
  757. }
  758. static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream3_id,
  759. size_t consumed, void *user_data,
  760. void *stream_user_data)
  761. {
  762. struct Curl_cfilter *cf = user_data;
  763. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  764. (void)conn;
  765. (void)stream_user_data;
  766. /* nghttp3 has consumed bytes on the QUIC stream and we need to
  767. * tell the QUIC connection to increase its flow control */
  768. ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream3_id, consumed);
  769. ngtcp2_conn_extend_max_offset(ctx->qconn, consumed);
  770. return 0;
  771. }
  772. static int cb_h3_end_headers(nghttp3_conn *conn, int64_t sid,
  773. int fin, void *user_data, void *stream_user_data)
  774. {
  775. struct Curl_cfilter *cf = user_data;
  776. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  777. struct Curl_easy *data = stream_user_data;
  778. curl_int64_t stream_id = (curl_int64_t)sid;
  779. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  780. (void)conn;
  781. (void)stream_id;
  782. (void)fin;
  783. (void)cf;
  784. if(!stream)
  785. return 0;
  786. /* add a CRLF only if we've received some headers */
  787. h3_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
  788. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] end_headers, status=%d",
  789. stream_id, stream->status_code);
  790. if(stream->status_code / 100 != 1) {
  791. stream->resp_hds_complete = TRUE;
  792. }
  793. h3_drain_stream(cf, data);
  794. return 0;
  795. }
  796. static int cb_h3_recv_header(nghttp3_conn *conn, int64_t sid,
  797. int32_t token, nghttp3_rcbuf *name,
  798. nghttp3_rcbuf *value, uint8_t flags,
  799. void *user_data, void *stream_user_data)
  800. {
  801. struct Curl_cfilter *cf = user_data;
  802. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  803. curl_int64_t stream_id = (curl_int64_t)sid;
  804. nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
  805. nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
  806. struct Curl_easy *data = stream_user_data;
  807. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  808. CURLcode result = CURLE_OK;
  809. (void)conn;
  810. (void)stream_id;
  811. (void)token;
  812. (void)flags;
  813. (void)cf;
  814. /* we might have cleaned up this transfer already */
  815. if(!stream)
  816. return 0;
  817. if(token == NGHTTP3_QPACK_TOKEN__STATUS) {
  818. result = Curl_http_decode_status(&stream->status_code,
  819. (const char *)h3val.base, h3val.len);
  820. if(result)
  821. return -1;
  822. Curl_dyn_reset(&ctx->scratch);
  823. result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/3 "));
  824. if(!result)
  825. result = Curl_dyn_addn(&ctx->scratch,
  826. (const char *)h3val.base, h3val.len);
  827. if(!result)
  828. result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
  829. if(!result)
  830. h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
  831. Curl_dyn_len(&ctx->scratch), FALSE);
  832. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] status: %s",
  833. stream_id, Curl_dyn_ptr(&ctx->scratch));
  834. if(result) {
  835. return -1;
  836. }
  837. }
  838. else {
  839. /* store as an HTTP1-style header */
  840. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] header: %.*s: %.*s",
  841. stream_id, (int)h3name.len, h3name.base,
  842. (int)h3val.len, h3val.base);
  843. Curl_dyn_reset(&ctx->scratch);
  844. result = Curl_dyn_addn(&ctx->scratch,
  845. (const char *)h3name.base, h3name.len);
  846. if(!result)
  847. result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
  848. if(!result)
  849. result = Curl_dyn_addn(&ctx->scratch,
  850. (const char *)h3val.base, h3val.len);
  851. if(!result)
  852. result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
  853. if(!result)
  854. h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
  855. Curl_dyn_len(&ctx->scratch), FALSE);
  856. }
  857. return 0;
  858. }
  859. static int cb_h3_stop_sending(nghttp3_conn *conn, int64_t stream_id,
  860. uint64_t app_error_code, void *user_data,
  861. void *stream_user_data)
  862. {
  863. struct Curl_cfilter *cf = user_data;
  864. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  865. int rv;
  866. (void)conn;
  867. (void)stream_user_data;
  868. rv = ngtcp2_conn_shutdown_stream_read(ctx->qconn, 0, stream_id,
  869. app_error_code);
  870. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  871. return NGTCP2_ERR_CALLBACK_FAILURE;
  872. }
  873. return 0;
  874. }
  875. static int cb_h3_reset_stream(nghttp3_conn *conn, int64_t sid,
  876. uint64_t app_error_code, void *user_data,
  877. void *stream_user_data) {
  878. struct Curl_cfilter *cf = user_data;
  879. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  880. curl_int64_t stream_id = (curl_int64_t)sid;
  881. struct Curl_easy *data = stream_user_data;
  882. int rv;
  883. (void)conn;
  884. (void)data;
  885. rv = ngtcp2_conn_shutdown_stream_write(ctx->qconn, 0, stream_id,
  886. app_error_code);
  887. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] reset -> %d", stream_id, rv);
  888. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  889. return NGTCP2_ERR_CALLBACK_FAILURE;
  890. }
  891. return 0;
  892. }
  893. static nghttp3_callbacks ngh3_callbacks = {
  894. cb_h3_acked_req_body, /* acked_stream_data */
  895. cb_h3_stream_close,
  896. cb_h3_recv_data,
  897. cb_h3_deferred_consume,
  898. NULL, /* begin_headers */
  899. cb_h3_recv_header,
  900. cb_h3_end_headers,
  901. NULL, /* begin_trailers */
  902. cb_h3_recv_header,
  903. NULL, /* end_trailers */
  904. cb_h3_stop_sending,
  905. NULL, /* end_stream */
  906. cb_h3_reset_stream,
  907. NULL, /* shutdown */
  908. NULL /* recv_settings */
  909. };
  910. static int init_ngh3_conn(struct Curl_cfilter *cf)
  911. {
  912. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  913. CURLcode result;
  914. int rc;
  915. int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;
  916. if(ngtcp2_conn_get_streams_uni_left(ctx->qconn) < 3) {
  917. return CURLE_QUIC_CONNECT_ERROR;
  918. }
  919. nghttp3_settings_default(&ctx->h3settings);
  920. rc = nghttp3_conn_client_new(&ctx->h3conn,
  921. &ngh3_callbacks,
  922. &ctx->h3settings,
  923. nghttp3_mem_default(),
  924. cf);
  925. if(rc) {
  926. result = CURLE_OUT_OF_MEMORY;
  927. goto fail;
  928. }
  929. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &ctrl_stream_id, NULL);
  930. if(rc) {
  931. result = CURLE_QUIC_CONNECT_ERROR;
  932. goto fail;
  933. }
  934. rc = nghttp3_conn_bind_control_stream(ctx->h3conn, ctrl_stream_id);
  935. if(rc) {
  936. result = CURLE_QUIC_CONNECT_ERROR;
  937. goto fail;
  938. }
  939. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_enc_stream_id, NULL);
  940. if(rc) {
  941. result = CURLE_QUIC_CONNECT_ERROR;
  942. goto fail;
  943. }
  944. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_dec_stream_id, NULL);
  945. if(rc) {
  946. result = CURLE_QUIC_CONNECT_ERROR;
  947. goto fail;
  948. }
  949. rc = nghttp3_conn_bind_qpack_streams(ctx->h3conn, qpack_enc_stream_id,
  950. qpack_dec_stream_id);
  951. if(rc) {
  952. result = CURLE_QUIC_CONNECT_ERROR;
  953. goto fail;
  954. }
  955. return CURLE_OK;
  956. fail:
  957. return result;
  958. }
  959. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  960. struct Curl_easy *data,
  961. struct h3_stream_ctx *stream,
  962. CURLcode *err)
  963. {
  964. ssize_t nread = -1;
  965. (void)cf;
  966. if(stream->reset) {
  967. failf(data,
  968. "HTTP/3 stream %" CURL_PRId64 " reset by server", stream->id);
  969. *err = data->req.bytecount? CURLE_PARTIAL_FILE : CURLE_HTTP3;
  970. goto out;
  971. }
  972. else if(!stream->resp_hds_complete) {
  973. failf(data,
  974. "HTTP/3 stream %" CURL_PRId64 " was closed cleanly, but before "
  975. "getting all response header fields, treated as error",
  976. stream->id);
  977. *err = CURLE_HTTP3;
  978. goto out;
  979. }
  980. *err = CURLE_OK;
  981. nread = 0;
  982. out:
  983. return nread;
  984. }
  985. /* incoming data frames on the h3 stream */
  986. static ssize_t cf_ngtcp2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  987. char *buf, size_t blen, CURLcode *err)
  988. {
  989. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  990. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  991. ssize_t nread = -1;
  992. struct cf_call_data save;
  993. struct pkt_io_ctx pktx;
  994. (void)ctx;
  995. (void)buf;
  996. CF_DATA_SAVE(save, cf, data);
  997. DEBUGASSERT(cf->connected);
  998. DEBUGASSERT(ctx);
  999. DEBUGASSERT(ctx->qconn);
  1000. DEBUGASSERT(ctx->h3conn);
  1001. *err = CURLE_OK;
  1002. pktx_init(&pktx, cf, data);
  1003. if(!stream) {
  1004. *err = CURLE_RECV_ERROR;
  1005. goto out;
  1006. }
  1007. if(cf_progress_ingress(cf, data, &pktx)) {
  1008. *err = CURLE_RECV_ERROR;
  1009. nread = -1;
  1010. goto out;
  1011. }
  1012. if(stream->xfer_result) {
  1013. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] xfer write failed", stream->id);
  1014. *err = stream->xfer_result;
  1015. nread = -1;
  1016. goto out;
  1017. }
  1018. else if(stream->closed) {
  1019. nread = recv_closed_stream(cf, data, stream, err);
  1020. goto out;
  1021. }
  1022. *err = CURLE_AGAIN;
  1023. nread = -1;
  1024. out:
  1025. if(cf_progress_egress(cf, data, &pktx)) {
  1026. *err = CURLE_SEND_ERROR;
  1027. nread = -1;
  1028. }
  1029. else {
  1030. CURLcode result2 = check_and_set_expiry(cf, data, &pktx);
  1031. if(result2) {
  1032. *err = result2;
  1033. nread = -1;
  1034. }
  1035. }
  1036. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_recv(blen=%zu) -> %zd, %d",
  1037. stream? stream->id : -1, blen, nread, *err);
  1038. CF_DATA_RESTORE(cf, save);
  1039. return nread;
  1040. }
  1041. static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
  1042. uint64_t datalen, void *user_data,
  1043. void *stream_user_data)
  1044. {
  1045. struct Curl_cfilter *cf = user_data;
  1046. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1047. struct Curl_easy *data = stream_user_data;
  1048. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1049. size_t skiplen;
  1050. (void)cf;
  1051. if(!stream)
  1052. return 0;
  1053. /* The server acknowledged `datalen` of bytes from our request body.
  1054. * This is a delta. We have kept this data in `sendbuf` for
  1055. * re-transmissions and can free it now. */
  1056. if(datalen >= (uint64_t)stream->sendbuf_len_in_flight)
  1057. skiplen = stream->sendbuf_len_in_flight;
  1058. else
  1059. skiplen = (size_t)datalen;
  1060. Curl_bufq_skip(&stream->sendbuf, skiplen);
  1061. stream->sendbuf_len_in_flight -= skiplen;
  1062. /* Everything ACKed, we resume upload processing */
  1063. if(!stream->sendbuf_len_in_flight) {
  1064. int rv = nghttp3_conn_resume_stream(conn, stream_id);
  1065. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1066. return NGTCP2_ERR_CALLBACK_FAILURE;
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. static nghttp3_ssize
  1072. cb_h3_read_req_body(nghttp3_conn *conn, int64_t stream_id,
  1073. nghttp3_vec *vec, size_t veccnt,
  1074. uint32_t *pflags, void *user_data,
  1075. void *stream_user_data)
  1076. {
  1077. struct Curl_cfilter *cf = user_data;
  1078. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1079. struct Curl_easy *data = stream_user_data;
  1080. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1081. ssize_t nwritten = 0;
  1082. size_t nvecs = 0;
  1083. (void)cf;
  1084. (void)conn;
  1085. (void)stream_id;
  1086. (void)user_data;
  1087. (void)veccnt;
  1088. if(!stream)
  1089. return NGHTTP3_ERR_CALLBACK_FAILURE;
  1090. /* nghttp3 keeps references to the sendbuf data until it is ACKed
  1091. * by the server (see `cb_h3_acked_req_body()` for updates).
  1092. * `sendbuf_len_in_flight` is the amount of bytes in `sendbuf`
  1093. * that we have already passed to nghttp3, but which have not been
  1094. * ACKed yet.
  1095. * Any amount beyond `sendbuf_len_in_flight` we need still to pass
  1096. * to nghttp3. Do that now, if we can. */
  1097. if(stream->sendbuf_len_in_flight < Curl_bufq_len(&stream->sendbuf)) {
  1098. nvecs = 0;
  1099. while(nvecs < veccnt &&
  1100. Curl_bufq_peek_at(&stream->sendbuf,
  1101. stream->sendbuf_len_in_flight,
  1102. (const unsigned char **)&vec[nvecs].base,
  1103. &vec[nvecs].len)) {
  1104. stream->sendbuf_len_in_flight += vec[nvecs].len;
  1105. nwritten += vec[nvecs].len;
  1106. ++nvecs;
  1107. }
  1108. DEBUGASSERT(nvecs > 0); /* we SHOULD have been be able to peek */
  1109. }
  1110. if(nwritten > 0 && stream->upload_left != -1)
  1111. stream->upload_left -= nwritten;
  1112. /* When we stopped sending and everything in `sendbuf` is "in flight",
  1113. * we are at the end of the request body. */
  1114. if(stream->upload_left == 0) {
  1115. *pflags = NGHTTP3_DATA_FLAG_EOF;
  1116. stream->send_closed = TRUE;
  1117. }
  1118. else if(!nwritten) {
  1119. /* Not EOF, and nothing to give, we signal WOULDBLOCK. */
  1120. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> AGAIN",
  1121. stream->id);
  1122. return NGHTTP3_ERR_WOULDBLOCK;
  1123. }
  1124. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> "
  1125. "%d vecs%s with %zu (buffered=%zu, left=%"
  1126. CURL_FORMAT_CURL_OFF_T ")",
  1127. stream->id, (int)nvecs,
  1128. *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
  1129. nwritten, Curl_bufq_len(&stream->sendbuf),
  1130. stream->upload_left);
  1131. return (nghttp3_ssize)nvecs;
  1132. }
  1133. /* Index where :authority header field will appear in request header
  1134. field list. */
  1135. #define AUTHORITY_DST_IDX 3
  1136. static ssize_t h3_stream_open(struct Curl_cfilter *cf,
  1137. struct Curl_easy *data,
  1138. const void *buf, size_t len,
  1139. CURLcode *err)
  1140. {
  1141. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1142. struct h3_stream_ctx *stream = NULL;
  1143. int64_t sid;
  1144. struct dynhds h2_headers;
  1145. size_t nheader;
  1146. nghttp3_nv *nva = NULL;
  1147. int rc = 0;
  1148. unsigned int i;
  1149. ssize_t nwritten = -1;
  1150. nghttp3_data_reader reader;
  1151. nghttp3_data_reader *preader = NULL;
  1152. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1153. *err = h3_data_setup(cf, data);
  1154. if(*err)
  1155. goto out;
  1156. stream = H3_STREAM_CTX(ctx, data);
  1157. DEBUGASSERT(stream);
  1158. if(!stream) {
  1159. *err = CURLE_FAILED_INIT;
  1160. goto out;
  1161. }
  1162. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1163. if(nwritten < 0)
  1164. goto out;
  1165. if(!stream->h1.done) {
  1166. /* need more data */
  1167. goto out;
  1168. }
  1169. DEBUGASSERT(stream->h1.req);
  1170. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1171. if(*err) {
  1172. nwritten = -1;
  1173. goto out;
  1174. }
  1175. /* no longer needed */
  1176. Curl_h1_req_parse_free(&stream->h1);
  1177. nheader = Curl_dynhds_count(&h2_headers);
  1178. nva = malloc(sizeof(nghttp3_nv) * nheader);
  1179. if(!nva) {
  1180. *err = CURLE_OUT_OF_MEMORY;
  1181. nwritten = -1;
  1182. goto out;
  1183. }
  1184. for(i = 0; i < nheader; ++i) {
  1185. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  1186. nva[i].name = (unsigned char *)e->name;
  1187. nva[i].namelen = e->namelen;
  1188. nva[i].value = (unsigned char *)e->value;
  1189. nva[i].valuelen = e->valuelen;
  1190. nva[i].flags = NGHTTP3_NV_FLAG_NONE;
  1191. }
  1192. rc = ngtcp2_conn_open_bidi_stream(ctx->qconn, &sid, data);
  1193. if(rc) {
  1194. failf(data, "can get bidi streams");
  1195. *err = CURLE_SEND_ERROR;
  1196. nwritten = -1;
  1197. goto out;
  1198. }
  1199. stream->id = (curl_int64_t)sid;
  1200. switch(data->state.httpreq) {
  1201. case HTTPREQ_POST:
  1202. case HTTPREQ_POST_FORM:
  1203. case HTTPREQ_POST_MIME:
  1204. case HTTPREQ_PUT:
  1205. /* known request body size or -1 */
  1206. if(data->state.infilesize != -1)
  1207. stream->upload_left = data->state.infilesize;
  1208. else
  1209. /* data sending without specifying the data amount up front */
  1210. stream->upload_left = -1; /* unknown */
  1211. break;
  1212. default:
  1213. /* there is not request body */
  1214. stream->upload_left = 0; /* no request body */
  1215. break;
  1216. }
  1217. stream->send_closed = (stream->upload_left == 0);
  1218. if(!stream->send_closed) {
  1219. reader.read_data = cb_h3_read_req_body;
  1220. preader = &reader;
  1221. }
  1222. rc = nghttp3_conn_submit_request(ctx->h3conn, stream->id,
  1223. nva, nheader, preader, data);
  1224. if(rc) {
  1225. switch(rc) {
  1226. case NGHTTP3_ERR_CONN_CLOSING:
  1227. CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send, "
  1228. "connection is closing", stream->id);
  1229. break;
  1230. default:
  1231. CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send -> "
  1232. "%d (%s)", stream->id, rc, ngtcp2_strerror(rc));
  1233. break;
  1234. }
  1235. *err = CURLE_SEND_ERROR;
  1236. nwritten = -1;
  1237. goto out;
  1238. }
  1239. if(Curl_trc_is_verbose(data)) {
  1240. infof(data, "[HTTP/3] [%" CURL_PRId64 "] OPENED stream for %s",
  1241. stream->id, data->state.url);
  1242. for(i = 0; i < nheader; ++i) {
  1243. infof(data, "[HTTP/3] [%" CURL_PRId64 "] [%.*s: %.*s]", stream->id,
  1244. (int)nva[i].namelen, nva[i].name,
  1245. (int)nva[i].valuelen, nva[i].value);
  1246. }
  1247. }
  1248. out:
  1249. free(nva);
  1250. Curl_dynhds_free(&h2_headers);
  1251. return nwritten;
  1252. }
  1253. static ssize_t cf_ngtcp2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1254. const void *buf, size_t len, CURLcode *err)
  1255. {
  1256. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1257. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1258. ssize_t sent = 0;
  1259. struct cf_call_data save;
  1260. struct pkt_io_ctx pktx;
  1261. CURLcode result;
  1262. CF_DATA_SAVE(save, cf, data);
  1263. DEBUGASSERT(cf->connected);
  1264. DEBUGASSERT(ctx->qconn);
  1265. DEBUGASSERT(ctx->h3conn);
  1266. pktx_init(&pktx, cf, data);
  1267. *err = CURLE_OK;
  1268. result = cf_progress_ingress(cf, data, &pktx);
  1269. if(result) {
  1270. *err = result;
  1271. sent = -1;
  1272. }
  1273. if(!stream || stream->id < 0) {
  1274. sent = h3_stream_open(cf, data, buf, len, err);
  1275. if(sent < 0) {
  1276. CURL_TRC_CF(data, cf, "failed to open stream -> %d", *err);
  1277. goto out;
  1278. }
  1279. stream = H3_STREAM_CTX(ctx, data);
  1280. }
  1281. else if(stream->upload_blocked_len) {
  1282. /* the data in `buf` has already been submitted or added to the
  1283. * buffers, but have been EAGAINed on the last invocation. */
  1284. DEBUGASSERT(len >= stream->upload_blocked_len);
  1285. if(len < stream->upload_blocked_len) {
  1286. /* Did we get called again with a smaller `len`? This should not
  1287. * happen. We are not prepared to handle that. */
  1288. failf(data, "HTTP/3 send again with decreased length");
  1289. *err = CURLE_HTTP3;
  1290. sent = -1;
  1291. goto out;
  1292. }
  1293. sent = (ssize_t)stream->upload_blocked_len;
  1294. stream->upload_blocked_len = 0;
  1295. }
  1296. else if(stream->closed) {
  1297. if(stream->resp_hds_complete) {
  1298. /* Server decided to close the stream after having sent us a final
  1299. * response. This is valid if it is not interested in the request
  1300. * body. This happens on 30x or 40x responses.
  1301. * We silently discard the data sent, since this is not a transport
  1302. * error situation. */
  1303. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] discarding data"
  1304. "on closed stream with response", stream->id);
  1305. *err = CURLE_OK;
  1306. sent = (ssize_t)len;
  1307. goto out;
  1308. }
  1309. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] send_body(len=%zu) "
  1310. "-> stream closed", stream->id, len);
  1311. *err = CURLE_HTTP3;
  1312. sent = -1;
  1313. goto out;
  1314. }
  1315. else {
  1316. sent = Curl_bufq_write(&stream->sendbuf, buf, len, err);
  1317. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send, add to "
  1318. "sendbuf(len=%zu) -> %zd, %d",
  1319. stream->id, len, sent, *err);
  1320. if(sent < 0) {
  1321. goto out;
  1322. }
  1323. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  1324. }
  1325. result = cf_progress_egress(cf, data, &pktx);
  1326. if(result) {
  1327. *err = result;
  1328. sent = -1;
  1329. }
  1330. if(stream && sent > 0 && stream->sendbuf_len_in_flight) {
  1331. /* We have unacknowledged DATA and cannot report success to our
  1332. * caller. Instead we EAGAIN and remember how much we have already
  1333. * "written" into our various internal connection buffers. */
  1334. stream->upload_blocked_len = sent;
  1335. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu), "
  1336. "%zu bytes in flight -> EGAIN", stream->id, len,
  1337. stream->sendbuf_len_in_flight);
  1338. *err = CURLE_AGAIN;
  1339. sent = -1;
  1340. }
  1341. out:
  1342. result = check_and_set_expiry(cf, data, &pktx);
  1343. if(result) {
  1344. *err = result;
  1345. sent = -1;
  1346. }
  1347. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu) -> %zd, %d",
  1348. stream? stream->id : -1, len, sent, *err);
  1349. CF_DATA_RESTORE(cf, save);
  1350. return sent;
  1351. }
  1352. static CURLcode qng_verify_peer(struct Curl_cfilter *cf,
  1353. struct Curl_easy *data)
  1354. {
  1355. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1356. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1357. cf->conn->httpversion = 30;
  1358. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1359. return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
  1360. }
  1361. static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
  1362. struct sockaddr_storage *remote_addr,
  1363. socklen_t remote_addrlen, int ecn,
  1364. void *userp)
  1365. {
  1366. struct pkt_io_ctx *pktx = userp;
  1367. struct cf_ngtcp2_ctx *ctx = pktx->cf->ctx;
  1368. ngtcp2_pkt_info pi;
  1369. ngtcp2_path path;
  1370. int rv;
  1371. ++pktx->pkt_count;
  1372. ngtcp2_addr_init(&path.local, (struct sockaddr *)&ctx->q.local_addr,
  1373. ctx->q.local_addrlen);
  1374. ngtcp2_addr_init(&path.remote, (struct sockaddr *)remote_addr,
  1375. remote_addrlen);
  1376. pi.ecn = (uint8_t)ecn;
  1377. rv = ngtcp2_conn_read_pkt(ctx->qconn, &path, &pi, pkt, pktlen, pktx->ts);
  1378. if(rv) {
  1379. CURL_TRC_CF(pktx->data, pktx->cf, "ingress, read_pkt -> %s (%d)",
  1380. ngtcp2_strerror(rv), rv);
  1381. if(!ctx->last_error.error_code) {
  1382. if(rv == NGTCP2_ERR_CRYPTO) {
  1383. ngtcp2_ccerr_set_tls_alert(&ctx->last_error,
  1384. ngtcp2_conn_get_tls_alert(ctx->qconn),
  1385. NULL, 0);
  1386. }
  1387. else {
  1388. ngtcp2_ccerr_set_liberr(&ctx->last_error, rv, NULL, 0);
  1389. }
  1390. }
  1391. if(rv == NGTCP2_ERR_CRYPTO)
  1392. /* this is a "TLS problem", but a failed certificate verification
  1393. is a common reason for this */
  1394. return CURLE_PEER_FAILED_VERIFICATION;
  1395. return CURLE_RECV_ERROR;
  1396. }
  1397. return CURLE_OK;
  1398. }
  1399. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  1400. struct Curl_easy *data,
  1401. struct pkt_io_ctx *pktx)
  1402. {
  1403. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1404. struct pkt_io_ctx local_pktx;
  1405. size_t pkts_chunk = 128, i;
  1406. CURLcode result = CURLE_OK;
  1407. if(!pktx) {
  1408. pktx_init(&local_pktx, cf, data);
  1409. pktx = &local_pktx;
  1410. }
  1411. else {
  1412. pktx_update_time(pktx, cf);
  1413. }
  1414. result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
  1415. if(result)
  1416. return result;
  1417. for(i = 0; i < 4; ++i) {
  1418. if(i)
  1419. pktx_update_time(pktx, cf);
  1420. pktx->pkt_count = 0;
  1421. result = vquic_recv_packets(cf, data, &ctx->q, pkts_chunk,
  1422. recv_pkt, pktx);
  1423. if(result || !pktx->pkt_count) /* error or got nothing */
  1424. break;
  1425. }
  1426. return result;
  1427. }
  1428. /**
  1429. * Read a network packet to send from ngtcp2 into `buf`.
  1430. * Return number of bytes written or -1 with *err set.
  1431. */
  1432. static ssize_t read_pkt_to_send(void *userp,
  1433. unsigned char *buf, size_t buflen,
  1434. CURLcode *err)
  1435. {
  1436. struct pkt_io_ctx *x = userp;
  1437. struct cf_ngtcp2_ctx *ctx = x->cf->ctx;
  1438. nghttp3_vec vec[16];
  1439. nghttp3_ssize veccnt;
  1440. ngtcp2_ssize ndatalen;
  1441. uint32_t flags;
  1442. int64_t stream_id;
  1443. int fin;
  1444. ssize_t nwritten, n;
  1445. veccnt = 0;
  1446. stream_id = -1;
  1447. fin = 0;
  1448. /* ngtcp2 may want to put several frames from different streams into
  1449. * this packet. `NGTCP2_WRITE_STREAM_FLAG_MORE` tells it to do so.
  1450. * When `NGTCP2_ERR_WRITE_MORE` is returned, we *need* to make
  1451. * another iteration.
  1452. * When ngtcp2 is happy (because it has no other frame that would fit
  1453. * or it has nothing more to send), it returns the total length
  1454. * of the assembled packet. This may be 0 if there was nothing to send. */
  1455. nwritten = 0;
  1456. *err = CURLE_OK;
  1457. for(;;) {
  1458. if(ctx->h3conn && ngtcp2_conn_get_max_data_left(ctx->qconn)) {
  1459. veccnt = nghttp3_conn_writev_stream(ctx->h3conn, &stream_id, &fin, vec,
  1460. sizeof(vec) / sizeof(vec[0]));
  1461. if(veccnt < 0) {
  1462. failf(x->data, "nghttp3_conn_writev_stream returned error: %s",
  1463. nghttp3_strerror((int)veccnt));
  1464. ngtcp2_ccerr_set_application_error(
  1465. &ctx->last_error,
  1466. nghttp3_err_infer_quic_app_error_code((int)veccnt), NULL, 0);
  1467. *err = CURLE_SEND_ERROR;
  1468. return -1;
  1469. }
  1470. }
  1471. flags = NGTCP2_WRITE_STREAM_FLAG_MORE |
  1472. (fin ? NGTCP2_WRITE_STREAM_FLAG_FIN : 0);
  1473. n = ngtcp2_conn_writev_stream(ctx->qconn, &x->ps.path,
  1474. NULL, buf, buflen,
  1475. &ndatalen, flags, stream_id,
  1476. (const ngtcp2_vec *)vec, veccnt, x->ts);
  1477. if(n == 0) {
  1478. /* nothing to send */
  1479. *err = CURLE_AGAIN;
  1480. nwritten = -1;
  1481. goto out;
  1482. }
  1483. else if(n < 0) {
  1484. switch(n) {
  1485. case NGTCP2_ERR_STREAM_DATA_BLOCKED: {
  1486. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
  1487. DEBUGASSERT(ndatalen == -1);
  1488. nghttp3_conn_block_stream(ctx->h3conn, stream_id);
  1489. CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRId64 "] block quic flow",
  1490. (curl_int64_t)stream_id);
  1491. DEBUGASSERT(stream);
  1492. if(stream)
  1493. stream->quic_flow_blocked = TRUE;
  1494. n = 0;
  1495. break;
  1496. }
  1497. case NGTCP2_ERR_STREAM_SHUT_WR:
  1498. DEBUGASSERT(ndatalen == -1);
  1499. nghttp3_conn_shutdown_stream_write(ctx->h3conn, stream_id);
  1500. n = 0;
  1501. break;
  1502. case NGTCP2_ERR_WRITE_MORE:
  1503. /* ngtcp2 wants to send more. update the flow of the stream whose data
  1504. * is in the buffer and continue */
  1505. DEBUGASSERT(ndatalen >= 0);
  1506. n = 0;
  1507. break;
  1508. default:
  1509. DEBUGASSERT(ndatalen == -1);
  1510. failf(x->data, "ngtcp2_conn_writev_stream returned error: %s",
  1511. ngtcp2_strerror((int)n));
  1512. ngtcp2_ccerr_set_liberr(&ctx->last_error, (int)n, NULL, 0);
  1513. *err = CURLE_SEND_ERROR;
  1514. nwritten = -1;
  1515. goto out;
  1516. }
  1517. }
  1518. if(ndatalen >= 0) {
  1519. /* we add the amount of data bytes to the flow windows */
  1520. int rv = nghttp3_conn_add_write_offset(ctx->h3conn, stream_id, ndatalen);
  1521. if(rv) {
  1522. failf(x->data, "nghttp3_conn_add_write_offset returned error: %s\n",
  1523. nghttp3_strerror(rv));
  1524. return CURLE_SEND_ERROR;
  1525. }
  1526. }
  1527. if(n > 0) {
  1528. /* packet assembled, leave */
  1529. nwritten = n;
  1530. goto out;
  1531. }
  1532. }
  1533. out:
  1534. return nwritten;
  1535. }
  1536. static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
  1537. struct Curl_easy *data,
  1538. struct pkt_io_ctx *pktx)
  1539. {
  1540. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1541. ssize_t nread;
  1542. size_t max_payload_size, path_max_payload_size, max_pktcnt;
  1543. size_t pktcnt = 0;
  1544. size_t gsolen = 0; /* this disables gso until we have a clue */
  1545. CURLcode curlcode;
  1546. struct pkt_io_ctx local_pktx;
  1547. if(!pktx) {
  1548. pktx_init(&local_pktx, cf, data);
  1549. pktx = &local_pktx;
  1550. }
  1551. else {
  1552. pktx_update_time(pktx, cf);
  1553. ngtcp2_path_storage_zero(&pktx->ps);
  1554. }
  1555. curlcode = vquic_flush(cf, data, &ctx->q);
  1556. if(curlcode) {
  1557. if(curlcode == CURLE_AGAIN) {
  1558. Curl_expire(data, 1, EXPIRE_QUIC);
  1559. return CURLE_OK;
  1560. }
  1561. return curlcode;
  1562. }
  1563. /* In UDP, there is a maximum theoretical packet paload length and
  1564. * a minimum payload length that is "guaranteed" to work.
  1565. * To detect if this minimum payload can be increased, ngtcp2 sends
  1566. * now and then a packet payload larger than the minimum. It that
  1567. * is ACKed by the peer, both parties know that it works and
  1568. * the subsequent packets can use a larger one.
  1569. * This is called PMTUD (Path Maximum Transmission Unit Discovery).
  1570. * Since a PMTUD might be rejected right on send, we do not want it
  1571. * be followed by other packets of lesser size. Because those would
  1572. * also fail then. So, if we detect a PMTUD while buffering, we flush.
  1573. */
  1574. max_payload_size = ngtcp2_conn_get_max_tx_udp_payload_size(ctx->qconn);
  1575. path_max_payload_size =
  1576. ngtcp2_conn_get_path_max_tx_udp_payload_size(ctx->qconn);
  1577. /* maximum number of packets buffered before we flush to the socket */
  1578. max_pktcnt = CURLMIN(MAX_PKT_BURST,
  1579. ctx->q.sendbuf.chunk_size / max_payload_size);
  1580. for(;;) {
  1581. /* add the next packet to send, if any, to our buffer */
  1582. nread = Curl_bufq_sipn(&ctx->q.sendbuf, max_payload_size,
  1583. read_pkt_to_send, pktx, &curlcode);
  1584. if(nread < 0) {
  1585. if(curlcode != CURLE_AGAIN)
  1586. return curlcode;
  1587. /* Nothing more to add, flush and leave */
  1588. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  1589. if(curlcode) {
  1590. if(curlcode == CURLE_AGAIN) {
  1591. Curl_expire(data, 1, EXPIRE_QUIC);
  1592. return CURLE_OK;
  1593. }
  1594. return curlcode;
  1595. }
  1596. goto out;
  1597. }
  1598. DEBUGASSERT(nread > 0);
  1599. if(pktcnt == 0) {
  1600. /* first packet in buffer. This is either of a known, "good"
  1601. * payload size or it is a PMTUD. We'll see. */
  1602. gsolen = (size_t)nread;
  1603. }
  1604. else if((size_t)nread > gsolen ||
  1605. (gsolen > path_max_payload_size && (size_t)nread != gsolen)) {
  1606. /* The just added packet is a PMTUD *or* the one(s) before the
  1607. * just added were PMTUD and the last one is smaller.
  1608. * Flush the buffer before the last add. */
  1609. curlcode = vquic_send_tail_split(cf, data, &ctx->q,
  1610. gsolen, nread, nread);
  1611. if(curlcode) {
  1612. if(curlcode == CURLE_AGAIN) {
  1613. Curl_expire(data, 1, EXPIRE_QUIC);
  1614. return CURLE_OK;
  1615. }
  1616. return curlcode;
  1617. }
  1618. pktcnt = 0;
  1619. continue;
  1620. }
  1621. if(++pktcnt >= max_pktcnt || (size_t)nread < gsolen) {
  1622. /* Reached MAX_PKT_BURST *or*
  1623. * the capacity of our buffer *or*
  1624. * last add was shorter than the previous ones, flush */
  1625. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  1626. if(curlcode) {
  1627. if(curlcode == CURLE_AGAIN) {
  1628. Curl_expire(data, 1, EXPIRE_QUIC);
  1629. return CURLE_OK;
  1630. }
  1631. return curlcode;
  1632. }
  1633. /* pktbuf has been completely sent */
  1634. pktcnt = 0;
  1635. }
  1636. }
  1637. out:
  1638. return CURLE_OK;
  1639. }
  1640. /*
  1641. * Called from transfer.c:data_pending to know if we should keep looping
  1642. * to receive more data from the connection.
  1643. */
  1644. static bool cf_ngtcp2_data_pending(struct Curl_cfilter *cf,
  1645. const struct Curl_easy *data)
  1646. {
  1647. (void)cf;
  1648. (void)data;
  1649. return FALSE;
  1650. }
  1651. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  1652. struct Curl_easy *data,
  1653. bool pause)
  1654. {
  1655. /* TODO: there seems right now no API in ngtcp2 to shrink/enlarge
  1656. * the streams windows. As we do in HTTP/2. */
  1657. if(!pause) {
  1658. h3_drain_stream(cf, data);
  1659. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1660. }
  1661. return CURLE_OK;
  1662. }
  1663. static CURLcode cf_ngtcp2_data_event(struct Curl_cfilter *cf,
  1664. struct Curl_easy *data,
  1665. int event, int arg1, void *arg2)
  1666. {
  1667. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1668. CURLcode result = CURLE_OK;
  1669. struct cf_call_data save;
  1670. CF_DATA_SAVE(save, cf, data);
  1671. (void)arg1;
  1672. (void)arg2;
  1673. switch(event) {
  1674. case CF_CTRL_DATA_SETUP:
  1675. break;
  1676. case CF_CTRL_DATA_PAUSE:
  1677. result = h3_data_pause(cf, data, (arg1 != 0));
  1678. break;
  1679. case CF_CTRL_DATA_DETACH:
  1680. h3_data_done(cf, data);
  1681. break;
  1682. case CF_CTRL_DATA_DONE:
  1683. h3_data_done(cf, data);
  1684. break;
  1685. case CF_CTRL_DATA_DONE_SEND: {
  1686. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1687. if(stream && !stream->send_closed) {
  1688. stream->send_closed = TRUE;
  1689. stream->upload_left = Curl_bufq_len(&stream->sendbuf);
  1690. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  1691. }
  1692. break;
  1693. }
  1694. case CF_CTRL_DATA_IDLE: {
  1695. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1696. CURL_TRC_CF(data, cf, "data idle");
  1697. if(stream && !stream->closed) {
  1698. result = check_and_set_expiry(cf, data, NULL);
  1699. if(result)
  1700. CURL_TRC_CF(data, cf, "data idle, check_and_set_expiry -> %d", result);
  1701. }
  1702. break;
  1703. }
  1704. default:
  1705. break;
  1706. }
  1707. CF_DATA_RESTORE(cf, save);
  1708. return result;
  1709. }
  1710. static void cf_ngtcp2_ctx_clear(struct cf_ngtcp2_ctx *ctx)
  1711. {
  1712. struct cf_call_data save = ctx->call_data;
  1713. if(ctx->qlogfd != -1) {
  1714. close(ctx->qlogfd);
  1715. }
  1716. Curl_vquic_tls_cleanup(&ctx->tls);
  1717. vquic_ctx_free(&ctx->q);
  1718. if(ctx->h3conn)
  1719. nghttp3_conn_del(ctx->h3conn);
  1720. if(ctx->qconn)
  1721. ngtcp2_conn_del(ctx->qconn);
  1722. Curl_bufcp_free(&ctx->stream_bufcp);
  1723. Curl_dyn_free(&ctx->scratch);
  1724. Curl_hash_clean(&ctx->streams);
  1725. Curl_hash_destroy(&ctx->streams);
  1726. Curl_ssl_peer_cleanup(&ctx->peer);
  1727. memset(ctx, 0, sizeof(*ctx));
  1728. ctx->qlogfd = -1;
  1729. ctx->call_data = save;
  1730. }
  1731. static void cf_ngtcp2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1732. {
  1733. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1734. struct cf_call_data save;
  1735. CF_DATA_SAVE(save, cf, data);
  1736. if(ctx && ctx->qconn) {
  1737. char buffer[NGTCP2_MAX_UDP_PAYLOAD_SIZE];
  1738. struct pkt_io_ctx pktx;
  1739. ngtcp2_ssize rc;
  1740. CURL_TRC_CF(data, cf, "close");
  1741. pktx_init(&pktx, cf, data);
  1742. rc = ngtcp2_conn_write_connection_close(ctx->qconn, NULL, /* path */
  1743. NULL, /* pkt_info */
  1744. (uint8_t *)buffer, sizeof(buffer),
  1745. &ctx->last_error, pktx.ts);
  1746. if(rc > 0) {
  1747. while((send(ctx->q.sockfd, buffer, (SEND_TYPE_ARG3)rc, 0) == -1) &&
  1748. SOCKERRNO == EINTR);
  1749. }
  1750. cf_ngtcp2_ctx_clear(ctx);
  1751. }
  1752. cf->connected = FALSE;
  1753. CF_DATA_RESTORE(cf, save);
  1754. }
  1755. static void cf_ngtcp2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1756. {
  1757. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1758. struct cf_call_data save;
  1759. CF_DATA_SAVE(save, cf, data);
  1760. CURL_TRC_CF(data, cf, "destroy");
  1761. if(ctx) {
  1762. cf_ngtcp2_ctx_clear(ctx);
  1763. free(ctx);
  1764. }
  1765. cf->ctx = NULL;
  1766. /* No CF_DATA_RESTORE(cf, save) possible */
  1767. (void)save;
  1768. }
  1769. #ifdef USE_OPENSSL
  1770. /* The "new session" callback must return zero if the session can be removed
  1771. * or non-zero if the session has been put into the session cache.
  1772. */
  1773. static int quic_ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid)
  1774. {
  1775. struct Curl_cfilter *cf;
  1776. struct cf_ngtcp2_ctx *ctx;
  1777. struct Curl_easy *data;
  1778. ngtcp2_crypto_conn_ref *cref;
  1779. cref = (ngtcp2_crypto_conn_ref *)SSL_get_app_data(ssl);
  1780. cf = cref? cref->user_data : NULL;
  1781. ctx = cf? cf->ctx : NULL;
  1782. data = cf? CF_DATA_CURRENT(cf) : NULL;
  1783. if(cf && data && ctx) {
  1784. CURLcode result = Curl_ossl_add_session(cf, data, &ctx->peer,
  1785. ssl_sessionid);
  1786. return result? 0 : 1;
  1787. }
  1788. return 0;
  1789. }
  1790. #endif /* USE_OPENSSL */
  1791. static CURLcode tls_ctx_setup(struct Curl_cfilter *cf,
  1792. struct Curl_easy *data,
  1793. void *user_data)
  1794. {
  1795. struct curl_tls_ctx *ctx = user_data;
  1796. (void)cf;
  1797. #ifdef USE_OPENSSL
  1798. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  1799. if(ngtcp2_crypto_boringssl_configure_client_context(ctx->ossl.ssl_ctx)
  1800. != 0) {
  1801. failf(data, "ngtcp2_crypto_boringssl_configure_client_context failed");
  1802. return CURLE_FAILED_INIT;
  1803. }
  1804. #else
  1805. if(ngtcp2_crypto_quictls_configure_client_context(ctx->ossl.ssl_ctx) != 0) {
  1806. failf(data, "ngtcp2_crypto_quictls_configure_client_context failed");
  1807. return CURLE_FAILED_INIT;
  1808. }
  1809. #endif /* !OPENSSL_IS_BORINGSSL && !OPENSSL_IS_AWSLC */
  1810. /* Enable the session cache because it's a prerequisite for the
  1811. * "new session" callback. Use the "external storage" mode to prevent
  1812. * OpenSSL from creating an internal session cache.
  1813. */
  1814. SSL_CTX_set_session_cache_mode(ctx->ossl.ssl_ctx,
  1815. SSL_SESS_CACHE_CLIENT |
  1816. SSL_SESS_CACHE_NO_INTERNAL);
  1817. SSL_CTX_sess_set_new_cb(ctx->ossl.ssl_ctx, quic_ossl_new_session_cb);
  1818. #elif defined(USE_GNUTLS)
  1819. if(ngtcp2_crypto_gnutls_configure_client_session(ctx->gtls.session) != 0) {
  1820. failf(data, "ngtcp2_crypto_gnutls_configure_client_session failed");
  1821. return CURLE_FAILED_INIT;
  1822. }
  1823. #elif defined(USE_WOLFSSL)
  1824. if(ngtcp2_crypto_wolfssl_configure_client_context(ctx->ssl_ctx) != 0) {
  1825. failf(data, "ngtcp2_crypto_wolfssl_configure_client_context failed");
  1826. return CURLE_FAILED_INIT;
  1827. }
  1828. #endif
  1829. return CURLE_OK;
  1830. }
  1831. /*
  1832. * Might be called twice for happy eyeballs.
  1833. */
  1834. static CURLcode cf_connect_start(struct Curl_cfilter *cf,
  1835. struct Curl_easy *data,
  1836. struct pkt_io_ctx *pktx)
  1837. {
  1838. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1839. int rc;
  1840. int rv;
  1841. CURLcode result;
  1842. const struct Curl_sockaddr_ex *sockaddr = NULL;
  1843. int qfd;
  1844. ctx->version = NGTCP2_PROTO_VER_MAX;
  1845. ctx->max_stream_window = H3_STREAM_WINDOW_SIZE;
  1846. ctx->max_idle_ms = CURL_QUIC_MAX_IDLE_MS;
  1847. Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
  1848. H3_STREAM_POOL_SPARES);
  1849. Curl_dyn_init(&ctx->scratch, CURL_MAX_HTTP_HEADER);
  1850. Curl_hash_offt_init(&ctx->streams, 63, h3_stream_hash_free);
  1851. result = Curl_ssl_peer_init(&ctx->peer, cf, TRNSPRT_QUIC);
  1852. if(result)
  1853. return result;
  1854. #define H3_ALPN "\x2h3\x5h3-29"
  1855. result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
  1856. H3_ALPN, sizeof(H3_ALPN) - 1,
  1857. tls_ctx_setup, &ctx->tls, &ctx->conn_ref);
  1858. if(result)
  1859. return result;
  1860. #ifdef USE_OPENSSL
  1861. SSL_set_quic_use_legacy_codepoint(ctx->tls.ossl.ssl, 0);
  1862. #endif
  1863. ctx->dcid.datalen = NGTCP2_MAX_CIDLEN;
  1864. result = Curl_rand(data, ctx->dcid.data, NGTCP2_MAX_CIDLEN);
  1865. if(result)
  1866. return result;
  1867. ctx->scid.datalen = NGTCP2_MAX_CIDLEN;
  1868. result = Curl_rand(data, ctx->scid.data, NGTCP2_MAX_CIDLEN);
  1869. if(result)
  1870. return result;
  1871. (void)Curl_qlogdir(data, ctx->scid.data, NGTCP2_MAX_CIDLEN, &qfd);
  1872. ctx->qlogfd = qfd; /* -1 if failure above */
  1873. quic_settings(ctx, data, pktx);
  1874. result = vquic_ctx_init(&ctx->q);
  1875. if(result)
  1876. return result;
  1877. Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL);
  1878. if(!sockaddr)
  1879. return CURLE_QUIC_CONNECT_ERROR;
  1880. ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
  1881. rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
  1882. &ctx->q.local_addrlen);
  1883. if(rv == -1)
  1884. return CURLE_QUIC_CONNECT_ERROR;
  1885. ngtcp2_addr_init(&ctx->connected_path.local,
  1886. (struct sockaddr *)&ctx->q.local_addr,
  1887. ctx->q.local_addrlen);
  1888. ngtcp2_addr_init(&ctx->connected_path.remote,
  1889. &sockaddr->sa_addr, sockaddr->addrlen);
  1890. rc = ngtcp2_conn_client_new(&ctx->qconn, &ctx->dcid, &ctx->scid,
  1891. &ctx->connected_path,
  1892. NGTCP2_PROTO_VER_V1, &ng_callbacks,
  1893. &ctx->settings, &ctx->transport_params,
  1894. NULL, cf);
  1895. if(rc)
  1896. return CURLE_QUIC_CONNECT_ERROR;
  1897. #ifdef USE_OPENSSL
  1898. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.ossl.ssl);
  1899. #elif defined(USE_GNUTLS)
  1900. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.gtls.session);
  1901. #else
  1902. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.ssl);
  1903. #endif
  1904. ngtcp2_ccerr_default(&ctx->last_error);
  1905. ctx->conn_ref.get_conn = get_conn;
  1906. ctx->conn_ref.user_data = cf;
  1907. return CURLE_OK;
  1908. }
  1909. static CURLcode cf_ngtcp2_connect(struct Curl_cfilter *cf,
  1910. struct Curl_easy *data,
  1911. bool blocking, bool *done)
  1912. {
  1913. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1914. CURLcode result = CURLE_OK;
  1915. struct cf_call_data save;
  1916. struct curltime now;
  1917. struct pkt_io_ctx pktx;
  1918. if(cf->connected) {
  1919. *done = TRUE;
  1920. return CURLE_OK;
  1921. }
  1922. /* Connect the UDP filter first */
  1923. if(!cf->next->connected) {
  1924. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  1925. if(result || !*done)
  1926. return result;
  1927. }
  1928. *done = FALSE;
  1929. now = Curl_now();
  1930. pktx_init(&pktx, cf, data);
  1931. CF_DATA_SAVE(save, cf, data);
  1932. if(ctx->reconnect_at.tv_sec && Curl_timediff(now, ctx->reconnect_at) < 0) {
  1933. /* Not time yet to attempt the next connect */
  1934. CURL_TRC_CF(data, cf, "waiting for reconnect time");
  1935. goto out;
  1936. }
  1937. if(!ctx->qconn) {
  1938. ctx->started_at = now;
  1939. result = cf_connect_start(cf, data, &pktx);
  1940. if(result)
  1941. goto out;
  1942. result = cf_progress_egress(cf, data, &pktx);
  1943. /* we do not expect to be able to recv anything yet */
  1944. goto out;
  1945. }
  1946. result = cf_progress_ingress(cf, data, &pktx);
  1947. if(result)
  1948. goto out;
  1949. result = cf_progress_egress(cf, data, &pktx);
  1950. if(result)
  1951. goto out;
  1952. if(ngtcp2_conn_get_handshake_completed(ctx->qconn)) {
  1953. ctx->handshake_at = now;
  1954. CURL_TRC_CF(data, cf, "handshake complete after %dms",
  1955. (int)Curl_timediff(now, ctx->started_at));
  1956. result = qng_verify_peer(cf, data);
  1957. if(!result) {
  1958. CURL_TRC_CF(data, cf, "peer verified");
  1959. cf->connected = TRUE;
  1960. cf->conn->alpn = CURL_HTTP_VERSION_3;
  1961. *done = TRUE;
  1962. connkeep(cf->conn, "HTTP/3 default");
  1963. }
  1964. }
  1965. out:
  1966. if(result == CURLE_RECV_ERROR && ctx->qconn &&
  1967. ngtcp2_conn_in_draining_period(ctx->qconn)) {
  1968. /* When a QUIC server instance is shutting down, it may send us a
  1969. * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
  1970. * state. The CONNECT may work in the near future again. Indicate
  1971. * that as a "weird" reply. */
  1972. result = CURLE_WEIRD_SERVER_REPLY;
  1973. }
  1974. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1975. if(result) {
  1976. struct ip_quadruple ip;
  1977. Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
  1978. infof(data, "QUIC connect to %s port %u failed: %s",
  1979. ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
  1980. }
  1981. #endif
  1982. if(!result && ctx->qconn) {
  1983. result = check_and_set_expiry(cf, data, &pktx);
  1984. }
  1985. if(result || *done)
  1986. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  1987. CF_DATA_RESTORE(cf, save);
  1988. return result;
  1989. }
  1990. static CURLcode cf_ngtcp2_query(struct Curl_cfilter *cf,
  1991. struct Curl_easy *data,
  1992. int query, int *pres1, void *pres2)
  1993. {
  1994. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1995. struct cf_call_data save;
  1996. switch(query) {
  1997. case CF_QUERY_MAX_CONCURRENT: {
  1998. const ngtcp2_transport_params *rp;
  1999. DEBUGASSERT(pres1);
  2000. CF_DATA_SAVE(save, cf, data);
  2001. rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
  2002. if(rp)
  2003. *pres1 = (rp->initial_max_streams_bidi > INT_MAX)?
  2004. INT_MAX : (int)rp->initial_max_streams_bidi;
  2005. else /* not arrived yet? */
  2006. *pres1 = Curl_multi_max_concurrent_streams(data->multi);
  2007. CURL_TRC_CF(data, cf, "query max_conncurrent -> %d", *pres1);
  2008. CF_DATA_RESTORE(cf, save);
  2009. return CURLE_OK;
  2010. }
  2011. case CF_QUERY_CONNECT_REPLY_MS:
  2012. if(ctx->q.got_first_byte) {
  2013. timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
  2014. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  2015. }
  2016. else
  2017. *pres1 = -1;
  2018. return CURLE_OK;
  2019. case CF_QUERY_TIMER_CONNECT: {
  2020. struct curltime *when = pres2;
  2021. if(ctx->q.got_first_byte)
  2022. *when = ctx->q.first_byte_at;
  2023. return CURLE_OK;
  2024. }
  2025. case CF_QUERY_TIMER_APPCONNECT: {
  2026. struct curltime *when = pres2;
  2027. if(cf->connected)
  2028. *when = ctx->handshake_at;
  2029. return CURLE_OK;
  2030. }
  2031. default:
  2032. break;
  2033. }
  2034. return cf->next?
  2035. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  2036. CURLE_UNKNOWN_OPTION;
  2037. }
  2038. static bool cf_ngtcp2_conn_is_alive(struct Curl_cfilter *cf,
  2039. struct Curl_easy *data,
  2040. bool *input_pending)
  2041. {
  2042. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2043. bool alive = FALSE;
  2044. const ngtcp2_transport_params *rp;
  2045. struct cf_call_data save;
  2046. CF_DATA_SAVE(save, cf, data);
  2047. *input_pending = FALSE;
  2048. if(!ctx->qconn)
  2049. goto out;
  2050. /* Both sides of the QUIC connection announce they max idle times in
  2051. * the transport parameters. Look at the minimum of both and if
  2052. * we exceed this, regard the connection as dead. The other side
  2053. * may have completely purged it and will no longer respond
  2054. * to any packets from us. */
  2055. rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
  2056. if(rp) {
  2057. timediff_t idletime;
  2058. uint64_t idle_ms = ctx->max_idle_ms;
  2059. if(rp->max_idle_timeout &&
  2060. (rp->max_idle_timeout / NGTCP2_MILLISECONDS) < idle_ms)
  2061. idle_ms = (rp->max_idle_timeout / NGTCP2_MILLISECONDS);
  2062. idletime = Curl_timediff(Curl_now(), ctx->q.last_io);
  2063. if(idletime > 0 && (uint64_t)idletime > idle_ms)
  2064. goto out;
  2065. }
  2066. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  2067. goto out;
  2068. alive = TRUE;
  2069. if(*input_pending) {
  2070. CURLcode result;
  2071. /* This happens before we've sent off a request and the connection is
  2072. not in use by any other transfer, there shouldn't be any data here,
  2073. only "protocol frames" */
  2074. *input_pending = FALSE;
  2075. result = cf_progress_ingress(cf, data, NULL);
  2076. CURL_TRC_CF(data, cf, "is_alive, progress ingress -> %d", result);
  2077. alive = result? FALSE : TRUE;
  2078. }
  2079. out:
  2080. CF_DATA_RESTORE(cf, save);
  2081. return alive;
  2082. }
  2083. struct Curl_cftype Curl_cft_http3 = {
  2084. "HTTP/3",
  2085. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  2086. 0,
  2087. cf_ngtcp2_destroy,
  2088. cf_ngtcp2_connect,
  2089. cf_ngtcp2_close,
  2090. Curl_cf_def_get_host,
  2091. cf_ngtcp2_adjust_pollset,
  2092. cf_ngtcp2_data_pending,
  2093. cf_ngtcp2_send,
  2094. cf_ngtcp2_recv,
  2095. cf_ngtcp2_data_event,
  2096. cf_ngtcp2_conn_is_alive,
  2097. Curl_cf_def_conn_keep_alive,
  2098. cf_ngtcp2_query,
  2099. };
  2100. CURLcode Curl_cf_ngtcp2_create(struct Curl_cfilter **pcf,
  2101. struct Curl_easy *data,
  2102. struct connectdata *conn,
  2103. const struct Curl_addrinfo *ai)
  2104. {
  2105. struct cf_ngtcp2_ctx *ctx = NULL;
  2106. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  2107. CURLcode result;
  2108. (void)data;
  2109. ctx = calloc(1, sizeof(*ctx));
  2110. if(!ctx) {
  2111. result = CURLE_OUT_OF_MEMORY;
  2112. goto out;
  2113. }
  2114. ctx->qlogfd = -1;
  2115. cf_ngtcp2_ctx_clear(ctx);
  2116. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  2117. if(result)
  2118. goto out;
  2119. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  2120. if(result)
  2121. goto out;
  2122. cf->conn = conn;
  2123. udp_cf->conn = cf->conn;
  2124. udp_cf->sockindex = cf->sockindex;
  2125. cf->next = udp_cf;
  2126. out:
  2127. *pcf = (!result)? cf : NULL;
  2128. if(result) {
  2129. if(udp_cf)
  2130. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  2131. Curl_safefree(cf);
  2132. Curl_safefree(ctx);
  2133. }
  2134. return result;
  2135. }
  2136. bool Curl_conn_is_ngtcp2(const struct Curl_easy *data,
  2137. const struct connectdata *conn,
  2138. int sockindex)
  2139. {
  2140. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  2141. (void)data;
  2142. for(; cf; cf = cf->next) {
  2143. if(cf->cft == &Curl_cft_http3)
  2144. return TRUE;
  2145. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2146. return FALSE;
  2147. }
  2148. return FALSE;
  2149. }
  2150. #endif