[#4993] Correctly handle trailing dot in DNS requests and responses for the hostname.
Motivation: We need to handle the trailing dot in the correct manner when creating DNS questions and responses. Modifications: - Add a trailing dot if not given to the hostname when construct a AbstractDnsRecord (this is the same as dig does). Result: Correctly handle trailing dots.
This commit is contained in:
parent
42419d918d
commit
48506f5b05
@ -67,12 +67,19 @@ public abstract class AbstractDnsRecord implements DnsRecord {
|
|||||||
// 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
|
||||||
this.name = IDN.toASCII(checkNotNull(name, "name"));
|
this.name = appendTrailingDot(IDN.toASCII(checkNotNull(name, "name")));
|
||||||
this.type = checkNotNull(type, "type");
|
this.type = checkNotNull(type, "type");
|
||||||
this.dnsClass = (short) dnsClass;
|
this.dnsClass = (short) dnsClass;
|
||||||
this.timeToLive = timeToLive;
|
this.timeToLive = timeToLive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String appendTrailingDot(String name) {
|
||||||
|
if (name.length() > 0 && name.charAt(name.length() - 1) != '.') {
|
||||||
|
return name + '.';
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name() {
|
public String name() {
|
||||||
return name;
|
return name;
|
||||||
|
@ -24,14 +24,28 @@ public class AbstractDnsRecordTest {
|
|||||||
public void testValidDomainName() {
|
public void testValidDomainName() {
|
||||||
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||||
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
||||||
Assert.assertEquals(name, record.name());
|
Assert.assertEquals(name + '.', record.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidDomainNameUmlaut() {
|
public void testValidDomainNameUmlaut() {
|
||||||
String name = "ä";
|
String name = "ä";
|
||||||
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
||||||
Assert.assertEquals("xn--4ca", record.name());
|
Assert.assertEquals("xn--4ca.", record.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidDomainNameTrailingDot() {
|
||||||
|
String name = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.";
|
||||||
|
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
||||||
|
Assert.assertEquals(name, record.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidDomainNameUmlautTrailingDot() {
|
||||||
|
String name = "ä.";
|
||||||
|
AbstractDnsRecord record = new AbstractDnsRecord(name, DnsRecordType.A, 0) { };
|
||||||
|
Assert.assertEquals("xn--4ca.", record.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
Loading…
Reference in New Issue
Block a user