From 8b3aec71ed7352fb20b089d3b513f2368b59fb7f Mon Sep 17 00:00:00 2001 From: Kazuho Oku Date: Tue, 23 Apr 2024 14:43:22 +0900 Subject: [PATCH] Revert "[cli] merge the socket creation logic" This reverts commit 07b28e5a2e95c11cce0313fbeea435154cff0929. --- src/cli.c | 151 +++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 80 deletions(-) diff --git a/src/cli.c b/src/cli.c index b3befc42..5463095f 100644 --- a/src/cli.c +++ b/src/cli.c @@ -51,7 +51,7 @@ #define MAX_BURST_PACKETS 10 FILE *quicly_trace_fp = NULL; -static unsigned verbosity = 0, udpbufsize = 0; +static unsigned verbosity = 0; static int suppress_output = 0, send_datagram_frame = 0; static int64_t enqueue_requests_at = 0, request_interval = 0; @@ -123,84 +123,6 @@ struct st_stream_data_t { FILE *outfp; }; -static int new_socket(int af) -{ - int fd; - - if ((fd = socket(af, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - perror("socket(2) failed"); - return -1; - } - fcntl(fd, F_SETFL, O_NONBLOCK); - { - int on = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) { - perror("setsockopt(SO_REUSEADDR) failed"); - return -1; - } - } - if (udpbufsize != 0) { - unsigned arg = udpbufsize; - if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &arg, sizeof(arg)) != 0) { - perror("setsockopt(SO_RCVBUF) failed"); - return -1; - } - arg = udpbufsize; - if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &arg, sizeof(arg)) != 0) { - perror("setsockopt(SO_RCVBUF) failed"); - return -1; - } - } -#if defined(IP_DONTFRAG) - { - int on = 1; - if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &on, sizeof(on)) != 0) - perror("Warning: setsockopt(IP_DONTFRAG) failed"); - } -#elif defined(IP_PMTUDISC_DO) - { - int opt = IP_PMTUDISC_DO; - if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) != 0) - perror("Warning: setsockopt(IP_MTU_DISCOVER) failed"); - } -#endif - switch (af) { - case AF_INET: { -#ifdef IP_PKTINFO - int on = 1; - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) != 0) { - perror("setsockopt(IP_PKTINFO) failed"); - return -1; - } -#elif defined(IP_RECVDSTADDR) - int on = 1; - if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on)) != 0) { - perror("setsockopt(IP_RECVDSTADDR) failed"); - return -1; - } -#endif - } break; - case AF_INET6: { - int on = 1; - if (setsockopt(fd, IPPROTO_IP, IPV6_RECVPKTINFO, &on, sizeof(on)) != 0) { - perror("setsockopt(IPV6_RECVPKTINNFO) failed"); - return -1; - } - } break; - default: - break; - } -#ifdef IP_RECVTOS - { - int on = 1; - if (setsockopt(fd, IPPROTO_IP, IP_RECVTOS, &on, sizeof(on)) != 0) - perror("Warning: setsockopt(IP_RECVTOS) failed"); - } -#endif - - return fd; -} - static void on_stop_sending(quicly_stream_t *stream, int err); static void on_receive_reset(quicly_stream_t *stream, int err); static void server_on_receive(quicly_stream_t *stream, size_t off, const void *src, size_t len); @@ -1340,6 +1262,7 @@ int main(int argc, char **argv) const char *cert_file = NULL, *raw_pubkey_file = NULL, *host, *port, *cid_key = NULL; struct sockaddr_storage sa; socklen_t salen; + unsigned udpbufsize = 0; int ch, opt_index, fd; ERR_load_crypto_strings(); @@ -1742,8 +1665,76 @@ int main(int argc, char **argv) if (resolve_address((void *)&sa, &salen, host, port, AF_INET, SOCK_DGRAM, IPPROTO_UDP) != 0) exit(1); - if ((fd = new_socket(sa.ss_family)) == -1) + if ((fd = socket(sa.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { + perror("socket(2) failed"); return 1; + } + fcntl(fd, F_SETFL, O_NONBLOCK); + { + int on = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) { + perror("setsockopt(SO_REUSEADDR) failed"); + return 1; + } + } + if (udpbufsize != 0) { + unsigned arg = udpbufsize; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &arg, sizeof(arg)) != 0) { + perror("setsockopt(SO_RCVBUF) failed"); + return 1; + } + arg = udpbufsize; + if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &arg, sizeof(arg)) != 0) { + perror("setsockopt(SO_RCVBUF) failed"); + return 1; + } + } +#if defined(IP_DONTFRAG) + { + int on = 1; + if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &on, sizeof(on)) != 0) + perror("Warning: setsockopt(IP_DONTFRAG) failed"); + } +#elif defined(IP_PMTUDISC_DO) + { + int opt = IP_PMTUDISC_DO; + if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &opt, sizeof(opt)) != 0) + perror("Warning: setsockopt(IP_MTU_DISCOVER) failed"); + } +#endif +#ifdef IP_RECVTOS + { + int on = 1; + if (setsockopt(fd, IPPROTO_IP, IP_RECVTOS, &on, sizeof(on)) != 0) + perror("Warning: setsockopt(IP_RECVTOS) failed"); + } +#endif + switch (sa.ss_family) { + case AF_INET: { +#ifdef IP_PKTINFO + int on = 1; + if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &on, sizeof(on)) != 0) { + perror("setsockopt(IP_PKTINFO) failed"); + return 1; + } +#elif defined(IP_RECVDSTADDR) + int on = 1; + if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &on, sizeof(on)) != 0) { + perror("setsockopt(IP_RECVDSTADDR) failed"); + return 1; + } +#endif + } break; + case AF_INET6: { + int on = 1; + if (setsockopt(fd, IPPROTO_IP, IPV6_RECVPKTINFO, &on, sizeof(on)) != 0) { + perror("setsockopt(IPV6_RECVPKTINNFO) failed"); + return 1; + } + } break; + default: + break; + } return ctx.tls->certificates.count != 0 ? run_server(fd, (void *)&sa, salen) : run_client(fd, (void *)&sa, host); }