Fixed issues with ensuring appropriate hostName's are FQDN (absolute)

This commit is contained in:
nathan 2018-01-11 14:59:33 +01:00
parent 6d7ccb322e
commit 1bfcc61a92
2 changed files with 56 additions and 26 deletions

View File

@ -54,29 +54,31 @@ class DnsQuestion extends DnsMessage implements AddressedEnvelope<DnsQuestion, I
}
private static
DnsQuestion newQuestion(final String inetHost, final int type, final boolean isRecursionDesired, boolean isResolveQuestion) {
// Convert to ASCII which will also check that the length is not too big.
// Convert to ASCII which will also check that the length is not too big. Throws null pointer if null.
// See:
// - https://github.com/netty/netty/issues/4937
// - https://github.com/netty/netty/issues/4935
String hostname = hostname(checkNotNull(inetHost, "hostname"));
String hostName = hostNameAsciiFix(checkNotNull(inetHost, "hostname"));
if (hostname == null) {
hostName = hostName.toLowerCase(Locale.US);
// NOTE: have to make sure that the hostname is a FQDN name
hostName = DnsRecordType.ensureFQDN(type, hostName);
Name name;
try {
name = Name.fromString(hostName);
} catch (Exception e) {
// Name.fromString may throw a TextParseException if it fails to parse
return null;
}
hostname = hostname.toLowerCase(Locale.US);
if (type == DnsRecordType.A || type == DnsRecordType.AAAA) {
// resolving a hostname -> ip address, the hostname MUST end in a dot
hostname = appendTrailingDot(hostname);
}
try {
DnsRecord questionRecord = DnsRecord.newRecord(Name.fromString(hostname), type, DnsClass.IN);
DnsRecord questionRecord = DnsRecord.newRecord(name, type, DnsClass.IN);
DnsQuestion question = new DnsQuestion(isResolveQuestion);
question.getHeader()
.setOpcode(DnsOpCode.QUERY);
@ -92,23 +94,23 @@ class DnsQuestion extends DnsMessage implements AddressedEnvelope<DnsQuestion, I
return question;
} catch (Exception e) {
// Name.fromString may throw a TextParseException if it fails to parse
e.printStackTrace();
}
return null;
}
public static
String hostname(String inetHost) {
String hostNameAsciiFix(String inetHost) {
try {
String hostname = java.net.IDN.toASCII(inetHost);
String hostName = java.net.IDN.toASCII(inetHost);
// Check for http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6894622
if (StringUtil.endsWith(inetHost, '.') && !StringUtil.endsWith(hostname, '.')) {
return hostname + '.';
if (StringUtil.endsWith(inetHost, '.') && !StringUtil.endsWith(hostName, '.')) {
return hostName + '.';
}
return hostname;
return hostName;
} catch (Exception e) {
// java.net.IDN.toASCII(...) may throw an IllegalArgumentException if it fails to parse the hostname
}
@ -116,14 +118,6 @@ class DnsQuestion extends DnsMessage implements AddressedEnvelope<DnsQuestion, I
return null;
}
private static
String appendTrailingDot(String hostname) {
if (!StringUtil.endsWith(hostname, '.')) {
return hostname + '.';
}
return hostname;
}
public
void init(int id, InetSocketAddress recipient) {
getHeader().setID(id);

View File

@ -8,6 +8,7 @@ import dorkbox.network.dns.Mnemonic;
import dorkbox.network.dns.exceptions.InvalidTypeException;
import dorkbox.network.dns.records.DnsRecord;
import dorkbox.network.dns.records.DnsTypeProtoAssignment;
import io.netty.util.internal.StringUtil;
/**
* Constants and functions relating to DNS Types
@ -562,4 +563,39 @@ class DnsRecordType {
return true;
}
}
private static final String ptrSuffix = ".in-addr.arpa";
public static
String ensureFQDN(int type, String hostName) {
// list of RecordTypes from: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html
switch (type) {
case PTR:
if (!hostName.endsWith(ptrSuffix)) {
// PTR absolutely MUST end in '.in-addr.arpa' in order for the DNS server to understand it.
// in this case, hostname is an ip address
return hostName + ptrSuffix;
}
case A:
case AAAA:
case CAA:
case CNAME:
case MX:
case NAPTR:
case NS:
case SOA:
case SPF:
case SRV:
case TXT:
// resolving a hostname -> ip address, the hostname MUST end in a dot
if (!StringUtil.endsWith(hostName, '.')) {
return hostName + '.';
} else {
return hostName;
}
default:
return hostName;
}
}
}