curl_ngtcp2.c 77 KB

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