Coverage Report

Created: 2025-11-23 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
25
#include "../curl_setup.h"
26
27
#ifdef HAVE_NETINET_UDP_H
28
#include <netinet/udp.h>
29
#endif
30
#ifdef USE_NGHTTP3
31
#include <nghttp3/nghttp3.h>
32
#endif
33
#include "../urldata.h"
34
#include "../bufq.h"
35
#include "../curlx/dynbuf.h"
36
#include "../curlx/fopen.h"
37
#include "../curlx/warnless.h"
38
#include "../cfilters.h"
39
#include "../curl_trc.h"
40
#include "curl_ngtcp2.h"
41
#include "curl_osslq.h"
42
#include "curl_quiche.h"
43
#include "../multiif.h"
44
#include "../rand.h"
45
#include "vquic.h"
46
#include "vquic_int.h"
47
#include "../curlx/strerr.h"
48
#include "../curlx/strparse.h"
49
50
/* The last 2 #include files should be in this order */
51
#include "../curl_memory.h"
52
#include "../memdebug.h"
53
54
55
#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3)
56
57
#define NW_CHUNK_SIZE     (64 * 1024)
58
#define NW_SEND_CHUNKS    1
59
60
61
int Curl_vquic_init(void)
62
{
63
#if defined(USE_NGTCP2) && defined(OPENSSL_QUIC_API2)
64
  if(ngtcp2_crypto_ossl_init())
65
    return 0;
66
#endif
67
68
  return 1;
69
}
70
71
void Curl_quic_ver(char *p, size_t len)
72
{
73
#if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
74
  Curl_ngtcp2_ver(p, len);
75
#elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
76
  Curl_osslq_ver(p, len);
77
#elif defined(USE_QUICHE)
78
  Curl_quiche_ver(p, len);
79
#endif
80
}
81
82
CURLcode vquic_ctx_init(struct cf_quic_ctx *qctx)
83
{
84
  Curl_bufq_init2(&qctx->sendbuf, NW_CHUNK_SIZE, NW_SEND_CHUNKS,
85
                  BUFQ_OPT_SOFT_LIMIT);
86
#if defined(__linux__) && defined(UDP_SEGMENT) && defined(HAVE_SENDMSG)
87
  qctx->no_gso = FALSE;
88
#else
89
  qctx->no_gso = TRUE;
90
#endif
91
#ifdef DEBUGBUILD
92
  {
93
    const char *p = getenv("CURL_DBG_QUIC_WBLOCK");
94
    if(p) {
95
      curl_off_t l;
96
      if(!curlx_str_number(&p, &l, 100))
97
        qctx->wblock_percent = (int)l;
98
    }
99
  }
100
#endif
101
  vquic_ctx_update_time(qctx);
102
103
  return CURLE_OK;
104
}
105
106
void vquic_ctx_free(struct cf_quic_ctx *qctx)
107
{
108
  Curl_bufq_free(&qctx->sendbuf);
109
}
110
111
void vquic_ctx_update_time(struct cf_quic_ctx *qctx)
112
{
113
  qctx->last_op = curlx_now();
114
}
115
116
static CURLcode send_packet_no_gso(struct Curl_cfilter *cf,
117
                                   struct Curl_easy *data,
118
                                   struct cf_quic_ctx *qctx,
119
                                   const uint8_t *pkt, size_t pktlen,
120
                                   size_t gsolen, size_t *psent);
121
122
static CURLcode do_sendmsg(struct Curl_cfilter *cf,
123
                           struct Curl_easy *data,
124
                           struct cf_quic_ctx *qctx,
125
                           const uint8_t *pkt, size_t pktlen, size_t gsolen,
126
                           size_t *psent)
