curl_ngtcp2.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  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(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. 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) {
  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 = (ngtcp2_tstamp)ctx->q.last_op.tv_sec * NGTCP2_SECONDS +
  294. (ngtcp2_tstamp)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 CURLcode 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, (uint64_t)nconsumed);
  453. ngtcp2_conn_extend_max_offset(tconn, (uint64_t)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, (timediff_t)(timeout / NGTCP2_MILLISECONDS),
  711. EXPIRE_QUIC);
  712. }
  713. }
  714. return CURLE_OK;
  715. }
  716. static void cf_ngtcp2_adjust_pollset(struct Curl_cfilter *cf,
  717. struct Curl_easy *data,
  718. struct easy_pollset *ps)
  719. {
  720. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  721. bool want_recv, want_send;
  722. if(!ctx->qconn)
  723. return;
  724. Curl_pollset_check(data, ps, ctx->q.sockfd, &want_recv, &want_send);
  725. if(!want_send && !Curl_bufq_is_empty(&ctx->q.sendbuf))
  726. want_send = TRUE;
  727. if(want_recv || want_send) {
  728. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  729. struct cf_call_data save;
  730. bool c_exhaust, s_exhaust;
  731. CF_DATA_SAVE(save, cf, data);
  732. c_exhaust = want_send && (!ngtcp2_conn_get_cwnd_left(ctx->qconn) ||
  733. !ngtcp2_conn_get_max_data_left(ctx->qconn));
  734. s_exhaust = want_send && stream && stream->id >= 0 &&
  735. stream->quic_flow_blocked;
  736. want_recv = (want_recv || c_exhaust || s_exhaust);
  737. want_send = (!s_exhaust && want_send) ||
  738. !Curl_bufq_is_empty(&ctx->q.sendbuf);
  739. Curl_pollset_set(data, ps, ctx->q.sockfd, want_recv, want_send);
  740. CF_DATA_RESTORE(cf, save);
  741. }
  742. }
  743. static int cb_h3_stream_close(nghttp3_conn *conn, int64_t sid,
  744. uint64_t app_error_code, void *user_data,
  745. void *stream_user_data)
  746. {
  747. struct Curl_cfilter *cf = user_data;
  748. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  749. struct Curl_easy *data = stream_user_data;
  750. curl_int64_t stream_id = (curl_int64_t)sid;
  751. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  752. (void)conn;
  753. (void)stream_id;
  754. /* we might be called by nghttp3 after we already cleaned up */
  755. if(!stream)
  756. return 0;
  757. stream->closed = TRUE;
  758. stream->error3 = (curl_uint64_t)app_error_code;
  759. if(stream->error3 != NGHTTP3_H3_NO_ERROR) {
  760. stream->reset = TRUE;
  761. stream->send_closed = TRUE;
  762. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] RESET: error %" CURL_PRIu64,
  763. stream->id, stream->error3);
  764. }
  765. else {
  766. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] CLOSED", stream->id);
  767. }
  768. h3_drain_stream(cf, data);
  769. return 0;
  770. }
  771. static void h3_xfer_write_resp_hd(struct Curl_cfilter *cf,
  772. struct Curl_easy *data,
  773. struct h3_stream_ctx *stream,
  774. const char *buf, size_t blen, bool eos)
  775. {
  776. /* If we already encountered an error, skip further writes */
  777. if(!stream->xfer_result) {
  778. stream->xfer_result = Curl_xfer_write_resp_hd(data, buf, blen, eos);
  779. if(stream->xfer_result)
  780. CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu "
  781. "bytes of headers", stream->id, stream->xfer_result, blen);
  782. }
  783. }
  784. static void h3_xfer_write_resp(struct Curl_cfilter *cf,
  785. struct Curl_easy *data,
  786. struct h3_stream_ctx *stream,
  787. const char *buf, size_t blen, bool eos)
  788. {
  789. /* If we already encountered an error, skip further writes */
  790. if(!stream->xfer_result) {
  791. stream->xfer_result = Curl_xfer_write_resp(data, buf, blen, eos);
  792. /* If the transfer write is errored, we do not want any more data */
  793. if(stream->xfer_result) {
  794. CURL_TRC_CF(data, cf, "[%"CURL_PRId64"] error %d writing %zu bytes "
  795. "of data", stream->id, stream->xfer_result, blen);
  796. }
  797. }
  798. }
  799. static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream3_id,
  800. const uint8_t *buf, size_t blen,
  801. void *user_data, void *stream_user_data)
  802. {
  803. struct Curl_cfilter *cf = user_data;
  804. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  805. struct Curl_easy *data = stream_user_data;
  806. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  807. (void)conn;
  808. (void)stream3_id;
  809. if(!stream)
  810. return NGHTTP3_ERR_CALLBACK_FAILURE;
  811. h3_xfer_write_resp(cf, data, stream, (char *)buf, blen, FALSE);
  812. if(blen) {
  813. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] ACK %zu bytes of DATA",
  814. stream->id, blen);
  815. ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream->id, blen);
  816. ngtcp2_conn_extend_max_offset(ctx->qconn, blen);
  817. }
  818. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] DATA len=%zu", stream->id, blen);
  819. return 0;
  820. }
  821. static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream3_id,
  822. size_t consumed, void *user_data,
  823. void *stream_user_data)
  824. {
  825. struct Curl_cfilter *cf = user_data;
  826. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  827. (void)conn;
  828. (void)stream_user_data;
  829. /* nghttp3 has consumed bytes on the QUIC stream and we need to
  830. * tell the QUIC connection to increase its flow control */
  831. ngtcp2_conn_extend_max_stream_offset(ctx->qconn, stream3_id, consumed);
  832. ngtcp2_conn_extend_max_offset(ctx->qconn, consumed);
  833. return 0;
  834. }
  835. static int cb_h3_end_headers(nghttp3_conn *conn, int64_t sid,
  836. int fin, void *user_data, void *stream_user_data)
  837. {
  838. struct Curl_cfilter *cf = user_data;
  839. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  840. struct Curl_easy *data = stream_user_data;
  841. curl_int64_t stream_id = (curl_int64_t)sid;
  842. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  843. (void)conn;
  844. (void)stream_id;
  845. (void)fin;
  846. (void)cf;
  847. if(!stream)
  848. return 0;
  849. /* add a CRLF only if we've received some headers */
  850. h3_xfer_write_resp_hd(cf, data, stream, STRCONST("\r\n"), stream->closed);
  851. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] end_headers, status=%d",
  852. stream_id, stream->status_code);
  853. if(stream->status_code / 100 != 1) {
  854. stream->resp_hds_complete = TRUE;
  855. }
  856. h3_drain_stream(cf, data);
  857. return 0;
  858. }
  859. static int cb_h3_recv_header(nghttp3_conn *conn, int64_t sid,
  860. int32_t token, nghttp3_rcbuf *name,
  861. nghttp3_rcbuf *value, uint8_t flags,
  862. void *user_data, void *stream_user_data)
  863. {
  864. struct Curl_cfilter *cf = user_data;
  865. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  866. curl_int64_t stream_id = (curl_int64_t)sid;
  867. nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
  868. nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
  869. struct Curl_easy *data = stream_user_data;
  870. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  871. CURLcode result = CURLE_OK;
  872. (void)conn;
  873. (void)stream_id;
  874. (void)token;
  875. (void)flags;
  876. (void)cf;
  877. /* we might have cleaned up this transfer already */
  878. if(!stream)
  879. return 0;
  880. if(token == NGHTTP3_QPACK_TOKEN__STATUS) {
  881. result = Curl_http_decode_status(&stream->status_code,
  882. (const char *)h3val.base, h3val.len);
  883. if(result)
  884. return -1;
  885. Curl_dyn_reset(&ctx->scratch);
  886. result = Curl_dyn_addn(&ctx->scratch, STRCONST("HTTP/3 "));
  887. if(!result)
  888. result = Curl_dyn_addn(&ctx->scratch,
  889. (const char *)h3val.base, h3val.len);
  890. if(!result)
  891. result = Curl_dyn_addn(&ctx->scratch, STRCONST(" \r\n"));
  892. if(!result)
  893. h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
  894. Curl_dyn_len(&ctx->scratch), FALSE);
  895. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] status: %s",
  896. stream_id, Curl_dyn_ptr(&ctx->scratch));
  897. if(result) {
  898. return -1;
  899. }
  900. }
  901. else {
  902. /* store as an HTTP1-style header */
  903. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] header: %.*s: %.*s",
  904. stream_id, (int)h3name.len, h3name.base,
  905. (int)h3val.len, h3val.base);
  906. Curl_dyn_reset(&ctx->scratch);
  907. result = Curl_dyn_addn(&ctx->scratch,
  908. (const char *)h3name.base, h3name.len);
  909. if(!result)
  910. result = Curl_dyn_addn(&ctx->scratch, STRCONST(": "));
  911. if(!result)
  912. result = Curl_dyn_addn(&ctx->scratch,
  913. (const char *)h3val.base, h3val.len);
  914. if(!result)
  915. result = Curl_dyn_addn(&ctx->scratch, STRCONST("\r\n"));
  916. if(!result)
  917. h3_xfer_write_resp_hd(cf, data, stream, Curl_dyn_ptr(&ctx->scratch),
  918. Curl_dyn_len(&ctx->scratch), FALSE);
  919. }
  920. return 0;
  921. }
  922. static int cb_h3_stop_sending(nghttp3_conn *conn, int64_t stream_id,
  923. uint64_t app_error_code, void *user_data,
  924. void *stream_user_data)
  925. {
  926. struct Curl_cfilter *cf = user_data;
  927. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  928. int rv;
  929. (void)conn;
  930. (void)stream_user_data;
  931. rv = ngtcp2_conn_shutdown_stream_read(ctx->qconn, 0, stream_id,
  932. app_error_code);
  933. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  934. return NGTCP2_ERR_CALLBACK_FAILURE;
  935. }
  936. return 0;
  937. }
  938. static int cb_h3_reset_stream(nghttp3_conn *conn, int64_t sid,
  939. uint64_t app_error_code, void *user_data,
  940. void *stream_user_data) {
  941. struct Curl_cfilter *cf = user_data;
  942. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  943. curl_int64_t stream_id = (curl_int64_t)sid;
  944. struct Curl_easy *data = stream_user_data;
  945. int rv;
  946. (void)conn;
  947. (void)data;
  948. rv = ngtcp2_conn_shutdown_stream_write(ctx->qconn, 0, stream_id,
  949. app_error_code);
  950. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] reset -> %d", stream_id, rv);
  951. if(rv && rv != NGTCP2_ERR_STREAM_NOT_FOUND) {
  952. return NGTCP2_ERR_CALLBACK_FAILURE;
  953. }
  954. return 0;
  955. }
  956. static nghttp3_callbacks ngh3_callbacks = {
  957. cb_h3_acked_req_body, /* acked_stream_data */
  958. cb_h3_stream_close,
  959. cb_h3_recv_data,
  960. cb_h3_deferred_consume,
  961. NULL, /* begin_headers */
  962. cb_h3_recv_header,
  963. cb_h3_end_headers,
  964. NULL, /* begin_trailers */
  965. cb_h3_recv_header,
  966. NULL, /* end_trailers */
  967. cb_h3_stop_sending,
  968. NULL, /* end_stream */
  969. cb_h3_reset_stream,
  970. NULL, /* shutdown */
  971. NULL /* recv_settings */
  972. };
  973. static CURLcode init_ngh3_conn(struct Curl_cfilter *cf)
  974. {
  975. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  976. CURLcode result;
  977. int rc;
  978. int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;
  979. if(ngtcp2_conn_get_streams_uni_left(ctx->qconn) < 3) {
  980. return CURLE_QUIC_CONNECT_ERROR;
  981. }
  982. nghttp3_settings_default(&ctx->h3settings);
  983. rc = nghttp3_conn_client_new(&ctx->h3conn,
  984. &ngh3_callbacks,
  985. &ctx->h3settings,
  986. nghttp3_mem_default(),
  987. cf);
  988. if(rc) {
  989. result = CURLE_OUT_OF_MEMORY;
  990. goto fail;
  991. }
  992. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &ctrl_stream_id, NULL);
  993. if(rc) {
  994. result = CURLE_QUIC_CONNECT_ERROR;
  995. goto fail;
  996. }
  997. rc = nghttp3_conn_bind_control_stream(ctx->h3conn, ctrl_stream_id);
  998. if(rc) {
  999. result = CURLE_QUIC_CONNECT_ERROR;
  1000. goto fail;
  1001. }
  1002. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_enc_stream_id, NULL);
  1003. if(rc) {
  1004. result = CURLE_QUIC_CONNECT_ERROR;
  1005. goto fail;
  1006. }
  1007. rc = ngtcp2_conn_open_uni_stream(ctx->qconn, &qpack_dec_stream_id, NULL);
  1008. if(rc) {
  1009. result = CURLE_QUIC_CONNECT_ERROR;
  1010. goto fail;
  1011. }
  1012. rc = nghttp3_conn_bind_qpack_streams(ctx->h3conn, qpack_enc_stream_id,
  1013. qpack_dec_stream_id);
  1014. if(rc) {
  1015. result = CURLE_QUIC_CONNECT_ERROR;
  1016. goto fail;
  1017. }
  1018. return CURLE_OK;
  1019. fail:
  1020. return result;
  1021. }
  1022. static ssize_t recv_closed_stream(struct Curl_cfilter *cf,
  1023. struct Curl_easy *data,
  1024. struct h3_stream_ctx *stream,
  1025. CURLcode *err)
  1026. {
  1027. ssize_t nread = -1;
  1028. (void)cf;
  1029. if(stream->reset) {
  1030. failf(data,
  1031. "HTTP/3 stream %" CURL_PRId64 " reset by server", stream->id);
  1032. *err = data->req.bytecount? CURLE_PARTIAL_FILE : CURLE_HTTP3;
  1033. goto out;
  1034. }
  1035. else if(!stream->resp_hds_complete) {
  1036. failf(data,
  1037. "HTTP/3 stream %" CURL_PRId64 " was closed cleanly, but before "
  1038. "getting all response header fields, treated as error",
  1039. stream->id);
  1040. *err = CURLE_HTTP3;
  1041. goto out;
  1042. }
  1043. *err = CURLE_OK;
  1044. nread = 0;
  1045. out:
  1046. return nread;
  1047. }
  1048. /* incoming data frames on the h3 stream */
  1049. static ssize_t cf_ngtcp2_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1050. char *buf, size_t blen, CURLcode *err)
  1051. {
  1052. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1053. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1054. ssize_t nread = -1;
  1055. struct cf_call_data save;
  1056. struct pkt_io_ctx pktx;
  1057. (void)ctx;
  1058. (void)buf;
  1059. CF_DATA_SAVE(save, cf, data);
  1060. DEBUGASSERT(cf->connected);
  1061. DEBUGASSERT(ctx);
  1062. DEBUGASSERT(ctx->qconn);
  1063. DEBUGASSERT(ctx->h3conn);
  1064. *err = CURLE_OK;
  1065. pktx_init(&pktx, cf, data);
  1066. if(!stream || ctx->shutdown_started) {
  1067. *err = CURLE_RECV_ERROR;
  1068. goto out;
  1069. }
  1070. if(cf_progress_ingress(cf, data, &pktx)) {
  1071. *err = CURLE_RECV_ERROR;
  1072. nread = -1;
  1073. goto out;
  1074. }
  1075. if(stream->xfer_result) {
  1076. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] xfer write failed", stream->id);
  1077. cf_ngtcp2_stream_close(cf, data, stream);
  1078. *err = stream->xfer_result;
  1079. nread = -1;
  1080. goto out;
  1081. }
  1082. else if(stream->closed) {
  1083. nread = recv_closed_stream(cf, data, stream, err);
  1084. goto out;
  1085. }
  1086. *err = CURLE_AGAIN;
  1087. nread = -1;
  1088. out:
  1089. if(cf_progress_egress(cf, data, &pktx)) {
  1090. *err = CURLE_SEND_ERROR;
  1091. nread = -1;
  1092. }
  1093. else {
  1094. CURLcode result2 = check_and_set_expiry(cf, data, &pktx);
  1095. if(result2) {
  1096. *err = result2;
  1097. nread = -1;
  1098. }
  1099. }
  1100. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_recv(blen=%zu) -> %zd, %d",
  1101. stream? stream->id : -1, blen, nread, *err);
  1102. CF_DATA_RESTORE(cf, save);
  1103. return nread;
  1104. }
  1105. static int cb_h3_acked_req_body(nghttp3_conn *conn, int64_t stream_id,
  1106. uint64_t datalen, void *user_data,
  1107. void *stream_user_data)
  1108. {
  1109. struct Curl_cfilter *cf = user_data;
  1110. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1111. struct Curl_easy *data = stream_user_data;
  1112. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1113. size_t skiplen;
  1114. (void)cf;
  1115. if(!stream)
  1116. return 0;
  1117. /* The server acknowledged `datalen` of bytes from our request body.
  1118. * This is a delta. We have kept this data in `sendbuf` for
  1119. * re-transmissions and can free it now. */
  1120. if(datalen >= (uint64_t)stream->sendbuf_len_in_flight)
  1121. skiplen = stream->sendbuf_len_in_flight;
  1122. else
  1123. skiplen = (size_t)datalen;
  1124. Curl_bufq_skip(&stream->sendbuf, skiplen);
  1125. stream->sendbuf_len_in_flight -= skiplen;
  1126. /* Everything ACKed, we resume upload processing */
  1127. if(!stream->sendbuf_len_in_flight) {
  1128. int rv = nghttp3_conn_resume_stream(conn, stream_id);
  1129. if(rv && rv != NGHTTP3_ERR_STREAM_NOT_FOUND) {
  1130. return NGTCP2_ERR_CALLBACK_FAILURE;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. static nghttp3_ssize
  1136. cb_h3_read_req_body(nghttp3_conn *conn, int64_t stream_id,
  1137. nghttp3_vec *vec, size_t veccnt,
  1138. uint32_t *pflags, void *user_data,
  1139. void *stream_user_data)
  1140. {
  1141. struct Curl_cfilter *cf = user_data;
  1142. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1143. struct Curl_easy *data = stream_user_data;
  1144. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1145. ssize_t nwritten = 0;
  1146. size_t nvecs = 0;
  1147. (void)cf;
  1148. (void)conn;
  1149. (void)stream_id;
  1150. (void)user_data;
  1151. (void)veccnt;
  1152. if(!stream)
  1153. return NGHTTP3_ERR_CALLBACK_FAILURE;
  1154. /* nghttp3 keeps references to the sendbuf data until it is ACKed
  1155. * by the server (see `cb_h3_acked_req_body()` for updates).
  1156. * `sendbuf_len_in_flight` is the amount of bytes in `sendbuf`
  1157. * that we have already passed to nghttp3, but which have not been
  1158. * ACKed yet.
  1159. * Any amount beyond `sendbuf_len_in_flight` we need still to pass
  1160. * to nghttp3. Do that now, if we can. */
  1161. if(stream->sendbuf_len_in_flight < Curl_bufq_len(&stream->sendbuf)) {
  1162. nvecs = 0;
  1163. while(nvecs < veccnt &&
  1164. Curl_bufq_peek_at(&stream->sendbuf,
  1165. stream->sendbuf_len_in_flight,
  1166. (const unsigned char **)&vec[nvecs].base,
  1167. &vec[nvecs].len)) {
  1168. stream->sendbuf_len_in_flight += vec[nvecs].len;
  1169. nwritten += vec[nvecs].len;
  1170. ++nvecs;
  1171. }
  1172. DEBUGASSERT(nvecs > 0); /* we SHOULD have been be able to peek */
  1173. }
  1174. if(nwritten > 0 && stream->upload_left != -1)
  1175. stream->upload_left -= nwritten;
  1176. /* When we stopped sending and everything in `sendbuf` is "in flight",
  1177. * we are at the end of the request body. */
  1178. if(stream->upload_left == 0) {
  1179. *pflags = NGHTTP3_DATA_FLAG_EOF;
  1180. stream->send_closed = TRUE;
  1181. }
  1182. else if(!nwritten) {
  1183. /* Not EOF, and nothing to give, we signal WOULDBLOCK. */
  1184. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> AGAIN",
  1185. stream->id);
  1186. return NGHTTP3_ERR_WOULDBLOCK;
  1187. }
  1188. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] read req body -> "
  1189. "%d vecs%s with %zu (buffered=%zu, left=%"
  1190. CURL_FORMAT_CURL_OFF_T ")",
  1191. stream->id, (int)nvecs,
  1192. *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
  1193. nwritten, Curl_bufq_len(&stream->sendbuf),
  1194. stream->upload_left);
  1195. return (nghttp3_ssize)nvecs;
  1196. }
  1197. /* Index where :authority header field will appear in request header
  1198. field list. */
  1199. #define AUTHORITY_DST_IDX 3
  1200. static ssize_t h3_stream_open(struct Curl_cfilter *cf,
  1201. struct Curl_easy *data,
  1202. const void *buf, size_t len,
  1203. CURLcode *err)
  1204. {
  1205. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1206. struct h3_stream_ctx *stream = NULL;
  1207. int64_t sid;
  1208. struct dynhds h2_headers;
  1209. size_t nheader;
  1210. nghttp3_nv *nva = NULL;
  1211. int rc = 0;
  1212. unsigned int i;
  1213. ssize_t nwritten = -1;
  1214. nghttp3_data_reader reader;
  1215. nghttp3_data_reader *preader = NULL;
  1216. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  1217. *err = h3_data_setup(cf, data);
  1218. if(*err)
  1219. goto out;
  1220. stream = H3_STREAM_CTX(ctx, data);
  1221. DEBUGASSERT(stream);
  1222. if(!stream) {
  1223. *err = CURLE_FAILED_INIT;
  1224. goto out;
  1225. }
  1226. nwritten = Curl_h1_req_parse_read(&stream->h1, buf, len, NULL, 0, err);
  1227. if(nwritten < 0)
  1228. goto out;
  1229. if(!stream->h1.done) {
  1230. /* need more data */
  1231. goto out;
  1232. }
  1233. DEBUGASSERT(stream->h1.req);
  1234. *err = Curl_http_req_to_h2(&h2_headers, stream->h1.req, data);
  1235. if(*err) {
  1236. nwritten = -1;
  1237. goto out;
  1238. }
  1239. /* no longer needed */
  1240. Curl_h1_req_parse_free(&stream->h1);
  1241. nheader = Curl_dynhds_count(&h2_headers);
  1242. nva = malloc(sizeof(nghttp3_nv) * nheader);
  1243. if(!nva) {
  1244. *err = CURLE_OUT_OF_MEMORY;
  1245. nwritten = -1;
  1246. goto out;
  1247. }
  1248. for(i = 0; i < nheader; ++i) {
  1249. struct dynhds_entry *e = Curl_dynhds_getn(&h2_headers, i);
  1250. nva[i].name = (unsigned char *)e->name;
  1251. nva[i].namelen = e->namelen;
  1252. nva[i].value = (unsigned char *)e->value;
  1253. nva[i].valuelen = e->valuelen;
  1254. nva[i].flags = NGHTTP3_NV_FLAG_NONE;
  1255. }
  1256. rc = ngtcp2_conn_open_bidi_stream(ctx->qconn, &sid, data);
  1257. if(rc) {
  1258. failf(data, "can get bidi streams");
  1259. *err = CURLE_SEND_ERROR;
  1260. nwritten = -1;
  1261. goto out;
  1262. }
  1263. stream->id = (curl_int64_t)sid;
  1264. ++ctx->used_bidi_streams;
  1265. switch(data->state.httpreq) {
  1266. case HTTPREQ_POST:
  1267. case HTTPREQ_POST_FORM:
  1268. case HTTPREQ_POST_MIME:
  1269. case HTTPREQ_PUT:
  1270. /* known request body size or -1 */
  1271. if(data->state.infilesize != -1)
  1272. stream->upload_left = data->state.infilesize;
  1273. else
  1274. /* data sending without specifying the data amount up front */
  1275. stream->upload_left = -1; /* unknown */
  1276. break;
  1277. default:
  1278. /* there is not request body */
  1279. stream->upload_left = 0; /* no request body */
  1280. break;
  1281. }
  1282. stream->send_closed = (stream->upload_left == 0);
  1283. if(!stream->send_closed) {
  1284. reader.read_data = cb_h3_read_req_body;
  1285. preader = &reader;
  1286. }
  1287. rc = nghttp3_conn_submit_request(ctx->h3conn, stream->id,
  1288. nva, nheader, preader, data);
  1289. if(rc) {
  1290. switch(rc) {
  1291. case NGHTTP3_ERR_CONN_CLOSING:
  1292. CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send, "
  1293. "connection is closing", stream->id);
  1294. break;
  1295. default:
  1296. CURL_TRC_CF(data, cf, "h3sid[%" CURL_PRId64 "] failed to send -> "
  1297. "%d (%s)", stream->id, rc, ngtcp2_strerror(rc));
  1298. break;
  1299. }
  1300. *err = CURLE_SEND_ERROR;
  1301. nwritten = -1;
  1302. goto out;
  1303. }
  1304. if(Curl_trc_is_verbose(data)) {
  1305. infof(data, "[HTTP/3] [%" CURL_PRId64 "] OPENED stream for %s",
  1306. stream->id, data->state.url);
  1307. for(i = 0; i < nheader; ++i) {
  1308. infof(data, "[HTTP/3] [%" CURL_PRId64 "] [%.*s: %.*s]", stream->id,
  1309. (int)nva[i].namelen, nva[i].name,
  1310. (int)nva[i].valuelen, nva[i].value);
  1311. }
  1312. }
  1313. out:
  1314. free(nva);
  1315. Curl_dynhds_free(&h2_headers);
  1316. return nwritten;
  1317. }
  1318. static ssize_t cf_ngtcp2_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1319. const void *buf, size_t len, CURLcode *err)
  1320. {
  1321. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1322. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1323. ssize_t sent = 0;
  1324. struct cf_call_data save;
  1325. struct pkt_io_ctx pktx;
  1326. CURLcode result;
  1327. CF_DATA_SAVE(save, cf, data);
  1328. DEBUGASSERT(cf->connected);
  1329. DEBUGASSERT(ctx->qconn);
  1330. DEBUGASSERT(ctx->h3conn);
  1331. pktx_init(&pktx, cf, data);
  1332. *err = CURLE_OK;
  1333. result = cf_progress_ingress(cf, data, &pktx);
  1334. if(result) {
  1335. *err = result;
  1336. sent = -1;
  1337. }
  1338. if(!stream || stream->id < 0) {
  1339. if(ctx->shutdown_started) {
  1340. CURL_TRC_CF(data, cf, "cannot open stream on closed connection");
  1341. *err = CURLE_SEND_ERROR;
  1342. sent = -1;
  1343. goto out;
  1344. }
  1345. sent = h3_stream_open(cf, data, buf, len, err);
  1346. if(sent < 0) {
  1347. CURL_TRC_CF(data, cf, "failed to open stream -> %d", *err);
  1348. goto out;
  1349. }
  1350. stream = H3_STREAM_CTX(ctx, data);
  1351. }
  1352. else if(stream->xfer_result) {
  1353. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] xfer write failed", stream->id);
  1354. cf_ngtcp2_stream_close(cf, data, stream);
  1355. *err = stream->xfer_result;
  1356. sent = -1;
  1357. goto out;
  1358. }
  1359. else if(stream->upload_blocked_len) {
  1360. /* the data in `buf` has already been submitted or added to the
  1361. * buffers, but have been EAGAINed on the last invocation. */
  1362. DEBUGASSERT(len >= stream->upload_blocked_len);
  1363. if(len < stream->upload_blocked_len) {
  1364. /* Did we get called again with a smaller `len`? This should not
  1365. * happen. We are not prepared to handle that. */
  1366. failf(data, "HTTP/3 send again with decreased length");
  1367. *err = CURLE_HTTP3;
  1368. sent = -1;
  1369. goto out;
  1370. }
  1371. sent = (ssize_t)stream->upload_blocked_len;
  1372. stream->upload_blocked_len = 0;
  1373. }
  1374. else if(stream->closed) {
  1375. if(stream->resp_hds_complete) {
  1376. /* Server decided to close the stream after having sent us a final
  1377. * response. This is valid if it is not interested in the request
  1378. * body. This happens on 30x or 40x responses.
  1379. * We silently discard the data sent, since this is not a transport
  1380. * error situation. */
  1381. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] discarding data"
  1382. "on closed stream with response", stream->id);
  1383. *err = CURLE_OK;
  1384. sent = (ssize_t)len;
  1385. goto out;
  1386. }
  1387. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] send_body(len=%zu) "
  1388. "-> stream closed", stream->id, len);
  1389. *err = CURLE_HTTP3;
  1390. sent = -1;
  1391. goto out;
  1392. }
  1393. else if(ctx->shutdown_started) {
  1394. CURL_TRC_CF(data, cf, "cannot send on closed connection");
  1395. *err = CURLE_SEND_ERROR;
  1396. sent = -1;
  1397. goto out;
  1398. }
  1399. else {
  1400. sent = Curl_bufq_write(&stream->sendbuf, buf, len, err);
  1401. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send, add to "
  1402. "sendbuf(len=%zu) -> %zd, %d",
  1403. stream->id, len, sent, *err);
  1404. if(sent < 0) {
  1405. goto out;
  1406. }
  1407. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  1408. }
  1409. result = cf_progress_egress(cf, data, &pktx);
  1410. if(result) {
  1411. *err = result;
  1412. sent = -1;
  1413. }
  1414. if(stream && sent > 0 && stream->sendbuf_len_in_flight) {
  1415. /* We have unacknowledged DATA and cannot report success to our
  1416. * caller. Instead we EAGAIN and remember how much we have already
  1417. * "written" into our various internal connection buffers. */
  1418. stream->upload_blocked_len = sent;
  1419. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu), "
  1420. "%zu bytes in flight -> EGAIN", stream->id, len,
  1421. stream->sendbuf_len_in_flight);
  1422. *err = CURLE_AGAIN;
  1423. sent = -1;
  1424. }
  1425. out:
  1426. result = check_and_set_expiry(cf, data, &pktx);
  1427. if(result) {
  1428. *err = result;
  1429. sent = -1;
  1430. }
  1431. CURL_TRC_CF(data, cf, "[%" CURL_PRId64 "] cf_send(len=%zu) -> %zd, %d",
  1432. stream? stream->id : -1, len, sent, *err);
  1433. CF_DATA_RESTORE(cf, save);
  1434. return sent;
  1435. }
  1436. static CURLcode qng_verify_peer(struct Curl_cfilter *cf,
  1437. struct Curl_easy *data)
  1438. {
  1439. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1440. cf->conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
  1441. cf->conn->httpversion = 30;
  1442. cf->conn->bundle->multiuse = BUNDLE_MULTIPLEX;
  1443. return Curl_vquic_tls_verify_peer(&ctx->tls, cf, data, &ctx->peer);
  1444. }
  1445. static CURLcode recv_pkt(const unsigned char *pkt, size_t pktlen,
  1446. struct sockaddr_storage *remote_addr,
  1447. socklen_t remote_addrlen, int ecn,
  1448. void *userp)
  1449. {
  1450. struct pkt_io_ctx *pktx = userp;
  1451. struct cf_ngtcp2_ctx *ctx = pktx->cf->ctx;
  1452. ngtcp2_pkt_info pi;
  1453. ngtcp2_path path;
  1454. int rv;
  1455. ++pktx->pkt_count;
  1456. ngtcp2_addr_init(&path.local, (struct sockaddr *)&ctx->q.local_addr,
  1457. (socklen_t)ctx->q.local_addrlen);
  1458. ngtcp2_addr_init(&path.remote, (struct sockaddr *)remote_addr,
  1459. remote_addrlen);
  1460. pi.ecn = (uint8_t)ecn;
  1461. rv = ngtcp2_conn_read_pkt(ctx->qconn, &path, &pi, pkt, pktlen, pktx->ts);
  1462. if(rv) {
  1463. CURL_TRC_CF(pktx->data, pktx->cf, "ingress, read_pkt -> %s (%d)",
  1464. ngtcp2_strerror(rv), rv);
  1465. cf_ngtcp2_err_set(pktx->cf, pktx->data, rv);
  1466. if(rv == NGTCP2_ERR_CRYPTO)
  1467. /* this is a "TLS problem", but a failed certificate verification
  1468. is a common reason for this */
  1469. return CURLE_PEER_FAILED_VERIFICATION;
  1470. return CURLE_RECV_ERROR;
  1471. }
  1472. return CURLE_OK;
  1473. }
  1474. static CURLcode cf_progress_ingress(struct Curl_cfilter *cf,
  1475. struct Curl_easy *data,
  1476. struct pkt_io_ctx *pktx)
  1477. {
  1478. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1479. struct pkt_io_ctx local_pktx;
  1480. size_t pkts_chunk = 128, i;
  1481. CURLcode result = CURLE_OK;
  1482. if(!pktx) {
  1483. pktx_init(&local_pktx, cf, data);
  1484. pktx = &local_pktx;
  1485. }
  1486. else {
  1487. pktx_update_time(pktx, cf);
  1488. }
  1489. result = Curl_vquic_tls_before_recv(&ctx->tls, cf, data);
  1490. if(result)
  1491. return result;
  1492. for(i = 0; i < 4; ++i) {
  1493. if(i)
  1494. pktx_update_time(pktx, cf);
  1495. pktx->pkt_count = 0;
  1496. result = vquic_recv_packets(cf, data, &ctx->q, pkts_chunk,
  1497. recv_pkt, pktx);
  1498. if(result || !pktx->pkt_count) /* error or got nothing */
  1499. break;
  1500. }
  1501. return result;
  1502. }
  1503. /**
  1504. * Read a network packet to send from ngtcp2 into `buf`.
  1505. * Return number of bytes written or -1 with *err set.
  1506. */
  1507. static ssize_t read_pkt_to_send(void *userp,
  1508. unsigned char *buf, size_t buflen,
  1509. CURLcode *err)
  1510. {
  1511. struct pkt_io_ctx *x = userp;
  1512. struct cf_ngtcp2_ctx *ctx = x->cf->ctx;
  1513. nghttp3_vec vec[16];
  1514. nghttp3_ssize veccnt;
  1515. ngtcp2_ssize ndatalen;
  1516. uint32_t flags;
  1517. int64_t stream_id;
  1518. int fin;
  1519. ssize_t nwritten, n;
  1520. veccnt = 0;
  1521. stream_id = -1;
  1522. fin = 0;
  1523. /* ngtcp2 may want to put several frames from different streams into
  1524. * this packet. `NGTCP2_WRITE_STREAM_FLAG_MORE` tells it to do so.
  1525. * When `NGTCP2_ERR_WRITE_MORE` is returned, we *need* to make
  1526. * another iteration.
  1527. * When ngtcp2 is happy (because it has no other frame that would fit
  1528. * or it has nothing more to send), it returns the total length
  1529. * of the assembled packet. This may be 0 if there was nothing to send. */
  1530. nwritten = 0;
  1531. *err = CURLE_OK;
  1532. for(;;) {
  1533. if(ctx->h3conn && ngtcp2_conn_get_max_data_left(ctx->qconn)) {
  1534. veccnt = nghttp3_conn_writev_stream(ctx->h3conn, &stream_id, &fin, vec,
  1535. sizeof(vec) / sizeof(vec[0]));
  1536. if(veccnt < 0) {
  1537. failf(x->data, "nghttp3_conn_writev_stream returned error: %s",
  1538. nghttp3_strerror((int)veccnt));
  1539. cf_ngtcp2_h3_err_set(x->cf, x->data, (int)veccnt);
  1540. *err = CURLE_SEND_ERROR;
  1541. return -1;
  1542. }
  1543. }
  1544. flags = NGTCP2_WRITE_STREAM_FLAG_MORE |
  1545. (fin ? NGTCP2_WRITE_STREAM_FLAG_FIN : 0);
  1546. n = ngtcp2_conn_writev_stream(ctx->qconn, &x->ps.path,
  1547. NULL, buf, buflen,
  1548. &ndatalen, flags, stream_id,
  1549. (const ngtcp2_vec *)vec, veccnt, x->ts);
  1550. if(n == 0) {
  1551. /* nothing to send */
  1552. *err = CURLE_AGAIN;
  1553. nwritten = -1;
  1554. goto out;
  1555. }
  1556. else if(n < 0) {
  1557. switch(n) {
  1558. case NGTCP2_ERR_STREAM_DATA_BLOCKED: {
  1559. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, x->data);
  1560. DEBUGASSERT(ndatalen == -1);
  1561. nghttp3_conn_block_stream(ctx->h3conn, stream_id);
  1562. CURL_TRC_CF(x->data, x->cf, "[%" CURL_PRId64 "] block quic flow",
  1563. (curl_int64_t)stream_id);
  1564. DEBUGASSERT(stream);
  1565. if(stream)
  1566. stream->quic_flow_blocked = TRUE;
  1567. n = 0;
  1568. break;
  1569. }
  1570. case NGTCP2_ERR_STREAM_SHUT_WR:
  1571. DEBUGASSERT(ndatalen == -1);
  1572. nghttp3_conn_shutdown_stream_write(ctx->h3conn, stream_id);
  1573. n = 0;
  1574. break;
  1575. case NGTCP2_ERR_WRITE_MORE:
  1576. /* ngtcp2 wants to send more. update the flow of the stream whose data
  1577. * is in the buffer and continue */
  1578. DEBUGASSERT(ndatalen >= 0);
  1579. n = 0;
  1580. break;
  1581. default:
  1582. DEBUGASSERT(ndatalen == -1);
  1583. failf(x->data, "ngtcp2_conn_writev_stream returned error: %s",
  1584. ngtcp2_strerror((int)n));
  1585. cf_ngtcp2_err_set(x->cf, x->data, (int)n);
  1586. *err = CURLE_SEND_ERROR;
  1587. nwritten = -1;
  1588. goto out;
  1589. }
  1590. }
  1591. if(ndatalen >= 0) {
  1592. /* we add the amount of data bytes to the flow windows */
  1593. int rv = nghttp3_conn_add_write_offset(ctx->h3conn, stream_id, ndatalen);
  1594. if(rv) {
  1595. failf(x->data, "nghttp3_conn_add_write_offset returned error: %s\n",
  1596. nghttp3_strerror(rv));
  1597. return CURLE_SEND_ERROR;
  1598. }
  1599. }
  1600. if(n > 0) {
  1601. /* packet assembled, leave */
  1602. nwritten = n;
  1603. goto out;
  1604. }
  1605. }
  1606. out:
  1607. return nwritten;
  1608. }
  1609. static CURLcode cf_progress_egress(struct Curl_cfilter *cf,
  1610. struct Curl_easy *data,
  1611. struct pkt_io_ctx *pktx)
  1612. {
  1613. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1614. ssize_t nread;
  1615. size_t max_payload_size, path_max_payload_size, max_pktcnt;
  1616. size_t pktcnt = 0;
  1617. size_t gsolen = 0; /* this disables gso until we have a clue */
  1618. CURLcode curlcode;
  1619. struct pkt_io_ctx local_pktx;
  1620. if(!pktx) {
  1621. pktx_init(&local_pktx, cf, data);
  1622. pktx = &local_pktx;
  1623. }
  1624. else {
  1625. pktx_update_time(pktx, cf);
  1626. ngtcp2_path_storage_zero(&pktx->ps);
  1627. }
  1628. curlcode = vquic_flush(cf, data, &ctx->q);
  1629. if(curlcode) {
  1630. if(curlcode == CURLE_AGAIN) {
  1631. Curl_expire(data, 1, EXPIRE_QUIC);
  1632. return CURLE_OK;
  1633. }
  1634. return curlcode;
  1635. }
  1636. /* In UDP, there is a maximum theoretical packet paload length and
  1637. * a minimum payload length that is "guaranteed" to work.
  1638. * To detect if this minimum payload can be increased, ngtcp2 sends
  1639. * now and then a packet payload larger than the minimum. It that
  1640. * is ACKed by the peer, both parties know that it works and
  1641. * the subsequent packets can use a larger one.
  1642. * This is called PMTUD (Path Maximum Transmission Unit Discovery).
  1643. * Since a PMTUD might be rejected right on send, we do not want it
  1644. * be followed by other packets of lesser size. Because those would
  1645. * also fail then. So, if we detect a PMTUD while buffering, we flush.
  1646. */
  1647. max_payload_size = ngtcp2_conn_get_max_tx_udp_payload_size(ctx->qconn);
  1648. path_max_payload_size =
  1649. ngtcp2_conn_get_path_max_tx_udp_payload_size(ctx->qconn);
  1650. /* maximum number of packets buffered before we flush to the socket */
  1651. max_pktcnt = CURLMIN(MAX_PKT_BURST,
  1652. ctx->q.sendbuf.chunk_size / max_payload_size);
  1653. for(;;) {
  1654. /* add the next packet to send, if any, to our buffer */
  1655. nread = Curl_bufq_sipn(&ctx->q.sendbuf, max_payload_size,
  1656. read_pkt_to_send, pktx, &curlcode);
  1657. if(nread < 0) {
  1658. if(curlcode != CURLE_AGAIN)
  1659. return curlcode;
  1660. /* Nothing more to add, flush and leave */
  1661. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  1662. if(curlcode) {
  1663. if(curlcode == CURLE_AGAIN) {
  1664. Curl_expire(data, 1, EXPIRE_QUIC);
  1665. return CURLE_OK;
  1666. }
  1667. return curlcode;
  1668. }
  1669. goto out;
  1670. }
  1671. DEBUGASSERT(nread > 0);
  1672. if(pktcnt == 0) {
  1673. /* first packet in buffer. This is either of a known, "good"
  1674. * payload size or it is a PMTUD. We'll see. */
  1675. gsolen = (size_t)nread;
  1676. }
  1677. else if((size_t)nread > gsolen ||
  1678. (gsolen > path_max_payload_size && (size_t)nread != gsolen)) {
  1679. /* The just added packet is a PMTUD *or* the one(s) before the
  1680. * just added were PMTUD and the last one is smaller.
  1681. * Flush the buffer before the last add. */
  1682. curlcode = vquic_send_tail_split(cf, data, &ctx->q,
  1683. gsolen, nread, nread);
  1684. if(curlcode) {
  1685. if(curlcode == CURLE_AGAIN) {
  1686. Curl_expire(data, 1, EXPIRE_QUIC);
  1687. return CURLE_OK;
  1688. }
  1689. return curlcode;
  1690. }
  1691. pktcnt = 0;
  1692. continue;
  1693. }
  1694. if(++pktcnt >= max_pktcnt || (size_t)nread < gsolen) {
  1695. /* Reached MAX_PKT_BURST *or*
  1696. * the capacity of our buffer *or*
  1697. * last add was shorter than the previous ones, flush */
  1698. curlcode = vquic_send(cf, data, &ctx->q, gsolen);
  1699. if(curlcode) {
  1700. if(curlcode == CURLE_AGAIN) {
  1701. Curl_expire(data, 1, EXPIRE_QUIC);
  1702. return CURLE_OK;
  1703. }
  1704. return curlcode;
  1705. }
  1706. /* pktbuf has been completely sent */
  1707. pktcnt = 0;
  1708. }
  1709. }
  1710. out:
  1711. return CURLE_OK;
  1712. }
  1713. /*
  1714. * Called from transfer.c:data_pending to know if we should keep looping
  1715. * to receive more data from the connection.
  1716. */
  1717. static bool cf_ngtcp2_data_pending(struct Curl_cfilter *cf,
  1718. const struct Curl_easy *data)
  1719. {
  1720. (void)cf;
  1721. (void)data;
  1722. return FALSE;
  1723. }
  1724. static CURLcode h3_data_pause(struct Curl_cfilter *cf,
  1725. struct Curl_easy *data,
  1726. bool pause)
  1727. {
  1728. /* TODO: there seems right now no API in ngtcp2 to shrink/enlarge
  1729. * the streams windows. As we do in HTTP/2. */
  1730. if(!pause) {
  1731. h3_drain_stream(cf, data);
  1732. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  1733. }
  1734. return CURLE_OK;
  1735. }
  1736. static CURLcode cf_ngtcp2_data_event(struct Curl_cfilter *cf,
  1737. struct Curl_easy *data,
  1738. int event, int arg1, void *arg2)
  1739. {
  1740. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1741. CURLcode result = CURLE_OK;
  1742. struct cf_call_data save;
  1743. CF_DATA_SAVE(save, cf, data);
  1744. (void)arg1;
  1745. (void)arg2;
  1746. switch(event) {
  1747. case CF_CTRL_DATA_SETUP:
  1748. break;
  1749. case CF_CTRL_DATA_PAUSE:
  1750. result = h3_data_pause(cf, data, (arg1 != 0));
  1751. break;
  1752. case CF_CTRL_DATA_DETACH:
  1753. h3_data_done(cf, data);
  1754. break;
  1755. case CF_CTRL_DATA_DONE:
  1756. h3_data_done(cf, data);
  1757. break;
  1758. case CF_CTRL_DATA_DONE_SEND: {
  1759. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1760. if(stream && !stream->send_closed) {
  1761. stream->send_closed = TRUE;
  1762. stream->upload_left = Curl_bufq_len(&stream->sendbuf);
  1763. (void)nghttp3_conn_resume_stream(ctx->h3conn, stream->id);
  1764. }
  1765. break;
  1766. }
  1767. case CF_CTRL_DATA_IDLE: {
  1768. struct h3_stream_ctx *stream = H3_STREAM_CTX(ctx, data);
  1769. CURL_TRC_CF(data, cf, "data idle");
  1770. if(stream && !stream->closed) {
  1771. result = check_and_set_expiry(cf, data, NULL);
  1772. if(result)
  1773. CURL_TRC_CF(data, cf, "data idle, check_and_set_expiry -> %d", result);
  1774. }
  1775. break;
  1776. }
  1777. default:
  1778. break;
  1779. }
  1780. CF_DATA_RESTORE(cf, save);
  1781. return result;
  1782. }
  1783. static void cf_ngtcp2_ctx_clear(struct cf_ngtcp2_ctx *ctx)
  1784. {
  1785. struct cf_call_data save = ctx->call_data;
  1786. if(ctx->qlogfd != -1) {
  1787. close(ctx->qlogfd);
  1788. }
  1789. Curl_vquic_tls_cleanup(&ctx->tls);
  1790. vquic_ctx_free(&ctx->q);
  1791. if(ctx->h3conn)
  1792. nghttp3_conn_del(ctx->h3conn);
  1793. if(ctx->qconn)
  1794. ngtcp2_conn_del(ctx->qconn);
  1795. Curl_bufcp_free(&ctx->stream_bufcp);
  1796. Curl_dyn_free(&ctx->scratch);
  1797. Curl_hash_clean(&ctx->streams);
  1798. Curl_hash_destroy(&ctx->streams);
  1799. Curl_ssl_peer_cleanup(&ctx->peer);
  1800. memset(ctx, 0, sizeof(*ctx));
  1801. ctx->qlogfd = -1;
  1802. ctx->call_data = save;
  1803. }
  1804. static CURLcode cf_ngtcp2_shutdown(struct Curl_cfilter *cf,
  1805. struct Curl_easy *data, bool *done)
  1806. {
  1807. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1808. struct cf_call_data save;
  1809. struct pkt_io_ctx pktx;
  1810. CURLcode result = CURLE_OK;
  1811. if(cf->shutdown || !ctx->qconn) {
  1812. *done = TRUE;
  1813. return CURLE_OK;
  1814. }
  1815. CF_DATA_SAVE(save, cf, data);
  1816. *done = FALSE;
  1817. pktx_init(&pktx, cf, data);
  1818. if(!ctx->shutdown_started) {
  1819. char buffer[NGTCP2_MAX_UDP_PAYLOAD_SIZE];
  1820. ngtcp2_ssize nwritten;
  1821. if(!Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1822. CURL_TRC_CF(data, cf, "shutdown, flushing sendbuf");
  1823. result = cf_progress_egress(cf, data, &pktx);
  1824. if(!Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1825. CURL_TRC_CF(data, cf, "sending shutdown packets blocked");
  1826. result = CURLE_OK;
  1827. goto out;
  1828. }
  1829. else if(result) {
  1830. CURL_TRC_CF(data, cf, "shutdown, error %d flushing sendbuf", result);
  1831. *done = TRUE;
  1832. goto out;
  1833. }
  1834. }
  1835. ctx->shutdown_started = TRUE;
  1836. nwritten = ngtcp2_conn_write_connection_close(
  1837. ctx->qconn, NULL, /* path */
  1838. NULL, /* pkt_info */
  1839. (uint8_t *)buffer, sizeof(buffer),
  1840. &ctx->last_error, pktx.ts);
  1841. CURL_TRC_CF(data, cf, "start shutdown(err_type=%d, err_code=%"
  1842. CURL_PRIu64 ") -> %d", ctx->last_error.type,
  1843. (curl_uint64_t)ctx->last_error.error_code, (int)nwritten);
  1844. if(nwritten > 0) {
  1845. Curl_bufq_write(&ctx->q.sendbuf, (const unsigned char *)buffer,
  1846. (size_t)nwritten, &result);
  1847. if(result) {
  1848. CURL_TRC_CF(data, cf, "error %d adding shutdown packets to sendbuf, "
  1849. "aborting shutdown", result);
  1850. goto out;
  1851. }
  1852. ctx->q.no_gso = TRUE;
  1853. ctx->q.gsolen = (size_t)nwritten;
  1854. ctx->q.split_len = 0;
  1855. }
  1856. }
  1857. if(!Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1858. CURL_TRC_CF(data, cf, "shutdown, flushing egress");
  1859. result = vquic_flush(cf, data, &ctx->q);
  1860. if(result == CURLE_AGAIN) {
  1861. CURL_TRC_CF(data, cf, "sending shutdown packets blocked");
  1862. result = CURLE_OK;
  1863. goto out;
  1864. }
  1865. else if(result) {
  1866. CURL_TRC_CF(data, cf, "shutdown, error %d flushing sendbuf", result);
  1867. *done = TRUE;
  1868. goto out;
  1869. }
  1870. }
  1871. if(Curl_bufq_is_empty(&ctx->q.sendbuf)) {
  1872. /* Sent everything off. ngtcp2 seems to have no support for graceful
  1873. * shutdowns. So, we are done. */
  1874. CURL_TRC_CF(data, cf, "shutdown completely sent off, done");
  1875. *done = TRUE;
  1876. result = CURLE_OK;
  1877. }
  1878. out:
  1879. CF_DATA_RESTORE(cf, save);
  1880. return result;
  1881. }
  1882. static void cf_ngtcp2_conn_close(struct Curl_cfilter *cf,
  1883. struct Curl_easy *data)
  1884. {
  1885. bool done;
  1886. cf_ngtcp2_shutdown(cf, data, &done);
  1887. }
  1888. static void cf_ngtcp2_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1889. {
  1890. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1891. struct cf_call_data save;
  1892. CF_DATA_SAVE(save, cf, data);
  1893. if(ctx && ctx->qconn) {
  1894. cf_ngtcp2_conn_close(cf, data);
  1895. cf_ngtcp2_ctx_clear(ctx);
  1896. CURL_TRC_CF(data, cf, "close");
  1897. }
  1898. cf->connected = FALSE;
  1899. CF_DATA_RESTORE(cf, save);
  1900. }
  1901. static void cf_ngtcp2_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  1902. {
  1903. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1904. struct cf_call_data save;
  1905. CF_DATA_SAVE(save, cf, data);
  1906. CURL_TRC_CF(data, cf, "destroy");
  1907. if(ctx) {
  1908. cf_ngtcp2_ctx_clear(ctx);
  1909. free(ctx);
  1910. }
  1911. cf->ctx = NULL;
  1912. /* No CF_DATA_RESTORE(cf, save) possible */
  1913. (void)save;
  1914. }
  1915. #ifdef USE_OPENSSL
  1916. /* The "new session" callback must return zero if the session can be removed
  1917. * or non-zero if the session has been put into the session cache.
  1918. */
  1919. static int quic_ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid)
  1920. {
  1921. struct Curl_cfilter *cf;
  1922. struct cf_ngtcp2_ctx *ctx;
  1923. struct Curl_easy *data;
  1924. ngtcp2_crypto_conn_ref *cref;
  1925. cref = (ngtcp2_crypto_conn_ref *)SSL_get_app_data(ssl);
  1926. cf = cref? cref->user_data : NULL;
  1927. ctx = cf? cf->ctx : NULL;
  1928. data = cf? CF_DATA_CURRENT(cf) : NULL;
  1929. if(cf && data && ctx) {
  1930. Curl_ossl_add_session(cf, data, &ctx->peer, ssl_sessionid);
  1931. return 1;
  1932. }
  1933. return 0;
  1934. }
  1935. #endif /* USE_OPENSSL */
  1936. static CURLcode tls_ctx_setup(struct Curl_cfilter *cf,
  1937. struct Curl_easy *data,
  1938. void *user_data)
  1939. {
  1940. struct curl_tls_ctx *ctx = user_data;
  1941. (void)cf;
  1942. #ifdef USE_OPENSSL
  1943. #if defined(OPENSSL_IS_BORINGSSL) || defined(OPENSSL_IS_AWSLC)
  1944. if(ngtcp2_crypto_boringssl_configure_client_context(ctx->ossl.ssl_ctx)
  1945. != 0) {
  1946. failf(data, "ngtcp2_crypto_boringssl_configure_client_context failed");
  1947. return CURLE_FAILED_INIT;
  1948. }
  1949. #else
  1950. if(ngtcp2_crypto_quictls_configure_client_context(ctx->ossl.ssl_ctx) != 0) {
  1951. failf(data, "ngtcp2_crypto_quictls_configure_client_context failed");
  1952. return CURLE_FAILED_INIT;
  1953. }
  1954. #endif /* !OPENSSL_IS_BORINGSSL && !OPENSSL_IS_AWSLC */
  1955. /* Enable the session cache because it's a prerequisite for the
  1956. * "new session" callback. Use the "external storage" mode to prevent
  1957. * OpenSSL from creating an internal session cache.
  1958. */
  1959. SSL_CTX_set_session_cache_mode(ctx->ossl.ssl_ctx,
  1960. SSL_SESS_CACHE_CLIENT |
  1961. SSL_SESS_CACHE_NO_INTERNAL);
  1962. SSL_CTX_sess_set_new_cb(ctx->ossl.ssl_ctx, quic_ossl_new_session_cb);
  1963. #elif defined(USE_GNUTLS)
  1964. if(ngtcp2_crypto_gnutls_configure_client_session(ctx->gtls.session) != 0) {
  1965. failf(data, "ngtcp2_crypto_gnutls_configure_client_session failed");
  1966. return CURLE_FAILED_INIT;
  1967. }
  1968. #elif defined(USE_WOLFSSL)
  1969. if(ngtcp2_crypto_wolfssl_configure_client_context(ctx->wssl.ctx) != 0) {
  1970. failf(data, "ngtcp2_crypto_wolfssl_configure_client_context failed");
  1971. return CURLE_FAILED_INIT;
  1972. }
  1973. #endif
  1974. return CURLE_OK;
  1975. }
  1976. /*
  1977. * Might be called twice for happy eyeballs.
  1978. */
  1979. static CURLcode cf_connect_start(struct Curl_cfilter *cf,
  1980. struct Curl_easy *data,
  1981. struct pkt_io_ctx *pktx)
  1982. {
  1983. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  1984. int rc;
  1985. int rv;
  1986. CURLcode result;
  1987. const struct Curl_sockaddr_ex *sockaddr = NULL;
  1988. int qfd;
  1989. ctx->version = NGTCP2_PROTO_VER_MAX;
  1990. ctx->max_stream_window = H3_STREAM_WINDOW_SIZE;
  1991. ctx->max_idle_ms = CURL_QUIC_MAX_IDLE_MS;
  1992. Curl_bufcp_init(&ctx->stream_bufcp, H3_STREAM_CHUNK_SIZE,
  1993. H3_STREAM_POOL_SPARES);
  1994. Curl_dyn_init(&ctx->scratch, CURL_MAX_HTTP_HEADER);
  1995. Curl_hash_offt_init(&ctx->streams, 63, h3_stream_hash_free);
  1996. result = Curl_ssl_peer_init(&ctx->peer, cf, TRNSPRT_QUIC);
  1997. if(result)
  1998. return result;
  1999. #define H3_ALPN "\x2h3\x5h3-29"
  2000. result = Curl_vquic_tls_init(&ctx->tls, cf, data, &ctx->peer,
  2001. H3_ALPN, sizeof(H3_ALPN) - 1,
  2002. tls_ctx_setup, &ctx->tls, &ctx->conn_ref);
  2003. if(result)
  2004. return result;
  2005. #ifdef USE_OPENSSL
  2006. SSL_set_quic_use_legacy_codepoint(ctx->tls.ossl.ssl, 0);
  2007. #endif
  2008. ctx->dcid.datalen = NGTCP2_MAX_CIDLEN;
  2009. result = Curl_rand(data, ctx->dcid.data, NGTCP2_MAX_CIDLEN);
  2010. if(result)
  2011. return result;
  2012. ctx->scid.datalen = NGTCP2_MAX_CIDLEN;
  2013. result = Curl_rand(data, ctx->scid.data, NGTCP2_MAX_CIDLEN);
  2014. if(result)
  2015. return result;
  2016. (void)Curl_qlogdir(data, ctx->scid.data, NGTCP2_MAX_CIDLEN, &qfd);
  2017. ctx->qlogfd = qfd; /* -1 if failure above */
  2018. quic_settings(ctx, data, pktx);
  2019. result = vquic_ctx_init(&ctx->q);
  2020. if(result)
  2021. return result;
  2022. Curl_cf_socket_peek(cf->next, data, &ctx->q.sockfd, &sockaddr, NULL);
  2023. if(!sockaddr)
  2024. return CURLE_QUIC_CONNECT_ERROR;
  2025. ctx->q.local_addrlen = sizeof(ctx->q.local_addr);
  2026. rv = getsockname(ctx->q.sockfd, (struct sockaddr *)&ctx->q.local_addr,
  2027. &ctx->q.local_addrlen);
  2028. if(rv == -1)
  2029. return CURLE_QUIC_CONNECT_ERROR;
  2030. ngtcp2_addr_init(&ctx->connected_path.local,
  2031. (struct sockaddr *)&ctx->q.local_addr,
  2032. ctx->q.local_addrlen);
  2033. ngtcp2_addr_init(&ctx->connected_path.remote,
  2034. &sockaddr->sa_addr, (socklen_t)sockaddr->addrlen);
  2035. rc = ngtcp2_conn_client_new(&ctx->qconn, &ctx->dcid, &ctx->scid,
  2036. &ctx->connected_path,
  2037. NGTCP2_PROTO_VER_V1, &ng_callbacks,
  2038. &ctx->settings, &ctx->transport_params,
  2039. NULL, cf);
  2040. if(rc)
  2041. return CURLE_QUIC_CONNECT_ERROR;
  2042. #ifdef USE_OPENSSL
  2043. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.ossl.ssl);
  2044. #elif defined(USE_GNUTLS)
  2045. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.gtls.session);
  2046. #else
  2047. ngtcp2_conn_set_tls_native_handle(ctx->qconn, ctx->tls.wssl.handle);
  2048. #endif
  2049. ngtcp2_ccerr_default(&ctx->last_error);
  2050. ctx->conn_ref.get_conn = get_conn;
  2051. ctx->conn_ref.user_data = cf;
  2052. return CURLE_OK;
  2053. }
  2054. static CURLcode cf_ngtcp2_connect(struct Curl_cfilter *cf,
  2055. struct Curl_easy *data,
  2056. bool blocking, bool *done)
  2057. {
  2058. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2059. CURLcode result = CURLE_OK;
  2060. struct cf_call_data save;
  2061. struct curltime now;
  2062. struct pkt_io_ctx pktx;
  2063. if(cf->connected) {
  2064. *done = TRUE;
  2065. return CURLE_OK;
  2066. }
  2067. /* Connect the UDP filter first */
  2068. if(!cf->next->connected) {
  2069. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  2070. if(result || !*done)
  2071. return result;
  2072. }
  2073. *done = FALSE;
  2074. now = Curl_now();
  2075. pktx_init(&pktx, cf, data);
  2076. CF_DATA_SAVE(save, cf, data);
  2077. if(ctx->reconnect_at.tv_sec && Curl_timediff(now, ctx->reconnect_at) < 0) {
  2078. /* Not time yet to attempt the next connect */
  2079. CURL_TRC_CF(data, cf, "waiting for reconnect time");
  2080. goto out;
  2081. }
  2082. if(!ctx->qconn) {
  2083. ctx->started_at = now;
  2084. result = cf_connect_start(cf, data, &pktx);
  2085. if(result)
  2086. goto out;
  2087. result = cf_progress_egress(cf, data, &pktx);
  2088. /* we do not expect to be able to recv anything yet */
  2089. goto out;
  2090. }
  2091. result = cf_progress_ingress(cf, data, &pktx);
  2092. if(result)
  2093. goto out;
  2094. result = cf_progress_egress(cf, data, &pktx);
  2095. if(result)
  2096. goto out;
  2097. if(ngtcp2_conn_get_handshake_completed(ctx->qconn)) {
  2098. ctx->handshake_at = now;
  2099. CURL_TRC_CF(data, cf, "handshake complete after %dms",
  2100. (int)Curl_timediff(now, ctx->started_at));
  2101. result = qng_verify_peer(cf, data);
  2102. if(!result) {
  2103. CURL_TRC_CF(data, cf, "peer verified");
  2104. cf->connected = TRUE;
  2105. cf->conn->alpn = CURL_HTTP_VERSION_3;
  2106. *done = TRUE;
  2107. connkeep(cf->conn, "HTTP/3 default");
  2108. }
  2109. }
  2110. out:
  2111. if(result == CURLE_RECV_ERROR && ctx->qconn &&
  2112. ngtcp2_conn_in_draining_period(ctx->qconn)) {
  2113. /* When a QUIC server instance is shutting down, it may send us a
  2114. * CONNECTION_CLOSE right away. Our connection then enters the DRAINING
  2115. * state. The CONNECT may work in the near future again. Indicate
  2116. * that as a "weird" reply. */
  2117. result = CURLE_WEIRD_SERVER_REPLY;
  2118. }
  2119. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2120. if(result) {
  2121. struct ip_quadruple ip;
  2122. Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
  2123. infof(data, "QUIC connect to %s port %u failed: %s",
  2124. ip.remote_ip, ip.remote_port, curl_easy_strerror(result));
  2125. }
  2126. #endif
  2127. if(!result && ctx->qconn) {
  2128. result = check_and_set_expiry(cf, data, &pktx);
  2129. }
  2130. if(result || *done)
  2131. CURL_TRC_CF(data, cf, "connect -> %d, done=%d", result, *done);
  2132. CF_DATA_RESTORE(cf, save);
  2133. return result;
  2134. }
  2135. static CURLcode cf_ngtcp2_query(struct Curl_cfilter *cf,
  2136. struct Curl_easy *data,
  2137. int query, int *pres1, void *pres2)
  2138. {
  2139. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2140. struct cf_call_data save;
  2141. switch(query) {
  2142. case CF_QUERY_MAX_CONCURRENT: {
  2143. DEBUGASSERT(pres1);
  2144. CF_DATA_SAVE(save, cf, data);
  2145. /* Set after transport params arrived and continually updated
  2146. * by callback. QUIC counts the number over the lifetime of the
  2147. * connection, ever increasing.
  2148. * We count the *open* transfers plus the budget for new ones. */
  2149. if(!ctx->qconn || ctx->shutdown_started) {
  2150. *pres1 = 0;
  2151. }
  2152. else if(ctx->max_bidi_streams) {
  2153. uint64_t avail_bidi_streams = 0;
  2154. uint64_t max_streams = CONN_INUSE(cf->conn);
  2155. if(ctx->max_bidi_streams > ctx->used_bidi_streams)
  2156. avail_bidi_streams = ctx->max_bidi_streams - ctx->used_bidi_streams;
  2157. max_streams += avail_bidi_streams;
  2158. *pres1 = (max_streams > INT_MAX)? INT_MAX : (int)max_streams;
  2159. }
  2160. else /* transport params not arrived yet? take our default. */
  2161. *pres1 = (int)Curl_multi_max_concurrent_streams(data->multi);
  2162. CURL_TRC_CF(data, cf, "query conn[%" CURL_FORMAT_CURL_OFF_T "]: "
  2163. "MAX_CONCURRENT -> %d (%zu in use)",
  2164. cf->conn->connection_id, *pres1, CONN_INUSE(cf->conn));
  2165. CF_DATA_RESTORE(cf, save);
  2166. return CURLE_OK;
  2167. }
  2168. case CF_QUERY_CONNECT_REPLY_MS:
  2169. if(ctx->q.got_first_byte) {
  2170. timediff_t ms = Curl_timediff(ctx->q.first_byte_at, ctx->started_at);
  2171. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  2172. }
  2173. else
  2174. *pres1 = -1;
  2175. return CURLE_OK;
  2176. case CF_QUERY_TIMER_CONNECT: {
  2177. struct curltime *when = pres2;
  2178. if(ctx->q.got_first_byte)
  2179. *when = ctx->q.first_byte_at;
  2180. return CURLE_OK;
  2181. }
  2182. case CF_QUERY_TIMER_APPCONNECT: {
  2183. struct curltime *when = pres2;
  2184. if(cf->connected)
  2185. *when = ctx->handshake_at;
  2186. return CURLE_OK;
  2187. }
  2188. default:
  2189. break;
  2190. }
  2191. return cf->next?
  2192. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  2193. CURLE_UNKNOWN_OPTION;
  2194. }
  2195. static bool cf_ngtcp2_conn_is_alive(struct Curl_cfilter *cf,
  2196. struct Curl_easy *data,
  2197. bool *input_pending)
  2198. {
  2199. struct cf_ngtcp2_ctx *ctx = cf->ctx;
  2200. bool alive = FALSE;
  2201. const ngtcp2_transport_params *rp;
  2202. struct cf_call_data save;
  2203. CF_DATA_SAVE(save, cf, data);
  2204. *input_pending = FALSE;
  2205. if(!ctx->qconn || ctx->shutdown_started)
  2206. goto out;
  2207. /* Both sides of the QUIC connection announce they max idle times in
  2208. * the transport parameters. Look at the minimum of both and if
  2209. * we exceed this, regard the connection as dead. The other side
  2210. * may have completely purged it and will no longer respond
  2211. * to any packets from us. */
  2212. rp = ngtcp2_conn_get_remote_transport_params(ctx->qconn);
  2213. if(rp) {
  2214. timediff_t idletime;
  2215. uint64_t idle_ms = ctx->max_idle_ms;
  2216. if(rp->max_idle_timeout &&
  2217. (rp->max_idle_timeout / NGTCP2_MILLISECONDS) < idle_ms)
  2218. idle_ms = (rp->max_idle_timeout / NGTCP2_MILLISECONDS);
  2219. idletime = Curl_timediff(Curl_now(), ctx->q.last_io);
  2220. if(idletime > 0 && (uint64_t)idletime > idle_ms)
  2221. goto out;
  2222. }
  2223. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  2224. goto out;
  2225. alive = TRUE;
  2226. if(*input_pending) {
  2227. CURLcode result;
  2228. /* This happens before we've sent off a request and the connection is
  2229. not in use by any other transfer, there shouldn't be any data here,
  2230. only "protocol frames" */
  2231. *input_pending = FALSE;
  2232. result = cf_progress_ingress(cf, data, NULL);
  2233. CURL_TRC_CF(data, cf, "is_alive, progress ingress -> %d", result);
  2234. alive = result? FALSE : TRUE;
  2235. }
  2236. out:
  2237. CF_DATA_RESTORE(cf, save);
  2238. return alive;
  2239. }
  2240. struct Curl_cftype Curl_cft_http3 = {
  2241. "HTTP/3",
  2242. CF_TYPE_IP_CONNECT | CF_TYPE_SSL | CF_TYPE_MULTIPLEX,
  2243. 0,
  2244. cf_ngtcp2_destroy,
  2245. cf_ngtcp2_connect,
  2246. cf_ngtcp2_close,
  2247. cf_ngtcp2_shutdown,
  2248. Curl_cf_def_get_host,
  2249. cf_ngtcp2_adjust_pollset,
  2250. cf_ngtcp2_data_pending,
  2251. cf_ngtcp2_send,
  2252. cf_ngtcp2_recv,
  2253. cf_ngtcp2_data_event,
  2254. cf_ngtcp2_conn_is_alive,
  2255. Curl_cf_def_conn_keep_alive,
  2256. cf_ngtcp2_query,
  2257. };
  2258. CURLcode Curl_cf_ngtcp2_create(struct Curl_cfilter **pcf,
  2259. struct Curl_easy *data,
  2260. struct connectdata *conn,
  2261. const struct Curl_addrinfo *ai)
  2262. {
  2263. struct cf_ngtcp2_ctx *ctx = NULL;
  2264. struct Curl_cfilter *cf = NULL, *udp_cf = NULL;
  2265. CURLcode result;
  2266. (void)data;
  2267. ctx = calloc(1, sizeof(*ctx));
  2268. if(!ctx) {
  2269. result = CURLE_OUT_OF_MEMORY;
  2270. goto out;
  2271. }
  2272. ctx->qlogfd = -1;
  2273. cf_ngtcp2_ctx_clear(ctx);
  2274. result = Curl_cf_create(&cf, &Curl_cft_http3, ctx);
  2275. if(result)
  2276. goto out;
  2277. result = Curl_cf_udp_create(&udp_cf, data, conn, ai, TRNSPRT_QUIC);
  2278. if(result)
  2279. goto out;
  2280. cf->conn = conn;
  2281. udp_cf->conn = cf->conn;
  2282. udp_cf->sockindex = cf->sockindex;
  2283. cf->next = udp_cf;
  2284. out:
  2285. *pcf = (!result)? cf : NULL;
  2286. if(result) {
  2287. if(udp_cf)
  2288. Curl_conn_cf_discard_sub(cf, udp_cf, data, TRUE);
  2289. Curl_safefree(cf);
  2290. Curl_safefree(ctx);
  2291. }
  2292. return result;
  2293. }
  2294. bool Curl_conn_is_ngtcp2(const struct Curl_easy *data,
  2295. const struct connectdata *conn,
  2296. int sockindex)
  2297. {
  2298. struct Curl_cfilter *cf = conn? conn->cfilter[sockindex] : NULL;
  2299. (void)data;
  2300. for(; cf; cf = cf->next) {
  2301. if(cf->cft == &Curl_cft_http3)
  2302. return TRUE;
  2303. if(cf->cft->flags & CF_TYPE_IP_CONNECT)
  2304. return FALSE;
  2305. }
  2306. return FALSE;
  2307. }
  2308. #endif