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

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,14 +31,11 @@ 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'));
}); });