Search in sources :

Example 11 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project vert.x by eclipse.

the class FakeDNSServer method start.

@Override
public void start() throws IOException {
    UdpTransport transport = new UdpTransport(ipAddress, port);
    setTransports(transport);
    acceptor = transport.getAcceptor();
    acceptor.setHandler(new DnsProtocolHandler(this, new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            RecordStore actual = store;
            if (actual == null) {
                return Collections.emptySet();
            } else {
                return actual.getRecords(question);
            }
        }
    }) {

        @Override
        public void sessionCreated(IoSession session) throws Exception {
            // Use our own codec to support AAAA testing
            session.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
        }

        @Override
        public void messageReceived(IoSession session, Object message) {
            if (message instanceof DnsMessage) {
                synchronized (FakeDNSServer.this) {
                    currentMessage.add((DnsMessage) message);
                }
            }
            super.messageReceived(session, message);
        }
    });
    // Allow the port to be reused even if the socket is in TIME_WAIT state
    ((DatagramSessionConfig) acceptor.getSessionConfig()).setReuseAddress(true);
    // Start the listener
    acceptor.bind();
}
Also used : UdpTransport(org.apache.directory.server.protocol.shared.transport.UdpTransport) QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) DatagramSessionConfig(org.apache.mina.transport.socket.DatagramSessionConfig) DnsMessage(org.apache.directory.server.dns.messages.DnsMessage) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) DnsProtocolHandler(org.apache.directory.server.dns.protocol.DnsProtocolHandler) DnsException(org.apache.directory.server.dns.DnsException) IOException(java.io.IOException) RecordStore(org.apache.directory.server.dns.store.RecordStore) IoSession(org.apache.mina.core.session.IoSession)

Example 12 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testFollowCNAMEEvenIfARecordIsPresent.

@Test
public void testFollowCNAMEEvenIfARecordIsPresent() throws IOException {
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            if (question.getDomainName().equals("cname.netty.io")) {
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put(DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.99");
                return Collections.<ResourceRecord>singleton(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.A, map1));
            } else {
                Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>(2);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put(DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname.netty.io");
                records.add(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.CNAME, map));
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put(DnsAttribute.IP_ADDRESS.toLowerCase(), "10.0.0.2");
                records.add(new TestDnsServer.TestResourceRecord(question.getDomainName(), RecordType.A, map1));
                return records;
            }
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(true).resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        resolver = builder.build();
        List<InetAddress> resolvedAddresses = resolver.resolveAll("somehost.netty.io").syncUninterruptibly().getNow();
        assertEquals(2, resolvedAddresses.size());
        assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 99 })));
        assertTrue(resolvedAddresses.contains(InetAddress.getByAddress(new byte[] { 10, 0, 0, 2 })));
    } finally {
        dnsServer2.stop();
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) RecordStore(org.apache.directory.server.dns.store.RecordStore) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test)

Example 13 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testCNAMEOnlyTriedOnAddressLookups.

