- /* IPv4 socket setup */
- sockets[0] = socket(PF_INET, SOCK_STREAM, 0);
- if (sockets[0] < 0)
- Log_fatal("socket IPv4");
- if (setsockopt(sockets[0], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
- Log_fatal("setsockopt IPv4: %s", strerror(errno));
- if (bind(sockets[0], (struct sockaddr *)addresses[0], sizeof (struct sockaddr_in)) < 0)
- Log_fatal("bind %s %d: %s", Util_addressToString(addresses[0]), Util_addressToPort(addresses[0]), strerror(errno));
- if (listen(sockets[0], 3) < 0)
- Log_fatal("listen IPv4");
- fcntl(sockets[0], F_SETFL, O_NONBLOCK);
-
- pollfds[TCP_SOCK].fd = sockets[0];
- pollfds[TCP_SOCK].events = POLLIN;
-
- /* IPv6 socket setup */
- sockets[1] = socket(PF_INET6, SOCK_STREAM, 0);
- if (sockets[1] < 0)
- Log_fatal("socket IPv6");
- if (setsockopt(sockets[1], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
- Log_fatal("setsockopt IPv6: %s", strerror(errno));
- if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(int)) != 0)
- Log_fatal("setsockopt IPv6: %s", strerror(errno));
- if (bind(sockets[1], (struct sockaddr *)addresses[1], sizeof (struct sockaddr_in6)) < 0)
- Log_fatal("bind %s %d: %s", Util_addressToString(addresses[1]), Util_addressToPort(addresses[1]), strerror(errno));
- if (listen(sockets[1], 3) < 0)
- Log_fatal("listen IPv6");
- fcntl(sockets[1], F_SETFL, O_NONBLOCK);
-
- pollfds[TCP_SOCK6].fd = sockets[1];
- pollfds[TCP_SOCK6].events = POLLIN;
+ if (hasv4) {
+ /* IPv4 socket setup */
+ sockets[0] = socket(PF_INET, SOCK_STREAM, 0);
+ if (sockets[0] < 0)
+ Log_fatal("socket IPv4");
+ if (setsockopt(sockets[0], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv4: %s", strerror(errno));
+ if (bind(sockets[0], (struct sockaddr *)addresses[0], sizeof (struct sockaddr_in)) < 0)
+ Log_fatal("bind %s %d: %s", Util_addressToString(addresses[0]), Util_addressToPort(addresses[0]), strerror(errno));
+ if (listen(sockets[0], 3) < 0)
+ Log_fatal("listen IPv4");
+ fcntl(sockets[0], F_SETFL, O_NONBLOCK);
+
+ pollfds[0].fd = sockets[0];
+ pollfds[0].events = POLLIN;
+ }
+
+ if (hasv6) {
+ /* IPv6 socket setup */
+ sockets[1] = socket(PF_INET6, SOCK_STREAM, 0);
+ if (sockets[1] < 0)
+ Log_fatal("socket IPv6: %s", strerror(errno));
+ if (setsockopt(sockets[1], SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv6: %s", strerror(errno));
+ if (setsockopt(sockets[1], IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(int)) != 0)
+ Log_fatal("setsockopt IPv6: %s", strerror(errno));
+ if (bind(sockets[1], (struct sockaddr *)addresses[1], sizeof (struct sockaddr_in6)) < 0)
+ Log_fatal("bind %s %d: %s", Util_addressToString(addresses[1]), Util_addressToPort(addresses[1]), strerror(errno));
+ if (listen(sockets[1], 3) < 0)
+ Log_fatal("listen IPv6");
+ fcntl(sockets[1], F_SETFL, O_NONBLOCK);
+
+
+ /* If there is an IPv4 address, then IPv6 will use the second socket, otherwise it uses the first */
+ pollfds[(hasv4) ? 1 : 0].fd = sockets[1];
+ pollfds[(hasv4) ? 1 : 0].events = POLLIN;
+ }