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