vquic.c 20 KB

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