From 092cdfa79fa8b65a633734f5da73fbd8e9455d07 Mon Sep 17 00:00:00 2001 From: Eric Watson Date: Tue, 24 Sep 2019 08:46:20 -0500 Subject: [PATCH] Delete cached interface after stopping This fixes an issue where the library could use a stale multicast address. Steps to reproduce: 1. Connect to a wifi network 2. Start a dnssd browser 3. Stop the dnssd browser 4. Connect to a different wifi network 5. Start a dnssd browser An error like this is thrown: Error: setMulticastInterface EADDRNOTAVAIL at Socket.setMulticastInterface (dgram.js:566:11) at Socket.eval (webpack:///./node_modules/dnssd/lib/NetworkInterface.js?:238:41) at Socket.emit (events.js:182:13) at startListening (dgram.js:128:10) at state.handle.lookup (dgram.js:249:7) at process._tickCallback (internal/process/next_tick.js:63:19) This is because we are still using the IP address from the first NetworkInterface instance, even though it is not valid for the new network. Clearing the interface from `activeInterfaces` on stop solves the problem. --- lib/NetworkInterface.js | 9 +++++++++ src/NetworkInterface.js | 9 +++++++++ test/tests/NetworkInterface.test.js | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/lib/NetworkInterface.js b/lib/NetworkInterface.js index c2b470b..dd93959 100644 --- a/lib/NetworkInterface.js +++ b/lib/NetworkInterface.js @@ -428,6 +428,15 @@ NetworkInterface.prototype.stop = function () { this._sockets = []; this._buffers = []; + var intf = this; + + // Remove any entries from active interfaces. + Object.keys(activeInterfaces).forEach(function (name) { + if (activeInterfaces[name] === intf) { + delete activeInterfaces[name]; + } + }); + debug('Done.'); }; diff --git a/src/NetworkInterface.js b/src/NetworkInterface.js index d342e4c..fc5f654 100644 --- a/src/NetworkInterface.js +++ b/src/NetworkInterface.js @@ -421,6 +421,15 @@ NetworkInterface.prototype.stop = function() { this._sockets = []; this._buffers = []; + const intf = this; + + // Remove any entries from active interfaces. + Object.keys(activeInterfaces).forEach((name) => { + if (activeInterfaces[name] === intf) { + delete activeInterfaces[name]; + } + }); + debug('Done.'); }; diff --git a/test/tests/NetworkInterface.test.js b/test/tests/NetworkInterface.test.js index 4fd7225..ec02c83 100644 --- a/test/tests/NetworkInterface.test.js +++ b/test/tests/NetworkInterface.test.js @@ -604,6 +604,15 @@ describe('NetworkInterface', function() { intf._sockets = [socket]; intf.stop(); }); + + it.only('should remove interface from active interfaces cache', function() { + const intf = new NetworkInterface.get(); + + intf.stop(); + const newIntf = new NetworkInterface.get(); + + expect(newIntf).to.not.equal(intf) + }); }); });