2013-07-10 12:02:48 +02:00
|
|
|
var dgram = require('dgram'),
|
|
|
|
EventEmitter = require('events').EventEmitter,
|
|
|
|
util = require('util'),
|
|
|
|
dns = require('dns');
|
|
|
|
|
|
|
|
var activeQueries = [];
|
|
|
|
|
|
|
|
var udpSocket = dgram.createSocket('udp4');
|
|
|
|
udpSocket.unref();
|
|
|
|
udpSocket.bind(21943);
|
|
|
|
udpSocket.on('message', function(buffer, rinfo) {
|
|
|
|
for(var i = 0; i < activeQueries.length; i++) {
|
|
|
|
var query = activeQueries[i];
|
2013-07-10 13:07:13 +02:00
|
|
|
if(query.options.address != rinfo.address) continue;
|
|
|
|
if(query.options.port != rinfo.port) continue;
|
2013-07-10 12:02:48 +02:00
|
|
|
query._udpResponse(buffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
2013-07-10 12:59:10 +02:00
|
|
|
query: function(options,callback) {
|
|
|
|
if(callback) options.callback = callback;
|
2013-07-10 12:02:48 +02:00
|
|
|
|
2013-07-10 12:59:10 +02:00
|
|
|
var type = (options.type || '').replace(/\W/g,'');
|
2013-07-10 12:02:48 +02:00
|
|
|
var protocol = require('./protocols/'+type);
|
2013-07-10 12:59:10 +02:00
|
|
|
|
2013-07-10 12:02:48 +02:00
|
|
|
var query = new protocol();
|
|
|
|
query.udpSocket = udpSocket;
|
|
|
|
query.type = type;
|
2013-07-10 12:59:10 +02:00
|
|
|
|
|
|
|
// copy over options
|
|
|
|
for(var i in options) query.options[i] = options[i];
|
|
|
|
|
2013-07-10 12:02:48 +02:00
|
|
|
activeQueries.push(query);
|
|
|
|
|
2013-07-10 12:06:44 +02:00
|
|
|
query.on('finished',function(state) {
|
2013-07-10 12:02:48 +02:00
|
|
|
var i = activeQueries.indexOf(query);
|
|
|
|
if(i >= 0) activeQueries.splice(i, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.nextTick(function() {
|
|
|
|
query.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|