diff --git a/bin/dns-parse.js b/bin/dns-parse.js index cf138db..b15df91 100644 --- a/bin/dns-parse.js +++ b/bin/dns-parse.js @@ -39,7 +39,7 @@ fs.readFileAsync(filename, null).then(function (nb) { try { record.data = pdns.unpackRdata(nb.buffer, packet, record); } catch (e) { - console.error(e.stack); + console.error('[Error] unpackRdata: ' + e.message); record.error = e; } } diff --git a/dns.type.aaaa.js b/dns.type.aaaa.js index 8db0337..6c8cd19 100644 --- a/dns.type.aaaa.js +++ b/dns.type.aaaa.js @@ -6,34 +6,28 @@ exports.DNS_TYPE_AAAA = function (ab, packet, record) { - var ui8 = record.rdata; - console.log("ByteLength: " + ui8.byteLength); - console.log("normal length: " + ui8.length); + 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; var s = ''; - var z = 0; - for (var i = 0; i < ui8.length; i += 1) { - console.log(ui8[i].toString(16)); - + // Note: byteLength is always 16 for AAAA records + for (i = 0; i < dv.byteLength; i += 2) { - if (i%2 === 1 && ui8[i].toString(16).length < 2) { - s = s + '0' + ui8[i].toString(16); - } else { - s = s + ui8[i].toString(16); - if (ui8[i].toString(16) === '0' && i%2 === 0){ - s = s.substring(0, s.length-1); - } - } + if (i > 0 && i < dv.byteLength) { + // add ':' between all... sedectets? sexdectets? ... pairs of two octets :) + s += ':'; + } - if (i%2 === 1) { - s = s + ":" - } - - if (i === 15) { - s = s.substring(0, s.length-1); - } + // Big Endian Uint16 is specified by using `false`, Little Endian is `true` + s += dv.getUint16(i, false).toString(16); } - console.log(s); + return s; }; + }('undefined' !== typeof window ? window : exports));