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();
}
}
}
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();
}
}
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();
}
}
}
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();
}
}
}
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();
}
Aggregations