Coverage Report

Created: 2025-11-09 06:51

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
  while((sent = sendmsg(qctx->sockfd, &msg, 0)) == -1 &&
159
        SOCKERRNO == SOCKEINTR)
160
    ;
161
162
  if(sent == -1) {
163
    switch(SOCKERRNO) {
164
    case EAGAIN:
165
#if EAGAIN != SOCKEWOULDBLOCK
166
    case SOCKEWOULDBLOCK:
167
#endif
168
      return CURLE_AGAIN;
169
    case SOCKEMSGSIZE:
170
      /* UDP datagram is too large; caused by PMTUD. Just let it be lost. */
171
      break;
172
    case EIO:
173
      if(pktlen > gsolen) {
174
        /* GSO failure */
175
        infof(data, "sendmsg() returned %zd (errno %d); disable GSO", sent,
176
              SOCKERRNO);
177
        qctx->no_gso = TRUE;
178
        return send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent);
179
      }
180
      FALLTHROUGH();
181
    default:
182
      failf(data, "sendmsg() returned %zd (errno %d)", sent, SOCKERRNO);
183
      result = CURLE_SEND_ERROR;
184
      goto out;
185
    }
186
  }
187
  else if(pktlen != (size_t)sent) {
188
    failf(data, "sendmsg() sent only %zd/%zu bytes", sent, pktlen);
189
    result = CURLE_SEND_ERROR;
190
    goto out;
191
  }
192
#else
193
  ssize_t sent;
194
  (void)gsolen;
195
196
  *psent = 0;
197
198
  while((sent = CURL_SEND(qctx->sockfd, (const char *)pkt,
199
                          (SEND_TYPE_ARG3)pktlen, 0)) == -1 &&
200
        SOCKERRNO == SOCKEINTR)
201
    ;
202
203
  if(sent == -1) {
204
    if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
205
      result = CURLE_AGAIN;
206
      goto out;
207
    }
208
    else {
209
      failf(data, "send() returned %zd (errno %d)", sent, SOCKERRNO);
210
      if(SOCKERRNO != SOCKEMSGSIZE) {
211
        result = CURLE_SEND_ERROR;
212
        goto out;
213
      }
214
      /* UDP datagram is too large; caused by PMTUD. Just let it be
215
         lost. */
216
    }
217
  }
218
#endif
219
  (void)cf;
220
  *psent = pktlen;
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(rc == -1) {
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
    nread = (size_t)rc;
529
    total_nread += nread;
530
    ++calls;
531
532
    gso_size = vquic_msghdr_get_udp_gro(&msg);
533
    if(gso_size == 0) {
534
      gso_size = nread;
535
    }
536
537
    result = recv_cb(buf, nread, gso_size,
538
                     msg.msg_name, msg.msg_namelen, 0, userp);
539
    if(result)
540
      goto out;
541
    pkts += (nread + gso_size - 1) / gso_size;
542
  }
543
544
out:
545
  if(total_nread || result)
546
    CURL_TRC_CF(data, cf, "vquic_recvmsg(len=%zu, packets=%zu, calls=%zu)"
547
                " -> %d", total_nread, pkts, calls, result);
548
  return result;
549
}
550
551
#else /* HAVE_SENDMMSG || HAVE_SENDMSG */
552
static CURLcode recvfrom_packets(struct Curl_cfilter *cf,
553
                                 struct Curl_easy *data,
554
                                 struct cf_quic_ctx *qctx,
555
                                 size_t max_pkts,
556
                                 vquic_recv_pkts_cb *recv_cb, void *userp)
