From 7290cbc48aea4971aa66c64c2f43d8a2611012ab Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 21 Aug 2017 10:30:58 +0200 Subject: [PATCH] More bullet-proof way of detecting if ipv6 is supported or not when using native transport Motivation: We should try to bind to an ipv6 only socket before we enable ipv6 support in the native transport as it may not work due setup of the platform. Modifications: Try to bind to ::1 use IPV6 later on if this works Result: Fixes [#7021]. --- .../src/main/c/netty_unix_socket.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/transport-native-unix-common/src/main/c/netty_unix_socket.c b/transport-native-unix-common/src/main/c/netty_unix_socket.c index 7b98abace9..fa655ccd6e 100644 --- a/transport-native-unix-common/src/main/c/netty_unix_socket.c +++ b/transport-native-unix-common/src/main/c/netty_unix_socket.c @@ -183,8 +183,16 @@ static int socket_type(JNIEnv* env) { } return AF_INET6; } else { + // Explicitly try to bind to ::1 to ensure IPV6 can really be used. + // See https://github.com/netty/netty/issues/7021. + struct sockaddr_in6 addr; + memset(&addr, 0, sizeof(addr)); + addr.sin6_family = AF_INET6; + addr.sin6_addr.s6_addr[15] = 1; /* [::1]:0 */ + int res = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); + close(fd); - return AF_INET6; + return res == 0 ? AF_INET6 : AF_INET; } }