Skip to content

Commit

Permalink
Allow to config dns bind address in DnsNameResolver (#11061)
Browse files Browse the repository at this point in the history
Motivation:

The DnsResolver default start address listen to "0.0.0.0", which may be not what the user wants. 

Modification:

Add localAddress as a param of DnsNameResolver and its builder

Result:

The DnsNameResolver's bind address can be configured.
  • Loading branch information
shoothzj committed Mar 8, 2021
1 parent 7d4aaa2 commit 0f12472
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,35 @@ public DnsNameResolver(
int ndots,
boolean decodeIdn,
boolean completeOncePreferredResolved) {
this(eventLoop, channelFactory, socketChannelFactory, resolveCache, cnameCache, authoritativeDnsServerCache,
null, dnsQueryLifecycleObserverFactory, queryTimeoutMillis, resolvedAddressTypes,
recursionDesired, maxQueriesPerResolve, traceEnabled, maxPayloadSize, optResourceEnabled,
hostsFileEntriesResolver, dnsServerAddressStreamProvider, searchDomains, ndots, decodeIdn,
completeOncePreferredResolved);
}

DnsNameResolver(
EventLoop eventLoop,
ChannelFactory<? extends DatagramChannel> channelFactory,
ChannelFactory<? extends SocketChannel> socketChannelFactory,
final DnsCache resolveCache,
final DnsCnameCache cnameCache,
final AuthoritativeDnsServerCache authoritativeDnsServerCache,
SocketAddress localAddress,
DnsQueryLifecycleObserverFactory dnsQueryLifecycleObserverFactory,
long queryTimeoutMillis,
ResolvedAddressTypes resolvedAddressTypes,
boolean recursionDesired,
int maxQueriesPerResolve,
boolean traceEnabled,
int maxPayloadSize,
boolean optResourceEnabled,
HostsFileEntriesResolver hostsFileEntriesResolver,
DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
String[] searchDomains,
int ndots,
boolean decodeIdn,
boolean completeOncePreferredResolved) {
super(eventLoop);
this.queryTimeoutMillis = queryTimeoutMillis > 0
? queryTimeoutMillis
Expand Down Expand Up @@ -453,7 +482,12 @@ protected void initChannel(DatagramChannel ch) {
});

channelFuture = responseHandler.channelActivePromise;
ChannelFuture future = b.register();
final ChannelFuture future;
if (localAddress == null) {
future = b.register();
} else {
future = b.bind(localAddress);
}
Throwable cause = future.cause();
if (cause != null) {
if (cause instanceof RuntimeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.netty.resolver.ResolvedAddressTypes;
import io.netty.util.concurrent.Future;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -42,6 +43,7 @@ public final class DnsNameResolverBuilder {
private DnsCache resolveCache;
private DnsCnameCache cnameCache;
private AuthoritativeDnsServerCache authoritativeDnsServerCache;
private SocketAddress localAddress;
private Integer minTtl;
private Integer maxTtl;
private Integer negativeTtl;
Expand Down Expand Up @@ -201,6 +203,16 @@ public DnsNameResolverBuilder authoritativeDnsServerCache(AuthoritativeDnsServer
return this;
}

/**
* Configure the address that will be used to bind too. If `null` the default will be used.
* @param localAddress the bind address
* @return {@code this}
*/
public DnsNameResolverBuilder localAddress(SocketAddress localAddress) {
this.localAddress = localAddress;
return this;
}

/**
* Sets the minimum and maximum TTL of the cached DNS resource records (in seconds). If the TTL of the DNS
* resource record returned by the DNS server is less than the minimum TTL or greater than the maximum TTL,
Expand Down Expand Up @@ -480,6 +492,7 @@ public DnsNameResolver build() {
resolveCache,
cnameCache,
authoritativeDnsServerCache,
localAddress,
dnsQueryLifecycleObserverFactory,
queryTimeoutMillis,
resolvedAddressTypes,
Expand Down

0 comments on commit 0f12472

Please sign in to comment.