Coverage Report

Created: 2025-10-30 06:17

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