diff --git a/README.md b/README.md index 025fc3e..5c271d9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,6 @@ mdns.start(() => setTimeout(() => mdns.stop(() => {}), 20 * 1000)) ``` - options - - `broadcast` - (true/false) announce our presence through mDNS - - `interval` - query interval - - `serviceTag` - name of the service announced + - `broadcast` - (true/false) announce our presence through mDNS, default false + - `interval` - query interval, default 10 * 1000 (10 seconds) + - `serviceTag` - name of the service announcedm, default 'ipfs.local` diff --git a/src/index.js b/src/index.js index cb1642f..04f8b95 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ class MulticastDNS extends EventEmitter { this.broadcast = options.broadcast !== false this.interval = options.interval || (1e3 * 10) - this.serviceTag = options.serviceTag || '_ipfs-discovery._udp' + this.serviceTag = options.serviceTag || 'ipfs.local' this.port = options.port || 5353 this.peerInfo = peerInfo this._queryInterval = null diff --git a/src/query.js b/src/query.js index 2c97c6c..ee39386 100644 --- a/src/query.js +++ b/src/query.js @@ -12,14 +12,19 @@ const tcp = new TCP() module.exports = { queryLAN: function (mdns, serviceTag, interval) { - return setInterval(() => { + const query = () => { + log('query', serviceTag) mdns.query({ questions: [{ name: serviceTag, type: 'PTR' }] }) - }, interval) + } + + // Immediately start a query, then do it every interval. + query() + return setInterval(query, interval) }, gotResponse: function (rsp, peerInfo, serviceTag, callback) { @@ -55,8 +60,9 @@ module.exports = { answers.a.forEach((a) => { multiaddrs.push(new Multiaddr('/ip4/' + a.data + '/tcp/' + port)) }) - - // TODO Create multiaddrs from AAAA (IPv6) records as well + answers.aaaa.forEach((a) => { + multiaddrs.push(new Multiaddr('/ip6/' + a.data + '/tcp/' + port)) + }) if (peerInfo.id.toB58String() === b58Id) { return // replied to myself, ignore @@ -141,6 +147,7 @@ module.exports = { } }) + log('responding to query') mdns.respond(answers) } } diff --git a/test/multicast-dns.spec.js b/test/multicast-dns.spec.js index aec3498..f7f1a7b 100644 --- a/test/multicast-dns.spec.js +++ b/test/multicast-dns.spec.js @@ -8,6 +8,7 @@ chai.use(dirtyChai) const multiaddr = require('multiaddr') const PeerInfo = require('peer-info') const parallel = require('async/parallel') +const series = require('async/series') const MulticastDNS = require('./../src') @@ -18,7 +19,7 @@ describe('MulticastDNS', () => { let pD before(function (done) { - this.timeout(40 * 1000) + this.timeout(80 * 1000) parallel([ (cb) => { PeerInfo.create((err, peer) => { @@ -35,6 +36,7 @@ describe('MulticastDNS', () => { pB = peer pB.multiaddrs.add(multiaddr('/ip4/127.0.0.1/tcp/20002')) + pB.multiaddrs.add(multiaddr('/ip6/::1/tcp/20002')) cb() }) }, @@ -64,7 +66,10 @@ describe('MulticastDNS', () => { const options = { port: 50001 // port must be the same } - const mdnsA = new MulticastDNS(pA, options) + const mdnsA = new MulticastDNS(pA, { + broadcast: false, // do not talk to ourself + port: 50001 + }) const mdnsB = new MulticastDNS(pB, options) parallel([ @@ -73,7 +78,10 @@ describe('MulticastDNS', () => { ], () => { mdnsA.once('peer', (peerInfo) => { expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String()) - done() + parallel([ + (cb) => mdnsA.stop(cb), + (cb) => mdnsB.stop(cb) + ], done) }) mdnsB.once('peer', (peerInfo) => {}) @@ -87,7 +95,10 @@ describe('MulticastDNS', () => { port: 50003 // port must be the same } - const mdnsA = new MulticastDNS(pA, options) + const mdnsA = new MulticastDNS(pA, { + broadcast: false, // do not talk to ourself + port: 50003 + }) const mdnsC = new MulticastDNS(pC, options) const mdnsD = new MulticastDNS(pD, options) @@ -100,13 +111,46 @@ describe('MulticastDNS', () => { mdnsA.once('peer', (peerInfo) => { expect(pC.id.toB58String()).to.eql(peerInfo.id.toB58String()) expect(peerInfo.multiaddrs.size).to.equal(1) - done() + parallel([ + (cb) => mdnsA.stop(cb), + (cb) => mdnsC.stop(cb), + (cb) => mdnsD.stop(cb) + ], done) }) mdnsC.once('peer', (peerInfo) => {}) }) }) + it('announces IP6 addresses', function (done) { + this.timeout(40 * 1000) + + const options = { + port: 50001 // port must be the same + } + const mdnsA = new MulticastDNS(pA, { + broadcast: false, // do not talk to ourself + port: 50001 + }) + const mdnsB = new MulticastDNS(pB, options) + + series([ + (cb) => mdnsB.start(cb), + (cb) => mdnsA.start(cb) + ], () => { + mdnsA.once('peer', (peerInfo) => { + expect(pB.id.toB58String()).to.eql(peerInfo.id.toB58String()) + expect(peerInfo.multiaddrs.size).to.equal(2) + parallel([ + (cb) => mdnsA.stop(cb), + (cb) => mdnsB.stop(cb) + ], done) + }) + + mdnsB.once('peer', (peerInfo) => {}) + }) + }) + it('doesn\'t emit peers after stop', function (done) { this.timeout(40 * 1000) @@ -116,18 +160,13 @@ describe('MulticastDNS', () => { const mdnsA = new MulticastDNS(pA, options) const mdnsC = new MulticastDNS(pC, options) - setTimeout(done, 15000) - - parallel([ + series([ (cb) => mdnsA.start(cb), + (cb) => setTimeout(cb, 1000), + (cb) => mdnsA.stop(cb), (cb) => mdnsC.start(cb) ], () => { - mdnsA.stop((err) => { - if (err) { - return done(err) - } - }) - + setTimeout(() => mdnsC.stop(done), 5000) mdnsC.once('peer', (peerInfo) => { done(new Error('Should not receive new peer.')) })