127
{
128
  CURLcode result = CURLE_OK;
129
#ifdef HAVE_SENDMSG
130
  struct iovec msg_iov;
131
  struct msghdr msg = {0};
132
  ssize_t rv;
133
#if defined(__linux__) && defined(UDP_SEGMENT)
134
  uint8_t msg_ctrl[32];
135
  struct cmsghdr *cm;
136
#endif
137
138
  *psent = 0;
139
  msg_iov.iov_base = (uint8_t *)CURL_UNCONST(pkt);
140
  msg_iov.iov_len = pktlen;
141
  msg.msg_iov = &msg_iov;
142
  msg.msg_iovlen = 1;
143
144
#if defined(__linux__) && defined(UDP_SEGMENT)
145
  if(pktlen > gsolen) {
146
    /* Only set this, when we need it. macOS, for example,
147
     * does not seem to like a msg_control of length 0. */
148
    msg.msg_control = msg_ctrl;
149
    assert(sizeof(msg_ctrl) >= CMSG_SPACE(sizeof(int)));
150
    msg.msg_controllen = CMSG_SPACE(sizeof(int));
151
    cm = CMSG_FIRSTHDR(&msg);
152
    cm->cmsg_level = SOL_UDP;
153
    cm->cmsg_type = UDP_SEGMENT;
154
    cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
155
    *(uint16_t *)(void *)CMSG_DATA(cm) = gsolen & 0xffff;
156
  }
157
#endif
158
159
  while((rv = sendmsg(qctx->sockfd, &msg, 0)) == -1 &&
160
        SOCKERRNO == SOCKEINTR)
161
    ;
162
163
  if(!curlx_sztouz(rv, psent)) {
164
    switch(SOCKERRNO) {
165
    case EAGAIN:
166
#if EAGAIN != SOCKEWOULDBLOCK
167
    case SOCKEWOULDBLOCK:
168
#endif
169
      return CURLE_AGAIN;
170
    case SOCKEMSGSIZE:
171
      /* UDP datagram is too large; caused by PMTUD. Just let it be lost. */
172
      break;
173
    case EIO:
174
      if(pktlen > gsolen) {
175
        /* GSO failure */
176
        infof(data, "sendmsg() returned %zd (errno %d); disable GSO", rv,
177
              SOCKERRNO);
178
        qctx->no_gso = TRUE;
179
        return send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent);
180
      }
181
      FALLTHROUGH();
182
    default:
183
      failf(data, "sendmsg() returned %zd (errno %d)", rv, SOCKERRNO);
184
      result = CURLE_SEND_ERROR;
185
      goto out;
186
    }
187
  }
188
  else if(pktlen != *psent) {
189
    failf(data, "sendmsg() sent only %zu/%zu bytes", *psent, pktlen);
190
    result = CURLE_SEND_ERROR;
191
    goto out;
192
  }
193
#else
194
  ssize_t rv;
195
  (void)gsolen;
196
197
  *psent = 0;
198
199
  while((rv = CURL_SEND(qctx->sockfd, (const char *)pkt,
200
                        (SEND_TYPE_ARG3)pktlen, 0)) == -1 &&
201
        SOCKERRNO == SOCKEINTR)
202
    ;
203
204
  if(!curlx_sztouz(rv, psent)) {
205
    if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
206
      result = CURLE_AGAIN;
207
      goto out;
208
    }
209
    else {
210
      failf(data, "send() returned %zd (errno %d)", rv, SOCKERRNO);
211
      if(SOCKERRNO != SOCKEMSGSIZE) {
212
        result = CURLE_SEND_ERROR;
213
        goto out;
214
      }
215
      /* UDP datagram is too large; caused by PMTUD. Just let it be
216
         lost. */
217
    }
218
  }
219
#endif
220
  (void)cf;
221
222
out:
223
  return result;
224
}
225
226
#ifdef HAVE_SENDMSG
227
#define VQUIC_SEND_METHOD   "sendmsg"
228
#else
229
#define VQUIC_SEND_METHOD   "send"
230
#endif
231
232
static CURLcode send_packet_no_gso(struct Curl_cfilter *cf,
233
                                   struct Curl_easy *data,
234
                                   struct cf_quic_ctx *qctx,
235
                                   const uint8_t *pkt, size_t pktlen,
236
                                   size_t gsolen, size_t *psent)
