Bug 1856942: part 5) Factor async loading of a sheet out of `Loader::LoadSheet`....
[gecko.git] / testing / xpcshell / dns-packet / README.md
blob2a729b3d107496e381554cc4116307e07bfc24c3
1 # dns-packet
2 [![](https://img.shields.io/npm/v/dns-packet.svg?style=flat)](https://www.npmjs.org/package/dns-packet) [![](https://img.shields.io/npm/dm/dns-packet.svg)](https://www.npmjs.org/package/dns-packet) [![](https://api.travis-ci.org/mafintosh/dns-packet.svg?style=flat)](https://travis-ci.org/mafintosh/dns-packet) [![Coverage Status](https://coveralls.io/repos/github/mafintosh/dns-packet/badge.svg?branch=master)](https://coveralls.io/github/mafintosh/dns-packet?branch=master)
4 An [abstract-encoding](https://github.com/mafintosh/abstract-encoding) compliant module for encoding / decoding DNS packets. Lifted out of [multicast-dns](https://github.com/mafintosh/multicast-dns) as a separate module.
6 ```
7 npm install dns-packet
8 ```
10 ## UDP Usage
12 ``` js
13 const dnsPacket = require('dns-packet')
14 const dgram = require('dgram')
16 const socket = dgram.createSocket('udp4')
18 const buf = dnsPacket.encode({
19   type: 'query',
20   id: 1,
21   flags: dnsPacket.RECURSION_DESIRED,
22   questions: [{
23     type: 'A',
24     name: 'google.com'
25   }]
28 socket.on('message', message => {
29   console.log(dnsPacket.decode(message)) // prints out a response from google dns
32 socket.send(buf, 0, buf.length, 53, '8.8.8.8')
33 ```
35 Also see [the UDP example](examples/udp.js).
37 ## TCP, TLS, HTTPS
39 While DNS has traditionally been used over a datagram transport, it is increasingly being carried over TCP for larger responses commonly including DNSSEC responses and TLS or HTTPS for enhanced security. See below examples on how to use `dns-packet` to wrap DNS packets in these protocols:
41 - [TCP](examples/tcp.js)
42 - [DNS over TLS](examples/tls.js)
43 - [DNS over HTTPS](examples/doh.js)
45 ## API
47 #### `var buf = packets.encode(packet, [buf], [offset])`
49 Encodes a DNS packet into a buffer containing a UDP payload.
51 #### `var packet = packets.decode(buf, [offset])`
53 Decode a DNS packet from a buffer containing a UDP payload.
55 #### `var buf = packets.streamEncode(packet, [buf], [offset])`
57 Encodes a DNS packet into a buffer containing a TCP payload.
59 #### `var packet = packets.streamDecode(buf, [offset])`
61 Decode a DNS packet from a buffer containing a TCP payload.
63 #### `var len = packets.encodingLength(packet)`
65 Returns how many bytes are needed to encode the DNS packet
67 ## Packets
69 Packets look like this
71 ``` js
73   type: 'query|response',
74   id: optionalIdNumber,
75   flags: optionalBitFlags,
76   questions: [...],
77   answers: [...],
78   additionals: [...],
79   authorities: [...]
81 ```
83 The bit flags available are
85 ``` js
86 packet.RECURSION_DESIRED
87 packet.RECURSION_AVAILABLE
88 packet.TRUNCATED_RESPONSE
89 packet.AUTHORITATIVE_ANSWER
90 packet.AUTHENTIC_DATA
91 packet.CHECKING_DISABLED
92 ```
94 To use more than one flag bitwise-or them together
96 ``` js
97 var flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE
98 ```
100 And to check for a flag use bitwise-and
102 ``` js
103 var isRecursive = message.flags & packet.RECURSION_DESIRED
106 A question looks like this
108 ``` js
110   type: 'A', // or SRV, AAAA, etc
111   class: 'IN', // one of IN, CS, CH, HS, ANY. Default: IN
112   name: 'google.com' // which record are you looking for
116 And an answer, additional, or authority looks like this
118 ``` js
120   type: 'A', // or SRV, AAAA, etc
121   class: 'IN', // one of IN, CS, CH, HS
122   name: 'google.com', // which name is this record for
123   ttl: optionalTimeToLiveInSeconds,
124   (record specific data, see below)
128 ## Supported record types
130 #### `A`
132 ``` js
134   data: 'IPv4 address' // fx 127.0.0.1
138 #### `AAAA`
140 ``` js
142   data: 'IPv6 address' // fx fe80::1
146 #### `CAA`
148 ``` js
150   flags: 128, // octet
151   tag: 'issue|issuewild|iodef',
152   value: 'ca.example.net',
153   issuerCritical: false
157 #### `CNAME`
159 ``` js
161   data: 'cname.to.another.record'
165 #### `DNAME`
167 ``` js
169   data: 'dname.to.another.record'
173 #### `DNSKEY`
175 ``` js
177   flags: 257, // 16 bits
178   algorithm: 1, // octet
179   key: Buffer
183 #### `DS`
185 ``` js
187   keyTag: 12345,
188   algorithm: 8,
189   digestType: 1,
190   digest: Buffer
194 #### `HINFO`
196 ``` js
198   data: {
199     cpu: 'cpu info',
200     os: 'os info'
201   }
205 #### `MX`
207 ``` js
209   preference: 10,
210   exchange: 'mail.example.net'
214 #### `NS`
216 ``` js
218   data: nameServer
222 #### `NSEC`
224 ``` js
226   nextDomain: 'a.domain',
227   rrtypes: ['A', 'TXT', 'RRSIG']
231 #### `NSEC3`
233 ``` js
235   algorithm: 1,
236   flags: 0,
237   iterations: 2,
238   salt: Buffer,
239   nextDomain: Buffer, // Hashed per RFC5155
240   rrtypes: ['A', 'TXT', 'RRSIG']
244 #### `NULL`
246 ``` js
248   data: Buffer('any binary data')
252 #### `OPT`
254 [EDNS0](https://tools.ietf.org/html/rfc6891) options.
256 ``` js
258   type: 'OPT',
259   name: '.',
260   udpPayloadSize: 4096,
261   flags: packet.DNSSEC_OK,
262   options: [{
263     // pass in any code/data for generic EDNS0 options
264     code: 12,
265     data: Buffer.alloc(31)
266   }, {
267     // Several EDNS0 options have enhanced support
268     code: 'PADDING',
269     length: 31,
270   }, {
271     code: 'CLIENT_SUBNET',
272     family: 2, // 1 for IPv4, 2 for IPv6
273     sourcePrefixLength: 64, // used to truncate IP address
274     scopePrefixLength: 0,
275     ip: 'fe80::',
276   }, {
277     code: 'TCP_KEEPALIVE',
278     timeout: 150 // increments of 100ms.  This means 15s.
279   }, {
280     code: 'KEY_TAG',
281     tags: [1, 2, 3],
282   }]
286 The options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support enhanced de/encoding. See [optionscodes.js](https://github.com/mafintosh/dns-packet/blob/master/optioncodes.js) for all supported option codes. If the `data` property is present on a option, it takes precedence. On decoding, `data` will always be defined.
288 #### `PTR`
290 ``` js
292   data: 'points.to.another.record'
296 #### `RP`
298 ``` js
300   mbox: 'admin.example.com',
301   txt: 'txt.example.com'
305 #### `RRSIG`
307 ``` js
309   typeCovered: 'A',
310   algorithm: 8,
311   labels: 1,
312   originalTTL: 3600,
313   expiration: timestamp,
314   inception: timestamp,
315   keyTag: 12345,
316   signersName: 'a.name',
317   signature: Buffer
321 #### `SOA`
323 ``` js
325   data:
326     {
327       mname: domainName,
328       rname: mailbox,
329       serial: zoneSerial,
330       refresh: refreshInterval,
331       retry: retryInterval,
332       expire: expireInterval,
333       minimum: minimumTTL
334     }
338 #### `SRV`
340 ``` js
342   data: {
343     port: servicePort,
344     target: serviceHostName,
345     priority: optionalServicePriority,
346     weight: optionalServiceWeight
347   }
351 #### `TXT`
353 ``` js
355   data: 'text' || Buffer || [ Buffer || 'text' ]
359 When encoding, scalar values are converted to an array and strings are converted to UTF-8 encoded Buffers. When decoding, the return value will always be an array of Buffer.
361 If you need another record type, open an issue and we'll try to add it.
363 ## License