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].
This commit is contained in:
Norman Maurer 2017-08-21 10:30:58 +02:00
parent b18a201d02
commit 7290cbc48a

View File

@ -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;
}
}