Skip to content

Commit 5bb166c

Browse files
committed
DnsNameResolver does not resolve property A+CNAME answer, close AsyncHttpClient#1193
1 parent b552bc4 commit 5bb166c

File tree

2 files changed

+35
-63
lines changed

2 files changed

+35
-63
lines changed

netty-bp/codec-dns/src/main/java/io/netty/handler/codec/dns/DefaultDnsRecordDecoder.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected DnsRecord decodeRecord(
9090

9191
if (type == DnsRecordType.PTR) {
9292
in.setIndex(offset, offset + length);
93-
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName(in));
93+
return new DefaultDnsPtrRecord(name, dnsClass, timeToLive, decodeName0(in));
9494
}
9595
return new DefaultDnsRawRecord(
9696
name, type, dnsClass, timeToLive, in.duplicate().setIndex(offset, offset + length).retain());
@@ -104,7 +104,19 @@ protected DnsRecord decodeRecord(
104104
* @param in the byte buffer containing the DNS packet
105105
* @return the domain name for an entry
106106
*/
107-
protected String decodeName(ByteBuf in) {
107+
protected String decodeName0(ByteBuf in) {
108+
return decodeName(in);
109+
}
110+
111+
/**
112+
* Retrieves a domain name given a buffer containing a DNS packet. If the
113+
* name contains a pointer, the position of the buffer will be set to
114+
* directly after the pointer's index after the name has been read.
115+
*
116+
* @param in the byte buffer containing the DNS packet
117+
* @return the domain name for an entry
118+
*/
119+
public static String decodeName(ByteBuf in) {
108120
int position = -1;
109121
int checked = 0;
110122
final int end = in.writerIndex();

netty-bp/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.netty.buffer.ByteBufHolder;
2121
import io.netty.channel.AddressedEnvelope;
2222
import io.netty.channel.socket.InternetProtocolFamily;
23+
import io.netty.handler.codec.CorruptedFrameException;
2324
import io.netty.handler.codec.dns.DefaultDnsQuestion;
2425
import io.netty.handler.codec.dns.DefaultDnsRecordDecoder;
2526
import io.netty.handler.codec.dns.DnsResponseCode;
@@ -29,7 +30,6 @@
2930
import io.netty.handler.codec.dns.DnsRecord;
3031
import io.netty.handler.codec.dns.DnsRecordType;
3132
import io.netty.handler.codec.dns.DnsResponse;
32-
import io.netty.util.CharsetUtil;
3333
import io.netty.util.ReferenceCountUtil;
3434
import io.netty.util.concurrent.Future;
3535
import io.netty.util.concurrent.FutureListener;
@@ -412,25 +412,23 @@ private void finishResolve() {
412412
final int tries = maxAllowedQueries - allowedQueries;
413413
final StringBuilder buf = new StringBuilder(64);
414414

415-
buf.append("failed to resolve ");
416-
buf.append(hostname);
417-
415+
buf.append("failed to resolve '")
416+
.append(hostname).append('\'');
418417
if (tries > 1) {
419-
buf.append(" after ");
420-
buf.append(tries);
421-
if (trace != null) {
422-
buf.append(" queries:");
423-
buf.append(trace);
418+
if (tries < maxAllowedQueries) {
419+
buf.append(" after ")
420+
.append(tries)
421+
.append(" queries ");
424422
} else {
425-
buf.append(" queries");
426-
}
427-
} else {
428-
if (trace != null) {
429-
buf.append(':');
430-
buf.append(trace);
423+
buf.append(". Exceeded max queries per resolve ")
424+
.append(maxAllowedQueries)
425+
.append(' ');
431426
}
432427
}
433-
428+
if (trace != null) {
429+
buf.append(':')
430+
.append(trace);
431+
}
434432
final UnknownHostException cause = new UnknownHostException(buf.toString());
435433

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

443-
/**
444-
* Adapted from {@link DefaultDnsRecordDecoder#decodeName(ByteBuf)}.
445-
*/
446-
static String decodeDomainName(ByteBuf buf) {
447-
buf.markReaderIndex();
441+
static String decodeDomainName(ByteBuf in) {
442+
in.markReaderIndex();
448443
try {
449-
int position = -1;
450-
int checked = 0;
451-
final int end = buf.writerIndex();
452-
final StringBuilder name = new StringBuilder(buf.readableBytes() << 1);
453-
for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) {
454-
boolean pointer = (len & 0xc0) == 0xc0;
455-
if (pointer) {
456-
if (position == -1) {
457-
position = buf.readerIndex() + 1;
458-
}
459-
460-
final int next = (len & 0x3f) << 8 | buf.readUnsignedByte();
461-
if (next >= end) {
462-
// Should not happen.
463-
return null;
464-
}
465-
buf.readerIndex(next);
466-
467-
// check for loops
468-
checked += 2;
469-
if (checked >= end) {
470-
// Name contains a loop; give up.
471-
return null;
472-
}
473-
} else {
474-
name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
475-
buf.skipBytes(len);
476-
}
477-
}
478-
479-
if (position != -1) {
480-
buf.readerIndex(position);
481-
}
482-
483-
if (name.length() == 0) {
484-
return null;
485-
}
486-
487-
return name.substring(0, name.length() - 1);
444+
return DefaultDnsRecordDecoder.decodeName(in);
445+
} catch (CorruptedFrameException e) {
446+
// In this case we just return null.
447+
return null;
488448
} finally {
489-
buf.resetReaderIndex();
449+
in.resetReaderIndex();
490450
}
491451
}
492452

0 commit comments

Comments
 (0)