'How to parse AAAA record RDATA using java
I want to parse the RDATA of a AAAA record response. As a quick 101 intro to DNS responses this is what an Answer section from a response looks like(according to RFC-1035 - mind you the format hasn't changed since it's inception, and most likely won't change any time soon):
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| |
/ /
/ NAME /
| |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| TYPE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| CLASS |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| TTL |
| |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| RDLENGTH |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
/ RDATA /
/ /
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
The last section RDATA is resource record type specific. So for example a CNAME record will have a different format.
According to RFC-3596 a AAAA data format is
A 128 bit IPv6 address is encoded in the data portion of an AAAA
resource record in network byte order (high-order byte first).
I haven't been able to find other RFCs pertaining specifically to AAAA record RDATA format.
Section 2.5 of RFC-3596 seems to better define how to parse the RDATA:
An IPv6 address is represented as a name in the IP6.ARPA domain by a
sequence of nibbles separated by dots with the suffix ".IP6.ARPA".
The sequence of nibbles is encoded in reverse order, i.e., the
low-order nibble is encoded first, followed by the next low-order
nibble and so on. Each nibble is represented by a hexadecimal digit.
For example, the reverse lookup domain name corresponding to the
address
4321:0:1:2:3:4:567:89ab
would be
b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.IP6.ARPA.
Putting 2 and 2 together it seems that the RDATA is 128 bit long little endian. With that said, I can't figure out HOW exactly to parse this correctly.
Here's my code
private AAAARecordData parseRDataForAAAARecord(DataInputStream dataInputStream, int rdLength) throws IOException {
byte[] byteArray = new byte[rdLength];
for (int i = 0; i < rdLength; i++) {
byteArray[i] = (byte)(dataInputStream.readByte() & 0xFF);
}
String result = HexFormat.of().formatHex(byteArray); // 2a001450400d0807000000000000200e
// do some more processing here maybe? not sure
return new AAAARecordData(result);
}
And some sample data for byteArray
[42, 0, 20, 80, 64, 13, 8, 7, 0, 0, 0, 0, 0, 0, 32, 14]
[42, 0, 20, 80, 64, 13, 8, 10, 0, 0, 0, 0, 0, 0, 32, 14]
The output of parseRDataForAAAARecord is 2a001450400d0804000000000000200e
Using wireshark to intercept the request I get
// other stuff
Answers
google.com: type AAAA, class IN, addr 2a00:1450:400d:80a::200e
Name: google.com
Type: AAAA (IPv6 Address) (28)
Class: IN (0x0001)
Time to live: 270 (4 minutes, 30 seconds)
Data length: 16
AAAA Address: 2a00:1450:400d:80a::200e
[Request In: 1]
[Time: 0.019593000 seconds]
As you may have guess by now, my result and the wireshark AAAA address are pretty similar. So clearly I'm not doing something correctly somewhere.
I'm fairly certain anything before the processing is correct as it works fine for A records and a few other record types.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