237
{
238
  const uint8_t *p, *end = pkt + pktlen;
239
  size_t sent, len, calls = 0;
240
  CURLcode result = CURLE_OK;
241
242
  *psent = 0;
243
244
  for(p = pkt; p < end; p += gsolen) {
245
    len = CURLMIN(gsolen, (size_t)(end - p));
246
    result = do_sendmsg(cf, data, qctx, p, len, len, &sent);
247
    if(result)
248
      goto out;
249
    *psent += sent;
250
    ++calls;
251
  }
252
out:
253
  CURL_TRC_CF(data, cf, "vquic_%s(len=%zu, gso=%zu, calls=%zu)"
254
              " -> %d, sent=%zu",
255
              VQUIC_SEND_METHOD, pktlen, gsolen, calls, result, *psent);
256
  return result;
257
}
258
259
static CURLcode vquic_send_packets(struct Curl_cfilter *cf,
260
                                   struct Curl_easy *data,
261
                                   struct cf_quic_ctx *qctx,
262
                                   const uint8_t *pkt, size_t pktlen,
263
                                   size_t gsolen, size_t *psent)
264
{
265
  CURLcode result;
266
#ifdef DEBUGBUILD
267
  /* simulate network blocking/partial writes */
268
  if(qctx->wblock_percent > 0) {
269
    unsigned char c;
270
    *psent = 0;
271
    Curl_rand(data, &c, 1);
272
    if(c >= ((100-qctx->wblock_percent)*256/100)) {
273
      CURL_TRC_CF(data, cf, "vquic_flush() simulate EWOULDBLOCK");
274
      return CURLE_AGAIN;
275
    }
276
  }
277
#endif
278
  if(qctx->no_gso && pktlen > gsolen) {
279
    result = send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent);
280
  }
281
  else {
282
    result = do_sendmsg(cf, data, qctx, pkt, pktlen, gsolen, psent);
283
    CURL_TRC_CF(data, cf, "vquic_%s(len=%zu, gso=%zu, calls=1)"
284
                " -> %d, sent=%zu",
285
                VQUIC_SEND_METHOD, pktlen, gsolen, result, *psent);
286
  }
287
  if(!result)
288
    qctx->last_io = qctx->last_op;
289
  return result;
290
}
291
292
CURLcode vquic_flush(struct Curl_cfilter *cf, struct Curl_easy *data,
293
                     struct cf_quic_ctx *qctx)
294
{
295
  const unsigned char *buf;
296
  size_t blen, sent;
297
  CURLcode result;
298
  size_t gsolen;
299
300
  while(Curl_bufq_peek(&qctx->sendbuf, &buf, &blen)) {
301
    gsolen = qctx->gsolen;
302
    if(qctx->split_len) {
303
      gsolen = qctx->split_gsolen;
304
      if(blen > qctx->split_len)
305
        blen = qctx->split_len;
306
    }
307
308
    result = vquic_send_packets(cf, data, qctx, buf, blen, gsolen, &sent);
309
    if(result) {
310
      if(result == CURLE_AGAIN) {
311
        Curl_bufq_skip(&qctx->sendbuf, sent);
312
        if(qctx->split_len)
313
          qctx->split_len -= sent;
314
      }
315
      return result;
316
    }
317
    Curl_bufq_skip(&qctx->sendbuf, sent);
318
    if(qctx->split_len)
319
      qctx->split_len -= sent;
320
  }
321
  return CURLE_OK;
322
}
323
324
CURLcode vquic_send(struct Curl_cfilter *cf, struct Curl_easy *data,
325
                    struct cf_quic_ctx *qctx, size_t gsolen)
