curl_ngtcp2.c 67 KB

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