diff --git a/dns.rdata.parse.js b/dns.rdata.parse.js new file mode 100644 index 0000000..894c808 --- /dev/null +++ b/dns.rdata.parse.js @@ -0,0 +1,44 @@ +(function (exports) { +'use strict'; + +var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES; +var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES; + +exports.DNS_RDATA_PARSE = function (ab, packet, record) { + // ab is needed if the rdata makes use of compression pointers + // packet is given for convenience + var parser; + + record.className = classes[record.class]; + if (!record.className) { + throw new Error("Support for DNS Class " + record.class.toString(16) + "(" + record.class + ")" + + " is not implemented yet. Open an issue if you actually need support" + + " (i.e. you're not working with a malformed packet)" + ); + } + + record.typeName = types[record.type]; + if (!record.typeName) { + throw new Error("Support for DNS Type " + record.type.toString(16) + "(" + record.type + ")" + + " is not implemented yet. Open an issue if you actually need support" + + " (i.e. you're not working with a malformed packet)" + ); + } + + try { + parser = exports['DNS_TYPE_' + record.typeName] + || require('./dns.type.' + record.typeName.toLowerCase()); + } + catch (e) { /*console.error(e)*/ } + + if (!parser) { + throw new Error("Parser for DNS Type " + record.typeName + " could not be loaded." + + " Did you include ?" + + " (or perhaps we plan to implement it and haven't yet - in which case please open an issue)" + ); + } + + return parser(ab, packet, record); +}; + +}('undefined' !== typeof window ? window : exports)); diff --git a/pure-parser-cli.js b/pure-parser-cli.js index 9e793bd..6706a35 100644 --- a/pure-parser-cli.js +++ b/pure-parser-cli.js @@ -26,12 +26,27 @@ fs.readFileAsync(filename, null).then(function (nb) { // // other reference impl // - console.log(require('dns-js').DNSPacket.parse(nb)); + //console.log(require('dns-js').DNSPacket.parse(nb)); // nb is a Uint8Array (ArrayBufferView) for nb.buffer // nb.buffer is the actual ArrayBuffer var packet = pdns.unpack(nb.buffer); + function tryParseRdata(record) { + record.data = null; + record.error = null; + + try { + record.data = pdns.unpackRdata(nb.buffer, packet, record); + } catch (e) { + record.error = e; + } + } + + packet.answers.forEach(tryParseRdata); + packet.authority.forEach(tryParseRdata); + packet.additional.forEach(tryParseRdata); + console.log('[packet]', nb.byteLength, 'bytes:'); console.log(packet); }); diff --git a/pure-parser.js b/pure-parser.js index 4896362..c982514 100644 --- a/pure-parser.js +++ b/pure-parser.js @@ -26,23 +26,6 @@ pdns.unpackHeader = function (i) { return header; }; -pdns.packHeader = function(h) { - var val = 0; - - val += (h.qr << 15) & 0x8000; - val += (h.opcode << 11) & 0x7800; - val += (h.aa << 10) & 0x400; - val += (h.tc << 9) & 0x200; - val += (h.rd << 8) & 0x100; - val += (h.ra << 7) & 0x80; - val += (h.res1 << 6) & 0x40; - val += (h.res2 << 5) & 0x20; - val += (h.res3 << 4) & 0x10; - val += h.rcode & 0xF; - - return val; -}; - pdns.unpackQname = function (ui8) { var total = 0; var i; @@ -230,3 +213,4 @@ pdns.unpack = function (ab) { header.total = total; return header; }; +pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE;