GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Resolve all localhost addresses without querying DNS servers (#16749)… · netty/netty@bb458bd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • resolver-dns/src
    • main/java/io/netty/resolver/dns
      • DnsNameResolver.java
    • test/java/io/netty/resolver/dns
      • DnsNameResolverTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java
+46-18Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import java.util.HashMap;
8484
import java.util.Iterator;
8585
import java.util.List;
86+
import java.util.Locale;
8687
import java.util.Map;
8788
import java.util.concurrent.TimeUnit;
8889

@@ -102,8 +103,8 @@ public class DnsNameResolver extends InetNameResolver {
102103

103104
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class);
104105
private static final String LOCALHOST = "localhost";
106+
private static final String DOT_LOCALHOST = '.' + LOCALHOST;
105107
private static final String WINDOWS_HOST_NAME;
106-
private static final InetAddress LOCALHOST_ADDRESS;
107108
private static final DnsRecord[] EMPTY_ADDITIONALS = new DnsRecord[0];
108109
private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES =
109110
{DnsRecordType.A};
@@ -136,18 +137,14 @@ public boolean isSharable() {
136137
static {
137138
if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) {
138139
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_ONLY;
139-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
140140
} else {
141141
if (NetUtil.isIpV6AddressesPreferred()) {
142142
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV6_PREFERRED;
143-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST6;
144143
} else {
145144
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_PREFERRED;
146-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
147145
}
148146
}
149147
logger.debug("Default ResolvedAddressTypes: {}", DEFAULT_RESOLVE_ADDRESS_TYPES);
150-
logger.debug("Localhost address: {}", LOCALHOST_ADDRESS);
151148

152149
String hostName;
153150
try {
@@ -701,7 +698,7 @@ private InetAddress resolveHostsFileEntry(String hostname) {
701698
return null;
702699
}
703700
InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes);
704-
return address == null && isLocalWindowsHost(hostname) ? LOCALHOST_ADDRESS : address;
701+
return address == null && isLocalHostAddress(hostname)? getLocalHostAddress() : address;
705702
}
706703

707704
private List<InetAddress> resolveHostsFileEntries(String hostname) {
@@ -714,23 +711,54 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) {
714711
.addresses(hostname, resolvedAddressTypes);
715712
} else {
716713
InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes);
717-
addresses = address != null ? Collections.singletonList(address) : null;
714+
addresses = address != null? Collections.singletonList(address) : null;
718715
}
719-
return addresses == null && isLocalWindowsHost(hostname) ?
720-
Collections.singletonList(LOCALHOST_ADDRESS) : addresses;
716+
return addresses == null && isLocalHostAddress(hostname)?
717+
Collections.singletonList(getLocalHostAddress()) : addresses;
721718
}
722719

723720
/**
724-
* Checks whether the given hostname is the localhost/host (computer) name on Windows OS.
725-
* Windows OS removed the localhost/host (computer) name information from the hosts file in the later versions
726-
* and such hostname cannot be resolved from hosts file.
727-
* See https://github.com/netty/netty/issues/5386
728-
* See https://github.com/netty/netty/issues/11142
721+
* Checks whether the given hostname refers to the current computer. This is the case for:
722+
* <ul>
723+
* <li>localhost.</li>
724+
* <li>any domain within .localhost.</li>
725+
* <li>the hostname of the local computer on Windows</li>
726+
* </ul>
727+
* <p>
728+
* According to RFC 6761 Section 6.3, localhost and subdomains of localhost should be resolved to the loopback
729+
* address by name resolution libraries without querying DNS servers. The hostname of the local machine can usually
730+
* be resolved from the hosts file, but on Windows, this is no longer possible.
731+
*
732+
* @param hostname the hostname that's being looked up
733+
* @return true if the hostname should point to the loopback adress. False otherwise.
734+
* @see <a href="https://github.com/netty/netty/issues/5386">Issue 5386</a>
735+
* @see <a href="https://github.com/netty/netty/issues/11142">Issue 11142</a>
736+
* @see <a href="https://github.com/netty/netty/issues/16744">Issue 16744</a>
737+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3">RFC 6761</a>
729738
*/
730-
private static boolean isLocalWindowsHost(String hostname) {
731-
return PlatformDependent.isWindows() &&
732-
(LOCALHOST.equalsIgnoreCase(hostname) ||
733-
(WINDOWS_HOST_NAME != null && WINDOWS_HOST_NAME.equalsIgnoreCase(hostname)));
739+
private static boolean isLocalHostAddress(String hostname) {
740+
if (PlatformDependent.isWindows() && WINDOWS_HOST_NAME != null &&
741+
WINDOWS_HOST_NAME.equalsIgnoreCase(hostname)) {
742+
return true;
743+
}
744+
745+
if (hostname.endsWith(".")) {
746+
hostname = hostname.substring(0, hostname.length() - 1);
747+
}
748+
return hostname.equalsIgnoreCase(LOCALHOST) || hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST);
749+
}
750+
751+
private InetAddress getLocalHostAddress() {
752+
switch (resolvedAddressTypes) {
753+
case IPV4_ONLY:
754+
case IPV4_PREFERRED:
755+
return NetUtil.LOCALHOST4;
756+
case IPV6_ONLY:
757+
case IPV6_PREFERRED:
758+
return NetUtil.LOCALHOST6;
759+
default:
760+
throw new IllegalStateException("Unknown ResolvedAddressTypes " + resolvedAddressTypes);
761+
}
734762
}
735763

