+ int yes = 1;
+ int i;
+ struct addrinfo *ai;
+ unsigned port, lastport = 0;
+ addr a;
+ socklen_t sz;
+
+ 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) {
+ a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+ "create-failed", "?ERRNO", A_END);
+ goto fail;
+ }
+ if (i == AFIX_INET6 &&
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes))) {
+ a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+ "set-v6only-failed", "?ERRNO", A_END);
+ goto fail;
+ }
+ 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))) {
+ a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+ "bind-failed", "?ERRNO", A_END);
+ goto fail;
+ }
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
+ setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
+ a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+ "set-buffers-failed", "?ERRNO", A_END);
+ goto fail;
+ }
+ fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
+ if (port)
+ udpsock[i].port = port;
+ else {
+ sz = sizeof(a);
+ if (getsockname(fd, &a.sa, &sz)) {
+ a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+ "read-local-address-failed", "?ERRNO", A_END);
+ goto fail;
+ }
+ udpsock[i].port = lastport = getport(&a);
+ }
+ T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
+ sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
+ sel_addfile(&udpsock[i].sf);
+ fd = -1;
+ }