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.

231 lines
3.3 KiB

4 years ago
  1. # dns-packet
  2. An [abstract-encoding](https://github.com/mafintosh/abstract-encoding) compliant module for encoding / decoding DNS packets.
  3. Lifted out of [multicast-dns](https://github.com/mafintosh/multicast-dns) as a separate module.
  4. ```
  5. npm install dns-packet
  6. ```
  7. [![build status](https://travis-ci.org/mafintosh/dns-packet.svg?branch=master)](https://travis-ci.org/mafintosh/dns-packet)
  8. ## Usage
  9. ``` js
  10. var packet = require('dns-packet')
  11. var dgram = require('dgram')
  12. var socket = dgram.createSocket('udp4')
  13. var buf = packet.encode({
  14. type: 'query',
  15. id: 1,
  16. flags: packet.RECURSION_DESIRED,
  17. questions: [{
  18. type: 'A',
  19. name: 'google.com'
  20. }]
  21. })
  22. socket.on('message', function (message) {
  23. console.log(packet.decode(message)) // prints out a response from google dns
  24. })
  25. socket.send(buf, 0, buf.length, 53, '8.8.8.8')
  26. ```
  27. ## API
  28. #### `var buf = packets.encode(packet, [buf], [offset])`
  29. Encodes a DNS packet into a buffer.
  30. #### `var packet = packets.decode(buf, [offset])`
  31. Decode a DNS packet from a buffer
  32. #### `var len = packets.encodingLength(packet)`
  33. Returns how many bytes are needed to encode the DNS packet
  34. ## Packets
  35. Packets look like this
  36. ``` js
  37. {
  38. type: 'query|response',
  39. id: optionalIdNumber,
  40. flags: optionalBitFlags,
  41. questions: [...],
  42. answers: [...],
  43. additionals: [...],
  44. authorities: [...]
  45. }
  46. ```
  47. The bit flags available are
  48. ``` js
  49. packet.RECURSION_DESIRED
  50. packet.RECURSION_AVAILABLE
  51. packet.TRUNCATED_RESPONSE
  52. packet.AUTHORITATIVE_ANSWER
  53. packet.AUTHENTIC_DATA
  54. packet.CHECKING_DISABLED
  55. ```
  56. To use more than one flag bitwise-or them together
  57. ``` js
  58. var flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE
  59. ```
  60. And to check for a flag use bitwise-and
  61. ``` js
  62. var isRecursive = message.flags & packet.RECURSION_DESIRED
  63. ```
  64. A question looks like this
  65. ``` js
  66. {
  67. type: 'A', // or SRV, AAAA, etc
  68. name: 'google.com' // which record are you looking for
  69. }
  70. ```
  71. And an answers, additional, or authority looks like this
  72. ``` js
  73. {
  74. type: 'A', // or SRV, AAAA, etc
  75. name: 'google.com', // which name is this record for
  76. ttl: optionalTimeToLiveInSeconds,
  77. (record specific data, see below)
  78. }
  79. ```
  80. Currently the different available records are
  81. #### `A`
  82. ``` js
  83. {
  84. data: 'IPv4 address' // fx 127.0.0.1
  85. }
  86. ```
  87. #### `AAAA`
  88. ``` js
  89. {
  90. data: 'IPv6 address' // fx fe80::1
  91. }
  92. ```
  93. #### `TXT`
  94. ``` js
  95. {
  96. data: Buffer('some text')
  97. }
  98. ```
  99. #### `NS`
  100. ``` js
  101. {
  102. data: nameServer
  103. }
  104. ```
  105. #### `NULL`
  106. ``` js
  107. {
  108. data: Buffer('any binary data')
  109. }
  110. ```
  111. #### `SOA`
  112. ``` js
  113. {
  114. data:
  115. {
  116. mname: domainName,
  117. rname: mailbox,
  118. serial: zoneSerial,
  119. refresh: refreshInterval,
  120. retry: retryInterval,
  121. expire: expireInterval,
  122. minimum: minimumTTL
  123. }
  124. }
  125. ```
  126. #### `SRV`
  127. ``` js
  128. {
  129. data: {
  130. port: servicePort,
  131. target: serviceHostName,
  132. priority: optionalServicePriority,
  133. weight: optionalServiceWeight
  134. }
  135. }
  136. ```
  137. #### `HINFO`
  138. ``` js
  139. {
  140. data: {
  141. cpu: 'cpu info',
  142. os: 'os info'
  143. }
  144. }
  145. ```
  146. #### `PTR`
  147. ``` js
  148. {
  149. data: 'points.to.another.record'
  150. }
  151. ```
  152. #### `CNAME`
  153. ``` js
  154. {
  155. data: 'cname.to.another.record'
  156. }
  157. ```
  158. #### `DNAME`
  159. ``` js
  160. {
  161. data: 'dname.to.another.record'
  162. }
  163. ```
  164. #### `CAA`
  165. ``` js
  166. {
  167. flags: 128, // octet
  168. tag: 'issue|issuewild|iodef',
  169. value: 'ca.example.net'
  170. }
  171. ```
  172. If you need another one, open an issue and we'll try to add it.
  173. ## License
  174. MIT