Updates to improve mDNS support and DNS recursion #6

Open
Ghost wants to merge 1 commits from (deleted):httpd into httpd
4 changed files with 166 additions and 42 deletions

View File

@ -31,6 +31,7 @@ cli.parse({
, 'address': [ false, 'ip address(es) to listen on (defaults to 0.0.0.0,::0)', 'string' ]
, 'port': [ 'p', 'port (defaults to 53 for dns and 5353 for mdns)', 'int' ]
, 'nameserver': [ false, 'the nameserver(s) to use for recursive lookups (defaults to ' + defaultNameservers.join(',') + ')', 'string' ]
, 'send': [ false, 'send query and response messages to the parent process', 'boolean', false ]
//, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ]
//, 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ]
//, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ]
@ -90,35 +91,61 @@ cli.main(function (args, cli) {
if (!('timeout' in cli)) {
cli.timeout = 3000;
}
cli.norecurse = true;
} else {
if (!cli.port) {
cli.port = cli.p = 53;
}
}
cli.send = cli.send && (typeof process.send === 'function');
function sendMsg(msg) {
if (cli.send)
process.send(msg);
}
var engine;
var path = require('path');
var engineOpts = { filepath: path.resolve(cli.input) };
var dnsd = {};
dnsd.onMessage = function (nb, cb) {
var byteOffset = nb._dnsByteOffset || nb.byteOffset;
var queryAb = nb.buffer.slice(byteOffset, byteOffset + nb.byteLength);
var query;
var count;
var byteLength = 0;
if (typeof nb === 'object') {
byteLength = nb.byteLength;
var byteOffset = nb._dnsByteOffset || nb.byteOffset;
var queryAb = nb.buffer.slice(byteOffset, byteOffset + nb.byteLength);
try {
query = dnsjs.DNSPacket.parse(queryAb);
} catch(e) {
// TODO log bad queries (?)
console.error("Could not parse DNS query, ignoring.");
console.error(e);
try {
hexdump = require('hexdump.js').hexdump;
console.error(hexdump(queryAb));
console.error('');
query = dnsjs.DNSPacket.parse(queryAb);
} catch(e) {
// ignore
// TODO log bad queries (?)
console.error("Could not parse DNS query, ignoring.");
console.error(e);
try {
hexdump = require('hexdump.js').hexdump;
console.error(hexdump(queryAb));
console.error('');
} catch(e) {
// ignore
}
return;
}
} else {
// Special mDNS only startup query to advertise DNS-SD records
query = {
header: { id: 0, qr: 0, opcode: 0, aa: 1, tc: 0, rd: 0, ra: 0, rcode: 0 }
, question: [{ name: nb, type: 12, typeName: 'PTR', class: 1, className: 'IN', unicastResponse: false }]
, answer: []
, authority: []
, additional: []
, dns_sd_startup: true
};
}
if (cli.mdns && query.header.qr) {
console.log('Ignoring mDNS answer loopback');
return;
}
@ -154,19 +181,20 @@ cli.main(function (args, cli) {
printer(q);
}
if (query.answer.length) {
console.error('[ERROR] Query contains an answer section:');
if (!cli.mdns)
console.log('[ERROR] Query contains an answer section:');
console.log(';; ANSWER SECTION:');
query.answer.forEach(print);
}
if (query.authority.length) {
console.log('');
console.error('[ERROR] Query contains an authority section:');
console.log('[ERROR] Query contains an authority section:');
console.log(';; AUTHORITY SECTION:');
query.authority.forEach(print);
}
if (query.additional.length) {
console.log('');
console.error('[ERROR] Query contains an additional section:');
console.log('[ERROR] Query contains an additional section:');
console.log(';; ADDITIONAL SECTION:');
query.additional.forEach(print);
}
@ -179,6 +207,7 @@ cli.main(function (args, cli) {
common.writeQuery(cli, query, queryAb);
//common.writeResponse(opts, query, nb, packet);
}
sendMsg({query: query});
function sendEmptyResponse(query, rcode) {
// rcode
@ -189,7 +218,7 @@ cli.main(function (args, cli) {
var newAb;
var emptyResp = {
header: {
id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
id: query.header.id || require('crypto').randomBytes(2).readUInt16BE(0)
, qr: 1
, opcode: 0
, aa: 0 // TODO it may be authoritative
@ -213,22 +242,44 @@ cli.main(function (args, cli) {
});
});
try {
newAb = dnsjs.DNSPacket.write(emptyResp);
} catch(e) {
console.error("Could not write empty DNS response");
console.error(e);
console.error(emptyResp);
cb(e, null, '[DEV] response sent (empty)');
return;
}
if (!cli.mdns) { // no empty response for mDNS
try {
newAb = dnsjs.DNSPacket.write(emptyResp);
} catch(e) {
console.error("Could not write empty DNS response");
console.error(e);
console.error(emptyResp);
cb(e, null, '[DEV] response sent (empty)');
return;
}
cb(null, newAb, '[DEV] response sent (empty)');
cb(null, newAb, '[DEV] response sent (empty)');
}
}
function sendResponse(newPacket) {
if (cli.mdns && !query.question[0].unicastResponse) {
newPacket.question = [];
}
var newAb;
if (cli.mdns) {
let answers = [];
newPacket.answer.forEach(a => {
// Don't send response if it is already in known answers in query
if (query.answer.find(q => { return q.data === a.data; })) {
console.log('Not sending local response for', a.data, '- already known');
} else {
answers.push(a);
}
});
if (!answers.length)
return;
newPacket.answer = answers;
}
try {
newAb = dnsjs.DNSPacket.write(newPacket);
} catch(e) {
@ -239,7 +290,26 @@ cli.main(function (args, cli) {
return;
}
cb(null, newAb, '[DEV] response sent (local query)');
if (cli.mdns) {
// mDNS requires id in response header to be 0. Force it here as DNSPacket.write doesn't know about mDNS
newAb.writeUInt16LE(0, 0);
newPacket.header.id = 0;
}
if (cli.mdns && !query.question[0].unicastResponse) {
// mDNS requires a random delay between 20 and 120ms to avoid collisions
setTimeout(() => {
cb(null, newAb, '[DEV] response sent (local query)', query.question[0].unicastResponse||false)
sendMsg({local: newPacket});
if (query.dns_sd_startup && query.question[0].name === '_services._dns-sd._udp.local')
newPacket.answer.forEach(a => dnsd.onMessage(a.data, cb)); // advertise the records referenced by the startup _services query
}, 20 + (100.0 * Math.random()) >> 0
);
} else {
cb(null, newAb, '[DEV] response sent (local query)', query.question[0].unicastResponse||false);
sendMsg({local: newPacket});
}
}
function recurse() {
@ -259,7 +329,7 @@ cli.main(function (args, cli) {
var newResponse = {
header: {
id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
id: query.header.id || require('crypto').randomBytes(2).readUInt16BE(0)
, qr: 0
, opcode: 0
, aa: 0 // query.header.aa ? 1 : 0 // NA? not sure what this would do
@ -297,6 +367,7 @@ cli.main(function (args, cli) {
}
cb(null, newAb, '[DEV] response sent');
sendMsg({recurse: newResponse});
}
}
@ -306,6 +377,7 @@ cli.main(function (args, cli) {
}
, onMessage: function (packet) {
// yay! recursion was available after all!
newResponse.header.qr = 1;
newResponse.header.ra = 1;
newResponse.header.rcode = NOERROR;
@ -392,7 +464,16 @@ cli.main(function (args, cli) {
respondWithResults(e);
return;
}
require('../lib/digd.js').query(engine, query, respondWithResults);
if (cli.mdns) {
// mdns allows multiple questions - should coalesce results...
query.question.forEach(q => {
let singleQ = query;
singleQ.question = [q];
require('../lib/digd.js').query(engine, singleQ, respondWithResults);
})
} else {
require('../lib/digd.js').query(engine, query, respondWithResults);
}
};
cli.defaultNameservers = defaultNameservers;
@ -407,6 +488,8 @@ cli.main(function (args, cli) {
console.log('index, defaultNameservers', index, cli.defaultNameservers);
}
}
sendMsg({ready: 'digd.js v' + pkg.version});
});
if (cli.tcp /* TODO v1.3 !cli.notcp */) {
require('../lib/tcpd.js').create(cli, dnsd);

View File

@ -7,6 +7,7 @@ module.exports.ask = function (query, cb) {
*/
var NOERROR = 0;
var SERVFAIL = 2;
var NXDOMAIN = 3;
var REFUSED = 5;
@ -242,7 +243,7 @@ module.exports.query = function (engine, query, cb) {
var results = {
header: {
id: query.header.id // same as request
id: query.header.id || require('crypto').randomBytes(2).readUInt16BE(0) // same as request if not mDNS
, qr: 1
, opcode: 0 // pretty much always 0 QUERY
, aa: 1 // TODO right now we assume that if we have the record, we're authoritative
@ -289,10 +290,10 @@ module.exports.query = function (engine, query, cb) {
console.log('[SOA] looking for', qnames, 'and proudly serving', err, myDomains);
if (err) { cb(err); return; }
// this should result in a REFUSED status
// this should result in a SERVFAIL status
if (!myDomains.length) {
// REFUSED will have no records, so we could still recursion, if enabled
results.header.rcode = REFUSED;
// SERVFAIL will have no records, so we could still recursion, if enabled
results.header.rcode = SERVFAIL;
cb(null, results);
return;
}
@ -392,9 +393,14 @@ module.exports.query = function (engine, query, cb) {
hasA = hasA || ('A' === r.type || 'A' === r.typeName || 'AAAA' === r.type || 'AAAA' === r.typeName);
return passCnames || ((r.type && r.type === query.question[0].type)
|| (r.type && r.type === query.question[0].typeName)
|| (r.typeName && r.typeName === query.question[0].typeName)
const isDNS_SD_Record = ('SRV' === r.type) || ('TXT' === r.type);
return passCnames || (
(r.type && r.type === query.question[0].type) ||
(r.type && r.type === query.question[0].typeName) ||
(r.typeName && r.typeName === query.question[0].typeName) ||
(('A' === r.type) && (('PTR' === query.question[0].typeName) || ('SRV' === query.question[0].typeName))) ||
isDNS_SD_Record
);
});
@ -407,6 +413,15 @@ module.exports.query = function (engine, query, cb) {
}
}
// DNS-SD requires selected matching records to be put into additional records - rfc6763 12.1, 12.2
myRecords = myRecords.filter(function (r) {
if (query.question[0].name !== r.name) {
results.additional.push(dbToResourceRecord(r));
return false;
} else
return true;
});
if (myRecords.length) {
myRecords.forEach(function (r) {
results.answer.push(dbToResourceRecord(r));

View File

@ -369,7 +369,8 @@ module.exports.create = function (opts) {
});
}
, get: function (query, cb) {
var myRecords = db.records.slice(0).filter(function (r) {
const localRecords = db.records.slice(0);
var myRecords = localRecords.filter(function (r) {
if ('string' !== typeof r.name) {
return false;
@ -383,6 +384,26 @@ module.exports.create = function (opts) {
}
}
});
// DNS-SD requires selected matching records to be put into additional records - rfc6763 12.1, 12.2
myRecords.slice(0).forEach(r => {
if ('PTR' === r.type) {
localRecords.forEach(l => {
if ((l.name === r.data) && (('SRV' === l.type) || ('TXT' === l.type)))
myRecords.push(l);
});
}
});
myRecords.slice(0).forEach(r => {
if ('SRV' === r.type) {
localRecords.forEach(l => {
if ((l.name === r.target) && (('A' === l.type) || ('AAAA' === l.type)))
myRecords.push(l);
});
}
});
process.nextTick(function () {
cb(null, myRecords);
});

View File

@ -32,11 +32,12 @@ module.exports.create = function (cli, dnsd) {
//console.log('[DEBUG] got a UDP message', nb.length);
//console.log(nb.toString('hex'));
dnsd.onMessage(nb, function (err, newAb, dbgmsg) {
dnsd.onMessage(nb, function (err, newAb, dbgmsg, unicastResponse) {
// 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);
const address = cli.mdns && !unicastResponse ? '224.0.0.251' : rinfo.address;
if (err) { server.send(Buffer.from([0x00]), rinfo.port, address); return; }
server.send(newAb, rinfo.port, address, function () {
console.log('[dnsd.onMessage] ' + dbgmsg, rinfo.port, address);
});
});
};
@ -46,13 +47,17 @@ module.exports.create = function (cli, dnsd) {
var server = this;
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 + ')');
if (cli.mdns) {
// special startup case to advertise local dns-sd records
handlers.onMessage('_services._dns-sd._udp.local', { address: '224.0.0.251', port: 5353 });
}
};
server.on('error', handlers.onError);