Skip to content

Commit

Permalink
Enforce proper DNS host comparison
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
watson committed Jan 10, 2016
1 parent 4c2654a commit c9b0bc5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var util = require('util')
var EventEmitter = require('events').EventEmitter
var serviceName = require('multicast-dns-service-types')
var dnsEqual = require('dns-equal')
var txt = require('mdns-txt')()

var TLD = '.local'
Expand Down Expand Up @@ -75,22 +76,22 @@ Browser.prototype._addService = function (service) {
Browser.prototype._removeService = function (fqdn) {
var service, index
this.services.some(function (s, i) {
if (s.fqdn === fqdn) {
if (dnsEqual(s.fqdn, fqdn)) {
service = s
index = i
return true
}
})
if (!service) return
this.services.splice(index, 1)
this._serviceMap[fqdn] = false
delete this._serviceMap[fqdn]
this.emit('down', service)
}

function goodbyes (name, packet) {
return packet.answers.concat(packet.additionals)
.filter(function (rr) {
return rr.name === name && rr.type === 'PTR' && rr.ttl === 0
return dnsEqual(rr.name, name) && rr.type === 'PTR' && rr.ttl === 0
})
.map(function (rr) {
return rr.data
Expand All @@ -104,7 +105,7 @@ function buildServicesFor (name, packet) {

return records
.filter(function (rr) {
return rr.name === name && rr.type === 'PTR'
return dnsEqual(rr.name, name) && rr.type === 'PTR'
})
.map(function (ptr) {
var service = {
Expand All @@ -113,7 +114,7 @@ function buildServicesFor (name, packet) {

records
.filter(function (rr) {
return rr.name === ptr.data && (rr.type === 'SRV' || rr.type === 'TXT')
return dnsEqual(rr.name, ptr.data) && (rr.type === 'SRV' || rr.type === 'TXT')
})
.forEach(function (rr) {
if (rr.type === 'SRV') {
Expand All @@ -135,7 +136,7 @@ function buildServicesFor (name, packet) {

records
.filter(function (rr) {
return rr.name === service.host && (rr.type === 'A' || rr.type === 'AAAA')
return dnsEqual(rr.name, service.host) && (rr.type === 'A' || rr.type === 'AAAA')
})
.forEach(function (rr) {
service.addresses.push(rr.data)
Expand Down
3 changes: 2 additions & 1 deletion lib/mdns-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var multicastdns = require('multicast-dns')
var dnsEqual = require('dns-equal')
var flatten = require('./flatten')

module.exports = Server
Expand Down Expand Up @@ -83,7 +84,7 @@ 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 _name === name
return dnsEqual(_name, name)
})
} else {
return self.registry[type]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A Bonjour/Zeroconf implementation in pure JavaScript",
"main": "index.js",
"dependencies": {
"dns-equal": "^1.0.0",
"mdns-txt": "^2.0.0",
"multicast-dns": "^5.0.0",
"multicast-dns-service-types": "^1.1.0"
Expand Down

0 comments on commit c9b0bc5

Please sign in to comment.