Search in sources :

Example 1 with ResourceRecordModifier

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

the class TestDnsServer method newNsRecord.

protected static ResourceRecord newNsRecord(String dnsname, String domainName) {
    ResourceRecordModifier rm = new ResourceRecordModifier();
    rm.setDnsClass(RecordClass.IN);
    rm.setDnsName(dnsname);
    rm.setDnsTtl(100);
    rm.setDnsType(RecordType.NS);
    rm.put(DnsAttribute.DOMAIN_NAME, domainName);
    return rm.getEntry();
}
Also used : ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier)

Example 2 with ResourceRecordModifier

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

the class DnsNameResolverTest method testCNAMERecursiveResolveDifferentNameServersForDomains.

@Test
public void testCNAMERecursiveResolveDifferentNameServersForDomains() throws IOException {
    final String firstName = "firstname.com";
    final String secondName = "secondname.com";
    final String lastName = "lastname.com";
    final String ipv4Addr = "1.2.3.4";
    final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            if (question.getDomainName().equals(firstName)) {
                rm.setDnsType(RecordType.CNAME);
                rm.put(DnsAttribute.DOMAIN_NAME, secondName);
            } else if (question.getDomainName().equals(lastName)) {
                rm.setDnsType(question.getRecordType());
                rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
            } else {
                return null;
            }
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer2.start();
    final TestDnsServer dnsServer3 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            if (question.getDomainName().equals(secondName)) {
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(question.getDomainName());
                rm.setDnsTtl(100);
                rm.setDnsType(RecordType.CNAME);
                rm.put(DnsAttribute.DOMAIN_NAME, lastName);
                return Collections.singleton(rm.getEntry());
            }
            return null;
        }
    });
    dnsServer3.start();
    DnsNameResolver resolver = null;
    try {
        resolver = newResolver().resolveCache(NoopDnsCache.INSTANCE).cnameCache(NoopDnsCnameCache.INSTANCE).recursionDesired(true).maxQueriesPerResolve(16).nameServerProvider(new DnsServerAddressStreamProvider() {

            @Override
            public DnsServerAddressStream nameServerAddressStream(String hostname) {
                if (hostname.equals(secondName + '.')) {
                    return DnsServerAddresses.singleton(dnsServer3.localAddress()).stream();
                }
                return DnsServerAddresses.singleton(dnsServer2.localAddress()).stream();
            }
        }).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED).build();
        assertResolvedAddress(resolver.resolve(firstName).syncUninterruptibly().getNow(), ipv4Addr, firstName);
    } finally {
        dnsServer2.stop();
        dnsServer3.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) RecordStore(org.apache.directory.server.dns.store.RecordStore) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceRecordModifier

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

the class DnsNameResolverTest method testCNAMERecursiveResolve.

private static void testCNAMERecursiveResolve(boolean ipv4Preferred) throws IOException {
    final String firstName = "firstname.com";
    final String secondName = "secondname.com";
    final String lastName = "lastname.com";
    final String ipv4Addr = "1.2.3.4";
    final String ipv6Addr = "::1";
    TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) {
            ResourceRecordModifier rm = new ResourceRecordModifier();
            rm.setDnsClass(RecordClass.IN);
            rm.setDnsName(question.getDomainName());
            rm.setDnsTtl(100);
            rm.setDnsType(RecordType.CNAME);
            if (question.getDomainName().equals(firstName)) {
                rm.put(DnsAttribute.DOMAIN_NAME, secondName);
            } else if (question.getDomainName().equals(secondName)) {
                rm.put(DnsAttribute.DOMAIN_NAME, lastName);
            } else if (question.getDomainName().equals(lastName)) {
                rm.setDnsType(question.getRecordType());
                switch(question.getRecordType()) {
                    case A:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
                        break;
                    case AAAA:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv6Addr);
                        break;
                    default:
                        return null;
                }
            } else {
                return null;
            }
            return Collections.singleton(rm.getEntry());
        }
    });
    dnsServer2.start();
    DnsNameResolver resolver = null;
    try {
        DnsNameResolverBuilder builder = newResolver().recursionDesired(true).maxQueriesPerResolve(16).nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress()));
        if (ipv4Preferred) {
            builder.resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED);
        } else {
            builder.resolvedAddressTypes(ResolvedAddressTypes.IPV6_PREFERRED);
        }
        resolver = builder.build();
        InetAddress resolvedAddress = resolver.resolve(firstName).syncUninterruptibly().getNow();
        if (ipv4Preferred) {
            assertEquals(ipv4Addr, resolvedAddress.getHostAddress());
        } else {
            assertEquals(ipv6Addr, NetUtil.toAddressString(resolvedAddress));
        }
        assertEquals(firstName, resolvedAddress.getHostName());
    } 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) RecordStore(org.apache.directory.server.dns.store.RecordStore) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) InetAddress(java.net.InetAddress)

Example 4 with ResourceRecordModifier

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

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

the class DnsNameResolverTest method testCNAMERecursiveResolveMultipleNameServers.

private static void testCNAMERecursiveResolveMultipleNameServers(boolean ipv4Preferred) throws IOException {
    final String firstName = "firstname.nettyfoo.com";
    final String lastName = "lastname.nettybar.com";
    final String ipv4Addr = "1.2.3.4";
    final String ipv6Addr = "::1";
    final AtomicBoolean hitServer2 = new AtomicBoolean();
    final TestDnsServer dnsServer2 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            hitServer2.set(true);
            if (question.getDomainName().equals(firstName)) {
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(question.getDomainName());
                rm.setDnsTtl(100);
                rm.setDnsType(RecordType.CNAME);
                rm.put(DnsAttribute.DOMAIN_NAME, lastName);
                return Collections.singleton(rm.getEntry());
            } else {
                throw new DnsException(ResponseCode.REFUSED);
            }
        }
    });
    final TestDnsServer dnsServer3 = new TestDnsServer(new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            if (question.getDomainName().equals(lastName)) {
                ResourceRecordModifier rm = new ResourceRecordModifier();
                rm.setDnsClass(RecordClass.IN);
                rm.setDnsName(question.getDomainName());
                rm.setDnsTtl(100);
                rm.setDnsType(question.getRecordType());
                switch(question.getRecordType()) {
                    case A:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv4Addr);
                        break;
                    case AAAA:
                        rm.put(DnsAttribute.IP_ADDRESS, ipv6Addr);
                        break;
                    default:
                        return null;
                }
                return Collections.singleton(rm.getEntry());
            } else {
                throw new DnsException(ResponseCode.REFUSED);
            }
        }
    });
    dnsServer2.start();
    dnsServer3.start();
    DnsNameResolver resolver = null;
    try {
        AuthoritativeDnsServerCache nsCache = new DefaultAuthoritativeDnsServerCache();
        // What we want to test is the following:
        // 1. Do a DNS query.
        // 2. CNAME is returned, we want to lookup that CNAME on multiple DNS servers
        // 3. The first DNS server should fail
        // 4. The second DNS server should succeed
        // This verifies that we do in fact follow multiple DNS servers in the CNAME resolution.
        // The DnsCache is used for the name server cache, but doesn't provide a InetSocketAddress (only InetAddress
        // so no port), so we only specify the name server in the cache, and then specify both name servers in the
        // fallback name server provider.
        nsCache.cache("nettyfoo.com.", dnsServer2.localAddress(), 10000, group.next());
        resolver = new DnsNameResolver(group.next(), new ReflectiveChannelFactory<DatagramChannel>(NioDatagramChannel.class), NoopDnsCache.INSTANCE, nsCache, NoopDnsQueryLifecycleObserverFactory.INSTANCE, 3000, ipv4Preferred ? ResolvedAddressTypes.IPV4_ONLY : ResolvedAddressTypes.IPV6_ONLY, true, 10, true, 4096, false, HostsFileEntriesResolver.DEFAULT, new SequentialDnsServerAddressStreamProvider(dnsServer2.localAddress(), dnsServer3.localAddress()), DnsNameResolver.DEFAULT_SEARCH_DOMAINS, 0, true) {

            @Override
            InetSocketAddress newRedirectServerAddress(InetAddress server) {
                int port = hitServer2.get() ? dnsServer3.localAddress().getPort() : dnsServer2.localAddress().getPort();
                return new InetSocketAddress(server, port);
            }
        };
        InetAddress resolvedAddress = resolver.resolve(firstName).syncUninterruptibly().getNow();
        if (ipv4Preferred) {
            assertEquals(ipv4Addr, resolvedAddress.getHostAddress());
        } else {
            assertEquals(ipv6Addr, NetUtil.toAddressString(resolvedAddress));
        }
        assertEquals(firstName, resolvedAddress.getHostName());
    } finally {
        dnsServer2.stop();
        dnsServer3.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) InetSocketAddress(java.net.InetSocketAddress) ResourceRecordModifier(org.apache.directory.server.dns.messages.ResourceRecordModifier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DnsException(org.apache.directory.server.dns.DnsException) RecordStore(org.apache.directory.server.dns.store.RecordStore) ReflectiveChannelFactory(io.netty.channel.ReflectiveChannelFactory) InetAddress(java.net.InetAddress)

Aggregations

ResourceRecordModifier (org.apache.directory.server.dns.messages.ResourceRecordModifier)7 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 Set (java.util.Set)5 QuestionRecord (org.apache.directory.server.dns.messages.QuestionRecord)5 RecordStore (org.apache.directory.server.dns.store.RecordStore)5 Test (org.junit.jupiter.api.Test)3 InetAddress (java.net.InetAddress)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ReflectiveChannelFactory (io.netty.channel.ReflectiveChannelFactory)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DnsException (org.apache.directory.server.dns.DnsException)1 ResourceRecord (org.apache.directory.server.dns.messages.ResourceRecord)1 Timeout (org.junit.jupiter.api.Timeout)1