multicast-dns
Advanced tools
Comparing version 6.1.1 to 6.2.0
65
cli.js
#!/usr/bin/env node | ||
var mdns = require('multicast-dns')() | ||
var mdns = require('./')() | ||
var path = require('path') | ||
var os = require('os') | ||
var announcing = process.argv.indexOf('--announce') > -1 | ||
if (process.argv.length < 3) { | ||
@@ -12,17 +15,51 @@ console.error('Usage: %s <hostname>', path.basename(process.argv[1])) | ||
mdns.on('response', function (response) { | ||
response.answers.forEach(function (answer) { | ||
if (answer.name === hostname) { | ||
console.log(answer.data) | ||
process.exit() | ||
} | ||
if (announcing) { | ||
var ip = getIp() | ||
mdns.on('query', function (query) { | ||
query.questions.forEach(function (q) { | ||
if (q.name === hostname) { | ||
console.log('Responding %s -> %s', q.name, ip) | ||
mdns.respond({ | ||
answers: [{ | ||
type: 'A', | ||
name: q.name, | ||
data: ip | ||
}] | ||
}) | ||
} | ||
}) | ||
}) | ||
}) | ||
} else { | ||
mdns.on('response', function (response) { | ||
response.answers.forEach(function (answer) { | ||
if (answer.name === hostname) { | ||
console.log(answer.data) | ||
process.exit() | ||
} | ||
}) | ||
}) | ||
mdns.query(hostname, 'A') | ||
mdns.query(hostname, 'A') | ||
// Give responses 3 seconds to respond | ||
setTimeout(function () { | ||
console.error('Hostname not found') | ||
process.exit(1) | ||
}, 3000) | ||
// Give responses 3 seconds to respond | ||
setTimeout(function () { | ||
console.error('Hostname not found') | ||
process.exit(1) | ||
}, 3000) | ||
} | ||
function getIp () { | ||
var networks = os.networkInterfaces() | ||
var found = '127.0.0.1' | ||
Object.keys(networks).forEach(function (k) { | ||
var n = networks[k] | ||
n.forEach(function (addr) { | ||
if (addr.family === 'IPv4' && !addr.internal) { | ||
found = addr.address | ||
} | ||
}) | ||
}) | ||
return found | ||
} |
46
index.js
@@ -5,2 +5,3 @@ var packet = require('dns-packet') | ||
var events = require('events') | ||
var os = require('os') | ||
@@ -53,7 +54,12 @@ var noop = function () {} | ||
if (opts.multicast !== false) { | ||
try { | ||
socket.addMembership(ip, opts.interface) | ||
} catch (err) { | ||
that.emit('error', err) | ||
var ifaces = opts.interface ? [].concat(opts.interface) : allInterfaces() | ||
for (var i = 0; i < ifaces.length; i++) { | ||
try { | ||
socket.addMembership(ip, ifaces[i]) | ||
} catch (err) { | ||
that.emit('error', err) | ||
} | ||
} | ||
socket.setMulticastTTL(opts.ttl || 255) | ||
@@ -82,8 +88,21 @@ socket.setMulticastLoopback(opts.loopback !== false) | ||
if (!rinfo) rinfo = me | ||
bind(function (err) { | ||
bind(onbind) | ||
function onbind (err) { | ||
if (destroyed) return cb() | ||
if (err) return cb(err) | ||
var message = packet.encode(value) | ||
socket.send(message, 0, message.length, rinfo.port, rinfo.address || rinfo.host, cb) | ||
}) | ||
socket.send(message, 0, message.length, rinfo.port, rinfo.address || rinfo.host, onsend) | ||
} | ||
function onsend (err) { | ||
if (err && err.code === 'ENETUNREACH' && rinfo.address !== '127.0.0.1') { | ||
rinfo = {port: me.port, address: '127.0.0.1'} | ||
onbind(null) | ||
return | ||
} | ||
cb(err) | ||
} | ||
} | ||
@@ -122,1 +141,14 @@ | ||
} | ||
function allInterfaces () { | ||
var networks = os.networkInterfaces() | ||
var res = [] | ||
Object.keys(networks).forEach(function (k) { | ||
networks[k].forEach(function (iface) { | ||
if (iface.family === 'IPv4') res.push(iface.address) | ||
}) | ||
}) | ||
return res | ||
} |
{ | ||
"name": "multicast-dns", | ||
"version": "6.1.1", | ||
"version": "6.2.0", | ||
"description": "Low level multicast-dns implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18237
370
8