From 2fb1657b7265213150274ea7bd65057b85871dc7 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Thu, 28 Sep 2017 01:47:43 +0100 Subject: [PATCH] pkstream/pkstream.c: Introduce an `initsock' function which does nothing. Only it checks the address family first. This will be important because IPv6 sockets /do/ need a little special initialization. --- pkstream/pkstream.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkstream/pkstream.c b/pkstream/pkstream.c index 580bb73f..dd4658e4 100644 --- a/pkstream/pkstream.c +++ b/pkstream/pkstream.c @@ -111,6 +111,15 @@ static socklen_t addrsz(const addr *a) } } +static int initsock(int fd, int af) +{ + switch (af) { + case AF_INET: break; + default: abort(); + } + return (0); +} + static const char *addrstr(const addr *a) { static char buf[128]; @@ -312,6 +321,7 @@ static void dolisten1(const addr *a, sel_file *sf) if ((fd = socket(a->sa.sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0 || setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) || + initsock(fd, a->sa.sa_family) || bind(fd, &a->sa, addrsz(a)) || listen(fd, 1) || nonblockify(fd) || cloexec(fd)) die(1, "couldn't set up listening socket: %s", strerror(errno)); @@ -507,6 +517,7 @@ int main(int argc, char *argv[]) for (ai = ailist; ai; ai = ai->ai_next) { if ((fd = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP)) >= 0 && + !initsock(fd, ai->ai_family) && (!DA_LEN(&bindhosts) || !bind(fd, &bindaddr.sa, addrsz(&bindaddr))) && !connect(fd, ai->ai_addr, ai->ai_addrlen)) @@ -524,6 +535,7 @@ int main(int argc, char *argv[]) aihint.ai_flags = AI_ADDRCONFIG | AI_PASSIVE; parseaddr(&aihint, argv[optind], 0, paf_parse, &ailist); ai = ailist; if ((fd_udp = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP)) < 0 || + initsock(fd_udp, ai->ai_family) || nonblockify(fd_udp) || cloexec(fd_udp) || setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) || setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) || -- 2.11.0