2
0

vquic.c 22 KB

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