You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
4.1 KiB

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