vquic.c 19 KB

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