Repository: netty/netty
Issue: io.netty.resolver.dns.DnsNameResolver does not resolve localhost on Windows
Labels: defect

On Windows localhost is not in hosts file and the DNS server does not resolve this address either, i.e it is handled by the Windows API. So using a `Bootstrap` (among others) with the resolver based on `DnsNameResolver` will not resolve localhost.

http://serverfault.com/questions/4689/windows-7-localhost-name-resolution-is-handled-within-dns-itself-why


Issue comments:
@vietj so what ? I think there is not much we can do without doing some crazy jni stuff :(

now in Vert.x we simply resolve localhost to `127.0.0.1` on Windows. Perhaps doing the same would make sense in Netty too.

Yeah maybe… and logging a warning. Let me do this.

> On 13 Jun 2016, at 11:49, Julien Viet notifications@github.com wrote:
> 
> now in Vert.x we simply resolve localhost to 127.0.0.1 on Windows. Perhaps doing the same would make sense in Netty too.
> 
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub https://github.com/netty/netty/issues/5386#issuecomment-225536427, or mute the thread https://github.com/notifications/unsubscribe/AAa0QrENFN-8OEAFmlqfsVSJxiXLgnmVks5qLSevgaJpZM4I0JNJ.

a workaround is to add this to the list of parsed hosts when it does not exists using the blocking API (which is not a problem usually as this is done in the resolver itself)

i.e you can still get the localhost value once and cache it in a static.

```
      try {
        if (!entries.containsKey("localhost")) {
          entries.put("localhost", InetAddress.getLocalHost());
        }
      } catch (UnknownHostException ignore) {
      }
      builder.hostsFileEntriesResolver(entries::get);
```

Fixed by https://github.com/netty/netty/pull/5410


Pull request review comments:

Patch that resolved the issue:
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 576a47229e6..a2d8101f812 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
@@ -40,6 +40,7 @@
 import io.netty.util.concurrent.FastThreadLocal;
 import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.Promise;
+import io.netty.util.internal.PlatformDependent;
 import io.netty.util.internal.UnstableApi;
 import io.netty.util.internal.logging.InternalLogger;
 import io.netty.util.internal.logging.InternalLoggerFactory;
@@ -61,6 +62,8 @@
 public class DnsNameResolver extends InetNameResolver {
 
     private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class);
+    private static final String LOCALHOST = "localhost";
+    private static final InetAddress LOCALHOST_ADDRESS;
 
     static final InetSocketAddress ANY_LOCAL_ADDR = new InetSocketAddress(0);
 
@@ -71,10 +74,12 @@ public class DnsNameResolver extends InetNameResolver {
         if (Boolean.getBoolean("java.net.preferIPv6Addresses")) {
             DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv6;
             DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv4;
+            LOCALHOST_ADDRESS = NetUtil.LOCALHOST6;
             logger.debug("-Djava.net.preferIPv6Addresses: true");
         } else {
             DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv4;
             DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv6;
+            LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
             logger.debug("-Djava.net.preferIPv6Addresses: false");
         }
     }
@@ -282,7 +287,18 @@ protected EventLoop executor() {
     }
 
     private InetAddress resolveHostsFileEntry(String hostname) {
-        return hostsFileEntriesResolver != null ? hostsFileEntriesResolver.address(hostname) : null;
+        if (hostsFileEntriesResolver == null) {
+            return null;
+        } else {
+            InetAddress address = hostsFileEntriesResolver.address(hostname);
+            if (address == null && PlatformDependent.isWindows() && LOCALHOST.equalsIgnoreCase(hostname)) {
+                // If we tried to resolve localhost we need workaround that windows removed localhost from its
+                // hostfile in later versions.
+                // See https://github.com/netty/netty/issues/5386
+                return LOCALHOST_ADDRESS;
+            }
+            return address;
+        }
     }
 
     @Override


Test patch: