From cb91788d05473c7e7534e88fb7ea4120eb5e73af Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Mon, 22 May 2017 16:58:00 +0200 Subject: [PATCH] When binding on the IPv6 any address bind for IPv4 too On some Linux systems this is the default. On most other operating systems accepting IPv4 connections on a IPv6 socket only happens when explicitly requested. Even on Linux it depends on the sysctl config. This change makes it independent from the system's defaults whether IPv4 connections will be accepted too through the same socket. --- src/network/network.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/network/network.cc b/src/network/network.cc index efe416f..26da603 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -41,6 +41,7 @@ #include #include #include +#include #include #include "dos_assert.h" @@ -326,6 +327,14 @@ bool Connection::try_bind( const char *addr, int port_low, int port_high ) throw NetworkException( "Unknown address family", 0 ); } + if ( local_addr.sa.sa_family == AF_INET6 + && memcmp(&local_addr.sin6.sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0 ) { + const int off = 0; + if ( setsockopt( sock(), IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off) ) ) { + perror( "setsockopt( IPV6_V6ONLY, off )" ); + } + } + if ( bind( sock(), &local_addr.sa, local_addr_len ) == 0 ) { set_MTU( local_addr.sa.sa_family ); return true;