Skip to content

Commit

Permalink
Ensure DnsNameResolver resolves the host(computer) name on Windows (#…
Browse files Browse the repository at this point in the history
…11167)

Motivation:

On Windows DnsNameResolver is not able to resolve the host(computer) name as it is not in the hosts file and the DNS server is also not able to resolve it.
The exception below is the result of the resolution:
Caused by: java.net.UnknownHostException: failed to resolve 'host(computer)-name' after 2 queries
	at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013)
	at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:966)
	at io.netty.resolver.dns.DnsResolveContext.query(DnsResolveContext.java:414)
	at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:938)
	at io.netty.resolver.dns.DnsResolveContext.access$700(DnsResolveContext.java:63)
	at io.netty.resolver.dns.DnsResolveContext$2.operationComplete(DnsResolveContext.java:467)

Modifications:

On Windows DnsNameResolver maps host(computer) name to LOCALHOST

Result:

DnsNameResolver is able to resolve the host(computer) name on Windows

Fixes #11142
  • Loading branch information
violetagg authored and normanmaurer committed Apr 20, 2021
1 parent c2f893f commit 311dae5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Expand Up @@ -91,6 +91,7 @@ public class DnsNameResolver extends InetNameResolver {

private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class);
private static final String LOCALHOST = "localhost";
private static final String WINDOWS_HOST_NAME;
private static final InetAddress LOCALHOST_ADDRESS;
private static final DnsRecord[] EMPTY_ADDITIONALS = new DnsRecord[0];
private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES =
Expand Down Expand Up @@ -127,6 +128,14 @@ public class DnsNameResolver extends InetNameResolver {
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
}
}

String hostName;
try {
hostName = PlatformDependent.isWindows() ? InetAddress.getLocalHost().getHostName() : null;
} catch (Exception ignore) {
hostName = null;
}
WINDOWS_HOST_NAME = hostName;
}

static {
Expand Down Expand Up @@ -673,10 +682,14 @@ private InetAddress resolveHostsFileEntry(String hostname) {
return null;
} else {
InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes);
if (address == null && PlatformDependent.isWindows() && LOCALHOST.equalsIgnoreCase(hostname)) {
if (address == null && PlatformDependent.isWindows() &&
(LOCALHOST.equalsIgnoreCase(hostname) ||
(WINDOWS_HOST_NAME != null && WINDOWS_HOST_NAME.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
// Need a workaround for resolving the host (computer) name in case it cannot be resolved from hostfile
// See https://github.com/netty/netty/issues/11142
return LOCALHOST_ADDRESS;
}
return address;
Expand Down
Expand Up @@ -47,6 +47,7 @@
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SocketUtils;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
Expand Down Expand Up @@ -112,8 +113,10 @@
import static io.netty.handler.codec.dns.DnsRecordType.AAAA;
import static io.netty.handler.codec.dns.DnsRecordType.CNAME;
import static io.netty.handler.codec.dns.DnsRecordType.SRV;
import static io.netty.resolver.dns.DnsNameResolver.DEFAULT_RESOLVE_ADDRESS_TYPES;
import static io.netty.resolver.dns.DnsServerAddresses.sequential;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -328,6 +331,18 @@ public class DnsNameResolverTest {
StringUtil.EMPTY_STRING);
}

private static final String HOST_NAME;

static {
String hostName;
try {
hostName = PlatformDependent.isWindows() ? InetAddress.getLocalHost().getHostName() : null;
} catch (Exception ignore) {
hostName = null;
}
HOST_NAME = hostName;
}

private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL);
private static final EventLoopGroup group = new MultithreadEventLoopGroup(1, NioHandler.newFactory());

Expand Down Expand Up @@ -746,6 +761,34 @@ public void testResolveEmptyIpv6() {
testResolve0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, StringUtil.EMPTY_STRING);
}

@Test
public void testResolveLocalhostIpv4() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isNotEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolve0(ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, "localhost");
}

@Test
public void testResolveLocalhostIpv6() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolve0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, "localhost");
}

@Test
public void testResolveHostNameIpv4() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isNotEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolve0(ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, HOST_NAME);
}

@Test
public void testResolveHostNameIpv6() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolve0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, HOST_NAME);
}

@Test
public void testResolveNullIpv4() {
testResolve0(ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, null);
Expand Down Expand Up @@ -779,6 +822,34 @@ public void testResolveAllEmptyIpv6() {
testResolveAll0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, StringUtil.EMPTY_STRING);
}

@Test
public void testResolveAllLocalhostIpv4() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isNotEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolveAll0(ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, "localhost");
}

@Test
public void testResolveAllLocalhostIpv6() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolveAll0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, "localhost");
}

@Test
public void testResolveAllHostNameIpv4() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isNotEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolveAll0(ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, HOST_NAME);
}

@Test
public void testResolveAllHostNameIpv6() {
assumeThat(PlatformDependent.isWindows()).isTrue();
assumeThat(DEFAULT_RESOLVE_ADDRESS_TYPES).isEqualTo(ResolvedAddressTypes.IPV6_PREFERRED);
testResolveAll0(ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, HOST_NAME);
}

@Test
public void testCNAMEResolveAllIpv4() throws IOException {
testCNAMERecursiveResolve(true);
Expand Down

0 comments on commit 311dae5

Please sign in to comment.