vquic.c 21 KB

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