Class DnsNameResolver

    • Field Detail

      • WINDOWS_HOST_NAME

        private static final java.lang.String WINDOWS_HOST_NAME
      • LOCALHOST_ADDRESS

        private static final java.net.InetAddress LOCALHOST_ADDRESS
      • EMPTY_ADDITIONALS

        private static final DnsRecord[] EMPTY_ADDITIONALS
      • IPV4_ONLY_RESOLVED_RECORD_TYPES

        private static final DnsRecordType[] IPV4_ONLY_RESOLVED_RECORD_TYPES
      • IPV4_ONLY_RESOLVED_PROTOCOL_FAMILIES

        private static final InternetProtocolFamily[] IPV4_ONLY_RESOLVED_PROTOCOL_FAMILIES
      • IPV4_PREFERRED_RESOLVED_RECORD_TYPES

        private static final DnsRecordType[] IPV4_PREFERRED_RESOLVED_RECORD_TYPES
      • IPV4_PREFERRED_RESOLVED_PROTOCOL_FAMILIES

        private static final InternetProtocolFamily[] IPV4_PREFERRED_RESOLVED_PROTOCOL_FAMILIES
      • IPV6_ONLY_RESOLVED_RECORD_TYPES

        private static final DnsRecordType[] IPV6_ONLY_RESOLVED_RECORD_TYPES
      • IPV6_ONLY_RESOLVED_PROTOCOL_FAMILIES

        private static final InternetProtocolFamily[] IPV6_ONLY_RESOLVED_PROTOCOL_FAMILIES
      • IPV6_PREFERRED_RESOLVED_RECORD_TYPES

        private static final DnsRecordType[] IPV6_PREFERRED_RESOLVED_RECORD_TYPES
      • IPV6_PREFERRED_RESOLVED_PROTOCOL_FAMILIES

        private static final InternetProtocolFamily[] IPV6_PREFERRED_RESOLVED_PROTOCOL_FAMILIES
      • DEFAULT_SEARCH_DOMAINS

        static final java.lang.String[] DEFAULT_SEARCH_DOMAINS
      • channelReadyPromise

        private final Promise<Channel> channelReadyPromise
      • nameServerComparator

        private final java.util.Comparator<java.net.InetSocketAddress> nameServerComparator
      • queryTimeoutMillis

        private final long queryTimeoutMillis
      • maxQueriesPerResolve

        private final int maxQueriesPerResolve
      • recursionDesired

        private final boolean recursionDesired
      • maxPayloadSize

        private final int maxPayloadSize
      • optResourceEnabled

        private final boolean optResourceEnabled
      • searchDomains

        private final java.lang.String[] searchDomains
      • ndots

        private final int ndots
      • supportsAAAARecords

        private final boolean supportsAAAARecords
      • supportsARecords

        private final boolean supportsARecords
      • resolveRecordTypes

        private final DnsRecordType[] resolveRecordTypes
      • decodeIdn

        private final boolean decodeIdn
      • completeOncePreferredResolved

        private final boolean completeOncePreferredResolved
      • socketBootstrap

        private final Bootstrap socketBootstrap
      • maxNumConsolidation

        private final int maxNumConsolidation
      • inflightLookups

        private final java.util.Map<java.lang.String,​Future<java.util.List<java.net.InetAddress>>> inflightLookups
    • Constructor Detail

      • DnsNameResolver

        @Deprecated
        public DnsNameResolver​(EventLoop eventLoop,
                               ChannelFactory<? extends DatagramChannel> channelFactory,
                               DnsCache resolveCache,
                               DnsCache authoritativeDnsServerCache,
                               DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
                               long queryTimeoutMillis,
                               ResolvedAddressTypes resolvedAddressTypes,
                               boolean recursionDesired,
                               int maxQueriesPerResolve,
                               boolean traceEnabled,
                               int maxPayloadSize,
                               boolean optResourceEnabled,
                               HostsFileEntriesResolver hostsFileEntriesResolver,
                               DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
                               java.lang.String[] searchDomains,
                               int ndots,
                               boolean decodeIdn)
        Deprecated.
        Creates a new DNS-based name resolver that communicates with the specified list of DNS servers.
        Parameters:
        eventLoop - the EventLoop which will perform the communication with the DNS servers
        channelFactory - the ChannelFactory that will create a DatagramChannel
        resolveCache - the DNS resolved entries cache
        authoritativeDnsServerCache - the cache used to find the authoritative DNS server for a domain
        dnsQueryLifecycleObserverFactory - used to generate new instances of DnsQueryLifecycleObserver which can be used to track metrics for DNS servers.
        queryTimeoutMillis - timeout of each DNS query in millis. 0 disables the timeout. If not set or a negative number is set, the default timeout is used.
        resolvedAddressTypes - the preferred address types
        recursionDesired - if recursion desired flag must be set
        maxQueriesPerResolve - the maximum allowed number of DNS queries for a given name resolution
        traceEnabled - if trace is enabled
        maxPayloadSize - the capacity of the datagram packet buffer
        optResourceEnabled - if automatic inclusion of a optional records is enabled
        hostsFileEntriesResolver - the HostsFileEntriesResolver used to check for local aliases
        dnsServerAddressStreamProvider - The DnsServerAddressStreamProvider used to determine the name servers for each hostname lookup.
        searchDomains - the list of search domain (can be null, if so, will try to default to the underlying platform ones)
        ndots - the ndots value
        decodeIdn - true if domain / host names should be decoded to unicode when received. See rfc3492.
      • DnsNameResolver

        @Deprecated
        public DnsNameResolver​(EventLoop eventLoop,
                               ChannelFactory<? extends DatagramChannel> channelFactory,
                               DnsCache resolveCache,
                               AuthoritativeDnsServerCache authoritativeDnsServerCache,
                               DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
                               long queryTimeoutMillis,
                               ResolvedAddressTypes resolvedAddressTypes,
                               boolean recursionDesired,
                               int maxQueriesPerResolve,
                               boolean traceEnabled,
                               int maxPayloadSize,
                               boolean optResourceEnabled,
                               HostsFileEntriesResolver hostsFileEntriesResolver,
                               DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
                               java.lang.String[] searchDomains,
                               int ndots,
                               boolean decodeIdn)
        Deprecated.
        Creates a new DNS-based name resolver that communicates with the specified list of DNS servers.
        Parameters:
        eventLoop - the EventLoop which will perform the communication with the DNS servers
        channelFactory - the ChannelFactory that will create a DatagramChannel
        resolveCache - the DNS resolved entries cache
        authoritativeDnsServerCache - the cache used to find the authoritative DNS server for a domain
        dnsQueryLifecycleObserverFactory - used to generate new instances of DnsQueryLifecycleObserver which can be used to track metrics for DNS servers.
        queryTimeoutMillis - timeout of each DNS query in millis. 0 disables the timeout. If not set or a negative number is set, the default timeout is used.
        resolvedAddressTypes - the preferred address types
        recursionDesired - if recursion desired flag must be set
        maxQueriesPerResolve - the maximum allowed number of DNS queries for a given name resolution
        traceEnabled - if trace is enabled
        maxPayloadSize - the capacity of the datagram packet buffer
        optResourceEnabled - if automatic inclusion of a optional records is enabled
        hostsFileEntriesResolver - the HostsFileEntriesResolver used to check for local aliases
        dnsServerAddressStreamProvider - The DnsServerAddressStreamProvider used to determine the name servers for each hostname lookup.
        searchDomains - the list of search domain (can be null, if so, will try to default to the underlying platform ones)
        ndots - the ndots value
        decodeIdn - true if domain / host names should be decoded to unicode when received. See rfc3492.
    • Method Detail

      • anyInterfaceSupportsIpV6

        private static boolean anyInterfaceSupportsIpV6()
        Returns true if any NetworkInterface supports IPv6, false otherwise.
      • getSearchDomainsHack

        private static java.util.List<java.lang.String> getSearchDomainsHack()
                                                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • newRedirectServerAddress

        java.net.InetSocketAddress newRedirectServerAddress​(java.net.InetAddress server)
      • newRedirectDnsServerStream

        protected DnsServerAddressStream newRedirectDnsServerStream​(java.lang.String hostname,
                                                                    java.util.List<java.net.InetSocketAddress> nameservers)
        Creates a new DnsServerAddressStream to following a redirected DNS query. By overriding this it provides the opportunity to sort the name servers before following a redirected DNS query.
        Parameters:
        hostname - the hostname.
        nameservers - The addresses of the DNS servers which are used in the event of a redirect. This may contain resolved and unresolved addresses so the used DnsServerAddressStream must allow unresolved addresses if you want to include these as well.
        Returns:
        A DnsServerAddressStream which will be used to follow the DNS redirect or null if none should be followed.
      • resolveCache

        public DnsCache resolveCache()
        Returns the resolution cache.
      • authoritativeDnsServerCache

        public AuthoritativeDnsServerCache authoritativeDnsServerCache()
        Returns the cache used for authoritative DNS servers for a domain.
      • queryTimeoutMillis

        public long queryTimeoutMillis()
        Returns the timeout of each DNS query performed by this resolver (in milliseconds). The default value is 5 seconds.
      • searchDomains

        final java.lang.String[] searchDomains()
      • ndots

        final int ndots()
      • supportsAAAARecords

        final boolean supportsAAAARecords()
      • supportsARecords

        final boolean supportsARecords()
      • resolveRecordTypes

        final DnsRecordType[] resolveRecordTypes()
      • isDecodeIdn

        final boolean isDecodeIdn()
      • isRecursionDesired

        public boolean isRecursionDesired()
        Returns true if and only if this resolver sends a DNS query with the RD (recursion desired) flag set. The default value is true.
      • maxQueriesPerResolve

        public int maxQueriesPerResolve()
        Returns the maximum allowed number of DNS queries to send when resolving a host name. The default value is 8.
      • maxPayloadSize

        public int maxPayloadSize()
        Returns the capacity of the datagram packet buffer (in bytes). The default value is 4096 bytes.
      • isOptResourceEnabled

        public boolean isOptResourceEnabled()
        Returns the automatic inclusion of a optional records that tries to give the remote DNS server a hint about how much data the resolver can read per response is enabled.
      • hostsFileEntriesResolver

        public HostsFileEntriesResolver hostsFileEntriesResolver()
        Returns the component that tries to resolve hostnames against the hosts file prior to asking to remotes DNS servers.
      • close

        public void close()
        Closes the internal datagram channel used for sending and receiving DNS messages, and clears all DNS resource records from the cache. Attempting to send a DNS query or to resolve a domain name will fail once this method has been called.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface NameResolver<java.net.InetAddress>
        Overrides:
        close in class SimpleNameResolver<java.net.InetAddress>
      • resolveHostsFileEntry

        private java.net.InetAddress resolveHostsFileEntry​(java.lang.String hostname)
      • resolveHostsFileEntries

        private java.util.List<java.net.InetAddress> resolveHostsFileEntries​(java.lang.String hostname)
      • isLocalWindowsHost

        private static boolean isLocalWindowsHost​(java.lang.String hostname)
        Checks whether the given hostname is the localhost/host (computer) name on Windows OS. Windows OS removed the localhost/host (computer) name information from the hosts file in the later versions and such hostname cannot be resolved from hosts file. See https://github.com/netty/netty/issues/5386 See https://github.com/netty/netty/issues/11142
      • resolve

        public final Future<java.net.InetAddress> resolve​(java.lang.String inetHost,
                                                          java.lang.Iterable<DnsRecord> additionals)
        Resolves the specified name into an address.
        Parameters:
        inetHost - the name to resolve
        additionals - additional records (OPT)
        Returns:
        the address as the result of the resolution
      • resolve

        public final Future<java.net.InetAddress> resolve​(java.lang.String inetHost,
                                                          java.lang.Iterable<DnsRecord> additionals,
                                                          Promise<java.net.InetAddress> promise)
        Resolves the specified name into an address.
        Parameters:
        inetHost - the name to resolve
        additionals - additional records (OPT)
        promise - the Promise which will be fulfilled when the name resolution is finished
        Returns:
        the address as the result of the resolution
      • resolveAll

        public final Future<java.util.List<java.net.InetAddress>> resolveAll​(java.lang.String inetHost,
                                                                             java.lang.Iterable<DnsRecord> additionals)
        Resolves the specified host name and port into a list of address.
        Parameters:
        inetHost - the name to resolve
        additionals - additional records (OPT)
        Returns:
        the list of the address as the result of the resolution
      • resolveAll

        public final Future<java.util.List<java.net.InetAddress>> resolveAll​(java.lang.String inetHost,
                                                                             java.lang.Iterable<DnsRecord> additionals,
                                                                             Promise<java.util.List<java.net.InetAddress>> promise)
        Resolves the specified host name and port into a list of address.
        Parameters:
        inetHost - the name to resolve
        additionals - additional records (OPT)
        promise - the Promise which will be fulfilled when the name resolution is finished
        Returns:
        the list of the address as the result of the resolution
      • resolveAll

        public final Future<java.util.List<DnsRecord>> resolveAll​(DnsQuestion question)
        Resolves the DnsRecords that are matched by the specified DnsQuestion. Unlike query(DnsQuestion), this method handles redirection, CNAMEs and multiple name servers. If the specified DnsQuestion is A or AAAA, this method looks up the configured HostsFileEntries before sending a query to the name servers. If a match is found in the HostsFileEntries, a synthetic A or AAAA record will be returned.
        Parameters:
        question - the question
        Returns:
        the list of the DnsRecords as the result of the resolution
      • resolveAll

        public final Future<java.util.List<DnsRecord>> resolveAll​(DnsQuestion question,
                                                                  java.lang.Iterable<DnsRecord> additionals)
        Resolves the DnsRecords that are matched by the specified DnsQuestion. Unlike query(DnsQuestion), this method handles redirection, CNAMEs and multiple name servers. If the specified DnsQuestion is A or AAAA, this method looks up the configured HostsFileEntries before sending a query to the name servers. If a match is found in the HostsFileEntries, a synthetic A or AAAA record will be returned.
        Parameters:
        question - the question
        additionals - additional records (OPT)
        Returns:
        the list of the DnsRecords as the result of the resolution
      • resolveAll

        public final Future<java.util.List<DnsRecord>> resolveAll​(DnsQuestion question,
                                                                  java.lang.Iterable<DnsRecord> additionals,
                                                                  Promise<java.util.List<DnsRecord>> promise)
        Resolves the DnsRecords that are matched by the specified DnsQuestion. Unlike query(DnsQuestion), this method handles redirection, CNAMEs and multiple name servers. If the specified DnsQuestion is A or AAAA, this method looks up the configured HostsFileEntries before sending a query to the name servers. If a match is found in the HostsFileEntries, a synthetic A or AAAA record will be returned.
        Parameters:
        question - the question
        additionals - additional records (OPT)
        promise - the Promise which will be fulfilled when the resolution is finished
        Returns:
        the list of the DnsRecords as the result of the resolution
      • toArray

        private static DnsRecord[] toArray​(java.lang.Iterable<DnsRecord> additionals,
                                           boolean validateType)
      • validateAdditional

        private static void validateAdditional​(DnsRecord record,
                                               boolean validateType)
      • loopbackAddress

        private java.net.InetAddress loopbackAddress()
      • doResolve

        protected void doResolve​(java.lang.String inetHost,
                                 DnsRecord[] additionals,
                                 Promise<java.net.InetAddress> promise,
                                 DnsCache resolveCache)
                          throws java.lang.Exception
        Hook designed for extensibility so one can pass a different cache on each resolution attempt instead of using the global one.
        Throws:
        java.lang.Exception
      • doResolveCached

        private boolean doResolveCached​(java.lang.String hostname,
                                        DnsRecord[] additionals,
                                        Promise<java.net.InetAddress> promise,
                                        DnsCache resolveCache)
      • trySuccess

        static <T> boolean trySuccess​(Promise<T> promise,
                                      T result)
      • tryFailure

        private static void tryFailure​(Promise<?> promise,
                                       java.lang.Throwable cause)
      • doResolveUncached

        private void doResolveUncached​(java.lang.String hostname,
                                       DnsRecord[] additionals,
                                       Promise<java.net.InetAddress> promise,
                                       DnsCache resolveCache,
                                       boolean completeEarlyIfPossible)
      • doResolveAll

        protected void doResolveAll​(java.lang.String inetHost,
                                    DnsRecord[] additionals,
                                    Promise<java.util.List<java.net.InetAddress>> promise,
                                    DnsCache resolveCache)
                             throws java.lang.Exception
        Hook designed for extensibility so one can pass a different cache on each resolution attempt instead of using the global one.
        Throws:
        java.lang.Exception
      • doResolveAllCached

        static boolean doResolveAllCached​(java.lang.String hostname,
                                          DnsRecord[] additionals,
                                          Promise<java.util.List<java.net.InetAddress>> promise,
                                          DnsCache resolveCache,
                                          InternetProtocolFamily[] resolvedInternetProtocolFamilies)
      • doResolveAllUncached

        private void doResolveAllUncached​(java.lang.String hostname,
                                          DnsRecord[] additionals,
                                          Promise<?> originalPromise,
                                          Promise<java.util.List<java.net.InetAddress>> promise,
                                          DnsCache resolveCache,
                                          boolean completeEarlyIfPossible)
      • doResolveAllUncached0

        private void doResolveAllUncached0​(java.lang.String hostname,
                                           DnsRecord[] additionals,
                                           Promise<?> originalPromise,
                                           Promise<java.util.List<java.net.InetAddress>> promise,
                                           DnsCache resolveCache,
                                           boolean completeEarlyIfPossible)
      • resolveNow

        private void resolveNow​(java.lang.String hostname,
                                DnsRecord[] additionals,
                                Promise<?> originalPromise,
                                Promise<java.util.List<java.net.InetAddress>> promise,
                                DnsCache resolveCache,
                                boolean completeEarlyIfPossible)
      • hostname

        private static java.lang.String hostname​(java.lang.String inetHost)
      • nextNameServerAddress

        private java.net.InetSocketAddress nextNameServerAddress()
      • query

        public Future<AddressedEnvelope<DnsResponse,​java.net.InetSocketAddress>> query​(java.net.InetSocketAddress nameServerAddr,
                                                                                             DnsQuestion question)
        Sends a DNS query with the specified question using the specified name server list.
      • query

        public Future<AddressedEnvelope<DnsResponse,​java.net.InetSocketAddress>> query​(java.net.InetSocketAddress nameServerAddr,
                                                                                             DnsQuestion question,
                                                                                             java.lang.Iterable<DnsRecord> additionals)
        Sends a DNS query with the specified question with additional records using the specified name server list.
      • isTransportOrTimeoutError

        public static boolean isTransportOrTimeoutError​(java.lang.Throwable cause)
        Returns true if the Throwable was caused by an timeout or transport error. These methods can be used on the Future.cause() that is returned by the various methods exposed by this DnsNameResolver.
      • isTimeoutError

        public static boolean isTimeoutError​(java.lang.Throwable cause)
        Returns true if the Throwable was caused by an timeout. These methods can be used on the Future.cause() that is returned by the various methods exposed by this DnsNameResolver.
      • flushQueries

        final void flushQueries()
      • newNameServerAddressStream

        final DnsServerAddressStream newNameServerAddressStream​(java.lang.String hostname)