1 | 'use strict'
|
2 |
|
3 | const Buffer = require('buffer').Buffer
|
4 | const types = require('./types')
|
5 | const rcodes = require('./rcodes')
|
6 | const opcodes = require('./opcodes')
|
7 | const classes = require('./classes')
|
8 | const optioncodes = require('./optioncodes')
|
9 | const ip = require('@leichtgewicht/ip-codec')
|
10 |
|
11 | const QUERY_FLAG = 0
|
12 | const RESPONSE_FLAG = 1 << 15
|
13 | const FLUSH_MASK = 1 << 15
|
14 | const NOT_FLUSH_MASK = ~FLUSH_MASK
|
15 | const QU_MASK = 1 << 15
|
16 | const NOT_QU_MASK = ~QU_MASK
|
17 |
|
18 | const name = exports.txt = exports.name = {}
|
19 |
|
20 | name.encode = function (str, buf, offset) {
|
21 | if (!buf) buf = Buffer.alloc(name.encodingLength(str))
|
22 | if (!offset) offset = 0
|
23 | const oldOffset = offset
|
24 |
|
25 |
|
26 | const n = str.replace(/^\.|\.$/gm, '')
|
27 | if (n.length) {
|
28 | const list = n.split('.')
|
29 |
|
30 | for (let i = 0; i < list.length; i++) {
|
31 | const len = buf.write(list[i], offset + 1)
|
32 | buf[offset] = len
|
33 | offset += len + 1
|
34 | }
|
35 | }
|
36 |
|
37 | buf[offset++] = 0
|
38 |
|
39 | name.encode.bytes = offset - oldOffset
|
40 | return buf
|
41 | }
|
42 |
|
43 | name.encode.bytes = 0
|
44 |
|
45 | name.decode = function (buf, offset) {
|
46 | if (!offset) offset = 0
|
47 |
|
48 | const list = []
|
49 | const oldOffset = offset
|
50 | let len = buf[offset++]
|
51 |
|
52 | if (len === 0) {
|
53 | name.decode.bytes = 1
|
54 | return '.'
|
55 | }
|
56 | if (len >= 0xc0) {
|
57 | const res = name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000)
|
58 | name.decode.bytes = 2
|
59 | return res
|
60 | }
|
61 |
|
62 | while (len) {
|
63 | if (len >= 0xc0) {
|
64 | list.push(name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000))
|
65 | offset++
|
66 | break
|
67 | }
|
68 |
|
69 | list.push(buf.toString('utf-8', offset, offset + len))
|
70 | offset += len
|
71 | len = buf[offset++]
|
72 | }
|
73 |
|
74 | name.decode.bytes = offset - oldOffset
|
75 | return list.join('.')
|
76 | }
|
77 |
|
78 | name.decode.bytes = 0
|
79 |
|
80 | name.encodingLength = function (n) {
|
81 | if (n === '.' || n === '..') return 1
|
82 | return Buffer.byteLength(n.replace(/^\.|\.$/gm, '')) + 2
|
83 | }
|
84 |
|
85 | const string = {}
|
86 |
|
87 | string.encode = function (s, buf, offset) {
|
88 | if (!buf) buf = Buffer.alloc(string.encodingLength(s))
|
89 | if (!offset) offset = 0
|
90 |
|
91 | const len = buf.write(s, offset + 1)
|
92 | buf[offset] = len
|
93 | string.encode.bytes = len + 1
|
94 | return buf
|
95 | }
|
96 |
|
97 | string.encode.bytes = 0
|
98 |
|
99 | string.decode = function (buf, offset) {
|
100 | if (!offset) offset = 0
|
101 |
|
102 | const len = buf[offset]
|
103 | const s = buf.toString('utf-8', offset + 1, offset + 1 + len)
|
104 | string.decode.bytes = len + 1
|
105 | return s
|
106 | }
|
107 |
|
108 | string.decode.bytes = 0
|
109 |
|
110 | string.encodingLength = function (s) {
|
111 | return Buffer.byteLength(s) + 1
|
112 | }
|
113 |
|
114 | const header = {}
|
115 |
|
116 | header.encode = function (h, buf, offset) {
|
117 | if (!buf) buf = header.encodingLength(h)
|
118 | if (!offset) offset = 0
|
119 |
|
120 | const flags = (h.flags || 0) & 32767
|
121 | const type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG
|
122 |
|
123 | buf.writeUInt16BE(h.id || 0, offset)
|
124 | buf.writeUInt16BE(flags | type, offset + 2)
|
125 | buf.writeUInt16BE(h.questions.length, offset + 4)
|
126 | buf.writeUInt16BE(h.answers.length, offset + 6)
|
127 | buf.writeUInt16BE(h.authorities.length, offset + 8)
|
128 | buf.writeUInt16BE(h.additionals.length, offset + 10)
|
129 |
|
130 | return buf
|
131 | }
|
132 |
|
133 | header.encode.bytes = 12
|
134 |
|
135 | header.decode = function (buf, offset) {
|
136 | if (!offset) offset = 0
|
137 | if (buf.length < 12) throw new Error('Header must be 12 bytes')
|
138 | const flags = buf.readUInt16BE(offset + 2)
|
139 |
|
140 | return {
|
141 | id: buf.readUInt16BE(offset),
|
142 | type: flags & RESPONSE_FLAG ? 'response' : 'query',
|
143 | flags: flags & 32767,
|
144 | flag_qr: ((flags >> 15) & 0x1) === 1,
|
145 | opcode: opcodes.toString((flags >> 11) & 0xf),
|
146 | flag_aa: ((flags >> 10) & 0x1) === 1,
|
147 | flag_tc: ((flags >> 9) & 0x1) === 1,
|
148 | flag_rd: ((flags >> 8) & 0x1) === 1,
|
149 | flag_ra: ((flags >> 7) & 0x1) === 1,
|
150 | flag_z: ((flags >> 6) & 0x1) === 1,
|
151 | flag_ad: ((flags >> 5) & 0x1) === 1,
|
152 | flag_cd: ((flags >> 4) & 0x1) === 1,
|
153 | rcode: rcodes.toString(flags & 0xf),
|
154 | questions: new Array(buf.readUInt16BE(offset + 4)),
|
155 | answers: new Array(buf.readUInt16BE(offset + 6)),
|
156 | authorities: new Array(buf.readUInt16BE(offset + 8)),
|
157 | additionals: new Array(buf.readUInt16BE(offset + 10))
|
158 | }
|
159 | }
|
160 |
|
161 | header.decode.bytes = 12
|
162 |
|
163 | header.encodingLength = function () {
|
164 | return 12
|
165 | }
|
166 |
|
167 | const runknown = exports.unknown = {}
|
168 |
|
169 | runknown.encode = function (data, buf, offset) {
|
170 | if (!buf) buf = Buffer.alloc(runknown.encodingLength(data))
|
171 | if (!offset) offset = 0
|
172 |
|
173 | buf.writeUInt16BE(data.length, offset)
|
174 | data.copy(buf, offset + 2)
|
175 |
|
176 | runknown.encode.bytes = data.length + 2
|
177 | return buf
|
178 | }
|
179 |
|
180 | runknown.encode.bytes = 0
|
181 |
|
182 | runknown.decode = function (buf, offset) {
|
183 | if (!offset) offset = 0
|
184 |
|
185 | const len = buf.readUInt16BE(offset)
|
186 | const data = buf.slice(offset + 2, offset + 2 + len)
|
187 | runknown.decode.bytes = len + 2
|
188 | return data
|
189 | }
|
190 |
|
191 | runknown.decode.bytes = 0
|
192 |
|
193 | runknown.encodingLength = function (data) {
|
194 | return data.length + 2
|
195 | }
|
196 |
|
197 | const rns = exports.ns = {}
|
198 |
|
199 | rns.encode = function (data, buf, offset) {
|
200 | if (!buf) buf = Buffer.alloc(rns.encodingLength(data))
|
201 | if (!offset) offset = 0
|
202 |
|
203 | name.encode(data, buf, offset + 2)
|
204 | buf.writeUInt16BE(name.encode.bytes, offset)
|
205 | rns.encode.bytes = name.encode.bytes + 2
|
206 | return buf
|
207 | }
|
208 |
|
209 | rns.encode.bytes = 0
|
210 |
|
211 | rns.decode = function (buf, offset) {
|
212 | if (!offset) offset = 0
|
213 |
|
214 | const len = buf.readUInt16BE(offset)
|
215 | const dd = name.decode(buf, offset + 2)
|
216 |
|
217 | rns.decode.bytes = len + 2
|
218 | return dd
|
219 | }
|
220 |
|
221 | rns.decode.bytes = 0
|
222 |
|
223 | rns.encodingLength = function (data) {
|
224 | return name.encodingLength(data) + 2
|
225 | }
|
226 |
|
227 | const rsoa = exports.soa = {}
|
228 |
|
229 | rsoa.encode = function (data, buf, offset) {
|
230 | if (!buf) buf = Buffer.alloc(rsoa.encodingLength(data))
|
231 | if (!offset) offset = 0
|
232 |
|
233 | const oldOffset = offset
|
234 | offset += 2
|
235 | name.encode(data.mname, buf, offset)
|
236 | offset += name.encode.bytes
|
237 | name.encode(data.rname, buf, offset)
|
238 | offset += name.encode.bytes
|
239 | buf.writeUInt32BE(data.serial || 0, offset)
|
240 | offset += 4
|
241 | buf.writeUInt32BE(data.refresh || 0, offset)
|
242 | offset += 4
|
243 | buf.writeUInt32BE(data.retry || 0, offset)
|
244 | offset += 4
|
245 | buf.writeUInt32BE(data.expire || 0, offset)
|
246 | offset += 4
|
247 | buf.writeUInt32BE(data.minimum || 0, offset)
|
248 | offset += 4
|
249 |
|
250 | buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
251 | rsoa.encode.bytes = offset - oldOffset
|
252 | return buf
|
253 | }
|
254 |
|
255 | rsoa.encode.bytes = 0
|
256 |
|
257 | rsoa.decode = function (buf, offset) {
|
258 | if (!offset) offset = 0
|
259 |
|
260 | const oldOffset = offset
|
261 |
|
262 | const data = {}
|
263 | offset += 2
|
264 | data.mname = name.decode(buf, offset)
|
265 | offset += name.decode.bytes
|
266 | data.rname = name.decode(buf, offset)
|
267 | offset += name.decode.bytes
|
268 | data.serial = buf.readUInt32BE(offset)
|
269 | offset += 4
|
270 | data.refresh = buf.readUInt32BE(offset)
|
271 | offset += 4
|
272 | data.retry = buf.readUInt32BE(offset)
|
273 | offset += 4
|
274 | data.expire = buf.readUInt32BE(offset)
|
275 | offset += 4
|
276 | data.minimum = buf.readUInt32BE(offset)
|
277 | offset += 4
|
278 |
|
279 | rsoa.decode.bytes = offset - oldOffset
|
280 | return data
|
281 | }
|
282 |
|
283 | rsoa.decode.bytes = 0
|
284 |
|
285 | rsoa.encodingLength = function (data) {
|
286 | return 22 + name.encodingLength(data.mname) + name.encodingLength(data.rname)
|
287 | }
|
288 |
|
289 | const rtxt = exports.txt = {}
|
290 |
|
291 | rtxt.encode = function (data, buf, offset) {
|
292 | if (!Array.isArray(data)) data = [data]
|
293 | for (let i = 0; i < data.length; i++) {
|
294 | if (typeof data[i] === 'string') {
|
295 | data[i] = Buffer.from(data[i])
|
296 | }
|
297 | if (!Buffer.isBuffer(data[i])) {
|
298 | throw new Error('Must be a Buffer')
|
299 | }
|
300 | }
|
301 |
|
302 | if (!buf) buf = Buffer.alloc(rtxt.encodingLength(data))
|
303 | if (!offset) offset = 0
|
304 |
|
305 | const oldOffset = offset
|
306 | offset += 2
|
307 |
|
308 | data.forEach(function (d) {
|
309 | buf[offset++] = d.length
|
310 | d.copy(buf, offset, 0, d.length)
|
311 | offset += d.length
|
312 | })
|
313 |
|
314 | buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
315 | rtxt.encode.bytes = offset - oldOffset
|
316 | return buf
|
317 | }
|
318 |
|
319 | rtxt.encode.bytes = 0
|
320 |
|
321 | rtxt.decode = function (buf, offset) {
|
322 | if (!offset) offset = 0
|
323 | const oldOffset = offset
|
324 | let remaining = buf.readUInt16BE(offset)
|
325 | offset += 2
|
326 |
|
327 | let data = []
|
328 | while (remaining > 0) {
|
329 | const len = buf[offset++]
|
330 | --remaining
|
331 | if (remaining < len) {
|
332 | throw new Error('Buffer overflow')
|
333 | }
|
334 | data.push(buf.slice(offset, offset + len))
|
335 | offset += len
|
336 | remaining -= len
|
337 | }
|
338 |
|
339 | rtxt.decode.bytes = offset - oldOffset
|
340 | return data
|
341 | }
|
342 |
|
343 | rtxt.decode.bytes = 0
|
344 |
|
345 | rtxt.encodingLength = function (data) {
|
346 | if (!Array.isArray(data)) data = [data]
|
347 | let length = 2
|
348 | data.forEach(function (buf) {
|
349 | if (typeof buf === 'string') {
|
350 | length += Buffer.byteLength(buf) + 1
|
351 | } else {
|
352 | length += buf.length + 1
|
353 | }
|
354 | })
|
355 | return length
|
356 | }
|
357 |
|
358 | const rnull = exports.null = {}
|
359 |
|
360 | rnull.encode = function (data, buf, offset) {
|
361 | if (!buf) buf = Buffer.alloc(rnull.encodingLength(data))
|
362 | if (!offset) offset = 0
|
363 |
|
364 | if (typeof data === 'string') data = Buffer.from(data)
|
365 | if (!data) data = Buffer.alloc(0)
|
366 |
|
367 | const oldOffset = offset
|
368 | offset += 2
|
369 |
|
370 | const len = data.length
|
371 | data.copy(buf, offset, 0, len)
|
372 | offset += len
|
373 |
|
374 | buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
375 | rnull.encode.bytes = offset - oldOffset
|
376 | return buf
|
377 | }
|
378 |
|
379 | rnull.encode.bytes = 0
|
380 |
|
381 | rnull.decode = function (buf, offset) {
|
382 | if (!offset) offset = 0
|
383 | const oldOffset = offset
|
384 | const len = buf.readUInt16BE(offset)
|
385 |
|
386 | offset += 2
|
387 |
|
388 | const data = buf.slice(offset, offset + len)
|
389 | offset += len
|
390 |
|
391 | rnull.decode.bytes = offset - oldOffset
|
392 | return data
|
393 | }
|
394 |
|
395 | rnull.decode.bytes = 0
|
396 |
|
397 | rnull.encodingLength = function (data) {
|
398 | if (!data) return 2
|
399 | return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2
|
400 | }
|
401 |
|
402 | const rhinfo = exports.hinfo = {}
|
403 |
|
404 | rhinfo.encode = function (data, buf, offset) {
|
405 | if (!buf) buf = Buffer.alloc(rhinfo.encodingLength(data))
|
406 | if (!offset) offset = 0
|
407 |
|
408 | const oldOffset = offset
|
409 | offset += 2
|
410 | string.encode(data.cpu, buf, offset)
|
411 | offset += string.encode.bytes
|
412 | string.encode(data.os, buf, offset)
|
413 | offset += string.encode.bytes
|
414 | buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
415 | rhinfo.encode.bytes = offset - oldOffset
|
416 | return buf
|
417 | }
|
418 |
|
419 | rhinfo.encode.bytes = 0
|
420 |
|
421 | rhinfo.decode = function (buf, offset) {
|
422 | if (!offset) offset = 0
|
423 |
|
424 | const oldOffset = offset
|
425 |
|
426 | const data = {}
|
427 | offset += 2
|
428 | data.cpu = string.decode(buf, offset)
|
429 | offset += string.decode.bytes
|
430 | data.os = string.decode(buf, offset)
|
431 | offset += string.decode.bytes
|
432 | rhinfo.decode.bytes = offset - oldOffset
|
433 | return data
|
434 | }
|
435 |
|
436 | rhinfo.decode.bytes = 0
|
437 |
|
438 | rhinfo.encodingLength = function (data) {
|
439 | return string.encodingLength(data.cpu) + string.encodingLength(data.os) + 2
|
440 | }
|
441 |
|
442 | const rptr = exports.ptr = {}
|
443 | const rcname = exports.cname = rptr
|
444 | const rdname = exports.dname = rptr
|
445 |
|
446 | rptr.encode = function (data, buf, offset) {
|
447 | if (!buf) buf = Buffer.alloc(rptr.encodingLength(data))
|
448 | if (!offset) offset = 0
|
449 |
|
450 | name.encode(data, buf, offset + 2)
|
451 | buf.writeUInt16BE(name.encode.bytes, offset)
|
452 | rptr.encode.bytes = name.encode.bytes + 2
|
453 | return buf
|
454 | }
|
455 |
|
456 | rptr.encode.bytes = 0
|
457 |
|
458 | rptr.decode = function (buf, offset) {
|
459 | if (!offset) offset = 0
|
460 |
|
461 | const data = name.decode(buf, offset + 2)
|
462 | rptr.decode.bytes = name.decode.bytes + 2
|
463 | return data
|
464 | }
|
465 |
|
466 | rptr.decode.bytes = 0
|
467 |
|
468 | rptr.encodingLength = function (data) {
|
469 | return name.encodingLength(data) + 2
|
470 | }
|
471 |
|
472 | const rsrv = exports.srv = {}
|
473 |
|
474 | rsrv.encode = function (data, buf, offset) {
|
475 | if (!buf) buf = Buffer.alloc(rsrv.encodingLength(data))
|
476 | if (!offset) offset = 0
|
477 |
|
478 | buf.writeUInt16BE(data.priority || 0, offset + 2)
|
479 | buf.writeUInt16BE(data.weight || 0, offset + 4)
|
480 | buf.writeUInt16BE(data.port || 0, offset + 6)
|
481 | name.encode(data.target, buf, offset + 8)
|
482 |
|
483 | const len = name.encode.bytes + 6
|
484 | buf.writeUInt16BE(len, offset)
|
485 |
|
486 | rsrv.encode.bytes = len + 2
|
487 | return buf
|
488 | }
|
489 |
|
490 | rsrv.encode.bytes = 0
|
491 |
|
492 | rsrv.decode = function (buf, offset) {
|
493 | if (!offset) offset = 0
|
494 |
|
495 | const len = buf.readUInt16BE(offset)
|
496 |
|
497 | const data = {}
|
498 | data.priority = buf.readUInt16BE(offset + 2)
|
499 | data.weight = buf.readUInt16BE(offset + 4)
|
500 | data.port = buf.readUInt16BE(offset + 6)
|
501 | data.target = name.decode(buf, offset + 8)
|
502 |
|
503 | rsrv.decode.bytes = len + 2
|
504 | return data
|
505 | }
|
506 |
|
507 | rsrv.decode.bytes = 0
|
508 |
|
509 | rsrv.encodingLength = function (data) {
|
510 | return 8 + name.encodingLength(data.target)
|
511 | }
|
512 |
|
513 | const rcaa = exports.caa = {}
|
514 |
|
515 | rcaa.ISSUER_CRITICAL = 1 << 7
|
516 |
|
517 | rcaa.encode = function (data, buf, offset) {
|
518 | const len = rcaa.encodingLength(data)
|
519 |
|
520 | if (!buf) buf = Buffer.alloc(rcaa.encodingLength(data))
|
521 | if (!offset) offset = 0
|
522 |
|
523 | if (data.issuerCritical) {
|
524 | data.flags = rcaa.ISSUER_CRITICAL
|
525 | }
|
526 |
|
527 | buf.writeUInt16BE(len - 2, offset)
|
528 | offset += 2
|
529 | buf.writeUInt8(data.flags || 0, offset)
|
530 | offset += 1
|
531 | string.encode(data.tag, buf, offset)
|
532 | offset += string.encode.bytes
|
533 | buf.write(data.value, offset)
|
534 | offset += Buffer.byteLength(data.value)
|
535 |
|
536 | rcaa.encode.bytes = len
|
537 | return buf
|
538 | }
|
539 |
|
540 | rcaa.encode.bytes = 0
|
541 |
|
542 | rcaa.decode = function (buf, offset) {
|
543 | if (!offset) offset = 0
|
544 |
|
545 | const len = buf.readUInt16BE(offset)
|
546 | offset += 2
|
547 |
|
548 | const oldOffset = offset
|
549 | const data = {}
|
550 | data.flags = buf.readUInt8(offset)
|
551 | offset += 1
|
552 | data.tag = string.decode(buf, offset)
|
553 | offset += string.decode.bytes
|
554 | data.value = buf.toString('utf-8', offset, oldOffset + len)
|
555 |
|
556 | data.issuerCritical = !!(data.flags & rcaa.ISSUER_CRITICAL)
|
557 |
|
558 | rcaa.decode.bytes = len + 2
|
559 |
|
560 | return data
|
561 | }
|
562 |
|
563 | rcaa.decode.bytes = 0
|
564 |
|
565 | rcaa.encodingLength = function (data) {
|
566 | return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2
|
567 | }
|
568 |
|
569 | const rmx = exports.mx = {}
|
570 |
|
571 | rmx.encode = function (data, buf, offset) {
|
572 | if (!buf) buf = Buffer.alloc(rmx.encodingLength(data))
|
573 | if (!offset) offset = 0
|
574 |
|
575 | const oldOffset = offset
|
576 | offset += 2
|
577 | buf.writeUInt16BE(data.preference || 0, offset)
|
578 | offset += 2
|
579 | name.encode(data.exchange, buf, offset)
|
580 | offset += name.encode.bytes
|
581 |
|
582 | buf.writeUInt16BE(offset - oldOffset - 2, oldOffset)
|
583 | rmx.encode.bytes = offset - oldOffset
|
584 | return buf
|
585 | }
|
586 |
|
587 | rmx.encode.bytes = 0
|
588 |
|
589 | rmx.decode = function (buf, offset) {
|
590 | if (!offset) offset = 0
|
591 |
|
592 | const oldOffset = offset
|
593 |
|
594 | const data = {}
|
595 | offset += 2
|
596 | data.preference = buf.readUInt16BE(offset)
|
597 | offset += 2
|
598 | data.exchange = name.decode(buf, offset)
|
599 | offset += name.decode.bytes
|
600 |
|
601 | rmx.decode.bytes = offset - oldOffset
|
602 | return data
|
603 | }
|
604 |
|
605 | rmx.encodingLength = function (data) {
|
606 | return 4 + name.encodingLength(data.exchange)
|
607 | }
|
608 |
|
609 | const ra = exports.a = {}
|
610 |
|
611 | ra.encode = function (host, buf, offset) {
|
612 | if (!buf) buf = Buffer.alloc(ra.encodingLength(host))
|
613 | if (!offset) offset = 0
|
614 |
|
615 | buf.writeUInt16BE(4, offset)
|
616 | offset += 2
|
617 | ip.v4.encode(host, buf, offset)
|
618 | ra.encode.bytes = 6
|
619 | return buf
|
620 | }
|
621 |
|
622 | ra.encode.bytes = 0
|
623 |
|
624 | ra.decode = function (buf, offset) {
|
625 | if (!offset) offset = 0
|
626 |
|
627 | offset += 2
|
628 | const host = ip.v4.decode(buf, offset)
|
629 | ra.decode.bytes = 6
|
630 | return host
|
631 | }
|
632 |
|
633 | ra.decode.bytes = 0
|
634 |
|
635 | ra.encodingLength = function () {
|
636 | return 6
|
637 | }
|
638 |
|
639 | const raaaa = exports.aaaa = {}
|
640 |
|
641 | raaaa.encode = function (host, buf, offset) {
|
642 | if (!buf) buf = Buffer.alloc(raaaa.encodingLength(host))
|
643 | if (!offset) offset = 0
|
644 |
|
645 | buf.writeUInt16BE(16, offset)
|
646 | offset += 2
|
647 | ip.v6.encode(host, buf, offset)
|
648 | raaaa.encode.bytes = 18
|
649 | return buf
|
650 | }
|
651 |
|
652 | raaaa.encode.bytes = 0
|
653 |
|
654 | raaaa.decode = function (buf, offset) {
|
655 | if (!offset) offset = 0
|
656 |
|
657 | offset += 2
|
658 | const host = ip.v6.decode(buf, offset)
|
659 | raaaa.decode.bytes = 18
|
660 | return host
|
661 | }
|
662 |
|
663 | raaaa.decode.bytes = 0
|
664 |
|
665 | raaaa.encodingLength = function () {
|
666 | return 18
|
667 | }
|
668 |
|
669 | const roption = exports.option = {}
|
670 |
|
671 | roption.encode = function (option, buf, offset) {
|
672 | if (!buf) buf = Buffer.alloc(roption.encodingLength(option))
|
673 | if (!offset) offset = 0
|
674 | const oldOffset = offset
|
675 |
|
676 | const code = optioncodes.toCode(option.code)
|
677 | buf.writeUInt16BE(code, offset)
|
678 | offset += 2
|
679 | if (option.data) {
|
680 | buf.writeUInt16BE(option.data.length, offset)
|
681 | offset += 2
|
682 | option.data.copy(buf, offset)
|
683 | offset += option.data.length
|
684 | } else {
|
685 | switch (code) {
|
686 |
|
687 |
|
688 | case 8:
|
689 |
|
690 | const spl = option.sourcePrefixLength || 0
|
691 | const fam = option.family || ip.familyOf(option.ip)
|
692 | const ipBuf = ip.encode(option.ip, Buffer.alloc)
|
693 | const ipLen = Math.ceil(spl / 8)
|
694 | buf.writeUInt16BE(ipLen + 4, offset)
|
695 | offset += 2
|
696 | buf.writeUInt16BE(fam, offset)
|
697 | offset += 2
|
698 | buf.writeUInt8(spl, offset++)
|
699 | buf.writeUInt8(option.scopePrefixLength || 0, offset++)
|
700 |
|
701 | ipBuf.copy(buf, offset, 0, ipLen)
|
702 | offset += ipLen
|
703 | break
|
704 |
|
705 |
|
706 | case 11:
|
707 | if (option.timeout) {
|
708 | buf.writeUInt16BE(2, offset)
|
709 | offset += 2
|
710 | buf.writeUInt16BE(option.timeout, offset)
|
711 | offset += 2
|
712 | } else {
|
713 | buf.writeUInt16BE(0, offset)
|
714 | offset += 2
|
715 | }
|
716 | break
|
717 | case 12:
|
718 | const len = option.length || 0
|
719 | buf.writeUInt16BE(len, offset)
|
720 | offset += 2
|
721 | buf.fill(0, offset, offset + len)
|
722 | offset += len
|
723 | break
|
724 |
|
725 | case 14:
|
726 | const tagsLen = option.tags.length * 2
|
727 | buf.writeUInt16BE(tagsLen, offset)
|
728 | offset += 2
|
729 | for (const tag of option.tags) {
|
730 | buf.writeUInt16BE(tag, offset)
|
731 | offset += 2
|
732 | }
|
733 | break
|
734 | default:
|
735 | throw new Error(`Unknown roption code: ${option.code}`)
|
736 | }
|
737 | }
|
738 |
|
739 | roption.encode.bytes = offset - oldOffset
|
740 | return buf
|
741 | }
|
742 |
|
743 | roption.encode.bytes = 0
|
744 |
|
745 | roption.decode = function (buf, offset) {
|
746 | if (!offset) offset = 0
|
747 | const option = {}
|
748 | option.code = buf.readUInt16BE(offset)
|
749 | option.type = optioncodes.toString(option.code)
|
750 | offset += 2
|
751 | const len = buf.readUInt16BE(offset)
|
752 | offset += 2
|
753 | option.data = buf.slice(offset, offset + len)
|
754 | switch (option.code) {
|
755 |
|
756 | case 8:
|
757 | option.family = buf.readUInt16BE(offset)
|
758 | offset += 2
|
759 | option.sourcePrefixLength = buf.readUInt8(offset++)
|
760 | option.scopePrefixLength = buf.readUInt8(offset++)
|
761 | const padded = Buffer.alloc((option.family === 1) ? 4 : 16)
|
762 | buf.copy(padded, 0, offset, offset + len - 4)
|
763 | option.ip = ip.decode(padded)
|
764 | break
|
765 |
|
766 | case 11:
|
767 | if (len > 0) {
|
768 | option.timeout = buf.readUInt16BE(offset)
|
769 | offset += 2
|
770 | }
|
771 | break
|
772 | case 14:
|
773 | option.tags = []
|
774 | for (let i = 0; i < len; i += 2) {
|
775 | option.tags.push(buf.readUInt16BE(offset))
|
776 | offset += 2
|
777 | }
|
778 |
|
779 | }
|
780 |
|
781 | roption.decode.bytes = len + 4
|
782 | return option
|
783 | }
|
784 |
|
785 | roption.decode.bytes = 0
|
786 |
|
787 | roption.encodingLength = function (option) {
|
788 | if (option.data) {
|
789 | return option.data.length + 4
|
790 | }
|
791 | const code = optioncodes.toCode(option.code)
|
792 | switch (code) {
|
793 | case 8:
|
794 | const spl = option.sourcePrefixLength || 0
|
795 | return Math.ceil(spl / 8) + 8
|
796 | case 11:
|
797 | return (typeof option.timeout === 'number') ? 6 : 4
|
798 | case 12:
|
799 | return option.length + 4
|
800 | case 14:
|
801 | return 4 + (option.tags.length * 2)
|
802 | }
|
803 | throw new Error(`Unknown roption code: ${option.code}`)
|
804 | }
|
805 |
|
806 | const ropt = exports.opt = {}
|
807 |
|
808 | ropt.encode = function (options, buf, offset) {
|
809 | if (!buf) buf = Buffer.alloc(ropt.encodingLength(options))
|
810 | if (!offset) offset = 0
|
811 | const oldOffset = offset
|
812 |
|
813 | const rdlen = encodingLengthList(options, roption)
|
814 | buf.writeUInt16BE(rdlen, offset)
|
815 | offset = encodeList(options, roption, buf, offset + 2)
|
816 |
|
817 | ropt.encode.bytes = offset - oldOffset
|
818 | return buf
|
819 | }
|
820 |
|
821 | ropt.encode.bytes = 0
|
822 |
|
823 | ropt.decode = function (buf, offset) {
|
824 | if (!offset) offset = 0
|
825 | const oldOffset = offset
|
826 |
|
827 | const options = []
|
828 | let rdlen = buf.readUInt16BE(offset)
|
829 | offset += 2
|
830 | let o = 0
|
831 | while (rdlen > 0) {
|
832 | options[o++] = roption.decode(buf, offset)
|
833 | offset += roption.decode.bytes
|
834 | rdlen -= roption.decode.bytes
|
835 | }
|
836 | ropt.decode.bytes = offset - oldOffset
|
837 | return options
|
838 | }
|
839 |
|
840 | ropt.decode.bytes = 0
|
841 |
|
842 | ropt.encodingLength = function (options) {
|
843 | return 2 + encodingLengthList(options || [], roption)
|
844 | }
|
845 |
|
846 | const rdnskey = exports.dnskey = {}
|
847 |
|
848 | rdnskey.PROTOCOL_DNSSEC = 3
|
849 | rdnskey.ZONE_KEY = 0x80
|
850 | rdnskey.SECURE_ENTRYPOINT = 0x8000
|
851 |
|
852 | rdnskey.encode = function (key, buf, offset) {
|
853 | if (!buf) buf = Buffer.alloc(rdnskey.encodingLength(key))
|
854 | if (!offset) offset = 0
|
855 | const oldOffset = offset
|
856 |
|
857 | const keydata = key.key
|
858 | if (!Buffer.isBuffer(keydata)) {
|
859 | throw new Error('Key must be a Buffer')
|
860 | }
|
861 |
|
862 | offset += 2
|
863 | buf.writeUInt16BE(key.flags, offset)
|
864 | offset += 2
|
865 | buf.writeUInt8(rdnskey.PROTOCOL_DNSSEC, offset)
|
866 | offset += 1
|
867 | buf.writeUInt8(key.algorithm, offset)
|
868 | offset += 1
|
869 | keydata.copy(buf, offset, 0, keydata.length)
|
870 | offset += keydata.length
|
871 |
|
872 | rdnskey.encode.bytes = offset - oldOffset
|
873 | buf.writeUInt16BE(rdnskey.encode.bytes - 2, oldOffset)
|
874 | return buf
|
875 | }
|
876 |
|
877 | rdnskey.encode.bytes = 0
|
878 |
|
879 | rdnskey.decode = function (buf, offset) {
|
880 | if (!offset) offset = 0
|
881 | const oldOffset = offset
|
882 |
|
883 | var key = {}
|
884 | var length = buf.readUInt16BE(offset)
|
885 | offset += 2
|
886 | key.flags = buf.readUInt16BE(offset)
|
887 | offset += 2
|
888 | if (buf.readUInt8(offset) !== rdnskey.PROTOCOL_DNSSEC) {
|
889 | throw new Error('Protocol must be 3')
|
890 | }
|
891 | offset += 1
|
892 | key.algorithm = buf.readUInt8(offset)
|
893 | offset += 1
|
894 | key.key = buf.slice(offset, oldOffset + length + 2)
|
895 | offset += key.key.length
|
896 | rdnskey.decode.bytes = offset - oldOffset
|
897 | return key
|
898 | }
|
899 |
|
900 | rdnskey.decode.bytes = 0
|
901 |
|
902 | rdnskey.encodingLength = function (key) {
|
903 | return 6 + Buffer.byteLength(key.key)
|
904 | }
|
905 |
|
906 | const rrrsig = exports.rrsig = {}
|
907 |
|
908 | rrrsig.encode = function (sig, buf, offset) {
|
909 | if (!buf) buf = Buffer.alloc(rrrsig.encodingLength(sig))
|
910 | if (!offset) offset = 0
|
911 | const oldOffset = offset
|
912 |
|
913 | const signature = sig.signature
|
914 | if (!Buffer.isBuffer(signature)) {
|
915 | throw new Error('Signature must be a Buffer')
|
916 | }
|
917 |
|
918 | offset += 2
|
919 | buf.writeUInt16BE(types.toType(sig.typeCovered), offset)
|
920 | offset += 2
|
921 | buf.writeUInt8(sig.algorithm, offset)
|
922 | offset += 1
|
923 | buf.writeUInt8(sig.labels, offset)
|
924 | offset += 1
|
925 | buf.writeUInt32BE(sig.originalTTL, offset)
|
926 | offset += 4
|
927 | buf.writeUInt32BE(sig.expiration, offset)
|
928 | offset += 4
|
929 | buf.writeUInt32BE(sig.inception, offset)
|
930 | offset += 4
|
931 | buf.writeUInt16BE(sig.keyTag, offset)
|
932 | offset += 2
|
933 | name.encode(sig.signersName, buf, offset)
|
934 | offset += name.encode.bytes
|
935 | signature.copy(buf, offset, 0, signature.length)
|
936 | offset += signature.length
|
937 |
|
938 | rrrsig.encode.bytes = offset - oldOffset
|
939 | buf.writeUInt16BE(rrrsig.encode.bytes - 2, oldOffset)
|
940 | return buf
|
941 | }
|
942 |
|
943 | rrrsig.encode.bytes = 0
|
944 |
|
945 | rrrsig.decode = function (buf, offset) {
|
946 | if (!offset) offset = 0
|
947 | const oldOffset = offset
|
948 |
|
949 | var sig = {}
|
950 | var length = buf.readUInt16BE(offset)
|
951 | offset += 2
|
952 | sig.typeCovered = types.toString(buf.readUInt16BE(offset))
|
953 | offset += 2
|
954 | sig.algorithm = buf.readUInt8(offset)
|
955 | offset += 1
|
956 | sig.labels = buf.readUInt8(offset)
|
957 | offset += 1
|
958 | sig.originalTTL = buf.readUInt32BE(offset)
|
959 | offset += 4
|
960 | sig.expiration = buf.readUInt32BE(offset)
|
961 | offset += 4
|
962 | sig.inception = buf.readUInt32BE(offset)
|
963 | offset += 4
|
964 | sig.keyTag = buf.readUInt16BE(offset)
|
965 | offset += 2
|
966 | sig.signersName = name.decode(buf, offset)
|
967 | offset += name.decode.bytes
|
968 | sig.signature = buf.slice(offset, oldOffset + length + 2)
|
969 | offset += sig.signature.length
|
970 | rrrsig.decode.bytes = offset - oldOffset
|
971 | return sig
|
972 | }
|
973 |
|
974 | rrrsig.decode.bytes = 0
|
975 |
|
976 | rrrsig.encodingLength = function (sig) {
|
977 | return 20 +
|
978 | name.encodingLength(sig.signersName) +
|
979 | Buffer.byteLength(sig.signature)
|
980 | }
|
981 |
|
982 | const rrp = exports.rp = {}
|
983 |
|
984 | rrp.encode = function (data, buf, offset) {
|
985 | if (!buf) buf = Buffer.alloc(rrp.encodingLength(data))
|
986 | if (!offset) offset = 0
|
987 | const oldOffset = offset
|
988 |
|
989 | offset += 2
|
990 | name.encode(data.mbox || '.', buf, offset)
|
991 | offset += name.encode.bytes
|
992 | name.encode(data.txt || '.', buf, offset)
|
993 | offset += name.encode.bytes
|
994 | rrp.encode.bytes = offset - oldOffset
|
995 | buf.writeUInt16BE(rrp.encode.bytes - 2, oldOffset)
|
996 | return buf
|
997 | }
|
998 |
|
999 | rrp.encode.bytes = 0
|
1000 |
|
1001 | rrp.decode = function (buf, offset) {
|
1002 | if (!offset) offset = 0
|
1003 | const oldOffset = offset
|
1004 |
|
1005 | const data = {}
|
1006 | offset += 2
|
1007 | data.mbox = name.decode(buf, offset) || '.'
|
1008 | offset += name.decode.bytes
|
1009 | data.txt = name.decode(buf, offset) || '.'
|
1010 | offset += name.decode.bytes
|
1011 | rrp.decode.bytes = offset - oldOffset
|
1012 | return data
|
1013 | }
|
1014 |
|
1015 | rrp.decode.bytes = 0
|
1016 |
|
1017 | rrp.encodingLength = function (data) {
|
1018 | return 2 + name.encodingLength(data.mbox || '.') + name.encodingLength(data.txt || '.')
|
1019 | }
|
1020 |
|
1021 | const typebitmap = {}
|
1022 |
|
1023 | typebitmap.encode = function (typelist, buf, offset) {
|
1024 | if (!buf) buf = Buffer.alloc(typebitmap.encodingLength(typelist))
|
1025 | if (!offset) offset = 0
|
1026 | const oldOffset = offset
|
1027 |
|
1028 | var typesByWindow = []
|
1029 | for (var i = 0; i < typelist.length; i++) {
|
1030 | var typeid = types.toType(typelist[i])
|
1031 | if (typesByWindow[typeid >> 8] === undefined) {
|
1032 | typesByWindow[typeid >> 8] = []
|
1033 | }
|
1034 | typesByWindow[typeid >> 8][(typeid >> 3) & 0x1F] |= 1 << (7 - (typeid & 0x7))
|
1035 | }
|
1036 |
|
1037 | for (i = 0; i < typesByWindow.length; i++) {
|
1038 | if (typesByWindow[i] !== undefined) {
|
1039 | var windowBuf = Buffer.from(typesByWindow[i])
|
1040 | buf.writeUInt8(i, offset)
|
1041 | offset += 1
|
1042 | buf.writeUInt8(windowBuf.length, offset)
|
1043 | offset += 1
|
1044 | windowBuf.copy(buf, offset)
|
1045 | offset += windowBuf.length
|
1046 | }
|
1047 | }
|
1048 |
|
1049 | typebitmap.encode.bytes = offset - oldOffset
|
1050 | return buf
|
1051 | }
|
1052 |
|
1053 | typebitmap.encode.bytes = 0
|
1054 |
|
1055 | typebitmap.decode = function (buf, offset, length) {
|
1056 | if (!offset) offset = 0
|
1057 | const oldOffset = offset
|
1058 |
|
1059 | var typelist = []
|
1060 | while (offset - oldOffset < length) {
|
1061 | var window = buf.readUInt8(offset)
|
1062 | offset += 1
|
1063 | var windowLength = buf.readUInt8(offset)
|
1064 | offset += 1
|
1065 | for (var i = 0; i < windowLength; i++) {
|
1066 | var b = buf.readUInt8(offset + i)
|
1067 | for (var j = 0; j < 8; j++) {
|
1068 | if (b & (1 << (7 - j))) {
|
1069 | var typeid = types.toString((window << 8) | (i << 3) | j)
|
1070 | typelist.push(typeid)
|
1071 | }
|
1072 | }
|
1073 | }
|
1074 | offset += windowLength
|
1075 | }
|
1076 |
|
1077 | typebitmap.decode.bytes = offset - oldOffset
|
1078 | return typelist
|
1079 | }
|
1080 |
|
1081 | typebitmap.decode.bytes = 0
|
1082 |
|
1083 | typebitmap.encodingLength = function (typelist) {
|
1084 | var extents = []
|
1085 | for (var i = 0; i < typelist.length; i++) {
|
1086 | var typeid = types.toType(typelist[i])
|
1087 | extents[typeid >> 8] = Math.max(extents[typeid >> 8] || 0, typeid & 0xFF)
|
1088 | }
|
1089 |
|
1090 | var len = 0
|
1091 | for (i = 0; i < extents.length; i++) {
|
1092 | if (extents[i] !== undefined) {
|
1093 | len += 2 + Math.ceil((extents[i] + 1) / 8)
|
1094 | }
|
1095 | }
|
1096 |
|
1097 | return len
|
1098 | }
|
1099 |
|
1100 | const rnsec = exports.nsec = {}
|
1101 |
|
1102 | rnsec.encode = function (record, buf, offset) {
|
1103 | if (!buf) buf = Buffer.alloc(rnsec.encodingLength(record))
|
1104 | if (!offset) offset = 0
|
1105 | const oldOffset = offset
|
1106 |
|
1107 | offset += 2
|
1108 | name.encode(record.nextDomain, buf, offset)
|
1109 | offset += name.encode.bytes
|
1110 | typebitmap.encode(record.rrtypes, buf, offset)
|
1111 | offset += typebitmap.encode.bytes
|
1112 |
|
1113 | rnsec.encode.bytes = offset - oldOffset
|
1114 | buf.writeUInt16BE(rnsec.encode.bytes - 2, oldOffset)
|
1115 | return buf
|
1116 | }
|
1117 |
|
1118 | rnsec.encode.bytes = 0
|
1119 |
|
1120 | rnsec.decode = function (buf, offset) {
|
1121 | if (!offset) offset = 0
|
1122 | const oldOffset = offset
|
1123 |
|
1124 | var record = {}
|
1125 | var length = buf.readUInt16BE(offset)
|
1126 | offset += 2
|
1127 | record.nextDomain = name.decode(buf, offset)
|
1128 | offset += name.decode.bytes
|
1129 | record.rrtypes = typebitmap.decode(buf, offset, length - (offset - oldOffset))
|
1130 | offset += typebitmap.decode.bytes
|
1131 |
|
1132 | rnsec.decode.bytes = offset - oldOffset
|
1133 | return record
|
1134 | }
|
1135 |
|
1136 | rnsec.decode.bytes = 0
|
1137 |
|
1138 | rnsec.encodingLength = function (record) {
|
1139 | return 2 +
|
1140 | name.encodingLength(record.nextDomain) +
|
1141 | typebitmap.encodingLength(record.rrtypes)
|
1142 | }
|
1143 |
|
1144 | const rnsec3 = exports.nsec3 = {}
|
1145 |
|
1146 | rnsec3.encode = function (record, buf, offset) {
|
1147 | if (!buf) buf = Buffer.alloc(rnsec3.encodingLength(record))
|
1148 | if (!offset) offset = 0
|
1149 | const oldOffset = offset
|
1150 |
|
1151 | const salt = record.salt
|
1152 | if (!Buffer.isBuffer(salt)) {
|
1153 | throw new Error('salt must be a Buffer')
|
1154 | }
|
1155 |
|
1156 | const nextDomain = record.nextDomain
|
1157 | if (!Buffer.isBuffer(nextDomain)) {
|
1158 | throw new Error('nextDomain must be a Buffer')
|
1159 | }
|
1160 |
|
1161 | offset += 2
|
1162 | buf.writeUInt8(record.algorithm, offset)
|
1163 | offset += 1
|
1164 | buf.writeUInt8(record.flags, offset)
|
1165 | offset += 1
|
1166 | buf.writeUInt16BE(record.iterations, offset)
|
1167 | offset += 2
|
1168 | buf.writeUInt8(salt.length, offset)
|
1169 | offset += 1
|
1170 | salt.copy(buf, offset, 0, salt.length)
|
1171 | offset += salt.length
|
1172 | buf.writeUInt8(nextDomain.length, offset)
|
1173 | offset += 1
|
1174 | nextDomain.copy(buf, offset, 0, nextDomain.length)
|
1175 | offset += nextDomain.length
|
1176 | typebitmap.encode(record.rrtypes, buf, offset)
|
1177 | offset += typebitmap.encode.bytes
|
1178 |
|
1179 | rnsec3.encode.bytes = offset - oldOffset
|
1180 | buf.writeUInt16BE(rnsec3.encode.bytes - 2, oldOffset)
|
1181 | return buf
|
1182 | }
|
1183 |
|
1184 | rnsec3.encode.bytes = 0
|
1185 |
|
1186 | rnsec3.decode = function (buf, offset) {
|
1187 | if (!offset) offset = 0
|
1188 | const oldOffset = offset
|
1189 |
|
1190 | var record = {}
|
1191 | var length = buf.readUInt16BE(offset)
|
1192 | offset += 2
|
1193 | record.algorithm = buf.readUInt8(offset)
|
1194 | offset += 1
|
1195 | record.flags = buf.readUInt8(offset)
|
1196 | offset += 1
|
1197 | record.iterations = buf.readUInt16BE(offset)
|
1198 | offset += 2
|
1199 | const saltLength = buf.readUInt8(offset)
|
1200 | offset += 1
|
1201 | record.salt = buf.slice(offset, offset + saltLength)
|
1202 | offset += saltLength
|
1203 | const hashLength = buf.readUInt8(offset)
|
1204 | offset += 1
|
1205 | record.nextDomain = buf.slice(offset, offset + hashLength)
|
1206 | offset += hashLength
|
1207 | record.rrtypes = typebitmap.decode(buf, offset, length - (offset - oldOffset))
|
1208 | offset += typebitmap.decode.bytes
|
1209 |
|
1210 | rnsec3.decode.bytes = offset - oldOffset
|
1211 | return record
|
1212 | }
|
1213 |
|
1214 | rnsec3.decode.bytes = 0
|
1215 |
|
1216 | rnsec3.encodingLength = function (record) {
|
1217 | return 8 +
|
1218 | record.salt.length +
|
1219 | record.nextDomain.length +
|
1220 | typebitmap.encodingLength(record.rrtypes)
|
1221 | }
|
1222 |
|
1223 | const rds = exports.ds = {}
|
1224 |
|
1225 | rds.encode = function (digest, buf, offset) {
|
1226 | if (!buf) buf = Buffer.alloc(rds.encodingLength(digest))
|
1227 | if (!offset) offset = 0
|
1228 | const oldOffset = offset
|
1229 |
|
1230 | const digestdata = digest.digest
|
1231 | if (!Buffer.isBuffer(digestdata)) {
|
1232 | throw new Error('Digest must be a Buffer')
|
1233 | }
|
1234 |
|
1235 | offset += 2
|
1236 | buf.writeUInt16BE(digest.keyTag, offset)
|
1237 | offset += 2
|
1238 | buf.writeUInt8(digest.algorithm, offset)
|
1239 | offset += 1
|
1240 | buf.writeUInt8(digest.digestType, offset)
|
1241 | offset += 1
|
1242 | digestdata.copy(buf, offset, 0, digestdata.length)
|
1243 | offset += digestdata.length
|
1244 |
|
1245 | rds.encode.bytes = offset - oldOffset
|
1246 | buf.writeUInt16BE(rds.encode.bytes - 2, oldOffset)
|
1247 | return buf
|
1248 | }
|
1249 |
|
1250 | rds.encode.bytes = 0
|
1251 |
|
1252 | rds.decode = function (buf, offset) {
|
1253 | if (!offset) offset = 0
|
1254 | const oldOffset = offset
|
1255 |
|
1256 | var digest = {}
|
1257 | var length = buf.readUInt16BE(offset)
|
1258 | offset += 2
|
1259 | digest.keyTag = buf.readUInt16BE(offset)
|
1260 | offset += 2
|
1261 | digest.algorithm = buf.readUInt8(offset)
|
1262 | offset += 1
|
1263 | digest.digestType = buf.readUInt8(offset)
|
1264 | offset += 1
|
1265 | digest.digest = buf.slice(offset, oldOffset + length + 2)
|
1266 | offset += digest.digest.length
|
1267 | rds.decode.bytes = offset - oldOffset
|
1268 | return digest
|
1269 | }
|
1270 |
|
1271 | rds.decode.bytes = 0
|
1272 |
|
1273 | rds.encodingLength = function (digest) {
|
1274 | return 6 + Buffer.byteLength(digest.digest)
|
1275 | }
|
1276 |
|
1277 | const renc = exports.record = function (type) {
|
1278 | switch (type.toUpperCase()) {
|
1279 | case 'A': return ra
|
1280 | case 'PTR': return rptr
|
1281 | case 'CNAME': return rcname
|
1282 | case 'DNAME': return rdname
|
1283 | case 'TXT': return rtxt
|
1284 | case 'NULL': return rnull
|
1285 | case 'AAAA': return raaaa
|
1286 | case 'SRV': return rsrv
|
1287 | case 'HINFO': return rhinfo
|
1288 | case 'CAA': return rcaa
|
1289 | case 'NS': return rns
|
1290 | case 'SOA': return rsoa
|
1291 | case 'MX': return rmx
|
1292 | case 'OPT': return ropt
|
1293 | case 'DNSKEY': return rdnskey
|
1294 | case 'RRSIG': return rrrsig
|
1295 | case 'RP': return rrp
|
1296 | case 'NSEC': return rnsec
|
1297 | case 'NSEC3': return rnsec3
|
1298 | case 'DS': return rds
|
1299 | }
|
1300 | return runknown
|
1301 | }
|
1302 |
|
1303 | const answer = exports.answer = {}
|
1304 |
|
1305 | answer.encode = function (a, buf, offset) {
|
1306 | if (!buf) buf = Buffer.alloc(answer.encodingLength(a))
|
1307 | if (!offset) offset = 0
|
1308 |
|
1309 | const oldOffset = offset
|
1310 |
|
1311 | name.encode(a.name, buf, offset)
|
1312 | offset += name.encode.bytes
|
1313 |
|
1314 | buf.writeUInt16BE(types.toType(a.type), offset)
|
1315 |
|
1316 | if (a.type.toUpperCase() === 'OPT') {
|
1317 | if (a.name !== '.') {
|
1318 | throw new Error('OPT name must be root.')
|
1319 | }
|
1320 | buf.writeUInt16BE(a.udpPayloadSize || 4096, offset + 2)
|
1321 | buf.writeUInt8(a.extendedRcode || 0, offset + 4)
|
1322 | buf.writeUInt8(a.ednsVersion || 0, offset + 5)
|
1323 | buf.writeUInt16BE(a.flags || 0, offset + 6)
|
1324 |
|
1325 | offset += 8
|
1326 | ropt.encode(a.options || [], buf, offset)
|
1327 | offset += ropt.encode.bytes
|
1328 | } else {
|
1329 | let klass = classes.toClass(a.class === undefined ? 'IN' : a.class)
|
1330 | if (a.flush) klass |= FLUSH_MASK
|
1331 | buf.writeUInt16BE(klass, offset + 2)
|
1332 | buf.writeUInt32BE(a.ttl || 0, offset + 4)
|
1333 |
|
1334 | offset += 8
|
1335 | const enc = renc(a.type)
|
1336 | enc.encode(a.data, buf, offset)
|
1337 | offset += enc.encode.bytes
|
1338 | }
|
1339 |
|
1340 | answer.encode.bytes = offset - oldOffset
|
1341 | return buf
|
1342 | }
|
1343 |
|
1344 | answer.encode.bytes = 0
|
1345 |
|
1346 | answer.decode = function (buf, offset) {
|
1347 | if (!offset) offset = 0
|
1348 |
|
1349 | const a = {}
|
1350 | const oldOffset = offset
|
1351 |
|
1352 | a.name = name.decode(buf, offset)
|
1353 | offset += name.decode.bytes
|
1354 | a.type = types.toString(buf.readUInt16BE(offset))
|
1355 | if (a.type === 'OPT') {
|
1356 | a.udpPayloadSize = buf.readUInt16BE(offset + 2)
|
1357 | a.extendedRcode = buf.readUInt8(offset + 4)
|
1358 | a.ednsVersion = buf.readUInt8(offset + 5)
|
1359 | a.flags = buf.readUInt16BE(offset + 6)
|
1360 | a.flag_do = ((a.flags >> 15) & 0x1) === 1
|
1361 | a.options = ropt.decode(buf, offset + 8)
|
1362 | offset += 8 + ropt.decode.bytes
|
1363 | } else {
|
1364 | const klass = buf.readUInt16BE(offset + 2)
|
1365 | a.ttl = buf.readUInt32BE(offset + 4)
|
1366 |
|
1367 | a.class = classes.toString(klass & NOT_FLUSH_MASK)
|
1368 | a.flush = !!(klass & FLUSH_MASK)
|
1369 |
|
1370 | const enc = renc(a.type)
|
1371 | a.data = enc.decode(buf, offset + 8)
|
1372 | offset += 8 + enc.decode.bytes
|
1373 | }
|
1374 |
|
1375 | answer.decode.bytes = offset - oldOffset
|
1376 | return a
|
1377 | }
|
1378 |
|
1379 | answer.decode.bytes = 0
|
1380 |
|
1381 | answer.encodingLength = function (a) {
|
1382 | const data = (a.data !== null && a.data !== undefined) ? a.data : a.options
|
1383 | return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(data)
|
1384 | }
|
1385 |
|
1386 | const question = exports.question = {}
|
1387 |
|
1388 | question.encode = function (q, buf, offset) {
|
1389 | if (!buf) buf = Buffer.alloc(question.encodingLength(q))
|
1390 | if (!offset) offset = 0
|
1391 |
|
1392 | const oldOffset = offset
|
1393 |
|
1394 | name.encode(q.name, buf, offset)
|
1395 | offset += name.encode.bytes
|
1396 |
|
1397 | buf.writeUInt16BE(types.toType(q.type), offset)
|
1398 | offset += 2
|
1399 |
|
1400 | buf.writeUInt16BE(classes.toClass(q.class === undefined ? 'IN' : q.class), offset)
|
1401 | offset += 2
|
1402 |
|
1403 | question.encode.bytes = offset - oldOffset
|
1404 | return q
|
1405 | }
|
1406 |
|
1407 | question.encode.bytes = 0
|
1408 |
|
1409 | question.decode = function (buf, offset) {
|
1410 | if (!offset) offset = 0
|
1411 |
|
1412 | const oldOffset = offset
|
1413 | const q = {}
|
1414 |
|
1415 | q.name = name.decode(buf, offset)
|
1416 | offset += name.decode.bytes
|
1417 |
|
1418 | q.type = types.toString(buf.readUInt16BE(offset))
|
1419 | offset += 2
|
1420 |
|
1421 | q.class = classes.toString(buf.readUInt16BE(offset))
|
1422 | offset += 2
|
1423 |
|
1424 | const qu = !!(q.class & QU_MASK)
|
1425 | if (qu) q.class &= NOT_QU_MASK
|
1426 |
|
1427 | question.decode.bytes = offset - oldOffset
|
1428 | return q
|
1429 | }
|
1430 |
|
1431 | question.decode.bytes = 0
|
1432 |
|
1433 | question.encodingLength = function (q) {
|
1434 | return name.encodingLength(q.name) + 4
|
1435 | }
|
1436 |
|
1437 | exports.AUTHORITATIVE_ANSWER = 1 << 10
|
1438 | exports.TRUNCATED_RESPONSE = 1 << 9
|
1439 | exports.RECURSION_DESIRED = 1 << 8
|
1440 | exports.RECURSION_AVAILABLE = 1 << 7
|
1441 | exports.AUTHENTIC_DATA = 1 << 5
|
1442 | exports.CHECKING_DISABLED = 1 << 4
|
1443 | exports.DNSSEC_OK = 1 << 15
|
1444 |
|
1445 | exports.encode = function (result, buf, offset) {
|
1446 | const allocing = !buf
|
1447 |
|
1448 | if (allocing) buf = Buffer.alloc(exports.encodingLength(result))
|
1449 | if (!offset) offset = 0
|
1450 |
|
1451 | const oldOffset = offset
|
1452 |
|
1453 | if (!result.questions) result.questions = []
|
1454 | if (!result.answers) result.answers = []
|
1455 | if (!result.authorities) result.authorities = []
|
1456 | if (!result.additionals) result.additionals = []
|
1457 |
|
1458 | header.encode(result, buf, offset)
|
1459 | offset += header.encode.bytes
|
1460 |
|
1461 | offset = encodeList(result.questions, question, buf, offset)
|
1462 | offset = encodeList(result.answers, answer, buf, offset)
|
1463 | offset = encodeList(result.authorities, answer, buf, offset)
|
1464 | offset = encodeList(result.additionals, answer, buf, offset)
|
1465 |
|
1466 | exports.encode.bytes = offset - oldOffset
|
1467 |
|
1468 |
|
1469 | if (allocing && exports.encode.bytes !== buf.length) {
|
1470 | return buf.slice(0, exports.encode.bytes)
|
1471 | }
|
1472 |
|
1473 | return buf
|
1474 | }
|
1475 |
|
1476 | exports.encode.bytes = 0
|
1477 |
|
1478 | exports.decode = function (buf, offset) {
|
1479 | if (!offset) offset = 0
|
1480 |
|
1481 | const oldOffset = offset
|
1482 | const result = header.decode(buf, offset)
|
1483 | offset += header.decode.bytes
|
1484 |
|
1485 | offset = decodeList(result.questions, question, buf, offset)
|
1486 | offset = decodeList(result.answers, answer, buf, offset)
|
1487 | offset = decodeList(result.authorities, answer, buf, offset)
|
1488 | offset = decodeList(result.additionals, answer, buf, offset)
|
1489 |
|
1490 | exports.decode.bytes = offset - oldOffset
|
1491 |
|
1492 | return result
|
1493 | }
|
1494 |
|
1495 | exports.decode.bytes = 0
|
1496 |
|
1497 | exports.encodingLength = function (result) {
|
1498 | return header.encodingLength(result) +
|
1499 | encodingLengthList(result.questions || [], question) +
|
1500 | encodingLengthList(result.answers || [], answer) +
|
1501 | encodingLengthList(result.authorities || [], answer) +
|
1502 | encodingLengthList(result.additionals || [], answer)
|
1503 | }
|
1504 |
|
1505 | exports.streamEncode = function (result) {
|
1506 | const buf = exports.encode(result)
|
1507 | const sbuf = Buffer.alloc(2)
|
1508 | sbuf.writeUInt16BE(buf.byteLength)
|
1509 | const combine = Buffer.concat([sbuf, buf])
|
1510 | exports.streamEncode.bytes = combine.byteLength
|
1511 | return combine
|
1512 | }
|
1513 |
|
1514 | exports.streamEncode.bytes = 0
|
1515 |
|
1516 | exports.streamDecode = function (sbuf) {
|
1517 | const len = sbuf.readUInt16BE(0)
|
1518 | if (sbuf.byteLength < len + 2) {
|
1519 |
|
1520 | return null
|
1521 | }
|
1522 | const result = exports.decode(sbuf.slice(2))
|
1523 | exports.streamDecode.bytes = exports.decode.bytes
|
1524 | return result
|
1525 | }
|
1526 |
|
1527 | exports.streamDecode.bytes = 0
|
1528 |
|
1529 | function encodingLengthList (list, enc) {
|
1530 | let len = 0
|
1531 | for (let i = 0; i < list.length; i++) len += enc.encodingLength(list[i])
|
1532 | return len
|
1533 | }
|
1534 |
|
1535 | function encodeList (list, enc, buf, offset) {
|
1536 | for (let i = 0; i < list.length; i++) {
|
1537 | enc.encode(list[i], buf, offset)
|
1538 | offset += enc.encode.bytes
|
1539 | }
|
1540 | return offset
|
1541 | }
|
1542 |
|
1543 | function decodeList (list, enc, buf, offset) {
|
1544 | for (let i = 0; i < list.length; i++) {
|
1545 | list[i] = enc.decode(buf, offset)
|
1546 | offset += enc.decode.bytes
|
1547 | }
|
1548 | return offset
|
1549 | }
|