Java Code Examples for io.netty.resolver.ResolvedAddressTypes#IPV4_ONLY

The following examples show how to use io.netty.resolver.ResolvedAddressTypes#IPV4_ONLY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnsNameResolverBuilder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Compute a {@link ResolvedAddressTypes} from some {@link InternetProtocolFamily}s.
 * An empty input will return the default value, based on "java.net" System properties.
 * Valid inputs are (), (IPv4), (IPv6), (Ipv4, IPv6) and (IPv6, IPv4).
 * @param internetProtocolFamilies a valid sequence of {@link InternetProtocolFamily}s
 * @return a {@link ResolvedAddressTypes}
 */
public static ResolvedAddressTypes computeResolvedAddressTypes(InternetProtocolFamily... internetProtocolFamilies) {
    if (internetProtocolFamilies == null || internetProtocolFamilies.length == 0) {
        return DnsNameResolver.DEFAULT_RESOLVE_ADDRESS_TYPES;
    }
    if (internetProtocolFamilies.length > 2) {
        throw new IllegalArgumentException("No more than 2 InternetProtocolFamilies");
    }

    switch(internetProtocolFamilies[0]) {
        case IPv4:
            return (internetProtocolFamilies.length >= 2
                    && internetProtocolFamilies[1] == InternetProtocolFamily.IPv6) ?
                    ResolvedAddressTypes.IPV4_PREFERRED: ResolvedAddressTypes.IPV4_ONLY;
        case IPv6:
            return (internetProtocolFamilies.length >= 2
                    && internetProtocolFamilies[1] == InternetProtocolFamily.IPv4) ?
                    ResolvedAddressTypes.IPV6_PREFERRED: ResolvedAddressTypes.IPV6_ONLY;
        default:
            throw new IllegalArgumentException(
                    "Couldn't resolve ResolvedAddressTypes from InternetProtocolFamily array");
    }
}
 
Example 2
Source File: DefaultDnsClient.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private static ResolvedAddressTypes toNettyType(final DnsResolverAddressTypes dnsResolverAddressTypes) {
    switch (dnsResolverAddressTypes) {
        case IPV4_ONLY:
            return ResolvedAddressTypes.IPV4_ONLY;
        case IPV6_ONLY:
            return ResolvedAddressTypes.IPV6_ONLY;
        case IPV6_PREFERRED:
            return ResolvedAddressTypes.IPV6_PREFERRED;
        case IPV4_PREFERRED:
            return ResolvedAddressTypes.IPV4_PREFERRED;
        default:
            throw new Error();
    }
}
 
Example 3
Source File: DnsAddressEndpointGroup.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static List<DnsQuestion> newQuestions(
        String hostname, @Nullable ResolvedAddressTypes resolvedAddressTypes) {

    if (resolvedAddressTypes == null) {
        if (NetUtil.isIpV4StackPreferred() || !anyInterfaceSupportsIpV6()) {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_ONLY;
        } else {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_PREFERRED;
        }
    }

    final ImmutableList.Builder<DnsQuestion> builder = ImmutableList.builder();
    switch (resolvedAddressTypes) {
        case IPV4_ONLY:
        case IPV4_PREFERRED:
        case IPV6_PREFERRED:
            builder.add(DnsQuestionWithoutTrailingDot.of(hostname, DnsRecordType.A));
            break;
    }
    switch (resolvedAddressTypes) {
        case IPV6_ONLY:
        case IPV4_PREFERRED:
        case IPV6_PREFERRED:
            builder.add(DnsQuestionWithoutTrailingDot.of(hostname, DnsRecordType.AAAA));
            break;
    }
    return builder.build();
}
 
