2017-01-14 02:04:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var dgram = require('dgram');
|
|
|
|
var socket = dgram.createSocket({
|
|
|
|
type: 'udp4'
|
|
|
|
, reuseAddr: true
|
|
|
|
});
|
|
|
|
var dns = require('dns-js');
|
|
|
|
//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-14 02:04:27 +00:00
|
|
|
socket.on('message', function (message, rinfo) {
|
|
|
|
console.log('Received %d bytes from %s:%d\n',
|
|
|
|
message.length, rinfo.address, rinfo.port);
|
|
|
|
//console.log(msg.toString('utf8'));
|
2017-01-14 19:58:35 +00:00
|
|
|
|
|
|
|
message.forEach(function(byte){
|
|
|
|
|
|
|
|
console.log(pad(byte.toString(2), 8,'0'));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('got here');
|
2017-01-14 02:04:27 +00:00
|
|
|
console.log(message.toString('hex'));
|
2017-01-14 19:58:35 +00:00
|
|
|
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);
|
|
|
|
});
|