557
{
558
  uint8_t buf[64*1024];
559
  int bufsize = (int)sizeof(buf);
560
  struct sockaddr_storage remote_addr;
561
  socklen_t remote_addrlen = sizeof(remote_addr);
562
  size_t total_nread, pkts, calls = 0;
563
  ssize_t nread;
564
  char errstr[STRERROR_LEN];
565
  CURLcode result = CURLE_OK;
566
567
  DEBUGASSERT(max_pkts > 0);
568
  for(pkts = 0, total_nread = 0; pkts < max_pkts;) {
569
    while((nread = recvfrom(qctx->sockfd, (char *)buf, bufsize, 0,
570
                            (struct sockaddr *)&remote_addr,
571
                            &remote_addrlen)) == -1 &&
572
          (SOCKERRNO == SOCKEINTR || SOCKERRNO == SOCKEMSGSIZE))
573
      ;
574
    if(nread == -1) {
575
      if(SOCKERRNO == EAGAIN || SOCKERRNO == SOCKEWOULDBLOCK) {
576
        CURL_TRC_CF(data, cf, "ingress, recvfrom -> EAGAIN");
577
        goto out;
578
      }
579
      if(!cf->connected && SOCKERRNO == SOCKECONNREFUSED) {
580
        struct ip_quadruple ip;
581
        if(!Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip))
582
          failf(data, "QUIC: connection to %s port %u refused",
583
                ip.remote_ip, ip.remote_port);
584
        result = CURLE_COULDNT_CONNECT;
585
        goto out;
586
      }
587
      curlx_strerror(SOCKERRNO, errstr, sizeof(errstr));
588
      failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)",
589
                  nread, SOCKERRNO, errstr);
590
      result = CURLE_RECV_ERROR;
591
      goto out;
592
    }
593
594
    ++pkts;
595
    ++calls;
596
    total_nread += (size_t)nread;
597
    result = recv_cb(buf, (size_t)nread, (size_t)nread,
598
                     &remote_addr, remote_addrlen, 0, userp);
599
    if(result)
600
      goto out;
601
  }
602
603
out:
604
  if(total_nread || result)
605
    CURL_TRC_CF(data, cf, "vquic_recvfrom(len=%zu, packets=%zu, calls=%zu)"
606
                " -> %d", total_nread, pkts, calls, result);
607
  return result;
608
}
609
#endif /* !HAVE_SENDMMSG && !HAVE_SENDMSG */
610
611
CURLcode vquic_recv_packets(struct Curl_cfilter *cf,
612
                            struct Curl_easy *data,
613
                            struct cf_quic_ctx *qctx,
614
                            size_t max_pkts,
615
                            vquic_recv_pkts_cb *recv_cb, void *userp)
616
{
617
  CURLcode result;
618
#ifdef HAVE_SENDMMSG
619
  result = recvmmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
620
#elif defined(HAVE_SENDMSG)
621
  result = recvmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
622
#else
623
  result = recvfrom_packets(cf, data, qctx, max_pkts, recv_cb, userp);
624
#endif
625
  if(!result) {
626
    if(!qctx->got_first_byte) {
627
      qctx->got_first_byte = TRUE;
628
      qctx->first_byte_at = qctx->last_op;
629
    }
630
    qctx->last_io = qctx->last_op;
631
  }
632
  return result;
633
}
634
635
/*
636
 * If the QLOGDIR environment variable is set, open and return a file
637
 * descriptor to write the log to.
638
 *
639
 * This function returns error if something failed outside of failing to
640
 * create the file. Open file success is deemed by seeing if the returned fd
641
 * is != -1.
642
 */
643
CURLcode Curl_qlogdir(struct Curl_easy *data,
644
                      unsigned char *scid,
645
                      size_t scidlen,
646
                      int *qlogfdp)
647
{
648
  char *qlog_dir = curl_getenv("QLOGDIR");
649
  *qlogfdp = -1;
650
  if(qlog_dir) {
651
    struct dynbuf fname;
652
    CURLcode result;
653
    unsigned int i;
654
    curlx_dyn_init(&fname, DYN_QLOG_NAME);
655
    result = curlx_dyn_add(&fname, qlog_dir);
656
    if(!result)
657
      result = curlx_dyn_add(&fname, "/");
658
    for(i = 0; (i < scidlen) && !result; i++) {
659
      char hex[3];
660
      curl_msnprintf(hex, 3, "%02x", scid[i]);
661
      result = curlx_dyn_add(&fname, hex);
662
    }
663
    if(!result)
664
      result = curlx_dyn_add(&fname, ".sqlog");
665
666
    if(!result) {
667
      int qlogfd = curlx_open(curlx_dyn_ptr(&fname),
668
                              O_WRONLY | O_CREAT | CURL_O_BINARY,
669
                              data->set.new_file_perms);
670
      if(qlogfd != -1)
671
        *qlogfdp = qlogfd;
672
    }
673
    curlx_dyn_free(&fname);
674
    free(qlog_dir);
675
    if(result)
676
      return result;
677
  }
678
679
  return CURLE_OK;
680
}
681
682
CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf,
683
                             struct Curl_easy *data,
