Fixed issues with ensuring appropriate hostName's are FQDN (absolute)
This commit is contained in:
parent
6d7ccb322e
commit
1bfcc61a92
@ -54,29 +54,31 @@ class DnsQuestion extends DnsMessage implements AddressedEnvelope<DnsQuestion, I
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static
|
private static
|
||||||
DnsQuestion newQuestion(final String inetHost, final int type, final boolean isRecursionDesired, boolean isResolveQuestion) {
|
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:
|
// See:
|
||||||
// - https://github.com/netty/netty/issues/4937
|
// - https://github.com/netty/netty/issues/4937
|
||||||
// - https://github.com/netty/netty/issues/4935
|
// - 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;
|
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 {
|
try {
|
||||||
DnsRecord questionRecord = DnsRecord.newRecord(Name.fromString(hostname), type, DnsClass.IN);
|
DnsRecord questionRecord = DnsRecord.newRecord(name, type, DnsClass.IN);
|
||||||
DnsQuestion question = new DnsQuestion(isResolveQuestion);
|
DnsQuestion question = new DnsQuestion(isResolveQuestion);
|
||||||
question.getHeader()
|
question.getHeader()
|
||||||
.setOpcode(DnsOpCode.QUERY);
|
.setOpcode(DnsOpCode.QUERY);
|
||||||
@ -92,23 +94,23 @@ class DnsQuestion extends DnsMessage implements AddressedEnvelope<DnsQuestion, I
|
|||||||
|
|
||||||
return question;
|
return question;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Name.fromString may throw a TextParseException if it fails to parse
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static
|
public static
|
||||||
String hostname(String inetHost) {
|
String hostNameAsciiFix(String inetHost) {
|
||||||
try {
|
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
|
// Check for http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6894622
|
||||||
if (StringUtil.endsWith(inetHost, '.') && !StringUtil.endsWith(hostname, '.')) {
|
if (StringUtil.endsWith(inetHost, '.') && !StringUtil.endsWith(hostName, '.')) {
|
||||||
return hostname + '.';
|
return hostName + '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
return hostname;
|
return hostName;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// java.net.IDN.toASCII(...) may throw an IllegalArgumentException if it fails to parse the hostname
|
// 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static
|
|
||||||
String appendTrailingDot(String hostname) {
|
|
||||||
if (!StringUtil.endsWith(hostname, '.')) {
|
|
||||||
return hostname + '.';
|
|
||||||
}
|
|
||||||
return hostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
public
|
||||||
void init(int id, InetSocketAddress recipient) {
|
void init(int id, InetSocketAddress recipient) {
|
||||||
getHeader().setID(id);
|
getHeader().setID(id);
|
||||||
|
@ -8,6 +8,7 @@ import dorkbox.network.dns.Mnemonic;
|
|||||||
import dorkbox.network.dns.exceptions.InvalidTypeException;
|
import dorkbox.network.dns.exceptions.InvalidTypeException;
|
||||||
import dorkbox.network.dns.records.DnsRecord;
|
import dorkbox.network.dns.records.DnsRecord;
|
||||||
import dorkbox.network.dns.records.DnsTypeProtoAssignment;
|
import dorkbox.network.dns.records.DnsTypeProtoAssignment;
|
||||||
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constants and functions relating to DNS Types
|
* Constants and functions relating to DNS Types
|
||||||
@ -562,4 +563,39 @@ class DnsRecordType {
|
|||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user