2014-09-19 15:36:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
package io.netty.resolver.dns;
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.channel.AddressedEnvelope;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.channel.ChannelFactory;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.EventLoop;
|
|
|
|
import io.netty.channel.FixedRecvByteBufAllocator;
|
|
|
|
import io.netty.channel.socket.DatagramChannel;
|
|
|
|
import io.netty.channel.socket.InternetProtocolFamily;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DatagramDnsQueryEncoder;
|
|
|
|
import io.netty.handler.codec.dns.DatagramDnsResponse;
|
2016-02-13 14:06:06 +01:00
|
|
|
import io.netty.handler.codec.dns.DnsRecord;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DatagramDnsResponseDecoder;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsQuestion;
|
|
|
|
import io.netty.handler.codec.dns.DnsResponse;
|
2015-12-14 15:27:49 +01:00
|
|
|
import io.netty.resolver.HostsFileEntriesResolver;
|
2015-12-13 00:11:59 +01:00
|
|
|
import io.netty.resolver.InetNameResolver;
|
2015-07-10 22:00:30 +02:00
|
|
|
import io.netty.util.NetUtil;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.ReferenceCountUtil;
|
2015-07-12 12:34:05 +02:00
|
|
|
import io.netty.util.concurrent.FastThreadLocal;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.Promise;
|
2016-06-13 12:44:35 +02:00
|
|
|
import io.netty.util.internal.PlatformDependent;
|
2016-04-12 14:22:41 +02:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
|
|
|
|
import java.net.IDN;
|
2015-07-10 22:00:30 +02:00
|
|
|
import java.net.InetAddress;
|
2014-09-19 15:36:32 +02:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2015-07-12 12:34:05 +02:00
|
|
|
import java.util.Collections;
|
2014-09-19 15:36:32 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
import static io.netty.util.internal.ObjectUtil.*;
|
2015-07-12 12:34:05 +02:00
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
2015-12-13 00:11:59 +01:00
|
|
|
* A DNS-based {@link InetNameResolver}.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2016-04-12 14:22:41 +02:00
|
|
|
@UnstableApi
|
2015-12-13 00:11:59 +01:00
|
|
|
public class DnsNameResolver extends InetNameResolver {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DnsNameResolver.class);
|
2016-06-13 12:44:35 +02:00
|
|
|
private static final String LOCALHOST = "localhost";
|
|
|
|
private static final InetAddress LOCALHOST_ADDRESS;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
static final InetSocketAddress ANY_LOCAL_ADDR = new InetSocketAddress(0);
|
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
static final InternetProtocolFamily[] DEFAULT_RESOLVE_ADDRESS_TYPES = new InternetProtocolFamily[2];
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
// Note that we did not use SystemPropertyUtil.getBoolean() here to emulate the behavior of JDK.
|
2015-12-13 00:11:59 +01:00
|
|
|
if (Boolean.getBoolean("java.net.preferIPv6Addresses")) {
|
2014-09-19 15:36:32 +02:00
|
|
|
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv6;
|
|
|
|
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv4;
|
2016-06-13 12:44:35 +02:00
|
|
|
LOCALHOST_ADDRESS = NetUtil.LOCALHOST6;
|
2014-09-19 15:36:32 +02:00
|
|
|
logger.debug("-Djava.net.preferIPv6Addresses: true");
|
|
|
|
} else {
|
|
|
|
DEFAULT_RESOLVE_ADDRESS_TYPES[0] = InternetProtocolFamily.IPv4;
|
|
|
|
DEFAULT_RESOLVE_ADDRESS_TYPES[1] = InternetProtocolFamily.IPv6;
|
2016-06-13 12:44:35 +02:00
|
|
|
LOCALHOST_ADDRESS = NetUtil.LOCALHOST4;
|
2014-09-19 15:36:32 +02:00
|
|
|
logger.debug("-Djava.net.preferIPv6Addresses: false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private static final DatagramDnsResponseDecoder DECODER = new DatagramDnsResponseDecoder();
|
|
|
|
private static final DatagramDnsQueryEncoder ENCODER = new DatagramDnsQueryEncoder();
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
final DnsServerAddresses nameServerAddresses;
|
2014-11-18 21:28:45 +01:00
|
|
|
final ChannelFuture bindFuture;
|
2014-09-19 15:36:32 +02:00
|
|
|
final DatagramChannel ch;
|
|
|
|
|
|
|
|
/**
|
2015-11-08 04:59:01 +01:00
|
|
|
* Manages the {@link DnsQueryContext}s in progress and their query IDs.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2015-11-08 04:59:01 +01:00
|
|
|
final DnsQueryContextManager queryContextManager = new DnsQueryContextManager();
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
/**
|
2015-12-13 00:11:59 +01:00
|
|
|
* Cache for {@link #doResolve(String, Promise)} and {@link #doResolveAll(String, Promise)}.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2016-01-07 17:49:15 +01:00
|
|
|
private final DnsCache resolveCache;
|
2015-07-12 12:34:05 +02:00
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
private final FastThreadLocal<DnsServerAddressStream> nameServerAddrStream =
|
|
|
|
new FastThreadLocal<DnsServerAddressStream>() {
|
2015-07-12 12:34:05 +02:00
|
|
|
@Override
|
2015-08-19 04:51:15 +02:00
|
|
|
protected DnsServerAddressStream initialValue() throws Exception {
|
|
|
|
return nameServerAddresses.stream();
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
};
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
private final long queryTimeoutMillis;
|
|
|
|
private final int maxQueriesPerResolve;
|
|
|
|
private final boolean traceEnabled;
|
|
|
|
private final InternetProtocolFamily[] resolvedAddressTypes;
|
|
|
|
private final boolean recursionDesired;
|
|
|
|
private final int maxPayloadSize;
|
|
|
|
private final boolean optResourceEnabled;
|
|
|
|
private final HostsFileEntriesResolver hostsFileEntriesResolver;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new DNS-based name resolver that communicates with the specified list of DNS servers.
|
|
|
|
*
|
|
|
|
* @param eventLoop the {@link EventLoop} which will perform the communication with the DNS servers
|
|
|
|
* @param channelFactory the {@link ChannelFactory} that will create a {@link DatagramChannel}
|
|
|
|
* @param localAddress the local address of the {@link DatagramChannel}
|
2015-08-19 04:51:15 +02:00
|
|
|
* @param nameServerAddresses the addresses of the DNS server. For each DNS query, a new stream is created from
|
|
|
|
* this to determine which DNS server should be contacted for the next retry in case
|
|
|
|
* of failure.
|
2016-01-07 17:49:15 +01:00
|
|
|
* @param resolveCache the DNS resolved entries cache
|
2015-12-14 15:27:49 +01:00
|
|
|
* @param queryTimeoutMillis timeout of each DNS query in millis
|
|
|
|
* @param resolvedAddressTypes list of the protocol families
|
|
|
|
* @param recursionDesired if recursion desired flag must be set
|
|
|
|
* @param maxQueriesPerResolve the maximum allowed number of DNS queries for a given name resolution
|
|
|
|
* @param traceEnabled if trace is enabled
|
|
|
|
* @param maxPayloadSize the capacity of the datagram packet buffer
|
|
|
|
* @param optResourceEnabled if automatic inclusion of a optional records is enabled
|
|
|
|
* @param hostsFileEntriesResolver the {@link HostsFileEntriesResolver} used to check for local aliases
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
|
|
|
public DnsNameResolver(
|
2015-12-14 15:27:49 +01:00
|
|
|
EventLoop eventLoop,
|
|
|
|
ChannelFactory<? extends DatagramChannel> channelFactory,
|
|
|
|
InetSocketAddress localAddress,
|
|
|
|
DnsServerAddresses nameServerAddresses,
|
2016-01-07 17:49:15 +01:00
|
|
|
DnsCache resolveCache,
|
2015-12-14 15:27:49 +01:00
|
|
|
long queryTimeoutMillis,
|
|
|
|
InternetProtocolFamily[] resolvedAddressTypes,
|
|
|
|
boolean recursionDesired,
|
|
|
|
int maxQueriesPerResolve,
|
|
|
|
boolean traceEnabled,
|
|
|
|
int maxPayloadSize,
|
|
|
|
boolean optResourceEnabled,
|
|
|
|
HostsFileEntriesResolver hostsFileEntriesResolver) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
super(eventLoop);
|
2015-07-12 12:34:05 +02:00
|
|
|
checkNotNull(channelFactory, "channelFactory");
|
|
|
|
checkNotNull(localAddress, "localAddress");
|
2015-12-14 15:27:49 +01:00
|
|
|
this.nameServerAddresses = checkNotNull(nameServerAddresses, "nameServerAddresses");
|
|
|
|
this.queryTimeoutMillis = checkPositive(queryTimeoutMillis, "queryTimeoutMillis");
|
|
|
|
this.resolvedAddressTypes = checkNonEmpty(resolvedAddressTypes, "resolvedAddressTypes");
|
|
|
|
this.recursionDesired = recursionDesired;
|
|
|
|
this.maxQueriesPerResolve = checkPositive(maxQueriesPerResolve, "maxQueriesPerResolve");
|
|
|
|
this.traceEnabled = traceEnabled;
|
|
|
|
this.maxPayloadSize = checkPositive(maxPayloadSize, "maxPayloadSize");
|
|
|
|
this.optResourceEnabled = optResourceEnabled;
|
|
|
|
this.hostsFileEntriesResolver = checkNotNull(hostsFileEntriesResolver, "hostsFileEntriesResolver");
|
2016-01-07 17:49:15 +01:00
|
|
|
this.resolveCache = resolveCache;
|
2015-07-12 12:34:05 +02:00
|
|
|
|
2014-11-18 21:28:45 +01:00
|
|
|
bindFuture = newChannel(channelFactory, localAddress);
|
|
|
|
ch = (DatagramChannel) bindFuture.channel();
|
2015-12-14 15:27:49 +01:00
|
|
|
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(maxPayloadSize));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 21:28:45 +01:00
|
|
|
private ChannelFuture newChannel(
|
2014-09-19 15:36:32 +02:00
|
|
|
ChannelFactory<? extends DatagramChannel> channelFactory, InetSocketAddress localAddress) {
|
|
|
|
|
|
|
|
Bootstrap b = new Bootstrap();
|
|
|
|
b.group(executor());
|
|
|
|
b.channelFactory(channelFactory);
|
2015-12-14 15:27:49 +01:00
|
|
|
final DnsResponseHandler responseHandler = new DnsResponseHandler();
|
2014-09-19 15:36:32 +02:00
|
|
|
b.handler(new ChannelInitializer<DatagramChannel>() {
|
|
|
|
@Override
|
|
|
|
protected void initChannel(DatagramChannel ch) throws Exception {
|
|
|
|
ch.pipeline().addLast(DECODER, ENCODER, responseHandler);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-18 21:28:45 +01:00
|
|
|
ChannelFuture bindFuture = b.bind(localAddress);
|
|
|
|
bindFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
|
|
|
public void operationComplete(ChannelFuture future) throws Exception {
|
2016-01-07 17:49:15 +01:00
|
|
|
resolveCache.clear();
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-18 21:28:45 +01:00
|
|
|
return bindFuture;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-07 17:49:15 +01:00
|
|
|
* Returns the resolution cache.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
2016-01-07 17:49:15 +01:00
|
|
|
public DnsCache resolveCache() {
|
|
|
|
return resolveCache;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the timeout of each DNS query performed by this resolver (in milliseconds).
|
|
|
|
* The default value is 5 seconds.
|
|
|
|
*/
|
|
|
|
public long queryTimeoutMillis() {
|
|
|
|
return queryTimeoutMillis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-13 00:11:59 +01:00
|
|
|
* Returns the list of the protocol families of the address resolved by {@link #resolve(String)}
|
2014-09-19 15:36:32 +02:00
|
|
|
* in the order of preference.
|
|
|
|
* The default value depends on the value of the system property {@code "java.net.preferIPv6Addresses"}.
|
|
|
|
*/
|
2015-12-14 15:27:49 +01:00
|
|
|
public List<InternetProtocolFamily> resolvedAddressTypes() {
|
|
|
|
return Arrays.asList(resolvedAddressTypes);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InternetProtocolFamily[] resolveAddressTypesUnsafe() {
|
2015-12-14 15:27:49 +01:00
|
|
|
return resolvedAddressTypes;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if and only if this resolver sends a DNS query with the RD (recursion desired) flag set.
|
|
|
|
* The default value is {@code true}.
|
|
|
|
*/
|
|
|
|
public boolean isRecursionDesired() {
|
|
|
|
return recursionDesired;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum allowed number of DNS queries to send when resolving a host name.
|
|
|
|
* The default value is {@code 8}.
|
|
|
|
*/
|
|
|
|
public int maxQueriesPerResolve() {
|
|
|
|
return maxQueriesPerResolve;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
/**
|
|
|
|
* Returns if this resolver should generate the detailed trace information in an exception message so that
|
|
|
|
* it is easier to understand the cause of resolution failure. The default value if {@code true}.
|
|
|
|
*/
|
|
|
|
public boolean isTraceEnabled() {
|
|
|
|
return traceEnabled;
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
|
|
|
* Returns the capacity of the datagram packet buffer (in bytes). The default value is {@code 4096} bytes.
|
|
|
|
*/
|
|
|
|
public int maxPayloadSize() {
|
|
|
|
return maxPayloadSize;
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:16:16 +02:00
|
|
|
/**
|
|
|
|
* Returns the automatic inclusion of a optional records that tries to give the remote DNS server a hint about how
|
|
|
|
* much data the resolver can read per response is enabled.
|
|
|
|
*/
|
|
|
|
public boolean isOptResourceEnabled() {
|
|
|
|
return optResourceEnabled;
|
|
|
|
}
|
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
/**
|
|
|
|
* Returns the component that tries to resolve hostnames against the hosts file prior to asking to
|
|
|
|
* remotes DNS servers.
|
|
|
|
*/
|
|
|
|
public HostsFileEntriesResolver hostsFileEntriesResolver() {
|
|
|
|
return hostsFileEntriesResolver;
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
|
|
|
* Closes the internal datagram channel used for sending and receiving DNS messages, and clears all DNS resource
|
|
|
|
* records from the cache. Attempting to send a DNS query or to resolve a domain name will fail once this method
|
|
|
|
* has been called.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
ch.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected EventLoop executor() {
|
|
|
|
return (EventLoop) super.executor();
|
|
|
|
}
|
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
private InetAddress resolveHostsFileEntry(String hostname) {
|
2016-06-13 12:44:35 +02:00
|
|
|
if (hostsFileEntriesResolver == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
InetAddress address = hostsFileEntriesResolver.address(hostname);
|
|
|
|
if (address == null && PlatformDependent.isWindows() && LOCALHOST.equalsIgnoreCase(hostname)) {
|
|
|
|
// If we tried to resolve localhost we need workaround that windows removed localhost from its
|
|
|
|
// hostfile in later versions.
|
|
|
|
// See https://github.com/netty/netty/issues/5386
|
|
|
|
return LOCALHOST_ADDRESS;
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
2015-12-14 15:27:49 +01:00
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
2015-12-13 00:11:59 +01:00
|
|
|
protected void doResolve(String inetHost, Promise<InetAddress> promise) throws Exception {
|
2016-01-07 17:49:15 +01:00
|
|
|
doResolve(inetHost, promise, resolveCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook designed for extensibility so one can pass a different cache on each resolution attempt
|
|
|
|
* instead of using the global one.
|
|
|
|
*/
|
|
|
|
protected void doResolve(String inetHost,
|
|
|
|
Promise<InetAddress> promise,
|
|
|
|
DnsCache resolveCache) throws Exception {
|
2015-12-13 00:11:59 +01:00
|
|
|
final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(inetHost);
|
2015-07-12 12:34:05 +02:00
|
|
|
if (bytes != null) {
|
2015-12-13 00:11:59 +01:00
|
|
|
// The inetHost is actually an ipaddress.
|
|
|
|
promise.setSuccess(InetAddress.getByAddress(bytes));
|
2015-07-12 12:34:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
final String hostname = hostname(inetHost);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
InetAddress hostsFileEntry = resolveHostsFileEntry(hostname);
|
|
|
|
if (hostsFileEntry != null) {
|
|
|
|
promise.setSuccess(hostsFileEntry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
if (!doResolveCached(hostname, promise, resolveCache)) {
|
|
|
|
doResolveUncached(hostname, promise, resolveCache);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
private boolean doResolveCached(String hostname,
|
|
|
|
Promise<InetAddress> promise,
|
|
|
|
DnsCache resolveCache) {
|
2015-07-12 12:34:05 +02:00
|
|
|
final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
|
|
|
|
if (cachedEntries == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
InetAddress address = null;
|
|
|
|
Throwable cause = null;
|
|
|
|
synchronized (cachedEntries) {
|
|
|
|
final int numEntries = cachedEntries.size();
|
|
|
|
assert numEntries > 0;
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
if (cachedEntries.get(0).cause() != null) {
|
|
|
|
cause = cachedEntries.get(0).cause();
|
|
|
|
} else {
|
|
|
|
// Find the first entry with the preferred address type.
|
2015-12-14 15:27:49 +01:00
|
|
|
for (InternetProtocolFamily f : resolvedAddressTypes) {
|
2015-07-12 12:34:05 +02:00
|
|
|
for (int i = 0; i < numEntries; i++) {
|
|
|
|
final DnsCacheEntry e = cachedEntries.get(i);
|
|
|
|
if (f.addressType().isInstance(e.address())) {
|
|
|
|
address = e.address();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address != null) {
|
2015-12-13 00:11:59 +01:00
|
|
|
setSuccess(promise, address);
|
2015-07-12 12:34:05 +02:00
|
|
|
} else if (cause != null) {
|
|
|
|
if (!promise.tryFailure(cause)) {
|
|
|
|
logger.warn("Failed to notify failure to a promise: {}", promise, cause);
|
|
|
|
}
|
2015-07-10 22:00:30 +02:00
|
|
|
} else {
|
2015-07-12 12:34:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
private static void setSuccess(Promise<InetAddress> promise, InetAddress result) {
|
2015-07-12 12:34:05 +02:00
|
|
|
if (!promise.trySuccess(result)) {
|
|
|
|
logger.warn("Failed to notify success ({}) to a promise: {}", result, promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
private void doResolveUncached(String hostname,
|
|
|
|
Promise<InetAddress> promise,
|
|
|
|
DnsCache resolveCache) {
|
2015-12-13 00:11:59 +01:00
|
|
|
final DnsNameResolverContext<InetAddress> ctx =
|
2016-01-07 17:49:15 +01:00
|
|
|
new DnsNameResolverContext<InetAddress>(this, hostname, promise, resolveCache) {
|
2015-07-12 12:34:05 +02:00
|
|
|
@Override
|
|
|
|
protected boolean finishResolve(
|
|
|
|
Class<? extends InetAddress> addressType, List<DnsCacheEntry> resolvedEntries) {
|
|
|
|
|
|
|
|
final int numEntries = resolvedEntries.size();
|
|
|
|
for (int i = 0; i < numEntries; i++) {
|
|
|
|
final InetAddress a = resolvedEntries.get(i).address();
|
|
|
|
if (addressType.isInstance(a)) {
|
2015-12-13 00:11:59 +01:00
|
|
|
setSuccess(promise(), a);
|
2015-07-12 12:34:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-13 00:11:59 +01:00
|
|
|
protected void doResolveAll(String inetHost, Promise<List<InetAddress>> promise) throws Exception {
|
2016-01-07 17:49:15 +01:00
|
|
|
doResolveAll(inetHost, promise, resolveCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook designed for extensibility so one can pass a different cache on each resolution attempt
|
|
|
|
* instead of using the global one.
|
|
|
|
*/
|
|
|
|
protected void doResolveAll(String inetHost,
|
|
|
|
Promise<List<InetAddress>> promise,
|
|
|
|
DnsCache resolveCache) throws Exception {
|
2015-07-12 12:34:05 +02:00
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
final byte[] bytes = NetUtil.createByteArrayFromIpAddressString(inetHost);
|
2015-07-12 12:34:05 +02:00
|
|
|
if (bytes != null) {
|
2015-07-10 22:00:30 +02:00
|
|
|
// The unresolvedAddress was created via a String that contains an ipaddress.
|
2015-12-13 00:11:59 +01:00
|
|
|
promise.setSuccess(Collections.singletonList(InetAddress.getByAddress(bytes)));
|
2015-07-12 12:34:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
final String hostname = hostname(inetHost);
|
2015-07-12 12:34:05 +02:00
|
|
|
|
2015-12-14 15:27:49 +01:00
|
|
|
InetAddress hostsFileEntry = resolveHostsFileEntry(hostname);
|
|
|
|
if (hostsFileEntry != null) {
|
|
|
|
promise.setSuccess(Collections.singletonList(hostsFileEntry));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
if (!doResolveAllCached(hostname, promise, resolveCache)) {
|
|
|
|
doResolveAllUncached(hostname, promise, resolveCache);
|
2015-07-10 22:00:30 +02:00
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
private boolean doResolveAllCached(String hostname,
|
|
|
|
Promise<List<InetAddress>> promise,
|
|
|
|
DnsCache resolveCache) {
|
2015-07-12 12:34:05 +02:00
|
|
|
final List<DnsCacheEntry> cachedEntries = resolveCache.get(hostname);
|
|
|
|
if (cachedEntries == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
List<InetAddress> result = null;
|
2015-07-12 12:34:05 +02:00
|
|
|
Throwable cause = null;
|
|
|
|
synchronized (cachedEntries) {
|
|
|
|
final int numEntries = cachedEntries.size();
|
|
|
|
assert numEntries > 0;
|
|
|
|
|
|
|
|
if (cachedEntries.get(0).cause() != null) {
|
|
|
|
cause = cachedEntries.get(0).cause();
|
|
|
|
} else {
|
2015-12-14 15:27:49 +01:00
|
|
|
for (InternetProtocolFamily f : resolvedAddressTypes) {
|
2015-07-12 12:34:05 +02:00
|
|
|
for (int i = 0; i < numEntries; i++) {
|
|
|
|
final DnsCacheEntry e = cachedEntries.get(i);
|
|
|
|
if (f.addressType().isInstance(e.address())) {
|
|
|
|
if (result == null) {
|
2015-12-13 00:11:59 +01:00
|
|
|
result = new ArrayList<InetAddress>(numEntries);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
2015-12-13 00:11:59 +01:00
|
|
|
result.add(e.address());
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
promise.trySuccess(result);
|
|
|
|
} else if (cause != null) {
|
|
|
|
promise.tryFailure(cause);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-07 17:49:15 +01:00
|
|
|
private void doResolveAllUncached(final String hostname,
|
|
|
|
final Promise<List<InetAddress>> promise,
|
|
|
|
DnsCache resolveCache) {
|
2015-12-13 00:11:59 +01:00
|
|
|
final DnsNameResolverContext<List<InetAddress>> ctx =
|
2016-01-07 17:49:15 +01:00
|
|
|
new DnsNameResolverContext<List<InetAddress>>(this, hostname, promise, resolveCache) {
|
2015-07-12 12:34:05 +02:00
|
|
|
@Override
|
|
|
|
protected boolean finishResolve(
|
|
|
|
Class<? extends InetAddress> addressType, List<DnsCacheEntry> resolvedEntries) {
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
List<InetAddress> result = null;
|
2015-07-12 12:34:05 +02:00
|
|
|
final int numEntries = resolvedEntries.size();
|
|
|
|
for (int i = 0; i < numEntries; i++) {
|
|
|
|
final InetAddress a = resolvedEntries.get(i).address();
|
|
|
|
if (addressType.isInstance(a)) {
|
|
|
|
if (result == null) {
|
2015-12-13 00:11:59 +01:00
|
|
|
result = new ArrayList<InetAddress>(numEntries);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
2015-12-13 00:11:59 +01:00
|
|
|
result.add(a);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != null) {
|
|
|
|
promise().trySuccess(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ctx.resolve();
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:11:59 +01:00
|
|
|
private static String hostname(String inetHost) {
|
|
|
|
return IDN.toASCII(inetHost);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question.
|
|
|
|
*/
|
2015-03-16 07:46:14 +01:00
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(DnsQuestion question) {
|
2015-07-12 12:34:05 +02:00
|
|
|
return query(nextNameServerAddress(), question);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question with additional records.
|
|
|
|
*/
|
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
|
|
|
DnsQuestion question, Iterable<DnsRecord> additional) {
|
|
|
|
return query(nextNameServerAddress(), question, additional);
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question.
|
|
|
|
*/
|
2015-03-16 07:46:14 +01:00
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
|
|
|
DnsQuestion question, Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise) {
|
2016-02-13 14:06:06 +01:00
|
|
|
return query(nextNameServerAddress(), question, Collections.<DnsRecord>emptyList(), promise);
|
2015-07-12 12:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private InetSocketAddress nextNameServerAddress() {
|
2015-08-19 04:51:15 +02:00
|
|
|
return nameServerAddrStream.get().next();
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question using the specified name server list.
|
|
|
|
*/
|
2015-03-16 07:46:14 +01:00
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
2015-07-12 12:34:05 +02:00
|
|
|
InetSocketAddress nameServerAddr, DnsQuestion question) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
return query0(nameServerAddr, question, Collections.<DnsRecord>emptyList(),
|
|
|
|
ch.eventLoop().<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>>newPromise());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question with additional records using the specified name server list.
|
|
|
|
*/
|
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
|
|
|
InetSocketAddress nameServerAddr, DnsQuestion question, Iterable<DnsRecord> additional) {
|
|
|
|
|
|
|
|
return query0(nameServerAddr, question, additional,
|
|
|
|
ch.eventLoop().<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>>newPromise());
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question using the specified name server list.
|
|
|
|
*/
|
2015-03-16 07:46:14 +01:00
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
2015-07-12 12:34:05 +02:00
|
|
|
InetSocketAddress nameServerAddr, DnsQuestion question,
|
2015-03-16 07:46:14 +01:00
|
|
|
Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
return query0(nameServerAddr, question, Collections.<DnsRecord>emptyList(), promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a DNS query with the specified question with additional records using the specified name server list.
|
|
|
|
*/
|
|
|
|
public Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query(
|
|
|
|
InetSocketAddress nameServerAddr, DnsQuestion question,
|
|
|
|
Iterable<DnsRecord> additional,
|
|
|
|
Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise) {
|
|
|
|
|
|
|
|
return query0(nameServerAddr, question, additional, promise);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> query0(
|
2015-07-12 12:34:05 +02:00
|
|
|
InetSocketAddress nameServerAddr, DnsQuestion question,
|
2016-02-13 14:06:06 +01:00
|
|
|
Iterable<DnsRecord> additional,
|
2015-03-16 07:46:14 +01:00
|
|
|
Promise<AddressedEnvelope<? extends DnsResponse, InetSocketAddress>> promise) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2016-02-13 14:06:06 +01:00
|
|
|
final Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> castPromise = cast(
|
|
|
|
checkNotNull(promise, "promise"));
|
2014-09-19 15:36:32 +02:00
|
|
|
try {
|
2016-02-13 14:06:06 +01:00
|
|
|
new DnsQueryContext(this, nameServerAddr, question, additional, castPromise).query();
|
2015-03-16 07:46:14 +01:00
|
|
|
return castPromise;
|
2014-09-19 15:36:32 +02:00
|
|
|
} catch (Exception e) {
|
2015-03-16 07:46:14 +01:00
|
|
|
return castPromise.setFailure(e);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private static Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>> cast(Promise<?> promise) {
|
|
|
|
return (Promise<AddressedEnvelope<DnsResponse, InetSocketAddress>>) promise;
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
private final class DnsResponseHandler extends ChannelInboundHandlerAdapter {
|
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
|
try {
|
2015-03-16 07:46:14 +01:00
|
|
|
final DatagramDnsResponse res = (DatagramDnsResponse) msg;
|
|
|
|
final int queryId = res.id();
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
logger.debug("{} RECEIVED: [{}: {}], {}", ch, queryId, res.sender(), res);
|
|
|
|
}
|
|
|
|
|
2015-11-08 04:59:01 +01:00
|
|
|
final DnsQueryContext qCtx = queryContextManager.get(res.sender(), queryId);
|
2014-09-19 15:36:32 +02:00
|
|
|
if (qCtx == null) {
|
2016-01-07 17:49:15 +01:00
|
|
|
logger.warn("{} Received a DNS response with an unknown ID: {}", ch, queryId);
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
qCtx.finish(res);
|
2014-09-19 15:36:32 +02:00
|
|
|
} finally {
|
2014-10-16 10:56:12 +02:00
|
|
|
ReferenceCountUtil.safeRelease(msg);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
2015-07-12 12:34:05 +02:00
|
|
|
logger.warn("{} Unexpected exception: ", ch, cause);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|