simplify dns.type.aaaa.js

This commit is contained in:
AJ ONeal 2017-01-30 11:21:06 -07:00
parent a05b5d27f2
commit 6d5251524b
2 changed files with 18 additions and 24 deletions

View File

@ -39,7 +39,7 @@ fs.readFileAsync(filename, null).then(function (nb) {
try { try {
record.data = pdns.unpackRdata(nb.buffer, packet, record); record.data = pdns.unpackRdata(nb.buffer, packet, record);
} catch (e) { } catch (e) {
console.error(e.stack); console.error('[Error] unpackRdata: ' + e.message);
record.error = e; record.error = e;
} }
} }

View File

@ -6,34 +6,28 @@
exports.DNS_TYPE_AAAA = function (ab, packet, record) { exports.DNS_TYPE_AAAA = function (ab, packet, record) {
var ui8 = record.rdata; var rdataAb = ab.slice(record.rdstart, record.rdstart + record.rdlength);
console.log("ByteLength: " + ui8.byteLength); // We can't use Uint16Array because it doesn't specify Endianness
console.log("normal length: " + ui8.length); // 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;
var s = ''; var s = '';
var z = 0;
for (var i = 0; i < ui8.length; i += 1) { // Note: byteLength is always 16 for AAAA records
console.log(ui8[i].toString(16)); for (i = 0; i < dv.byteLength; i += 2) {
if (i%2 === 1 && ui8[i].toString(16).length < 2) { if (i > 0 && i < dv.byteLength) {
s = s + '0' + ui8[i].toString(16); // add ':' between all... sedectets? sexdectets? ... pairs of two octets :)
} else { s += ':';
s = s + ui8[i].toString(16); }
if (ui8[i].toString(16) === '0' && i%2 === 0){
s = s.substring(0, s.length-1);
}
}
if (i%2 === 1) { // Big Endian Uint16 is specified by using `false`, Little Endian is `true`
s = s + ":" s += dv.getUint16(i, false).toString(16);
}
if (i === 15) {
s = s.substring(0, s.length-1);
}
} }
console.log(s);
return s; return s;
}; };
}('undefined' !== typeof window ? window : exports)); }('undefined' !== typeof window ? window : exports));