dns-suite.js/dns.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

;(function (exports) {
'use strict';
// Value: IP Address
// Meaning:Use: 16 octets represting the IP address
var Parser = (exports.DNS_PARSER || require('./dns.parser.js').DNS_PARSER);
var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER);
exports.DNSPacket = {
parse: function (nb) {
// backwards compat with node buffer
var ab = nb.buffer || nb;
var packet = Parser.unpack(ab);
function tryParseRdata(record) {
record.data = null;
record.error = null;
try {
record.data = Parser.unpackRdata(ab, packet, record);
} catch (e) {
console.error('[Error] unpackRdata: ' + e.message);
record.error = e;
}
}
packet.answer.forEach(tryParseRdata);
packet.authority.forEach(tryParseRdata);
packet.additional.forEach(tryParseRdata);
return packet;
}
, write: function (json) {
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));