From 26816ee0b14becfa2882a84f2b25635a61558498 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 17 Feb 2017 19:40:01 -0700 Subject: [PATCH] send query, receive response --- bin/dig.js | 161 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 55 deletions(-) diff --git a/bin/dig.js b/bin/dig.js index b3399d0..1633611 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -52,6 +52,108 @@ function hexdump(ab) { return head + '\n' + str + '\n' + trail; } +function request(query, opts) { + var queryAb = dnsjs.DNSPacket.write(query); + + if (opts.debug) { + console.log(''); + console.log('DNS Question:'); + console.log(''); + console.log(query); + console.log(''); + console.log(hexdump(queryAb)); + console.log(''); + console.log(dnsjs.DNSPacket.parse(queryAb)); + console.log(''); + } + + var count = 0; + var handlers = {}; + var server = dgram.createSocket({ + type: 'udp4' + , reuseAddr: true + }); + + handlers.onError = function (err) { + console.error("error:", err.stack); + server.close(); + }; + handlers.onMessage = function (nb) { + var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); + + if (packet.id !== query.id) { + console.log('ignoring packet for ', packet.question[0].name); + } + + if (!opts.mdns) { + server.close(); + } + + console.log(''); + console.log('DNS Response:'); + console.log(packet); + + if (opts.output) { + var path = require('path'); + var filename = packet.question[0].typeName + '-' + count + '.bin'; + var fullpath = path.join('samples', filename); + + count += 1; + + return fs.writeFileAsync(fullpath, nb).then(function () { + console.log('wrote ' + nb.length + ' bytes to ' + fullpath); + }); + } + }; + handlers.onListening = function () { + /*jshint validthis:true*/ + var server = this; + var nameserver = opts.nameserver; + var nameservers; + var index; + + if (!nameserver) { + nameservers = require('dns').getServers(); + index = (Math.round(Math.random() * 7777)) % nameservers.length; + nameserver = nameservers[index]; + if (opts.debug) { + console.log(index, nameservers); + } + } + + if (opts.mdns || '224.0.0.251' === opts.nameserver) { + server.setBroadcast(true); + server.addMembership(opts.nameserver); + } + + if (opts.debug) { + console.log(''); + console.log('Bound and Listening:'); + console.log(server.address()); + } + + if (opts.debug) { + console.log('querying ' + nameserver + ':' + opts.port); + } + server.send(Buffer.from(queryAb), opts.port, nameserver, function () { + if (opts.debug) { + console.log(''); + console.log('request sent'); + } + }); + }; + + + server.on('error', handlers.onError); + server.on('message', handlers.onMessage); + server.on('listening', handlers.onListening); + + // 0 dns request + // 53 dns server + // 5353 mdns + server.bind(0); +} + cli.main(function (args, cli) { args.forEach(function (arg) { if (-1 !== commonTypes.indexOf(arg.toUpperCase())) { @@ -88,6 +190,9 @@ cli.main(function (args, cli) { if (!cli.port) { cli.port = cli.p = 5353; } + if (!cli.nameserver) { + cli.nameserver = '224.0.0.251'; + } } if (!cli.type) { @@ -128,60 +233,6 @@ cli.main(function (args, cli) { } ] }; - var queryAb = dnsjs.DNSPacket.write(query); - if (cli.debug) { - console.log(''); - console.log('DNS Question:'); - console.log(''); - console.log(query); - console.log(''); - console.log(hexdump(queryAb)); - console.log(''); - console.log(dnsjs.DNSPacket.parse(queryAb)); - console.log(''); - } - - - - - process.exit(1); - var server = dgram.createSocket({ - type: 'udp4' - , reuseAddr: true - }); - - var handlers = {}; - handlers.onError = function (err) { - console.error("error:", err.stack); - server.close(); - }; - handlers.onMessage = function (buffer) { - var path = require('path'); - var filename = type + '-' + count + '.mdns.bin'; - var fullpath = path.join('samples', filename); - - count += 1; - - fs.writeFileAsync(fullpath, buffer).then(function () { - console.log('wrote ' + buffer.length + ' bytes to ' + fullpath); - }); - }; - handlers.onListening = function () { - /*jshint validthis:true*/ - var server = this; - console.log(server.address()); - - server.setBroadcast(true); - server.addMembership('224.0.0.251'); - }; - - - server.on('error', handlers.onError); - server.on('message', handlers.onMessage); - server.on('listening', handlers.onListening); - - // 53 dns - // 5353 mdns - server.bind(5353); + request(query, cli); });