From: Mark Wooding Date: Thu, 28 Sep 2017 00:47:43 +0000 (+0100) Subject: pkstream/pkstream.c: Introduce an `initsock' function which does nothing. X-Git-Tag: 1.5.0~41^2~47 X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/commitdiff_plain/2fb1657b7265213150274ea7bd65057b85871dc7?hp=0bf142d2dc68ff6dc43c329332ec09ac28166905 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. --- 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)) ||