1 |
3a515b92
|
cagy
|
var types = require('./types')
|
2 |
|
|
var rcodes = require('./rcodes')
|
3 |
|
|
var opcodes = require('./opcodes')
|
4 |
|
|
var ip = require('ip')
|
5 |
|
|
var Buffer = require('safe-buffer').Buffer
|
6 |
|
|
|
7 |
|
|
var QUERY_FLAG = 0
|
8 |
|
|
var RESPONSE_FLAG = 1 << 15
|
9 |
|
|
var FLUSH_MASK = 1 << 15
|
10 |
|
|
var NOT_FLUSH_MASK = ~FLUSH_MASK
|
11 |
|
|
var QU_MASK = 1 << 15
|
12 |
|
|
var NOT_QU_MASK = ~QU_MASK
|
13 |
|
|
|
14 |
|
|
var name = exports.txt = exports.name = {}
|
15 |
|
|
|
16 |
|
|
name.encode = function (str, buf, offset) {
|
17 |
|
|
if (!buf) buf = Buffer.allocUnsafe(name.encodingLength(str))
|
18 |
|
|
if (!offset) offset = 0
|
19 |
|
|
var oldOffset = offset
|
20 |
|
|
|
21 |
|
|
// strip leading and trailing .
|
22 |
|
|
var n = str.replace(/^\.|\.$/gm, '')
|
23 |
|
|
if (n.length) {
|
24 |
|
|
var list = n.split('.')
|
25 |
|
|
|
26 |
|
|
for (var i = 0; i < list.length; i++) {
|
27 |
|
|
var len = buf.write(list[i], offset + 1)
|
28 |
|
|
buf[offset] = len
|
29 |
|
|
offset += len + 1
|
30 |
|
|
}
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
buf[offset++] = 0
|
34 |
|
|
|
35 |
|
|
name.encode.bytes = offset - oldOffset
|
36 |
|
|
return buf
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
name.encode.bytes = 0
|
40 |
|
|
|
41 |
|
|
name.decode = function (buf, offset) {
|
42 |
|
|
if (!offset) offset = 0
|
43 |
|
|
|
44 |
|
|
var list = []
|
45 |
|
|
var oldOffset = offset
|
46 |
|
|
var len = buf[offset++]
|
47 |
|
|
|
48 |
|
|
if (len === 0) {
|
49 |
|
|
name.decode.bytes = 1
|
50 |
|
|
return '.'
|
51 |
|
|
}
|
52 |
|
|
if (len >= 0xc0) {
|
53 |
|
|
var res = name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000)
|
54 |
|
|
name.decode.bytes = 2
|
55 |
|
|
return res
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
while (len) {
|
59 |
|
|
if (len >= 0xc0) {
|
60 |
|
|
list.push(name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000))
|
61 |
|
|
offset++
|
62 |
|
|
break
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
list.push(buf.toString('utf-8', offset, offset + len))
|
66 |
|
|
offset += len
|
67 |
|
|
len = buf[offset++]
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
name.decode.bytes = offset - oldOffset
|
71 |
|
|
return list.join('.')
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
name.decode.bytes = 0
|
75 |
|
|
|
76 |
|
|
name.encodingLength = function (n) {
|
77 |
|
|
return Buffer.byteLength(n) + 2
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
var string = {}
|
81 |
|
|
|
82 |
|
|
string.encode = function (s, buf, offset) {
|
83 |
|
|
if (!buf) buf = Buffer.allocUnsafe(string.encodingLength(s))
|
84 |
|
|
if (!offset) offset = 0
|
85 |
|
|
|
86 |
|
|
var len = buf.write(s, offset + 1)
|
87 |
|
|
buf[offset] = len
|
88 |
|
|
string.encode.bytes = len + 1
|
89 |
|
|
return buf
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
string.encode.bytes = 0
|
93 |
|
|
|
94 |
|
|
string.decode = function (buf, offset) {
|
95 |
|
|
if (!offset) offset = 0
|
96 |
|
|
|
97 |
|
|
var len = buf[offset]
|
98 |
|
|
var s = buf.toString('utf-8', offset + 1, offset + 1 + len)
|
99 |
|
|
string.decode.bytes = len + 1
|
100 |
|
|
return s
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
string.decode.bytes = 0
|
104 |
|
|
|
105 |
|
|
string.encodingLength = function (s) {
|
106 |
|
|
return Buffer.byteLength(s) + 1
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
var header = {}
|
110 |
|
|
|
111 |
|
|
header.encode = function (h, buf, offset) {
|
112 |
|
|
if (!buf) buf = header.encodingLength(h)
|
113 |
|
|
if (!offset) offset = 0
|
114 |
|
|
|
115 |
|
|
var flags = (h.flags || 0) & 32767
|
116 |
|
|
var type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG
|
117 |
|
|
|
118 |
|
|
buf.writeUInt16BE(h.id || 0, offset)
|
119 |
|
|
buf.writeUInt16BE(flags | type, offset + 2)
|
120 |
|
|
buf.writeUInt16BE(h.questions.length, offset + 4)
|
121 |
|
|
buf.writeUInt16BE(h.answers.length, offset + 6)
|
122 |
|
|
buf.writeUInt16BE(h.authorities.length, offset + 8)
|
123 |
|
|
buf.writeUInt16BE(h.additionals.length, offset + 10)
|
124 |
|
|
|
125 |
|
|
return buf
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
header.encode.bytes = 12
|
129 |
|
|
|
130 |
|
|
header.decode = function (buf, offset) {
|
131 |
|
|
if (!offset) offset = 0
|
132 |
|
|
if (buf.length < 12) throw new Error('Header must be 12 bytes')
|
133 |
|
|
var flags = buf.readUInt16BE(offset + 2)
|
134 |
|
|
|
135 |
|
|
return {
|
136 |
|
|
id: buf.readUInt16BE(offset),
|
137 |
|
|
type: flags & RESPONSE_FLAG ? 'response' : 'query',
|
138 |
|
|
flags: flags & 32767,
|
139 |
|
|
flag_qr: ((flags >> 15) & 0x1) === 1,
|
140 |
|
|
opcode: opcodes.toString((flags >> 11) & 0xf),
|
141 |
|
|
flag_auth: ((flags >> 10) & 0x1) === 1,
|
142 |
|
|
flag_trunc: ((flags >> 9) & 0x1) === 1,
|
143 |
|
|
flag_rd: ((flags >> 8) & 0x1) === 1,
|
144 |
|
|
flag_ra: ((flags >> 7) & 0x1) === 1,
|
145 |
|
|
flag_z: ((flags >> 6) & 0x1) === 1,
|
146 |
|
|
flag_ad: ((flags >> 5) & 0x1) === 1,
|
147 |
|
|
flag_cd: ((flags >> 4) & 0x1) === 1,
|
148 |
|
|
rcode: rcodes.toString(flags & 0xf),
|
149 |
|
|
questions: new Array(buf.readUInt16BE(offset + 4)),
|
150 |
|
|
answers: new Array(buf.readUInt16BE(offset + 6)),
|
151 |
|
|
authorities: new Array(buf.readUInt16BE(offset + 8)),
|
152 |
|
|
additionals: new Array(buf.readUInt16BE(offset + 10))
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
header.decode.bytes = 12
|
157 |
|
|
|
158 |
|
|
header.encodingLength = function () {
|
159 |
|
|
return 12
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
var runknown = exports.unknown = {}
|
163 |
|
|
|
164 |
|
|
runknown.encode = function (data, buf, offset) {
|
165 |
|
|
if (!buf) buf = Buffer.allocUnsafe(runknown.encodingLength(data))
|
166 |
|
|
if (!offset) offset = 0
|
167 |
|
|
|
168 |
|
|
buf.writeUInt16BE(data.length, offset)
|
169 |
|
|
data.copy(buf, offset + 2)
|
170 |
|
|
|
171 |
|
|
runknown.encode.bytes = data.length + 2
|
172 |
|
|
return buf
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
runknown.encode.bytes = 0
|
176 |
|
|
|
177 |
|
|
runknown.decode = function (buf, offset) {
|
178 |
|
|
if (!offset) offset = 0
|
179 |
|
|
|
180 |
|
|
var len = buf.readUInt16BE(offset)
|
181 |
|
|
var data = buf.slice(offset + 2, offset + 2 + len)
|
182 |
|
|
runknown.decode.bytes = len + 2
|
183 |
|
|
return data
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
runknown.decode.bytes = 0
|
187 |
|
|
|
188 |
|
|
runknown.encodingLength = function (data) {
|
189 |
|
|
return data.length + 2
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
var rns = exports.ns = {}
|
193 |
|
|
|
194 |
|
|
rns.encode = function (data, buf, offset) {
|
195 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rns.encodingLength(data))
|
196 |
|
|
if (!offset) offset = 0
|
197 |
|
|
|
198 |
|
|
name.encode(data, buf, offset + 2)
|
199 |
|
|
buf.writeUInt16BE(name.encode.bytes, offset)
|
200 |
|
|
rns.encode.bytes = name.encode.bytes + 2
|
201 |
|
|
return buf
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
rns.encode.bytes = 0
|
205 |
|
|
|
206 |
|
|
rns.decode = function (buf, offset) {
|
207 |
|
|
if (!offset) offset = 0
|
208 |
|
|
|
209 |
|
|
var len = buf.readUInt16BE(offset)
|
210 |
|
|
var dd = name.decode(buf, offset + 2)
|
211 |
|
|
|
212 |
|
|
rns.decode.bytes = len + 2
|
213 |
|
|
return dd
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
rns.decode.bytes = 0
|
217 |
|
|
|
218 |
|
|
rns.encodingLength = function (data) {
|
219 |
|
|
return name.encodingLength(data) + 2
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
var rsoa = exports.soa = {}
|
223 |
|
|
|
224 |
|
|
rsoa.encode = function (data, buf, offset) {
|
225 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rsoa.encodingLength(data))
|
226 |
|
|
if (!offset) offset = 0
|
227 |
|
|
|
228 |
|
|
var oldOffset = offset
|
229 |
|
|
offset += 2
|
230 |
|
|
name.encode(data.mname, buf, offset)
|
231 |
|
|
offset += name.encode.bytes
|
232 |
|
|
name.encode(data.rname, buf, offset)
|
233 |
|
|
offset += name.encode.bytes
|
234 |
|
|
buf.writeUInt32BE(data.serial || 0, offset)
|
235 |
|
|
offset += 4
|
236 |
|
|
buf.writeUInt32BE(data.refresh || 0, offset)
|
237 |
|
|
offset += 4
|
238 |
|
|
buf.writeUInt32BE(data.retry || 0, offset)
|
239 |
|
|
offset += 4
|
240 |
|
|
buf.writeUInt32BE(data.expire || 0, offset)
|
241 |
|
|
offset += 4
|
242 |
|
|
buf.writeUInt32BE(data.minimum || 0, offset)
|
243 |
|
|
offset += 4
|
244 |
|
|
|
245 |
|
|
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
246 |
|
|
rsoa.encode.bytes = offset - oldOffset
|
247 |
|
|
return buf
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
rsoa.encode.bytes = 0
|
251 |
|
|
|
252 |
|
|
rsoa.decode = function (buf, offset) {
|
253 |
|
|
if (!offset) offset = 0
|
254 |
|
|
|
255 |
|
|
var oldOffset = offset
|
256 |
|
|
|
257 |
|
|
var data = {}
|
258 |
|
|
offset += 2
|
259 |
|
|
data.mname = name.decode(buf, offset)
|
260 |
|
|
offset += name.decode.bytes
|
261 |
|
|
data.rname = name.decode(buf, offset)
|
262 |
|
|
offset += name.decode.bytes
|
263 |
|
|
data.serial = buf.readUInt32BE(offset)
|
264 |
|
|
offset += 4
|
265 |
|
|
data.refresh = buf.readUInt32BE(offset)
|
266 |
|
|
offset += 4
|
267 |
|
|
data.retry = buf.readUInt32BE(offset)
|
268 |
|
|
offset += 4
|
269 |
|
|
data.expire = buf.readUInt32BE(offset)
|
270 |
|
|
offset += 4
|
271 |
|
|
data.minimum = buf.readUInt32BE(offset)
|
272 |
|
|
offset += 4
|
273 |
|
|
|
274 |
|
|
rsoa.decode.bytes = offset - oldOffset
|
275 |
|
|
return data
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
rsoa.decode.bytes = 0
|
279 |
|
|
|
280 |
|
|
rsoa.encodingLength = function (data) {
|
281 |
|
|
return 22 + name.encodingLength(data.mname) + name.encodingLength(data.rname)
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
var rtxt = exports.txt = exports.null = {}
|
285 |
|
|
var rnull = rtxt
|
286 |
|
|
|
287 |
|
|
rtxt.encode = function (data, buf, offset) {
|
288 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rtxt.encodingLength(data))
|
289 |
|
|
if (!offset) offset = 0
|
290 |
|
|
|
291 |
|
|
if (typeof data === 'string') data = Buffer.from(data)
|
292 |
|
|
if (!data) data = Buffer.allocUnsafe(0)
|
293 |
|
|
|
294 |
|
|
var oldOffset = offset
|
295 |
|
|
offset += 2
|
296 |
|
|
|
297 |
|
|
var len = data.length
|
298 |
|
|
data.copy(buf, offset, 0, len)
|
299 |
|
|
offset += len
|
300 |
|
|
|
301 |
|
|
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
302 |
|
|
rtxt.encode.bytes = offset - oldOffset
|
303 |
|
|
return buf
|
304 |
|
|
}
|
305 |
|
|
|
306 |
|
|
rtxt.encode.bytes = 0
|
307 |
|
|
|
308 |
|
|
rtxt.decode = function (buf, offset) {
|
309 |
|
|
if (!offset) offset = 0
|
310 |
|
|
var oldOffset = offset
|
311 |
|
|
var len = buf.readUInt16BE(offset)
|
312 |
|
|
|
313 |
|
|
offset += 2
|
314 |
|
|
|
315 |
|
|
var data = buf.slice(offset, offset + len)
|
316 |
|
|
offset += len
|
317 |
|
|
|
318 |
|
|
rtxt.decode.bytes = offset - oldOffset
|
319 |
|
|
return data
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
rtxt.decode.bytes = 0
|
323 |
|
|
|
324 |
|
|
rtxt.encodingLength = function (data) {
|
325 |
|
|
if (!data) return 2
|
326 |
|
|
return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
var rhinfo = exports.hinfo = {}
|
330 |
|
|
|
331 |
|
|
rhinfo.encode = function (data, buf, offset) {
|
332 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rhinfo.encodingLength(data))
|
333 |
|
|
if (!offset) offset = 0
|
334 |
|
|
|
335 |
|
|
var oldOffset = offset
|
336 |
|
|
offset += 2
|
337 |
|
|
string.encode(data.cpu, buf, offset)
|
338 |
|
|
offset += string.encode.bytes
|
339 |
|
|
string.encode(data.os, buf, offset)
|
340 |
|
|
offset += string.encode.bytes
|
341 |
|
|
buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
342 |
|
|
rhinfo.encode.bytes = offset - oldOffset
|
343 |
|
|
return buf
|
344 |
|
|
}
|
345 |
|
|
|
346 |
|
|
rhinfo.encode.bytes = 0
|
347 |
|
|
|
348 |
|
|
rhinfo.decode = function (buf, offset) {
|
349 |
|
|
if (!offset) offset = 0
|
350 |
|
|
|
351 |
|
|
var oldOffset = offset
|
352 |
|
|
|
353 |
|
|
var data = {}
|
354 |
|
|
offset += 2
|
355 |
|
|
data.cpu = string.decode(buf, offset)
|
356 |
|
|
offset += string.decode.bytes
|
357 |
|
|
data.os = string.decode(buf, offset)
|
358 |
|
|
offset += string.decode.bytes
|
359 |
|
|
rhinfo.decode.bytes = offset - oldOffset
|
360 |
|
|
return data
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
rhinfo.decode.bytes = 0
|
364 |
|
|
|
365 |
|
|
rhinfo.encodingLength = function (data) {
|
366 |
|
|
return string.encodingLength(data.cpu) + string.encodingLength(data.os) + 2
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
var rptr = exports.ptr = {}
|
370 |
|
|
var rcname = exports.cname = rptr
|
371 |
|
|
var rdname = exports.dname = rptr
|
372 |
|
|
|
373 |
|
|
rptr.encode = function (data, buf, offset) {
|
374 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rptr.encodingLength(data))
|
375 |
|
|
if (!offset) offset = 0
|
376 |
|
|
|
377 |
|
|
name.encode(data, buf, offset + 2)
|
378 |
|
|
buf.writeUInt16BE(name.encode.bytes, offset)
|
379 |
|
|
rptr.encode.bytes = name.encode.bytes + 2
|
380 |
|
|
return buf
|
381 |
|
|
}
|
382 |
|
|
|
383 |
|
|
rptr.encode.bytes = 0
|
384 |
|
|
|
385 |
|
|
rptr.decode = function (buf, offset) {
|
386 |
|
|
if (!offset) offset = 0
|
387 |
|
|
|
388 |
|
|
var data = name.decode(buf, offset + 2)
|
389 |
|
|
rptr.decode.bytes = name.decode.bytes + 2
|
390 |
|
|
return data
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
rptr.decode.bytes = 0
|
394 |
|
|
|
395 |
|
|
rptr.encodingLength = function (data) {
|
396 |
|
|
return name.encodingLength(data) + 2
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
var rsrv = exports.srv = {}
|
400 |
|
|
|
401 |
|
|
rsrv.encode = function (data, buf, offset) {
|
402 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rsrv.encodingLength(data))
|
403 |
|
|
if (!offset) offset = 0
|
404 |
|
|
|
405 |
|
|
buf.writeUInt16BE(data.priority || 0, offset + 2)
|
406 |
|
|
buf.writeUInt16BE(data.weight || 0, offset + 4)
|
407 |
|
|
buf.writeUInt16BE(data.port || 0, offset + 6)
|
408 |
|
|
name.encode(data.target, buf, offset + 8)
|
409 |
|
|
|
410 |
|
|
var len = name.encode.bytes + 6
|
411 |
|
|
buf.writeUInt16BE(len, offset)
|
412 |
|
|
|
413 |
|
|
rsrv.encode.bytes = len + 2
|
414 |
|
|
return buf
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
rsrv.encode.bytes = 0
|
418 |
|
|
|
419 |
|
|
rsrv.decode = function (buf, offset) {
|
420 |
|
|
if (!offset) offset = 0
|
421 |
|
|
|
422 |
|
|
var len = buf.readUInt16BE(offset)
|
423 |
|
|
|
424 |
|
|
var data = {}
|
425 |
|
|
data.priority = buf.readUInt16BE(offset + 2)
|
426 |
|
|
data.weight = buf.readUInt16BE(offset + 4)
|
427 |
|
|
data.port = buf.readUInt16BE(offset + 6)
|
428 |
|
|
data.target = name.decode(buf, offset + 8)
|
429 |
|
|
|
430 |
|
|
rsrv.decode.bytes = len + 2
|
431 |
|
|
return data
|
432 |
|
|
}
|
433 |
|
|
|
434 |
|
|
rsrv.decode.bytes = 0
|
435 |
|
|
|
436 |
|
|
rsrv.encodingLength = function (data) {
|
437 |
|
|
return 8 + name.encodingLength(data.target)
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
var rcaa = exports.caa = {}
|
441 |
|
|
|
442 |
|
|
rcaa.ISSUER_CRITICAL = 1 << 7
|
443 |
|
|
|
444 |
|
|
rcaa.encode = function (data, buf, offset) {
|
445 |
|
|
var len = rcaa.encodingLength(data)
|
446 |
|
|
|
447 |
|
|
if (!buf) buf = Buffer.allocUnsafe(rcaa.encodingLength(data))
|
448 |
|
|
if (!offset) offset = 0
|
449 |
|
|
|
450 |
|
|
if (data.issuerCritical) {
|
451 |
|
|
data.flags = rcaa.ISSUER_CRITICAL
|
452 |
|
|
}
|
453 |
|
|
|
454 |
|
|
buf.writeUInt16BE(len - 2, offset)
|
455 |
|
|
offset += 2
|
456 |
|
|
buf.writeUInt8(data.flags || 0, offset)
|
457 |
|
|
offset += 1
|
458 |
|
|
string.encode(data.tag, buf, offset)
|
459 |
|
|
offset += string.encode.bytes
|
460 |
|
|
buf.write(data.value, offset)
|
461 |
|
|
offset += Buffer.byteLength(data.value)
|
462 |
|
|
|
463 |
|
|
rcaa.encode.bytes = len
|
464 |
|
|
return buf
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
rcaa.encode.bytes = 0
|
468 |
|
|
|
469 |
|
|
rcaa.decode = function (buf, offset) {
|
470 |
|
|
if (!offset) offset = 0
|
471 |
|
|
|
472 |
|
|
var len = buf.readUInt16BE(offset)
|
473 |
|
|
offset += 2
|
474 |
|
|
|
475 |
|
|
var oldOffset = offset
|
476 |
|
|
var data = {}
|
477 |
|
|
data.flags = buf.readUInt8(offset)
|
478 |
|
|
offset += 1
|
479 |
|
|
data.tag = string.decode(buf, offset)
|
480 |
|
|
offset += string.decode.bytes
|
481 |
|
|
data.value = buf.toString('utf-8', offset, oldOffset + len)
|
482 |
|
|
|
483 |
|
|
data.issuerCritical = !!(data.flags & rcaa.ISSUER_CRITICAL)
|
484 |
|
|
|
485 |
|
|
rcaa.decode.bytes = len + 2
|
486 |
|
|
|
487 |
|
|
return data
|
488 |
|
|
}
|
489 |
|
|
|
490 |
|
|
rcaa.decode.bytes = 0
|
491 |
|
|
|
492 |
|
|
rcaa.encodingLength = function (data) {
|
493 |
|
|
return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2
|
494 |
|
|
}
|
495 |
|
|
|
496 |
|
|
var ra = exports.a = {}
|
497 |
|
|
|
498 |
|
|
ra.encode = function (host, buf, offset) {
|
499 |
|
|
if (!buf) buf = Buffer.allocUnsafe(ra.encodingLength(host))
|
500 |
|
|
if (!offset) offset = 0
|
501 |
|
|
|
502 |
|
|
buf.writeUInt16BE(4, offset)
|
503 |
|
|
offset += 2
|
504 |
|
|
ip.toBuffer(host, buf, offset)
|
505 |
|
|
ra.encode.bytes = 6
|
506 |
|
|
return buf
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
ra.encode.bytes = 0
|
510 |
|
|
|
511 |
|
|
ra.decode = function (buf, offset) {
|
512 |
|
|
if (!offset) offset = 0
|
513 |
|
|
|
514 |
|
|
offset += 2
|
515 |
|
|
var host = ip.toString(buf, offset, 4)
|
516 |
|
|
ra.decode.bytes = 6
|
517 |
|
|
return host
|
518 |
|
|
}
|
519 |
|
|
|
520 |
|
|
ra.decode.bytes = 0
|
521 |
|
|
|
522 |
|
|
ra.encodingLength = function () {
|
523 |
|
|
return 6
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
var raaaa = exports.aaaa = {}
|
527 |
|
|
|
528 |
|
|
raaaa.encode = function (host, buf, offset) {
|
529 |
|
|
if (!buf) buf = Buffer.allocUnsafe(raaaa.encodingLength(host))
|
530 |
|
|
if (!offset) offset = 0
|
531 |
|
|
|
532 |
|
|
buf.writeUInt16BE(16, offset)
|
533 |
|
|
offset += 2
|
534 |
|
|
ip.toBuffer(host, buf, offset)
|
535 |
|
|
raaaa.encode.bytes = 18
|
536 |
|
|
return buf
|
537 |
|
|
}
|
538 |
|
|
|
539 |
|
|
raaaa.encode.bytes = 0
|
540 |
|
|
|
541 |
|
|
raaaa.decode = function (buf, offset) {
|
542 |
|
|
if (!offset) offset = 0
|
543 |
|
|
|
544 |
|
|
offset += 2
|
545 |
|
|
var host = ip.toString(buf, offset, 16)
|
546 |
|
|
raaaa.decode.bytes = 18
|
547 |
|
|
return host
|
548 |
|
|
}
|
549 |
|
|
|
550 |
|
|
raaaa.decode.bytes = 0
|
551 |
|
|
|
552 |
|
|
raaaa.encodingLength = function () {
|
553 |
|
|
return 18
|
554 |
|
|
}
|
555 |
|
|
|
556 |
|
|
var renc = exports.record = function (type) {
|
557 |
|
|
switch (type.toUpperCase()) {
|
558 |
|
|
case 'A': return ra
|
559 |
|
|
case 'PTR': return rptr
|
560 |
|
|
case 'CNAME': return rcname
|
561 |
|
|
case 'DNAME': return rdname
|
562 |
|
|
case 'TXT': return rtxt
|
563 |
|
|
case 'NULL': return rnull
|
564 |
|
|
case 'AAAA': return raaaa
|
565 |
|
|
case 'SRV': return rsrv
|
566 |
|
|
case 'HINFO': return rhinfo
|
567 |
|
|
case 'CAA': return rcaa
|
568 |
|
|
case 'NS': return rns
|
569 |
|
|
case 'SOA': return rsoa
|
570 |
|
|
}
|
571 |
|
|
return runknown
|
572 |
|
|
}
|
573 |
|
|
|
574 |
|
|
var answer = exports.answer = {}
|
575 |
|
|
|
576 |
|
|
answer.encode = function (a, buf, offset) {
|
577 |
|
|
if (!buf) buf = Buffer.allocUnsafe(answer.encodingLength(a))
|
578 |
|
|
if (!offset) offset = 0
|
579 |
|
|
|
580 |
|
|
var oldOffset = offset
|
581 |
|
|
|
582 |
|
|
name.encode(a.name, buf, offset)
|
583 |
|
|
offset += name.encode.bytes
|
584 |
|
|
|
585 |
|
|
buf.writeUInt16BE(types.toType(a.type), offset)
|
586 |
|
|
|
587 |
|
|
var klass = a.class === undefined ? 1 : a.class
|
588 |
|
|
if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit
|
589 |
|
|
buf.writeUInt16BE(klass, offset + 2)
|
590 |
|
|
|
591 |
|
|
buf.writeUInt32BE(a.ttl || 0, offset + 4)
|
592 |
|
|
|
593 |
|
|
var enc = renc(a.type)
|
594 |
|
|
enc.encode(a.data, buf, offset + 8)
|
595 |
|
|
offset += 8 + enc.encode.bytes
|
596 |
|
|
|
597 |
|
|
answer.encode.bytes = offset - oldOffset
|
598 |
|
|
return buf
|
599 |
|
|
}
|
600 |
|
|
|
601 |
|
|
answer.encode.bytes = 0
|
602 |
|
|
|
603 |
|
|
answer.decode = function (buf, offset) {
|
604 |
|
|
if (!offset) offset = 0
|
605 |
|
|
|
606 |
|
|
var a = {}
|
607 |
|
|
var oldOffset = offset
|
608 |
|
|
|
609 |
|
|
a.name = name.decode(buf, offset)
|
610 |
|
|
offset += name.decode.bytes
|
611 |
|
|
a.type = types.toString(buf.readUInt16BE(offset))
|
612 |
|
|
a.class = buf.readUInt16BE(offset + 2)
|
613 |
|
|
a.ttl = buf.readUInt32BE(offset + 4)
|
614 |
|
|
|
615 |
|
|
a.flush = !!(a.class & FLUSH_MASK)
|
616 |
|
|
if (a.flush) a.class &= NOT_FLUSH_MASK
|
617 |
|
|
|
618 |
|
|
var enc = renc(a.type)
|
619 |
|
|
a.data = enc.decode(buf, offset + 8)
|
620 |
|
|
offset += 8 + enc.decode.bytes
|
621 |
|
|
|
622 |
|
|
answer.decode.bytes = offset - oldOffset
|
623 |
|
|
return a
|
624 |
|
|
}
|
625 |
|
|
|
626 |
|
|
answer.decode.bytes = 0
|
627 |
|
|
|
628 |
|
|
answer.encodingLength = function (a) {
|
629 |
|
|
return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(a.data)
|
630 |
|
|
}
|
631 |
|
|
|
632 |
|
|
var question = exports.question = {}
|
633 |
|
|
|
634 |
|
|
question.encode = function (q, buf, offset) {
|
635 |
|
|
if (!buf) buf = Buffer.allocUnsafe(question.encodingLength(q))
|
636 |
|
|
if (!offset) offset = 0
|
637 |
|
|
|
638 |
|
|
var oldOffset = offset
|
639 |
|
|
|
640 |
|
|
name.encode(q.name, buf, offset)
|
641 |
|
|
offset += name.encode.bytes
|
642 |
|
|
|
643 |
|
|
buf.writeUInt16BE(types.toType(q.type), offset)
|
644 |
|
|
offset += 2
|
645 |
|
|
|
646 |
|
|
buf.writeUInt16BE(q.class === undefined ? 1 : q.class, offset)
|
647 |
|
|
offset += 2
|
648 |
|
|
|
649 |
|
|
question.encode.bytes = offset - oldOffset
|
650 |
|
|
return q
|
651 |
|
|
}
|
652 |
|
|
|
653 |
|
|
question.encode.bytes = 0
|
654 |
|
|
|
655 |
|
|
question.decode = function (buf, offset) {
|
656 |
|
|
if (!offset) offset = 0
|
657 |
|
|
|
658 |
|
|
var oldOffset = offset
|
659 |
|
|
var q = {}
|
660 |
|
|
|
661 |
|
|
q.name = name.decode(buf, offset)
|
662 |
|
|
offset += name.decode.bytes
|
663 |
|
|
|
664 |
|
|
q.type = types.toString(buf.readUInt16BE(offset))
|
665 |
|
|
offset += 2
|
666 |
|
|
|
667 |
|
|
q.class = buf.readUInt16BE(offset)
|
668 |
|
|
offset += 2
|
669 |
|
|
|
670 |
|
|
var qu = !!(q.class & QU_MASK)
|
671 |
|
|
if (qu) q.class &= NOT_QU_MASK
|
672 |
|
|
|
673 |
|
|
question.decode.bytes = offset - oldOffset
|
674 |
|
|
return q
|
675 |
|
|
}
|
676 |
|
|
|
677 |
|
|
question.decode.bytes = 0
|
678 |
|
|
|
679 |
|
|
question.encodingLength = function (q) {
|
680 |
|
|
return name.encodingLength(q.name) + 4
|
681 |
|
|
}
|
682 |
|
|
|
683 |
|
|
exports.AUTHORITATIVE_ANSWER = 1 << 10
|
684 |
|
|
exports.TRUNCATED_RESPONSE = 1 << 9
|
685 |
|
|
exports.RECURSION_DESIRED = 1 << 8
|
686 |
|
|
exports.RECURSION_AVAILABLE = 1 << 7
|
687 |
|
|
exports.AUTHENTIC_DATA = 1 << 5
|
688 |
|
|
exports.CHECKING_DISABLED = 1 << 4
|
689 |
|
|
|
690 |
|
|
exports.encode = function (result, buf, offset) {
|
691 |
|
|
if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result))
|
692 |
|
|
if (!offset) offset = 0
|
693 |
|
|
|
694 |
|
|
var oldOffset = offset
|
695 |
|
|
|
696 |
|
|
if (!result.questions) result.questions = []
|
697 |
|
|
if (!result.answers) result.answers = []
|
698 |
|
|
if (!result.authorities) result.authorities = []
|
699 |
|
|
if (!result.additionals) result.additionals = []
|
700 |
|
|
|
701 |
|
|
header.encode(result, buf, offset)
|
702 |
|
|
offset += header.encode.bytes
|
703 |
|
|
|
704 |
|
|
offset = encodeList(result.questions, question, buf, offset)
|
705 |
|
|
offset = encodeList(result.answers, answer, buf, offset)
|
706 |
|
|
offset = encodeList(result.authorities, answer, buf, offset)
|
707 |
|
|
offset = encodeList(result.additionals, answer, buf, offset)
|
708 |
|
|
|
709 |
|
|
exports.encode.bytes = offset - oldOffset
|
710 |
|
|
|
711 |
|
|
return buf
|
712 |
|
|
}
|
713 |
|
|
|
714 |
|
|
exports.encode.bytes = 0
|
715 |
|
|
|
716 |
|
|
exports.decode = function (buf, offset) {
|
717 |
|
|
if (!offset) offset = 0
|
718 |
|
|
|
719 |
|
|
var oldOffset = offset
|
720 |
|
|
var result = header.decode(buf, offset)
|
721 |
|
|
offset += header.decode.bytes
|
722 |
|
|
|
723 |
|
|
offset = decodeList(result.questions, question, buf, offset)
|
724 |
|
|
offset = decodeList(result.answers, answer, buf, offset)
|
725 |
|
|
offset = decodeList(result.authorities, answer, buf, offset)
|
726 |
|
|
offset = decodeList(result.additionals, answer, buf, offset)
|
727 |
|
|
|
728 |
|
|
exports.decode.bytes = offset - oldOffset
|
729 |
|
|
|
730 |
|
|
return result
|
731 |
|
|
}
|
732 |
|
|
|
733 |
|
|
exports.decode.bytes = 0
|
734 |
|
|
|
735 |
|
|
exports.encodingLength = function (result) {
|
736 |
|
|
return header.encodingLength(result) +
|
737 |
|
|
encodingLengthList(result.questions || [], question) +
|
738 |
|
|
encodingLengthList(result.answers || [], answer) +
|
739 |
|
|
encodingLengthList(result.authorities || [], answer) +
|
740 |
|
|
encodingLengthList(result.additionals || [], answer)
|
741 |
|
|
}
|
742 |
|
|
|
743 |
|
|
function encodingLengthList (list, enc) {
|
744 |
|
|
var len = 0
|
745 |
|
|
for (var i = 0; i < list.length; i++) len += enc.encodingLength(list[i])
|
746 |
|
|
return len
|
747 |
|
|
}
|
748 |
|
|
|
749 |
|
|
function encodeList (list, enc, buf, offset) {
|
750 |
|
|
for (var i = 0; i < list.length; i++) {
|
751 |
|
|
enc.encode(list[i], buf, offset)
|
752 |
|
|
offset += enc.encode.bytes
|
753 |
|
|
}
|
754 |
|
|
return offset
|
755 |
|
|
}
|
756 |
|
|
|
757 |
|
|
function decodeList (list, enc, buf, offset) {
|
758 |
|
|
for (var i = 0; i < list.length; i++) {
|
759 |
|
|
list[i] = enc.decode(buf, offset)
|
760 |
|
|
offset += enc.decode.bytes
|
761 |
|
|
}
|
762 |
|
|
return offset
|
763 |
|
|
}
|