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.
13 const dnsPacket = require('dns-packet')
14 const dgram = require('dgram')
16 const socket = dgram.createSocket('udp4')
18 const buf = dnsPacket.encode({
21 flags: dnsPacket.RECURSION_DESIRED,
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')
35 Also see [the UDP example](examples/udp.js).
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)
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
69 Packets look like this
73 type: 'query|response',
75 flags: optionalBitFlags,
83 The bit flags available are
86 packet.RECURSION_DESIRED
87 packet.RECURSION_AVAILABLE
88 packet.TRUNCATED_RESPONSE
89 packet.AUTHORITATIVE_ANSWER
91 packet.CHECKING_DISABLED
94 To use more than one flag bitwise-or them together
97 var flags = packet.RECURSION_DESIRED | packet.RECURSION_AVAILABLE
100 And to check for a flag use bitwise-and
103 var isRecursive = message.flags & packet.RECURSION_DESIRED
106 A question looks like this
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
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
134 data: 'IPv4 address' // fx 127.0.0.1
142 data: 'IPv6 address' // fx fe80::1
151 tag: 'issue|issuewild|iodef',
152 value: 'ca.example.net',
153 issuerCritical: false
161 data: 'cname.to.another.record'
169 data: 'dname.to.another.record'
177 flags: 257, // 16 bits
178 algorithm: 1, // octet
210 exchange: 'mail.example.net'
226 nextDomain: 'a.domain',
227 rrtypes: ['A', 'TXT', 'RRSIG']
239 nextDomain: Buffer, // Hashed per RFC5155
240 rrtypes: ['A', 'TXT', 'RRSIG']
248 data: Buffer('any binary data')
254 [EDNS0](https://tools.ietf.org/html/rfc6891) options.
260 udpPayloadSize: 4096,
261 flags: packet.DNSSEC_OK,
263 // pass in any code/data for generic EDNS0 options
265 data: Buffer.alloc(31)
267 // Several EDNS0 options have enhanced support
271 code: 'CLIENT_SUBNET',
272 family: 2, // 1 for IPv4, 2 for IPv6
273 sourcePrefixLength: 64, // used to truncate IP address
274 scopePrefixLength: 0,
277 code: 'TCP_KEEPALIVE',
278 timeout: 150 // increments of 100ms. This means 15s.
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.
292 data: 'points.to.another.record'
300 mbox: 'admin.example.com',
301 txt: 'txt.example.com'
313 expiration: timestamp,
314 inception: timestamp,
316 signersName: 'a.name',
330 refresh: refreshInterval,
331 retry: retryInterval,
332 expire: expireInterval,
344 target: serviceHostName,
345 priority: optionalServicePriority,
346 weight: optionalServiceWeight
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.