2017-01-14 02:04:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var dgram = require('dgram');
|
|
|
|
var socket = dgram.createSocket({
|
|
|
|
type: 'udp4'
|
|
|
|
, reuseAddr: true
|
|
|
|
});
|
2017-05-22 20:14:18 +00:00
|
|
|
var dns = require('../');
|
2017-01-14 02:04:27 +00:00
|
|
|
//var DNSPacket = dns.DNSPacket;
|
|
|
|
|
|
|
|
var broadcast = '224.0.0.251'; // mdns
|
|
|
|
var port = 5353; // mdns
|
|
|
|
|
2017-01-14 19:58:35 +00:00
|
|
|
// ex: pad('11111', 8, '0')
|
|
|
|
function pad(str, len, ch) {
|
|
|
|
|
|
|
|
while (str.length < len) {
|
|
|
|
str = ch + str;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-01-18 23:49:17 +00:00
|
|
|
function binaryAgent(str) {
|
|
|
|
|
|
|
|
var binString = '';
|
|
|
|
|
|
|
|
str.split(' ').map(function(bin) {
|
|
|
|
binString += String.fromCharCode(parseInt(bin, 2));
|
|
|
|
});
|
|
|
|
return binString;
|
|
|
|
}
|
|
|
|
|
2017-01-14 02:04:27 +00:00
|
|
|
socket.on('message', function (message, rinfo) {
|
2017-05-22 20:14:18 +00:00
|
|
|
console.log('Received %d bytes from %s:%d', message.length, rinfo.address, rinfo.port);
|
2017-01-14 02:04:27 +00:00
|
|
|
//console.log(msg.toString('utf8'));
|
2017-01-14 19:58:35 +00:00
|
|
|
|
|
|
|
message.forEach(function(byte){
|
2017-05-22 20:14:18 +00:00
|
|
|
console.log(pad(byte.toString(2), 8, '0'));
|
2017-01-14 19:58:35 +00:00
|
|
|
});
|
|
|
|
|
2017-05-22 20:14:18 +00:00
|
|
|
|
2017-01-18 23:49:17 +00:00
|
|
|
// console.log(message.toString('hex'));
|
|
|
|
// console.log(message.toString('ascii'));
|
2017-01-14 02:04:27 +00:00
|
|
|
var packets;
|
|
|
|
|
|
|
|
try {
|
|
|
|
packets = dns.DNSPacket.parse(message);
|
|
|
|
}
|
|
|
|
catch (er) {
|
|
|
|
//partial, skip it
|
|
|
|
console.error(er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(packets)) { packets = [packets]; }
|
|
|
|
|
|
|
|
require('./cloud-respond.js').respond(socket, packets, rinfo);
|
2017-01-14 19:58:35 +00:00
|
|
|
|
|
|
|
// console.log(packets);
|
|
|
|
// console.log('\n');
|
2017-01-14 02:04:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.bind(port, function () {
|
2017-01-14 19:58:35 +00:00
|
|
|
console.log('***********************************')
|
2017-01-14 02:04:27 +00:00
|
|
|
console.log('bound on', port);
|
2017-01-14 19:58:35 +00:00
|
|
|
console.log('***********************************')
|
2017-01-14 02:04:27 +00:00
|
|
|
console.log('bound on', this.address());
|
|
|
|
|
|
|
|
socket.setBroadcast(true);
|
|
|
|
socket.addMembership(broadcast);
|
|
|
|
|
|
|
|
// buf.forEach parseInt(byte.toString('hex'), 16).toString(2);
|
|
|
|
});
|