curl_ngtcp2.c 76 KB

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