326
{
327
  qctx->gsolen = gsolen;
328
  return vquic_flush(cf, data, qctx);
329
}
330
331
CURLcode vquic_send_tail_split(struct Curl_cfilter *cf, struct Curl_easy *data,
332
                               struct cf_quic_ctx *qctx, size_t gsolen,
333
                               size_t tail_len, size_t tail_gsolen)
334
{
335
  DEBUGASSERT(Curl_bufq_len(&qctx->sendbuf) > tail_len);
336
  qctx->split_len = Curl_bufq_len(&qctx->sendbuf) - tail_len;
337
  qctx->split_gsolen = gsolen;
338
  qctx->gsolen = tail_gsolen;
339
  CURL_TRC_CF(data, cf, "vquic_send_tail_split: [%zu gso=%zu][%zu gso=%zu]",
340
              qctx->split_len, qctx->split_gsolen,
341
              tail_len, qctx->gsolen);
342
  return vquic_flush(cf, data, qctx);
343
}
344
345
#if defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG)
346
static size_t vquic_msghdr_get_udp_gro(struct msghdr *msg)
347
{
348
  int gso_size = 0;
349
#if defined(__linux__) && defined(UDP_GRO)
350
  struct cmsghdr *cmsg;
351
352
  /* Workaround musl CMSG_NXTHDR issue */
353
#if defined(__clang__) && !defined(__GLIBC__)
354
#pragma clang diagnostic push
355
#pragma clang diagnostic ignored "-Wsign-compare"
356
#pragma clang diagnostic ignored "-Wcast-align"
357
#endif
358
  for(cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
359
#if defined(__clang__) && !defined(__GLIBC__)
360
#pragma clang diagnostic pop
361
#endif
362
    if(cmsg->cmsg_level == SOL_UDP && cmsg->cmsg_type == UDP_GRO) {
363
      memcpy(&gso_size, CMSG_DATA(cmsg), sizeof(gso_size));
364
365
      break;
366
    }
367
  }
368
#endif
369
  (void)msg;
370
371
  return (size_t)gso_size;
372
}
373
#endif
374
375
#ifdef HAVE_SENDMMSG
376
static CURLcode recvmmsg_packets(struct Curl_cfilter *cf,
377
                                 struct Curl_easy *data,
378
                                 struct cf_quic_ctx *qctx,
379
                                 size_t max_pkts,
380
                                 vquic_recv_pkts_cb *recv_cb, void *userp)
381
{
382
#if defined(__linux__) && defined(UDP_GRO)
383
#define MMSG_NUM  16
384
#define UDP_GRO_CNT_MAX  64
385
#else
386
#define MMSG_NUM  64
387
#define UDP_GRO_CNT_MAX  1
388
#endif
389
#define MSG_BUF_SIZE  (UDP_GRO_CNT_MAX * 1500)
390
  struct iovec msg_iov[MMSG_NUM];
391
  struct mmsghdr mmsg[MMSG_NUM];
392
  uint8_t msg_ctrl[MMSG_NUM * CMSG_SPACE(sizeof(int))];
393
  struct sockaddr_storage remote_addr[MMSG_NUM];
394
  size_t total_nread = 0, pkts = 0, calls = 0;
395
  int mcount, i, n;
396
  char errstr[STRERROR_LEN];
397
  CURLcode result = CURLE_OK;
398
  size_t gso_size;
399
  char *sockbuf = NULL;
400
  uint8_t (*bufs)[MSG_BUF_SIZE] = NULL;
401
402
  DEBUGASSERT(max_pkts > 0);
403
  result = Curl_multi_xfer_sockbuf_borrow(data, MMSG_NUM * MSG_BUF_SIZE,
404
                                          &sockbuf);
405
  if(result)
406
    goto out;
407
  bufs = (uint8_t (*)[MSG_BUF_SIZE])sockbuf;
408
409
  total_nread = 0;
410
  while(pkts < max_pkts) {
411
    n = (int)CURLMIN(CURLMIN(MMSG_NUM, IOV_MAX), max_pkts);
412
    memset(&mmsg, 0, sizeof(mmsg));
413
    for(i = 0; i < n; ++i) {
414
      msg_iov[i].iov_base = bufs[i];
415
      msg_iov[i].iov_len = (int)sizeof(bufs[i]);
416
      mmsg[i].msg_hdr.msg_iov = &msg_iov[i];
417
      mmsg[i].msg_hdr.msg_iovlen = 1;
418
      mmsg[i].msg_hdr.msg_name = &remote_addr[i];
419
      mmsg[i].msg_hdr.msg_namelen = sizeof(remote_addr[i]);
420
      mmsg[i].msg_hdr.msg_control = &msg_ctrl[i * CMSG_SPACE(sizeof(int))];
421
      mmsg[i].msg_hdr.msg_controllen = CMSG_SPACE(sizeof(int));
422
    }
423
424
    while((mcount = recvmmsg(qctx->sockfd, mmsg, n, 0, NULL)) == -1 &&
425
          (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE))
426
      ;
427
    if(mcount == -1) {
428
      if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
429
        CURL_TRC_CF(data, cf, "ingress, recvmmsg -> EAGAIN");
430
        goto out;
431
      }
432
      if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) {
433
        struct ip_quadruple ip;
434
        if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip))
