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