Skip to content

Commit

Permalink
Revert "[cli] merge the socket creation logic"
Browse files Browse the repository at this point in the history
This reverts commit 07b28e5.
  • Loading branch information
kazuho committed Apr 23, 2024
1 parent a62d92f commit 8b3aec7
Showing 1 changed file with 71 additions and 80 deletions.
151 changes: 71 additions & 80 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

0 comments on commit 8b3aec7

Please sign in to comment.