2017-01-30 23:30:08 +00:00
|
|
|
;(function (exports) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Parser = (exports.DNS_PARSER || require('./dns.parser.js').DNS_PARSER);
|
|
|
|
var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER);
|
2017-02-23 23:35:19 +00:00
|
|
|
//var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES;
|
|
|
|
//var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES;
|
2017-01-30 23:30:08 +00:00
|
|
|
exports.DNSPacket = {
|
|
|
|
parse: function (nb) {
|
|
|
|
// backwards compat with node buffer
|
2017-02-24 01:09:21 +00:00
|
|
|
var ab = nb;
|
|
|
|
if (nb.buffer) {
|
|
|
|
// TODO pass byteOffsets to unpacker
|
|
|
|
ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength);
|
|
|
|
}
|
2017-01-30 23:30:08 +00:00
|
|
|
var packet = Parser.unpack(ab);
|
|
|
|
|
|
|
|
function tryParseRdata(record) {
|
2017-02-23 23:35:19 +00:00
|
|
|
|
2017-01-30 23:30:08 +00:00
|
|
|
try {
|
2017-02-03 01:39:32 +00:00
|
|
|
|
|
|
|
record = Parser.unpackRdata(ab, packet, record);
|
2017-01-30 23:30:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('[Error] unpackRdata: ' + e.message);
|
|
|
|
record.error = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 01:28:43 +00:00
|
|
|
packet.answer.forEach(tryParseRdata);
|
2017-01-30 23:30:08 +00:00
|
|
|
packet.authority.forEach(tryParseRdata);
|
|
|
|
packet.additional.forEach(tryParseRdata);
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
2017-02-23 23:35:19 +00:00
|
|
|
// Backwards compat
|
2017-01-30 23:30:08 +00:00
|
|
|
, write: function (json) {
|
2017-02-23 23:35:19 +00:00
|
|
|
return Buffer.from(Packer.pack(json));
|
|
|
|
}
|
|
|
|
, pack: function (json) {
|
2017-01-30 23:30:08 +00:00
|
|
|
return Packer.pack(json);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ('undefined' !== typeof module) {
|
|
|
|
// backwards compat node dns.js
|
|
|
|
exports.Parser = exports.DNS_PARSER;
|
|
|
|
exports.Packer = exports.DNS_PACKER;
|
|
|
|
}
|
|
|
|
|
|
|
|
}('undefined' !== typeof window ? window : exports));
|