multicast-dns
Low level multicast-dns implementation in pure javascript
npm install multicast-dns
Usage
var mdns = require('multicast-dns')()
mdns.on('response', function(response) {
console.log('got a response packet:', response)
})
mdns.on('query', function(query) {
console.log('got a query packet:', query)
})
mdns.query({
questions:[{
name: 'brunhilde.local',
type: 'A'
}]
})
Running the above (change brunhilde.local
to your-own-hostname.local
) will print an echo of the query packet first
got a query packet: { type: 'query',
qdcount: 1,
ancount: 0,
nscount: 0,
arcount: 0,
questions: [ { name: 'brunhilde.local', type: 'A', class: 1 } ],
answers: [],
authorities: [],
additionals: [] }
And then a response packet
got a response packet: { type: 'response',
qdcount: 0,
ancount: 1,
nscount: 0,
arcount: 2,
questions: [],
answers:
[ { name: 'brunhilde.local',
type: 'A',
class: 32769,
ttl: 120,
data: '192.168.1.5' } ],
authorities: [],
additionals:
[ { name: 'brunhilde.local',
type: 'A',
class: 32769,
ttl: 120,
data: '192.168.1.5' },
{ name: 'brunhilde.local',
type: 'AAAA',
class: 32769,
ttl: 120,
data: 'fe80::5ef9:38ff:fe8c:ceaa' } ] }
API
A packet has the following format
{
questions: [{
name:'brunhilde.local',
type:'A'
}],
answers: [{
name:'brunhilde.local',
type:'A',
ttl:seconds,
data:(record type specific data)
}],
additionals: [
(same format as answers)
],
authorities: [
(same format as answers)
]
}
Currently data from SRV
, A
, PTR
, TXT
, AAAA
and HINFO
records is passed
mdns = multicastdns([options])
Creates a new mdns
instance. Options can contain the following
{
multicast: true
interface: 'en0'
port: 5353,
ip: '224.0.0.251',
ttl: 255,
loopback: true
}
mdns.on('query', (packet, rinfo))
Emitted when a query packet is received.
mdns.on('query', function(query) {
if (query.questions[0] && query.questions[0].name === 'brunhilde.local') {
mdns.respond(someResponse)
}
})
mdns.on('response', (packet, rinfo))
Emitted when a response packet is received.
The response might not be a response to a query you send as this
is the result of someone multicasting a response.
mdns.query(packet, [cb])
Send a dns query. The callback will be called when the packet was sent.
The following shorthands are equivalent
mdns.query('brunhilde.local', 'A')
mdns.query([{name:'brunhilde.local', type:'A'}])
mdns.query({
questions: [{name:'brunhilde.local', type:'A'}]
})
mdns.respond(packet, [cb])
Send a dns response. The callback will be called when the packet was sent.
mdns.respond({
answers: [{
name: 'my-service',
type: 'SRV',
data: {
port:9999,
weigth: 0,
priority: 10,
target: 'my-service.example.com'
}
}, {
name: 'brunhilde.local',
type: 'A'
ttl: 300,
data: '192.168.1.5'
}]
})
The following shorthands are equivalent
mdns.response([{name:'brunhilde.local', type:'A', data:'192.158.1.5'}])
mdns.response({
answers: [{name:'brunhilde.local', type:'A', data:'192.158.1.5'}]
})
mdns.destroy()
Destroy the mdns instance. Closes the udp socket.
License
MIT