Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var et = require('elementtree');
var parseUrl = require('url').parse;
var os = require('os');
var concat = require('concat-stream');
var address = require('network-address');
var ip = require('ip');
var debug = require('debug')('upnp-device-client');
var pkg = require('./package.json');

Expand Down Expand Up @@ -364,12 +364,17 @@ DeviceClient.prototype.ensureEventingServer = function(callback) {
listener(e);
});
});

// be sure we quit response by sending back a 200 OK, otherwise well developed UPNP Devices will kick us out of their subscription list
res.end();

}));

});

this.server.listen(0, address.ipv4());
// be sure that we are listening on the correct interface where the client resides
var iface = this.getIfaceForUrl(this.url);
this.server.listen(0, ip.address(iface));
}

if(!this.listening) {
Expand All @@ -393,6 +398,27 @@ DeviceClient.prototype.releaseEventingServer = function() {
};


DeviceClient.prototype.getIfaceForUrl = function(_url) {
var options = parseUrl(_url);
var interfaces = os.networkInterfaces();
var retIface = "";

Object.keys(interfaces).map(function (nic) {
for (var i = 0; i < interfaces[nic].length; i++)
{
if(interfaces[nic][i].family.toLowerCase() == "ipv4")
{
var base1 = ip.mask(interfaces[nic][i].address, interfaces[nic][i].netmask);
var base2 = ip.mask(options.hostname, interfaces[nic][i].netmask); // TODO: maybe resolve IP here from hostname if hostname is not an ip
if (base1 == base2)
retIface = nic;
}
}
});
return retIface;
};


function parseEvents(buf) {
var events = [];
var doc = et.parse(buf.toString());
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "upnp-device-client",
"version": "1.0.2",
"version": "1.0.3",
"description": "A simple and versatile UPnP device client",
"author": "thibauts",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"concat-stream": "^1.4.8",
"debug": "^2.1.3",
"elementtree": "~0.1.6",
"network-address": "^1.0.0"
"ip": "^1.1.4"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down