/src/curl/lib/vquic/vquic.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | #include "vquic/vquic.h" |
27 | | #include "vtls/vtls.h" |
28 | | |
29 | | #include "curl_trc.h" |
30 | | |
31 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) |
32 | | |
33 | | #ifdef HAVE_NETINET_UDP_H |
34 | | #include <netinet/udp.h> |
35 | | #endif |
36 | | #ifdef HAVE_NETINET_IP_H |
37 | | #include <netinet/ip.h> |
38 | | #endif |
39 | | |
40 | | #ifdef USE_NGHTTP3 |
41 | | #include <nghttp3/nghttp3.h> |
42 | | #endif |
43 | | |
44 | | #if defined(USE_APPLE_FAST_UDP) && defined(__APPLE__) |
45 | | #include <sys/syscall.h> |
46 | | #if defined(SYS_recvmsg_x) && defined(SYS_sendmsg_x) |
47 | | #define HAVE_APPLE_MSG_X |
48 | | #endif |
49 | | #endif |
50 | | |
51 | | #include "bufq.h" |
52 | | #include "curlx/dynbuf.h" |
53 | | #include "curlx/fopen.h" |
54 | | #include "cfilters.h" |
55 | | #include "vdns/cf-dns.h" |
56 | | #include "vquic/cf-ngtcp2.h" |
57 | | #include "vquic/cf-ngtcp2-cmn.h" |
58 | | #include "vquic/cf-ngtcp2-proxy.h" |
59 | | #include "vquic/cf-quiche.h" |
60 | | #include "multiif.h" |
61 | | #include "progress.h" |
62 | | #include "rand.h" |
63 | | #include "vquic/vquic_int.h" |
64 | | #include "curlx/strerr.h" |
65 | | #include "curlx/strparse.h" |
66 | | |
67 | | |
68 | | #define NW_CHUNK_SIZE (64 * 1024) |
69 | | #define NW_SEND_CHUNKS 1 |
70 | | |
71 | | #ifdef HAVE_APPLE_MSG_X |
72 | | |
73 | | /* this is `struct msghdr` with an additional field at the end */ |
74 | | struct msghdr_x { |
75 | | void *msg_name; /* optional address */ |
76 | | socklen_t msg_namelen; /* size of address */ |
77 | | struct iovec *msg_iov; /* scatter/gather array */ |
78 | | int msg_iovlen; /* # elements in msg_iov */ |
79 | | void *msg_control; /* ancillary data, see below */ |
80 | | socklen_t msg_controllen; /* ancillary data buffer len */ |
81 | | int msg_flags; /* flags on received message */ |
82 | | size_t msg_datalen; /* byte length of buffer in msg_iov */ |
83 | | }; |
84 | | #endif |
85 | | |
86 | | #ifdef CURLVERBOSE |
87 | | #ifdef HAVE_APPLE_MSG_X |
88 | | #define VQUIC_SEND_METHOD "sendmsg_x" |
89 | | #elif defined(HAVE_SENDMSG) |
90 | | #define VQUIC_SEND_METHOD "sendmsg" |
91 | | #else |
92 | | #define VQUIC_SEND_METHOD "send" |
93 | | #endif |
94 | | #endif |
95 | | |
96 | | int Curl_vquic_init(void) |
97 | | { |
98 | | #if defined(USE_NGTCP2) && defined(OPENSSL_QUIC_API2) |
99 | | if(ngtcp2_crypto_ossl_init()) |
100 | | return 0; |
101 | | #endif |
102 | | |
103 | | return 1; |
104 | | } |
105 | | |
106 | | void Curl_vquic_cleanup(void) |
107 | | { |
108 | | #if defined(USE_NGTCP2) && defined(OPENSSL_QUIC_API2) && \ |
109 | | (NGTCP2_VERSION_NUM >= 0x011800) |
110 | | ngtcp2_crypto_ossl_free(); |
111 | | #endif |
112 | | } |
113 | | |
114 | | void Curl_quic_ver(char *p, size_t len) |
115 | | { |
116 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
117 | | Curl_ngtcp2_ver(p, len); |
118 | | #elif defined(USE_QUICHE) |
119 | | Curl_quiche_ver(p, len); |
120 | | #endif |
121 | | } |
122 | | |
123 | | CURLcode Curl_vquic_ctx_init(struct Curl_easy *data, |
124 | | struct cf_quic_ctx *qctx) |
125 | | { |
126 | | Curl_bufq_init2(&qctx->sendbuf, NW_CHUNK_SIZE, NW_SEND_CHUNKS, |
127 | | BUFQ_OPT_SOFT_LIMIT); |
128 | | #if defined(__linux__) && defined(UDP_SEGMENT) && defined(HAVE_SENDMSG) |
129 | | qctx->no_gso = FALSE; |
130 | | #elif defined(HAVE_APPLE_MSG_X) |
131 | | qctx->no_gso = FALSE; |
132 | | #else |
133 | | qctx->no_gso = TRUE; |
134 | | #endif |
135 | | #ifdef DEBUGBUILD |
136 | | { |
137 | | const char *p = getenv("CURL_DBG_QUIC_WBLOCK"); |
138 | | if(p) { |
139 | | curl_off_t l; |
140 | | if(!curlx_str_number(&p, &l, 100)) |
141 | | qctx->wblock_percent = (int)l; |
142 | | } |
143 | | } |
144 | | #endif |
145 | | Curl_vquic_ctx_set_time(qctx, Curl_pgrs_now(data)); |
146 | | |
147 | | return CURLE_OK; |
148 | | } |
149 | | |
150 | | void Curl_vquic_ctx_free(struct cf_quic_ctx *qctx) |
151 | | { |
152 | | Curl_bufq_free(&qctx->sendbuf); |
153 | | } |
154 | | |
155 | | void Curl_vquic_ctx_set_time(struct cf_quic_ctx *qctx, |
156 | | const struct curltime *pnow) |
157 | | { |
158 | | qctx->last_op = *pnow; |
159 | | } |
160 | | |
161 | | void Curl_vquic_ctx_update_time(struct cf_quic_ctx *qctx, |
162 | | const struct curltime *pnow) |
163 | | { |
164 | | qctx->last_op = *pnow; |
165 | | } |
166 | | |
167 | | #ifdef HAVE_APPLE_MSG_X |
168 | | static CURLcode do_sendmsg(struct Curl_cfilter *cf, |
169 | | struct Curl_easy *data, |
170 | | struct cf_quic_ctx *qctx, |
171 | | const uint8_t *pkt, size_t pktlen, size_t gsolen, |
172 | | size_t *psent) |
173 | | { |
174 | | #define MSG_X_SNUM 64 |
175 | | struct iovec msg_iov[MSG_X_SNUM]; |
176 | | struct msghdr_x mmsg[MSG_X_SNUM]; |
177 | | char errstr[STRERROR_LEN]; |
178 | | size_t n, i = 0, sent = 0; |
179 | | int rc; |
180 | | CURLcode result = CURLE_OK; |
181 | | VERBOSE(size_t calls = 0); |
182 | | |
183 | | *psent = 0; |
184 | | if(!pktlen) |
185 | | return CURLE_OK; |
186 | | if(!gsolen || (pktlen < gsolen)) |
187 | | gsolen = pktlen; |
188 | | n = (pktlen + gsolen - 1) / gsolen; |
189 | | while(i < n) { |
190 | | size_t j, batch = CURLMIN(n - i, MSG_X_SNUM), pkts_sent = 0; |
191 | | |
192 | | for(j = 0; j < batch; ++j) { |
193 | | const size_t offset = (i + j) * gsolen; |
194 | | msg_iov[j].iov_base = CURL_UNCONST(pkt + offset); |
195 | | msg_iov[j].iov_len = CURLMIN(gsolen, pktlen - offset); |
196 | | memset(&mmsg[j], 0, sizeof(mmsg[j])); |
197 | | mmsg[j].msg_iov = &msg_iov[j]; |
198 | | mmsg[j].msg_iovlen = 1; |
199 | | mmsg[j].msg_datalen = msg_iov[j].iov_len; |
200 | | } |
201 | | |
202 | | #if defined(CURL_HAVE_DIAG) && defined(__APPLE__) |
203 | | #pragma GCC diagnostic push |
204 | | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
205 | | #endif |
206 | | while((rc = syscall(SYS_sendmsg_x, qctx->sockfd, &mmsg, batch, 0)) == -1 && |
207 | | (SOCKERRNO == SOCKEINTR)) |
208 | | ; |
209 | | #if defined(CURL_HAVE_DIAG) && defined(__APPLE__) |
210 | | #pragma GCC diagnostic pop |
211 | | #endif |
212 | | |
213 | | if(rc < 0) { |
214 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
215 | | CURL_TRC_CF(data, cf, "egress, sendmsg_x -> EAGAIN"); |
216 | | result = sent ? CURLE_OK : CURLE_AGAIN; |
217 | | goto out; |
218 | | } |
219 | | if(SOCKERRNO != SOCKEMSGSIZE) { |
220 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
221 | | failf(data, "QUIC: sendmsg_x() returned %d (errno=%d; %s)", |
222 | | rc, SOCKERRNO, errstr); |
223 | | result = CURLE_SEND_ERROR; |
224 | | goto out; |
225 | | } |
226 | | /* Error was SOCKEMSGSIZE. Network stack does not accept the packet |
227 | | * length(s). This might be a PMTUD. Just drop it into the void |
228 | | * and fall through to the success handling. */ |
229 | | pkts_sent = batch; |
230 | | } |
231 | | else |
232 | | pkts_sent = CURLMIN((size_t)rc, batch); |
233 | | |
234 | | VERBOSE(++calls); |
235 | | if(!pkts_sent) { /* no packets of the current batch were sent */ |
236 | | result = sent ? CURLE_OK : CURLE_AGAIN; |
237 | | goto out; |
238 | | } |
239 | | i += pkts_sent; /* Some have been sent */ |
240 | | for(j = 0; j < pkts_sent; ++j) |
241 | | sent += msg_iov[j].iov_len; |
242 | | if(pkts_sent < batch) |
243 | | goto out; /* but not all of them */ |
244 | | } |
245 | | |
246 | | out: |
247 | | *psent = sent; |
248 | | if(sent || result) |
249 | | CURL_TRC_CF(data, cf, |
250 | | "vquic_sendmsg_x(len=%zu, gso=%zu, packets=%zu, " |
251 | | "calls=%zu) -> %d", |
252 | | sent, gsolen, n, calls, (int)result); |
253 | | return result; |
254 | | } |
255 | | |
256 | | #else /* HAVE_APPLE_MSG_X */ |
257 | | |
258 | | static CURLcode send_packet_no_gso(struct Curl_cfilter *cf, |
259 | | struct Curl_easy *data, |
260 | | struct cf_quic_ctx *qctx, |
261 | | const uint8_t *pkt, size_t pktlen, |
262 | | size_t gsolen, size_t *psent); |
263 | | |
264 | | static CURLcode do_sendmsg(struct Curl_cfilter *cf, |
265 | | struct Curl_easy *data, |
266 | | struct cf_quic_ctx *qctx, |
267 | | const uint8_t *pkt, size_t pktlen, size_t gsolen, |
268 | | size_t *psent) |
269 | | { |
270 | | CURLcode result = CURLE_OK; |
271 | | #ifdef HAVE_SENDMSG |
272 | | struct iovec msg_iov; |
273 | | struct msghdr msg = { 0 }; |
274 | | ssize_t rv; |
275 | | #if defined(__linux__) && defined(UDP_SEGMENT) |
276 | | uint8_t msg_ctrl[32]; |
277 | | struct cmsghdr *cm; |
278 | | #endif |
279 | | |
280 | | *psent = 0; |
281 | | msg_iov.iov_base = (uint8_t *)CURL_UNCONST(pkt); |
282 | | msg_iov.iov_len = pktlen; |
283 | | msg.msg_iov = &msg_iov; |
284 | | msg.msg_iovlen = 1; |
285 | | |
286 | | #if defined(__linux__) && defined(UDP_SEGMENT) |
287 | | if(pktlen > gsolen) { |
288 | | /* Only set this, when we need it. macOS, for example, |
289 | | * does not seem to like a msg_control of length 0. */ |
290 | | memset(msg_ctrl, 0, sizeof(msg_ctrl)); |
291 | | msg.msg_control = msg_ctrl; |
292 | | DEBUGASSERT(sizeof(msg_ctrl) >= CMSG_SPACE(sizeof(int))); |
293 | | msg.msg_controllen = CMSG_SPACE(sizeof(int)); |
294 | | cm = CMSG_FIRSTHDR(&msg); |
295 | | cm->cmsg_level = SOL_UDP; |
296 | | cm->cmsg_type = UDP_SEGMENT; |
297 | | cm->cmsg_len = CMSG_LEN(sizeof(uint16_t)); |
298 | | *(uint16_t *)(void *)CMSG_DATA(cm) = gsolen & 0xffff; |
299 | | } |
300 | | #endif |
301 | | |
302 | | while((rv = sendmsg(qctx->sockfd, &msg, 0)) == -1 && SOCKERRNO == SOCKEINTR) |
303 | | ; |
304 | | |
305 | | if(!curlx_sztouz(rv, psent)) { |
306 | | int sockerr = SOCKERRNO; |
307 | | if(SOCK_EAGAIN(sockerr)) |
308 | | return CURLE_AGAIN; |
309 | | switch(sockerr) { |
310 | | case SOCKEMSGSIZE: |
311 | | /* UDP datagram is too large; caused by PMTUD. Let it be lost. */ |
312 | | *psent = pktlen; |
313 | | break; |
314 | | case EIO: |
315 | | if(pktlen > gsolen) { |
316 | | /* GSO failure */ |
317 | | infof(data, "sendmsg() returned %zd (errno %d); disable GSO", rv, |
318 | | sockerr); |
319 | | qctx->no_gso = TRUE; |
320 | | return send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent); |
321 | | } |
322 | | FALLTHROUGH(); |
323 | | default: |
324 | | failf(data, "sendmsg() returned %zd (errno %d)", rv, sockerr); |
325 | | result = CURLE_SEND_ERROR; |
326 | | goto out; |
327 | | } |
328 | | } |
329 | | else if(pktlen != *psent) { |
330 | | failf(data, "sendmsg() sent only %zu/%zu bytes", *psent, pktlen); |
331 | | result = CURLE_SEND_ERROR; |
332 | | goto out; |
333 | | } |
334 | | #else |
335 | | ssize_t rv; |
336 | | (void)gsolen; |
337 | | |
338 | | *psent = 0; |
339 | | |
340 | | while((rv = swrite(qctx->sockfd, pkt, pktlen)) == -1 && |
341 | | SOCKERRNO == SOCKEINTR) |
342 | | ; |
343 | | |
344 | | if(!curlx_sztouz(rv, psent)) { |
345 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
346 | | result = CURLE_AGAIN; |
347 | | goto out; |
348 | | } |
349 | | else { |
350 | | if(SOCKERRNO != SOCKEMSGSIZE) { |
351 | | failf(data, "send() returned %zd (errno %d)", rv, SOCKERRNO); |
352 | | result = CURLE_SEND_ERROR; |
353 | | goto out; |
354 | | } |
355 | | /* UDP datagram is too large; caused by PMTUD. Let it be lost. */ |
356 | | *psent = pktlen; |
357 | | } |
358 | | } |
359 | | #endif |
360 | | (void)cf; |
361 | | |
362 | | out: |
363 | | CURL_TRC_CF(data, cf, |
364 | | "vquic_%s(len=%zu, gso=%zu, calls=1) -> %d, sent=%zu", |
365 | | VQUIC_SEND_METHOD, pktlen, gsolen, (int)result, *psent); |
366 | | return result; |
367 | | } |
368 | | |
369 | | #endif /* !HAVE_APPLE_MSG_X */ |
370 | | |
371 | | static CURLcode send_packet_no_gso(struct Curl_cfilter *cf, |
372 | | struct Curl_easy *data, |
373 | | struct cf_quic_ctx *qctx, |
374 | | const uint8_t *pkt, size_t pktlen, |
375 | | size_t gsolen, size_t *psent) |
376 | | { |
377 | | const uint8_t *p, *end = pkt + pktlen; |
378 | | size_t sent, len; |
379 | | CURLcode result = CURLE_OK; |
380 | | VERBOSE(size_t calls = 0); |
381 | | |
382 | | *psent = 0; |
383 | | |
384 | | for(p = pkt; p < end; p += gsolen) { |
385 | | len = CURLMIN(gsolen, (size_t)(end - p)); |
386 | | result = do_sendmsg(cf, data, qctx, p, len, len, &sent); |
387 | | if(result) |
388 | | goto out; |
389 | | *psent += sent; |
390 | | VERBOSE(++calls); |
391 | | } |
392 | | out: |
393 | | CURL_TRC_CF(data, cf, |
394 | | "vquic_%s(len=%zu, gso=%zu, calls=%zu) -> %d, sent=%zu", |
395 | | VQUIC_SEND_METHOD, pktlen, gsolen, calls, (int)result, *psent); |
396 | | return result; |
397 | | } |
398 | | |
399 | | /* Split QUIC payload by datagram (gso) boundaries when sending over a |
400 | | * non-UDP lower filter (for example CONNECT-UDP proxy tunnel). */ |
401 | | static CURLcode send_packet_no_gso_cf(struct Curl_cfilter *cf, |
402 | | struct Curl_easy *data, |
403 | | const uint8_t *pkt, size_t pktlen, |
404 | | size_t gsolen, size_t *psent) |
405 | | { |
406 | | const uint8_t *p, *end = pkt + pktlen; |
407 | | size_t sent, len; |
408 | | CURLcode result = CURLE_OK; |
409 | | VERBOSE(size_t calls = 0); |
410 | | |
411 | | *psent = 0; |
412 | | |
413 | | /* Send one datagram-sized chunk per call into the lower filter. */ |
414 | | for(p = pkt; p < end; p += len) { |
415 | | len = CURLMIN(gsolen, (size_t)(end - p)); |
416 | | result = Curl_conn_cf_send(cf->next, data, p, len, FALSE, &sent); |
417 | | /* Report forward progress even if we return CURLE_AGAIN later. */ |
418 | | VERBOSE(++calls); |
419 | | /* Preserve lower-filter errors (including CURLE_AGAIN). */ |
420 | | if(result) |
421 | | goto out; |
422 | | |
423 | | if(sent != len) { |
424 | | /* We can only send the complete datagram, not parts. */ |
425 | | result = CURLE_SEND_ERROR; |
426 | | goto out; |
427 | | } |
428 | | *psent += sent; |
429 | | } |
430 | | |
431 | | out: |
432 | | CURL_TRC_CF(data, cf, |
433 | | "vquic_cf_send(len=%zu, gso=%zu, calls=%zu) -> %d, sent=%zu", |
434 | | pktlen, gsolen, calls, (int)result, *psent); |
435 | | return result; |
436 | | } |
437 | | |
438 | | static CURLcode vquic_send_packets(struct Curl_cfilter *cf, |
439 | | struct Curl_easy *data, |
440 | | struct cf_quic_ctx *qctx, |
441 | | const uint8_t *pkt, size_t pktlen, |
442 | | size_t gsolen, size_t *psent) |
443 | | { |
444 | | CURLcode result; |
445 | | #ifdef DEBUGBUILD |
446 | | /* simulate network blocking/partial writes */ |
447 | | if(qctx->wblock_percent > 0) { |
448 | | unsigned char c; |
449 | | *psent = 0; |
450 | | Curl_rand(data, &c, 1); |
451 | | if(c >= ((100 - qctx->wblock_percent) * 256 / 100)) { |
452 | | CURL_TRC_CF(data, cf, "vquic_flush() simulate EWOULDBLOCK"); |
453 | | return CURLE_AGAIN; |
454 | | } |
455 | | } |
456 | | #endif |
457 | | if(qctx->no_gso && pktlen > gsolen) { |
458 | | result = send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent); |
459 | | } |
460 | | else { |
461 | | result = do_sendmsg(cf, data, qctx, pkt, pktlen, gsolen, psent); |
462 | | } |
463 | | if(!result) |
464 | | qctx->last_io = qctx->last_op; |
465 | | return result; |
466 | | } |
467 | | |
468 | | CURLcode Curl_vquic_flush(struct Curl_cfilter *cf, struct Curl_easy *data, |
469 | | struct cf_quic_ctx *qctx) |
470 | | { |
471 | | const unsigned char *buf; |
472 | | size_t blen, sent; |
473 | | CURLcode result; |
474 | | size_t gsolen; |
475 | | |
476 | | while(Curl_bufq_peek(&qctx->sendbuf, &buf, &blen)) { |
477 | | gsolen = qctx->gsolen; |
478 | | if(qctx->split_len) { |
479 | | gsolen = qctx->split_gsolen; |
480 | | if(blen > qctx->split_len) |
481 | | blen = qctx->split_len; |
482 | | } |
483 | | |
484 | | if(qctx->sockfd != CURL_SOCKET_BAD) { |
485 | | /* Direct UDP socket (via happy eyeballs) */ |
486 | | result = vquic_send_packets(cf, data, qctx, buf, blen, gsolen, &sent); |
487 | | } |
488 | | else { |
489 | | /* Tunneled QUIC (CONNECT-UDP through proxy) */ |
490 | | if(gsolen && (blen > gsolen)) { |
491 | | /* Send one datagram at a time to preserve packet boundaries. */ |
492 | | result = send_packet_no_gso_cf(cf, data, buf, blen, gsolen, &sent); |
493 | | } |
494 | | else { |
495 | | /* No GSO aggregate to split, regular lower-filter send is enough. */ |
496 | | result = Curl_conn_cf_send(cf->next, data, buf, blen, FALSE, &sent); |
497 | | } |
498 | | } |
499 | | |
500 | | if(result) { |
501 | | if(result == CURLE_AGAIN) { |
502 | | Curl_bufq_skip(&qctx->sendbuf, sent); |
503 | | if(qctx->split_len) |
504 | | qctx->split_len -= sent; |
505 | | } |
506 | | return result; |
507 | | } |
508 | | Curl_bufq_skip(&qctx->sendbuf, sent); |
509 | | if(qctx->split_len) |
510 | | qctx->split_len -= sent; |
511 | | } |
512 | | return CURLE_OK; |
513 | | } |
514 | | |
515 | | CURLcode Curl_vquic_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
516 | | struct cf_quic_ctx *qctx, size_t gsolen) |
517 | | { |
518 | | qctx->gsolen = gsolen; |
519 | | return Curl_vquic_flush(cf, data, qctx); |
520 | | } |
521 | | |
522 | | CURLcode Curl_vquic_send_tail_split(struct Curl_cfilter *cf, |
523 | | struct Curl_easy *data, |
524 | | struct cf_quic_ctx *qctx, size_t gsolen, |
525 | | size_t tail_len, size_t tail_gsolen) |
526 | | { |
527 | | DEBUGASSERT(Curl_bufq_len(&qctx->sendbuf) > tail_len); |
528 | | qctx->split_len = Curl_bufq_len(&qctx->sendbuf) - tail_len; |
529 | | qctx->split_gsolen = gsolen; |
530 | | qctx->gsolen = tail_gsolen; |
531 | | CURL_TRC_CF(data, cf, "vquic_send_tail_split: [%zu gso=%zu][%zu gso=%zu]", |
532 | | qctx->split_len, qctx->split_gsolen, tail_len, qctx->gsolen); |
533 | | return Curl_vquic_flush(cf, data, qctx); |
534 | | } |
535 | | |
536 | | #if (defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG)) && \ |
537 | | !defined(HAVE_APPLE_MSG_X) |
538 | | static size_t vquic_msghdr_get_udp_gro(struct msghdr *msg) |
539 | | { |
540 | | int gso_size = 0; |
541 | | #if defined(__linux__) && defined(UDP_GRO) |
542 | | struct cmsghdr *cmsg; |
543 | | |
544 | | /* Workaround musl CMSG_NXTHDR issue */ |
545 | | #if defined(__clang__) && !defined(__GLIBC__) |
546 | | #pragma clang diagnostic push |
547 | | #pragma clang diagnostic ignored "-Wsign-compare" |
548 | | #pragma clang diagnostic ignored "-Wcast-align" |
549 | | #endif |
550 | | for(cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
551 | | #if defined(__clang__) && !defined(__GLIBC__) |
552 | | #pragma clang diagnostic pop |
553 | | #endif |
554 | | if(cmsg->cmsg_level == SOL_UDP && cmsg->cmsg_type == UDP_GRO) { |
555 | | memcpy(&gso_size, CMSG_DATA(cmsg), sizeof(gso_size)); |
556 | | |
557 | | break; |
558 | | } |
559 | | } |
560 | | #endif /* linux && UDP_GRO */ |
561 | | (void)msg; |
562 | | |
563 | | return (size_t)gso_size; |
564 | | } |
565 | | #endif /* (HAVE_SENDMMSG || HAVE_SENDMSG) && !HAVE_APPLE_MSG_X */ |
566 | | |
567 | | #if (defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG) || \ |
568 | | defined(HAVE_APPLE_MSG_X)) && \ |
569 | | (defined(IP_RECVTOS) || defined(IP_TOS)) && defined(IPTOS_ECN_MASK) |
570 | | static uint8_t vquic_msghdr_get_ecn(struct msghdr *msg, int family) |
571 | | { |
572 | | struct cmsghdr *cmsg; |
573 | | switch(family) { |
574 | | case AF_INET: |
575 | | /* Workaround musl CMSG_NXTHDR issue */ |
576 | | #if defined(__clang__) && !defined(__GLIBC__) |
577 | | #pragma clang diagnostic push |
578 | | #pragma clang diagnostic ignored "-Wsign-compare" |
579 | | #pragma clang diagnostic ignored "-Wcast-align" |
580 | | #endif |
581 | | for(cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
582 | | #if defined(__clang__) && !defined(__GLIBC__) |
583 | | #pragma clang diagnostic pop |
584 | | #endif |
585 | | if(cmsg->cmsg_level == IPPROTO_IP && |
586 | | #ifdef __APPLE__ |
587 | | cmsg->cmsg_type == IP_RECVTOS |
588 | | #else |
589 | | cmsg->cmsg_type == IP_TOS |
590 | | #endif |
591 | | && cmsg->cmsg_len) { |
592 | | return *(uint8_t *)(CMSG_DATA(cmsg)) & IPTOS_ECN_MASK; |
593 | | } |
594 | | } |
595 | | break; |
596 | | case AF_INET6: |
597 | | /* Workaround musl CMSG_NXTHDR issue */ |
598 | | #if defined(__clang__) && !defined(__GLIBC__) |
599 | | #pragma clang diagnostic push |
600 | | #pragma clang diagnostic ignored "-Wsign-compare" |
601 | | #pragma clang diagnostic ignored "-Wcast-align" |
602 | | #endif |
603 | | for(cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
604 | | #if defined(__clang__) && !defined(__GLIBC__) |
605 | | #pragma clang diagnostic pop |
606 | | #endif |
607 | | if(cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_TCLASS && |
608 | | cmsg->cmsg_len) { |
609 | | unsigned int tos; |
610 | | |
611 | | memcpy(&tos, CMSG_DATA(cmsg), sizeof(int)); |
612 | | |
613 | | return (uint8_t)(tos & IPTOS_ECN_MASK); |
614 | | } |
615 | | } |
616 | | break; |
617 | | } |
618 | | return 0; |
619 | | } |
620 | | #else |
621 | | #define vquic_msghdr_get_ecn(a, b) 0 |
622 | | #endif /* HAVE_SENDMMSG || HAVE_SENDMSG || HAVE_APPLE_MSG_X ... */ |
623 | | |
624 | | #ifdef HAVE_SENDMMSG |
625 | | |
626 | | static CURLcode recvmmsg_packets(struct Curl_cfilter *cf, |
627 | | struct Curl_easy *data, |
628 | | struct cf_quic_ctx *qctx, |
629 | | size_t max_pkts, |
630 | | Curl_vquic_recv_pkts_cb *recv_cb, void *userp) |
631 | | { |
632 | | #if defined(__linux__) && defined(UDP_GRO) |
633 | | #define MMSG_NUM 16 |
634 | | #define UDP_GRO_CNT_MAX 64 |
635 | | #define CMSG_PER_MSG_SIZE (2 * CMSG_SPACE(sizeof(int))) |
636 | | #else |
637 | | #define MMSG_NUM 64 |
638 | | #define UDP_GRO_CNT_MAX 1 |
639 | | #define CMSG_PER_MSG_SIZE CMSG_SPACE(sizeof(int)) |
640 | | #endif |
641 | | #define MSG_BUF_SIZE (UDP_GRO_CNT_MAX * 1500) |
642 | | struct iovec msg_iov[MMSG_NUM]; |
643 | | struct mmsghdr mmsg[MMSG_NUM]; |
644 | | uint8_t msg_ctrl[MMSG_NUM * CMSG_PER_MSG_SIZE]; |
645 | | struct sockaddr_storage remote_addr[MMSG_NUM]; |
646 | | size_t total_nread = 0, pkts = 0; |
647 | | #ifdef CURLVERBOSE |
648 | | size_t calls = 0; |
649 | | #endif |
650 | | int mcount, i, n; |
651 | | char errstr[STRERROR_LEN]; |
652 | | CURLcode result = CURLE_OK; |
653 | | size_t gso_size; |
654 | | char *sockbuf = NULL; |
655 | | uint8_t (*bufs)[MSG_BUF_SIZE] = NULL; |
656 | | uint8_t ecn = 0; |
657 | | |
658 | | DEBUGASSERT(max_pkts > 0); |
659 | | result = Curl_multi_xfer_sockbuf_borrow(data, MMSG_NUM * MSG_BUF_SIZE, |
660 | | &sockbuf); |
661 | | if(result) |
662 | | goto out; |
663 | | bufs = (uint8_t (*)[MSG_BUF_SIZE])sockbuf; |
664 | | |
665 | | total_nread = 0; |
666 | | while(pkts < max_pkts) { |
667 | | n = (int)CURLMIN(CURLMIN(MMSG_NUM, IOV_MAX), max_pkts); |
668 | | memset(&mmsg, 0, sizeof(mmsg)); |
669 | | for(i = 0; i < n; ++i) { |
670 | | msg_iov[i].iov_base = bufs[i]; |
671 | | msg_iov[i].iov_len = sizeof(bufs[i]); |
672 | | mmsg[i].msg_hdr.msg_iov = &msg_iov[i]; |
673 | | mmsg[i].msg_hdr.msg_iovlen = 1; |
674 | | mmsg[i].msg_hdr.msg_name = &remote_addr[i]; |
675 | | mmsg[i].msg_hdr.msg_namelen = sizeof(remote_addr[i]); |
676 | | mmsg[i].msg_hdr.msg_control = &msg_ctrl[i * CMSG_PER_MSG_SIZE]; |
677 | | mmsg[i].msg_hdr.msg_controllen = CMSG_PER_MSG_SIZE; |
678 | | } |
679 | | |
680 | | while((mcount = recvmmsg(qctx->sockfd, mmsg, n, 0, NULL)) == -1 && |
681 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
682 | | ; |
683 | | if(mcount == -1) { |
684 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
685 | | CURL_TRC_CF(data, cf, "ingress, recvmmsg -> EAGAIN"); |
686 | | goto out; |
687 | | } |
688 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
689 | | struct ip_quadruple ip; |
690 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
691 | | failf(data, "QUIC: connection to %s port %u refused", |
692 | | ip.remote_ip, ip.remote_port); |
693 | | result = CURLE_COULDNT_CONNECT; |
694 | | goto out; |
695 | | } |
696 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
697 | | failf(data, "QUIC: recvmmsg() unexpectedly returned %d (errno=%d; %s)", |
698 | | mcount, SOCKERRNO, errstr); |
699 | | result = CURLE_RECV_ERROR; |
700 | | goto out; |
701 | | } |
702 | | |
703 | | VERBOSE(++calls); |
704 | | for(i = 0; i < mcount; ++i) { |
705 | | /* A zero-length UDP packet is no QUIC packet. Ignore. */ |
706 | | if(!mmsg[i].msg_len) { |
707 | | ++pkts; |
708 | | continue; |
709 | | } |
710 | | total_nread += mmsg[i].msg_len; |
711 | | |
712 | | ecn = vquic_msghdr_get_ecn(&mmsg[i].msg_hdr, remote_addr[i].ss_family); |
713 | | gso_size = vquic_msghdr_get_udp_gro(&mmsg[i].msg_hdr); |
714 | | if(gso_size == 0) |
715 | | gso_size = mmsg[i].msg_len; |
716 | | |
717 | | result = recv_cb(bufs[i], mmsg[i].msg_len, gso_size, |
718 | | mmsg[i].msg_hdr.msg_name, |
719 | | mmsg[i].msg_hdr.msg_namelen, ecn, userp); |
720 | | if(result) |
721 | | goto out; |
722 | | pkts += (mmsg[i].msg_len + gso_size - 1) / gso_size; |
723 | | } |
724 | | } |
725 | | |
726 | | out: |
727 | | if(total_nread || result) |
728 | | CURL_TRC_CF(data, cf, |
729 | | "vquic_recvmmsg(len=%zu, packets=%zu, calls=%zu) -> %d", |
730 | | total_nread, pkts, calls, (int)result); |
731 | | Curl_multi_xfer_sockbuf_release(data, sockbuf); |
732 | | return result; |
733 | | } |
734 | | |
735 | | #elif defined(HAVE_APPLE_MSG_X) |
736 | | |
737 | | static CURLcode recvmsg_x_packets(struct Curl_cfilter *cf, |
738 | | struct Curl_easy *data, |
739 | | struct cf_quic_ctx *qctx, |
740 | | size_t max_pkts, |
741 | | Curl_vquic_recv_pkts_cb *recv_cb, |
742 | | void *userp) |
743 | | { |
744 | | #define MSG_X_NUM 64 |
745 | | #define MSG_BUF_SIZE (2048) |
746 | | #define CMSG_PER_MSG_SIZE CMSG_SPACE(sizeof(int)) |
747 | | struct iovec msg_iov[MSG_X_NUM]; |
748 | | struct msghdr_x mmsg[MSG_X_NUM]; |
749 | | uint8_t msg_ctrl[MSG_X_NUM * CMSG_PER_MSG_SIZE]; |
750 | | struct sockaddr_storage remote_addr[MSG_X_NUM]; |
751 | | size_t total_nread = 0, pkts = 0; |
752 | | #ifdef CURLVERBOSE |
753 | | size_t calls = 0; |
754 | | #endif |
755 | | int mcount, i; |
756 | | char errstr[STRERROR_LEN]; |
757 | | CURLcode result = CURLE_OK; |
758 | | size_t gso_size; |
759 | | char *sockbuf = NULL; |
760 | | uint8_t (*bufs)[MSG_BUF_SIZE] = NULL; |
761 | | uint8_t ecn = 0; |
762 | | |
763 | | DEBUGASSERT(max_pkts > 0); |
764 | | result = Curl_multi_xfer_sockbuf_borrow(data, MSG_X_NUM * MSG_BUF_SIZE, |
765 | | &sockbuf); |
766 | | if(result) |
767 | | goto out; |
768 | | bufs = (uint8_t (*)[MSG_BUF_SIZE])sockbuf; |
769 | | |
770 | | total_nread = 0; |
771 | | while(pkts < max_pkts) { |
772 | | int n = (int)CURLMIN(CURLMIN(MSG_X_NUM, IOV_MAX), max_pkts); |
773 | | memset(&mmsg, 0, sizeof(mmsg)); |
774 | | for(i = 0; i < n; ++i) { |
775 | | msg_iov[i].iov_base = bufs[i]; |
776 | | msg_iov[i].iov_len = sizeof(bufs[i]); |
777 | | mmsg[i].msg_iov = &msg_iov[i]; |
778 | | mmsg[i].msg_iovlen = 1; |
779 | | mmsg[i].msg_name = &remote_addr[i]; |
780 | | mmsg[i].msg_namelen = sizeof(remote_addr[i]); |
781 | | mmsg[i].msg_control = &msg_ctrl[i * CMSG_PER_MSG_SIZE]; |
782 | | mmsg[i].msg_controllen = CMSG_PER_MSG_SIZE; |
783 | | } |
784 | | |
785 | | #if defined(CURL_HAVE_DIAG) && defined(__APPLE__) |
786 | | #pragma GCC diagnostic push |
787 | | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
788 | | #endif |
789 | | while((mcount = syscall(SYS_recvmsg_x, qctx->sockfd, mmsg, n, 0)) == -1 && |
790 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
791 | | ; |
792 | | #if defined(CURL_HAVE_DIAG) && defined(__APPLE__) |
793 | | #pragma GCC diagnostic pop |
794 | | #endif |
795 | | if(mcount == -1) { |
796 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
797 | | CURL_TRC_CF(data, cf, "ingress, recvmsg_x -> EAGAIN"); |
798 | | goto out; |
799 | | } |
800 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
801 | | struct ip_quadruple ip; |
802 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
803 | | failf(data, "QUIC: connection to %s port %u refused", |
804 | | ip.remote_ip, ip.remote_port); |
805 | | result = CURLE_COULDNT_CONNECT; |
806 | | goto out; |
807 | | } |
808 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
809 | | failf(data, "QUIC: recvmsg_x() unexpectedly returned %d (errno=%d; %s)", |
810 | | mcount, SOCKERRNO, errstr); |
811 | | result = CURLE_RECV_ERROR; |
812 | | goto out; |
813 | | } |
814 | | |
815 | | VERBOSE(++calls); |
816 | | for(i = 0; i < mcount; ++i) { |
817 | | /* A zero-length UDP packet is no QUIC packet. Ignore. */ |
818 | | if(!mmsg[i].msg_datalen) { |
819 | | ++pkts; |
820 | | continue; |
821 | | } |
822 | | total_nread += mmsg[i].msg_datalen; |
823 | | ecn = vquic_msghdr_get_ecn((struct msghdr *)&mmsg[i], |
824 | | remote_addr[i].ss_family); |
825 | | gso_size = mmsg[i].msg_datalen; |
826 | | |
827 | | result = recv_cb(bufs[i], mmsg[i].msg_datalen, gso_size, |
828 | | mmsg[i].msg_name, |
829 | | mmsg[i].msg_namelen, ecn, userp); |
830 | | if(result) |
831 | | goto out; |
832 | | pkts += (mmsg[i].msg_datalen + gso_size - 1) / gso_size; |
833 | | } |
834 | | } |
835 | | |
836 | | out: |
837 | | if(total_nread || result) |
838 | | CURL_TRC_CF(data, cf, |
839 | | "vquic_recvmsg_x(len=%zu, packets=%zu, calls=%zu) -> %d", |
840 | | total_nread, pkts, calls, (int)result); |
841 | | Curl_multi_xfer_sockbuf_release(data, sockbuf); |
842 | | return result; |
843 | | } |
844 | | |
845 | | #elif defined(HAVE_SENDMSG) |
846 | | static CURLcode recvmsg_packets(struct Curl_cfilter *cf, |
847 | | struct Curl_easy *data, |
848 | | struct cf_quic_ctx *qctx, |
849 | | size_t max_pkts, |
850 | | Curl_vquic_recv_pkts_cb *recv_cb, void *userp) |
851 | | { |
852 | | #define CMSG_PER_MSG_SIZE CMSG_SPACE(sizeof(int)) |
853 | | struct iovec msg_iov; |
854 | | struct msghdr msg; |
855 | | uint8_t buf[64 * 1024]; |
856 | | struct sockaddr_storage remote_addr; |
857 | | size_t total_nread, pkts, calls; |
858 | | ssize_t rc; |
859 | | size_t nread; |
860 | | char errstr[STRERROR_LEN]; |
861 | | CURLcode result = CURLE_OK; |
862 | | uint8_t msg_ctrl[CMSG_PER_MSG_SIZE]; |
863 | | size_t gso_size; |
864 | | uint8_t ecn = 0; |
865 | | |
866 | | DEBUGASSERT(max_pkts > 0); |
867 | | for(pkts = 0, total_nread = 0, calls = 0; pkts < max_pkts;) { |
868 | | /* fully initialize this on each call to `recvmsg()`. There seem to |
869 | | * operating systems out there that mess with `msg_iov.iov_len`. */ |
870 | | memset(&msg, 0, sizeof(msg)); |
871 | | msg_iov.iov_base = buf; |
872 | | msg_iov.iov_len = sizeof(buf); |
873 | | msg.msg_iov = &msg_iov; |
874 | | msg.msg_iovlen = 1; |
875 | | msg.msg_control = msg_ctrl; |
876 | | msg.msg_name = &remote_addr; |
877 | | msg.msg_namelen = sizeof(remote_addr); |
878 | | msg.msg_controllen = CMSG_PER_MSG_SIZE; |
879 | | |
880 | | while((rc = recvmsg(qctx->sockfd, &msg, 0)) == -1 && |
881 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
882 | | ; |
883 | | if(!curlx_sztouz(rc, &nread)) { |
884 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
885 | | goto out; |
886 | | } |
887 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
888 | | struct ip_quadruple ip; |
889 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
890 | | failf(data, "QUIC: connection to %s port %u refused", |
891 | | ip.remote_ip, ip.remote_port); |
892 | | result = CURLE_COULDNT_CONNECT; |
893 | | goto out; |
894 | | } |
895 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
896 | | failf(data, "QUIC: recvmsg() unexpectedly returned %zd (errno=%d; %s)", |
897 | | rc, SOCKERRNO, errstr); |
898 | | result = CURLE_RECV_ERROR; |
899 | | goto out; |
900 | | } |
901 | | |
902 | | total_nread += nread; |
903 | | ++calls; |
904 | | |
905 | | /* A 0-length UDP packet is no QUIC packet */ |
906 | | if(!nread) { |
907 | | ++pkts; |
908 | | continue; |
909 | | } |
910 | | |
911 | | ecn = vquic_msghdr_get_ecn(&msg, remote_addr.ss_family); |
912 | | gso_size = vquic_msghdr_get_udp_gro(&msg); |
913 | | if(gso_size == 0) |
914 | | gso_size = nread; |
915 | | |
916 | | result = recv_cb(buf, nread, gso_size, |
917 | | msg.msg_name, msg.msg_namelen, ecn, userp); |
918 | | if(result) |
919 | | goto out; |
920 | | pkts += (nread + gso_size - 1) / gso_size; |
921 | | } |
922 | | |
923 | | out: |
924 | | if(total_nread || result) |
925 | | CURL_TRC_CF(data, cf, |
926 | | "vquic_recvmsg(len=%zu, packets=%zu, calls=%zu) -> %d", |
927 | | total_nread, pkts, calls, (int)result); |
928 | | return result; |
929 | | } |
930 | | |
931 | | #else /* HAVE_SENDMMSG || HAVE_SENDMSG */ |
932 | | static CURLcode recvfrom_packets(struct Curl_cfilter *cf, |
933 | | struct Curl_easy *data, |
934 | | struct cf_quic_ctx *qctx, |
935 | | size_t max_pkts, |
936 | | Curl_vquic_recv_pkts_cb *recv_cb, void *userp) |
937 | | { |
938 | | uint8_t buf[64 * 1024]; |
939 | | int bufsize = (int)sizeof(buf); |
940 | | struct sockaddr_storage remote_addr; |
941 | | socklen_t remote_addrlen = sizeof(remote_addr); |
942 | | size_t total_nread, pkts, calls = 0, nread; |
943 | | ssize_t rv; |
944 | | char errstr[STRERROR_LEN]; |
945 | | CURLcode result = CURLE_OK; |
946 | | |
947 | | DEBUGASSERT(max_pkts > 0); |
948 | | for(pkts = 0, total_nread = 0; pkts < max_pkts;) { |
949 | | while((rv = recvfrom(qctx->sockfd, (char *)buf, bufsize, 0, |
950 | | (struct sockaddr *)&remote_addr, |
951 | | &remote_addrlen)) == -1 && |
952 | | (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE)) |
953 | | ; |
954 | | if(!curlx_sztouz(rv, &nread)) { |
955 | | if(SOCK_EAGAIN(SOCKERRNO)) { |
956 | | CURL_TRC_CF(data, cf, "ingress, recvfrom -> EAGAIN"); |
957 | | goto out; |
958 | | } |
959 | | if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) { |
960 | | struct ip_quadruple ip; |
961 | | if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip)) |
962 | | failf(data, "QUIC: connection to %s port %u refused", |
963 | | ip.remote_ip, ip.remote_port); |
964 | | result = CURLE_COULDNT_CONNECT; |
965 | | goto out; |
966 | | } |
967 | | curlx_strerror(SOCKERRNO, errstr, sizeof(errstr)); |
968 | | failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)", |
969 | | rv, SOCKERRNO, errstr); |
970 | | result = CURLE_RECV_ERROR; |
971 | | goto out; |
972 | | } |
973 | | |
974 | | ++pkts; |
975 | | ++calls; |
976 | | |
977 | | /* A 0-length UDP packet is no QUIC packet */ |
978 | | if(!nread) |
979 | | continue; |
980 | | |
981 | | total_nread += nread; |
982 | | result = recv_cb(buf, nread, nread, &remote_addr, remote_addrlen, |
983 | | 0, userp); |
984 | | if(result) |
985 | | goto out; |
986 | | } |
987 | | |
988 | | out: |
989 | | if(total_nread || result) |
990 | | CURL_TRC_CF(data, cf, |
991 | | "vquic_recvfrom(len=%zu, packets=%zu, calls=%zu) -> %d", |
992 | | total_nread, pkts, calls, (int)result); |
993 | | return result; |
994 | | } |
995 | | #endif /* !HAVE_SENDMMSG && !HAVE_SENDMSG */ |
996 | | |
997 | | CURLcode Curl_vquic_recv_packets(struct Curl_cfilter *cf, |
998 | | struct Curl_easy *data, |
999 | | struct cf_quic_ctx *qctx, |
1000 | | size_t max_pkts, |
1001 | | Curl_vquic_recv_pkts_cb *recv_cb, void *userp) |
1002 | | { |
1003 | | CURLcode result; |
1004 | | #ifdef HAVE_SENDMMSG |
1005 | | result = recvmmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
1006 | | #elif defined(HAVE_APPLE_MSG_X) |
1007 | | result = recvmsg_x_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
1008 | | #elif defined(HAVE_SENDMSG) |
1009 | | result = recvmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
1010 | | #else |
1011 | | result = recvfrom_packets(cf, data, qctx, max_pkts, recv_cb, userp); |
1012 | | #endif |
1013 | | if(!result) { |
1014 | | if(!qctx->got_first_byte) { |
1015 | | qctx->got_first_byte = TRUE; |
1016 | | qctx->first_byte_at = qctx->last_op; |
1017 | | } |
1018 | | qctx->last_io = qctx->last_op; |
1019 | | } |
1020 | | return result; |
1021 | | } |
1022 | | |
1023 | | /* |
1024 | | * If the QLOGDIR environment variable is set, open and return a file |
1025 | | * descriptor to write the log to. |
1026 | | * |
1027 | | * This function returns error if something failed outside of failing to |
1028 | | * create the file. Open file success is deemed by seeing if the returned fd |
1029 | | * is != -1. |
1030 | | */ |
1031 | | CURLcode Curl_qlogdir(struct Curl_easy *data, |
1032 | | unsigned char *scid, |
1033 | | size_t scidlen, |
1034 | | int *qlogfdp) |
1035 | | { |
1036 | | char *qlog_dir = curl_getenv("QLOGDIR"); |
1037 | | *qlogfdp = -1; |
1038 | | if(qlog_dir) { |
1039 | | struct dynbuf fname; |
1040 | | CURLcode result; |
1041 | | unsigned int i; |
1042 | | curlx_dyn_init(&fname, DYN_QLOG_NAME); |
1043 | | result = curlx_dyn_add(&fname, qlog_dir); |
1044 | | if(!result) |
1045 | | result = curlx_dyn_add(&fname, "/"); |
1046 | | for(i = 0; (i < scidlen) && !result; i++) { |
1047 | | char hex[3]; |
1048 | | curl_msnprintf(hex, 3, "%02x", scid[i]); |
1049 | | result = curlx_dyn_add(&fname, hex); |
1050 | | } |
1051 | | if(!result) |
1052 | | result = curlx_dyn_add(&fname, ".sqlog"); |
1053 | | |
1054 | | if(!result) { |
1055 | | int qlogfd = curlx_open(curlx_dyn_ptr(&fname), |
1056 | | O_WRONLY | O_CREAT | CURL_O_BINARY, |
1057 | | data->set.new_file_perms |
1058 | | #ifdef _WIN32 |
1059 | | & (_S_IREAD | _S_IWRITE) |
1060 | | #endif |
1061 | | ); |
1062 | | if(qlogfd != -1) |
1063 | | *qlogfdp = qlogfd; |
1064 | | } |
1065 | | curlx_dyn_free(&fname); |
1066 | | curlx_free(qlog_dir); |
1067 | | if(result) |
1068 | | return result; |
1069 | | } |
1070 | | |
1071 | | return CURLE_OK; |
1072 | | } |
1073 | | |
1074 | | CURLcode Curl_cf_quic_insert_after(struct Curl_cfilter *cf_at, |
1075 | | struct Curl_easy *data, |
1076 | | struct Curl_peer *origin, |
1077 | | struct Curl_peer *peer) |
1078 | | { |
1079 | | CURLcode result; |
1080 | | |
1081 | | (void)data; /* not used in all cases and compilers are stupid */ |
1082 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
1083 | | result = Curl_cf_ngtcp2_insert_after(cf_at, origin, peer); |
1084 | | #elif defined(USE_QUICHE) |
1085 | | result = Curl_cf_quiche_insert_after(cf_at, origin, peer); |
1086 | | #else |
1087 | | (void)cf_at; |
1088 | | (void)origin; |
1089 | | (void)peer; |
1090 | | result = CURLE_NOT_BUILT_IN; |
1091 | | #endif |
1092 | | |
1093 | | #if defined(USE_HTTPSRR) && defined(USE_ECH) |
1094 | | /* When using ECH, kick off the HTTPS-RR resolve */ |
1095 | | if(!result && (origin->scheme->family == CURLPROTO_HTTP) && |
1096 | | CURLECH_ENABLED(data) && |
1097 | | Curl_ssl_supports(data, SSLSUPP_ECH) && |
1098 | | (data->set.tls_ech != CURLECH_GREASE) && |
1099 | | !CURL_EASY_STR(data, STRING_ECH_CONFIG)) { |
1100 | | result = Curl_conn_dns_add_https_resolve(data, cf_at->conn, |
1101 | | cf_at->sockindex, origin); |
1102 | | } |
1103 | | #endif /* USE_HTTPSRR && USE_ECH */ |
1104 | | return result; |
1105 | | } |
1106 | | |
1107 | | CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf, |
1108 | | struct Curl_easy *data, |
1109 | | struct Curl_peer *origin, |
1110 | | struct Curl_peer *peer, |
1111 | | uint8_t transport_peer, |
1112 | | struct connectdata *conn, |
1113 | | struct Curl_sockaddr_ex *addr, |
1114 | | struct Curl_peer *tunnel_peer, |
1115 | | uint8_t tunnel_transport) |
1116 | | { |
1117 | | (void)transport_peer; |
1118 | | (void)tunnel_transport; |
1119 | | (void)tunnel_peer; |
1120 | | DEBUGASSERT(transport_peer == TRNSPRT_QUIC); |
1121 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
1122 | | return Curl_cf_ngtcp2_create(pcf, data, origin, peer, conn, addr); |
1123 | | #elif defined(USE_QUICHE) |
1124 | | return Curl_cf_quiche_create(pcf, data, origin, peer, conn, addr); |
1125 | | #else |
1126 | | *pcf = NULL; |
1127 | | (void)data; |
1128 | | (void)origin; |
1129 | | (void)peer; |
1130 | | (void)conn; |
1131 | | (void)addr; |
1132 | | (void)tunnel_peer; |
1133 | | (void)tunnel_transport; |
1134 | | return CURLE_NOT_BUILT_IN; |
1135 | | #endif |
1136 | | } |
1137 | | |
1138 | | #if !defined(CURL_DISABLE_PROXY) && defined(USE_PROXY_HTTP3) |
1139 | | |
1140 | | CURLcode Curl_cf_h3_proxy_insert_after(struct Curl_cfilter *cf_at, |
1141 | | struct Curl_easy *data, |
1142 | | struct Curl_peer *origin, |
1143 | | struct Curl_peer *peer, |
1144 | | struct Curl_peer *tunnel_peer, |
1145 | | uint8_t tunnel_transport) |
1146 | | { |
1147 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
1148 | | return Curl_cf_ngtcp2_proxy_insert_after(cf_at, data, origin, peer, |
1149 | | tunnel_peer, tunnel_transport); |
1150 | | #else |
1151 | | (void)cf_at; |
1152 | | (void)data; |
1153 | | (void)origin; |
1154 | | (void)peer; |
1155 | | (void)tunnel_peer; |
1156 | | (void)tunnel_transport; |
1157 | | return CURLE_NOT_BUILT_IN; |
1158 | | #endif |
1159 | | } |
1160 | | |
1161 | | CURLcode Curl_cf_h3_proxy_create(struct Curl_cfilter **pcf, |
1162 | | struct Curl_easy *data, |
1163 | | struct Curl_peer *origin, |
1164 | | struct Curl_peer *peer, |
1165 | | uint8_t transport_peer, |
1166 | | struct connectdata *conn, |
1167 | | struct Curl_sockaddr_ex *addr, |
1168 | | struct Curl_peer *tunnel_peer, |
1169 | | uint8_t tunnel_transport) |
1170 | | { |
1171 | | DEBUGASSERT(transport_peer == TRNSPRT_QUIC); |
1172 | | #if defined(USE_NGTCP2) && defined(USE_NGHTTP3) |
1173 | | return Curl_cf_ngtcp2_proxy_create(pcf, data, origin, peer, transport_peer, |
1174 | | conn, addr, |
1175 | | tunnel_peer, tunnel_transport); |
1176 | | #else |
1177 | | *pcf = NULL; |
1178 | | (void)data; |
1179 | | (void)conn; |
1180 | | (void)addr; |
1181 | | (void)peer; |
1182 | | (void)transport_peer; |
1183 | | (void)tunnel_peer; |
1184 | | (void)tunnel_transport; |
1185 | | return CURLE_NOT_BUILT_IN; |
1186 | | #endif |
1187 | | } |
1188 | | |
1189 | | #endif /* !CURL_DISABLE_PROXY && USE_PROXY_HTTP3 */ |
1190 | | |
1191 | | CURLcode Curl_conn_may_http3(struct Curl_easy *data, |
1192 | | const struct connectdata *conn, |
1193 | | unsigned char transport) |
1194 | | { |
1195 | | if(transport == TRNSPRT_UNIX) { |
1196 | | failf(data, "HTTP/3 cannot be used over UNIX domain sockets"); |
1197 | | return CURLE_QUIC_CONNECT_ERROR; |
1198 | | } |
1199 | | if(!(data->state.origin->scheme->flags & PROTOPT_SSL)) { |
1200 | | failf(data, "HTTP/3 requested for non-HTTPS URL"); |
1201 | | return CURLE_URL_MALFORMAT; |
1202 | | } |
1203 | | #ifndef CURL_DISABLE_PROXY |
1204 | | if(conn->socks_proxy.peer) { |
1205 | | failf(data, "HTTP/3 is not supported over a SOCKS proxy"); |
1206 | | return CURLE_URL_MALFORMAT; |
1207 | | } |
1208 | | #else |
1209 | | (void)conn; |
1210 | | #endif |
1211 | | |
1212 | | return CURLE_OK; |
1213 | | } |
1214 | | |
1215 | | #ifdef CURLVERBOSE |
1216 | | const char *Curl_vquic_h3_err_str(uint64_t error_code) |
1217 | | { |
1218 | | if(error_code <= UINT_MAX) { |
1219 | | switch((unsigned int)error_code) { |
1220 | | case CURL_H3_ERR_NO_ERROR: |
1221 | | return "NO_ERROR"; |
1222 | | case CURL_H3_ERR_GENERAL_PROTOCOL_ERROR: |
1223 | | return "GENERAL_PROTOCOL_ERROR"; |
1224 | | case CURL_H3_ERR_INTERNAL_ERROR: |
1225 | | return "INTERNAL_ERROR"; |
1226 | | case CURL_H3_ERR_STREAM_CREATION_ERROR: |
1227 | | return "STREAM_CREATION_ERROR"; |
1228 | | case CURL_H3_ERR_CLOSED_CRITICAL_STREAM: |
1229 | | return "CLOSED_CRITICAL_STREAM"; |
1230 | | case CURL_H3_ERR_FRAME_UNEXPECTED: |
1231 | | return "FRAME_UNEXPECTED"; |
1232 | | case CURL_H3_ERR_FRAME_ERROR: |
1233 | | return "FRAME_ERROR"; |
1234 | | case CURL_H3_ERR_EXCESSIVE_LOAD: |
1235 | | return "EXCESSIVE_LOAD"; |
1236 | | case CURL_H3_ERR_ID_ERROR: |
1237 | | return "ID_ERROR"; |
1238 | | case CURL_H3_ERR_SETTINGS_ERROR: |
1239 | | return "SETTINGS_ERROR"; |
1240 | | case CURL_H3_ERR_MISSING_SETTINGS: |
1241 | | return "MISSING_SETTINGS"; |
1242 | | case CURL_H3_ERR_REQUEST_REJECTED: |
1243 | | return "REQUEST_REJECTED"; |
1244 | | case CURL_H3_ERR_REQUEST_CANCELLED: |
1245 | | return "REQUEST_CANCELLED"; |
1246 | | case CURL_H3_ERR_REQUEST_INCOMPLETE: |
1247 | | return "REQUEST_INCOMPLETE"; |
1248 | | case CURL_H3_ERR_MESSAGE_ERROR: |
1249 | | return "MESSAGE_ERROR"; |
1250 | | case CURL_H3_ERR_CONNECT_ERROR: |
1251 | | return "CONNECT_ERROR"; |
1252 | | case CURL_H3_ERR_VERSION_FALLBACK: |
1253 | | return "VERSION_FALLBACK"; |
1254 | | default: |
1255 | | break; |
1256 | | } |
1257 | | } |
1258 | | /* RFC 9114 ch. 8.1 + 9, reserved future error codes that are NO_ERROR */ |
1259 | | if((error_code >= 0x21) && !((error_code - 0x21) % 0x1f)) |
1260 | | return "NO_ERROR"; |
1261 | | return "unknown"; |
1262 | | } |
1263 | | #endif /* CURLVERBOSE */ |
1264 | | |
1265 | | #if defined(USE_NGTCP2) || defined(USE_NGHTTP3) |
1266 | | |
1267 | | static void *vquic_ngtcp2_malloc(size_t size, void *user_data) |
1268 | | { |
1269 | | (void)user_data; |
1270 | | return Curl_cmalloc(size); |
1271 | | } |
1272 | | |
1273 | | static void vquic_ngtcp2_free(void *ptr, void *user_data) |
1274 | | { |
1275 | | (void)user_data; |
1276 | | Curl_cfree(ptr); |
1277 | | } |
1278 | | |
1279 | | static void *vquic_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data) |
1280 | | { |
1281 | | (void)user_data; |
1282 | | return Curl_ccalloc(nmemb, size); |
1283 | | } |
1284 | | |
1285 | | static void *vquic_ngtcp2_realloc(void *ptr, size_t size, void *user_data) |
1286 | | { |
1287 | | (void)user_data; |
1288 | | return Curl_crealloc(ptr, size); |
1289 | | } |
1290 | | |
1291 | | #ifdef USE_NGTCP2 |
1292 | | static struct ngtcp2_mem vquic_ngtcp2_mem = { |
1293 | | NULL, |
1294 | | vquic_ngtcp2_malloc, |
1295 | | vquic_ngtcp2_free, |
1296 | | vquic_ngtcp2_calloc, |
1297 | | vquic_ngtcp2_realloc |
1298 | | }; |
1299 | | struct ngtcp2_mem *Curl_ngtcp2_mem(void) |
1300 | | { |
1301 | | return &vquic_ngtcp2_mem; |
1302 | | } |
1303 | | #endif |
1304 | | |
1305 | | #ifdef USE_NGHTTP3 |
1306 | | static struct nghttp3_mem vquic_nghttp3_mem = { |
1307 | | NULL, |
1308 | | vquic_ngtcp2_malloc, |
1309 | | vquic_ngtcp2_free, |
1310 | | vquic_ngtcp2_calloc, |
1311 | | vquic_ngtcp2_realloc |
1312 | | }; |
1313 | | struct nghttp3_mem *Curl_nghttp3_mem(void) |
1314 | | { |
1315 | | return &vquic_nghttp3_mem; |
1316 | | } |
1317 | | #endif |
1318 | | |
1319 | | #endif /* USE_NGTCP2 || USE_NGHTTP3 */ |
1320 | | |
1321 | | #else /* CURL_DISABLE_HTTP || !USE_HTTP3 */ |
1322 | | |
1323 | | CURLcode Curl_conn_may_http3(struct Curl_easy *data, |
1324 | | const struct connectdata *conn, |
1325 | | unsigned char transport) |
1326 | 0 | { |
1327 | 0 | (void)data; |
1328 | 0 | (void)conn; |
1329 | 0 | (void)transport; |
1330 | 0 | DEBUGF(infof(data, "QUIC is not supported in this build")); |
1331 | 0 | return CURLE_NOT_BUILT_IN; |
1332 | 0 | } |
1333 | | |
1334 | | #endif /* !CURL_DISABLE_HTTP && USE_HTTP3 */ |