From dde3f561bcffc5b0ef845009ca751abfb14d9ead Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 15 Apr 2019 13:07:05 +0200 Subject: [PATCH] =?UTF-8?q?Use=20ResolvedAddressTypes.IPV4=5FONLY=20in=20D?= =?UTF-8?q?nsNameResolver=20by=20default=20if=20n=E2=80=A6=20(#9048)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation: To closely mimic what the JDK does we should not try to resolve AAAA records if the system itself does not support IPv6 at all as it is impossible to connect to this addresses later on. In this case we need to use ResolvedAddressTypes.IPV4_ONLY. Modifications: Add static method to detect if IPv6 is supported and if not use ResolvedAddressTypes.IPV4_ONLY. Result: More consistent behaviour between JDK and our resolver implementation. --- .../netty/resolver/dns/DnsNameResolver.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index bd97f5a503..0d37263202 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -65,10 +65,13 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.Enumeration; import java.util.Iterator; import java.util.List; @@ -109,7 +112,7 @@ public class DnsNameResolver extends InetNameResolver { private static final int DEFAULT_NDOTS; static { - if (NetUtil.isIpV4StackPreferred()) { + if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) { DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_ONLY; LOCALHOST_ADDRESS = NetUtil.LOCALHOST4; } else { @@ -145,6 +148,28 @@ public class DnsNameResolver extends InetNameResolver { DEFAULT_NDOTS = ndots; } + /** + * Returns {@code true} if any {@link NetworkInterface} supports {@code IPv6}, {@code false} otherwise. + */ + private static boolean anyInterfaceSupportsIpV6() { + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces.hasMoreElements()) { + NetworkInterface iface = interfaces.nextElement(); + Enumeration addresses = iface.getInetAddresses(); + while (addresses.hasMoreElements()) { + if (addresses.nextElement() instanceof Inet6Address) { + return true; + } + } + } + } catch (SocketException e) { + logger.debug("Unable to detect if any interface supports IPv6, assuming IPv4-only", e); + // ignore + } + return false; + } + @SuppressWarnings("unchecked") private static List getSearchDomainsHack() throws Exception { // This code on Java 9+ yields a warning about illegal reflective access that will be denied in