Updates for improved mDNS support and DNS-SD
This commit is contained in:
		
							parent
							
								
									1517ba0a4b
								
							
						
					
					
						commit
						c8583c96fb
					
				
							
								
								
									
										141
									
								
								bin/digd.js
									
									
									
									
									
								
							
							
						
						
									
										141
									
								
								bin/digd.js
									
									
									
									
									
								
							@ -31,6 +31,7 @@ cli.parse({
 | 
				
			|||||||
, 'address': [ false, 'ip address(es) to listen on (defaults to 0.0.0.0,::0)', 'string' ]
 | 
					, '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' ]
 | 
					, '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' ]
 | 
					, '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' ]
 | 
					//, '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' ]
 | 
					//, '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' ]
 | 
					//, '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)) {
 | 
					    if (!('timeout' in cli)) {
 | 
				
			||||||
      cli.timeout = 3000;
 | 
					      cli.timeout = 3000;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    cli.norecurse = true;
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    if (!cli.port) {
 | 
					    if (!cli.port) {
 | 
				
			||||||
      cli.port = cli.p = 53;
 | 
					      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 engine;
 | 
				
			||||||
  var path = require('path');
 | 
					  var path = require('path');
 | 
				
			||||||
  var engineOpts = { filepath: path.resolve(cli.input) };
 | 
					  var engineOpts = { filepath: path.resolve(cli.input) };
 | 
				
			||||||
  var dnsd = {};
 | 
					  var dnsd = {};
 | 
				
			||||||
  dnsd.onMessage = function (nb, cb) {
 | 
					  dnsd.onMessage = function (nb, cb) {
 | 
				
			||||||
    var byteOffset = nb._dnsByteOffset || nb.byteOffset;
 | 
					 | 
				
			||||||
    var queryAb = nb.buffer.slice(byteOffset, byteOffset + nb.byteLength);
 | 
					 | 
				
			||||||
    var query;
 | 
					    var query;
 | 
				
			||||||
    var count;
 | 
					    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 {
 | 
					      try {
 | 
				
			||||||
        hexdump = require('hexdump.js').hexdump;
 | 
					        query = dnsjs.DNSPacket.parse(queryAb);
 | 
				
			||||||
        console.error(hexdump(queryAb));
 | 
					 | 
				
			||||||
        console.error('');
 | 
					 | 
				
			||||||
      } catch(e) {
 | 
					      } 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;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -154,19 +181,20 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
      printer(q);
 | 
					      printer(q);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (query.answer.length) {
 | 
					    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:');
 | 
					      console.log(';; ANSWER SECTION:');
 | 
				
			||||||
      query.answer.forEach(print);
 | 
					      query.answer.forEach(print);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (query.authority.length) {
 | 
					    if (query.authority.length) {
 | 
				
			||||||
      console.log('');
 | 
					      console.log('');
 | 
				
			||||||
      console.error('[ERROR] Query contains an authority section:');
 | 
					      console.log('[ERROR] Query contains an authority section:');
 | 
				
			||||||
      console.log(';; AUTHORITY SECTION:');
 | 
					      console.log(';; AUTHORITY SECTION:');
 | 
				
			||||||
      query.authority.forEach(print);
 | 
					      query.authority.forEach(print);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (query.additional.length) {
 | 
					    if (query.additional.length) {
 | 
				
			||||||
      console.log('');
 | 
					      console.log('');
 | 
				
			||||||
      console.error('[ERROR] Query contains an additional section:');
 | 
					      console.log('[ERROR] Query contains an additional section:');
 | 
				
			||||||
      console.log(';; ADDITIONAL SECTION:');
 | 
					      console.log(';; ADDITIONAL SECTION:');
 | 
				
			||||||
      query.additional.forEach(print);
 | 
					      query.additional.forEach(print);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -179,6 +207,7 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
      common.writeQuery(cli, query, queryAb);
 | 
					      common.writeQuery(cli, query, queryAb);
 | 
				
			||||||
      //common.writeResponse(opts, query, nb, packet);
 | 
					      //common.writeResponse(opts, query, nb, packet);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    sendMsg({query: query});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function sendEmptyResponse(query, rcode) {
 | 
					    function sendEmptyResponse(query, rcode) {
 | 
				
			||||||
      // rcode
 | 
					      // rcode
 | 
				
			||||||
@ -189,7 +218,7 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
      var newAb;
 | 
					      var newAb;
 | 
				
			||||||
      var emptyResp = {
 | 
					      var emptyResp = {
 | 
				
			||||||
        header: {
 | 
					        header: {
 | 
				
			||||||
          id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
 | 
					          id: query.header.id || require('crypto').randomBytes(2).readUInt16BE(0)
 | 
				
			||||||
        , qr: 1
 | 
					        , qr: 1
 | 
				
			||||||
        , opcode: 0
 | 
					        , opcode: 0
 | 
				
			||||||
        , aa: 0     // TODO it may be authoritative
 | 
					        , aa: 0     // TODO it may be authoritative
 | 
				
			||||||
@ -213,22 +242,44 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      try {
 | 
					      if (!cli.mdns) { // no empty response for mDNS
 | 
				
			||||||
        newAb = dnsjs.DNSPacket.write(emptyResp);
 | 
					        try {
 | 
				
			||||||
      } catch(e) {
 | 
					          newAb = dnsjs.DNSPacket.write(emptyResp);
 | 
				
			||||||
        console.error("Could not write empty DNS response");
 | 
					        } catch(e) {
 | 
				
			||||||
        console.error(e);
 | 
					          console.error("Could not write empty DNS response");
 | 
				
			||||||
        console.error(emptyResp);
 | 
					          console.error(e);
 | 
				
			||||||
        cb(e, null, '[DEV] response sent (empty)');
 | 
					          console.error(emptyResp);
 | 
				
			||||||
        return;
 | 
					          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) {
 | 
					    function sendResponse(newPacket) {
 | 
				
			||||||
 | 
					      if (cli.mdns && !query.question[0].unicastResponse) {
 | 
				
			||||||
 | 
					        newPacket.question = [];
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      var newAb;
 | 
					      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 {
 | 
					      try {
 | 
				
			||||||
        newAb = dnsjs.DNSPacket.write(newPacket);
 | 
					        newAb = dnsjs.DNSPacket.write(newPacket);
 | 
				
			||||||
      } catch(e) {
 | 
					      } catch(e) {
 | 
				
			||||||
@ -239,7 +290,26 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
        return;
 | 
					        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() {
 | 
					    function recurse() {
 | 
				
			||||||
@ -259,7 +329,7 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      var newResponse = {
 | 
					      var newResponse = {
 | 
				
			||||||
        header: {
 | 
					        header: {
 | 
				
			||||||
          id: query.header.id // require('crypto').randomBytes(2).readUInt16BE(0)
 | 
					          id: query.header.id || require('crypto').randomBytes(2).readUInt16BE(0)
 | 
				
			||||||
        , qr: 0
 | 
					        , qr: 0
 | 
				
			||||||
        , opcode: 0
 | 
					        , opcode: 0
 | 
				
			||||||
        , aa: 0 // query.header.aa ? 1 : 0 // NA? not sure what this would do
 | 
					        , 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');
 | 
					            cb(null, newAb, '[DEV] response sent');
 | 
				
			||||||
 | 
					            sendMsg({recurse: newResponse});
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -306,6 +377,7 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
          }
 | 
					          }
 | 
				
			||||||
        , onMessage: function (packet) {
 | 
					        , onMessage: function (packet) {
 | 
				
			||||||
            // yay! recursion was available after all!
 | 
					            // yay! recursion was available after all!
 | 
				
			||||||
 | 
					            newResponse.header.qr = 1;
 | 
				
			||||||
            newResponse.header.ra = 1;
 | 
					            newResponse.header.ra = 1;
 | 
				
			||||||
            newResponse.header.rcode = NOERROR;
 | 
					            newResponse.header.rcode = NOERROR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -392,7 +464,16 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
      respondWithResults(e);
 | 
					      respondWithResults(e);
 | 
				
			||||||
      return;
 | 
					      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;
 | 
					  cli.defaultNameservers = defaultNameservers;
 | 
				
			||||||
@ -407,6 +488,8 @@ cli.main(function (args, cli) {
 | 
				
			|||||||
        console.log('index, defaultNameservers', index, cli.defaultNameservers);
 | 
					        console.log('index, defaultNameservers', index, cli.defaultNameservers);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    sendMsg({ready: 'digd.js v' + pkg.version});
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
  if (cli.tcp /* TODO v1.3 !cli.notcp */) {
 | 
					  if (cli.tcp /* TODO v1.3 !cli.notcp */) {
 | 
				
			||||||
    require('../lib/tcpd.js').create(cli, dnsd);
 | 
					    require('../lib/tcpd.js').create(cli, dnsd);
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										29
									
								
								lib/digd.js
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								lib/digd.js
									
									
									
									
									
								
							@ -7,6 +7,7 @@ module.exports.ask = function (query, cb) {
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var NOERROR = 0;
 | 
					var NOERROR = 0;
 | 
				
			||||||
 | 
					var SERVFAIL = 2;
 | 
				
			||||||
var NXDOMAIN = 3;
 | 
					var NXDOMAIN = 3;
 | 
				
			||||||
var REFUSED = 5;
 | 
					var REFUSED = 5;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -242,7 +243,7 @@ module.exports.query = function (engine, query, cb) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  var results = {
 | 
					  var results = {
 | 
				
			||||||
    header: {
 | 
					    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
 | 
					    , qr: 1
 | 
				
			||||||
    , opcode: 0             // pretty much always 0 QUERY
 | 
					    , opcode: 0             // pretty much always 0 QUERY
 | 
				
			||||||
    , aa: 1                 // TODO right now we assume that if we have the record, we're authoritative
 | 
					    , 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);
 | 
					      console.log('[SOA] looking for', qnames, 'and proudly serving', err, myDomains);
 | 
				
			||||||
      if (err) { cb(err); return; }
 | 
					      if (err) { cb(err); return; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // this should result in a REFUSED status
 | 
					      // this should result in a SERVFAIL status
 | 
				
			||||||
      if (!myDomains.length) {
 | 
					      if (!myDomains.length) {
 | 
				
			||||||
        // REFUSED will have no records, so we could still recursion, if enabled
 | 
					        // SERVFAIL will have no records, so we could still recursion, if enabled
 | 
				
			||||||
        results.header.rcode = REFUSED;
 | 
					        results.header.rcode = SERVFAIL;
 | 
				
			||||||
        cb(null, results);
 | 
					        cb(null, results);
 | 
				
			||||||
        return;
 | 
					        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);
 | 
					        hasA = hasA || ('A' === r.type || 'A' === r.typeName || 'AAAA' === r.type || 'AAAA' === r.typeName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return passCnames || ((r.type && r.type === query.question[0].type)
 | 
					        const isDNS_SD_Record = ('SRV' === r.type) || ('TXT' === r.type);
 | 
				
			||||||
          || (r.type && r.type === query.question[0].typeName)
 | 
					
 | 
				
			||||||
          || (r.typeName && r.typeName === query.question[0].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) ||
 | 
				
			||||||
 | 
					          (('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) {
 | 
					    if (myRecords.length) {
 | 
				
			||||||
      myRecords.forEach(function (r) {
 | 
					      myRecords.forEach(function (r) {
 | 
				
			||||||
        results.answer.push(dbToResourceRecord(r));
 | 
					        results.answer.push(dbToResourceRecord(r));
 | 
				
			||||||
 | 
				
			|||||||
@ -369,7 +369,8 @@ module.exports.create = function (opts) {
 | 
				
			|||||||
      });
 | 
					      });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  , get: function (query, cb) {
 | 
					  , 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) {
 | 
					        if ('string' !== typeof r.name) {
 | 
				
			||||||
          return false;
 | 
					          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 () {
 | 
					      process.nextTick(function () {
 | 
				
			||||||
        cb(null, myRecords);
 | 
					        cb(null, myRecords);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										15
									
								
								lib/udpd.js
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								lib/udpd.js
									
									
									
									
									
								
							@ -32,11 +32,12 @@ module.exports.create = function (cli, dnsd) {
 | 
				
			|||||||
      //console.log('[DEBUG] got a UDP message', nb.length);
 | 
					      //console.log('[DEBUG] got a UDP message', nb.length);
 | 
				
			||||||
      //console.log(nb.toString('hex'));
 | 
					      //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
 | 
					        // TODO send legit error message
 | 
				
			||||||
        if (err) { server.send(Buffer.from([0x00]), rinfo.port, rinfo.address); return; }
 | 
					        const address = cli.mdns && !unicastResponse ? '224.0.0.251' : rinfo.address;
 | 
				
			||||||
        server.send(newAb, rinfo.port, rinfo.address, function () {
 | 
					        if (err) { server.send(Buffer.from([0x00]), rinfo.port, address); return; }
 | 
				
			||||||
          console.log('[dnsd.onMessage] ' + dbgmsg, rinfo.port, rinfo.address);
 | 
					        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;
 | 
					      var server = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (cli.mdns || '224.0.0.251' === cli.nameserver) {
 | 
					      if (cli.mdns || '224.0.0.251' === cli.nameserver) {
 | 
				
			||||||
        server.setBroadcast(true);
 | 
					 | 
				
			||||||
        server.addMembership(cli.nameserver);
 | 
					        server.addMembership(cli.nameserver);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.log('');
 | 
					      console.log('');
 | 
				
			||||||
      console.log('Bound and Listening:');
 | 
					      console.log('Bound and Listening:');
 | 
				
			||||||
      console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')');
 | 
					      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);
 | 
					    server.on('error', handlers.onError);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user