435
          failf(data, "QUIC: connection to %s port %u refused",
436
                ip.remote_ip, ip.remote_port);
437
        result = CURLE_COULDNT_CONNECT;
438
        goto out;
439
      }
440
      curlx_strerror(SOCKERRNO, errstr, sizeof(errstr));
441
      failf(data, "QUIC: recvmmsg() unexpectedly returned %d (errno=%d; %s)",
442
                  mcount, SOCKERRNO, errstr);
443
      result = CURLE_RECV_ERROR;
444
      goto out;
445
    }
446
447
    ++calls;
448
    for(i = 0; i < mcount; ++i) {
449
      total_nread += mmsg[i].msg_len;
450
451
      gso_size = vquic_msghdr_get_udp_gro(&mmsg[i].msg_hdr);
452
      if(gso_size == 0) {
453
        gso_size = mmsg[i].msg_len;
454
      }
455
456
      result = recv_cb(bufs[i], mmsg[i].msg_len, gso_size,
457
                       mmsg[i].msg_hdr.msg_name,
458
                       mmsg[i].msg_hdr.msg_namelen, 0, userp);
459
      if(result)
460
        goto out;
461
      pkts += (mmsg[i].msg_len + gso_size - 1) / gso_size;
462
    }
463
  }
464
465
out:
466
  if(total_nread || result)
467
    CURL_TRC_CF(data, cf, "vquic_recvmmsg(len=%zu, packets=%zu, calls=%zu)"
468
                " -> %d", total_nread, pkts, calls, result);
469
  Curl_multi_xfer_sockbuf_release(data, sockbuf);
470
  return result;
471
}
472
473
#elif defined(HAVE_SENDMSG)
474
static CURLcode recvmsg_packets(struct Curl_cfilter *cf,
475
                                struct Curl_easy *data,
476
                                struct cf_quic_ctx *qctx,
477
                                size_t max_pkts,
478
                                vquic_recv_pkts_cb *recv_cb, void *userp)
