dns-suite.js/bin/mdns-capture.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-01-21 01:15:16 +00:00
'use strict';
// pass a terminal arg
2017-01-21 10:16:08 +00:00
var type = process.argv[2];
2017-01-21 01:15:16 +00:00
var count = parseInt(process.argv[3]) || 0;
if (!type) {
console.error("Usage: node aj-listener.js <type> [count]");
console.error("Example: node aj-listener.js _service 0");
process.exit(1);
}
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
var dgram = require('dgram');
var server = dgram.createSocket({
type: 'udp4'
, reuseAddr: true
});
var handlers = {};
handlers.onError = function (err) {
console.error("error:", err.stack);
server.close();
};
handlers.onMessage = function (buffer) {
2017-01-21 21:44:37 +00:00
var path = require('path');
2017-01-21 22:07:10 +00:00
var filename = type + '-' + count + '.mdns.bin';
var fullpath = path.join('samples', filename);
2017-01-21 10:16:08 +00:00
count += 1;
2017-01-21 01:15:16 +00:00
2017-01-21 22:07:10 +00:00
fs.writeFileAsync(fullpath, buffer).then(function () {
console.log('wrote ' + buffer.length + ' bytes to ' + fullpath);
2017-01-21 01:15:16 +00:00
});
};
handlers.onListening = function () {
/*jshint validthis:true*/
var server = this;
console.log(server.address());
2017-01-27 02:18:46 +00:00
2017-01-21 01:15:16 +00:00
server.setBroadcast(true);
server.addMembership('224.0.0.251');
};
server.on('error', handlers.onError);
server.on('message', handlers.onMessage);
server.on('listening', handlers.onListening);
// 53 dns
// 5353 mdns
2017-01-21 21:44:37 +00:00
server.bind(5353);