From c7a2516f5b39046d113a5bb80b64500faadcef8a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 30 Aug 2022 11:57:30 +0200 Subject: net: bind to IPv6 This makes sure we use the dual-stack feature to support both IPv4 and IPv6. --- src/net.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/net.c b/src/net.c index 5c44c70..534807c 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ int net_bind(const char *port) int socket_fd, ret = 0; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; @@ -35,8 +35,16 @@ int net_bind(const char *port) } static const int yes = 1; + static const int no = 0; + + if (it->ai_family == AF_INET6) { + if (setsockopt(socket_fd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)) < 0) { + print_errno("setsockopt"); + goto close_socket; + } + } - if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) { + if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { print_errno("setsockopt"); goto close_socket; } -- cgit v1.2.3