479
{
480
  struct iovec msg_iov;
481
  struct msghdr msg;
482
  uint8_t buf[64*1024];
483
  struct sockaddr_storage remote_addr;
484
  size_t total_nread, pkts, calls;
485
  ssize_t rc;
486
  size_t nread;
487
  char errstr[STRERROR_LEN];
488
  CURLcode result = CURLE_OK;
489
  uint8_t msg_ctrl[CMSG_SPACE(sizeof(int))];
490
  size_t gso_size;
491
492
  DEBUGASSERT(max_pkts > 0);
493
  for(pkts = 0, total_nread = 0, calls = 0; pkts < max_pkts;) {
494
    /* fully initialise this on each call to `recvmsg()`. There seem to
495
     * operating systems out there that mess with `msg_iov.iov_len`. */
496
    memset(&msg, 0, sizeof(msg));
497
    msg_iov.iov_base = buf;
498
    msg_iov.iov_len = (int)sizeof(buf);
499
    msg.msg_iov = &msg_iov;
500
    msg.msg_iovlen = 1;
501
    msg.msg_control = msg_ctrl;
502
    msg.msg_name = &remote_addr;
503
    msg.msg_namelen = sizeof(remote_addr);
504
    msg.msg_controllen = sizeof(msg_ctrl);
505
506
    while((rc = recvmsg(qctx->sockfd, &msg, 0)) == -1 &&
507
          (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE))
508
      ;
509
    if(!curlx_sztouz(rc, &nread)) {
510
      if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
511
        goto out;
512
      }
513
      if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) {
514
        struct ip_quadruple ip;
515
        if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip))
516
          failf(data, "QUIC: connection to %s port %u refused",
517
                ip.remote_ip, ip.remote_port);
518
        result = CURLE_COULDNT_CONNECT;
519
        goto out;
520
      }
521
      curlx_strerror(SOCKERRNO, errstr, sizeof(errstr));
522
      failf(data, "QUIC: recvmsg() unexpectedly returned %zd (errno=%d; %s)",
523
                  rc, SOCKERRNO, errstr);
524
      result = CURLE_RECV_ERROR;
525
      goto out;
526
    }
527
528
    total_nread += nread;
529
    ++calls;
530
531
    gso_size = vquic_msghdr_get_udp_gro(&msg);
532
    if(gso_size == 0) {
533
      gso_size = nread;
534
    }
535
536
    result = recv_cb(buf, nread, gso_size,
537
                     msg.msg_name, msg.msg_namelen, 0, userp);
538
    if(result)
539
      goto out;
540
    pkts += (nread + gso_size - 1) / gso_size;
541
  }
542
543
out:
544
  if(total_nread || result)
545
    CURL_TRC_CF(data, cf, "vquic_recvmsg(len=%zu, packets=%zu, calls=%zu)"
546
                " -> %d", total_nread, pkts, calls, result);
547
  return result;
548
}
549
550
#else /* HAVE_SENDMMSG || HAVE_SENDMSG */
551
static CURLcode recvfrom_packets(struct Curl_cfilter *cf,
552
                                 struct Curl_easy *data,
553
                                 struct cf_quic_ctx *qctx,
554
                                 size_t max_pkts,
555
                                 vquic_recv_pkts_cb *recv_cb, void *userp)
556
{
557
  uint8_t buf[64*1024];
558
  int bufsize = (int)sizeof(buf);
559
  struct sockaddr_storage remote_addr;
560
  socklen_t remote_addrlen = sizeof(remote_addr);
561
  size_t total_nread, pkts, calls = 0, nread;
562
  ssize_t rv;
563
  char errstr[STRERROR_LEN];
564
  CURLcode result = CURLE_OK;
565
566
  DEBUGASSERT(max_pkts > 0);
567
  for(pkts = 0, total_nread = 0; pkts < max_pkts;) {
568
    while((rv = recvfrom(qctx->sockfd, (char *)buf, bufsize, 0,
569
                         (struct sockaddr *)&remote_addr,
570
                         &remote_addrlen)) == -1 &&
571
          (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE))
572
      ;
573
    if(!curlx_sztouz(rv, &nread)) {
574
      if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
575
        CURL_TRC_CF(data, cf, "ingress, recvfrom -> EAGAIN");
576
        goto out;
577
      }
578
      if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) {
579
        struct ip_quadruple ip;
580
        if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip))
581
          failf(data, "QUIC: connection to %s port %u refused",
582
                ip.remote_ip, ip.remote_port);
583
        result = CURLE_COULDNT_CONNECT;
584
        goto out;
