Search in sources :

Example 1 with InternetProtocolFamily

use of io.netty.channel.socket.InternetProtocolFamily in project netty by netty.

the class DnsNameResolverTest method testResolve0.

private static Map<String, InetAddress> testResolve0(DnsNameResolver resolver, Set<String> excludedDomains) throws InterruptedException {
    assertThat(resolver.isRecursionDesired(), is(true));
    final Map<String, InetAddress> results = new HashMap<String, InetAddress>();
    final Map<String, Future<InetAddress>> futures = new LinkedHashMap<String, Future<InetAddress>>();
    for (String name : DOMAINS) {
        if (excludedDomains.contains(name)) {
            continue;
        }
        resolve(resolver, futures, name);
    }
    for (Entry<String, Future<InetAddress>> e : futures.entrySet()) {
        String unresolved = e.getKey();
        InetAddress resolved = e.getValue().sync().getNow();
        logger.info("{}: {}", unresolved, resolved.getHostAddress());
        assertThat(resolved.getHostName(), is(unresolved));
        boolean typeMatches = false;
        for (InternetProtocolFamily f : resolver.resolvedInternetProtocolFamiliesUnsafe()) {
            Class<?> resolvedType = resolved.getClass();
            if (f.addressType().isAssignableFrom(resolvedType)) {
                typeMatches = true;
            }
        }
        assertThat(typeMatches, is(true));
        results.put(resolved.getHostName(), resolved);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) Future(io.netty.util.concurrent.Future) InetAddress(java.net.InetAddress) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with InternetProtocolFamily

use of io.netty.channel.socket.InternetProtocolFamily in project netty by netty.

the class DnsNameResolver method doResolveAllCached.

private boolean doResolveAllCached(String hostname, DnsRecord[] additionals, Promise<List<InetAddress>> promise, DnsCache resolveCache) {
    final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname, additionals);
    if (cachedEntries == null || cachedEntries.isEmpty()) {
        return false;
    }
    List<InetAddress> result = null;
    Throwable cause = null;
    synchronized (cachedEntries) {
        final int numEntries = cachedEntries.size();
        assert numEntries > 0;
        if (cachedEntries.get(0).cause() != null) {
            cause = cachedEntries.get(0).cause();
        } else {
            for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
                for (int i = 0; i < numEntries; i++) {
                    final DnsCacheEntry e = cachedEntries.get(i);
                    if (f.addressType().isInstance(e.address())) {
                        if (result == null) {
                            result = new ArrayList<InetAddress>(numEntries);
                        }
                        result.add(e.address());
                    }
                }
            }
        }
    }
    if (result != null) {
        trySuccess(promise, result);
        return true;
    }
    if (cause != null) {
        tryFailure(promise, cause);
        return true;
    }
    return false;
}
Also used : InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) InetAddress(java.net.InetAddress)

Example 3 with InternetProtocolFamily

use of io.netty.channel.socket.InternetProtocolFamily in project netty by netty.

the class DnsNameResolver method doResolveCached.

private boolean doResolveCached(String hostname, DnsRecord[] additionals, Promise<InetAddress> promise, DnsCache resolveCache) {
    final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname, additionals);
    if (cachedEntries == null || cachedEntries.isEmpty()) {
        return false;
    }
    InetAddress address = null;
    Throwable cause = null;
    synchronized (cachedEntries) {
        final int numEntries = cachedEntries.size();
        assert numEntries > 0;
        if (cachedEntries.get(0).cause() != null) {
            cause = cachedEntries.get(0).cause();
        } else {
            // Find the first entry with the preferred address type.
            for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
                for (int i = 0; i < numEntries; i++) {
                    final DnsCacheEntry e = cachedEntries.get(i);
                    if (f.addressType().isInstance(e.address())) {
                        address = e.address();
                        break;
                    }
                }
            }
        }
    }
    if (address != null) {
        trySuccess(promise, address);
        return true;
    }
    if (cause != null) {
        tryFailure(promise, cause);
        return true;
    }
    return false;
}
Also used : InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) InetAddress(java.net.InetAddress)

Example 4 with InternetProtocolFamily

use of io.netty.channel.socket.InternetProtocolFamily in project netty by netty.

the class DnsNameResolverContext method finishResolve.

private void finishResolve(Promise<T> promise) {
    if (!queriesInProgress.isEmpty()) {
        // If there are queries in progress, we should cancel it because we already finished the resolution.
        for (Iterator<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> i = queriesInProgress.iterator(); i.hasNext(); ) {
            Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = i.next();
            i.remove();
            if (!f.cancel(false)) {
                f.addListener(RELEASE_RESPONSE);
            }
        }
    }
    if (resolvedEntries != null) {
        // Found at least one resolved address.
        for (InternetProtocolFamily f : resolvedInternetProtocolFamilies) {
            if (finishResolve(f.addressType(), resolvedEntries, promise)) {
                return;
            }
        }
    }
    // No resolved address found.
    final int tries = maxAllowedQueries - allowedQueries;
    final StringBuilder buf = new StringBuilder(64);
    buf.append("failed to resolve '");
    if (pristineHostname != null) {
        buf.append(pristineHostname);
    } else {
        buf.append(hostname);
    }
    buf.append('\'');
    if (tries > 1) {
        if (tries < maxAllowedQueries) {
            buf.append(" after ").append(tries).append(" queries ");
        } else {
            buf.append(". Exceeded max queries per resolve ").append(maxAllowedQueries).append(' ');
        }
    }
    if (trace != null) {
        buf.append(':').append(trace);
    }
    final UnknownHostException cause = new UnknownHostException(buf.toString());
    resolveCache.cache(hostname, additionals, cause, parent.ch.eventLoop());
    promise.tryFailure(cause);
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) DnsResponse(io.netty.handler.codec.dns.DnsResponse) InternetProtocolFamily(io.netty.channel.socket.InternetProtocolFamily) Future(io.netty.util.concurrent.Future)

Aggregations

InternetProtocolFamily (io.netty.channel.socket.InternetProtocolFamily)4 InetAddress (java.net.InetAddress)3 Future (io.netty.util.concurrent.Future)2 AddressedEnvelope (io.netty.channel.AddressedEnvelope)1 DnsResponse (io.netty.handler.codec.dns.DnsResponse)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1