dns-suite.js/bin/dns-parse.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-01-21 10:18:10 +00:00
'use strict';
// EXAMPLE:
2017-01-21 21:44:37 +00:00
// node bin/dns-parse.js samples/a-0.mdns.bin
2017-01-21 10:18:10 +00:00
// pass a terminal arg
var filename = process.argv[2];
if (!filename) {
2017-01-21 21:44:37 +00:00
console.error("Usage: node bin/dns-parse.js <path/to/sample.bin>");
console.error("Example: node bin/dns-parse.js ./samples/services-0.mdns.bin");
2017-01-21 10:18:10 +00:00
process.exit(1);
}
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
var pdns = require('./pure-parser');
fs.readFileAsync(filename, null).then(function (nb) {
2017-01-21 10:32:50 +00:00
//
// current reference impl
//
//console.log(require('native-dns-packet').parse(nb));
//
// other reference impl
//
2017-01-21 21:33:51 +00:00
//console.log(require('dns-js').DNSPacket.parse(nb));
2017-01-21 10:32:50 +00:00
2017-01-21 10:18:10 +00:00
// nb is a Uint8Array (ArrayBufferView) for nb.buffer
// nb.buffer is the actual ArrayBuffer
2017-01-21 17:45:45 +00:00
var packet = pdns.unpack(nb.buffer);
2017-01-21 21:33:51 +00:00
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);
2017-01-21 17:45:45 +00:00
console.log('[packet]', nb.byteLength, 'bytes:');
console.log(packet);
2017-01-21 10:18:10 +00:00
});