@Test
public void testCNAMEOnlyTriedOnAddressLookups() throws Exception {
    final AtomicInteger cnameQueries = new AtomicInteger();
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
            if (questionRecord.getRecordType() == RecordType.CNAME) {
                cnameQueries.incrementAndGet();
            }
            return Collections.emptySet();
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        resolver = newNonCachedResolver(ResolvedAddressTypes.IPV4_PREFERRED).maxQueriesPerResolve(4).searchDomains(Collections.<String>emptyList()).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress())).build();
        // We expect these resolves to fail with UnknownHostException,
        // and then check that no unexpected CNAME queries were performed.
        assertThat(resolver.resolveAll(new DefaultDnsQuestion("lookup-srv.netty.io", SRV)).await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(0, cnameQueries.get());
        assertThat(resolver.resolveAll(new DefaultDnsQuestion("lookup-naptr.netty.io", NAPTR)).await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(0, cnameQueries.get());
        assertThat(resolver.resolveAll(new DefaultDnsQuestion("lookup-cname.netty.io", CNAME)).await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(1, cnameQueries.getAndSet(0));
        assertThat(resolver.resolveAll(new DefaultDnsQuestion("lookup-a.netty.io", A)).await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(1, cnameQueries.getAndSet(0));
        assertThat(resolver.resolveAll(new DefaultDnsQuestion("lookup-aaaa.netty.io", AAAA)).await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(1, cnameQueries.getAndSet(0));
        assertThat(resolver.resolveAll("lookup-address.netty.io").await().cause(), instanceOf(UnknownHostException.class));
        assertEquals(1, cnameQueries.getAndSet(0));
    } finally {
        dnsServer2.stop();
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) UnknownHostException(java.net.UnknownHostException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordStore(org.apache.directory.server.dns.store.RecordStore) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) Test(org.junit.jupiter.api.Test)

Example 14 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testRRNameContainsDifferentSearchDomain.

private static void testRRNameContainsDifferentSearchDomain(final List<String> searchDomains, String unresolved) throws Exception {
    final String ipAddrPrefix = "1.2.3.";
    TestDnsServer searchDomainServer = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord questionRecord) {
            Set<ResourceRecord> records = new HashSet<ResourceRecord>(searchDomains.size());
            final String qName = questionRecord.getDomainName();
            for (String searchDomain : searchDomains) {
                if (qName.endsWith(searchDomain)) {
                    continue;
                }
                final ResourceRecord rr = newARecord(qName + '.' + searchDomain, ipAddrPrefix + ThreadLocalRandom.current().nextInt(1, 10));
                logger.info("Adding A record: " + rr);
                records.add(rr);
            }
            return records;
        }
    });
    searchDomainServer.start();
    final DnsNameResolver resolver = newResolver(false, null, searchDomainServer).searchDomains(searchDomains).build();
    try {
        final List<InetAddress> addresses = resolver.resolveAll(unresolved).sync().get();
        assertThat(addresses, Matchers.<InetAddress>hasSize(greaterThan(0)));
        for (InetAddress address : addresses) {
            assertThat(address.getHostName(), startsWith(unresolved));
            assertThat(address.getHostAddress(), startsWith(ipAddrPrefix));
        }
    } finally {
        resolver.close();
        searchDomainServer.stop();
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) RecordStore(org.apache.directory.server.dns.store.RecordStore) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) InetAddress(java.net.InetAddress)

Example 15 with RecordStore

use of org.apache.directory.server.dns.store.RecordStore in project netty by netty.

the class DnsNameResolverTest method testSrvWithCnameNotCached.

@Test
@Timeout(value = 2000, unit = TimeUnit.MILLISECONDS)
public void testSrvWithCnameNotCached() throws Exception {
    final AtomicBoolean alias = new AtomicBoolean();
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            String name = question.getDomainName();
            if (name.equals("service.netty.io")) {
                Set<ResourceRecord> records = new HashSet<ResourceRecord>(2);
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(name);
                rm.setDnsTtl(10);
                rm.setDnsType(RecordType.CNAME);
                rm.put(DnsAttribute.DOMAIN_NAME, "alias.service.netty.io");
                records.add(rm.getEntry());
                rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(name);
                rm.setDnsTtl(10);
                rm.setDnsType(RecordType.SRV);
                rm.put(DnsAttribute.DOMAIN_NAME, "foo.service.netty.io");
                rm.put(DnsAttribute.SERVICE_PORT, "8080");
                rm.put(DnsAttribute.SERVICE_PRIORITY, "10");
                rm.put(DnsAttribute.SERVICE_WEIGHT, "1");
                records.add(rm.getEntry());
                return records;
            }
            if (name.equals("foo.service.netty.io")) {
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(name);
                rm.setDnsTtl(10);
                rm.setDnsType(RecordType.A);
                rm.put(DnsAttribute.IP_ADDRESS, "10.0.0.1");
                return Collections.singleton(rm.getEntry());
            }
            if (alias.get()) {
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(name);
                rm.setDnsTtl(10);
                rm.setDnsType(RecordType.SRV);
                rm.put(DnsAttribute.DOMAIN_NAME, "foo.service.netty.io");
                rm.put(DnsAttribute.SERVICE_PORT, "8080");
                rm.put(DnsAttribute.SERVICE_PRIORITY, "10");
                rm.put(DnsAttribute.SERVICE_WEIGHT, "1");
                return Collections.singleton(rm.getEntry());
            }
            return null;
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(false).queryTimeoutMillis(10000).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED).completeOncePreferredResolved(true).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        resolver = builder.build();
        assertNotEmptyAndRelease(resolver.resolveAll(new DefaultDnsQuestion("service.netty.io", SRV)));
        alias.set(true);
        assertNotEmptyAndRelease(resolver.resolveAll(new DefaultDnsQuestion("service.netty.io", SRV)));
        alias.set(false);
        assertNotEmptyAndRelease(resolver.resolveAll(new DefaultDnsQuestion("service.netty.io", SRV)));
    } finally {
        dnsServer2.stop();
        if (resolver != null) {
            resolver.close();
        }
    }
}
Also used : QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordStore(org.apache.directory.server.dns.store.RecordStore) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)19 RecordStore (org.apache.directory.server.dns.store.RecordStore)19 HashSet (java.util.HashSet)18 LinkedHashSet (java.util.LinkedHashSet)18 Set (java.util.Set)18 Test (org.junit.jupiter.api.Test)12 InetAddress (java.net.InetAddress)10 ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)10 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)5 ResourceRecordModifier (org.apache.directory.server.dns.messages.ResourceRecordModifier)5 InetSocketAddress (java.net.InetSocketAddress)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 DnsException (org.apache.directory.server.dns.DnsException)3 DnsMessage (org.apache.directory.server.dns.messages.DnsMessage)3 DnsResponse (io.netty.handler.codec.dns.DnsResponse)2 DatagramSocket (java.net.DatagramSocket)2