dns-suite.js/parser/type.aaaa.js

49 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2017-01-27 02:18:46 +00:00
(function (exports) {
'use strict';
// Value: IP Address
// Meaning:Use: 16 octets represting the IP address
2017-02-17 23:13:57 +00:00
exports.DNS_PARSER_TYPE_AAAA = function (ab, packet, record) {
2017-01-29 01:35:57 +00:00
2017-01-30 18:21:06 +00:00
var rdataAb = ab.slice(record.rdstart, record.rdstart + record.rdlength);
// We can't use Uint16Array because it doesn't specify Endianness
// Intel x86, x64, and (usually) ARM are Little Endian, but Network Order is Big Endian
// DataView *does* let us specify endianness, so we can use that
// http://stackoverflow.com/questions/13514614/why-is-network-byte-order-defined-to-be-big-endian
var dv = new DataView(rdataAb);
var i = 0;
2017-01-29 01:35:57 +00:00
var s = '';
2017-01-30 18:21:06 +00:00
// Note: byteLength is always 16 for AAAA records
for (i = 0; i < dv.byteLength; i += 2) {
if (i > 0 && i < dv.byteLength) {
// add ':' between all... sedectets? sexdectets? ... pairs of two octets :)
s += ':';
}
// Big Endian Uint16 is specified by using `false`, Little Endian is `true`
s += dv.getUint16(i, false).toString(16);
2017-01-29 01:35:57 +00:00
}
2017-01-30 18:21:06 +00:00
// Represent the string address as recommended on the wikipedia page
// https://en.wikipedia.org/wiki/IPv6_address#Recommended_representation_as_text.
// (shorten the longest section of 0's as long as it's more than one section, replacing
// the left-most instance in the event of ties.)
var re = /:(0:)+/g;
var match;
var longest = '_BAD';
while (!!(match = re.exec(s))) {
if (match[0].length > longest.length) {
longest = match[0];
}
}
s = s.replace(longest, '::');
record.address = s;
return record;
2017-01-27 02:18:46 +00:00
};
2017-01-30 18:21:06 +00:00
2017-01-27 02:18:46 +00:00
}('undefined' !== typeof window ? window : exports));