From 53cae83af4c13ca9be469ed46b11e7b1796671f9 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Mon, 22 May 2017 14:14:18 -0600 Subject: [PATCH] changed examples to use `dns-suite` instead of `dns-js` --- dns.packer.js | 9 +++++- examples/cloud-respond.js | 63 +++++++++++++++++++++++---------------- examples/dns_test.js | 3 +- examples/listen.js | 11 +++---- packer/type.srv.js | 6 ++-- parser/type.soa.js | 8 ++--- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/dns.packer.js b/dns.packer.js index 594283c..61f876a 100644 --- a/dns.packer.js +++ b/dns.packer.js @@ -37,6 +37,11 @@ var dnspack = exports.DNS_PACKER = { if (!r.className) { throw new Error("no className"); } + if (!classes[r.className]) { + console.warn("ignoring invalid class '" + r.className + "' for '" + r.name); + } else { + r.class = classes[r.className]; + } } if (!r.type) { @@ -44,7 +49,9 @@ var dnspack = exports.DNS_PACKER = { throw new Error("no typeName"); } if (!types[r.typeName]) { - console.warn("ignoring invalid type '" + r.type + "' for '" + r.name + "', ignoring"); + console.warn("ignoring invalid type '" + r.typeName + "' for '" + r.name); + } else { + r.type = types[r.typeName]; } } } diff --git a/examples/cloud-respond.js b/examples/cloud-respond.js index 97d440d..7fe5957 100644 --- a/examples/cloud-respond.js +++ b/examples/cloud-respond.js @@ -1,20 +1,44 @@ 'use strict'; module.exports.respond = function (socket, packets, rinfo) { - var dns = require('dns-js'); + var dns = require('../'); var os = require('os'); var queryname = '_cloud._tcp.local'; console.log(packets); packets.forEach(function (packet) { + // Only respond to queries, otherwise we'll end up responding to ourselves forever. + if (packet.header.qr !== 0) { + return; + } + packet.question.forEach(function (q) { if (queryname !== q.name) { return; } console.log('question', q.name, q.typeName, q.className, q.flag, q); - var rpacket = new dns.DNSPacket(); + var rpacket = { + header: { + id: packet.header.id + , qr: 1 + , opcode: 0 + , aa: 1 + , tc: 0 + , rd: 0 + , ra: 0 + , res1: 0 + , res2: 0 + , res3: 0 + , rcode: 0 + , } + , question: [q] + , answer: [] + , authority: [] + , additional: [] + , edns_options: [] + }; var ifaces = os.networkInterfaces(); //var llRe = /^(fe80|169)/i; // link-local @@ -29,9 +53,9 @@ module.exports.respond = function (socket, packets, rinfo) { iface.forEach(function (pface) { rpacket.additional.push({ name: q.name - , type: ('IPv4' === pface.family ? dns.DNSRecord.Type.A : dns.DNSRecord.Type.AAAA) + , typeName: ('IPv4' === pface.family ? 'A' : 'AAAA') , ttl: 10 - , class: dns.DNSRecord.Class.IN + , className: 'IN' , address: pface.address // '_workstation._tcp.local' }); }); @@ -40,43 +64,33 @@ module.exports.respond = function (socket, packets, rinfo) { var myRndId = 'be1af7a'; rpacket.answer.push({ name: q.name - , type: dns.DNSRecord.Type.PTR + , typeName: 'PTR' , ttl: 10 - , class: dns.DNSRecord.Class.IN + , className: 'IN' , data: myRndId + '.' + queryname }); - rpacket.question.push(new dns.DNSRecord( - queryname // Name - , dns.DNSRecord.Type.PTR // Type - , dns.DNSRecord.Class.IN // Class - //, null // TTL - )); rpacket.additional.push({ name: myRndId + '.' + queryname - , type: dns.DNSRecord.Type.SRV + , typeName: 'SRV' , ttl: 10 - , class: dns.DNSRecord.Class.IN - , priority: 0 + , className: 'IN' + , priority: 1 , weight: 0 , port: 443 - , target: myRndId + ".local" + , target: myRndId + ".local" }); rpacket.additional.push({ name: myRndId + '.' + '_device-info._tcp.local' - , type: dns.DNSRecord.Type.TXT + , typeName: 'TXT' , ttl: 10 - , class: dns.DNSRecord.Class.IN + , className: 'IN' , data: ["model=CloudHome1,1", "dappsvers=1"] }); - rpacket.header.id = packet.header.id; - rpacket.header.aa = 1; - rpacket.header.qr = 1; - rpacket.header.rd = 0; console.log(''); console.log('START JSON PACKET'); console.log(rpacket); - var buf = dns.DNSPacket.toBuffer(rpacket); + var buf = dns.DNSPacket.write(rpacket); console.log(buf.toString('hex')); console.log('END JSON PACKET'); console.log(''); @@ -89,8 +103,7 @@ module.exports.respond = function (socket, packets, rinfo) { console.log(''); socket.send(buf, rinfo.port, rinfo.address); }); - /* - */ + packet.answer.forEach(function (a) { console.log('answer', a.name, a.typeName, a.className, a.flag, a); }); diff --git a/examples/dns_test.js b/examples/dns_test.js index acba7a9..6efa30c 100644 --- a/examples/dns_test.js +++ b/examples/dns_test.js @@ -1,7 +1,6 @@ 'use strict'; var dgram = require('dgram'); -var dnsjs = require('dns-js'); // SO_REUSEADDR and SO_REUSEPORT are set because // the system mDNS Responder may already be listening on this port @@ -22,4 +21,4 @@ socket.bind(port, function () { socket.addMembership(broadcast); // ... more stuff -}); \ No newline at end of file +}); diff --git a/examples/listen.js b/examples/listen.js index cc12267..a3c8821 100644 --- a/examples/listen.js +++ b/examples/listen.js @@ -5,7 +5,7 @@ var socket = dgram.createSocket({ type: 'udp4' , reuseAddr: true }); -var dns = require('dns-js'); +var dns = require('../'); //var DNSPacket = dns.DNSPacket; var broadcast = '224.0.0.251'; // mdns @@ -31,17 +31,14 @@ return binString; } socket.on('message', function (message, rinfo) { - console.log('Received %d bytes from %s:%d\n', - message.length, rinfo.address, rinfo.port); + console.log('Received %d bytes from %s:%d', message.length, rinfo.address, rinfo.port); //console.log(msg.toString('utf8')); message.forEach(function(byte){ - - console.log(pad(byte.toString(2), 8,'0')); - + console.log(pad(byte.toString(2), 8, '0')); }); - + // console.log(message.toString('hex')); // console.log(message.toString('ascii')); var packets; diff --git a/packer/type.srv.js b/packer/type.srv.js index dee2731..063576f 100644 --- a/packer/type.srv.js +++ b/packer/type.srv.js @@ -3,7 +3,7 @@ // SRV RDATA contains: // Priority: The relative priority of this service. 16-bit (range 0-65535) -// Weight: Used when more than one serivice has the same priority. 16-bit +// Weight: Used when more than one serivice has the same priority. 16-bit // (range 0-65535) // Port: Port number assigned to the symbolic service. 16-bit (range 0-65535) // Target: The name of the host that will provide service. @@ -31,7 +31,7 @@ exports.DNS_PACKER_TYPE_SRV = function (ab, dv, total, record) { // console.log("record port is: " + record.port); // console.log("record target is: " + record.target); // console.log("total length currently is: " + total); - + var srvLen = 6; // 16-bit priority, weight and port = 6 Bytes var rdLenIndex = total; @@ -62,7 +62,7 @@ exports.DNS_PACKER_TYPE_SRV = function (ab, dv, total, record) { // RDLENGTH dv.setUint16(rdLenIndex, srvLen, false); - + return total; }; diff --git a/parser/type.soa.js b/parser/type.soa.js index 534197a..2ca64ff 100644 --- a/parser/type.soa.js +++ b/parser/type.soa.js @@ -24,12 +24,12 @@ exports.DNS_PARSER_TYPE_SOA = function (ab, packet, record) { // Primary NS record.name_server = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; - + // if there exists compression pointers in the rdata if (cpcount > 0){ // do something awesome with compression pointers to get the email address // I need the length of all the data before the email address starts. - // if there are compression pointers then there will be a byte to indicate the length of each label, the label, + // if there are compression pointers then there will be a byte to indicate the length of each label, the label, // then there will be a compression pointer to grab the longest label. var start = 2; // start or email_addr. take into account compression pointer and address length @@ -41,7 +41,7 @@ exports.DNS_PARSER_TYPE_SOA = function (ab, packet, record) { if(parseInt(dv.getUint8(start - 2), 10) === 192){ record.email_addr = unpackLabels(new Uint8Array(ab), record.rdstart + start ,{ byteLength: 0, cpcount: 0, labels: [], name: '' }).name; } - } + } } // if there are no compression pointers, we can get the email address directly from the offset else { @@ -57,7 +57,7 @@ exports.DNS_PARSER_TYPE_SOA = function (ab, packet, record) { record.ex = dv.getUint32(dv.byteLength - 8, false); // Minimum TTL record.nx = dv.getUint32(dv.byteLength - 4, false); - + return record;