From cdd490ec42986885d028142cfd2384fdef4b0cf1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Wed, 20 Sep 2017 13:27:53 -0600 Subject: [PATCH] factor out printing of header and query --- bin/dig.js | 24 +++--------------------- dns-request.js | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/bin/dig.js b/bin/dig.js index 9d9b35c..17baffc 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -157,9 +157,9 @@ cli.main(function (args, cli) { cli.onError = function (err) { console.error("error:", err.stack); }; + cli.onMessage = function (nb) { var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); - var flags = ""; if (packet.id !== query.id) { console.log('ignoring packet for ', packet.question[0].name); @@ -172,26 +172,8 @@ cli.main(function (args, cli) { } console.log(';; Got answer:'); - // 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); - }); + dig.logQuestion(packet); + function print(q) { var printer = common.printers[q.typeName] || common.printers.ANY; printer(q); diff --git a/dns-request.js b/dns-request.js index bb49454..cc1720a 100644 --- a/dns-request.js +++ b/dns-request.js @@ -4,7 +4,32 @@ var dnsjs = require('dns-suite'); var crypto = require('crypto'); 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 nameservers; var nameserver = opts.nameserver; @@ -76,7 +101,7 @@ function request(queryAb, opts) { }, ms); } -function requestJson(query, opts) { +function resolveJson(query, opts) { var queryAb = dnsjs.DNSPacket.write(query); var options = { onError: opts.onError @@ -87,15 +112,20 @@ function requestJson(query, opts) { } , onListening: opts.onListening , onSent: opts.onSent + , onClose: opts.onClose + , onTimeout: opts.onTimeout , mdns: opts.mdns , nameserver: opts.nameserver , port: opts.port , timeout: opts.timeout }; - return request(queryAb, options); + return resolve(queryAb, options); } -module.exports.resolve = request; -module.exports.request = request; -module.exports.requestJson = requestJson; +module.exports.resolve = resolve; +module.exports.resolveJson = resolveJson; +module.exports.request = resolve; +module.exports.requestJson = resolveJson; + +module.exports.logQuestion = logQuestion;