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.buffer.ByteBuf;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.buffer.ByteBufHolder;
|
|
|
|
import io.netty.channel.AddressedEnvelope;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.channel.socket.InternetProtocolFamily;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DefaultDnsQuestion;
|
|
|
|
import io.netty.handler.codec.dns.DefaultDnsRecordDecoder;
|
2015-07-12 12:34:05 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsResponseCode;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DnsSection;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsQuestion;
|
2015-03-16 07:46:14 +01:00
|
|
|
import io.netty.handler.codec.dns.DnsRawRecord;
|
|
|
|
import io.netty.handler.codec.dns.DnsRecord;
|
|
|
|
import io.netty.handler.codec.dns.DnsRecordType;
|
2014-09-19 15:36:32 +02:00
|
|
|
import io.netty.handler.codec.dns.DnsResponse;
|
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
import io.netty.util.concurrent.FutureListener;
|
|
|
|
import io.netty.util.concurrent.Promise;
|
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
|
|
|
|
|
import java.net.Inet4Address;
|
|
|
|
import java.net.Inet6Address;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.IdentityHashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
abstract class DnsNameResolverContext<T> {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
private static final int INADDRSZ4 = 4;
|
|
|
|
private static final int INADDRSZ6 = 16;
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private static final FutureListener<AddressedEnvelope<DnsResponse, InetSocketAddress>> RELEASE_RESPONSE =
|
|
|
|
new FutureListener<AddressedEnvelope<DnsResponse, InetSocketAddress>>() {
|
|
|
|
@Override
|
|
|
|
public void operationComplete(Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> future) {
|
|
|
|
if (future.isSuccess()) {
|
|
|
|
future.getNow().release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
private final DnsNameResolver parent;
|
2015-08-19 04:51:15 +02:00
|
|
|
private final DnsServerAddressStream nameServerAddrs;
|
2015-07-12 12:34:05 +02:00
|
|
|
private final Promise<T> promise;
|
2014-09-19 15:36:32 +02:00
|
|
|
private final String hostname;
|
2015-07-12 12:34:05 +02:00
|
|
|
private final boolean traceEnabled;
|
2014-09-19 15:36:32 +02:00
|
|
|
private final int maxAllowedQueries;
|
|
|
|
private final InternetProtocolFamily[] resolveAddressTypes;
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private final Set<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> queriesInProgress =
|
|
|
|
Collections.newSetFromMap(
|
|
|
|
new IdentityHashMap<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>, Boolean>());
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
private List<DnsCacheEntry> resolvedEntries;
|
2014-09-19 15:36:32 +02:00
|
|
|
private StringBuilder trace;
|
|
|
|
private int allowedQueries;
|
|
|
|
private boolean triedCNAME;
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
protected DnsNameResolverContext(DnsNameResolver parent, String hostname, Promise<T> promise) {
|
2014-09-19 15:36:32 +02:00
|
|
|
this.parent = parent;
|
|
|
|
this.promise = promise;
|
|
|
|
this.hostname = hostname;
|
|
|
|
|
2015-08-19 04:51:15 +02:00
|
|
|
nameServerAddrs = parent.nameServerAddresses.stream();
|
2014-09-19 15:36:32 +02:00
|
|
|
maxAllowedQueries = parent.maxQueriesPerResolve();
|
|
|
|
resolveAddressTypes = parent.resolveAddressTypesUnsafe();
|
2015-07-12 12:34:05 +02:00
|
|
|
traceEnabled = parent.isTraceEnabled();
|
2014-09-19 15:36:32 +02:00
|
|
|
allowedQueries = maxAllowedQueries;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
protected Promise<T> promise() {
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
void resolve() {
|
2015-12-07 09:06:57 +01:00
|
|
|
InetSocketAddress nameServerAddrToTry = nameServerAddrs.next();
|
2014-09-19 15:36:32 +02:00
|
|
|
for (InternetProtocolFamily f: resolveAddressTypes) {
|
2015-03-16 07:46:14 +01:00
|
|
|
final DnsRecordType type;
|
2014-09-19 15:36:32 +02:00
|
|
|
switch (f) {
|
|
|
|
case IPv4:
|
2015-03-16 07:46:14 +01:00
|
|
|
type = DnsRecordType.A;
|
2014-09-19 15:36:32 +02:00
|
|
|
break;
|
|
|
|
case IPv6:
|
2015-03-16 07:46:14 +01:00
|
|
|
type = DnsRecordType.AAAA;
|
2014-09-19 15:36:32 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
|
2015-12-07 09:06:57 +01:00
|
|
|
query(nameServerAddrToTry, new DefaultDnsQuestion(hostname, type));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
private void query(InetSocketAddress nameServerAddr, final DnsQuestion question) {
|
2014-09-19 15:36:32 +02:00
|
|
|
if (allowedQueries == 0 || promise.isCancelled()) {
|
2015-08-27 05:52:29 +02:00
|
|
|
tryToFinishResolve();
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedQueries --;
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
final Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = parent.query(nameServerAddr, question);
|
2014-09-19 15:36:32 +02:00
|
|
|
queriesInProgress.add(f);
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
f.addListener(new FutureListener<AddressedEnvelope<DnsResponse, InetSocketAddress>>() {
|
2014-09-19 15:36:32 +02:00
|
|
|
@Override
|
2015-03-16 07:46:14 +01:00
|
|
|
public void operationComplete(Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> future) {
|
2014-09-19 15:36:32 +02:00
|
|
|
queriesInProgress.remove(future);
|
|
|
|
|
2015-10-30 04:53:05 +01:00
|
|
|
if (promise.isDone() || future.isCancelled()) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (future.isSuccess()) {
|
|
|
|
onResponse(question, future.getNow());
|
|
|
|
} else {
|
2015-07-12 12:34:05 +02:00
|
|
|
// Server did not respond or I/O error occurred; try again.
|
|
|
|
if (traceEnabled) {
|
|
|
|
addTrace(future.cause());
|
|
|
|
}
|
|
|
|
query(nameServerAddrs.next(), question);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
tryToFinishResolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
void onResponse(final DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
|
2014-09-19 15:36:32 +02:00
|
|
|
try {
|
2015-07-12 12:34:05 +02:00
|
|
|
final DnsResponse res = envelope.content();
|
|
|
|
final DnsResponseCode code = res.code();
|
|
|
|
if (code == DnsResponseCode.NOERROR) {
|
|
|
|
final DnsRecordType type = question.type();
|
|
|
|
if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
|
|
|
|
onResponseAorAAAA(type, question, envelope);
|
|
|
|
} else if (type == DnsRecordType.CNAME) {
|
|
|
|
onResponseCNAME(question, envelope);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (traceEnabled) {
|
|
|
|
addTrace(envelope.sender(),
|
|
|
|
"response code: " + code + " with " + res.count(DnsSection.ANSWER) + " answer(s) and " +
|
|
|
|
res.count(DnsSection.AUTHORITY) + " authority resource(s)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry with the next server if the server did not tell us that the domain does not exist.
|
|
|
|
if (code != DnsResponseCode.NXDOMAIN) {
|
|
|
|
query(nameServerAddrs.next(), question);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
} finally {
|
2015-07-12 12:34:05 +02:00
|
|
|
ReferenceCountUtil.safeRelease(envelope);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private void onResponseAorAAAA(
|
|
|
|
DnsRecordType qType, DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
// We often get a bunch of CNAMES as well when we asked for A/AAAA.
|
2015-03-16 07:46:14 +01:00
|
|
|
final DnsResponse response = envelope.content();
|
2014-09-19 15:36:32 +02:00
|
|
|
final Map<String, String> cnames = buildAliasMap(response);
|
2015-03-16 07:46:14 +01:00
|
|
|
final int answerCount = response.count(DnsSection.ANSWER);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
boolean found = false;
|
2015-03-16 07:46:14 +01:00
|
|
|
for (int i = 0; i < answerCount; i ++) {
|
|
|
|
final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
|
|
|
|
final DnsRecordType type = r.type();
|
|
|
|
if (type != DnsRecordType.A && type != DnsRecordType.AAAA) {
|
2014-09-19 15:36:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String qName = question.name().toLowerCase(Locale.US);
|
|
|
|
final String rName = r.name().toLowerCase(Locale.US);
|
|
|
|
|
|
|
|
// Make sure the record is for the questioned domain.
|
|
|
|
if (!rName.equals(qName)) {
|
|
|
|
// Even if the record's name is not exactly same, it might be an alias defined in the CNAME records.
|
|
|
|
String resolved = qName;
|
|
|
|
do {
|
|
|
|
resolved = cnames.get(resolved);
|
|
|
|
if (rName.equals(resolved)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (resolved != null);
|
|
|
|
|
|
|
|
if (resolved == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
if (!(r instanceof DnsRawRecord)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final ByteBuf content = ((ByteBufHolder) r).content();
|
2014-09-19 15:36:32 +02:00
|
|
|
final int contentLen = content.readableBytes();
|
|
|
|
if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
final byte[] addrBytes = new byte[contentLen];
|
|
|
|
content.getBytes(content.readerIndex(), addrBytes);
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
final InetAddress resolved;
|
2014-09-19 15:36:32 +02:00
|
|
|
try {
|
2015-07-12 12:34:05 +02:00
|
|
|
resolved = InetAddress.getByAddress(hostname, addrBytes);
|
2014-09-19 15:36:32 +02:00
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
// Should never reach here.
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
2015-07-12 12:34:05 +02:00
|
|
|
|
|
|
|
if (resolvedEntries == null) {
|
|
|
|
resolvedEntries = new ArrayList<DnsCacheEntry>(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
final DnsCacheEntry e = new DnsCacheEntry(hostname, resolved);
|
|
|
|
parent.cache(hostname, resolved, r.timeToLive());
|
|
|
|
resolvedEntries.add(e);
|
|
|
|
found = true;
|
|
|
|
|
|
|
|
// Note that we do not break from the loop here, so we decode/cache all A/AAAA records.
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
if (traceEnabled) {
|
|
|
|
addTrace(envelope.sender(), "no matching " + qType + " record found");
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
// We aked for A/AAAA but we got only CNAME.
|
|
|
|
if (!cnames.isEmpty()) {
|
2015-03-16 07:46:14 +01:00
|
|
|
onResponseCNAME(question, envelope, cnames, false);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
private void onResponseCNAME(DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> envelope) {
|
|
|
|
onResponseCNAME(question, envelope, buildAliasMap(envelope.content()), true);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onResponseCNAME(
|
2015-03-16 07:46:14 +01:00
|
|
|
DnsQuestion question, AddressedEnvelope<DnsResponse, InetSocketAddress> response,
|
|
|
|
Map<String, String> cnames, boolean trace) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
|
|
|
// Resolve the host name in the question into the real host name.
|
|
|
|
final String name = question.name().toLowerCase(Locale.US);
|
|
|
|
String resolved = name;
|
|
|
|
boolean found = false;
|
|
|
|
for (;;) {
|
|
|
|
String next = cnames.get(resolved);
|
|
|
|
if (next != null) {
|
|
|
|
found = true;
|
|
|
|
resolved = next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
followCname(response.sender(), name, resolved);
|
2015-07-12 12:34:05 +02:00
|
|
|
} else if (trace && traceEnabled) {
|
2014-09-19 15:36:32 +02:00
|
|
|
addTrace(response.sender(), "no matching CNAME record found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Map<String, String> buildAliasMap(DnsResponse response) {
|
2015-03-16 07:46:14 +01:00
|
|
|
final int answerCount = response.count(DnsSection.ANSWER);
|
2014-09-19 15:36:32 +02:00
|
|
|
Map<String, String> cnames = null;
|
2015-03-16 07:46:14 +01:00
|
|
|
for (int i = 0; i < answerCount; i ++) {
|
|
|
|
final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
|
|
|
|
final DnsRecordType type = r.type();
|
|
|
|
if (type != DnsRecordType.CNAME) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(r instanceof DnsRawRecord)) {
|
2014-09-19 15:36:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
final ByteBuf recordContent = ((ByteBufHolder) r).content();
|
|
|
|
final String domainName = decodeDomainName(recordContent);
|
|
|
|
if (domainName == null) {
|
2014-09-19 15:36:32 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnames == null) {
|
|
|
|
cnames = new HashMap<String, String>();
|
|
|
|
}
|
|
|
|
|
2015-03-16 07:46:14 +01:00
|
|
|
cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnames != null? cnames : Collections.<String, String>emptyMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tryToFinishResolve() {
|
|
|
|
if (!queriesInProgress.isEmpty()) {
|
|
|
|
// There are still some queries we did not receive responses for.
|
|
|
|
if (gotPreferredAddress()) {
|
|
|
|
// But it's OK to finish the resolution process if we got a resolved address of the preferred type.
|
|
|
|
finishResolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We did not get any resolved address of the preferred type, so we can't finish the resolution process.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no queries left to try.
|
2015-07-12 12:34:05 +02:00
|
|
|
if (resolvedEntries == null) {
|
2014-09-19 15:36:32 +02:00
|
|
|
// .. and we could not find any A/AAAA records.
|
|
|
|
if (!triedCNAME) {
|
|
|
|
// As the last resort, try to query CNAME, just in case the name server has it.
|
|
|
|
triedCNAME = true;
|
2015-07-12 12:34:05 +02:00
|
|
|
query(nameServerAddrs.next(), new DefaultDnsQuestion(hostname, DnsRecordType.CNAME));
|
2014-09-19 15:36:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have at least one resolved address or tried CNAME as the last resort..
|
|
|
|
finishResolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean gotPreferredAddress() {
|
2015-07-12 12:34:05 +02:00
|
|
|
if (resolvedEntries == null) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
final int size = resolvedEntries.size();
|
2014-09-19 15:36:32 +02:00
|
|
|
switch (resolveAddressTypes[0]) {
|
|
|
|
case IPv4:
|
|
|
|
for (int i = 0; i < size; i ++) {
|
2015-07-12 12:34:05 +02:00
|
|
|
if (resolvedEntries.get(i).address() instanceof Inet4Address) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPv6:
|
|
|
|
for (int i = 0; i < size; i ++) {
|
2015-07-12 12:34:05 +02:00
|
|
|
if (resolvedEntries.get(i).address() instanceof Inet6Address) {
|
2014-09-19 15:36:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finishResolve() {
|
|
|
|
if (!queriesInProgress.isEmpty()) {
|
|
|
|
// If there are queries in progress, we should cancel it because we already finished the resolution.
|
2015-03-16 07:46:14 +01:00
|
|
|
for (Iterator<Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> i = queriesInProgress.iterator();
|
|
|
|
i.hasNext();) {
|
|
|
|
Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = i.next();
|
2014-09-19 15:36:32 +02:00
|
|
|
i.remove();
|
|
|
|
|
|
|
|
if (!f.cancel(false)) {
|
|
|
|
f.addListener(RELEASE_RESPONSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
if (resolvedEntries != null) {
|
2014-09-19 15:36:32 +02:00
|
|
|
// Found at least one resolved address.
|
|
|
|
for (InternetProtocolFamily f: resolveAddressTypes) {
|
2015-07-12 12:34:05 +02:00
|
|
|
if (finishResolve(f.addressType(), resolvedEntries)) {
|
|
|
|
return;
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No resolved address found.
|
2015-07-12 12:34:05 +02:00
|
|
|
final int tries = maxAllowedQueries - allowedQueries;
|
|
|
|
final StringBuilder buf = new StringBuilder(64);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
buf.append("failed to resolve ");
|
|
|
|
buf.append(hostname);
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
if (tries > 1) {
|
|
|
|
buf.append(" after ");
|
|
|
|
buf.append(tries);
|
|
|
|
if (trace != null) {
|
|
|
|
buf.append(" queries:");
|
|
|
|
buf.append(trace);
|
|
|
|
} else {
|
|
|
|
buf.append(" queries");
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
2015-07-12 12:34:05 +02:00
|
|
|
} else {
|
|
|
|
if (trace != null) {
|
|
|
|
buf.append(':');
|
|
|
|
buf.append(trace);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
final UnknownHostException cause = new UnknownHostException(buf.toString());
|
|
|
|
|
|
|
|
parent.cache(hostname, cause);
|
|
|
|
promise.tryFailure(cause);
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
protected abstract boolean finishResolve(
|
|
|
|
Class<? extends InetAddress> addressType, List<DnsCacheEntry> resolvedEntries);
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
/**
|
2015-03-16 07:46:14 +01:00
|
|
|
* Adapted from {@link DefaultDnsRecordDecoder#decodeName(ByteBuf)}.
|
2014-09-19 15:36:32 +02:00
|
|
|
*/
|
|
|
|
static String decodeDomainName(ByteBuf buf) {
|
|
|
|
buf.markReaderIndex();
|
|
|
|
try {
|
|
|
|
int position = -1;
|
|
|
|
int checked = 0;
|
2015-03-16 07:46:14 +01:00
|
|
|
final int end = buf.writerIndex();
|
|
|
|
final StringBuilder name = new StringBuilder(buf.readableBytes() << 1);
|
2014-09-19 15:36:32 +02:00
|
|
|
for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) {
|
|
|
|
boolean pointer = (len & 0xc0) == 0xc0;
|
|
|
|
if (pointer) {
|
|
|
|
if (position == -1) {
|
|
|
|
position = buf.readerIndex() + 1;
|
|
|
|
}
|
2015-03-16 07:46:14 +01:00
|
|
|
|
|
|
|
final int next = (len & 0x3f) << 8 | buf.readUnsignedByte();
|
|
|
|
if (next >= end) {
|
|
|
|
// Should not happen.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
buf.readerIndex(next);
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
// check for loops
|
|
|
|
checked += 2;
|
2015-03-16 07:46:14 +01:00
|
|
|
if (checked >= end) {
|
2014-09-19 15:36:32 +02:00
|
|
|
// Name contains a loop; give up.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
|
|
|
|
buf.skipBytes(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (position != -1) {
|
|
|
|
buf.readerIndex(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.length() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name.substring(0, name.length() - 1);
|
|
|
|
} finally {
|
|
|
|
buf.resetReaderIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
private void followCname(InetSocketAddress nameServerAddr, String name, String cname) {
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
if (traceEnabled) {
|
|
|
|
if (trace == null) {
|
|
|
|
trace = new StringBuilder(128);
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
trace.append(StringUtil.NEWLINE);
|
|
|
|
trace.append("\tfrom ");
|
|
|
|
trace.append(nameServerAddr);
|
|
|
|
trace.append(": ");
|
|
|
|
trace.append(name);
|
|
|
|
trace.append(" CNAME ");
|
|
|
|
trace.append(cname);
|
|
|
|
}
|
2014-09-19 15:36:32 +02:00
|
|
|
|
2015-07-12 12:34:05 +02:00
|
|
|
final InetSocketAddress nextAddr = nameServerAddrs.next();
|
|
|
|
query(nextAddr, new DefaultDnsQuestion(cname, DnsRecordType.A));
|
|
|
|
query(nextAddr, new DefaultDnsQuestion(cname, DnsRecordType.AAAA));
|
2014-09-19 15:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addTrace(InetSocketAddress nameServerAddr, String msg) {
|
2015-07-12 12:34:05 +02:00
|
|
|
assert traceEnabled;
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
if (trace == null) {
|
|
|
|
trace = new StringBuilder(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace.append(StringUtil.NEWLINE);
|
|
|
|
trace.append("\tfrom ");
|
|
|
|
trace.append(nameServerAddr);
|
|
|
|
trace.append(": ");
|
|
|
|
trace.append(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addTrace(Throwable cause) {
|
2015-07-12 12:34:05 +02:00
|
|
|
assert traceEnabled;
|
|
|
|
|
2014-09-19 15:36:32 +02:00
|
|
|
if (trace == null) {
|
|
|
|
trace = new StringBuilder(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
trace.append(StringUtil.NEWLINE);
|
|
|
|
trace.append("Caused by: ");
|
|
|
|
trace.append(cause);
|
|
|
|
}
|
|
|
|
}
|