add support for modular rdata parsing

This commit is contained in:
AJ ONeal 2017-01-21 14:33:51 -07:00
parent 98eb48dd34
commit 96a851e5ad
3 changed files with 61 additions and 18 deletions

44
dns.rdata.parse.js Normal file
View File

@ -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 <script src=\"dns.type." + record.typeName.toLowerCase() + ".js\"></script> ?"
+ " (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));

View File

@ -26,12 +26,27 @@ fs.readFileAsync(filename, null).then(function (nb) {
// //
// other reference impl // 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 is a Uint8Array (ArrayBufferView) for nb.buffer
// nb.buffer is the actual ArrayBuffer // nb.buffer is the actual ArrayBuffer
var packet = pdns.unpack(nb.buffer); 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]', nb.byteLength, 'bytes:');
console.log(packet); console.log(packet);
}); });

View File

@ -26,23 +26,6 @@ pdns.unpackHeader = function (i) {
return header; 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) { pdns.unpackQname = function (ui8) {
var total = 0; var total = 0;
var i; var i;
@ -230,3 +213,4 @@ pdns.unpack = function (ab) {
header.total = total; header.total = total;
return header; return header;
}; };
pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE;