vquic.c 21 KB

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