source: trip-planner-front/node_modules/dns-packet/index.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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