Example 4
Source File: DnsNameResolverTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static void testRecursiveResolveCache(boolean cache)
        throws Exception {
    final String hostname = "some.record.netty.io";
    final String hostname2 = "some2.record.netty.io";

    final TestDnsServer dnsServerAuthority = new TestDnsServer(new HashSet<String>(
            Arrays.asList(hostname, hostname2)));
    dnsServerAuthority.start();

    TestDnsServer dnsServer = new RedirectingTestDnsServer(hostname,
            dnsServerAuthority.localAddress().getAddress().getHostAddress());
    dnsServer.start();

    TestDnsCache nsCache = new TestDnsCache(cache ? new DefaultDnsCache() : NoopDnsCache.INSTANCE);
    TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
            new TestRecursiveCacheDnsQueryLifecycleObserverFactory();
    EventLoopGroup group = new NioEventLoopGroup(1);
    DnsNameResolver resolver = new DnsNameResolver(
            group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class),
            NoopDnsCache.INSTANCE, nsCache, lifecycleObserverFactory, 3000, ResolvedAddressTypes.IPV4_ONLY, true,
            10, true, 4096, false, HostsFileEntriesResolver.DEFAULT,
            new SingletonDnsServerAddressStreamProvider(dnsServer.localAddress()),
            DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {
        @Override
        int dnsRedirectPort(InetAddress server) {
            return server.equals(dnsServerAuthority.localAddress().getAddress()) ?
                    dnsServerAuthority.localAddress().getPort() : DNS_PORT;
        }
    };

    // Java7 will strip of the "." so we need to adjust the expected dnsname. Both are valid in terms of the RFC
    // so its ok.
    String expectedDnsName = PlatformDependent.javaVersion() == 7 ?
            "dns4.some.record.netty.io" : "dns4.some.record.netty.io.";

    try {
        resolver.resolveAll(hostname).syncUninterruptibly();

        TestDnsQueryLifecycleObserver observer = lifecycleObserverFactory.observers.poll();
        assertNotNull(observer);
        assertTrue(lifecycleObserverFactory.observers.isEmpty());
        assertEquals(4, observer.events.size());
        QueryWrittenEvent writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
        assertEquals(dnsServer.localAddress(), writtenEvent1.dnsServerAddress);
        QueryRedirectedEvent redirectedEvent = (QueryRedirectedEvent) observer.events.poll();

        assertEquals(expectedDnsName, redirectedEvent.nameServers.get(0).getHostName());
        assertEquals(dnsServerAuthority.localAddress(), redirectedEvent.nameServers.get(0));
        QueryWrittenEvent writtenEvent2 = (QueryWrittenEvent) observer.events.poll();
        assertEquals(dnsServerAuthority.localAddress(), writtenEvent2.dnsServerAddress);
        QuerySucceededEvent succeededEvent = (QuerySucceededEvent) observer.events.poll();

        if (cache) {
            assertNull(nsCache.cache.get("io.", null));
            assertNull(nsCache.cache.get("netty.io.", null));
            List<? extends DnsCacheEntry> entries = nsCache.cache.get("record.netty.io.", null);
            assertEquals(1, entries.size());

            assertNull(nsCache.cache.get(hostname, null));

            // Test again via cache.
            resolver.resolveAll(hostname).syncUninterruptibly();

            observer = lifecycleObserverFactory.observers.poll();
            assertNotNull(observer);
            assertTrue(lifecycleObserverFactory.observers.isEmpty());
            assertEquals(2, observer.events.size());
            writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
            assertEquals(expectedDnsName, writtenEvent1.dnsServerAddress.getHostName());
            assertEquals(dnsServerAuthority.localAddress(), writtenEvent1.dnsServerAddress);
            succeededEvent = (QuerySucceededEvent) observer.events.poll();

            resolver.resolveAll(hostname2).syncUninterruptibly();

            observer = lifecycleObserverFactory.observers.poll();
            assertNotNull(observer);
            assertTrue(lifecycleObserverFactory.observers.isEmpty());
            assertEquals(2, observer.events.size());
            writtenEvent1 = (QueryWrittenEvent) observer.events.poll();
            assertEquals(expectedDnsName, writtenEvent1.dnsServerAddress.getHostName());
            assertEquals(dnsServerAuthority.localAddress(), writtenEvent1.dnsServerAddress);
            succeededEvent = (QuerySucceededEvent) observer.events.poll();

            // Check that it only queried the cache for record.netty.io.
            assertNull(nsCache.cacheHits.get("io."));
            assertNull(nsCache.cacheHits.get("netty.io."));
            assertNotNull(nsCache.cacheHits.get("record.netty.io."));
            assertNull(nsCache.cacheHits.get("some.record.netty.io."));
        }
    } finally {
        resolver.close();
        group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
        dnsServer.stop();
        dnsServerAuthority.stop();
    }
}