curl_ngtcp2.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  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 = want_send && (!ngtcp2_conn_get_cwnd_left(ctx->qconn) ||
  1029. !ngtcp2_conn_get_max_data_left(ctx->qconn));
  1030. s_exhaust = want_send && stream && stream->id >= 0 &&
  1031. stream->quic_flow_blocked;
  1032. want_recv = (want_recv || c_exhaust || s_exhaust);
  1033. want_send = (!s_exhaust && want_send) ||
  1034. !Curl_bufq_is_empty(&ctx->q.sendbuf);
  1035. Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
  1036. CF_DATA_RESTORE(cf, save);
  1037. }
  1038. }
  1039. static int cb_h3_stream_close(nghttp3_conn *conn, int64_t stream_id,
  1040. uint64_t app_error_code, void *user_data,
  1041. void *stream_user_data)
  1042. {
  1043. struct Curl_cfilter *cf = user_data;
  1044. struct Curl_easy *data = stream_user_data;
  1045. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1046. (void)conn;
  1047. (void)stream_id;
  1048. /* we might be called by nghttp3 after we already cleaned up */
  1049. if(!stream)
  1050. return 0;
  1051. stream->closed = TRUE;
  1052. stream->error3 = app_error_code;
  1053. if(stream->error3 != NGHTTP3_H3_NO_ERROR) {
  1054. stream->reset = TRUE;
  1055. stream->send_closed = TRUE;
  1056. CURL_TRC_CF(data, cf, "[%" PRId64 "] RESET: error %" PRId64,
  1057. stream->id, stream->error3);
  1058. }
  1059. else {
  1060. CURL_TRC_CF(data, cf, "[%" PRId64 "] CLOSED", stream->id);
  1061. }
  1062. h3_drain_stream(cf, data);
  1063. return 0;
  1064. }
  1065. /*
  1066. * write_resp_raw() copies response data in raw format to the `data`'s
  1067. * receive buffer. If not enough space is available, it appends to the
  1068. * `data`'s overflow buffer.
  1069. */
  1070. static CURLcode write_resp_raw(struct Curl_cfilter *cf,
  1071. struct Curl_easy *data,
  1072. const void *mem, size_t memlen,
  1073. bool flow)
  1074. {
  1075. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1076. CURLcode result = CURLE_OK;
  1077. ssize_t nwritten;
  1078. (void)cf;
  1079. if(!stream) {
  1080. return CURLE_RECV_ERROR;
  1081. }
  1082. nwritten = Curl_bufq_write(&stream->recvbuf, mem, memlen, &result);
  1083. if(nwritten < 0) {
  1084. return result;
  1085. }
  1086. if(!flow)
  1087. stream->recv_buf_nonflow += (size_t)nwritten;
  1088. if((size_t)nwritten < memlen) {
  1089. /* This MUST not happen. Our recbuf is dimensioned to hold the
  1090. * full max_stream_window and then some for this very reason. */
  1091. DEBUGASSERT(0);
  1092. return CURLE_RECV_ERROR;
  1093. }
  1094. return result;
  1095. }
  1096. static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream3_id,
  1097. const uint8_t *buf, size_t buflen,
  1098. void *user_data, void *stream_user_data)
  1099. {
  1100. struct Curl_cfilter *cf = user_data;
  1101. struct Curl_easy *data = stream_user_data;
  1102. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1103. CURLcode result;
  1104. (void)conn;
  1105. (void)stream3_id;
  1106. if(!stream)
  1107. return NGHTTP3_ERR_CALLBACK_FAILURE;
  1108. result = write_resp_raw(cf, data, buf, buflen, TRUE);
  1109. if(result) {
  1110. CURL_TRC_CF(data, cf, "[%" PRId64 "] DATA len=%zu, ERROR receiving %d",
  1111. stream->id, buflen, result);
  1112. return NGHTTP3_ERR_CALLBACK_FAILURE;
  1113. }
  1114. CURL_TRC_CF(data, cf, "[%" PRId64 "] DATA len=%zu", stream->id, buflen);
  1115. h3_drain_stream(cf, data);
  1116. return 0;
  1117. }
  1118. static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream3_id,
  1119. size_t consumed, void *user_data,
  1120. void *stream_user_data)
  1121. {
  1122. struct Curl_cfilter *cf = user_data;
  1123. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1124. (void)conn;
  1125. (void)stream_user_data;
  1126. /* nghttp3 has consumed bytes on the QUIC stream and we need to
  1127. * tell the QUIC connection to increase its flow control */
  1128. ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream3_id, consumed);
  1129. ngtcp2_conn_extend_max_offset(ctx->qconn, consumed);
  1130. return 0;
  1131. }
  1132. static int cb_h3_end_headers(nghttp3_conn *conn, int64_t stream_id,
  1133. int fin, void *user_data, void *stream_user_data)
  1134. {
  1135. struct Curl_cfilter *cf = user_data;
  1136. struct Curl_easy *data = stream_user_data;
  1137. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1138. CURLcode result = CURLE_OK;
  1139. (void)conn;
  1140. (void)stream_id;
  1141. (void)fin;
  1142. (void)cf;
  1143. if(!stream)
  1144. return 0;
  1145. /* add a CRLF only if we've received some headers */
  1146. result = write_resp_raw(cf, data, "\r\n", 2, FALSE);
  1147. if(result) {
  1148. return -1;
  1149. }
  1150. CURL_TRC_CF(data, cf, "[%" PRId64 "] end_headers, status=%d",
  1151. stream_id, stream->status_code);
  1152. if(stream->status_code / 100 != 1) {
  1153. stream->resp_hds_complete = TRUE;
  1154. }
  1155. h3_drain_stream(cf, data);
  1156. return 0;
  1157. }
  1158. static int cb_h3_recv_header(nghttp3_conn *conn, int64_t stream_id,
  1159. int32_t token, nghttp3_rcbuf *name,
  1160. nghttp3_rcbuf *value, uint8_t flags,
  1161. void *user_data, void *stream_user_data)
  1162. {
  1163. struct Curl_cfilter *cf = user_data;
  1164. nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
  1165. nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
  1166. struct Curl_easy *data = stream_user_data;
  1167. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1168. CURLcode result = CURLE_OK;
  1169. (void)conn;
  1170. (void)stream_id;
  1171. (void)token;
  1172. (void)flags;
  1173. (void)cf;
  1174. /* we might have cleaned up this transfer already */
  1175. if(!stream)
  1176. return 0;
  1177. if(token == NGHTTP3_QPACK_TOKEN__STATUS) {
  1178. char line[14]; /* status line is always 13 characters long */
  1179. size_t ncopy;
  1180. result = Curl_http_decode_status(&stream->status_code,
  1181. (const char *)h3val.base, h3val.len);
  1182. if(result)
  1183. return -1;
  1184. ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n",
  1185. stream->status_code);
  1186. CURL_TRC_CF(data, cf, "[%" PRId64 "] status: %s", stream_id, line);
  1187. result = write_resp_raw(cf, data, line, ncopy, FALSE);
  1188. if(result) {
  1189. return -1;
  1190. }
  1191. }
  1192. else {
  1193. /* store as an HTTP1-style header */
  1194. CURL_TRC_CF(data, cf, "[%" PRId64 "] header: %.*s: %.*s",
  1195. stream_id, (int)h3name.len, h3name.base,
  1196. (int)h3val.len, h3val.base);
  1197. result = write_resp_raw(cf, data, h3name.base, h3name.len, FALSE);
  1198. if(result) {
  1199. return -1;
  1200. }
  1201. result = write_resp_raw(cf, data, ": ", 2, FALSE);
  1202. if(result) {
  1203. return -1;
  1204. }
  1205. result = write_resp_raw(cf, data, h3val.base, h3val.len, FALSE);
  1206. if(result) {
  1207. return -1;
  1208. }
  1209. result = write_resp_raw(cf, data, "\r\n", 2, FALSE);
  1210. if(result) {
  1211. return -1;
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. static int cb_h3_stop_sending(nghttp3_conn *conn, int64_t stream_id,
  1217. uint64_t app_error_code, void *user_data,
  1218. void *stream_user_data)
  1219. {
  1220. struct Curl_cfilter *cf = user_data;
  1221. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1222. int rv;
  1223. (void)conn;
  1224. (void)stream_user_data;
  1225. rv = ngtcp2_conn_shutdown_stream_read(ctx->qconn, 0, stream_id,
  1226. app_error_code);
  1227. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  1228. return NGTCP2_ERR_CALLBACK_FAILURE;
  1229. }
  1230. return 0;
  1231. }
  1232. static int cb_h3_reset_stream(nghttp3_conn *conn, int64_t stream_id,
  1233. uint64_t app_error_code, void *user_data,
  1234. void *stream_user_data) {
  1235. struct Curl_cfilter *cf = user_data;
  1236. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1237. struct Curl_easy *data = stream_user_data;
  1238. int rv;
  1239. (void)conn;
  1240. (void)data;
  1241. rv = ngtcp2_conn_shutdown_stream_write(ctx->qconn, 0, stream_id,
  1242. app_error_code);
  1243. CURL_TRC_CF(data, cf, "[%" PRId64 "] reset -> %d", stream_id, rv);
  1244. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  1245. return NGTCP2_ERR_CALLBACK_FAILURE;
  1246. }
  1247. return 0;
  1248. }
  1249. static nghttp3_callbacks ngh3_callbacks = {
  1250. cb_h3_acked_req_body, /* acked_stream_data */
  1251. cb_h3_stream_close,
  1252. cb_h3_recv_data,
  1253. cb_h3_deferred_consume,
  1254. NULL, /* begin_headers */
  1255. cb_h3_recv_header,
  1256. cb_h3_end_headers,
  1257. NULL, /* begin_trailers */
  1258. cb_h3_recv_header,
  1259. NULL, /* end_trailers */
  1260. cb_h3_stop_sending,
  1261. NULL, /* end_stream */
  1262. cb_h3_reset_stream,
  1263. NULL, /* shutdown */
  1264. NULL /* recv_settings */
  1265. };
  1266. static int init_ngh3_conn(struct Curl_cfilter *cf)
  1267. {
  1268. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1269. CURLcode result;
  1270. int rc;
  1271. int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;
  1272. if(ngtcp2_conn_get_streams_uni_left(ctx->qconn) < 3) {
  1273. return CURLE_QUIC_CONNECT_ERROR;
  1274. }
  1275. nghttp3_settings_default(&ctx->h3settings);
  1276. rc = nghttp3_conn_client_new(&ctx->h3conn,
  1277. &ngh3_callbacks,
  1278. &ctx->h3settings,
  1279. nghttp3_mem_default(),
  1280. cf);
  1281. if(rc) {
  1282. result = CURLE_OUT_OF_MEMORY;
  1283. goto fail;
  1284. }
  1285. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &ctrl_stream_id, NULL);
  1286. if(rc) {
  1287. result = CURLE_QUIC_CONNECT_ERROR;
  1288. goto fail;
  1289. }
  1290. rc = nghttp3_conn_bind_control_stream(ctx->h3conn, ctrl_stream_id);
  1291. if(rc) {
  1292. result = CURLE_QUIC_CONNECT_ERROR;
  1293. goto fail;
  1294. }
  1295. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_enc_stream_id, NULL);
  1296. if(rc) {
  1297. result = CURLE_QUIC_CONNECT_ERROR;
  1298. goto fail;
  1299. }
  1300. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_dec_stream_id, NULL);
  1301. if(rc) {
  1302. result = CURLE_QUIC_CONNECT_ERROR;
  1303. goto fail;
  1304. }
  1305. rc = nghttp3_conn_bind_qpack_streams(ctx->h3conn, qpack_enc_stream_id,
  1306. qpack_dec_stream_id);
  1307. if(rc) {
  1308. result = CURLE_QUIC_CONNECT_ERROR;
  1309. goto fail;
  1310. }
  1311. return CURLE_OK;
  1312. fail:
  1313. return result;
  1314. }
  1315. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  1316. struct Curl_easy *data,
  1317. struct h3_stream_ctx *stream,
  1318. CURLcode *err)
  1319. {
  1320. ssize_t nread = -1;
  1321. (void)cf;
  1322. if(stream->reset) {
  1323. failf(data,
  1324. "HTTP/3 stream %" PRId64 " reset by server", stream->id);
  1325. *err = stream->resp_hds_complete? CURLE_PARTIAL_FILE : CURLE_HTTP3;
  1326. goto out;
  1327. }
  1328. else if(!stream->resp_hds_complete) {
  1329. failf(data,
  1330. "HTTP/3 stream %" PRId64 " was closed cleanly, but before getting"
  1331. " all response header fields, treated as error",
  1332. stream->id);
  1333. *err = CURLE_HTTP3;
  1334. goto out;
  1335. }
  1336. *err = CURLE_OK;
  1337. nread = 0;
  1338. out:
  1339. return nread;
  1340. }
  1341. /* incoming data frames on the h3 stream */
  1342. static ssize_t cf_ngtcp2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1343. char *buf, size_t len, CURLcode *err)
  1344. {
  1345. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1346. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1347. ssize_t nread = -1;
  1348. struct cf_call_data save;
  1349. struct pkt_io_ctx pktx;
  1350. (void)ctx;
  1351. CF_DATA_SAVE(save, cf, data);
  1352. DEBUGASSERT(cf->connected);
  1353. DEBUGASSERT(ctx);
  1354. DEBUGASSERT(ctx->qconn);
  1355. DEBUGASSERT(ctx->h3conn);
  1356. *err = CURLE_OK;
  1357. pktx_init(&pktx, cf, data);
  1358. if(!stream) {
  1359. *err = CURLE_RECV_ERROR;
  1360. goto out;
  1361. }
  1362. if(!Curl_bufq_is_empty(&stream->recvbuf)) {
  1363. nread = Curl_bufq_read(&stream->recvbuf,
  1364. (unsigned char *)buf, len, err);
  1365. if(nread < 0) {
  1366. CURL_TRC_CF(data, cf, "[%" PRId64 "] read recvbuf(len=%zu) "
  1367. "-> %zd, %d", stream->id, len, nread, *err);
  1368. goto out;
  1369. }
  1370. report_consumed_data(cf, data, nread);
  1371. }
  1372. if(cf_progress_ingress(cf, data, &pktx)) {
  1373. *err = CURLE_RECV_ERROR;
  1374. nread = -1;
  1375. goto out;
  1376. }
  1377. /* recvbuf had nothing before, maybe after progressing ingress? */
  1378. if(nread < 0 && !Curl_bufq_is_empty(&stream->recvbuf)) {
  1379. nread = Curl_bufq_read(&stream->recvbuf,
  1380. (unsigned char *)buf, len, err);
  1381. if(nread < 0) {
  1382. CURL_TRC_CF(data, cf, "[%" PRId64 "] read recvbuf(len=%zu) "
  1383. "-> %zd, %d", stream->id, len, nread, *err);
  1384. goto out;
  1385. }
  1386. report_consumed_data(cf, data, nread);
  1387. }
  1388. if(nread > 0) {
  1389. h3_drain_stream(cf, data);
  1390. }
  1391. else {
  1392. if(stream->closed) {
  1393. nread = recv_closed_stream(cf, data, stream, err);
  1394. goto out;
  1395. }
  1396. *err = CURLE_AGAIN;
  1397. nread = -1;
  1398. }
  1399. out:
  1400. if(cf_progress_egress(cf, data, &pktx)) {
  1401. *err = CURLE_SEND_ERROR;
  1402. nread = -1;
  1403. }
  1404. else {
  1405. CURLcode result2 = check_and_set_expiry(cf, data, &pktx);
  1406. if(result2) {
  1407. *err = result2;
  1408. nread = -1;
  1409. }
  1410. }
  1411. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_recv(len=%zu) -> %zd, %d",
  1412. stream? stream->id : -1, len, nread, *err);
  1413. CF_DATA_RESTORE(cf, save);
  1414. return nread;
  1415. }
  1416. static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
  1417. uint64_t datalen, void *user_data,
  1418. void *stream_user_data)
  1419. {
  1420. struct Curl_cfilter *cf = user_data;
  1421. struct Curl_easy *data = stream_user_data;
  1422. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1423. size_t skiplen;
  1424. (void)cf;
  1425. if(!stream)
  1426. return 0;
  1427. /* The server acknowledged `datalen` of bytes from our request body.
  1428. * This is a delta. We have kept this data in `sendbuf` for
  1429. * re-transmissions and can free it now. */
  1430. if(datalen >= (uint64_t)stream->sendbuf_len_in_flight)
  1431. skiplen = stream->sendbuf_len_in_flight;
  1432. else
  1433. skiplen = (size_t)datalen;
  1434. Curl_bufq_skip(&stream->sendbuf, skiplen);
  1435. stream->sendbuf_len_in_flight -= skiplen;
  1436. /* Everything ACKed, we resume upload processing */
  1437. if(!stream->sendbuf_len_in_flight) {
  1438. int rv = nghttp3_conn_resume_stream(conn, stream_id);
  1439. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1440. return NGTCP2_ERR_CALLBACK_FAILURE;
  1441. }
  1442. }
  1443. return 0;
  1444. }
  1445. static nghttp3_ssize
  1446. cb_h3_read_req_body(nghttp3_conn *conn, int64_t stream_id,
  1447. nghttp3_vec *vec, size_t veccnt,
  1448. uint32_t *pflags, void *user_data,
  1449. void *stream_user_data)
  1450. {
  1451. struct Curl_cfilter *cf = user_data;
  1452. struct Curl_easy *data = stream_user_data;
  1453. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1454. ssize_t nwritten = 0;
  1455. size_t nvecs = 0;
  1456. (void)cf;
  1457. (void)conn;
  1458. (void)stream_id;
  1459. (void)user_data;
  1460. (void)veccnt;
  1461. if(!stream)
  1462. return NGHTTP3_ERR_CALLBACK_FAILURE;
  1463. /* nghttp3 keeps references to the sendbuf data until it is ACKed
  1464. * by the server (see `cb_h3_acked_req_body()` for updates).
  1465. * `sendbuf_len_in_flight` is the amount of bytes in `sendbuf`
  1466. * that we have already passed to nghttp3, but which have not been
  1467. * ACKed yet.
  1468. * Any amount beyond `sendbuf_len_in_flight` we need still to pass
  1469. * to nghttp3. Do that now, if we can. */
  1470. if(stream->sendbuf_len_in_flight < Curl_bufq_len(&stream->sendbuf)) {
  1471. nvecs = 0;
  1472. while(nvecs < veccnt &&
  1473. Curl_bufq_peek_at(&stream->sendbuf,
  1474. stream->sendbuf_len_in_flight,
  1475. (const unsigned char **)&vec[nvecs].base,
  1476. &vec[nvecs].len)) {
  1477. stream->sendbuf_len_in_flight += vec[nvecs].len;
  1478. nwritten += vec[nvecs].len;
  1479. ++nvecs;
  1480. }
  1481. DEBUGASSERT(nvecs > 0); /* we SHOULD have been be able to peek */
  1482. }
  1483. if(nwritten > 0 && stream->upload_left != -1)
  1484. stream->upload_left -= nwritten;
  1485. /* When we stopped sending and everything in `sendbuf` is "in flight",
  1486. * we are at the end of the request body. */
  1487. if(stream->upload_left == 0) {
  1488. *pflags = NGHTTP3_DATA_FLAG_EOF;
  1489. stream->send_closed = TRUE;
  1490. }
  1491. else if(!nwritten) {
  1492. /* Not EOF, and nothing to give, we signal WOULDBLOCK. */
  1493. CURL_TRC_CF(data, cf, "[%" PRId64 "] read req body -> AGAIN",
  1494. stream->id);
  1495. return NGHTTP3_ERR_WOULDBLOCK;
  1496. }
  1497. CURL_TRC_CF(data, cf, "[%" PRId64 "] read req body -> "
  1498. "%d vecs%s with %zu (buffered=%zu, left=%"
  1499. CURL_FORMAT_CURL_OFF_T ")",
  1500. stream->id, (int)nvecs,
  1501. *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
  1502. nwritten, Curl_bufq_len(&stream->sendbuf),
  1503. stream->upload_left);
  1504. return (nghttp3_ssize)nvecs;
  1505. }
  1506. /* Index where :authority header field will appear in request header
  1507. field list. */
  1508. #define AUTHORITY_DST_IDX 3
  1509. static ssize_t h3_stream_open(struct Curl_cfilter *cf,
  1510. struct Curl_easy *data,
  1511. const void *buf, size_t len,
  1512. CURLcode *err)
  1513. {
  1514. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1515. struct h3_stream_ctx *stream = NULL;
  1516. struct dynhds h2_headers;
  1517. size_t nheader;
  1518. nghttp3_nv *nva = NULL;
  1519. int rc = 0;
  1520. unsigned int i;
  1521. ssize_t nwritten = -1;
  1522. nghttp3_data_reader reader;
  1523. nghttp3_data_reader *preader = NULL;
  1524. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1525. *err = h3_data_setup(cf, data);
  1526. if(*err)
  1527. goto out;
  1528. stream = H3_STREAM_CTX(data);
  1529. DEBUGASSERT(stream);
  1530. if(!stream) {
  1531. *err = CURLE_FAILED_INIT;
  1532. goto out;
  1533. }
  1534. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1535. if(nwritten < 0)
  1536. goto out;
  1537. if(!stream->h1.done) {
  1538. /* need more data */
  1539. goto out;
  1540. }
  1541. DEBUGASSERT(stream->h1.req);
  1542. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1543. if(*err) {
  1544. nwritten = -1;
  1545. goto out;
  1546. }
  1547. /* no longer needed */
  1548. Curl_h1_req_parse_free(&stream->h1);
  1549. nheader = Curl_dynhds_count(&h2_headers);
  1550. nva = malloc(sizeof(nghttp3_nv) * nheader);
  1551. if(!nva) {
  1552. *err = CURLE_OUT_OF_MEMORY;
  1553. nwritten = -1;
  1554. goto out;
  1555. }
  1556. for(i = 0; i < nheader; ++i) {
  1557. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  1558. nva[i].name = (unsigned char *)e->name;
  1559. nva[i].namelen = e->namelen;
  1560. nva[i].value = (unsigned char *)e->value;
  1561. nva[i].valuelen = e->valuelen;
  1562. nva[i].flags = NGHTTP3_NV_FLAG_NONE;
  1563. }
  1564. rc = ngtcp2_conn_open_bidi_stream(ctx->qconn, &stream->id, data);
  1565. if(rc) {
  1566. failf(data, "can get bidi streams");
  1567. *err = CURLE_SEND_ERROR;
  1568. goto out;
  1569. }
  1570. switch(data->state.httpreq) {
  1571. case HTTPREQ_POST:
  1572. case HTTPREQ_POST_FORM:
  1573. case HTTPREQ_POST_MIME:
  1574. case HTTPREQ_PUT:
  1575. /* known request body size or -1 */
  1576. if(data->state.infilesize != -1)
  1577. stream->upload_left = data->state.infilesize;
  1578. else
  1579. /* data sending without specifying the data amount up front */
  1580. stream->upload_left = -1; /* unknown */
  1581. break;
  1582. default:
  1583. /* there is not request body */
  1584. stream->upload_left = 0; /* no request body */
  1585. break;
  1586. }
  1587. stream->send_closed = (stream->upload_left == 0);
  1588. if(!stream->send_closed) {
  1589. reader.read_data = cb_h3_read_req_body;
  1590. preader = &reader;
  1591. }
  1592. rc = nghttp3_conn_submit_request(ctx->h3conn, stream->id,
  1593. nva, nheader, preader, data);
  1594. if(rc) {
  1595. switch(rc) {
  1596. case NGHTTP3_ERR_CONN_CLOSING:
  1597. CURL_TRC_CF(data, cf, "h3sid[%"PRId64"] failed to send, "
  1598. "connection is closing", stream->id);
  1599. break;
  1600. default:
  1601. CURL_TRC_CF(data, cf, "h3sid[%"PRId64"] failed to send -> %d (%s)",
  1602. stream->id, rc, ngtcp2_strerror(rc));
  1603. break;
  1604. }
  1605. *err = CURLE_SEND_ERROR;
  1606. nwritten = -1;
  1607. goto out;
  1608. }
  1609. if(Curl_trc_is_verbose(data)) {
  1610. infof(data, "[HTTP/3] [%" PRId64 "] OPENED stream for %s",
  1611. stream->id, data->state.url);
  1612. for(i = 0; i < nheader; ++i) {
  1613. infof(data, "[HTTP/3] [%" PRId64 "] [%.*s: %.*s]", stream->id,
  1614. (int)nva[i].namelen, nva[i].name,
  1615. (int)nva[i].valuelen, nva[i].value);
  1616. }
  1617. }
  1618. out:
  1619. free(nva);
  1620. Curl_dynhds_free(&h2_headers);
  1621. return nwritten;
  1622. }
  1623. static ssize_t cf_ngtcp2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1624. const void *buf, size_t len, CURLcode *err)
  1625. {
  1626. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1627. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  1628. ssize_t sent = 0;
  1629. struct cf_call_data save;
  1630. struct pkt_io_ctx pktx;
  1631. CURLcode result;
  1632. CF_DATA_SAVE(save, cf, data);
  1633. DEBUGASSERT(cf->connected);
  1634. DEBUGASSERT(ctx->qconn);
  1635. DEBUGASSERT(ctx->h3conn);
  1636. pktx_init(&pktx, cf, data);
  1637. *err = CURLE_OK;
  1638. result = cf_progress_ingress(cf, data, &pktx);
  1639. if(result) {
  1640. *err = result;
  1641. sent = -1;
  1642. }
  1643. if(!stream || stream->id < 0) {
  1644. sent = h3_stream_open(cf, data, buf, len, err);
  1645. if(sent < 0) {
  1646. CURL_TRC_CF(data, cf, "failed to open stream -> %d", *err);
  1647. goto out;
  1648. }
  1649. stream = H3_STREAM_CTX(data);
  1650. }
  1651. else if(stream->upload_blocked_len) {
  1652. /* the data in `buf` has already been submitted or added to the
  1653. * buffers, but have been EAGAINed on the last invocation. */
  1654. DEBUGASSERT(len >= stream->upload_blocked_len);
  1655. if(len < stream->upload_blocked_len) {
  1656. /* Did we get called again with a smaller `len`? This should not
  1657. * happen. We are not prepared to handle that. */
  1658. failf(data, "HTTP/3 send again with decreased length");
  1659. *err = CURLE_HTTP3;
  1660. sent = -1;
  1661. goto out;
  1662. }
  1663. sent = (ssize_t)stream->upload_blocked_len;
  1664. stream->upload_blocked_len = 0;
  1665. }
  1666. else if(stream->closed) {
  1667. if(stream->resp_hds_complete) {
  1668. /* Server decided to close the stream after having sent us a final
  1669. * response. This is valid if it is not interested in the request
  1670. * body. This happens on 30x or 40x responses.
  1671. * We silently discard the data sent, since this is not a transport
  1672. * error situation. */
  1673. CURL_TRC_CF(data, cf, "[%" PRId64 "] discarding data"
  1674. "on closed stream with response", stream->id);
  1675. *err = CURLE_OK;
  1676. sent = (ssize_t)len;
  1677. goto out;
  1678. }
  1679. *err = CURLE_HTTP3;
  1680. sent = -1;
  1681. goto out;
  1682. }
  1683. else {
  1684. sent = Curl_bufq_write(&stream->sendbuf, buf, len, err);
  1685. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send, add to "
  1686. "sendbuf(len=%zu) -> %zd, %d",
  1687. stream->id, len, sent, *err);
  1688. if(sent < 0) {
  1689. goto out;
  1690. }
  1691. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  1692. }
  1693. result = cf_progress_egress(cf, data, &pktx);
  1694. if(result) {
  1695. *err = result;
  1696. sent = -1;
  1697. }
  1698. if(stream && sent > 0 && stream->sendbuf_len_in_flight) {
  1699. /* We have unacknowledged DATA and cannot report success to our
  1700. * caller. Instead we EAGAIN and remember how much we have already
  1701. * "written" into our various internal connection buffers. */
  1702. stream->upload_blocked_len = sent;
  1703. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send(len=%zu), "
  1704. "%zu bytes in flight -> EGAIN", stream->id, len,
  1705. stream->sendbuf_len_in_flight);
  1706. *err = CURLE_AGAIN;
  1707. sent = -1;
  1708. }
  1709. out:
  1710. result = check_and_set_expiry(cf, data, &pktx);
  1711. if(result) {
  1712. *err = result;
  1713. sent = -1;
  1714. }
  1715. CURL_TRC_CF(data, cf, "[%" PRId64 "] cf_send(len=%zu) -> %zd, %d",
  1716. stream? stream->id : -1, len, sent, *err);
  1717. CF_DATA_RESTORE(cf, save);
  1718. return sent;
  1719. }
  1720. static CURLcode qng_verify_peer(struct Curl_cfilter *cf,
  1721. struct Curl_easy *data)
  1722. {
  1723. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1724. struct ssl_primary_config *conn_config;
  1725. CURLcode result = CURLE_OK;
  1726. conn_config = Curl_ssl_cf_get_primary_config(cf);
  1727. if(!conn_config)
  1728. return CURLE_FAILED_INIT;
  1729. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1730. cf->conn->httpversion = 30;
  1731. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1732. if(conn_config->verifyhost) {
  1733. #ifdef USE_OPENSSL
  1734. X509 *server_cert;
  1735. server_cert = SSL_get1_peer_certificate(ctx->ssl);
  1736. if(!server_cert) {
  1737. return CURLE_PEER_FAILED_VERIFICATION;
  1738. }
  1739. result = Curl_ossl_verifyhost(data, cf->conn, &ctx->peer, server_cert);
  1740. X509_free(server_cert);
  1741. if(result)
  1742. return result;
  1743. #elif defined(USE_GNUTLS)
  1744. result = Curl_gtls_verifyserver(data, ctx->gtls->session,
  1745. conn_config, &data->set.ssl, &ctx->peer,
  1746. data->set.str[STRING_SSL_PINNEDPUBLICKEY]);
  1747. if(result)
  1748. return result;
  1749. #elif defined(USE_WOLFSSL)
  1750. if(!ctx->peer.sni ||
  1751. wolfSSL_check_domain_name(ctx->ssl, ctx->peer.sni) == SSL_FAILURE)
  1752. return CURLE_PEER_FAILED_VERIFICATION;
  1753. #endif
  1754. infof(data, "Verified certificate just fine");
  1755. }
  1756. else
  1757. infof(data, "Skipped certificate verification");
  1758. #ifdef USE_OPENSSL
  1759. if(data->set.ssl.certinfo)
  1760. /* asked to gather certificate info */
  1761. (void)Curl_ossl_certchain(data, ctx->ssl);
  1762. #endif
  1763. return result;
  1764. }
  1765. static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
  1766. struct sockaddr_storage *remote_addr,
  1767. socklen_t remote_addrlen, int ecn,
  1768. void *userp)
  1769. {
  1770. struct pkt_io_ctx *pktx = userp;
  1771. struct cf_ngtcp2_ctx *ctx = pktx->cf->ctx;
  1772. ngtcp2_pkt_info pi;
  1773. ngtcp2_path path;
  1774. int rv;
  1775. ++pktx->pkt_count;
  1776. ngtcp2_addr_init(&path.local, (struct sockaddr *)&ctx->q.local_addr,
  1777. ctx->q.local_addrlen);
  1778. ngtcp2_addr_init(&path.remote, (struct sockaddr *)remote_addr,
  1779. remote_addrlen);
  1780. pi.ecn = (uint8_t)ecn;
  1781. rv = ngtcp2_conn_read_pkt(ctx->qconn, &path, &pi, pkt, pktlen, pktx->ts);
  1782. if(rv) {
  1783. CURL_TRC_CF(pktx->data, pktx->cf, "ingress, read_pkt -> %s (%d)",
  1784. ngtcp2_strerror(rv), rv);
  1785. if(!ctx->last_error.error_code) {
  1786. if(rv == NGTCP2_ERR_CRYPTO) {
  1787. ngtcp2_ccerr_set_tls_alert(&ctx->last_error,
  1788. ngtcp2_conn_get_tls_alert(ctx->qconn),
  1789. NULL, 0);
  1790. }
  1791. else {
  1792. ngtcp2_ccerr_set_liberr(&ctx->last_error, rv, NULL, 0);
  1793. }
  1794. }
  1795. if(rv == NGTCP2_ERR_CRYPTO)
  1796. /* this is a "TLS problem", but a failed certificate verification
  1797. is a common reason for this */
  1798. return CURLE_PEER_FAILED_VERIFICATION;
  1799. return CURLE_RECV_ERROR;
  1800. }
  1801. return CURLE_OK;
  1802. }
  1803. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  1804. struct Curl_easy *data,
  1805. struct pkt_io_ctx *pktx)
  1806. {
  1807. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1808. struct pkt_io_ctx local_pktx;
  1809. size_t pkts_chunk = 128, i;
  1810. size_t pkts_max = 10 * pkts_chunk;
  1811. CURLcode result = CURLE_OK;
  1812. if(!pktx) {
  1813. pktx_init(&local_pktx, cf, data);
  1814. pktx = &local_pktx;
  1815. }
  1816. else {
  1817. pktx_update_time(pktx, cf);
  1818. }
  1819. #ifdef USE_OPENSSL
  1820. if(!ctx->x509_store_setup) {
  1821. result = Curl_ssl_setup_x509_store(cf, data, ctx->sslctx);
  1822. if(result)
  1823. return result;
  1824. ctx->x509_store_setup = TRUE;
  1825. }
  1826. #endif
  1827. for(i = 0; i < pkts_max; i += pkts_chunk) {
  1828. pktx->pkt_count = 0;
  1829. result = vquic_recv_packets(cf, data, &ctx->q, pkts_chunk,
  1830. recv_pkt, pktx);
  1831. if(result) /* error */
  1832. break;
  1833. if(pktx->pkt_count < pkts_chunk) /* got less than we could */
  1834. break;
  1835. /* give egress a chance before we receive more */
  1836. result = cf_progress_egress(cf, data, pktx);
  1837. if(result) /* error */
  1838. break;
  1839. }
  1840. return result;
  1841. }
  1842. /**
  1843. * Read a network packet to send from ngtcp2 into `buf`.
  1844. * Return number of bytes written or -1 with *err set.
  1845. */
  1846. static ssize_t read_pkt_to_send(void *userp,
  1847. unsigned char *buf, size_t buflen,
  1848. CURLcode *err)
  1849. {
  1850. struct pkt_io_ctx *x = userp;
  1851. struct cf_ngtcp2_ctx *ctx = x->cf->ctx;
  1852. nghttp3_vec vec[16];
  1853. nghttp3_ssize veccnt;
  1854. ngtcp2_ssize ndatalen;
  1855. uint32_t flags;
  1856. int64_t stream_id;
  1857. int fin;
  1858. ssize_t nwritten, n;
  1859. veccnt = 0;
  1860. stream_id = -1;
  1861. fin = 0;
  1862. /* ngtcp2 may want to put several frames from different streams into
  1863. * this packet. `NGTCP2_WRITE_STREAM_FLAG_MORE` tells it to do so.
  1864. * When `NGTCP2_ERR_WRITE_MORE` is returned, we *need* to make
  1865. * another iteration.
  1866. * When ngtcp2 is happy (because it has no other frame that would fit
  1867. * or it has nothing more to send), it returns the total length
  1868. * of the assembled packet. This may be 0 if there was nothing to send. */
  1869. nwritten = 0;
  1870. *err = CURLE_OK;
  1871. for(;;) {
  1872. if(ctx->h3conn && ngtcp2_conn_get_max_data_left(ctx->qconn)) {
  1873. veccnt = nghttp3_conn_writev_stream(ctx->h3conn, &stream_id, &fin, vec,
  1874. sizeof(vec) / sizeof(vec[0]));
  1875. if(veccnt < 0) {
  1876. failf(x->data, "nghttp3_conn_writev_stream returned error: %s",
  1877. nghttp3_strerror((int)veccnt));
  1878. ngtcp2_ccerr_set_application_error(
  1879. &ctx->last_error,
  1880. nghttp3_err_infer_quic_app_error_code((int)veccnt), NULL, 0);
  1881. *err = CURLE_SEND_ERROR;
  1882. return -1;
  1883. }
  1884. }
  1885. flags = NGTCP2_WRITE_STREAM_FLAG_MORE |
  1886. (fin ? NGTCP2_WRITE_STREAM_FLAG_FIN : 0);
  1887. n = ngtcp2_conn_writev_stream(ctx->qconn, &x->ps.path,
  1888. NULL, buf, buflen,
  1889. &ndatalen, flags, stream_id,
  1890. (const ngtcp2_vec *)vec, veccnt, x->ts);
  1891. if(n == 0) {
  1892. /* nothing to send */
  1893. *err = CURLE_AGAIN;
  1894. nwritten = -1;
  1895. goto out;
  1896. }
  1897. else if(n < 0) {
  1898. switch(n) {
  1899. case NGTCP2_ERR_STREAM_DATA_BLOCKED: {
  1900. struct h3_stream_ctx *stream = H3_STREAM_CTX(x->data);
  1901. DEBUGASSERT(ndatalen == -1);
  1902. nghttp3_conn_block_stream(ctx->h3conn, stream_id);
  1903. CURL_TRC_CF(x->data, x->cf, "[%" PRId64 "] block quic flow",
  1904. stream_id);
  1905. DEBUGASSERT(stream);
  1906. if(stream)
  1907. stream->quic_flow_blocked = TRUE;
  1908. n = 0;
  1909. break;
  1910. }
  1911. case NGTCP2_ERR_STREAM_SHUT_WR:
  1912. DEBUGASSERT(ndatalen == -1);
  1913. nghttp3_conn_shutdown_stream_write(ctx->h3conn, stream_id);
  1914. n = 0;
  1915. break;
  1916. case NGTCP2_ERR_WRITE_MORE:
  1917. /* ngtcp2 wants to send more. update the flow of the stream whose data
  1918. * is in the buffer and continue */
  1919. DEBUGASSERT(ndatalen >= 0);
  1920. n = 0;
  1921. break;
  1922. default:
  1923. DEBUGASSERT(ndatalen == -1);
  1924. failf(x->data, "ngtcp2_conn_writev_stream returned error: %s",
  1925. ngtcp2_strerror((int)n));
  1926. ngtcp2_ccerr_set_liberr(&ctx->last_error, (int)n, NULL, 0);
  1927. *err = CURLE_SEND_ERROR;
  1928. nwritten = -1;
  1929. goto out;
  1930. }
  1931. }
  1932. if(ndatalen >= 0) {
  1933. /* we add the amount of data bytes to the flow windows */
  1934. int rv = nghttp3_conn_add_write_offset(ctx->h3conn, stream_id, ndatalen);
  1935. if(rv) {
  1936. failf(x->data, "nghttp3_conn_add_write_offset returned error: %s\n",
  1937. nghttp3_strerror(rv));
  1938. return CURLE_SEND_ERROR;
  1939. }
  1940. }
  1941. if(n > 0) {
  1942. /* packet assembled, leave */
  1943. nwritten = n;
  1944. goto out;
  1945. }
  1946. }
  1947. out:
  1948. return nwritten;
  1949. }
  1950. static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
  1951. struct Curl_easy *data,
  1952. struct pkt_io_ctx *pktx)
  1953. {
  1954. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1955. ssize_t nread;
  1956. size_t max_payload_size, path_max_payload_size, max_pktcnt;
  1957. size_t pktcnt = 0;
  1958. size_t gsolen = 0; /* this disables gso until we have a clue */
  1959. CURLcode curlcode;
  1960. struct pkt_io_ctx local_pktx;
  1961. if(!pktx) {
  1962. pktx_init(&local_pktx, cf, data);
  1963. pktx = &local_pktx;
  1964. }
  1965. else {
  1966. pktx_update_time(pktx, cf);
  1967. ngtcp2_path_storage_zero(&pktx->ps);
  1968. }
  1969. curlcode = vquic_flush(cf, data, &ctx->q);
  1970. if(curlcode) {
  1971. if(curlcode == CURLE_AGAIN) {
  1972. Curl_expire(data, 1, EXPIRE_QUIC);
  1973. return CURLE_OK;
  1974. }
  1975. return curlcode;
  1976. }
  1977. /* In UDP, there is a maximum theoretical packet paload length and
  1978. * a minimum payload length that is "guarantueed" to work.
  1979. * To detect if this minimum payload can be increased, ngtcp2 sends
  1980. * now and then a packet payload larger than the minimum. It that
  1981. * is ACKed by the peer, both parties know that it works and
  1982. * the subsequent packets can use a larger one.
  1983. * This is called PMTUD (Path Maximum Transmission Unit Discovery).
  1984. * Since a PMTUD might be rejected right on send, we do not want it
  1985. * be followed by other packets of lesser size. Because those would
  1986. * also fail then. So, if we detect a PMTUD while buffering, we flush.
  1987. */
  1988. max_payload_size = ngtcp2_conn_get_max_tx_udp_payload_size(ctx->qconn);
  1989. path_max_payload_size =
  1990. ngtcp2_conn_get_path_max_tx_udp_payload_size(ctx->qconn);
  1991. /* maximum number of packets buffered before we flush to the socket */
  1992. max_pktcnt = CURLMIN(MAX_PKT_BURST,
  1993. ctx->q.sendbuf.chunk_size / max_payload_size);
  1994. for(;;) {
  1995. /* add the next packet to send, if any, to our buffer */
  1996. nread = Curl_bufq_sipn(&ctx->q.sendbuf, max_payload_size,
  1997. read_pkt_to_send, pktx, &curlcode);
  1998. if(nread < 0) {
  1999. if(curlcode != CURLE_AGAIN)
  2000. return curlcode;
  2001. /* Nothing more to add, flush and leave */
  2002. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  2003. if(curlcode) {
  2004. if(curlcode == CURLE_AGAIN) {
  2005. Curl_expire(data, 1, EXPIRE_QUIC);
  2006. return CURLE_OK;
  2007. }
  2008. return curlcode;
  2009. }
  2010. goto out;
  2011. }
  2012. DEBUGASSERT(nread > 0);
  2013. if(pktcnt == 0) {
  2014. /* first packet in buffer. This is either of a known, "good"
  2015. * payload size or it is a PMTUD. We'll see. */
  2016. gsolen = (size_t)nread;
  2017. }
  2018. else if((size_t)nread > gsolen ||
  2019. (gsolen > path_max_payload_size && (size_t)nread != gsolen)) {
  2020. /* The just added packet is a PMTUD *or* the one(s) before the
  2021. * just added were PMTUD and the last one is smaller.
  2022. * Flush the buffer before the last add. */
  2023. curlcode = vquic_send_tail_split(cf, data, &ctx->q,
  2024. gsolen, nread, nread);
  2025. if(curlcode) {
  2026. if(curlcode == CURLE_AGAIN) {
  2027. Curl_expire(data, 1, EXPIRE_QUIC);
  2028. return CURLE_OK;
  2029. }
  2030. return curlcode;
  2031. }
  2032. pktcnt = 0;
  2033. continue;
  2034. }
  2035. if(++pktcnt >= max_pktcnt || (size_t)nread < gsolen) {
  2036. /* Reached MAX_PKT_BURST *or*
  2037. * the capacity of our buffer *or*
  2038. * last add was shorter than the previous ones, flush */
  2039. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  2040. if(curlcode) {
  2041. if(curlcode == CURLE_AGAIN) {
  2042. Curl_expire(data, 1, EXPIRE_QUIC);
  2043. return CURLE_OK;
  2044. }
  2045. return curlcode;
  2046. }
  2047. /* pktbuf has been completely sent */
  2048. pktcnt = 0;
  2049. }
  2050. }
  2051. out:
  2052. return CURLE_OK;
  2053. }
  2054. /*
  2055. * Called from transfer.c:data_pending to know if we should keep looping
  2056. * to receive more data from the connection.
  2057. */
  2058. static bool cf_ngtcp2_data_pending(struct Curl_cfilter *cf,
  2059. const struct Curl_easy *data)
  2060. {
  2061. const struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  2062. (void)cf;
  2063. return stream && !Curl_bufq_is_empty(&stream->recvbuf);
  2064. }
  2065. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  2066. struct Curl_easy *data,
  2067. bool pause)
  2068. {
  2069. /* TODO: there seems right now no API in ngtcp2 to shrink/enlarge
  2070. * the streams windows. As we do in HTTP/2. */
  2071. if(!pause) {
  2072. h3_drain_stream(cf, data);
  2073. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  2074. }
  2075. return CURLE_OK;
  2076. }
  2077. static CURLcode cf_ngtcp2_data_event(struct Curl_cfilter *cf,
  2078. struct Curl_easy *data,
  2079. int event, int arg1, void *arg2)
  2080. {
  2081. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2082. CURLcode result = CURLE_OK;
  2083. struct cf_call_data save;
  2084. CF_DATA_SAVE(save, cf, data);
  2085. (void)arg1;
  2086. (void)arg2;
  2087. switch(event) {
  2088. case CF_CTRL_DATA_SETUP:
  2089. break;
  2090. case CF_CTRL_DATA_PAUSE:
  2091. result = h3_data_pause(cf, data, (arg1 != 0));
  2092. break;
  2093. case CF_CTRL_DATA_DETACH:
  2094. h3_data_done(cf, data);
  2095. break;
  2096. case CF_CTRL_DATA_DONE:
  2097. h3_data_done(cf, data);
  2098. break;
  2099. case CF_CTRL_DATA_DONE_SEND: {
  2100. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  2101. if(stream && !stream->send_closed) {
  2102. stream->send_closed = TRUE;
  2103. stream->upload_left = Curl_bufq_len(&stream->sendbuf);
  2104. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  2105. }
  2106. break;
  2107. }
  2108. case CF_CTRL_DATA_IDLE: {
  2109. struct h3_stream_ctx *stream = H3_STREAM_CTX(data);
  2110. CURL_TRC_CF(data, cf, "data idle");
  2111. if(stream && !stream->closed) {
  2112. result = check_and_set_expiry(cf, data, NULL);
  2113. if(result)
  2114. CURL_TRC_CF(data, cf, "data idle, check_and_set_expiry -> %d", result);
  2115. }
  2116. break;
  2117. }
  2118. default:
  2119. break;
  2120. }
  2121. CF_DATA_RESTORE(cf, save);
  2122. return result;
  2123. }
  2124. static void cf_ngtcp2_ctx_clear(struct cf_ngtcp2_ctx *ctx)
  2125. {
  2126. struct cf_call_data save = ctx->call_data;
  2127. if(ctx->qlogfd != -1) {
  2128. close(ctx->qlogfd);
  2129. }
  2130. #ifdef USE_OPENSSL
  2131. if(ctx->ssl)
  2132. SSL_free(ctx->ssl);
  2133. if(ctx->sslctx)
  2134. SSL_CTX_free(ctx->sslctx);
  2135. #elif defined(USE_GNUTLS)
  2136. if(ctx->gtls) {
  2137. if(ctx->gtls->cred)
  2138. gnutls_certificate_free_credentials(ctx->gtls->cred);
  2139. if(ctx->gtls->session)
  2140. gnutls_deinit(ctx->gtls->session);
  2141. free(ctx->gtls);
  2142. }
  2143. #elif defined(USE_WOLFSSL)
  2144. if(ctx->ssl)
  2145. wolfSSL_free(ctx->ssl);
  2146. if(ctx->sslctx)
  2147. wolfSSL_CTX_free(ctx->sslctx);
  2148. #endif
  2149. vquic_ctx_free(&ctx->q);
  2150. if(ctx->h3conn)
  2151. nghttp3_conn_del(ctx->h3conn);
  2152. if(ctx->qconn)
  2153. ngtcp2_conn_del(ctx->qconn);
  2154. Curl_bufcp_free(&ctx->stream_bufcp);
  2155. Curl_ssl_peer_cleanup(&ctx->peer);
  2156. memset(ctx, 0, sizeof(*ctx));
  2157. ctx->qlogfd = -1;
  2158. ctx->call_data = save;
  2159. }
  2160. static void cf_ngtcp2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  2161. {
  2162. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2163. struct cf_call_data save;
  2164. CF_DATA_SAVE(save, cf, data);
  2165. if(ctx && ctx->qconn) {
  2166. char buffer[NGTCP2_MAX_UDP_PAYLOAD_SIZE];
  2167. struct pkt_io_ctx pktx;
  2168. ngtcp2_ssize rc;
  2169. CURL_TRC_CF(data, cf, "close");
  2170. pktx_init(&pktx, cf, data);
  2171. rc = ngtcp2_conn_write_connection_close(ctx->qconn, NULL, /* path */
  2172. NULL, /* pkt_info */
  2173. (uint8_t *)buffer, sizeof(buffer),
  2174. &ctx->last_error, pktx.ts);
  2175. if(rc > 0) {
  2176. while((send(ctx->q.sockfd, buffer, (SEND_TYPE_ARG3)rc, 0) == -1) &&
  2177. SOCKERRNO == EINTR);
  2178. }
  2179. cf_ngtcp2_ctx_clear(ctx);
  2180. }
  2181. cf->connected = FALSE;
  2182. CF_DATA_RESTORE(cf, save);
  2183. }
  2184. static void cf_ngtcp2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  2185. {
  2186. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2187. struct cf_call_data save;
  2188. CF_DATA_SAVE(save, cf, data);
  2189. CURL_TRC_CF(data, cf, "destroy");
  2190. if(ctx) {
  2191. cf_ngtcp2_ctx_clear(ctx);
  2192. free(ctx);
  2193. }
  2194. cf->ctx = NULL;
  2195. /* No CF_DATA_RESTORE(cf, save) possible */
  2196. (void)save;
  2197. }
  2198. /*
  2199. * Might be called twice for happy eyeballs.
  2200. */
  2201. static CURLcode cf_connect_start(struct Curl_cfilter *cf,
  2202. struct Curl_easy *data,
  2203. struct pkt_io_ctx *pktx)
  2204. {
  2205. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2206. int rc;
  2207. int rv;
  2208. CURLcode result;
  2209. const struct Curl_sockaddr_ex *sockaddr = NULL;
  2210. int qfd;
  2211. ctx->version = NGTCP2_PROTO_VER_MAX;
  2212. ctx->max_stream_window = H3_STREAM_WINDOW_SIZE;
  2213. ctx->max_idle_ms = CURL_QUIC_MAX_IDLE_MS;
  2214. Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
  2215. H3_STREAM_POOL_SPARES);
  2216. result = Curl_ssl_peer_init(&ctx->peer, cf);
  2217. if(result)
  2218. return result;
  2219. #ifdef USE_OPENSSL
  2220. result = quic_ssl_ctx(&ctx->sslctx, cf, data);
  2221. if(result)
  2222. return result;
  2223. result = quic_set_client_cert(cf, data);
  2224. if(result)
  2225. return result;
  2226. #elif defined(USE_WOLFSSL)
  2227. result = quic_ssl_ctx(&ctx->sslctx, cf, data);
  2228. if(result)
  2229. return result;
  2230. #endif
  2231. result = quic_init_ssl(cf, data);
  2232. if(result)
  2233. return result;
  2234. ctx->dcid.datalen = NGTCP2_MAX_CIDLEN;
  2235. result = Curl_rand(data, ctx->dcid.data, NGTCP2_MAX_CIDLEN);
  2236. if(result)
  2237. return result;
  2238. ctx->scid.datalen = NGTCP2_MAX_CIDLEN;
  2239. result = Curl_rand(data, ctx->scid.data, NGTCP2_MAX_CIDLEN);
  2240. if(result)
  2241. return result;
  2242. (void)Curl_qlogdir(data, ctx->scid.data, NGTCP2_MAX_CIDLEN, &qfd);
  2243. ctx->qlogfd = qfd; /* -1 if failure above */
  2244. quic_settings(ctx, data, pktx);
  2245. result = vquic_ctx_init(&ctx->q);
  2246. if(result)
  2247. return result;
  2248. Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd,
  2249. &sockaddr, NULL, NULL, NULL, NULL);
  2250. if(!sockaddr)
  2251. return CURLE_QUIC_CONNECT_ERROR;
  2252. ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
  2253. rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
  2254. &ctx->q.local_addrlen);
  2255. if(rv == -1)
  2256. return CURLE_QUIC_CONNECT_ERROR;
  2257. ngtcp2_addr_init(&ctx->connected_path.local,
  2258. (struct sockaddr *)&ctx->q.local_addr,
  2259. ctx->q.local_addrlen);
  2260. ngtcp2_addr_init(&ctx->connected_path.remote,
  2261. &sockaddr->sa_addr, sockaddr->addrlen);
  2262. rc = ngtcp2_conn_client_new(&ctx->qconn, &ctx->dcid, &ctx->scid,
  2263. &ctx->connected_path,
  2264. NGTCP2_PROTO_VER_V1, &ng_callbacks,
  2265. &ctx->settings, &ctx->transport_params,
  2266. NULL, cf);
  2267. if(rc)
  2268. return CURLE_QUIC_CONNECT_ERROR;
  2269. #ifdef USE_GNUTLS
  2270. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->gtls->session);
  2271. #else
  2272. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->ssl);
  2273. #endif
  2274. ngtcp2_ccerr_default(&ctx->last_error);
  2275. ctx->conn_ref.get_conn = get_conn;
  2276. ctx->conn_ref.user_data = cf;
  2277. return CURLE_OK;
  2278. }
  2279. static CURLcode cf_ngtcp2_connect(struct Curl_cfilter *cf,
  2280. struct Curl_easy *data,
  2281. bool blocking, bool *done)
  2282. {
  2283. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2284. CURLcode result = CURLE_OK;
  2285. struct cf_call_data save;
  2286. struct curltime now;
  2287. struct pkt_io_ctx pktx;
  2288. if(cf->connected) {
  2289. *done = TRUE;
  2290. return CURLE_OK;
  2291. }
  2292. /* Connect the UDP filter first */
  2293. if(!cf->next->connected) {
  2294. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  2295. if(result || !*done)
  2296. return result;
  2297. }
  2298. *done = FALSE;
  2299. now = Curl_now();
  2300. pktx_init(&pktx, cf, data);
  2301. CF_DATA_SAVE(save, cf, data);
  2302. if(ctx->reconnect_at.tv_sec && Curl_timediff(now, ctx->reconnect_at) < 0) {
  2303. /* Not time yet to attempt the next connect */
  2304. CURL_TRC_CF(data, cf, "waiting for reconnect time");
  2305. goto out;
  2306. }
  2307. if(!ctx->qconn) {
  2308. ctx->started_at = now;
  2309. result = cf_connect_start(cf, data, &pktx);
  2310. if(result)
  2311. goto out;
  2312. result = cf_progress_egress(cf, data, &pktx);
  2313. /* we do not expect to be able to recv anything yet */
  2314. goto out;
  2315. }
  2316. result = cf_progress_ingress(cf, data, &pktx);
  2317. if(result)
  2318. goto out;
  2319. result = cf_progress_egress(cf, data, &pktx);
  2320. if(result)
  2321. goto out;
  2322. if(ngtcp2_conn_get_handshake_completed(ctx->qconn)) {
  2323. ctx->handshake_at = now;
  2324. CURL_TRC_CF(data, cf, "handshake complete after %dms",
  2325. (int)Curl_timediff(now, ctx->started_at));
  2326. result = qng_verify_peer(cf, data);
  2327. if(!result) {
  2328. CURL_TRC_CF(data, cf, "peer verified");
  2329. cf->connected = TRUE;
  2330. cf->conn->alpn = CURL_HTTP_VERSION_3;
  2331. *done = TRUE;
  2332. connkeep(cf->conn, "HTTP/3 default");
  2333. }
  2334. }
  2335. out:
  2336. if(result == CURLE_RECV_ERROR && ctx->qconn &&
  2337. ngtcp2_conn_in_draining_period(ctx->qconn)) {
  2338. /* When a QUIC server instance is shutting down, it may send us a
  2339. * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
  2340. * state. The CONNECT may work in the near future again. Indicate
  2341. * that as a "weird" reply. */
  2342. result = CURLE_WEIRD_SERVER_REPLY;
  2343. }
  2344. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2345. if(result) {
  2346. const char *r_ip = NULL;
  2347. int r_port = 0;
  2348. Curl_cf_socket_peek(cf->next, data, NULL, NULL,
  2349. &r_ip, &r_port, NULL, NULL);
  2350. infof(data, "QUIC connect to %s port %u failed: %s",
  2351. r_ip, r_port, curl_easy_strerror(result));
  2352. }
  2353. #endif
  2354. if(!result && ctx->qconn) {
  2355. result = check_and_set_expiry(cf, data, &pktx);
  2356. }
  2357. if(result || *done)
  2358. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  2359. CF_DATA_RESTORE(cf, save);
  2360. return result;
  2361. }
  2362. static CURLcode cf_ngtcp2_query(struct Curl_cfilter *cf,
  2363. struct Curl_easy *data,
  2364. int query, int *pres1, void *pres2)
  2365. {
  2366. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2367. struct cf_call_data save;
  2368. switch(query) {
  2369. case CF_QUERY_MAX_CONCURRENT: {
  2370. const ngtcp2_transport_params *rp;
  2371. DEBUGASSERT(pres1);
  2372. CF_DATA_SAVE(save, cf, data);
  2373. rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
  2374. if(rp)
  2375. *pres1 = (rp->initial_max_streams_bidi > INT_MAX)?
  2376. INT_MAX : (int)rp->initial_max_streams_bidi;
  2377. else /* not arrived yet? */
  2378. *pres1 = Curl_multi_max_concurrent_streams(data->multi);
  2379. CURL_TRC_CF(data, cf, "query max_conncurrent -> %d", *pres1);
  2380. CF_DATA_RESTORE(cf, save);
  2381. return CURLE_OK;
  2382. }
  2383. case CF_QUERY_CONNECT_REPLY_MS:
  2384. if(ctx->got_first_byte) {
  2385. timediff_t ms = Curl_timediff(ctx->first_byte_at, ctx->started_at);
  2386. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  2387. }
  2388. else
  2389. *pres1 = -1;
  2390. return CURLE_OK;
  2391. case CF_QUERY_TIMER_CONNECT: {
  2392. struct curltime *when = pres2;
  2393. if(ctx->got_first_byte)
  2394. *when = ctx->first_byte_at;
  2395. return CURLE_OK;
  2396. }
  2397. case CF_QUERY_TIMER_APPCONNECT: {
  2398. struct curltime *when = pres2;
  2399. if(cf->connected)
  2400. *when = ctx->handshake_at;
  2401. return CURLE_OK;
  2402. }
  2403. default:
  2404. break;
  2405. }
  2406. return cf->next?
  2407. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  2408. CURLE_UNKNOWN_OPTION;
  2409. }
  2410. static bool cf_ngtcp2_conn_is_alive(struct Curl_cfilter *cf,
  2411. struct Curl_easy *data,
  2412. bool *input_pending)
  2413. {
  2414. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2415. bool alive = FALSE;
  2416. const ngtcp2_transport_params *rp;
  2417. struct cf_call_data save;
  2418. CF_DATA_SAVE(save, cf, data);
  2419. *input_pending = FALSE;
  2420. if(!ctx->qconn)
  2421. goto out;
  2422. /* Both sides of the QUIC connection announce they max idle times in
  2423. * the transport parameters. Look at the minimum of both and if
  2424. * we exceed this, regard the connection as dead. The other side
  2425. * may have completely purged it and will no longer respond
  2426. * to any packets from us. */
  2427. rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
  2428. if(rp) {
  2429. timediff_t idletime;
  2430. uint64_t idle_ms = ctx->max_idle_ms;
  2431. if(rp->max_idle_timeout &&
  2432. (rp->max_idle_timeout / NGTCP2_MILLISECONDS) < idle_ms)
  2433. idle_ms = (rp->max_idle_timeout / NGTCP2_MILLISECONDS);
  2434. idletime = Curl_timediff(Curl_now(), ctx->q.last_io);
  2435. if(idletime > 0 && (uint64_t)idletime > idle_ms)
  2436. goto out;
  2437. }
  2438. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  2439. goto out;
  2440. alive = TRUE;
  2441. if(*input_pending) {
  2442. CURLcode result;
  2443. /* This happens before we've sent off a request and the connection is
  2444. not in use by any other transfer, there shouldn't be any data here,
  2445. only "protocol frames" */
  2446. *input_pending = FALSE;
  2447. result = cf_progress_ingress(cf, data, NULL);
  2448. CURL_TRC_CF(data, cf, "is_alive, progress ingress -> %d", result);
  2449. alive = result? FALSE : TRUE;
  2450. }
  2451. out:
  2452. CF_DATA_RESTORE(cf, save);
  2453. return alive;
  2454. }
  2455. struct Curl_cftype Curl_cft_http3 = {
  2456. "HTTP/3",
  2457. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  2458. 0,
  2459. cf_ngtcp2_destroy,
  2460. cf_ngtcp2_connect,
  2461. cf_ngtcp2_close,
  2462. Curl_cf_def_get_host,
  2463. cf_ngtcp2_adjust_pollset,
  2464. cf_ngtcp2_data_pending,
  2465. cf_ngtcp2_send,
  2466. cf_ngtcp2_recv,
  2467. cf_ngtcp2_data_event,
  2468. cf_ngtcp2_conn_is_alive,
  2469. Curl_cf_def_conn_keep_alive,
  2470. cf_ngtcp2_query,
  2471. };
  2472. CURLcode Curl_cf_ngtcp2_create(struct Curl_cfilter **pcf,
  2473. struct Curl_easy *data,
  2474. struct connectdata *conn,
  2475. const struct Curl_addrinfo *ai)
  2476. {
  2477. struct cf_ngtcp2_ctx *ctx = NULL;
  2478. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  2479. CURLcode result;
  2480. (void)data;
  2481. ctx = calloc(1, sizeof(*ctx));
  2482. if(!ctx) {
  2483. result = CURLE_OUT_OF_MEMORY;
  2484. goto out;
  2485. }
  2486. ctx->qlogfd = -1;
  2487. cf_ngtcp2_ctx_clear(ctx);
  2488. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  2489. if(result)
  2490. goto out;
  2491. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  2492. if(result)
  2493. goto out;
  2494. cf->conn = conn;
  2495. udp_cf->conn = cf->conn;
  2496. udp_cf->sockindex = cf->sockindex;
  2497. cf->next = udp_cf;
  2498. out:
  2499. *pcf = (!result)? cf : NULL;
  2500. if(result) {
  2501. if(udp_cf)
  2502. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  2503. Curl_safefree(cf);
  2504. Curl_safefree(ctx);
  2505. }
  2506. return result;
  2507. }
  2508. bool Curl_conn_is_ngtcp2(const struct Curl_easy *data,
  2509. const struct connectdata *conn,
  2510. int sockindex)
  2511. {
  2512. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  2513. (void)data;
  2514. for(; cf; cf = cf->next) {
  2515. if(cf->cft == &Curl_cft_http3)
  2516. return TRUE;
  2517. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2518. return FALSE;
  2519. }
  2520. return FALSE;
  2521. }
  2522. #endif