curl_ngtcp2.c 84 KB

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