585
      }
586
      curlx_strerror(SOCKERRNO, errstr, sizeof(errstr));
587
      failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)",
588
                  rv, SOCKERRNO, errstr);
589
      result = CURLE_RECV_ERROR;
590
      goto out;
591
    }
592
593
    ++pkts;
594
    ++calls;
595
    total_nread += nread;
596
    result = recv_cb(buf, nread, nread, &remote_addr, remote_addrlen,
597
                     0, userp);
598
    if(result)
599
      goto out;
600
  }
601
602
out:
603
  if(total_nread || result)
604
    CURL_TRC_CF(data, cf, "vquic_recvfrom(len=%zu, packets=%zu, calls=%zu)"
605
                " -> %d", total_nread, pkts, calls, result);
606
  return result;
607
}
608
#endif /* !HAVE_SENDMMSG && !HAVE_SENDMSG */
609
610
CURLcode vquic_recv_packets(struct Curl_cfilter *cf,
611
                            struct Curl_easy *data,
612
                            struct cf_quic_ctx *qctx,
613
                            size_t max_pkts,
614
                            vquic_recv_pkts_cb *recv_cb, void *userp)
615
{
616
  CURLcode result;
617
#ifdef HAVE_SENDMMSG
618
  result = recvmmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
619
#elif defined(HAVE_SENDMSG)
620
  result = recvmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
621
#else
622
  result = recvfrom_packets(cf, data, qctx, max_pkts, recv_cb, userp);
623
#endif
624
  if(!result) {
625
    if(!qctx->got_first_byte) {
626
      qctx->got_first_byte = TRUE;
627
      qctx->first_byte_at = qctx->last_op;
628
    }
629
    qctx->last_io = qctx->last_op;
630
  }
631
  return result;
632
}
633
634
/*
635
 * If the QLOGDIR environment variable is set, open and return a file
636
 * descriptor to write the log to.
637
 *
638
 * This function returns error if something failed outside of failing to
639
 * create the file. Open file success is deemed by seeing if the returned fd
640
 * is != -1.
641
 */
642
CURLcode Curl_qlogdir(struct Curl_easy *data,
643
                      unsigned char *scid,
644
                      size_t scidlen,
645
                      int *qlogfdp)
646
{
647
  char *qlog_dir = curl_getenv("QLOGDIR");
648
  *qlogfdp = -1;
649
  if(qlog_dir) {
650
    struct dynbuf fname;
651
    CURLcode result;
652
    unsigned int i;
653
    curlx_dyn_init(&fname, DYN_QLOG_NAME);
654
    result = curlx_dyn_add(&fname, qlog_dir);
655
    if(!result)
656
      result = curlx_dyn_add(&fname, "/");
657
    for(i = 0; (i < scidlen) && !result; i++) {
658
      char hex[3];
659
      curl_msnprintf(hex, 3, "%02x", scid[i]);
660
      result = curlx_dyn_add(&fname, hex);
661
    }
662
    if(!result)
663
      result = curlx_dyn_add(&fname, ".sqlog");
664
665
    if(!result) {
666
      int qlogfd = curlx_open(curlx_dyn_ptr(&fname),
667
                              O_WRONLY | O_CREAT | CURL_O_BINARY,
668
                              data->set.new_file_perms);
669
      if(qlogfd != -1)
670
        *qlogfdp = qlogfd;
671
    }
672
    curlx_dyn_free(&fname);
673
    free(qlog_dir);
674
    if(result)
675
      return result;
676
  }
677
678
  return CURLE_OK;
679
}
680
681
CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf,
682
                             struct Curl_easy *data,
683
                             struct connectdata *conn,
684
                             const struct Curl_addrinfo *ai,
685
                             int transport)
