Search in sources :

Example 1 with DnsServerAddresses

use of io.netty.resolver.dns.DnsServerAddresses in project netty by netty.

the class MacOSDnsServerAddressStreamProvider method retrieveCurrentMappings.

private static Map<String, DnsServerAddresses> retrieveCurrentMappings() {
    DnsResolver[] resolvers = resolvers();
    if (resolvers == null || resolvers.length == 0) {
        return Collections.emptyMap();
    }
    Arrays.sort(resolvers, RESOLVER_COMPARATOR);
    Map<String, DnsServerAddresses> resolverMap = new HashMap<String, DnsServerAddresses>(resolvers.length);
    for (DnsResolver resolver : resolvers) {
        // Skip mdns
        if ("mdns".equalsIgnoreCase(resolver.options())) {
            continue;
        }
        InetSocketAddress[] nameservers = resolver.nameservers();
        if (nameservers == null || nameservers.length == 0) {
            continue;
        }
        String domain = resolver.domain();
        if (domain == null) {
            // Default mapping.
            domain = StringUtil.EMPTY_STRING;
        }
        InetSocketAddress[] servers = resolver.nameservers();
        for (int a = 0; a < servers.length; a++) {
            InetSocketAddress address = servers[a];
            // Check if the default port should be used
            if (address.getPort() == 0) {
                int port = resolver.port();
                if (port == 0) {
                    port = 53;
                }
                servers[a] = new InetSocketAddress(address.getAddress(), port);
            }
        }
        resolverMap.put(domain, DnsServerAddresses.sequential(servers));
    }
    return resolverMap;
}
Also used : DnsServerAddresses(io.netty.resolver.dns.DnsServerAddresses) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with DnsServerAddresses

use of io.netty.resolver.dns.DnsServerAddresses in project netty by netty.

the class MacOSDnsServerAddressStreamProvider method nameServerAddressStream.

@Override
public DnsServerAddressStream nameServerAddressStream(String hostname) {
    long last = lastRefresh.get();
    Map<String, DnsServerAddresses> resolverMap = currentMappings;
    if (System.nanoTime() - last > REFRESH_INTERVAL) {
        // amount of time, but that's ok.
        if (lastRefresh.compareAndSet(last, System.nanoTime())) {
            resolverMap = currentMappings = retrieveCurrentMappings();
        }
    }
    final String originalHostname = hostname;
    for (; ; ) {
        int i = hostname.indexOf('.', 1);
        if (i < 0 || i == hostname.length() - 1) {
            // Try access default mapping.
            DnsServerAddresses addresses = resolverMap.get(StringUtil.EMPTY_STRING);
            if (addresses != null) {
                return addresses.stream();
            }
            return DnsServerAddressStreamProviders.unixDefault().nameServerAddressStream(originalHostname);
        }
        DnsServerAddresses addresses = resolverMap.get(hostname);
        if (addresses != null) {
            return addresses.stream();
        }
        hostname = hostname.substring(i + 1);
    }
}
Also used : DnsServerAddresses(io.netty.resolver.dns.DnsServerAddresses)

Aggregations

DnsServerAddresses (io.netty.resolver.dns.DnsServerAddresses)2 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1