factor out printing of header and query

This commit is contained in:
AJ ONeal 2017-09-20 13:27:53 -06:00
parent 57ced95c0d
commit cdd490ec42
2 changed files with 39 additions and 27 deletions

View File

@ -157,9 +157,9 @@ cli.main(function (args, cli) {
cli.onError = function (err) { cli.onError = function (err) {
console.error("error:", err.stack); console.error("error:", err.stack);
}; };
cli.onMessage = function (nb) { cli.onMessage = function (nb) {
var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength));
var flags = "";
if (packet.id !== query.id) { if (packet.id !== query.id) {
console.log('ignoring packet for ', packet.question[0].name); console.log('ignoring packet for ', packet.question[0].name);
@ -172,26 +172,8 @@ cli.main(function (args, cli) {
} }
console.log(';; Got answer:'); console.log(';; Got answer:');
// TODO opcode 0 QUERY rcode 0 NOERROR dig.logQuestion(packet);
console.log(';; ->>HEADER<<- [opcode: ' + packet.header.opcode + ', status: ' + packet.header.rcode + '], id: ' + packet.header.id);
if (packet.header.tc) { console.log("Truncated [tc] (we don't know the normal way to print a tc packet... you should record this with -o tc-packet.dig and send it to us)"); }
flags += ";; flags:";
if (packet.header.qr) { flags += " qr"; }
if (packet.header.aa) { flags += " aa"; }
if (packet.header.rd) { flags += " rd"; }
if (packet.header.ra) { flags += " ra"; }
flags += "; QUERY: " + packet.question.length + ", ANSWER: " + packet.answer.length + ", AUTHORITY: " + packet.authority.length + ", ADDITIONAL: " + packet.additional.length;
console.log(flags);
if (packet.header.res1) { console.log("[res1] (we don't know how to print a packet with res1 yet)"); }
if (packet.header.res2) { console.log("[res2] (we don't know how to print a packet with res2 yet)"); }
if (packet.header.res3) { console.log("[res3] (we don't know how to print a packet with res2 yet)"); }
// {"id":32736,"qr":1,"opcode":0,"aa":0,"tc":0,"rd":1,"ra":0,"res1":0,"res2":0,"res3":0,"rcode":5}
//console.log(JSON.stringify(packet.header));
console.log('');
console.log(';; QUESTION SECTION:');
packet.question.forEach(function (q) {
console.log(';' + q.name + '.', ' ', q.className, q.typeName);
});
function print(q) { function print(q) {
var printer = common.printers[q.typeName] || common.printers.ANY; var printer = common.printers[q.typeName] || common.printers.ANY;
printer(q); printer(q);

View File

@ -4,7 +4,32 @@ var dnsjs = require('dns-suite');
var crypto = require('crypto'); var crypto = require('crypto');
var dgram = require('dgram'); var dgram = require('dgram');
function request(queryAb, opts) { function logQuestion(packet) {
var flags = "";
// TODO opcode 0 QUERY rcode 0 NOERROR
console.log(';; ->>HEADER<<- [opcode: ' + packet.header.opcode + ', status: ' + packet.header.rcode + '], id: ' + packet.header.id);
if (packet.header.tc) { console.log("Truncated [tc] (we don't know the normal way to print a tc packet... you should record this with -o tc-packet.dig and send it to us)"); }
flags += ";; flags:";
if (packet.header.qr) { flags += " qr"; }
if (packet.header.aa) { flags += " aa"; }
if (packet.header.rd) { flags += " rd"; }
if (packet.header.ra) { flags += " ra"; }
flags += "; QUERY: " + packet.question.length + ", ANSWER: " + packet.answer.length + ", AUTHORITY: " + packet.authority.length + ", ADDITIONAL: " + packet.additional.length;
console.log(flags);
if (packet.header.res1) { console.log("[res1] (we don't know how to print a packet with res1 yet)"); }
if (packet.header.res2) { console.log("[res2] (we don't know how to print a packet with res2 yet)"); }
if (packet.header.res3) { console.log("[res3] (we don't know how to print a packet with res2 yet)"); }
// {"id":32736,"qr":1,"opcode":0,"aa":0,"tc":0,"rd":1,"ra":0,"res1":0,"res2":0,"res3":0,"rcode":5}
//console.log(JSON.stringify(packet.header));
console.log('');
console.log(';; QUESTION SECTION:');
packet.question.forEach(function (q) {
console.log(';' + q.name + '.', ' ', q.className, q.typeName);
});
}
function resolve(queryAb, opts) {
var handlers = {}; var handlers = {};
var nameservers; var nameservers;
var nameserver = opts.nameserver; var nameserver = opts.nameserver;
@ -76,7 +101,7 @@ function request(queryAb, opts) {
}, ms); }, ms);
} }
function requestJson(query, opts) { function resolveJson(query, opts) {
var queryAb = dnsjs.DNSPacket.write(query); var queryAb = dnsjs.DNSPacket.write(query);
var options = { var options = {
onError: opts.onError onError: opts.onError
@ -87,15 +112,20 @@ function requestJson(query, opts) {
} }
, onListening: opts.onListening , onListening: opts.onListening
, onSent: opts.onSent , onSent: opts.onSent
, onClose: opts.onClose
, onTimeout: opts.onTimeout
, mdns: opts.mdns , mdns: opts.mdns
, nameserver: opts.nameserver , nameserver: opts.nameserver
, port: opts.port , port: opts.port
, timeout: opts.timeout , timeout: opts.timeout
}; };
return request(queryAb, options); return resolve(queryAb, options);
} }
module.exports.resolve = request; module.exports.resolve = resolve;
module.exports.request = request; module.exports.resolveJson = resolveJson;
module.exports.requestJson = requestJson; module.exports.request = resolve;
module.exports.requestJson = resolveJson;
module.exports.logQuestion = logQuestion;