curl_ngtcp2.c 84 KB

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