changed examples to use `dns-suite` instead of `dns-js`

This commit is contained in:
tigerbot 2017-05-22 14:14:18 -06:00
parent 8fc78f35f0
commit 53cae83af4
6 changed files with 58 additions and 42 deletions

View File

@ -37,6 +37,11 @@ var dnspack = exports.DNS_PACKER = {
if (!r.className) { if (!r.className) {
throw new Error("no 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) { if (!r.type) {
@ -44,7 +49,9 @@ var dnspack = exports.DNS_PACKER = {
throw new Error("no typeName"); throw new Error("no typeName");
} }
if (!types[r.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];
} }
} }
} }

View File

@ -1,20 +1,44 @@
'use strict'; 'use strict';
module.exports.respond = function (socket, packets, rinfo) { module.exports.respond = function (socket, packets, rinfo) {
var dns = require('dns-js'); var dns = require('../');
var os = require('os'); var os = require('os');
var queryname = '_cloud._tcp.local'; var queryname = '_cloud._tcp.local';
console.log(packets); console.log(packets);
packets.forEach(function (packet) { 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) { packet.question.forEach(function (q) {
if (queryname !== q.name) { if (queryname !== q.name) {
return; return;
} }
console.log('question', q.name, q.typeName, q.className, q.flag, q); 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 ifaces = os.networkInterfaces();
//var llRe = /^(fe80|169)/i; // link-local //var llRe = /^(fe80|169)/i; // link-local
@ -29,9 +53,9 @@ module.exports.respond = function (socket, packets, rinfo) {
iface.forEach(function (pface) { iface.forEach(function (pface) {
rpacket.additional.push({ rpacket.additional.push({
name: q.name name: q.name
, type: ('IPv4' === pface.family ? dns.DNSRecord.Type.A : dns.DNSRecord.Type.AAAA) , typeName: ('IPv4' === pface.family ? 'A' : 'AAAA')
, ttl: 10 , ttl: 10
, class: dns.DNSRecord.Class.IN , className: 'IN'
, address: pface.address // '_workstation._tcp.local' , address: pface.address // '_workstation._tcp.local'
}); });
}); });
@ -40,43 +64,33 @@ module.exports.respond = function (socket, packets, rinfo) {
var myRndId = 'be1af7a'; var myRndId = 'be1af7a';
rpacket.answer.push({ rpacket.answer.push({
name: q.name name: q.name
, type: dns.DNSRecord.Type.PTR , typeName: 'PTR'
, ttl: 10 , ttl: 10
, class: dns.DNSRecord.Class.IN , className: 'IN'
, data: myRndId + '.' + queryname , 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({ rpacket.additional.push({
name: myRndId + '.' + queryname name: myRndId + '.' + queryname
, type: dns.DNSRecord.Type.SRV , typeName: 'SRV'
, ttl: 10 , ttl: 10
, class: dns.DNSRecord.Class.IN , className: 'IN'
, priority: 0 , priority: 1
, weight: 0 , weight: 0
, port: 443 , port: 443
, target: myRndId + ".local" , target: myRndId + ".local"
}); });
rpacket.additional.push({ rpacket.additional.push({
name: myRndId + '.' + '_device-info._tcp.local' name: myRndId + '.' + '_device-info._tcp.local'
, type: dns.DNSRecord.Type.TXT , typeName: 'TXT'
, ttl: 10 , ttl: 10
, class: dns.DNSRecord.Class.IN , className: 'IN'
, data: ["model=CloudHome1,1", "dappsvers=1"] , 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('');
console.log('START JSON PACKET'); console.log('START JSON PACKET');
console.log(rpacket); console.log(rpacket);
var buf = dns.DNSPacket.toBuffer(rpacket); var buf = dns.DNSPacket.write(rpacket);
console.log(buf.toString('hex')); console.log(buf.toString('hex'));
console.log('END JSON PACKET'); console.log('END JSON PACKET');
console.log(''); console.log('');
@ -89,8 +103,7 @@ module.exports.respond = function (socket, packets, rinfo) {
console.log(''); console.log('');
socket.send(buf, rinfo.port, rinfo.address); socket.send(buf, rinfo.port, rinfo.address);
}); });
/*
*/
packet.answer.forEach(function (a) { packet.answer.forEach(function (a) {
console.log('answer', a.name, a.typeName, a.className, a.flag, a); console.log('answer', a.name, a.typeName, a.className, a.flag, a);
}); });

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var dgram = require('dgram'); var dgram = require('dgram');
var dnsjs = require('dns-js');
// SO_REUSEADDR and SO_REUSEPORT are set because // SO_REUSEADDR and SO_REUSEPORT are set because
// the system mDNS Responder may already be listening on this port // the system mDNS Responder may already be listening on this port
@ -22,4 +21,4 @@ socket.bind(port, function () {
socket.addMembership(broadcast); socket.addMembership(broadcast);
// ... more stuff // ... more stuff
}); });

View File

@ -5,7 +5,7 @@ var socket = dgram.createSocket({
type: 'udp4' type: 'udp4'
, reuseAddr: true , reuseAddr: true
}); });
var dns = require('dns-js'); var dns = require('../');
//var DNSPacket = dns.DNSPacket; //var DNSPacket = dns.DNSPacket;
var broadcast = '224.0.0.251'; // mdns var broadcast = '224.0.0.251'; // mdns
@ -31,17 +31,14 @@ return binString;
} }
socket.on('message', function (message, rinfo) { socket.on('message', function (message, rinfo) {
console.log('Received %d bytes from %s:%d\n', console.log('Received %d bytes from %s:%d', message.length, rinfo.address, rinfo.port);
message.length, rinfo.address, rinfo.port);
//console.log(msg.toString('utf8')); //console.log(msg.toString('utf8'));
message.forEach(function(byte){ 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('hex'));
// console.log(message.toString('ascii')); // console.log(message.toString('ascii'));
var packets; var packets;

View File

@ -3,7 +3,7 @@
// SRV RDATA contains: // SRV RDATA contains:
// Priority: The relative priority of this service. 16-bit (range 0-65535) // 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) // (range 0-65535)
// Port: Port number assigned to the symbolic service. 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. // 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 port is: " + record.port);
// console.log("record target is: " + record.target); // console.log("record target is: " + record.target);
// console.log("total length currently is: " + total); // console.log("total length currently is: " + total);
var srvLen = 6; // 16-bit priority, weight and port = 6 Bytes var srvLen = 6; // 16-bit priority, weight and port = 6 Bytes
var rdLenIndex = total; var rdLenIndex = total;
@ -62,7 +62,7 @@ exports.DNS_PACKER_TYPE_SRV = function (ab, dv, total, record) {
// RDLENGTH // RDLENGTH
dv.setUint16(rdLenIndex, srvLen, false); dv.setUint16(rdLenIndex, srvLen, false);
return total; return total;
}; };

View File

@ -24,12 +24,12 @@ exports.DNS_PARSER_TYPE_SOA = function (ab, packet, record) {
// Primary NS // Primary NS
record.name_server = unpackLabels(new Uint8Array(ab), record.rdstart, { byteLength: 0, cpcount: 0, labels: [], name: '' }).name; 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 there exists compression pointers in the rdata
if (cpcount > 0){ if (cpcount > 0){
// do something awesome with compression pointers to get the email address // do something awesome with compression pointers to get the email address
// I need the length of all the data before the email address starts. // 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. // 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 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){ 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; 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 } // if there are no compression pointers, we can get the email address directly from the offset
else { else {
@ -57,7 +57,7 @@ exports.DNS_PARSER_TYPE_SOA = function (ab, packet, record) {
record.ex = dv.getUint32(dv.byteLength - 8, false); record.ex = dv.getUint32(dv.byteLength - 8, false);
// Minimum TTL // Minimum TTL
record.nx = dv.getUint32(dv.byteLength - 4, false); record.nx = dv.getUint32(dv.byteLength - 4, false);
return record; return record;