multicast-dns
Advanced tools
Comparing version 4.0.1 to 5.0.0
108
index.js
@@ -12,5 +12,6 @@ var packets = require('./packets') | ||
var that = new events.EventEmitter() | ||
var port = opts.port || 5353 | ||
var port = typeof opts.port === 'number' ? opts.port : 5353 | ||
var type = opts.type || 'udp4' | ||
var ip = opts.ip || opts.host || (type === 'udp4' ? '224.0.0.251' : null) | ||
var me = {address: ip, port: port} | ||
@@ -21,61 +22,58 @@ if (type === 'udp6' && (!ip || !opts.interface)) { | ||
var createSocket = function () { | ||
var socket = dgram.createSocket({ | ||
type: type, | ||
reuseAddr: opts.reuseAddr !== false, | ||
toString: function () { | ||
return type | ||
} | ||
}) | ||
var socket = dgram.createSocket({ | ||
type: type, | ||
reuseAddr: opts.reuseAddr !== false, | ||
toString: function () { | ||
return type | ||
} | ||
}) | ||
socket.on('error', function (err) { | ||
socket.on('error', function (err) { | ||
that.emit('warning', err) | ||
}) | ||
socket.on('message', function (message, rinfo) { | ||
try { | ||
message = packets.decode(message) | ||
} catch (err) { | ||
that.emit('warning', err) | ||
}) | ||
return | ||
} | ||
socket.on('message', function (message, rinfo) { | ||
try { | ||
message = packets.decode(message) | ||
} catch (err) { | ||
that.emit('warning', err) | ||
return | ||
} | ||
that.emit('packet', message, rinfo) | ||
that.emit('packet', message, rinfo) | ||
if (message.type === 'query') that.emit('query', message, rinfo) | ||
if (message.type === 'response') that.emit('response', message, rinfo) | ||
}) | ||
if (message.type === 'query') that.emit('query', message, rinfo) | ||
if (message.type === 'response') that.emit('response', message, rinfo) | ||
}) | ||
socket.on('listening', function () { | ||
if (!port) port = me.port = socket.address().port | ||
if (opts.multicast !== false) { | ||
socket.addMembership(ip, opts.interface) | ||
socket.setMulticastTTL(opts.ttl || 255) | ||
socket.setMulticastLoopback(opts.loopback !== false) | ||
} | ||
}) | ||
socket.on('listening', function () { | ||
if (opts.multicast !== false) { | ||
socket.addMembership(ip, opts.interface) | ||
socket.setMulticastTTL(opts.ttl || 255) | ||
socket.setMulticastLoopback(opts.loopback !== false) | ||
} | ||
var bind = thunky(function (cb) { | ||
socket.once('error', cb) | ||
socket.bind(port, function () { | ||
socket.removeListener('error', cb) | ||
cb(null) | ||
}) | ||
return socket | ||
} | ||
var receiveSocket = createSocket() | ||
var sendSocket = createSocket() | ||
var sendBind = thunky(function (cb) { | ||
sendSocket.on('error', cb) | ||
sendSocket.bind(0, function () { | ||
sendSocket.removeListener('error', cb) | ||
cb(null, sendSocket) | ||
}) | ||
}) | ||
receiveSocket.bind(port, function () { | ||
bind(function (err) { | ||
if (err) return that.emit('error', err) | ||
that.emit('ready') | ||
}) | ||
that.send = function (packet, cb) { | ||
that.send = function (packet, rinfo, cb) { | ||
if (typeof rinfo === 'function') return that.send(packet, null, rinfo) | ||
if (!cb) cb = noop | ||
sendBind(function (err, socket) { | ||
if (!rinfo) rinfo = me | ||
bind(function (err) { | ||
if (err) return cb(err) | ||
var message = packets.encode(packet) | ||
sendSocket.send(message, 0, message.length, port, ip, cb) | ||
socket.send(message, 0, message.length, rinfo.port, rinfo.address || rinfo.host, cb) | ||
}) | ||
@@ -85,12 +83,13 @@ } | ||
that.response = | ||
that.respond = function (res, cb) { | ||
if (!cb) cb = noop | ||
that.respond = function (res, rinfo, cb) { | ||
if (Array.isArray(res)) res = {answers: res} | ||
res.type = 'response' | ||
that.send(res, cb) | ||
that.send(res, rinfo, cb) | ||
} | ||
that.query = function (q, type, cb) { | ||
if (typeof type === 'function') return that.query(q, null, type) | ||
that.query = function (q, type, rinfo, cb) { | ||
if (typeof type === 'function') return that.query(q, null, null, type) | ||
if (typeof type === 'object' && type && type.port) return that.query(q, null, type, rinfo) | ||
if (typeof rinfo === 'function') return that.query(q, type, null, rinfo) | ||
if (!cb) cb = noop | ||
@@ -102,3 +101,3 @@ | ||
q.type = 'query' | ||
that.send(q, cb) | ||
that.send(q, rinfo, cb) | ||
} | ||
@@ -108,7 +107,4 @@ | ||
if (!cb) cb = noop | ||
sendSocket.once('close', function () { | ||
receiveSocket.once('close', cb) | ||
receiveSocket.close() | ||
}) | ||
sendSocket.close() | ||
socket.once('close', cb) | ||
socket.close() | ||
} | ||
@@ -115,0 +111,0 @@ |
{ | ||
"name": "multicast-dns", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "Low level multicast-dns implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -78,7 +78,6 @@ var types = require('./types') | ||
header.decode = function (buf, offset) { | ||
var flags = buf.readUInt16BE(offset + 2) | ||
header.decode.bytes = 12 | ||
return { | ||
type: flags & RESPONSE_FLAG ? 'response' : 'query', | ||
id: buf.readUInt16BE(offset), | ||
type: buf.readUInt16BE(offset + 2) & RESPONSE_FLAG ? 'response' : 'query', | ||
qdcount: buf.readUInt16BE(offset + 4), | ||
@@ -92,3 +91,3 @@ ancount: buf.readUInt16BE(offset + 6), | ||
header.encode = function (h, buf, offset) { | ||
buf.writeUInt16BE(0, offset) | ||
buf.writeUInt16BE(h.id || 0, offset) | ||
buf.writeUInt16BE(h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG, offset + 2) | ||
@@ -95,0 +94,0 @@ buf.writeUInt16BE(h.qdcount, offset + 4) |
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
28235
707