736764
/**
Collapse file

‎resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@
112112
import org.junit.jupiter.api.Timeout;
113113
import org.junit.jupiter.api.function.Executable;
114114
import org.junit.jupiter.params.ParameterizedTest;
115+
import org.junit.jupiter.params.provider.Arguments;
115116
import org.junit.jupiter.params.provider.EnumSource;
117+
import org.junit.jupiter.params.provider.MethodSource;
116118

117119
import static io.netty.handler.codec.dns.DnsRecordType.A;
118120
import static io.netty.handler.codec.dns.DnsRecordType.AAAA;
@@ -861,6 +863,46 @@ public void testResolveHostNameIpv6(DnsNameResolverChannelStrategy strategy) {
861863
testResolve0(strategy, ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, WINDOWS_HOST_NAME);
862864
}
863865

866+
private static List<Object[]> testResolveLocalhostWithoutDNSArgs() {
867+
DnsNameResolverChannelStrategy[] strategies = DnsNameResolverChannelStrategy.values();
868+
List<String> names = asList("localhost", "localhost.", "test.localhost", "TEsT.LOCalhost", "test.localhost.");
869+
870+
List<Object[]> output = new ArrayList<Object[]>();
871+
for (DnsNameResolverChannelStrategy strategy : strategies) {
872+
for (String name : names) {
873+
output.add(new Object[] { strategy, ResolvedAddressTypes.IPV4_ONLY, NetUtil.LOCALHOST4, name });
874+
output.add(new Object[] { strategy, ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, name });
875+
}
876+
}
877+
878+
return output;
879+
}
880+
881+
@ParameterizedTest
882+
@MethodSource("testResolveLocalhostWithoutDNSArgs")
883+
public void testResolveLocalhostWithoutDNSOrHostsFile(DnsNameResolverChannelStrategy strategy,
884+
ResolvedAddressTypes addressTypes, InetAddress expectedAddr,
885+
String name) {
886+
DnsNameResolver resolver = newResolver(strategy, addressTypes)
887+
.hostsFileEntriesResolver(new HostsFileEntriesResolver() {
888+
@Override
889+
public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
890+
// The hosts file should not be required to resolve localhost addresses.
891+
return null;
892+
}
893+
})
894+
.build();
895+
try {
896+
InetAddress address = resolver.resolve(name).syncUninterruptibly().getNow();
897+
assertEquals(expectedAddr, address);
898+
899+
// We are resolving the local address, so we shouldn't make any queries.
900+
assertNoQueriesMade(resolver);
901+
} finally {
902+
resolver.close();
903+
}
904+
}
905+
864906
@ParameterizedTest
865907
@EnumSource(DnsNameResolverChannelStrategy.class)
866908
public void testResolveNullIpv4(DnsNameResolverChannelStrategy strategy) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.