diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index da4cf59f7f..dfff7ce9b3 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -858,13 +858,19 @@ public class DnsNameResolver extends InetNameResolver { static void trySuccess(Promise promise, T result) { if (!promise.trySuccess(result)) { - logger.warn("Failed to notify success ({}) to a promise: {}", result, promise); + // There is nothing really wrong with not be able to notify the promise as we may have raced here because + // of multiple queries that have been executed. Log it with trace level anyway just in case the user + // wants to better understand what happened. + logger.trace("Failed to notify success ({}) to a promise: {}", result, promise); } } private static void tryFailure(Promise promise, Throwable cause) { if (!promise.tryFailure(cause)) { - logger.warn("Failed to notify failure to a promise: {}", promise, cause); + // There is nothing really wrong with not be able to notify the promise as we may have raced here because + // of multiple queries that have been executed. Log it with trace level anyway just in case the user + // wants to better understand what happened. + logger.trace("Failed to notify failure to a promise: {}", promise, cause); } } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java index 8707eb4053..eebcb268c1 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java @@ -2550,4 +2550,31 @@ public class DnsNameResolverTest { } } } + + @Test + public void testDropAAAA() throws IOException { + String host = "somehost.netty.io"; + TestDnsServer dnsServer2 = new TestDnsServer(Collections.singleton(host)); + dnsServer2.start(true); + DnsNameResolver resolver = null; + try { + DnsNameResolverBuilder builder = newResolver() + .recursionDesired(false) + .queryTimeoutMillis(500) + .resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED) + .maxQueriesPerResolve(16) + .nameServerProvider(new SingletonDnsServerAddressStreamProvider(dnsServer2.localAddress())); + + resolver = builder.build(); + List addressList = resolver.resolveAll(host).syncUninterruptibly().getNow(); + assertEquals(1, addressList.size()); + assertEquals(host, addressList.get(0).getHostName()); + } finally { + dnsServer2.stop(); + if (resolver != null) { + resolver.close(); + } + } + } + } diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java b/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java index 34943f37a4..23f01a6998 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/TestDnsServer.java @@ -17,6 +17,7 @@ package io.netty.resolver.dns; import io.netty.util.NetUtil; import org.apache.directory.server.dns.DnsServer; +import org.apache.directory.server.dns.io.decoder.DnsMessageDecoder; import org.apache.directory.server.dns.io.encoder.DnsMessageEncoder; import org.apache.directory.server.dns.io.encoder.ResourceRecordEncoder; import org.apache.directory.server.dns.messages.DnsMessage; @@ -27,7 +28,6 @@ import org.apache.directory.server.dns.messages.ResourceRecord; import org.apache.directory.server.dns.messages.ResourceRecordImpl; import org.apache.directory.server.dns.messages.ResourceRecordModifier; import org.apache.directory.server.dns.protocol.DnsProtocolHandler; -import org.apache.directory.server.dns.protocol.DnsUdpDecoder; import org.apache.directory.server.dns.protocol.DnsUdpEncoder; import org.apache.directory.server.dns.store.DnsAttribute; import org.apache.directory.server.dns.store.RecordStore; @@ -37,6 +37,8 @@ import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFactory; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.ProtocolDecoder; +import org.apache.mina.filter.codec.ProtocolDecoderAdapter; +import org.apache.mina.filter.codec.ProtocolDecoderOutput; import org.apache.mina.filter.codec.ProtocolEncoder; import org.apache.mina.filter.codec.ProtocolEncoderOutput; import org.apache.mina.transport.socket.DatagramAcceptor; @@ -83,6 +85,13 @@ class TestDnsServer extends DnsServer { @Override public void start() throws IOException { + start(false); + } + + /** + * Start the {@link TestDnsServer} but drop all {@code AAAA} queries and not send any response to these at all. + */ + public void start(final boolean dropAAAAQueries) throws IOException { InetSocketAddress address = new InetSocketAddress(NetUtil.LOCALHOST4, 0); UdpTransport transport = new UdpTransport(address.getHostName(), address.getPort()); setTransports(transport); @@ -94,7 +103,8 @@ class TestDnsServer extends DnsServer { public void sessionCreated(IoSession session) { // USe our own codec to support AAAA testing session.getFilterChain() - .addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory())); + .addFirst("codec", new ProtocolCodecFilter( + new TestDnsProtocolUdpCodecFactory(dropAAAAQueries))); } }); @@ -142,6 +152,11 @@ class TestDnsServer extends DnsServer { private final class TestDnsProtocolUdpCodecFactory implements ProtocolCodecFactory { private final DnsMessageEncoder encoder = new DnsMessageEncoder(); private final TestAAAARecordEncoder recordEncoder = new TestAAAARecordEncoder(); + private final boolean dropAAAArecords; + + TestDnsProtocolUdpCodecFactory(boolean dropAAAArecords) { + this.dropAAAArecords = dropAAAArecords; + } @Override public ProtocolEncoder getEncoder(IoSession session) { @@ -175,7 +190,22 @@ class TestDnsServer extends DnsServer { @Override public ProtocolDecoder getDecoder(IoSession session) { - return new DnsUdpDecoder(); + return new ProtocolDecoderAdapter() { + private DnsMessageDecoder decoder = new DnsMessageDecoder(); + + @Override + public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws IOException { + DnsMessage message = decoder.decode(in); + if (dropAAAArecords) { + for (QuestionRecord record: message.getQuestionRecords()) { + if (record.getRecordType() == RecordType.AAAA) { + return; + } + } + } + out.write(message); + } + }; } private final class TestAAAARecordEncoder extends ResourceRecordEncoder {