include/fetch/ft_h3.h

filter: Statementstmt 9.0% 1 / 11 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 7.6% 1 / 13 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/fetch/ft_hm.h ↑ Summary include/fetch/ft_altsvc.h »
1 #ifndef FT_H3_H
2 #define FT_H3_H
3  
4 /* ft_h3.h - the HTTP/3 client, beside ft_h2.h.
5 *
6 * The h2 glue is 250 lines because nghttp2 does the work. This is longer for
7 * one structural reason: nghttp2 PUSHES bytes at a send callback, while
8 * nghttp3 is PULLED from, and the puller is ngtcp2, which also owns the loss
9 * detection, the packet framing and the handshake. Two libraries stacked
10 * where h2 has one, and the write half is a pump rather than a callback.
11 *
12 * ---- how a request flows --------------------------------------------------
13 *
14 * ft_h3_connect UDP socket, ngtcp2_conn_client_new, an SSL handed to
15 * ngtcp2_crypto_ossl, ALPN "h3", SNI. The state becomes
16 * FT_QUIC_HANDSHAKING.
17 * ft_h3_step one readiness pass: drain datagrams into
18 * ngtcp2_conn_read_pkt, then pump ft_h3_write until
19 * ngtcp2 has nothing left, then rearm the probe timeout.
20 * handshake_completed nghttp3_conn_client_new, the three unidirectional
21 * streams QUIC requires (control, QPACK encoder, QPACK
22 * decoder), then every request waiting on this
23 * connection is submitted.
24 * ft_h3_write nghttp3_conn_writev_stream -> ngtcp2_conn_writev_stream
25 * -> sendto, in one loop, because ngtcp2 packs several
26 * stream writes into one datagram and only it knows when
27 * one is full.
28 *
29 * ---- multiplexing, which h2 here does not do ------------------------------
30 *
31 * Fetch does ONE request per HTTP/2 connection (ft_conn_reusable returns 0
32 * for is_h2). That is not inherited: an h3 connection carrying one request
33 * paid for a QUIC handshake to get less than keep-alive gives. So a request
34 * is an ft_h3_stream and NOT an ft_conn - the connection holds a list of
35 * them, each with its own future, headers and body, and each settles when
36 * its own stream ends. The connection outlives all of them and is parked in
37 * the pool under the "h3:" key that ft_http.h's pool gained for this.
38 *
39 * ---- what is deliberately not here ---------------------------------------
40 *
41 * Connection migration, 0-RTT, ECN, GSO, and the datagram extension. Each is
42 * an optimisation or a feature on top of a connection that works; none of
43 * them changes the shape above. Alt-Svc discovery is in ft_altsvc.h, because
44 * it is HTTP/1 and HTTP/2 response parsing and has nothing to do with QUIC.
45 *
46 * Included from ft_http.h after ft_conn, ft_arm, ft_settle_response and
47 * ft_conn_fail. Compiles to stubs without -DFT_HAVE_QUIC.
48 */
49  
50 #ifdef FT_HAVE_QUIC
51  
52 #include <ngtcp2/ngtcp2.h>
53 #include <ngtcp2/ngtcp2_crypto.h>
54 #include <ngtcp2/ngtcp2_crypto_ossl.h>
55 #include <nghttp3/nghttp3.h>
56 #include <openssl/ssl.h>
57 #include <openssl/rand.h>
58  
59 #define FT_H3_AVAILABLE 1
60 #define FT_H3_CIDLEN 16
61 #define FT_H3_MTU 1452 /* a conservative path MTU */
62  
63 /* One request on a QUIC connection.
64 *
65 * Everything here is per-REQUEST and is what an ft_conn holds directly on the
66 * other transports. It is split out because an h3 connection carries several
67 * at once, which is the whole point of using it. */
68 typedef struct ft_h3_stream {
69 int64_t id; /* -1 until submitted */
70 SV *future;
71 SV *on_body, *on_headers;
72 int headers_fired;
73 int status;
74 AV *headers, *trailers;
75 char *dbody; /* response body, unless on_body streams it */
76 size_t dblen, dbcap;
77 size_t body_recv;
78 /* the request, kept until submitted and while the body is being read */
79 SV *rq_method, *rq_scheme, *rq_authority, *rq_path;
80 AV *rq_headers;
81 SV *rq_body;
82 size_t rq_body_off;
83 int simple;
84 int submitted;
85 struct ft_h3_stream *next;
86 } ft_h3_stream;
87  
88 /* The QUIC half of an ft_conn. Hung off c->h3 so ft_conn needs no new
89 * members: everything QUIC is behind this one pointer. */
90 typedef struct ft_qconn {
91 ft_conn *c; /* the owning connection, for callbacks */
92 ngtcp2_conn *conn;
93 nghttp3_conn *h3;
94 SSL *ssl;
95 ngtcp2_crypto_ossl_ctx *ossl;
96 ngtcp2_crypto_conn_ref conn_ref;
97 ngtcp2_path_storage path;
98 struct sockaddr_storage peer;
99 socklen_t peerlen;
100 ft_h3_stream *streams;
101 int handshake_done;
102 int closing; /* a fatal error is being reported */
103 int nstreams; /* live streams, for pooling decisions */
104 } ft_qconn;
105  
106 static int ft_h3_write(pTHX_ ft_conn *c);
107 static int ft_h3_submit(pTHX_ ft_conn *c, ft_h3_stream *st);
108 static void ft_h3_rearm(pTHX_ ft_conn *c);
109  
110 /* The QUIC TLS context, separate from ft_tls.h's.
111 *
112 * QUIC is TLS 1.3 only - there is no QUIC over 1.2 - so the floor is raised
113 * rather than inherited. Sharing ft_client_ctx would also mean sharing its
114 * ALPN and its session cache with connections that are not QUIC at all, and
115 * an SSL_CTX is cheap to have twice. */
116 static SSL_CTX *ft_h3_ctx(int verify) {
117 static SSL_CTX *ctx_v = NULL, *ctx_n = NULL;
118 static int inited = 0;
119 SSL_CTX **slot = verify ? &ctx_v : &ctx_n;
120 if (!inited) {
121 /* Optional, and recommended by the library: without it the crypto
122 * shim takes a slow path on every packet. Failing is not fatal. */
123 (void)ngtcp2_crypto_ossl_init();
124 inited = 1;
125 }
126 if (!*slot) {
127 SSL_CTX *x = SSL_CTX_new(TLS_client_method());
128 if (!x) return NULL;
129 SSL_CTX_set_min_proto_version(x, TLS1_3_VERSION);
130 SSL_CTX_set_max_proto_version(x, TLS1_3_VERSION);
131 if (verify) {
132 SSL_CTX_set_verify(x, SSL_VERIFY_PEER, NULL);
133 SSL_CTX_set_default_verify_paths(x);
134 } else {
135 SSL_CTX_set_verify(x, SSL_VERIFY_NONE, NULL);
136 }
137 *slot = x;
138 }
139 return *slot;
140 }
141  
142 /* ---- clock ---------------------------------------------------------------
143 *
144 * ngtcp2 measures everything in monotonic nanoseconds and compares the values
145 * it is given against the ones it stored, so this must be the same clock
146 * every time it is asked - never a wall clock, which steps. */
147 static ngtcp2_tstamp ft_h3_now(void) {
148 struct timespec ts;
149 clock_gettime(CLOCK_MONOTONIC, &ts);
150 return (ngtcp2_tstamp)ts.tv_sec * NGTCP2_SECONDS + (ngtcp2_tstamp)ts.tv_nsec;
151 }
152  
153 /* ---- streams ------------------------------------------------------------- */
154  
155 static ft_h3_stream *ft_h3_stream_new(pTHX) {
156 ft_h3_stream *st;
157 Newxz(st, 1, ft_h3_stream);
158 st->id = -1;
159 return st;
160 }
161  
162 static void ft_h3_stream_free(pTHX_ ft_h3_stream *st) {
163 if (!st) return;
164 if (st->future) SvREFCNT_dec(st->future);
165 if (st->on_body) SvREFCNT_dec(st->on_body);
166 if (st->on_headers) SvREFCNT_dec(st->on_headers);
167 if (st->headers) SvREFCNT_dec((SV *)st->headers);
168 if (st->trailers) SvREFCNT_dec((SV *)st->trailers);
169 if (st->rq_method) SvREFCNT_dec(st->rq_method);
170 if (st->rq_scheme) SvREFCNT_dec(st->rq_scheme);
171 if (st->rq_authority) SvREFCNT_dec(st->rq_authority);
172 if (st->rq_path) SvREFCNT_dec(st->rq_path);
173 if (st->rq_headers) SvREFCNT_dec((SV *)st->rq_headers);
174 if (st->rq_body) SvREFCNT_dec(st->rq_body);
175 Safefree(st->dbody);
176 Safefree(st);
177 }
178  
179 static ft_h3_stream *ft_h3_find(ft_qconn *q, int64_t id) {
180 ft_h3_stream *st;
181 for (st = q->streams; st; st = st->next) if (st->id == id) return st;
182 return NULL;
183 }
184  
185 static void ft_h3_unlink(ft_qconn *q, ft_h3_stream *st) {
186 ft_h3_stream **pp = &q->streams;
187 while (*pp) {
188 if (*pp == st) { *pp = st->next; q->nstreams--; return; }
189 pp = &(*pp)->next;
190 }
191 }
192  
193 /* One finished request. The CONNECTION is untouched: its other streams are
194 * still running and it stays poolable afterwards, which is the difference
195 * from every other transport here.
196 *
197 * The future is settled here and the STRUCT IS NOT FREED. nghttp3 calls
198 * end_stream and then stream_close, and both are handed the stream pointer as
199 * user data - so freeing on the first is a use-after-free on the second. The
200 * later of the two owns the memory; this one only owns the answer. */
201 static void ft_h3_stream_done(pTHX_ ft_qconn *q, ft_h3_stream *st) {
202 (void)q;
203 if (!st->future) return; /* already settled */
204 ft_settle_response(aTHX_ st->future, st->status, st->headers, st->trailers,
205 st->dbody, st->dblen, st->simple);
206 SvREFCNT_dec(st->future);
207 st->future = NULL;
208 }
209  
210 /* Fail every outstanding request on this connection with one message. A QUIC
211 * connection dying takes all of its streams with it, and each is somebody's
212 * pending future - leaving any of them unsettled is a caller that waits for
213 * ever, which is worse than the error. */
214 static void ft_h3_fail_all(pTHX_ ft_conn *c, const char *msg) {
215 ft_qconn *q = (ft_qconn *)c->h3;
216 SV *e;
217 if (!q || q->closing) return;
218 q->closing = 1;
219 e = sv_2mortal(newSVpvf("Fetch: %s", msg));
220 while (q->streams) {
221 ft_h3_stream *st = q->streams;
222 q->streams = st->next;
223 q->nstreams--;
224 if (st->future && hmf_state(aTHX_ st->future) == HMF_PENDING)
225 hmf_settle(aTHX_ st->future, HMF_FAILED, &e, 1);
226 ft_h3_stream_free(aTHX_ st);
227 }
228 q->closing = 0;
229 ft_conn_fail(aTHX_ c, msg);
230 }
231  
232 /* ---- nghttp3 callbacks --------------------------------------------------- */
233  
234 static int ft_h3_cb_begin_headers(nghttp3_conn *conn, int64_t sid,
235 void *user, void *stream_user) {
236 (void)conn; (void)sid; (void)user; (void)stream_user;
237 return 0;
238 }
239  
240 static int ft_h3_cb_recv_header(nghttp3_conn *conn, int64_t sid, int32_t token,
241 nghttp3_rcbuf *name, nghttp3_rcbuf *value,
242 uint8_t flags, void *user, void *stream_user) {
243 ft_qconn *q = (ft_qconn *)user;
244 ft_h3_stream *st = (ft_h3_stream *)stream_user;
245 nghttp3_vec n = nghttp3_rcbuf_get_buf(name);
246 nghttp3_vec v = nghttp3_rcbuf_get_buf(value);
247 dTHX;
248 (void)conn; (void)flags;
249 if (!st) st = ft_h3_find(q, sid);
250 if (!st) return 0;
251 if (token == NGHTTP3_QPACK_TOKEN__STATUS) {
252 st->status = (int)strtol((const char *)v.base, NULL, 10);
253 return 0;
254 }
255 if (n.len && n.base[0] == ':') return 0; /* other pseudo-headers */
256 if (!st->headers) st->headers = newAV();
257 av_push(st->headers, newSVpvn((const char *)n.base, n.len));
258 av_push(st->headers, newSVpvn((const char *)v.base, v.len));
259 return 0;
260 }
261  
262 /* Trailers: a second header block, after the body. Kept apart from the
263 * response headers for the reason ft_h2.h spells out - gRPC puts the call
264 * status in them and nowhere else, and a consumer must be able to tell a
265 * trailer from a header. */
266 static int ft_h3_cb_recv_trailer(nghttp3_conn *conn, int64_t sid, int32_t token,
267 nghttp3_rcbuf *name, nghttp3_rcbuf *value,
268 uint8_t flags, void *user, void *stream_user) {
269 ft_qconn *q = (ft_qconn *)user;
270 ft_h3_stream *st = (ft_h3_stream *)stream_user;
271 nghttp3_vec n = nghttp3_rcbuf_get_buf(name);
272 nghttp3_vec v = nghttp3_rcbuf_get_buf(value);
273 dTHX;
274 (void)conn; (void)flags; (void)token;
275 if (!st) st = ft_h3_find(q, sid);
276 if (!st || (n.len && n.base[0] == ':')) return 0;
277 if (!st->trailers) st->trailers = newAV();
278 av_push(st->trailers, newSVpvn((const char *)n.base, n.len));
279 av_push(st->trailers, newSVpvn((const char *)v.base, v.len));
280 return 0;
281 }
282  
283 /* The response head is complete. This is where on_headers fires, before any
284 * body, exactly as the other two transports do it. */
285 static int ft_h3_cb_end_headers(nghttp3_conn *conn, int64_t sid, int fin,
286 void *user, void *stream_user) {
287 ft_qconn *q = (ft_qconn *)user;
288 ft_h3_stream *st = (ft_h3_stream *)stream_user;
289 dTHX;
290 (void)conn; (void)fin;
291 if (!st) st = ft_h3_find(q, sid);
292 if (!st || st->headers_fired || !st->on_headers) return 0;
293 st->headers_fired = 1;
294 {
295 dSP;
296 SV *hrv = st->headers ? newRV_inc((SV *)st->headers)
297 : newRV_noinc((SV *)newAV());
298 ENTER; SAVETMPS;
299 PUSHMARK(SP);
300 EXTEND(SP, 2);
301 PUSHs(sv_2mortal(newSViv(st->status)));
302 PUSHs(sv_2mortal(hrv));
303 PUTBACK;
304 call_sv(st->on_headers, G_DISCARD | G_EVAL);
305 if (SvTRUE(ERRSV))
306 warn("Fetch: on_headers callback died: %s", SvPV_nolen(ERRSV));
307 FREETMPS; LEAVE;
308 }
309 return 0;
310 }
311  
312 static int ft_h3_cb_recv_data(nghttp3_conn *conn, int64_t sid,
313 const uint8_t *data, size_t datalen,
314 void *user, void *stream_user) {
315 ft_qconn *q = (ft_qconn *)user;
316 ft_h3_stream *st = (ft_h3_stream *)stream_user;
317 dTHX;
318 (void)conn;
319 if (!st) st = ft_h3_find(q, sid);
320 if (!st) return 0;
321 /* The flow-control window is credited here and in deferred_consume, and
322 * NOT also in ngtcp2's recv_stream_data - crediting both opens the window
323 * twice and lets the peer send past what was advertised. */
324 ngtcp2_conn_extend_max_stream_offset(q->conn, sid, datalen);
325 ngtcp2_conn_extend_max_offset(q->conn, datalen);
326 if (st->on_body) { /* stream: emit, do not buffer */
327 ft_emit_body_sv(aTHX_ st->on_body, (const char *)data, datalen);
328 st->body_recv += datalen;
329 return 0;
330 }
331 if (st->dblen + datalen > st->dbcap) {
332 st->dbcap = (st->dblen + datalen) * 2 + 64;
333 Renew(st->dbody, st->dbcap, char);
334 }
335 memcpy(st->dbody + st->dblen, data, datalen);
336 st->dblen += datalen;
337 return 0;
338 }
339  
340 /* Bytes nghttp3 skipped over (frame headers, QPACK instructions). They came
341 * out of the same window and have to be credited back too, or the peer runs
342 * out of room by exactly the framing overhead and the transfer stalls. */
343 static int ft_h3_cb_deferred_consume(nghttp3_conn *conn, int64_t sid,
344 size_t consumed, void *user,
345 void *stream_user) {
346 ft_qconn *q = (ft_qconn *)user;
347 (void)conn; (void)stream_user;
348 ngtcp2_conn_extend_max_stream_offset(q->conn, sid, consumed);
349 ngtcp2_conn_extend_max_offset(q->conn, consumed);
350 return 0;
351 }
352  
353 static int ft_h3_cb_end_stream(nghttp3_conn *conn, int64_t sid,
354 void *user, void *stream_user) {
355 ft_qconn *q = (ft_qconn *)user;
356 ft_h3_stream *st = (ft_h3_stream *)stream_user;
357 dTHX;
358 (void)conn;
359 if (!st) st = ft_h3_find(q, sid);
360 if (st) ft_h3_stream_done(aTHX_ q, st);
361 return 0;
362 }
363  
364 static int ft_h3_cb_stream_close(nghttp3_conn *conn, int64_t sid,
365 uint64_t app_error_code, void *user,
366 void *stream_user) {
367 ft_qconn *q = (ft_qconn *)user;
368 ft_h3_stream *st = (ft_h3_stream *)stream_user;
369 dTHX;
370 if (!st) st = ft_h3_find(q, sid);
371 if (!st) return 0;
372 /* Closed with the response already delivered is the ordinary path and
373 * end_stream has settled it. Closed while a future is still pending is a
374 * reset: the caller gets an error rather than a truncated 200. */
375 if (st->future) {
376 SV *e = sv_2mortal(newSVpvf("Fetch: http/3 stream reset (0x%" UVxf ")",
377 (UV)app_error_code));
378 if (hmf_state(aTHX_ st->future) == HMF_PENDING)
379 hmf_settle(aTHX_ st->future, HMF_FAILED, &e, 1);
380 SvREFCNT_dec(st->future);
381 st->future = NULL;
382 }
383 /* The last callback this stream gets, so it owns the memory. Clearing
384 * the user data first is what stops a later ngtcp2 callback naming an
385 * address this connection has since handed to something else. */
386 nghttp3_conn_set_stream_user_data(conn, sid, NULL);
387 ft_h3_unlink(q, st);
388 ft_h3_stream_free(aTHX_ st);
389 return 0;
390 }
391  
392 static int ft_h3_cb_stop_sending(nghttp3_conn *conn, int64_t sid,
393 uint64_t app_error_code, void *user,
394 void *stream_user) {
395 ft_qconn *q = (ft_qconn *)user;
396 (void)conn; (void)stream_user;
397 ngtcp2_conn_shutdown_stream_read(q->conn, 0, sid, app_error_code);
398 return 0;
399 }
400  
401 static int ft_h3_cb_reset_stream(nghttp3_conn *conn, int64_t sid,
402 uint64_t app_error_code, void *user,
403 void *stream_user) {
404 ft_qconn *q = (ft_qconn *)user;
405 (void)conn; (void)stream_user;
406 ngtcp2_conn_shutdown_stream_write(q->conn, 0, sid, app_error_code);
407 return 0;
408 }
409  
410 static void ft_h3_cb_rand(uint8_t *dest, size_t destlen) {
411 ft_os_random(dest, destlen);
412 }
413  
414 /* The request body, pulled rather than pushed. nghttp3 keeps the POINTER, so
415 * the SV must stay alive until the bytes are acknowledged - which is what
416 * holding rq_body on the stream is for. */
417 static nghttp3_ssize ft_h3_read_body(nghttp3_conn *conn, int64_t sid,
418 nghttp3_vec *vec, size_t veccnt,
419 uint32_t *pflags, void *user,
420 void *stream_user) {
421 ft_h3_stream *st = (ft_h3_stream *)stream_user;
422 ft_qconn *q = (ft_qconn *)user;
423 STRLEN blen = 0;
424 const char *b;
425 dTHX;
426 (void)conn; (void)veccnt;
427 if (!st) st = ft_h3_find(q, sid);
428 if (!st || !st->rq_body) { *pflags |= NGHTTP3_DATA_FLAG_EOF; return 0; }
429 b = SvPV(st->rq_body, blen);
430 if (st->rq_body_off >= blen) { *pflags |= NGHTTP3_DATA_FLAG_EOF; return 0; }
431 vec[0].base = (uint8_t *)b + st->rq_body_off;
432 vec[0].len = blen - st->rq_body_off;
433 st->rq_body_off = blen;
434 *pflags |= NGHTTP3_DATA_FLAG_EOF;
435 return 1;
436 }
437  
438 /* ---- ngtcp2 callbacks ---------------------------------------------------- */
439  
440 static ngtcp2_conn *ft_h3_get_conn(ngtcp2_crypto_conn_ref *ref) {
441 return ((ft_qconn *)ref->user_data)->conn;
442 }
443  
444 static void ft_h3_cb_qrand(uint8_t *dest, size_t destlen,
445 const ngtcp2_rand_ctx *ctx) {
446 (void)ctx;
447 ft_os_random(dest, destlen);
448 }
449  
450 static int ft_h3_cb_get_new_cid(ngtcp2_conn *conn, ngtcp2_cid *cid,
451 uint8_t *token, size_t cidlen, void *user) {
452 (void)conn; (void)user;
453 ft_os_random(cid->data, cidlen);
454 cid->datalen = cidlen;
455 ft_os_random(token, NGTCP2_STATELESS_RESET_TOKENLEN);
456 return 0;
457 }
458  
459 static int ft_h3_cb_recv_stream_data(ngtcp2_conn *conn, uint32_t flags,
460 int64_t sid, uint64_t offset,
461 const uint8_t *data, size_t datalen,
462 void *user, void *stream_user) {
463 ft_qconn *q = (ft_qconn *)user;
464 nghttp3_ssize n;
465 dTHX;
466 (void)conn; (void)offset; (void)stream_user;
467 if (!q->h3) return 0;
468 n = nghttp3_conn_read_stream(q->h3, sid, data, datalen,
469 (flags & NGTCP2_STREAM_DATA_FLAG_FIN) ? 1 : 0);
470 if (n < 0) return NGTCP2_ERR_CALLBACK_FAILURE;
471 return 0;
472 }
473  
474 static int ft_h3_cb_qstream_close(ngtcp2_conn *conn, uint32_t flags,
475 int64_t sid, uint64_t app_error_code,
476 void *user, void *stream_user) {
477 ft_qconn *q = (ft_qconn *)user;
478 (void)conn; (void)stream_user;
479 if (!(flags & NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET))
480 app_error_code = NGHTTP3_H3_NO_ERROR;
481 if (q->h3 && nghttp3_conn_close_stream(q->h3, sid, app_error_code) != 0)
482 return NGTCP2_ERR_CALLBACK_FAILURE;
483 return 0;
484 }
485  
486 static int ft_h3_cb_acked_stream_data(ngtcp2_conn *conn, int64_t sid,
487 uint64_t offset, uint64_t datalen,
488 void *user, void *stream_user) {
489 ft_qconn *q = (ft_qconn *)user;
490 (void)conn; (void)offset; (void)stream_user;
491 if (q->h3 && nghttp3_conn_add_ack_offset(q->h3, sid, datalen) != 0)
492 return NGTCP2_ERR_CALLBACK_FAILURE;
493 return 0;
494 }
495  
496 /* The peer opened a stream's send window again. Without telling nghttp3, a
497 * stream it stopped offering when the window closed is never offered again
498 * and the request simply stops - which only shows up under concurrency. */
499 static int ft_h3_cb_extend_max_stream_data(ngtcp2_conn *conn, int64_t sid,
500 uint64_t max_data, void *user,
501 void *stream_user) {
502 ft_qconn *q = (ft_qconn *)user;
503 (void)conn; (void)max_data; (void)stream_user;
504 if (q->h3 && nghttp3_conn_unblock_stream(q->h3, sid) != 0)
505 return NGTCP2_ERR_CALLBACK_FAILURE;
506 return 0;
507 }
508  
509 /* The TLS handshake finished, so HTTP/3 can start.
510 *
511 * The three unidirectional streams are not optional and not an optimisation:
512 * RFC 9114 requires a control stream and the two QPACK streams before any
513 * request, and a peer that never sees them closes the connection. */
514 static int ft_h3_cb_handshake_completed(ngtcp2_conn *conn, void *user) {
515 ft_qconn *q = (ft_qconn *)user;
516 nghttp3_callbacks cbs;
517 nghttp3_settings settings;
518 int64_t ctrl = -1, qenc = -1, qdec = -1;
519 ft_h3_stream *st;
520 dTHX;
521 (void)conn;
522  
523 memset(&cbs, 0, sizeof cbs);
524 cbs.begin_headers = ft_h3_cb_begin_headers;
525 cbs.recv_header = ft_h3_cb_recv_header;
526 cbs.end_headers = ft_h3_cb_end_headers;
527 cbs.recv_trailer = ft_h3_cb_recv_trailer;
528 cbs.recv_data = ft_h3_cb_recv_data;
529 cbs.deferred_consume = ft_h3_cb_deferred_consume;
530 cbs.end_stream = ft_h3_cb_end_stream;
531 cbs.stream_close = ft_h3_cb_stream_close;
532 cbs.stop_sending = ft_h3_cb_stop_sending;
533 cbs.reset_stream = ft_h3_cb_reset_stream;
534 cbs.rand = ft_h3_cb_rand;
535  
536 nghttp3_settings_default(&settings);
537 settings.qpack_max_dtable_capacity = 4096;
538 settings.qpack_blocked_streams = 100;
539  
540 if (nghttp3_conn_client_new(&q->h3, &cbs, &settings, NULL, q) != 0)
541 return NGTCP2_ERR_CALLBACK_FAILURE;
542 if (ngtcp2_conn_open_uni_stream(q->conn, &ctrl, NULL) != 0
543 || nghttp3_conn_bind_control_stream(q->h3, ctrl) != 0
544 || ngtcp2_conn_open_uni_stream(q->conn, &qenc, NULL) != 0
545 || ngtcp2_conn_open_uni_stream(q->conn, &qdec, NULL) != 0
546 || nghttp3_conn_bind_qpack_streams(q->h3, qenc, qdec) != 0)
547 return NGTCP2_ERR_CALLBACK_FAILURE;
548  
549 q->handshake_done = 1;
550 q->c->state = FT_WRITING;
551 /* Everything queued while the handshake ran goes out now, in order. */
552 for (st = q->streams; st; st = st->next)
553 if (!st->submitted && ft_h3_submit(aTHX_ q->c, st) != 0)
554 return NGTCP2_ERR_CALLBACK_FAILURE;
555 return 0;
556 }
557  
558 /* ---- the write pump ------------------------------------------------------
559 *
560 * QUIC PULLS. There is no send callback and no write buffer: bytes are asked
561 * for and handed straight to sendto. ngtcp2 packs several stream writes into
562 * one datagram and only it knows when one is full, which is why this is a
563 * loop and not a call. 0 to carry on, -1 if the connection was failed. */
564 static int ft_h3_write(pTHX_ ft_conn *c) {
565 ft_qconn *q = (ft_qconn *)c->h3;
566 uint8_t buf[FT_H3_MTU];
567 ngtcp2_path_storage ps;
568 ngtcp2_pkt_info pi;
569 for (;;) {
570 nghttp3_vec vec[16];
571 ngtcp2_ssize n, wrote = 0;
572 nghttp3_ssize nvec = 0;
573 int64_t sid = -1;
574 int fin = 0;
575 uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
576  
577 if (q->h3) {
578 nvec = nghttp3_conn_writev_stream(q->h3, &sid, &fin, vec,
579 sizeof(vec) / sizeof(vec[0]));
580 if (nvec < 0) { ft_h3_fail_all(aTHX_ c, "http/3 write failed"); return -1; }
581 }
582 if (sid < 0) flags = NGTCP2_WRITE_STREAM_FLAG_NONE;
583 else if (fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
584  
585 ngtcp2_path_storage_zero(&ps);
586 n = ngtcp2_conn_writev_stream(q->conn, &ps.path, &pi, buf, sizeof buf,
587 &wrote, flags, sid,
588 (const ngtcp2_vec *)vec, (size_t)nvec,
589 ft_h3_now());
590 if (n < 0) {
591 if (n == NGTCP2_ERR_WRITE_MORE) {
592 /* taken into the packet being built but not yet framed out */
593 if (wrote > 0 && q->h3
594 && nghttp3_conn_add_write_offset(q->h3, sid, (size_t)wrote) != 0) {
595 ft_h3_fail_all(aTHX_ c, "http/3 write offset failed");
596 return -1;
597 }
598 continue;
599 }
600 if (n == NGTCP2_ERR_STREAM_DATA_BLOCKED
601 || n == NGTCP2_ERR_STREAM_SHUT_WR) {
602 /* one stream cannot progress; the others still can */
603 if (q->h3 && sid >= 0) nghttp3_conn_block_stream(q->h3, sid);
604 continue;
605 }
606 ft_h3_fail_all(aTHX_ c, ngtcp2_strerror((int)n));
607 return -1;
608 }
609 if (wrote > 0 && q->h3
610 && nghttp3_conn_add_write_offset(q->h3, sid, (size_t)wrote) != 0) {
611 ft_h3_fail_all(aTHX_ c, "http/3 write offset failed");
612 return -1;
613 }
614 if (n == 0) break; /* nothing more to send */
615 /* send, not sendto: the socket is connected to the one peer (see
616 * ft_h3_connect), so there is no address to give and giving one is
617 * EISCONN. */
618 if (ft_os_send(c->fd, buf, (size_t)n, 0) < 0) {
619 if (errno == EAGAIN || errno == EWOULDBLOCK) {
620 ft_arm(aTHX_ c, HM_EV_READ | HM_EV_WRITE);
621 return 0;
622 }
623 ft_h3_fail_all(aTHX_ c, strerror(errno));
624 return -1;
625 }
626 }
627 return 0;
628 }
629  
630 /* The probe timeout fired. ngtcp2 decides what that means - a retransmit, a
631 * PING, or the connection being dead - and writing is what carries it out. */
632 static void ft_h3_on_pto(pTHX_ ft_conn *c) {
633 ft_qconn *q = (ft_qconn *)c->h3;
634 if (!q || q->closing) return;
635 if (ngtcp2_conn_handle_expiry(q->conn, ft_h3_now()) != 0) {
636 ft_h3_fail_all(aTHX_ c, "http/3 connection timed out");
637 return;
638 }
639 if (ft_h3_write(aTHX_ c) < 0) return;
640 ft_h3_rearm(aTHX_ c);
641 }
642  
643 /* Rearm the probe timeout wherever ngtcp2 has moved it to. Called after every
644 * read and every write, because that is how often it moves. */
645 static void ft_h3_rearm(pTHX_ ft_conn *c) {
646 ft_qconn *q = (ft_qconn *)c->h3;
647 ngtcp2_tstamp t = ngtcp2_conn_get_expiry(q->conn);
648 ngtcp2_tstamp now;
649 double secs;
650 if (t == UINT64_MAX) { ft_conn_cancel_pto(aTHX_ c); return; }
651 now = ft_h3_now();
652 secs = (t <= now) ? 0.000001 : (double)(t - now) / (double)NGTCP2_SECONDS;
653 ft_conn_arm_pto(aTHX_ c, secs, ft_h3_on_pto);
654 }
655  
656 /* ---- submitting a request ------------------------------------------------ */
657  
658 #define FT_H3_NV(NAME, VAL, VLEN) \
659 { (uint8_t *)(NAME), (uint8_t *)(VAL), sizeof(NAME) - 1, (VLEN), \
660 NGHTTP3_NV_FLAG_NONE }
661  
662 static int ft_h3_submit(pTHX_ ft_conn *c, ft_h3_stream *st) {
663 ft_qconn *q = (ft_qconn *)c->h3;
664 nghttp3_nv *nva;
665 int nvn = 0;
666 STRLEN ml, sl, al, pl;
667 const char *m = SvPV(st->rq_method, ml);
668 const char *sc = SvPV(st->rq_scheme, sl);
669 const char *au = SvPV(st->rq_authority, al);
670 const char *pa = SvPV(st->rq_path, pl);
671 SSize_t extra = st->rq_headers ? (av_len(st->rq_headers) + 1) / 2 : 0;
672 nghttp3_data_reader dr, *drp = NULL;
673 int64_t sid = -1;
674 int rv;
675  
676 if (ngtcp2_conn_open_bidi_stream(q->conn, &sid, st) != 0) return -1;
677 st->id = sid;
678  
679 Newx(nva, 4 + extra, nghttp3_nv);
680 { nghttp3_nv a = FT_H3_NV(":method", m, ml); nva[nvn++] = a; }
681 { nghttp3_nv a = FT_H3_NV(":scheme", sc, sl); nva[nvn++] = a; }
682 { nghttp3_nv a = FT_H3_NV(":authority", au, al); nva[nvn++] = a; }
683 { nghttp3_nv a = FT_H3_NV(":path", pa, pl); nva[nvn++] = a; }
684 if (st->rq_headers) {
685 SSize_t j;
686 for (j = 0; j + 1 <= av_len(st->rq_headers); j += 2) {
687 SV **ks = av_fetch(st->rq_headers, j, 0);
688 SV **vs = av_fetch(st->rq_headers, j + 1, 0);
689 STRLEN kl = 0, vl = 0;
690 char *k = ks ? SvPV(*ks, kl) : (char *)"";
691 char *v = vs ? SvPV(*vs, vl) : (char *)"";
692 nva[nvn].name = (uint8_t *)k; nva[nvn].namelen = kl;
693 nva[nvn].value = (uint8_t *)v; nva[nvn].valuelen = vl;
694 nva[nvn].flags = NGHTTP3_NV_FLAG_NONE;
695 nvn++;
696 }
697 }
698 if (st->rq_body && SvOK(st->rq_body) && SvCUR(st->rq_body)) {
699 dr.read_data = ft_h3_read_body;
700 drp = &dr;
701 }
702 rv = nghttp3_conn_submit_request(q->h3, sid, nva, nvn, drp, st);
703 Safefree(nva);
704 if (rv != 0) return -1;
705 st->submitted = 1;
706 return 0;
707 }
708  
709 /* ---- connect ------------------------------------------------------------- */
710  
711 /* ALPN. QUIC's is inside the QUIC handshake and has nothing to do with the
712 * TLS-over-TCP list in ft_tls.h, which stays as it is. */
713 static const unsigned char FT_H3_ALPN[] = { 2, 'h', '3' };
714  
715 /* Open the QUIC connection. The socket is a UDP one, so nothing above this
716 * changes: the loop watches an fd for readability either way. */
717 static int ft_h3_connect(pTHX_ ft_conn *c, struct addrinfo *ai) {
718 ft_qconn *q;
719 ngtcp2_callbacks cbs;
720 ngtcp2_settings settings;
721 ngtcp2_transport_params params;
722 ngtcp2_cid dcid, scid;
723 struct sockaddr_storage local;
724 socklen_t locallen = sizeof local;
725 SSL_CTX *ctx;
726 int fd;
727  
728 fd = ft_os_socket(ai->ai_family, SOCK_DGRAM, 0);
729 if (fd < 0) return -1;
730 ft_os_set_nonblock(fd);
731  
732 Newxz(q, 1, ft_qconn);
733 q->c = c;
734 memcpy(&q->peer, ai->ai_addr, ai->ai_addrlen);
735 q->peerlen = (socklen_t)ai->ai_addrlen;
736  
737 /* The local address is needed for the path, and a UDP socket has none
738 * until it is bound - so connect() it, which binds it and also lets the
739 * kernel filter datagrams from anyone else. */
740 if (ft_os_connect(fd, (struct sockaddr *)&q->peer, (int)q->peerlen) != 0
741 && errno != EINPROGRESS) {
742 Safefree(q); ft_os_close(fd); return -1;
743 }
744 if (getsockname(fd, (struct sockaddr *)&local, &locallen) < 0) {
745 Safefree(q); ft_os_close(fd); return -1;
746 }
747 ngtcp2_path_storage_init(&q->path, (struct sockaddr *)&local, locallen,
748 (struct sockaddr *)&q->peer, q->peerlen, NULL);
749  
750 ft_os_random(scid.data, FT_H3_CIDLEN); scid.datalen = FT_H3_CIDLEN;
751 ft_os_random(dcid.data, FT_H3_CIDLEN); dcid.datalen = FT_H3_CIDLEN;
752  
753 memset(&cbs, 0, sizeof cbs);
754 /* the eleven ngtcp2_crypto supplies ready-made (the client half) */
755 cbs.client_initial = ngtcp2_crypto_client_initial_cb;
756 cbs.recv_crypto_data = ngtcp2_crypto_recv_crypto_data_cb;
757 cbs.encrypt = ngtcp2_crypto_encrypt_cb;
758 cbs.decrypt = ngtcp2_crypto_decrypt_cb;
759 cbs.hp_mask = ngtcp2_crypto_hp_mask_cb;
760 cbs.update_key = ngtcp2_crypto_update_key_cb;
761 cbs.delete_crypto_aead_ctx = ngtcp2_crypto_delete_crypto_aead_ctx_cb;
762 cbs.delete_crypto_cipher_ctx = ngtcp2_crypto_delete_crypto_cipher_ctx_cb;
763 cbs.get_path_challenge_data = ngtcp2_crypto_get_path_challenge_data_cb;
764 cbs.version_negotiation = ngtcp2_crypto_version_negotiation_cb;
765 cbs.recv_retry = ngtcp2_crypto_recv_retry_cb;
766 /* ...and the glue that is ours */
767 cbs.rand = ft_h3_cb_qrand;
768 cbs.get_new_connection_id = ft_h3_cb_get_new_cid;
769 cbs.handshake_completed = ft_h3_cb_handshake_completed;
770 cbs.recv_stream_data = ft_h3_cb_recv_stream_data;
771 cbs.stream_close = ft_h3_cb_qstream_close;
772 cbs.acked_stream_data_offset = ft_h3_cb_acked_stream_data;
773 cbs.extend_max_stream_data = ft_h3_cb_extend_max_stream_data;
774  
775 ngtcp2_settings_default(&settings);
776 ngtcp2_transport_params_default(&params);
777 settings.initial_ts = ft_h3_now();
778 params.initial_max_stream_data_bidi_local = 256 * 1024;
779 params.initial_max_stream_data_bidi_remote = 256 * 1024;
780 params.initial_max_stream_data_uni = 256 * 1024;
781 params.initial_max_data = 1024 * 1024;
782 params.initial_max_streams_bidi = 0; /* a client accepts none */
783 params.initial_max_streams_uni = 8; /* the server's control
784 * + QPACK streams */
785 params.max_idle_timeout = 30 * NGTCP2_SECONDS;
786  
787 if (ngtcp2_conn_client_new(&q->conn, &dcid, &scid, &q->path.path,
788 NGTCP2_PROTO_VER_V1, &cbs, &settings, &params,
789 NULL, q) != 0) {
790 Safefree(q); ft_os_close(fd); return -1;
791 }
792  
793 ctx = ft_h3_ctx(c->verify);
794 if (!ctx) { ngtcp2_conn_del(q->conn); Safefree(q); ft_os_close(fd); return -1; }
795 q->ssl = SSL_new(ctx);
796 if (!q->ssl
797 || ngtcp2_crypto_ossl_ctx_new(&q->ossl, q->ssl) != 0
798 || ngtcp2_crypto_ossl_configure_client_session(q->ssl) != 0) {
799 if (q->ssl && !q->ossl) SSL_free(q->ssl);
800 ngtcp2_conn_del(q->conn); Safefree(q); ft_os_close(fd);
801 return -1;
802 }
803 q->conn_ref.get_conn = ft_h3_get_conn;
804 q->conn_ref.user_data = q;
805 SSL_set_app_data(q->ssl, &q->conn_ref);
806 /* ngtcp2 drives the handshake itself and never calls SSL_connect, so the
807 * side has to be set here or the first Initial fails with a crypto error
808 * and nothing in the error queue to explain it. */
809 SSL_set_connect_state(q->ssl);
810 SSL_set_alpn_protos(q->ssl, FT_H3_ALPN, sizeof FT_H3_ALPN);
811 if (c->host && *c->host) {
812 SSL_set_tlsext_host_name(q->ssl, c->host);
813 if (c->verify) {
814 SSL_set_hostflags(q->ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
815 SSL_set1_host(q->ssl, c->host);
816 SSL_set_verify(q->ssl, SSL_VERIFY_PEER, NULL);
817 }
818 }
819 ngtcp2_conn_set_tls_native_handle(q->conn, q->ossl);
820  
821 c->fd = fd;
822 c->h3 = q;
823 c->is_h3 = 1;
824 c->state = FT_QUIC_HANDSHAKING;
825 return 0;
826 }
827  
828 /* ---- the readiness pass -------------------------------------------------- */
829  
830 static void ft_h3_step(pTHX_ ft_conn *c) {
831 ft_qconn *q = (ft_qconn *)c->h3;
832 uint8_t buf[65536];
833 ngtcp2_pkt_info pi;
834 for (;;) {
835 /* recv, not recvfrom: the socket is connected, so the kernel has
836 * already dropped anything from anyone else and the sender is always
837 * q->peer. That connect() is also what gets this an ECONNREFUSED
838 * when nothing is listening - an unconnected UDP socket is told
839 * nothing and would wait out the idle timeout instead. */
840 ssize_t n = ft_os_recv(c->fd, buf, sizeof buf, 0);
841 if (n < 0) {
842 if (errno == EAGAIN || errno == EWOULDBLOCK) break;
843 if (errno == EINTR) continue;
844 ft_h3_fail_all(aTHX_ c, strerror(errno));
845 return;
846 }
847 memset(&pi, 0, sizeof pi);
848 {
849 if (ngtcp2_conn_read_pkt(q->conn, &q->path.path, &pi, buf,
850 (size_t)n, ft_h3_now()) != 0) {
851 ft_h3_fail_all(aTHX_ c, "http/3 connection error");
852 return;
853 }
854 }
855 /* A callback may have failed everything and freed the connection. */
856 if (!c->h3) return;
857 }
858 if (ft_h3_write(aTHX_ c) < 0) return;
859 ft_h3_rearm(aTHX_ c);
860 /* Read is always wanted: the response, the acks and the peer's own
861 * control stream all arrive that way. */
862 if (!(c->armed & HM_EV_WRITE)) ft_arm(aTHX_ c, HM_EV_READ);
863 }
864  
865 /* ---- the request entry point --------------------------------------------
866 *
867 * Attach one request to this connection. Submitted at once if the handshake
868 * has finished, and queued for ft_h3_cb_handshake_completed if it has not -
869 * which is what lets several requests be started before the connection is
870 * even up, and is the difference between multiplexing and taking turns.
871 *
872 * The stream owns its own copies of everything: the caller's SVs go out of
873 * scope while the response is still arriving. */
874 static void ft_h3_request(pTHX_ ft_conn *c, SV *future,
875 SV *method, SV *scheme, SV *authority, SV *path,
876 SV *headers_av, SV *body, SV *on_body,
877 SV *on_headers, int simple) {
878 ft_qconn *q = (ft_qconn *)c->h3;
879 ft_h3_stream *st = ft_h3_stream_new(aTHX);
880 st->future = SvREFCNT_inc(future);
881 st->rq_method = newSVsv(method);
882 st->rq_scheme = newSVsv(scheme);
883 st->rq_authority = newSVsv(authority);
884 st->rq_path = newSVsv(path);
885 st->simple = simple;
886 if (headers_av && SvROK(headers_av) && SvTYPE(SvRV(headers_av)) == SVt_PVAV)
887 st->rq_headers = (AV *)SvREFCNT_inc(SvRV(headers_av));
888 if (body && SvOK(body)) st->rq_body = newSVsv(body);
889 if (on_body && SvROK(on_body)) st->on_body = SvREFCNT_inc(on_body);
890 if (on_headers && SvROK(on_headers)) st->on_headers = SvREFCNT_inc(on_headers);
891 st->next = q->streams;
892 q->streams = st;
893 q->nstreams++;
894 if (q->handshake_done && ft_h3_submit(aTHX_ c, st) != 0) {
895 ft_h3_fail_all(aTHX_ c, "http/3 submit failed");
896 return;
897 }
898 if (q->handshake_done) {
899 if (ft_h3_write(aTHX_ c) < 0) return;
900 ft_h3_rearm(aTHX_ c);
901 }
902 }
903  
904 /* Is this pooled connection still usable?
905 *
906 * The h1 pool asks with a one-byte MSG_PEEK, which says whether the socket is
907 * still there. That question does not transfer: a QUIC connection can be dead
908 * with its UDP socket perfectly healthy - the peer's idle timeout expired, or
909 * it sent CONNECTION_CLOSE - so what is asked instead is whether ngtcp2 still
910 * considers the connection open. */
911 static int ft_conn_alive_h3(ft_conn *c) {
912 ft_qconn *q = (ft_qconn *)c->h3;
913 if (!q || q->closing || !q->conn) return 0;
914 if (ngtcp2_conn_in_closing_period2(q->conn)
915 || ngtcp2_conn_in_draining_period2(q->conn)) return 0;
916 return 1;
917 }
918  
919 static void ft_h3_free(pTHX_ ft_conn *c) {
920 ft_qconn *q = (ft_qconn *)c->h3;
921 if (!q) return;
922 c->h3 = NULL;
923 while (q->streams) {
924 ft_h3_stream *st = q->streams;
925 q->streams = st->next;
926 ft_h3_stream_free(aTHX_ st);
927 }
928 if (q->h3) nghttp3_conn_del(q->h3);
929 if (q->ossl) ngtcp2_crypto_ossl_ctx_del(q->ossl); /* frees the SSL too */
930 else if (q->ssl) SSL_free(q->ssl);
931 if (q->conn) ngtcp2_conn_del(q->conn);
932 Safefree(q);
933 }
934  
935 #else /* stubs: no ngtcp2/nghttp3, or an OpenSSL that cannot drive them */
936  
937 /* Every entry point the h3 half offers, so ft_http.h compiles unchanged and
938 * ft_h3_start's own FT_H3_AVAILABLE test is the ONE place that decides. A
939 * caller never has to #ifdef, which is also why these are stubs rather than
940 * absent: an absent function moves the decision into the preprocessor and
941 * into every call site. */
942 #define FT_H3_AVAILABLE 0
943 typedef struct ft_h3_stream ft_h3_stream;
944 0 static int ft_h3_connect(pTHX_ ft_conn *c, struct addrinfo *ai) {
945 0 (void)c; (void)ai; return -1; }
946 0 static void ft_h3_step(pTHX_ ft_conn *c) {
947 0 ft_conn_fail(aTHX_ c, "HTTP/3 not built (need ngtcp2 + nghttp3)"); }
948 119 static void ft_h3_free(pTHX_ ft_conn *c) { (void)c; }
949 0 static int ft_conn_alive_h3(ft_conn *c) { (void)c; return 0; }
950 0 static void ft_h3_request(pTHX_ ft_conn *c, SV *future,
951 SV *method, SV *scheme, SV *authority, SV *path,
952 SV *headers_av, SV *body, SV *on_body,
953 SV *on_headers, int simple) {
954 (void)c; (void)method; (void)scheme; (void)authority; (void)path;
955 (void)headers_av; (void)body; (void)on_body; (void)on_headers;
956 (void)simple;
957 0 { SV *e = sv_2mortal(newSVpvs("Fetch: HTTP/3 not built "
958 "(need ngtcp2 + nghttp3)"));
959 0 ▶ if (hmf_state(aTHX_ future) == HMF_PENDING)
Branch: 1 branch
BranchTrueFalseTotal
if (hmf_state(aTHX_ future) == HMF_PENDING) 0 0 0
960 0 hmf_settle(aTHX_ future, HMF_FAILED, &e, 1);
961 }
962 0 }
963  
964 #endif /* FT_HAVE_QUIC */
965  
966 #endif /* FT_H3_H */
« include/fetch/ft_hm.h ↑ Summary include/fetch/ft_altsvc.h »