From b42656661778474bc89dfce8d354deb6b19ca6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Karas?= Date: Sat, 13 Feb 2016 14:06:06 +0100 Subject: [PATCH] DnsNameResolver: makes possible to define additional records in DNS query Motivation: Current DnsNameResolver api don't allow to define additional records in DNS query. It can be useful in many cases. For example when we want to query dns server with real client address (EDNS-CLIENT-SUBNET extension: http://tools.ietf.org/html/draft-vandergaast-edns-client-subnet-02 ) Modifications: This change add new query methods with list of additional DnsRecord-s for query. Result: It is possible to create dns query with EDNS-CLIENT-SUBNET extension for example. --- .../netty/resolver/dns/DnsNameResolver.java | 47 +++++++++++++++---- .../netty/resolver/dns/DnsQueryContext.java | 27 ++++++++--- 2 files changed, 58 insertions(+), 16 deletions(-) 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 c536e4c640..52853ba5af 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 @@ -29,6 +29,7 @@ import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.InternetProtocolFamily; import io.netty.handler.codec.dns.DatagramDnsQueryEncoder; import io.netty.handler.codec.dns.DatagramDnsResponse; +import io.netty.handler.codec.dns.DnsRecord; import io.netty.handler.codec.dns.DatagramDnsResponseDecoder; import io.netty.handler.codec.dns.DnsQuestion; import io.netty.handler.codec.dns.DnsResponse; @@ -505,12 +506,20 @@ public class DnsNameResolver extends InetNameResolver { return query(nextNameServerAddress(), question); } + /** + * Sends a DNS query with the specified question with additional records. + */ + public Future> query( + DnsQuestion question, Iterable additional) { + return query(nextNameServerAddress(), question, additional); + } + /** * Sends a DNS query with the specified question. */ public Future> query( DnsQuestion question, Promise> promise) { - return query(nextNameServerAddress(), question, promise); + return query(nextNameServerAddress(), question, Collections.emptyList(), promise); } private InetSocketAddress nextNameServerAddress() { @@ -523,9 +532,18 @@ public class DnsNameResolver extends InetNameResolver { public Future> query( InetSocketAddress nameServerAddr, DnsQuestion question) { - return query0(checkNotNull(nameServerAddr, "nameServerAddr"), - checkNotNull(question, "question"), - ch.eventLoop().>newPromise()); + return query0(nameServerAddr, question, Collections.emptyList(), + ch.eventLoop().>newPromise()); + } + + /** + * Sends a DNS query with the specified question with additional records using the specified name server list. + */ + public Future> query( + InetSocketAddress nameServerAddr, DnsQuestion question, Iterable additional) { + + return query0(nameServerAddr, question, additional, + ch.eventLoop().>newPromise()); } /** @@ -535,18 +553,29 @@ public class DnsNameResolver extends InetNameResolver { InetSocketAddress nameServerAddr, DnsQuestion question, Promise> promise) { - return query0(checkNotNull(nameServerAddr, "nameServerAddr"), - checkNotNull(question, "question"), - checkNotNull(promise, "promise")); + return query0(nameServerAddr, question, Collections.emptyList(), promise); + } + + /** + * Sends a DNS query with the specified question with additional records using the specified name server list. + */ + public Future> query( + InetSocketAddress nameServerAddr, DnsQuestion question, + Iterable additional, + Promise> promise) { + + return query0(nameServerAddr, question, additional, promise); } private Future> query0( InetSocketAddress nameServerAddr, DnsQuestion question, + Iterable additional, Promise> promise) { - final Promise> castPromise = cast(promise); + final Promise> castPromise = cast( + checkNotNull(promise, "promise")); try { - new DnsQueryContext(this, nameServerAddr, question, castPromise).query(); + new DnsQueryContext(this, nameServerAddr, question, additional, castPromise).query(); return castPromise; } catch (Exception e) { return castPromise.setFailure(e); diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java index 8217c82ea6..d95d9a7a62 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryContext.java @@ -35,8 +35,11 @@ import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import java.net.InetSocketAddress; +import java.util.List; import java.util.concurrent.TimeUnit; +import static io.netty.util.internal.ObjectUtil.checkNotNull; + final class DnsQueryContext { private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsQueryContext.class); @@ -45,6 +48,7 @@ final class DnsQueryContext { private final Promise> promise; private final int id; private final DnsQuestion question; + private final Iterable additional; private final DnsRecord optResource; private final InetSocketAddress nameServerAddr; @@ -53,12 +57,15 @@ final class DnsQueryContext { DnsQueryContext(DnsNameResolver parent, InetSocketAddress nameServerAddr, - DnsQuestion question, Promise> promise) { + DnsQuestion question, + Iterable additional, + Promise> promise) { - this.parent = parent; - this.nameServerAddr = nameServerAddr; - this.question = question; - this.promise = promise; + this.parent = checkNotNull(parent, "parent"); + this.nameServerAddr = checkNotNull(nameServerAddr, "nameServerAddr"); + this.question = checkNotNull(question, "question"); + this.additional = checkNotNull(additional, "additional"); + this.promise = checkNotNull(promise, "promise"); recursionDesired = parent.isRecursionDesired(); id = parent.queryContextManager.add(this); @@ -82,10 +89,16 @@ final class DnsQueryContext { final DnsQuestion question = question(); final InetSocketAddress nameServerAddr = nameServerAddr(); final DatagramDnsQuery query = new DatagramDnsQuery(null, nameServerAddr, id); + query.setRecursionDesired(recursionDesired); - query.setRecord(DnsSection.QUESTION, question); + + query.addRecord(DnsSection.QUESTION, question); + + for (DnsRecord record:additional) { + query.addRecord(DnsSection.ADDITIONAL, record); + } if (optResource != null) { - query.setRecord(DnsSection.ADDITIONAL, optResource); + query.addRecord(DnsSection.ADDITIONAL, optResource); } if (logger.isDebugEnabled()) {