Skip to content

Commit

Permalink
DnsNameResolver does not resolve property A+CNAME answer, close #1193
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Jun 20, 2016
1 parent b552bc4 commit 5bb166c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 63 deletions.
Expand Up @@ -90,7 +90,7 @@ protected DnsRecord decodeRecord(

if (type == DnsRecordType.PTR) {
in.setIndex(offset, offset + length);
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName(in));
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName0(in));
}
return new DefaultDnsRawRecord(
name, type, dnsClass, timeToLive, in.duplicate().setIndex(offset, offset + length).retain());
Expand All @@ -104,7 +104,19 @@ protected DnsRecord decodeRecord(
* @param in the byte buffer containing the DNS packet
* @return the domain name for an entry
*/
protected String decodeName(ByteBuf in) {
protected String decodeName0(ByteBuf in) {
return decodeName(in);
}

/**
* Retrieves a domain name given a buffer containing a DNS packet. If the
* name contains a pointer, the position of the buffer will be set to
* directly after the pointer's index after the name has been read.
*
* @param in the byte buffer containing the DNS packet
* @return the domain name for an entry
*/
public static String decodeName(ByteBuf in) {
int position = -1;
int checked = 0;
final int end = in.writerIndex();
Expand Down
Expand Up @@ -20,6 +20,7 @@
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.AddressedEnvelope;
import io.netty.channel.socket.InternetProtocolFamily;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.dns.DefaultDnsQuestion;
import io.netty.handler.codec.dns.DefaultDnsRecordDecoder;
import io.netty.handler.codec.dns.DnsResponseCode;
Expand All @@ -29,7 +30,6 @@
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.codec.dns.DnsRecordType;
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;
Expand Down Expand Up @@ -412,25 +412,23 @@ private void finishResolve() {
final int tries = maxAllowedQueries - allowedQueries;
final StringBuilder buf = new StringBuilder(64);

buf.append("failed to resolve ");
buf.append(hostname);

buf.append("failed to resolve '")
.append(hostname).append('\'');
if (tries > 1) {
buf.append(" after ");
buf.append(tries);
if (trace != null) {
buf.append(" queries:");
buf.append(trace);
if (tries < maxAllowedQueries) {
buf.append(" after ")
.append(tries)
.append(" queries ");
} else {
buf.append(" queries");
}
} else {
if (trace != null) {
buf.append(':');
buf.append(trace);
buf.append(". Exceeded max queries per resolve ")
.append(maxAllowedQueries)
.append(' ');
}
}

if (trace != null) {
buf.append(':')
.append(trace);
}
final UnknownHostException cause = new UnknownHostException(buf.toString());

resolveCache.cache(hostname, cause, parent.ch.eventLoop());
Expand All @@ -440,53 +438,15 @@ private void finishResolve() {
protected abstract boolean finishResolve(
InternetProtocolFamily f, List<DnsCacheEntry> resolvedEntries);

/**
* Adapted from {@link DefaultDnsRecordDecoder#decodeName(ByteBuf)}.
*/
static String decodeDomainName(ByteBuf buf) {
buf.markReaderIndex();
static String decodeDomainName(ByteBuf in) {
in.markReaderIndex();
try {
int position = -1;
int checked = 0;
final int end = buf.writerIndex();
final StringBuilder name = new StringBuilder(buf.readableBytes() << 1);
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;
}

final int next = (len & 0x3f) << 8 | buf.readUnsignedByte();
if (next >= end) {
// Should not happen.
return null;
}
buf.readerIndex(next);

// check for loops
checked += 2;
if (checked >= end) {
// 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);
return DefaultDnsRecordDecoder.decodeName(in);
} catch (CorruptedFrameException e) {
// In this case we just return null.
return null;
} finally {
buf.resetReaderIndex();
in.resetReaderIndex();
}
}

Expand Down

0 comments on commit 5bb166c

Please sign in to comment.