Socket
Socket
Sign inDemoInstall

mdns-discovery

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mdns-discovery - npm Package Compare versions

Comparing version 0.0.5 to 0.1.1

.npmignore

166

mdns-discovery.js
"use strict";
var debug = require('debug')('mdns-discovery');
var dgram = require("dgram");

@@ -7,3 +8,4 @@ var packet = require('dns-packet');

var defaultOptions = {
name: 'm_d_n_s',
//name: 'm_d_n_s',
nsme: '',
port: 5353,

@@ -15,3 +17,3 @@ ip: "224.0.0.251",

q: {
name: '_amzn-wplay._tcp.local',
name: '',
type: 'PTR',

@@ -21,2 +23,4 @@ class: 0x8001

timeout: 4,
broadcast: true,
multicast: true,
multicastTTL: 64,

@@ -28,3 +32,13 @@ ttl: 64

function checkOptions(opts, def) {
function MulticastDNS(opts) {
if (this instanceof MulticastDNS === false) {
return new MulticastDNS(opts);
}
this.setOptions(opts);
this.clients = [];
this.found = [];
}
function checkOptions (opts, def) {
//opts = opts || {};

@@ -50,27 +64,2 @@ def = def || defaultOptions;

function MulticastDNS(opts) {
if (this instanceof MulticastDNS === false) {
return new MulticastDNS(opts);
}
this.setOptions(opts);
// opts.name = opts.name || 'm_d_n_s';
// opts.port = opts.port || 5353;
// opts.ip = opts.ip || "224.0.0.251";
// opts.reuseAddr = opts.reuseAddr !== false;
// opts.interfaces = opts.interfaces || [];
// opts.type = opts.type || 'udp4';
// opts.q = opts.q || {};
// opts.q.name = opts.q.name || '_amzn-wplay._tcp.local';
// opts.q.type = opts.q.type || dnsjs.DNSRecord.Type.PTR;
// opts.q.class = opts.q.class || 0x8001;
// if (opts.timeout === undefined) opts.timeout = 30;
// if (opts.multicastTTL === undefined) opts.multicastTTL = 64;
// if (opts.ttl === undefined) opts.ttl = 64;
this.options = opts;
this.clients = [];
this.found = [];
}
MulticastDNS.prototype.setOptions = function (opts) {

@@ -80,5 +69,9 @@ opts = opts || {};

opts.q.name = opts.q.name || opts.name;
var to = opts.timeout;
checkOptions(opts);
if (!opts.find && !to) opts.timeout = 0;
this.options = opts;
}
MulticastDNS.prototype.getInterfaces = function () {

@@ -94,2 +87,3 @@ if (this.options.interfaces && this.options.interfaces.length) return;

self.options.interfaces.push(alias.address);
debug("found interface: %s", alias.address);
}

@@ -99,34 +93,6 @@ }

};
MulticastDNS.prototype.browse = MulticastDNS.prototype.run;
MulticastDNS.prototype.getPayloadx = function () {
var payload = new Buffer(
[].concat([0x00, 0x00, // ID
0x00, 0x00, // fixed
0x00, 0x01, // QDCount: number of entries in the Question Section
0x00, 0x00,
0x00, 0x00,
0x00, 0x00
],
this.options.name.length,
this.options.name.split("").map(function (letter) {
return letter.charCodeAt(0);
}),
"local".length,
"local".split("").map(function (letter) {
return letter.charCodeAt(0);
}),
0x00, // NULL terminate
[0x00, 0x01, // QTYPE
0x80, 0x01 // QCLASS
// http://tools.ietf.org/html/draft-cheshire-dnsext-multicastdns-06#section-6.5
// Multicast DNS defines the top bit in the class field of a DNS question as the "unicast response" bit.
]
)
);
return payload;
};
MulticastDNS.prototype.getPayload = function () {
var message = packet.encode({

@@ -147,5 +113,7 @@ questions: [this.options.q]

};
MulticastDNS.prototype.onError = function (err) {
};
MulticastDNS.prototype.prepare = function (interfaceIp) {

@@ -155,5 +123,8 @@

client.on("message", this.onMessage.bind(this));
//client.on("message", this.onMessage.bind(this));
client.on("message", function(message, rinfo) {
this.onMessage(message, rinfo);
}.bind(this));
client.on("listening", function() {
console.log("listening on ",client.address());
debug("listening on ",client.address());

@@ -170,5 +141,7 @@ client.setBroadcast(this.options.broadcast !== false);

if (this.options.ttl) client.setTTL(this.options.ttl);
var payload = this.getPayload();
client.send(payload, 0, payload.length, this.options.port, this.options.ip, function(err, bytes) {
});
if (this.options.q.name) {
var payload = this.getPayload();
client.send(payload, 0, payload.length, this.options.port, this.options.ip, function (err, bytes) {
});
}
}.bind(this));

@@ -182,2 +155,3 @@ client.on("close", this.onClose.bind(this));

MulticastDNS.prototype.close = function () {

@@ -194,3 +168,9 @@ if (this.timer) {

MulticastDNS.prototype.run = function (readyCallback) {
MulticastDNS.prototype.run = function (timeout, readyCallback) {
if (typeof timeout === 'function') {
readyCallback = timeout;
timeout = undefined;
}
if (timeout === undefined) timeout = this.options.timeout;
this.getInterfaces();

@@ -200,8 +180,15 @@ this.options.interfaces.forEach(function(interfaceIp) {

}.bind(this));
if (this.options.timeout) this.timer = setTimeout(function() {
if (timeout) this.timer = setTimeout(function() {
this.close();
if (debug.enabled) {
this.found.foreEach(function(info) {
debug("found: %s - %s", info.ip, info.name);
});
}
readyCallback && readyCallback(this.found);
}.bind(this), this.options.timeout * 1000);
}.bind(this), timeout * 1000);
return this;
};
MulticastDNS.prototype.onPacket = function (packets, rinfo) {

@@ -213,3 +200,3 @@ //this.options.find = 'amzn.dmgr:';

if (a.name.indexOf(this.options.find) === 0) {
var found = this.found.find(function(v) { //xxxx
var found = this.found.find(function(v) {
return v.ip === rinfo.address;

@@ -224,4 +211,32 @@ });

}.bind(this));
return this;
};
MulticastDNS.prototype.on = function (name, fn) {
switch(name) {
case 'packet': this.__proto__.onPacket = fn; break;
case 'message': this.__proto__.onMessage = fn; break;
}
return this;
}
MulticastDNS.prototype.onIP = function (ip, fn) {
var args = arguments;
if (args.length < 2) return;
fn = args[args.length-1];
if (typeof fn !== 'function') return;
if (!this.onIPs) this.onIPs = [];
fn = fn.bind(this);
for (var i=0; i<args.length-1; i++) {
this.onIPs.push({
ip: args[i],
fn: fn
});
}
return this;
}
MulticastDNS.prototype.onMessage = function (message, rinfo) {

@@ -237,9 +252,14 @@

//var s = message.toString();
console.log(rinfo.address);
var m = packet.decode(message);
if (packets.answers) packets.answers.forEach(function(packet, i) {
//console.log('packet[%d]=%s type=%s class=%d, ttl=%d', i, packet.name, packet.type, packet.class, packet.ttl);
console.log(`packet[${i}]=${packet.name}, type=${packet.type}, class=${packet.class}, ttl=${packet.ttl}}`);
if (this.onIPs) {
var found = this.onIPs.find(function(entry) {
return (entry.ip === rinfo.address);
});
if (found) found.fn(packets, rinfo);
}
if (debug.enabled && packets.answers) packets.answers.forEach(function(packet, i) {
//debug('packet[%d]=%s type=%s class=%d, ttl=%d', i, packet.name, packet.type, packet.class, packet.ttl);
debug(`${rinfo.address} - packet[${i}]=${packet.name}, type=${packet.type}, class=${packet.class}, ttl=${packet.ttl}}`);
});
return this;
};

@@ -249,3 +269,1 @@

module.exports = MulticastDNS;
//new MulticastDNS().run();
{
"name": "mdns-discovery",
"version": "0.0.5",
"version": "0.1.1",
"description": "Multicast DNS",

@@ -20,10 +20,11 @@ "author": {

"broadcast",
"datagram",
"dgram",
"multicast",
"discovery"
"datagram",
"dgram",
"multicast",
"discovery"
],
"dependencies": {
"dgram": "^1.0.1",
"dns-packet": "^1.1.1"
"dns-packet": "^1.1.1",
"debug": "~2.2.0"
},

@@ -33,3 +34,3 @@ "devDependencies": {

"chai": "^3.4.1"
},
},
"scripts": {

@@ -46,3 +47,7 @@ "test": "node node_modules/mocha/bin/mocha"

"main": "mdns-discovery.js",
"readmeFilename": "readme.md"
"readmeFilename": "readme.md",
"readme": "###mnds Multicast DNS\r\n<!--\r\n[![NPM version](http://img.shields.io/npm/v/soef.svg)](https://www.npmjs.com/package/soef)\r\n[![Tests](http://img.shields.io/travis/soef/soef/master.svg)](https://travis-ci.org/soef/soef)\r\n[![Build status](https://ci.appveyor.com/api/projects/status/njb3gh6f49motmuk?svg=true)](https://ci.appveyor.com/project/soef/soef)\r\n-->\r\n\r\n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/soef/soef/blob/master/LICENSE)\r\n\r\n",
"_id": "mdns-discovery@0.0.4",
"_shasum": "25b2ade419133ded1f4e9198f5143a33a099a8ec",
"_from": "mdns-discovery@latest"
}

@@ -10,1 +10,42 @@ ###mnds Multicast DNS

####Some Examples:
Find all Amazon Fire TV devices on the local network:
```
var Mdns = require('mdns-discovery');
var mdns = new Mdns({ timeout: 5, name: '_amzn-wplay._tcp.local', find: 'amzn.dmgr:'});
mdns.run (function(res) {
res.forEach(enry) {
console.log(entry);
}
});
```
List all mdns questions and answers for 10 seconds:
```
var Mdns = require('mdns-discovery');
var mdns = new Mdns({ timeout: 10 });
mdns.on('packet', function (packets, rinfo) {
if (packets.answers) packets.answers.forEach(function(packet, i) {
console.log(`A: ${rinfo.address} - packet[${i}]=${packet.name}, type=${packet.type}, class=${packet.class}, ttl=${packet.ttl}}`);
});
if (packets.questions) packets.questions.forEach(function(packet, i) {
console.log(`Q: ${rinfo.address} - packet[${i}]=${packet.name}, type=${packet.type}, class=${packet.class}, ttl=${packet.ttl}}`);
});
});
mdns.run ();
```
Presence:
```
var mdns = require('mdns-discovery')();
mdns.onIP('192.168.1.31', function (packet, rinfo) {
if (packet.answers.length) {
console.log(rinfo.address + ' is present');
}
}).run ();
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc