Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add A & AAAA record support #12

Merged
merged 4 commits into from
May 6, 2016
Merged
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
5 changes: 3 additions & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ function buildServicesFor (name, packet) {
return (rr.type === 'A' || rr.type === 'AAAA') && dnsEqual(rr.name, service.host)
})
.forEach(function (rr) {
service.addresses.push(rr.data)
if (service.addresses.indexOf(rr.data) === -1) { // avoid duplicate
service.addresses.push(rr.data)
}
})

return service
})
.filter(function (rr) {
Expand Down
5 changes: 4 additions & 1 deletion lib/mdns-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Server.prototype._respondToQuery = function (query) {
additionals = additionals
.concat(self._recordsFor(answer.data, 'SRV'))
.concat(self._recordsFor(answer.data, 'TXT'))
.concat(self._recordsFor(answer.data, 'A'))
.concat(self._recordsFor(answer.data, 'AAAA'))
})
}

Expand All @@ -83,7 +85,8 @@ Server.prototype._recordsFor = function (name, type) {
if (name) {
return self.registry[type].filter(function (record) {
var _name = ~name.indexOf('.') ? record.name : record.name.split('.')[0]
return dnsEqual(_name, name)
var host = (record.type === 'A' || record.type === 'AAAA')
return dnsEqual(_name, name) || host // the A or AAAA name match the host name, not the service name
})
} else {
return self.registry[type]
Expand Down
32 changes: 31 additions & 1 deletion lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ function Service (opts) {
}

Service.prototype._records = function () {
return [rr_ptr(this), rr_srv(this), rr_txt(this)]
var records = [rr_ptr(this), rr_srv(this), rr_txt(this)]
var self = this
var networks_list = os.networkInterfaces()
Object.keys(networks_list).forEach(function (itr_idx, index, arr) { // itr_idx = interface name
networks_list[itr_idx].forEach(function (itr, index2, arr2) { // for each interface (itr)
if (itr.internal === false && itr.family === 'IPv4') {
records.push(rr_a(self, itr.address))
} else if (itr.internal === false && itr.family === 'IPv6') {
records.push(rr_aaaa(self, itr.address))
}
})
})
return records
}

function rr_ptr (service) {
Expand Down Expand Up @@ -63,3 +75,21 @@ function rr_txt (service) {
data: txt.encode(service.txt)
}
}

function rr_a (service, ip_address) {
return {
name: service.host,
type: 'A',
ttl: 120,
data: ip_address
}
}

function rr_aaaa (service, ip_address) {
return {
name: service.host,
type: 'AAAA',
ttl: 120,
data: ip_address
}
}
19 changes: 18 additions & 1 deletion test/bonjour.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ var afterAll = require('after-all')
var Service = require('../lib/service')
var Bonjour = require('../')

var get_addresses = function () {
var addresses = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
var addrs = itrs[i]
for (var j in addrs) {
if (addrs[j].internal === false) {
addresses.push(addrs[j].address)
}
}
}
return addresses
}

var port = function (cb) {
var s = dgram.createSocket('udp4')
s.bind(0, function () {
Expand Down Expand Up @@ -80,13 +94,14 @@ test('bonjour.find', function (bonjour, t) {
t.equal(s.type, 'test')
t.equal(s.protocol, 'tcp')
t.deepEqual(s.subtypes, [])
t.deepEqual(s.addresses, [])
t.deepEqual(s.addresses, get_addresses())

if (++ups === 2) {
// use timeout in an attempt to make sure the invalid record doesn't
// bubble up
setTimeout(function () {
bonjour.destroy()
browser.stop()
t.end()
}, 50)
}
Expand All @@ -111,6 +126,7 @@ test('bonjour.find - down event', function (bonjour, t) {

browser.on('down', function (s) {
t.equal(s.name, 'Foo Bar')
browser.stop()
bonjour.destroy()
t.end()
})
Expand All @@ -135,6 +151,7 @@ test('bonjour.findOne - emitter', function (bonjour, t) {
var browser = bonjour.findOne({ type: 'test' })
browser.on('up', function (s) {
t.equal(s.name, 'Emitter')
browser.stop()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you added the browser.stop() calls a few places? Did the event loop hang?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it was one of my tentative to fix a bug that I didn't clear ... I can remove it if you want me to.
for whatever reason, the buildServicesFor function acted weirdly during the test; it was pushing duplicate addresses to the addresses field.
I resolved it, but I can't really explain why it was doing that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem - I removed it my self :)

bonjour.destroy()
t.end()
})
Expand Down
18 changes: 16 additions & 2 deletions test/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ var os = require('os')
var test = require('tape')
var Service = require('../lib/service')

var get_addresses_records = function (host) {
var addresses_records = []
var itrs = os.networkInterfaces()
for (var i in itrs) {
var addrs = itrs[i]
for (var j in addrs) {
if (addrs[j].internal === false) {
addresses_records.push({ data: addrs[j].address, name: host, ttl: 120, type: addrs[j].family === 'IPv4' ? 'A' : 'AAAA' })
}
}
}
return addresses_records
}

test('no name', function (t) {
t.throws(function () {
new Service({ type: 'http', port: 3000 }) // eslint-disable-line no-new
Expand Down Expand Up @@ -63,7 +77,7 @@ test('_records() - minimal', function (t) {
{ data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
{ data: { port: 3000, target: os.hostname() }, name: s.fqdn, ttl: 120, type: 'SRV' },
{ data: new Buffer('00', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
])
].concat(get_addresses_records(s.host)))
t.end()
})

Expand All @@ -73,6 +87,6 @@ test('_records() - everything', function (t) {
{ data: s.fqdn, name: '_http._tcp.local', ttl: 28800, type: 'PTR' },
{ data: { port: 3000, target: 'example.com' }, name: s.fqdn, ttl: 120, type: 'SRV' },
{ data: new Buffer('07666f6f3d626172', 'hex'), name: s.fqdn, ttl: 4500, type: 'TXT' }
])
].concat(get_addresses_records(s.host)))
t.end()
})