xref: /curl/lib/vquic/vquic.c (revision 49f83c30)
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 /* WIP, experimental: use recvmmsg() on linux
26  * we have no configure check, yet
27  * and also it is only available for _GNU_SOURCE, which
28  * we do not use otherwise.
29 #define HAVE_SENDMMSG
30  */
31 #if defined(HAVE_SENDMMSG)
32 #define _GNU_SOURCE
33 #include <sys/socket.h>
34 #undef _GNU_SOURCE
35 #endif
36 
37 #include "curl_setup.h"
38 
39 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42 #include "urldata.h"
43 #include "bufq.h"
44 #include "dynbuf.h"
45 #include "cfilters.h"
46 #include "curl_trc.h"
47 #include "curl_msh3.h"
48 #include "curl_ngtcp2.h"
49 #include "curl_osslq.h"
50 #include "curl_quiche.h"
51 #include "rand.h"
52 #include "vquic.h"
53 #include "vquic_int.h"
54 #include "strerror.h"
55 
56 /* The last 3 #include files should be in this order */
57 #include "curl_printf.h"
58 #include "curl_memory.h"
59 #include "memdebug.h"
60 
61 
62 #ifdef USE_HTTP3
63 
64 #ifdef O_BINARY
65 #define QLOGMODE O_WRONLY|O_CREAT|O_BINARY
66 #else
67 #define QLOGMODE O_WRONLY|O_CREAT
68 #endif
69 
70 #define NW_CHUNK_SIZE     (64 * 1024)
71 #define NW_SEND_CHUNKS    2
72 
73 
Curl_quic_ver(char * p,size_t len)74 void Curl_quic_ver(char *p, size_t len)
75 {
76 #if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
77   Curl_ngtcp2_ver(p, len);
78 #elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
79   Curl_osslq_ver(p, len);
80 #elif defined(USE_QUICHE)
81   Curl_quiche_ver(p, len);
82 #elif defined(USE_MSH3)
83   Curl_msh3_ver(p, len);
84 #endif
85 }
86 
vquic_ctx_init(struct cf_quic_ctx * qctx)87 CURLcode vquic_ctx_init(struct cf_quic_ctx *qctx)
88 {
89   Curl_bufq_init2(&qctx->sendbuf, NW_CHUNK_SIZE, NW_SEND_CHUNKS,
90                   BUFQ_OPT_SOFT_LIMIT);
91 #if defined(__linux__) && defined(UDP_SEGMENT) && defined(HAVE_SENDMSG)
92   qctx->no_gso = FALSE;
93 #else
94   qctx->no_gso = TRUE;
95 #endif
96 #ifdef DEBUGBUILD
97   {
98     char *p = getenv("CURL_DBG_QUIC_WBLOCK");
99     if(p) {
100       long l = strtol(p, NULL, 10);
101       if(l >= 0 && l <= 100)
102         qctx->wblock_percent = (int)l;
103     }
104   }
105 #endif
106   vquic_ctx_update_time(qctx);
107 
108   return CURLE_OK;
109 }
110 
vquic_ctx_free(struct cf_quic_ctx * qctx)111 void vquic_ctx_free(struct cf_quic_ctx *qctx)
112 {
113   Curl_bufq_free(&qctx->sendbuf);
114 }
115 
vquic_ctx_update_time(struct cf_quic_ctx * qctx)116 void vquic_ctx_update_time(struct cf_quic_ctx *qctx)
117 {
118   qctx->last_op = Curl_now();
119 }
120 
121 static CURLcode send_packet_no_gso(struct Curl_cfilter *cf,
122                                    struct Curl_easy *data,
123                                    struct cf_quic_ctx *qctx,
124                                    const uint8_t *pkt, size_t pktlen,
125                                    size_t gsolen, size_t *psent);
126 
do_sendmsg(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,const uint8_t * pkt,size_t pktlen,size_t gsolen,size_t * psent)127 static CURLcode do_sendmsg(struct Curl_cfilter *cf,
128                            struct Curl_easy *data,
129                            struct cf_quic_ctx *qctx,
130                            const uint8_t *pkt, size_t pktlen, size_t gsolen,
131                            size_t *psent)
132 {
133 #ifdef HAVE_SENDMSG
134   struct iovec msg_iov;
135   struct msghdr msg = {0};
136   ssize_t sent;
137 #if defined(__linux__) && defined(UDP_SEGMENT)
138   uint8_t msg_ctrl[32];
139   struct cmsghdr *cm;
140 #endif
141 
142   *psent = 0;
143   msg_iov.iov_base = (uint8_t *)pkt;
144   msg_iov.iov_len = pktlen;
145   msg.msg_iov = &msg_iov;
146   msg.msg_iovlen = 1;
147 
148 #if defined(__linux__) && defined(UDP_SEGMENT)
149   if(pktlen > gsolen) {
150     /* Only set this, when we need it. macOS, for example,
151      * does not seem to like a msg_control of length 0. */
152     msg.msg_control = msg_ctrl;
153     assert(sizeof(msg_ctrl) >= CMSG_SPACE(sizeof(uint16_t)));
154     msg.msg_controllen = CMSG_SPACE(sizeof(uint16_t));
155     cm = CMSG_FIRSTHDR(&msg);
156     cm->cmsg_level = SOL_UDP;
157     cm->cmsg_type = UDP_SEGMENT;
158     cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
159     *(uint16_t *)(void *)CMSG_DATA(cm) = gsolen & 0xffff;
160   }
161 #endif
162 
163 
164   while((sent = sendmsg(qctx->sockfd, &msg, 0)) == -1 && SOCKERRNO == EINTR)
165     ;
166 
167   if(sent == -1) {
168     switch(SOCKERRNO) {
169     case EAGAIN:
170 #if EAGAIN != EWOULDBLOCK
171     case EWOULDBLOCK:
172 #endif
173       return CURLE_AGAIN;
174     case EMSGSIZE:
175       /* UDP datagram is too large; caused by PMTUD. Just let it be lost. */
176       break;
177     case EIO:
178       if(pktlen > gsolen) {
179         /* GSO failure */
180         failf(data, "sendmsg() returned %zd (errno %d); disable GSO", sent,
181               SOCKERRNO);
182         qctx->no_gso = TRUE;
183         return send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent);
184       }
185       FALLTHROUGH();
186     default:
187       failf(data, "sendmsg() returned %zd (errno %d)", sent, SOCKERRNO);
188       return CURLE_SEND_ERROR;
189     }
190   }
191   else {
192     assert(pktlen == (size_t)sent);
193   }
194 #else
195   ssize_t sent;
196   (void)gsolen;
197 
198   *psent = 0;
199 
200   while((sent = send(qctx->sockfd,
201                      (const char *)pkt, (SEND_TYPE_ARG3)pktlen, 0)) == -1 &&
202         SOCKERRNO == EINTR)
203     ;
204 
205   if(sent == -1) {
206     if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) {
207       return CURLE_AGAIN;
208     }
209     else {
210       failf(data, "send() returned %zd (errno %d)", sent, SOCKERRNO);
211       if(SOCKERRNO != EMSGSIZE) {
212         return CURLE_SEND_ERROR;
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   return CURLE_OK;
223 }
224 
send_packet_no_gso(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,const uint8_t * pkt,size_t pktlen,size_t gsolen,size_t * psent)225 static CURLcode send_packet_no_gso(struct Curl_cfilter *cf,
226                                    struct Curl_easy *data,
227                                    struct cf_quic_ctx *qctx,
228                                    const uint8_t *pkt, size_t pktlen,
229                                    size_t gsolen, size_t *psent)
230 {
231   const uint8_t *p, *end = pkt + pktlen;
232   size_t sent;
233 
234   *psent = 0;
235 
236   for(p = pkt; p < end; p += gsolen) {
237     size_t len = CURLMIN(gsolen, (size_t)(end - p));
238     CURLcode curlcode = do_sendmsg(cf, data, qctx, p, len, len, &sent);
239     if(curlcode != CURLE_OK) {
240       return curlcode;
241     }
242     *psent += sent;
243   }
244 
245   return CURLE_OK;
246 }
247 
vquic_send_packets(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,const uint8_t * pkt,size_t pktlen,size_t gsolen,size_t * psent)248 static CURLcode vquic_send_packets(struct Curl_cfilter *cf,
249                                    struct Curl_easy *data,
250                                    struct cf_quic_ctx *qctx,
251                                    const uint8_t *pkt, size_t pktlen,
252                                    size_t gsolen, size_t *psent)
253 {
254   CURLcode result;
255 #ifdef DEBUGBUILD
256   /* simulate network blocking/partial writes */
257   if(qctx->wblock_percent > 0) {
258     unsigned char c;
259     Curl_rand(data, &c, 1);
260     if(c >= ((100-qctx->wblock_percent)*256/100)) {
261       CURL_TRC_CF(data, cf, "vquic_flush() simulate EWOULDBLOCK");
262       return CURLE_AGAIN;
263     }
264   }
265 #endif
266   if(qctx->no_gso && pktlen > gsolen) {
267     result = send_packet_no_gso(cf, data, qctx, pkt, pktlen, gsolen, psent);
268   }
269   else {
270     result = do_sendmsg(cf, data, qctx, pkt, pktlen, gsolen, psent);
271   }
272   if(!result)
273     qctx->last_io = qctx->last_op;
274   return result;
275 }
276 
vquic_flush(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx)277 CURLcode vquic_flush(struct Curl_cfilter *cf, struct Curl_easy *data,
278                      struct cf_quic_ctx *qctx)
279 {
280   const unsigned char *buf;
281   size_t blen, sent;
282   CURLcode result;
283   size_t gsolen;
284 
285   while(Curl_bufq_peek(&qctx->sendbuf, &buf, &blen)) {
286     gsolen = qctx->gsolen;
287     if(qctx->split_len) {
288       gsolen = qctx->split_gsolen;
289       if(blen > qctx->split_len)
290         blen = qctx->split_len;
291     }
292 
293     result = vquic_send_packets(cf, data, qctx, buf, blen, gsolen, &sent);
294     CURL_TRC_CF(data, cf, "vquic_send(len=%zu, gso=%zu) -> %d, sent=%zu",
295                 blen, gsolen, result, sent);
296     if(result) {
297       if(result == CURLE_AGAIN) {
298         Curl_bufq_skip(&qctx->sendbuf, sent);
299         if(qctx->split_len)
300           qctx->split_len -= sent;
301       }
302       return result;
303     }
304     Curl_bufq_skip(&qctx->sendbuf, sent);
305     if(qctx->split_len)
306       qctx->split_len -= sent;
307   }
308   return CURLE_OK;
309 }
310 
vquic_send(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t gsolen)311 CURLcode vquic_send(struct Curl_cfilter *cf, struct Curl_easy *data,
312                         struct cf_quic_ctx *qctx, size_t gsolen)
313 {
314   qctx->gsolen = gsolen;
315   return vquic_flush(cf, data, qctx);
316 }
317 
vquic_send_tail_split(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t gsolen,size_t tail_len,size_t tail_gsolen)318 CURLcode vquic_send_tail_split(struct Curl_cfilter *cf, struct Curl_easy *data,
319                                struct cf_quic_ctx *qctx, size_t gsolen,
320                                size_t tail_len, size_t tail_gsolen)
321 {
322   DEBUGASSERT(Curl_bufq_len(&qctx->sendbuf) > tail_len);
323   qctx->split_len = Curl_bufq_len(&qctx->sendbuf) - tail_len;
324   qctx->split_gsolen = gsolen;
325   qctx->gsolen = tail_gsolen;
326   CURL_TRC_CF(data, cf, "vquic_send_tail_split: [%zu gso=%zu][%zu gso=%zu]",
327               qctx->split_len, qctx->split_gsolen,
328               tail_len, qctx->gsolen);
329   return vquic_flush(cf, data, qctx);
330 }
331 
332 #ifdef HAVE_SENDMMSG
recvmmsg_packets(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t max_pkts,vquic_recv_pkt_cb * recv_cb,void * userp)333 static CURLcode recvmmsg_packets(struct Curl_cfilter *cf,
334                                  struct Curl_easy *data,
335                                  struct cf_quic_ctx *qctx,
336                                  size_t max_pkts,
337                                  vquic_recv_pkt_cb *recv_cb, void *userp)
338 {
339 #define MMSG_NUM  64
340   struct iovec msg_iov[MMSG_NUM];
341   struct mmsghdr mmsg[MMSG_NUM];
342   uint8_t bufs[MMSG_NUM][2*1024];
343   struct sockaddr_storage remote_addr[MMSG_NUM];
344   size_t total_nread, pkts;
345   int mcount, i, n;
346   char errstr[STRERROR_LEN];
347   CURLcode result = CURLE_OK;
348 
349   DEBUGASSERT(max_pkts > 0);
350   pkts = 0;
351   total_nread = 0;
352   while(pkts < max_pkts) {
353     n = (int)CURLMIN(MMSG_NUM, max_pkts);
354     memset(&mmsg, 0, sizeof(mmsg));
355     for(i = 0; i < n; ++i) {
356       msg_iov[i].iov_base = bufs[i];
357       msg_iov[i].iov_len = (int)sizeof(bufs[i]);
358       mmsg[i].msg_hdr.msg_iov = &msg_iov[i];
359       mmsg[i].msg_hdr.msg_iovlen = 1;
360       mmsg[i].msg_hdr.msg_name = &remote_addr[i];
361       mmsg[i].msg_hdr.msg_namelen = sizeof(remote_addr[i]);
362     }
363 
364     while((mcount = recvmmsg(qctx->sockfd, mmsg, n, 0, NULL)) == -1 &&
365           SOCKERRNO == EINTR)
366       ;
367     if(mcount == -1) {
368       if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) {
369         CURL_TRC_CF(data, cf, "ingress, recvmmsg -> EAGAIN");
370         goto out;
371       }
372       if(!cf->connected && SOCKERRNO == ECONNREFUSED) {
373         struct ip_quadruple ip;
374         Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
375         failf(data, "QUIC: connection to %s port %u refused",
376               ip.remote_ip, ip.remote_port);
377         result = CURLE_COULDNT_CONNECT;
378         goto out;
379       }
380       Curl_strerror(SOCKERRNO, errstr, sizeof(errstr));
381       failf(data, "QUIC: recvmsg() unexpectedly returned %d (errno=%d; %s)",
382                   mcount, SOCKERRNO, errstr);
383       result = CURLE_RECV_ERROR;
384       goto out;
385     }
386 
387     CURL_TRC_CF(data, cf, "recvmmsg() -> %d packets", mcount);
388     pkts += mcount;
389     for(i = 0; i < mcount; ++i) {
390       total_nread += mmsg[i].msg_len;
391       result = recv_cb(bufs[i], mmsg[i].msg_len,
392                        mmsg[i].msg_hdr.msg_name, mmsg[i].msg_hdr.msg_namelen,
393                        0, userp);
394       if(result)
395         goto out;
396     }
397   }
398 
399 out:
400   if(total_nread || result)
401     CURL_TRC_CF(data, cf, "recvd %zu packets with %zu bytes -> %d",
402                 pkts, total_nread, result);
403   return result;
404 }
405 
406 #elif defined(HAVE_SENDMSG)
recvmsg_packets(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t max_pkts,vquic_recv_pkt_cb * recv_cb,void * userp)407 static CURLcode recvmsg_packets(struct Curl_cfilter *cf,
408                                 struct Curl_easy *data,
409                                 struct cf_quic_ctx *qctx,
410                                 size_t max_pkts,
411                                 vquic_recv_pkt_cb *recv_cb, void *userp)
412 {
413   struct iovec msg_iov;
414   struct msghdr msg;
415   uint8_t buf[64*1024];
416   struct sockaddr_storage remote_addr;
417   size_t total_nread, pkts;
418   ssize_t nread;
419   char errstr[STRERROR_LEN];
420   CURLcode result = CURLE_OK;
421 
422   msg_iov.iov_base = buf;
423   msg_iov.iov_len = (int)sizeof(buf);
424 
425   memset(&msg, 0, sizeof(msg));
426   msg.msg_iov = &msg_iov;
427   msg.msg_iovlen = 1;
428 
429   DEBUGASSERT(max_pkts > 0);
430   for(pkts = 0, total_nread = 0; pkts < max_pkts;) {
431     msg.msg_name = &remote_addr;
432     msg.msg_namelen = sizeof(remote_addr);
433     while((nread = recvmsg(qctx->sockfd, &msg, 0)) == -1 &&
434           SOCKERRNO == EINTR)
435       ;
436     if(nread == -1) {
437       if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) {
438         goto out;
439       }
440       if(!cf->connected && SOCKERRNO == ECONNREFUSED) {
441         struct ip_quadruple ip;
442         Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
443         failf(data, "QUIC: connection to %s port %u refused",
444               ip.remote_ip, ip.remote_port);
445         result = CURLE_COULDNT_CONNECT;
446         goto out;
447       }
448       Curl_strerror(SOCKERRNO, errstr, sizeof(errstr));
449       failf(data, "QUIC: recvmsg() unexpectedly returned %zd (errno=%d; %s)",
450                   nread, SOCKERRNO, errstr);
451       result = CURLE_RECV_ERROR;
452       goto out;
453     }
454 
455     ++pkts;
456     total_nread += (size_t)nread;
457     result = recv_cb(buf, (size_t)nread, msg.msg_name, msg.msg_namelen,
458                      0, userp);
459     if(result)
460       goto out;
461   }
462 
463 out:
464   if(total_nread || result)
465     CURL_TRC_CF(data, cf, "recvd %zu packets with %zu bytes -> %d",
466                 pkts, total_nread, result);
467   return result;
468 }
469 
470 #else /* HAVE_SENDMMSG || HAVE_SENDMSG */
recvfrom_packets(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t max_pkts,vquic_recv_pkt_cb * recv_cb,void * userp)471 static CURLcode recvfrom_packets(struct Curl_cfilter *cf,
472                                  struct Curl_easy *data,
473                                  struct cf_quic_ctx *qctx,
474                                  size_t max_pkts,
475                                  vquic_recv_pkt_cb *recv_cb, void *userp)
476 {
477   uint8_t buf[64*1024];
478   int bufsize = (int)sizeof(buf);
479   struct sockaddr_storage remote_addr;
480   socklen_t remote_addrlen = sizeof(remote_addr);
481   size_t total_nread, pkts;
482   ssize_t nread;
483   char errstr[STRERROR_LEN];
484   CURLcode result = CURLE_OK;
485 
486   DEBUGASSERT(max_pkts > 0);
487   for(pkts = 0, total_nread = 0; pkts < max_pkts;) {
488     while((nread = recvfrom(qctx->sockfd, (char *)buf, bufsize, 0,
489                             (struct sockaddr *)&remote_addr,
490                             &remote_addrlen)) == -1 &&
491           SOCKERRNO == EINTR)
492       ;
493     if(nread == -1) {
494       if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) {
495         CURL_TRC_CF(data, cf, "ingress, recvfrom -> EAGAIN");
496         goto out;
497       }
498       if(!cf->connected && SOCKERRNO == ECONNREFUSED) {
499         struct ip_quadruple ip;
500         Curl_cf_socket_peek(cf->next, data, NULL, NULL, &ip);
501         failf(data, "QUIC: connection to %s port %u refused",
502               ip.remote_ip, ip.remote_port);
503         result = CURLE_COULDNT_CONNECT;
504         goto out;
505       }
506       Curl_strerror(SOCKERRNO, errstr, sizeof(errstr));
507       failf(data, "QUIC: recvfrom() unexpectedly returned %zd (errno=%d; %s)",
508                   nread, SOCKERRNO, errstr);
509       result = CURLE_RECV_ERROR;
510       goto out;
511     }
512 
513     ++pkts;
514     total_nread += (size_t)nread;
515     result = recv_cb(buf, (size_t)nread, &remote_addr, remote_addrlen,
516                      0, userp);
517     if(result)
518       goto out;
519   }
520 
521 out:
522   if(total_nread || result)
523     CURL_TRC_CF(data, cf, "recvd %zu packets with %zu bytes -> %d",
524                 pkts, total_nread, result);
525   return result;
526 }
527 #endif /* !HAVE_SENDMMSG && !HAVE_SENDMSG */
528 
vquic_recv_packets(struct Curl_cfilter * cf,struct Curl_easy * data,struct cf_quic_ctx * qctx,size_t max_pkts,vquic_recv_pkt_cb * recv_cb,void * userp)529 CURLcode vquic_recv_packets(struct Curl_cfilter *cf,
530                             struct Curl_easy *data,
531                             struct cf_quic_ctx *qctx,
532                             size_t max_pkts,
533                             vquic_recv_pkt_cb *recv_cb, void *userp)
534 {
535   CURLcode result;
536 #if defined(HAVE_SENDMMSG)
537   result = recvmmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
538 #elif defined(HAVE_SENDMSG)
539   result = recvmsg_packets(cf, data, qctx, max_pkts, recv_cb, userp);
540 #else
541   result = recvfrom_packets(cf, data, qctx, max_pkts, recv_cb, userp);
542 #endif
543   if(!result) {
544     if(!qctx->got_first_byte) {
545       qctx->got_first_byte = TRUE;
546       qctx->first_byte_at = qctx->last_op;
547     }
548     qctx->last_io = qctx->last_op;
549   }
550   return result;
551 }
552 
553 /*
554  * If the QLOGDIR environment variable is set, open and return a file
555  * descriptor to write the log to.
556  *
557  * This function returns error if something failed outside of failing to
558  * create the file. Open file success is deemed by seeing if the returned fd
559  * is != -1.
560  */
Curl_qlogdir(struct Curl_easy * data,unsigned char * scid,size_t scidlen,int * qlogfdp)561 CURLcode Curl_qlogdir(struct Curl_easy *data,
562                       unsigned char *scid,
563                       size_t scidlen,
564                       int *qlogfdp)
565 {
566   const char *qlog_dir = getenv("QLOGDIR");
567   *qlogfdp = -1;
568   if(qlog_dir) {
569     struct dynbuf fname;
570     CURLcode result;
571     unsigned int i;
572     Curl_dyn_init(&fname, DYN_QLOG_NAME);
573     result = Curl_dyn_add(&fname, qlog_dir);
574     if(!result)
575       result = Curl_dyn_add(&fname, "/");
576     for(i = 0; (i < scidlen) && !result; i++) {
577       char hex[3];
578       msnprintf(hex, 3, "%02x", scid[i]);
579       result = Curl_dyn_add(&fname, hex);
580     }
581     if(!result)
582       result = Curl_dyn_add(&fname, ".sqlog");
583 
584     if(!result) {
585       int qlogfd = open(Curl_dyn_ptr(&fname), QLOGMODE,
586                         data->set.new_file_perms);
587       if(qlogfd != -1)
588         *qlogfdp = qlogfd;
589     }
590     Curl_dyn_free(&fname);
591     if(result)
592       return result;
593   }
594 
595   return CURLE_OK;
596 }
597 
Curl_cf_quic_create(struct Curl_cfilter ** pcf,struct Curl_easy * data,struct connectdata * conn,const struct Curl_addrinfo * ai,int transport)598 CURLcode Curl_cf_quic_create(struct Curl_cfilter **pcf,
599                              struct Curl_easy *data,
600                              struct connectdata *conn,
601                              const struct Curl_addrinfo *ai,
602                              int transport)
603 {
604   (void)transport;
605   DEBUGASSERT(transport == TRNSPRT_QUIC);
606 #if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
607   return Curl_cf_ngtcp2_create(pcf, data, conn, ai);
608 #elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
609   return Curl_cf_osslq_create(pcf, data, conn, ai);
610 #elif defined(USE_QUICHE)
611   return Curl_cf_quiche_create(pcf, data, conn, ai);
612 #elif defined(USE_MSH3)
613   return Curl_cf_msh3_create(pcf, data, conn, ai);
614 #else
615   *pcf = NULL;
616   (void)data;
617   (void)conn;
618   (void)ai;
619   return CURLE_NOT_BUILT_IN;
620 #endif
621 }
622 
Curl_conn_is_http3(const struct Curl_easy * data,const struct connectdata * conn,int sockindex)623 bool Curl_conn_is_http3(const struct Curl_easy *data,
624                         const struct connectdata *conn,
625                         int sockindex)
626 {
627 #if defined(USE_NGTCP2) && defined(USE_NGHTTP3)
628   return Curl_conn_is_ngtcp2(data, conn, sockindex);
629 #elif defined(USE_OPENSSL_QUIC) && defined(USE_NGHTTP3)
630   return Curl_conn_is_osslq(data, conn, sockindex);
631 #elif defined(USE_QUICHE)
632   return Curl_conn_is_quiche(data, conn, sockindex);
633 #elif defined(USE_MSH3)
634   return Curl_conn_is_msh3(data, conn, sockindex);
635 #else
636   return ((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
637           (conn->httpversion == 30));
638 #endif
639 }
640 
Curl_conn_may_http3(struct Curl_easy * data,const struct connectdata * conn)641 CURLcode Curl_conn_may_http3(struct Curl_easy *data,
642                              const struct connectdata *conn)
643 {
644   if(conn->transport == TRNSPRT_UNIX) {
645     /* cannot do QUIC over a unix domain socket */
646     return CURLE_QUIC_CONNECT_ERROR;
647   }
648   if(!(conn->handler->flags & PROTOPT_SSL)) {
649     failf(data, "HTTP/3 requested for non-HTTPS URL");
650     return CURLE_URL_MALFORMAT;
651   }
652 #ifndef CURL_DISABLE_PROXY
653   if(conn->bits.socksproxy) {
654     failf(data, "HTTP/3 is not supported over a SOCKS proxy");
655     return CURLE_URL_MALFORMAT;
656   }
657   if(conn->bits.httpproxy && conn->bits.tunnel_proxy) {
658     failf(data, "HTTP/3 is not supported over a HTTP proxy");
659     return CURLE_URL_MALFORMAT;
660   }
661 #endif
662 
663   return CURLE_OK;
664 }
665 
666 #else /* USE_HTTP3 */
667 
Curl_conn_may_http3(struct Curl_easy * data,const struct connectdata * conn)668 CURLcode Curl_conn_may_http3(struct Curl_easy *data,
669                              const struct connectdata *conn)
670 {
671   (void)conn;
672   (void)data;
673   DEBUGF(infof(data, "QUIC is not supported in this build"));
674   return CURLE_NOT_BUILT_IN;
675 }
676 
677 #endif /* !USE_HTTP3 */
678