aaac7fed
liuqimichale
add
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# multicast-dns
Low level multicast-dns implementation in pure javascript
```
npm install multicast-dns
```
[](http://travis-ci.org/mafintosh/multicast-dns)
## Usage
``` js
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)
})
// lets query for an A record for 'brunhilde.local'
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
``` js
got a query packet: { type: 'query',
questions: [ { name: 'brunhilde.local', type: 'A', class: 1 } ],
answers: [],
authorities: [],
additionals: [] }
```
And then a response packet
``` js
got a response packet: { type: 'response',
questions: [],
answers:
[ { name: 'brunhilde.local',
type: 'A',
class: 1,
ttl: 120,
flush: true,
data: '192.168.1.5' } ],
authorities: [],
additionals:
[ { name: 'brunhilde.local',
type: 'A',
class: 1,
ttl: 120,
flush: true,
data: '192.168.1.5' },
{ name: 'brunhilde.local',
type: 'AAAA',
class: 1,
ttl: 120,
flush: true,
data: 'fe80::5ef9:38ff:fe8c:ceaa' } ] }
```
# CLI
```
npm install -g multicast-dns
```
```
multicast-dns brunhilde.local
> 192.168.1.1
```
# API
A packet has the following format
``` js
{
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
``` js
{
multicast: true // use udp multicasting
interface: '192.168.0.2' // explicitly specify a network interface. defaults to all
port: 5353, // set the udp port
ip: '224.0.0.251', // set the udp ip
ttl: 255, // set the multicast ttl
loopback: true, // receive your own packets
reuseAddr: true // set the reuseAddr option when creating the socket (requires node >=0.11.13)
}
```
#### `mdns.on('query', (packet, rinfo))`
Emitted when a query packet is received.
``` js
mdns.on('query', function(query) {
if (query.questions[0] && query.questions[0].name === 'brunhilde.local') {
mdns.respond(someResponse) // see below
}
})
```
#### `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
``` js
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.
``` js
// reply with a SRV and a A record as an answer
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
``` js
mdns.respond([{name:'brunhilde.local', type:'A', data:'192.158.1.5'}])
mdns.respond({
answers: [{name:'brunhilde.local', type:'A', data:'192.158.1.5'}]
})
```
#### `mdns.destroy()`
Destroy the mdns instance. Closes the udp socket.
# Development
To start hacking on this module you can use this example to get started
```
git clone git://github.com/mafintosh/multicast-dns.git
npm install
node example.js
node cli.js $(hostname).local
```
## License
MIT
|