- /* --- Note on socket buffer sizes --- *
- *
- * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
- * sizes I pass to @setsockopt@. I'm not putting special-case code here
- * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
- * guessing me.
- */
-
- if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
- die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
- BURN(sin);
- sin.sin_family = AF_INET;
- sin.sin_addr = addr;
- sin.sin_port = htons(port);
- if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
- die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
- if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
- setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
- die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
- strerror(errno));
+ for (i = 0; i < NADDRFAM; i++) udpsock[i].sf.fd = -1;
+
+ for (ai = ailist; ai; ai = ai->ai_next) {
+ if ((i = afix(ai->ai_family)) < 0) continue;
+ if (udpsock[i].sf.fd != -1) continue;
+
+ /* --- Note on socket buffer sizes --- *
+ *
+ * For some bizarre reason, Linux 2.2 (at least) doubles the socket
+ * buffer sizes I pass to @setsockopt@. I'm not putting special-case
+ * code here for Linux: BSD (at least TCPv2) does what I tell it rather
+ * than second-guessing me.
+ */
+
+ if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0)
+ die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
+ if (i == AFIX_INET6 &&
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes))) {
+ die(EXIT_FAILURE, "failed to set IPv6-only state: %s",
+ strerror(errno));
+ }
+ assert(ai->ai_addrlen <= sizeof(a));
+ memcpy(&a, ai->ai_addr, ai->ai_addrlen);
+ if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
+ if (bind(fd, &a.sa, addrsz(&a)))
+ die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
+ setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
+ die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
+ strerror(errno));
+ }
+ fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
+ sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
+ sel_addfile(&udpsock[i].sf);
+ T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
+ if (port)
+ udpsock[i].port = port;
+ else {
+ sz = sizeof(a);
+ if (getsockname(fd, &a.sa, &sz)) {
+ die(EXIT_FAILURE, "failed to read local socket address: %s",
+ strerror(errno));
+ }
+ udpsock[i].port = lastport = getport(&a);
+ }