684
                             struct connectdata *conn,
685
                             const struct Curl_addrinfo *ai,
686
                             int transport)
687
{
688
  (void)transport;
689
  DEBUGASSERT(transport == TRNSPRT_QUIC);
690
#if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
691
  return Curl_cf_ngtcp2_create(pcf, data, conn, ai);
692
#elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
693
  return Curl_cf_osslq_create(pcf, data, conn, ai);
694
#elif defined(USE_QUICHE)
695
  return Curl_cf_quiche_create(pcf, data, conn, ai);
696
#else
697
  *pcf = NULL;
698
  (void)data;
699
  (void)conn;
700
  (void)ai;
701
  return CURLE_NOT_BUILT_IN;
702
#endif
703
}
704
705
CURLcode Curl_conn_may_http3(struct Curl_easy *data,
706
                             const struct connectdata *conn,
707
                             unsigned char transport)
708
{
709
  if(transport == TRNSPRT_UNIX) {
710
    /* cannot do QUIC over a Unix domain socket */
711
    return CURLE_QUIC_CONNECT_ERROR;
712
  }
713
  if(!(conn->handler->flags & PROTOPT_SSL)) {
714
    failf(data, "HTTP/3 requested for non-HTTPS URL");
715
    return CURLE_URL_MALFORMAT;
716
  }
717
#ifndef CURL_DISABLE_PROXY
718
  if(conn->bits.socksproxy) {
719
    failf(data, "HTTP/3 is not supported over a SOCKS proxy");
720
    return CURLE_URL_MALFORMAT;
721
  }
722
  if(conn->bits.httpproxy && conn->bits.tunnel_proxy) {
723
    failf(data, "HTTP/3 is not supported over an HTTP proxy");
724
    return CURLE_URL_MALFORMAT;
725
  }
726
#endif
727
728
  return CURLE_OK;
729
}
730
731
#if defined(USE_NGTCP2) || defined(USE_NGHTTP3)
732
733
static void *vquic_ngtcp2_malloc(size_t size, void *user_data)
734
{
735
  (void)user_data;
736
  return Curl_cmalloc(size);
737
}
738
739
static void vquic_ngtcp2_free(void *ptr, void *user_data)
740
{
741
  (void)user_data;
742
  Curl_cfree(ptr);
743
}
744
745
static void *vquic_ngtcp2_calloc(size_t nmemb, size_t size, void *user_data)
746
{
747
  (void)user_data;
748
  return Curl_ccalloc(nmemb, size);
749
}
750
751
static void *vquic_ngtcp2_realloc(void *ptr, size_t size, void *user_data)
752
{
753
  (void)user_data;
754
  return Curl_crealloc(ptr, size);
755
}
756
757
#ifdef USE_NGTCP2
758
static struct ngtcp2_mem vquic_ngtcp2_mem = {
759
  NULL,
760
  vquic_ngtcp2_malloc,
761
  vquic_ngtcp2_free,
762
  vquic_ngtcp2_calloc,
763
  vquic_ngtcp2_realloc
764
};
765
struct ngtcp2_mem *Curl_ngtcp2_mem(void)
766
{
767
  return &vquic_ngtcp2_mem;
768
}
769
#endif
770
771
#ifdef USE_NGHTTP3
772
static struct nghttp3_mem vquic_nghttp3_mem = {
773
  NULL,
774
  vquic_ngtcp2_malloc,
775
  vquic_ngtcp2_free,
776
  vquic_ngtcp2_calloc,
777
  vquic_ngtcp2_realloc
778
};
779
struct nghttp3_mem *Curl_nghttp3_mem(void)
780
{
781
  return &vquic_nghttp3_mem;
782
}
783
#endif
784
785
#endif /* USE_NGTCP2 || USE_NGHTTP3 */
786
787
#else /* CURL_DISABLE_HTTP || !USE_HTTP3 */
788
789
CURLcode Curl_conn_may_http3(struct Curl_easy *data,
790
                             const struct connectdata *conn,
791
                             unsigned char transport)
792
0
{
793
0
  (void)conn;
794
0
  (void)data;
795
0
  (void)transport;
796
0
  DEBUGF(infof(data, "QUIC is not supported in this build"));
797
0
  return CURLE_NOT_BUILT_IN;
798
0
}
799
800
#endif /* !CURL_DISABLE_HTTP && USE_HTTP3 */