Search in sources :

Example 6 with DnsMessage

use of org.apache.directory.server.dns.messages.DnsMessage in project netty by netty.

the class DnsNameResolverTest method testMultipleAdditionalRecordsForSameNSRecord.

private static void testMultipleAdditionalRecordsForSameNSRecord(final boolean reversed) throws Exception {
    final String domain = "netty.io";
    final String hostname = "test.netty.io";
    final String ns1Name = "ns1." + domain;
    final InetSocketAddress ns1Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 1 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
    final InetSocketAddress ns2Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 2 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
    final InetSocketAddress ns3Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 3 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
    final InetSocketAddress ns4Address = new InetSocketAddress(InetAddress.getByAddress(ns1Name, new byte[] { 10, 0, 0, 4 }), DefaultDnsServerAddressStreamProvider.DNS_PORT);
    TestDnsServer redirectServer = new TestDnsServer(new HashSet<String>(asList(hostname, ns1Name))) {

        @Override
        protected DnsMessage filterMessage(DnsMessage message) {
            for (QuestionRecord record : message.getQuestionRecords()) {
                if (record.getDomainName().equals(hostname)) {
                    message.getAdditionalRecords().clear();
                    message.getAnswerRecords().clear();
                    message.getAuthorityRecords().add(TestDnsServer.newNsRecord(domain, ns1Name));
                    message.getAdditionalRecords().add(newARecord(ns1Address));
                    message.getAdditionalRecords().add(newARecord(ns2Address));
                    message.getAdditionalRecords().add(newARecord(ns3Address));
                    message.getAdditionalRecords().add(newARecord(ns4Address));
                    return message;
                }
            }
            return message;
        }

        private ResourceRecord newARecord(InetSocketAddress address) {
            return newARecord(address.getHostName(), address.getAddress().getHostAddress());
        }
    };
    redirectServer.start();
    EventLoopGroup group = new NioEventLoopGroup(1);
    final List<InetSocketAddress> cached = new CopyOnWriteArrayList<InetSocketAddress>();
    final AuthoritativeDnsServerCache authoritativeDnsServerCache = new AuthoritativeDnsServerCache() {

        @Override
        public DnsServerAddressStream get(String hostname) {
            return null;
        }

        @Override
        public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
            cached.add(address);
        }

        @Override
        public void clear() {
        // NOOP
        }

        @Override
        public boolean clear(String hostname) {
            return false;
        }
    };
    final AtomicReference<DnsServerAddressStream> redirectedRef = new AtomicReference<DnsServerAddressStream>();
    final DnsNameResolver resolver = new DnsNameResolver(group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class), NoopDnsCache.INSTANCE, authoritativeDnsServerCache, NoopDnsQueryLifecycleObserverFactory.INSTANCE, 2000, ResolvedAddressTypes.IPV4_ONLY, true, 10, true, 4096, false, HostsFileEntriesResolver.DEFAULT, new SingletonDnsServerAddressStreamProvider(redirectServer.localAddress()), DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {

        @Override
        protected DnsServerAddressStream newRedirectDnsServerStream(String hostname, List<InetSocketAddress> nameservers) {
            if (reversed) {
                Collections.reverse(nameservers);
            }
            DnsServerAddressStream stream = new SequentialDnsServerAddressStream(nameservers, 0);
            redirectedRef.set(stream);
            return stream;
        }
    };
    try {
        Throwable cause = resolver.resolveAll(hostname).await().cause();
        assertTrue(cause instanceof UnknownHostException);
        DnsServerAddressStream redirected = redirectedRef.get();
        assertNotNull(redirected);
        assertEquals(4, redirected.size());
        assertEquals(4, cached.size());
        if (reversed) {
            assertEquals(ns4Address, redirected.next());
            assertEquals(ns3Address, redirected.next());
            assertEquals(ns2Address, redirected.next());
            assertEquals(ns1Address, redirected.next());
        } else {
            assertEquals(ns1Address, redirected.next());
            assertEquals(ns2Address, redirected.next());
            assertEquals(ns3Address, redirected.next());
            assertEquals(ns4Address, redirected.next());
        }
        // We should always have the same order in the cache.
        assertEquals(ns1Address, cached.get(0));
        assertEquals(ns2Address, cached.get(1));
        assertEquals(ns3Address, cached.get(2));
        assertEquals(ns4Address, cached.get(3));
    } finally {
        resolver.close();
        group.shutdownGracefully(0, 0, TimeUnit.SECONDS);
        redirectServer.stop();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) DnsMessage(org.apache.directory.server.dns.messages.DnsMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoop(io.netty.channel.EventLoop) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

DnsMessage (org.apache.directory.server.dns.messages.DnsMessage)6 UnknownHostException (java.net.UnknownHostException)5 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)5 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)4 InetSocketAddress (java.net.InetSocketAddress)4 EventLoop (io.netty.channel.EventLoop)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 DatagramChannel (io.netty.channel.socket.DatagramChannel)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Set (java.util.Set)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2