diff --git a/lib/tcpd.js b/lib/tcpd.js index 8b6d608..2c4b4e9 100644 --- a/lib/tcpd.js +++ b/lib/tcpd.js @@ -53,10 +53,11 @@ module.exports.create = function (cli, dnsd) { process.exit(0); } console.error("TCP Server Error:"); - console.error(err); + console.error(err.stack); tcpServer.close(function () { setTimeout(runTcp, 1000); }); + //throw new Error(err); }); tcpServer.listen(cli.port, function () { diff --git a/lib/udpd.js b/lib/udpd.js index fbdf317..f969ec2 100644 --- a/lib/udpd.js +++ b/lib/udpd.js @@ -1,58 +1,66 @@ 'use strict'; module.exports.create = function (cli, dnsd) { - var server = require('dgram').createSocket({ - type: cli.udp6 ? 'udp6' : 'udp4' - , reuseAddr: true - }); - server.bind({ - port: cli.port - , address: cli.address - }); - - var handlers = {}; - handlers.onError = function (err) { - if ('EACCES' === err.code) { - console.error(""); - console.error("EACCES: Couldn't bind to port. You probably need to use sudo, authbind, or setcap."); - console.error(""); - process.exit(123); - return; - } - console.error("error:", err.stack); - server.close(); - }; - - handlers.onMessage = function (nb, rinfo) { - //console.log('[DEBUG] got a UDP message', nb.length); - //console.log(nb.toString('hex')); - - dnsd.onMessage(nb, function (err, newAb, dbgmsg) { - // TODO send legit error message - if (err) { server.send(Buffer.from([0x00])); return; } - server.send(newAb, rinfo.port, rinfo.address, function () { - console.log(dbgmsg, rinfo.port, rinfo.address); - }); + function runUdp() { + var server = require('dgram').createSocket({ + type: cli.udp6 ? 'udp6' : 'udp4' + , reuseAddr: true + }); + server.bind({ + port: cli.port + , address: cli.address }); - }; - handlers.onListening = function () { - /*jshint validthis:true*/ - var server = this; + var handlers = {}; + handlers.onError = function (err) { + if ('EACCES' === err.code) { + console.error(""); + console.error("EACCES: Couldn't bind to port. You probably need to use sudo, authbind, or setcap."); + console.error(""); + process.exit(123); + return; + } + console.error("UDP Server Error:"); + console.error(err.stack); + server.close(function () { + setTimeout(runUdp, 1000); + }); + //throw new Error(err); + }; - if (cli.mdns || '224.0.0.251' === cli.nameserver) { - server.setBroadcast(true); - server.addMembership(cli.nameserver); - } + handlers.onMessage = function (nb, rinfo) { + //console.log('[DEBUG] got a UDP message', nb.length); + //console.log(nb.toString('hex')); - console.log(''); - console.log('Bound and Listening:'); - console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')'); - }; + dnsd.onMessage(nb, function (err, newAb, dbgmsg) { + // TODO send legit error message + if (err) { server.send(Buffer.from([0x00]), rinfo.port, rinfo.address); return; } + server.send(newAb, rinfo.port, rinfo.address, function () { + console.log('[dnsd.onMessage] ' + dbgmsg, rinfo.port, rinfo.address); + }); + }); + }; - server.on('error', handlers.onError); - server.on('message', handlers.onMessage); - server.on('listening', handlers.onListening); + handlers.onListening = function () { + /*jshint validthis:true*/ + var server = this; - return server; + if (cli.mdns || '224.0.0.251' === cli.nameserver) { + server.setBroadcast(true); + server.addMembership(cli.nameserver); + } + + console.log(''); + console.log('Bound and Listening:'); + console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')'); + }; + + server.on('error', handlers.onError); + server.on('message', handlers.onMessage); + server.on('listening', handlers.onListening); + + return server; + } + + return runUdp(); };