netty / netty

Netty project - an event-driven asynchronous network application framework
http://netty.io
Apache License 2.0
33.42k stars 15.92k forks source link

DNS nameservers queried in wrong order #13482

Open nir2 opened 1 year ago

nir2 commented 1 year ago

Expected behavior

On Windows, the system default DNS nameservers should be queried in same order as in the Windows operating system

Actual behavior

The DNS nameservers are queried in reverse order

Steps to reproduce

Have a setup with multiple nameservers, e.g. one nameserver from your internet provider (192.168.x.x) and one from an OpenVPN connection (172.16.x.x). In the Windows operating system first the nameserver from the OpenVPN connection is queried, and afterwards, if this fails, the ISP nameserver is queried. In Netty the opposite search order is used.

Minimal yet complete reproducer code (or URL to code)

(code example is in Scala)

private val resolverGroup = new DnsAddressResolverGroup(classOf[NioDatagramChannel], DefaultDnsServerAddressStreamProvider.INSTANCE)`

and later:

   def clientConnect(context: ConnectContext.Client): ChannelFuture = {
      logger.debug(s"CONNECTING [${context.address.uri}]")
      val b: Bootstrap = new Bootstrap
      b.group(clientGroup).channel(classOf[NioSocketChannel])
      // Set the Bootstrap.resolver to a non-blocking DnsResolver (though getting a list of default dns servers itself may be blocking,
      // cite from the DefaultDnsServerAddressStreamProvider documentation: "This may use the JDK's blocking DNS resolution to bootstrap
      // the default DNS server addresses.")
      b.resolver(resolverGroup)

I think that the problem is, that in the Netty code the nameserver search list is determined by only calling via reflection sun.net.dns.ResolverConfiguration.nameservers but I think that also
sun.net.dns.ResolverConfiguration.searchlist should be used to sort the nameservers into the right order

JDK code snippet:

    /**
     * Returns a list corresponding to the domain search path. The
     * list is ordered by the search order used for host name lookup.
     * Each element in the list returns a {@link java.lang.String}
     * containing a domain name or suffix.
     *
     * @return list of domain names
     */
    public abstract List<String> searchlist();

    /**
     * Returns a list of name servers used for host name lookup.
     * Each element in the list returns a {@link java.lang.String}
     * containing the textual representation of the IP address of
     * the name server.
     *
     * @return list of the name servers
     */
    public abstract List<String> nameservers();

Netty version

4.1.86

JVM version (e.g. java -version)

IMPLEMENTOR="Azul Systems, Inc." IMPLEMENTOR_VERSION="Zulu11.56+19-CA" JAVA_VERSION="11.0.15" JAVA_VERSION_DATE="2022-04-19"

OS version (e.g. uname -a)

Windows 11, Build 22H2

nir2 commented 1 year ago

Ps: I just saw, in the Netty API documentation you find the line: "These defaults do not respect your host's machines defaults". So this rather is a feature request, and not a bug report.

/**
 * A {@link DnsServerAddressStreamProvider} which will use predefined default DNS servers to use for DNS resolution.
 * These defaults do not respect your host's machines defaults.
 * <p>
 * This may use the JDK's blocking DNS resolution to bootstrap the default DNS server addresses.
 */
public final class DefaultDnsServerAddressStreamProvider implements DnsServerAddressStreamProvider {