curl_ngtcp2.c 76 KB

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