Search in sources :

Example 6 with ResourceRecord

use of org.apache.directory.server.dns.messages.ResourceRecord 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 7 with ResourceRecord

use of org.apache.directory.server.dns.messages.ResourceRecord 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 8 with ResourceRecord

use of org.apache.directory.server.dns.messages.ResourceRecord 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)

Example 9 with ResourceRecord

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

the class DnsNameResolverTest method testNotIncludeDuplicates.

@Test
public void testNotIncludeDuplicates() throws IOException {
    final String name = "netty.io";
    final String ipv4Addr = "1.2.3.4";
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            Set<ResourceRecord> records = new LinkedHashSet<ResourceRecord>(4);
            String qName = question.getDomainName().toLowerCase();
            if (qName.equals(name)) {
                records.add(new TestDnsServer.TestResourceRecord(qName, RecordType.CNAME, Collections.<String, Object>singletonMap(DnsAttribute.DOMAIN_NAME.toLowerCase(), "cname.netty.io")));
                records.add(new TestDnsServer.TestResourceRecord(qName, RecordType.A, Collections.<String, Object>singletonMap(DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
            } else {
                records.add(new TestDnsServer.TestResourceRecord(qName, RecordType.A, Collections.<String, Object>singletonMap(DnsAttribute.IP_ADDRESS.toLowerCase(), ipv4Addr)));
            }
            return records;
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(true).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        builder.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY);
        resolver = builder.build();
        List<InetAddress> resolvedAddresses = resolver.resolveAll(name).syncUninterruptibly().getNow();
        assertEquals(Collections.singletonList(InetAddress.getByAddress(name, new byte[] { 1, 2, 3, 4 })), resolvedAddresses);
    } 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) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test)

Example 10 with ResourceRecord

use of org.apache.directory.server.dns.messages.ResourceRecord 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)

Aggregations

ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)11 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)10 RecordStore (org.apache.directory.server.dns.store.RecordStore)10 HashSet (java.util.HashSet)9 LinkedHashSet (java.util.LinkedHashSet)9 Set (java.util.Set)9 Test (org.junit.jupiter.api.Test)7 InetAddress (java.net.InetAddress)6 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)3 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1