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 — Auto-port 5.0: Resolve all localhost addresses without querying DNS s… · netty/netty@df9f928

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
@@ -78,6 +78,7 @@
7878
import java.util.HashMap;
7979
import java.util.Iterator;
8080
import java.util.List;
81+
import java.util.Locale;
8182
import java.util.Map;
8283
import java.util.concurrent.TimeUnit;
8384

@@ -97,8 +98,8 @@ public class DnsNameResolver extends InetNameResolver {
9798

9899
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class);
99100
private static final String LOCALHOST = "localhost";
101+
private static final String DOT_LOCALHOST = '.' + LOCALHOST;
100102
private static final String WINDOWS_HOST_NAME;
101-
private static final InetAddress LOCALHOST_ADDRESS;
102103
private static final DnsRecord[] EMPTY_ADDITIONALS = new DnsRecord[0];
103104
private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES =
104105
{DnsRecordType.A};
@@ -131,18 +132,14 @@ public boolean isSharable() {
131132
static {
132133
if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) {
133134
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_ONLY;
134-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
135135
} else {
136136
if (NetUtil.isIpV6AddressesPreferred()) {
137137
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV6_PREFERRED;
138-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST6;
139138
} else {
140139
DEFAULT_RESOLVE_ADDRESS_TYPES = ResolvedAddressTypes.IPV4_PREFERRED;
141-
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
142140
}
143141
}
144142
logger.debug("Default ResolvedAddressTypes: {}", DEFAULT_RESOLVE_ADDRESS_TYPES);
145-
logger.debug("Localhost address: {}", LOCALHOST_ADDRESS);
146143

147144
String hostName;
148145
try {
@@ -688,7 +685,7 @@ private InetAddress resolveHostsFileEntry(String hostname) {
688685
return null;
689686
}
690687
InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes);
691-
return address == null && isLocalWindowsHost(hostname) ? LOCALHOST_ADDRESS : address;
688+
return address == null && isLocalHostAddress(hostname)? getLocalHostAddress() : address;
692689
}
693690

694691
private List<InetAddress> resolveHostsFileEntries(String hostname) {
@@ -701,23 +698,54 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) {
701698
.addresses(hostname, resolvedAddressTypes);
702699
} else {
703700
InetAddress address = hostsFileEntriesResolver.address(hostname, resolvedAddressTypes);
704-
addresses = address != null ? Collections.singletonList(address) : null;
701+
addresses = address != null? Collections.singletonList(address) : null;
705702
}
706-
return addresses == null && isLocalWindowsHost(hostname) ?
707-
Collections.singletonList(LOCALHOST_ADDRESS) : addresses;
703+
return addresses == null && isLocalHostAddress(hostname)?
704+
Collections.singletonList(getLocalHostAddress()) : addresses;
708705
}
709706

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

723751
/**
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
+44-3Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
import org.junit.jupiter.api.Test;
7373
import org.junit.jupiter.api.Timeout;
7474
import org.junit.jupiter.api.function.Executable;
75+
import org.junit.jupiter.params.ParameterizedTest;
76+
import org.junit.jupiter.params.provider.Arguments;
77+
import org.junit.jupiter.params.provider.EnumSource;
78+
import org.junit.jupiter.params.provider.MethodSource;
7579

7680
import java.io.IOException;
7781
import java.io.InputStream;
@@ -113,9 +117,6 @@
113117
import java.util.concurrent.atomic.AtomicInteger;
114118
import java.util.concurrent.atomic.AtomicReference;
115119

116-
import org.junit.jupiter.params.ParameterizedTest;
117-
import org.junit.jupiter.params.provider.EnumSource;
118-
119120
import static io.netty.handler.codec.dns.DnsRecordType.A;
120121
import static io.netty.handler.codec.dns.DnsRecordType.AAAA;
121122
import static io.netty.handler.codec.dns.DnsRecordType.CNAME;
@@ -861,6 +862,46 @@ public void testResolveHostNameIpv6(DnsNameResolverChannelStrategy strategy) thr
861862
testResolve0(strategy, ResolvedAddressTypes.IPV6_ONLY, NetUtil.LOCALHOST6, WINDOWS_HOST_NAME);
862863
}
863864

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

0 commit comments

Comments
0 (0)
You are visitor 01402775 — 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.