686
{
687
  (void)transport;
688
  DEBUGASSERT(transport == TRNSPRT_QUIC);
689
#if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
690
  return Curl_cf_ngtcp2_create(pcf, data, conn, ai);
691
#elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
692
  return Curl_cf_osslq_create(pcf, data, conn, ai);
693
#elif defined(USE_QUICHE)
694
  return Curl_cf_quiche_create(pcf, data, conn, ai);
695
#else
696
  *pcf = NULL;
697
  (void)data;
698
  (void)conn;
699
  (void)ai;
700
  return CURLE_NOT_BUILT_IN;
701
#endif
702
}
703
704
CURLcode Curl_conn_may_http3(struct Curl_easy *data,
705
                             const struct connectdata *conn,
706
                             unsigned char transport)
707
{
708
  if(transport == TRNSPRT_UNIX) {
709
    /* cannot do QUIC over a Unix domain socket */
710
    return CURLE_QUIC_CONNECT_ERROR;
711
  }
712
  if(!(conn->handler->flags & PROTOPT_SSL)) {
713
    failf(data, "HTTP/3 requested for non-HTTPS URL");
714
    return CURLE_URL_MALFORMAT;
715
  }
716
#ifndef CURL_DISABLE_PROXY
717
  if(conn->bits.socksproxy) {
718
    failf(data, "HTTP/3 is not supported over a SOCKS proxy");
719
    return CURLE_URL_MALFORMAT;
720
  }
721
  if(conn->bits.httpproxy && conn->bits.tunnel_proxy) {
722
    failf(data, "HTTP/3 is not supported over an HTTP proxy");
723
    return CURLE_URL_MALFORMAT;
724
  }
725
#endif
726
727
  return CURLE_OK;
728
}
729
730
#if defined(USE_NGTCP2) || defined(USE_NGHTTP3)
731
732
static void *vquic_ngtcp2_malloc(size_t size, void *user_data)
733
{
734
  (void)user_data;
735
  return Curl_cmalloc(size);
736
}
737
738
static void vquic_ngtcp2_free(void *ptr, void *user_data)
739
{
740
  (void)user_data;
741
  Curl_cfree(ptr);
742
}
743
744
static void *vquic_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data)
745
{
746
  (void)user_data;
747
  return Curl_ccalloc(nmemb, size);
748
}
749
750
static void *vquic_ngtcp2_realloc(void *ptr, size_t size, void *user_data)
751
{
752
  (void)user_data;
753
  return Curl_crealloc(ptr, size);
754
}
755
756
#ifdef USE_NGTCP2
757
static struct ngtcp2_mem vquic_ngtcp2_mem = {
758
  NULL,
759
  vquic_ngtcp2_malloc,
760
  vquic_ngtcp2_free,
761
  vquic_ngtcp2_calloc,
762
  vquic_ngtcp2_realloc
763
};
764
struct ngtcp2_mem *Curl_ngtcp2_mem(void)
765
{
766
  return &vquic_ngtcp2_mem;
767
}
768
#endif
769
770
#ifdef USE_NGHTTP3
771
static struct nghttp3_mem vquic_nghttp3_mem = {
772
  NULL,
773
  vquic_ngtcp2_malloc,
774
  vquic_ngtcp2_free,
775
  vquic_ngtcp2_calloc,
776
  vquic_ngtcp2_realloc
777
};
778
struct nghttp3_mem *Curl_nghttp3_mem(void)
779
{
780
  return &vquic_nghttp3_mem;
781
}
782
#endif
783
784
#endif /* USE_NGTCP2 || USE_NGHTTP3 */
785
786
#else /* CURL_DISABLE_HTTP || !USE_HTTP3 */
787
788
CURLcode Curl_conn_may_http3(struct Curl_easy *data,
789
                             const struct connectdata *conn,
790
                             unsigned char transport)
791
0
{
792
0
  (void)conn;
793
0
  (void)data;
794
0
  (void)transport;
795
0
  DEBUGF(infof(data, "QUIC is not supported in this build"));
796
0
  return CURLE_NOT_BUILT_IN;
797
0
}
798
799
#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 */