/
ndns.js
2197 lines (1920 loc) · 54.1 KB
/
ndns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var util = require('util');
var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
if(debugLevel & 0x4) {
debug = function (x) { util.error('NDNS: ' + x); };
} else {
debug = function () { };
}
var dgram = require('dgram');
var events = require('events');
var Buffer = require('buffer').Buffer;
var FreeList = require('freelist').FreeList;
var ns_packsiz = 512; // Default UDP Packet size
var ns_maxdname = 1025; // Maximum domain name
var ns_maxmsg = 65535; // Maximum message size
var ns_maxcdname = 255; // Maximum compressed domain name
var ns_maxlabel = 63; // Maximum compressed domain label
var ns_hfixedsz = 12; // Bytes of fixed data in header
var ns_qfixedsz = 4; // Bytes of fixed data in query
var ns_rrfixedsz = 10; // Bytes of fixed data in r record
var ns_int32sz = 4; // Bytes of data in a u_int32_t
var ns_int16sz = 2; // Bytes of data in a u_int16_t
var ns_int8sz = 1; // Bytes of data in a u_int8_t
var ns_inaddrsz = 4; // IPv4 T_A
var ns_in6addrsz = 16; // IPv6 T_AAAA
var ns_cmprsflgs = 0xc0;// Flag bits indicating name compression.
var ns_defaultport = 53;// For both UDP and TCP.
function _enum (obj) {
for (key in obj) {
global[key] = obj[key];
}
return obj;
}
var ns_sect = _enum({
'ns_s_qd': 0, // Query: Question.
'ns_s_zn': 0, // Update: Zone.
'ns_s_an': 1, // Query: Answer.
'ns_s_pr': 1, // Update: Prerequisites.
'ns_s_ns': 2, // Query: Name servers.
'ns_s_ud': 2, // Query: Update.
'ns_s_ar': 3, // Query|Update: Additional records.
'ns_s_max': 4,
});
var ns_flag = _enum({
'ns_f_qr': 0, // Question/Response.
'ns_f_opcode': 1, // Operation code.
'ns_f_aa': 2, // Authorative Answer.
'ns_f_tc': 3, // Truncation occured.
'ns_f_rd': 4, // Recursion Desired.
'ns_f_ra': 5, // Recursion Available.
'ns_f_z': 6, // MBZ
'ns_f_ad': 7, // Authentic Data (DNSSEC)
'ns_f_cd': 8, // Checking Disabled (DNSSEC)
'ns_f_rcode': 9, // Response code.
'ns_f_max': 10,
});
// Currently defined opcodes.
var ns_opcode = _enum({
'ns_o_query': 0, // Standard query.
'ns_o_iquery': 1, // Inverse query (deprecated/unsupported).
'ns_o_status': 2, // Name server status query (unsupported).
// Opcode 3 is undefined/reserved
'ns_o_notify': 4, // Zone change notification.
'ns_o_update': 5, // Zone update message.
});
// Currently defined response codes
var ns_rcode = _enum({
'ns_r_noerror': 0, // No error occured.
'ns_r_formerr': 1, // Format error.
'ns_r_servfail': 2, // Server failure.
'ns_r_nxdomain': 3, // Name error.
'ns_r_notimpl': 4, // Unimplemented.
'ns_r_refused': 5, // Operation refused.
// These are for BIND_UPDATE
'ns_r_yxdomain': 6, // Name exists
'ns_r_yxrrset': 7, // RRset exists
'ns_r_nxrrset': 8, // RRset does not exist
'ns_r_notauth': 9, // Not authoritative for zone
'ns_r_notzone': 10, // Zone of record different from zone section
'ns_r_max': 11,
// The following are EDNS extended rcodes
'ns_r_badvers': 16,
// The following are TSIG errors
'ns_r_badsig': 16,
'ns_r_badkey': 17,
'ns_r_badtime': 18,
});
// BIND_UPDATE
var ns_update_operation = _enum({
'ns_oup_delete': 0,
'ns_oup_add': 1,
'ns_oup_max': 2,
});
var NS_TSIG = _enum({
'NS_TSIG_FUDGE': 300,
'NS_TSIG_TCP_COUNT': 100,
'NS_TSIG_ALG_HMAC_MD5': "HMAC-MD5.SIG-ALG.REG.INT",
'NS_TSIG_ERROR_NO_TSIG': -10,
'NS_TSIG_ERROR_NO_SPACE': -11,
'NS_TSIG_ERROR_FORMERR': -12,
});
// Currently defined type values for resources and queries.
var ns_type = _enum({
'ns_t_invalid': 0, // Cookie.
'ns_t_a': 1, // Host address.
'ns_t_ns': 2, // Authoritative server.
'ns_t_md': 3, // Mail destination.
'ns_t_mf': 4, // Mail forwarder.
'ns_t_cname': 5, // Canonical name.
'ns_t_soa': 6, // Start of authority zone.
'ns_t_mb': 7, // Mailbox domain name.
'ns_t_mg': 8, // Mail group member.
'ns_t_mr': 9, // Mail rename name.
'ns_t_null': 10, // Null resource record.
'ns_t_wks': 11, // Well known service.
'ns_t_ptr': 12, // Domain name pointer.
'ns_t_hinfo': 13, // Host information.
'ns_t_minfo': 14, // Mailbox information.
'ns_t_mx': 15, // Mail routing information.
'ns_t_txt': 16, // Text strings.
'ns_t_rp': 17, // Responsible person.
'ns_t_afsdb': 18, // AFS cell database.
'ns_t_x25': 19, // X_25 calling address.
'ns_t_isdn': 20, // ISDN calling address.
'ns_t_rt': 21, // Router.
'ns_t_nsap': 22, // NSAP address.
'ns_t_ns_nsap_ptr': 23, // Reverse NSAP lookup (deprecated)
'ns_t_sig': 24, // Security signature.
'ns_t_key': 25, // Security key.
'ns_t_px': 26, // X.400 mail mapping.
'ns_t_gpos': 27, // Geographical position (withdrawn).
'ns_t_aaaa': 28, // Ip6 Address.
'ns_t_loc': 29, // Location Information.
'ns_t_nxt': 30, // Next domain (security)
'ns_t_eid': 31, // Endpoint identifier.
'ns_t_nimloc': 32, // Nimrod Locator.
'ns_t_srv': 33, // Server Selection.
'ns_t_atma': 34, // ATM Address
'ns_t_naptr': 35, // Naming Authority PoinTeR
'ns_t_kx': 36, // Key Exchange
'ns_t_cert': 37, // Certification Record
'ns_t_a6': 38, // IPv6 Address (deprecated, use ns_t_aaaa)
'ns_t_dname': 39, // Non-terminal DNAME (for IPv6)
'ns_t_sink': 40, // Kitchen sink (experimental)
'ns_t_opt': 41, // EDNS0 option (meta-RR)
'ns_t_apl': 42, // Address prefix list (RFC3123)
'ns_t_ds': 43, // Delegation Signer
'ns_t_sshfp': 44, // SSH Fingerprint
'ns_t_ipseckey': 45,// IPSEC Key
'ns_t_rrsig': 46, // RRSet Signature
'ns_t_nsec': 47, // Negative Security
'ns_t_dnskey': 48, // DNS Key
'ns_t_dhcid': 49, // Dynamic host configuartion identifier
'ns_t_nsec3': 50, // Negative security type 3
'ns_t_nsec3param': 51, // Negative security type 3 parameters
'ns_t_hip': 55, // Host Identity Protocol
'ns_t_spf': 99, // Sender Policy Framework
'ns_t_tkey': 249, // Transaction key
'ns_t_tsig': 250, // Transaction signature.
'ns_t_ixfr': 251, // Incremental zone transfer.
'ns_t_axfr': 252, // Transfer zone of authority.
'ns_t_mailb': 253, // Transfer mailbox records.
'ns_t_maila': 254, // Transfer mail agent records.
'ns_t_any': 255, // Wildcard match.
'ns_t_zxfr': 256, // BIND-specific, nonstandard.
'ns_t_dlv': 32769, // DNSSEC look-aside validation.
'ns_t_max': 65536
});
exports.ns_type = ns_type;
// Values for class field
var ns_class = _enum({
'ns_c_invalid': 0, // Cookie.
'ns_c_in': 1, // Internet.
'ns_c_2': 2, // unallocated/unsupported.
'ns_c_chaos': 3, // MIT Chaos-net.
'ns_c_hs': 4, // MIT Hesoid.
// Query class values which do not appear in resource records
'ns_c_none': 254, // for prereq. sections in update requests
'ns_c_any': 255, // Wildcard match.
'ns_c_max': 65535,
});
exports.ns_class = ns_class;
// DNSSEC constants.
var ns_key_types = _enum({
'ns_kt_rsa': 1, // key type RSA/MD5
'ns_kt_dh': 2, // Diffie Hellman
'ns_kt_dsa': 3, // Digital Signature Standard (MANDATORY)
'ns_kt_private': 4 // Private key type starts with OID
});
var ns_cert_type = _enum({
'cert_t_pkix': 1, // PKIX (X.509v3)
'cert_t_spki': 2, // SPKI
'cert_t_pgp': 3, // PGP
'cert_t_url': 253, // URL private type
'cert_t_oid': 254 // OID private type
});
// Flags field of the KEY RR rdata
var ns_type_elt = 0x40; //edns0 extended label type
var dns_labeltype_bitstring = 0x41;
var digitvalue = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 48
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 64
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96
-1, 12, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 256
];
var hexvalue = [
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f",
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f",
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f",
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f",
"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af",
"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf",
"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf",
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df",
"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef",
"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff",
];
var digits = "0123456789";
var ns_flagdata = [
{ mask: 0x8000, shift: 15 }, // qr.
{ mask: 0x7800, shift: 11 }, // opcode.
{ mask: 0x0400, shift: 10 }, // aa.
{ mask: 0x0200, shift: 9 }, // tc.
{ mask: 0x0100, shift: 8 }, // rd.
{ mask: 0x0080, shift: 7 }, // ra.
{ mask: 0x0040, shift: 6 }, // z.
{ mask: 0x0020, shift: 5 }, // ad.
{ mask: 0x0010, shift: 4 }, // cd.
{ mask: 0x000f, shift: 0 }, // rcode.
{ mask: 0x0000, shift: 0 }, // expansion (1/6).
{ mask: 0x0000, shift: 0 }, // expansion (2/6).
{ mask: 0x0000, shift: 0 }, // expansion (3/6).
{ mask: 0x0000, shift: 0 }, // expansion (4/6).
{ mask: 0x0000, shift: 0 }, // expansion (5/6).
{ mask: 0x0000, shift: 0 }, // expansion (6/6).
];
var res_opcodes = [
"QUERY",
"IQUERY",
"CQUERYM",
"CQUERYU", // experimental
"NOTIFY", // experimental
"UPDATE",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"ZONEINIT",
"ZONEREF",
];
var res_sectioncodes = [
"ZONE",
"PREREQUISITES",
"UPDATE",
"ADDITIONAL",
];
var p_class_syms = {
1: "IN",
3: "CHAOS",
4: "HESOID",
254: "ANY",
255: "NONE"
};
var p_default_section_syms = {
0: "QUERY",
1: "ANSWER",
2: "AUTHORITY",
3: "ADDITIONAL"
};
var p_key_syms = {
1: ["RSA", "RSA KEY with MD5 hash"],
2: ["DH", "Diffie Hellman"],
3: ["DSA", "Digital Signature Algorithm"],
4: ["PRIVATE", "Algorithm obtained from OID"]
};
var p_cert_syms = {
1: ["PKIX", "PKIX (X.509v3) Certificate"],
2: ["SKPI", "SPKI Certificate"],
3: ["PGP", "PGP Certificate"],
253: ["URL", "URL Private"],
254: ["OID", "OID Private"]
};
var p_type_syms = {
1: "A",
2: "NS",
3: "MD",
4: "MF",
5: "CNAME",
6: "SOA",
7: "MB",
8: "MG",
9: "MR",
10: "NULL",
11: "WKS",
12: "PTR",
13: "HINFO",
14: "MINFO",
15: "MX",
16: "TXT",
17: "RP",
18: "AFSDB",
19: "X25",
20: "ISDN",
21: "RT",
22: "NSAP",
23: "NSAP_PTR",
24: "SIG",
25: "KEY",
26: "PX",
27: "GPOS",
28: "AAAA",
29: "LOC",
30: "NXT",
31: "EID",
32: "NIMLOC",
33: "SRV",
34: "ATMA",
35: "NAPTR",
36: "KX",
37: "CERT",
38: "A6",
39: "DNAME",
40: "SINK",
41: "OPT",
42: "APL",
43: "DS",
44: "SSHFP",
45: "IPSECKEY",
46: "RRSIG",
47: "NSEC",
48: "DNSKEY",
49: "DHCID",
50: "NSEC3",
51: "NSEC3PARAM",
55: "HIP",
99: "SPF",
249: "TKEY",
250: "TSIG",
251: "IXFR",
252: "AXFR",
253: "MAILB",
254: "MAILA",
255: "ANY",
32769: "DLV",
256: "ZXFR",
};
var p_rcode_syms = {
0: ["NOERROR", "no error"],
1: ["FORMERR", "format error"],
2: ["SERVFAIL", "server failed"],
3: ["NXDOMAIN", "no such domain name"],
4: ["NOTIMP", "not implemented"],
5: ["REFUSED", "refused"],
// These are for BIND_UPDATE
6: ["YXDOMAIN", "domain name exist"],
7: ["YXRRSET", "rrset exists"],
8: ["NXRRSET", "rrset doesn't exist"],
9: ["NOTAUTH", "not authoritative"],
10: ["NOTZONE", "not in zone"],
11: ["", ""],
// The following are EDNS extended rcodes
// The following are TSIG errors
16: ["BADSIG", "bad signature"],
17: ["BADKEY", "bad key"],
18: ["BADTIME", "bad time"]
};
var n_type_syms = {};
for (var k in p_type_syms)
n_type_syms[p_type_syms[k]] = k;
var n_class_syms = {};
for (var k in p_class_syms)
n_class_syms[p_class_syms[k]] = k;
function Ptr () {
this.p = (arguments.length == 1) ? arguments[0] : null;
}
exports.Ptr = Ptr;
Ptr.prototype.get = function () {
return this.p;
};
Ptr.prototype.set = function (val) {
return this.p = val;
};
function ns_name_ntop(src, dst, dstsiz) {
var cp;
var dn, eom;
var c;
var n;
var l;
cp = 0;
dn = 0;
eom = dstsiz;
while((n = src[cp++]) != 0) {
if((n & ns_cmprsflgs) == ns_cmprsflgs) {
/* some kind of compression pointer */
errno.set('EMSGSIZE');
return (-1);
}
if(dn != 0) {
if(dn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn++] = 0x2e; /* '.' */
}
if ((l = labellen(src, cp - 1)) < 0) {
errno.set('EMSGSIZE');
return (-1);
}
if(dn + l >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
if((n & ns_cmprsflgs) == ns_type_elt) {
var m;
if(n != dns_labeltype_bitstring) {
/* labellen should reject this case */
return (-1);
}
var cpp = new Ptr(cp);
if ((m = decode_bitstring(src, cpp, dst, dn, eom)) < 0) {
errno.set('EMSGSIZE');
return (-1);
}
cp = cpp.get();
dn += m;
continue;
}
for(; l > 0; l--) {
c = src[cp++];
if(special(c)) {
if(dn + 1 >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn++] = 0x5c; /* '\\' */
dst[dn++] = c;
}
else if(!printable(c)) {
if(dn + 3 >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn++] = 0x5c; /* '\\' */
dst[dn++] = digits[c / 100];
dst[dn++] = digits[(c % 100) / 10];
dst[dn++] = digits[c % 10];
}
else {
if(dn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn++] = c;
}
}
}
if (dn == 0) {
if (dn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn++] = 0x2e; // '.'
}
if (dn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[dn] = 0;
return (dn);
}
exports.ns_name_ntop = ns_name_ntop;
function ns_name_pton (src, dst, dstsiz) {
return ns_name_pton2(src, dst, dstsiz, null);
}
exports.ns_name_pton = ns_name_pton;
function ns_name_pton2(src, dst, dstsiz, dstlenp) {
var label, bp, epm
var c, n, escaped, e = 0;
var cp;
escaped = 0;
bp = 0;
eom = dstsiz;
label = bp++;
var srcn = 0;
var done = false; // instead of goto
while ((c = src[srcn++]) != 0) {
if (escaped) {
if (c == 91) { // '['; start a bit string label
if ((cp = strchr(src, srcn, 93)) == null) { // ']'
errno.set('EINVAL');
return(-1);
}
var srcp = new Ptr(srcn);
var bpp = new Ptr(bp);
var labelp = new Ptr(label);
if ((e = encode_bitstring (src, srcp, cp + 2,
labelp, dst, bpp, eom)
!= 0)) {
errno.set(e);
return(-1);
}
label = labelp.get();
bp = bpp.get();
srcn = srcp.get();
escaped = 0;
label = bp++;
if ((c = src[srcn++]) == 0) {
done = true;
break;
}
}
else if ((cp = digits.indexOf(String.fromCharCode(c))) != -1) {
n = (cp * 100);
if ((c = src[srcn++]) ||
(cp = digits.indexOf(String.fromCharCode(c))) == -1) {
errno.set('EMSGSIZE');
return (-1);
}
n += (cp) * 10;
if ((c = src[srcn++]) == 0 ||
(cp = digits.indexOf(String.fromCharCode(c))) == -1) {
errno.set('EMSGSIZE');
return (-1);
}
n += cp;
if (n > 255) {
errno.set('EMSGSIZE');
return (-1);
}
c = n;
}
escaped = 0;
} else if (c == 92) { // '\\'
escaped = 1;
continue;
} else if (c == 46) { // '.'
c = (bp - label - 1)
if ((c & ns_cmprsflgs) != 0) { // label too big
errno.set('EMSGSIZE');
return (-1);
}
if (label >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[label] = c;
// Fully qualified?
if (src[srcn] == 0) {
if (c != 0) {
if (bp >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[bp++] = 0;
}
if ((bp) > ns_maxcdname) {
errno.set('EMSGSIZE');
return (-1);
}
if (dstlenp != null) {
dstlenp.set(bp);
}
return (1);
}
if (c == 0 || src[srcn] == 46) { // '.'
errno.set('EMSGSIZE');
return (-1);
}
label = bp++;
continue;
}
if (bp >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[bp++] = c;
}
if (!done) {
c = (bp - label - 1);
if ((c & ns_cmprsflgs) != 0) {
errno.set('EMSGSIZE');
return (-1);
}
}
// done:
if (label >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[label] = c;
if (c != 0) {
if (bp >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
dst[bp++] = 0;
}
if (bp > ns_maxcdname) { // src too big
errno.set('EMSGSIZE');
return (-1);
}
if (dstlenp != null) {
dstlenp.set(bp);
}
return (0);
}
function strchr (src, off, n) {
while (off < buf.length && buf[off] != 0) {
if (buf[off] == n)
return off;
off++;
}
return null;
}
function ns_name_unpack (msg, offset, len, dst, dstsiz) {
return ns_name_unpack2 (msg, offset, len, dst, dstsiz, null);
}
exports.ns_name_unpack = ns_name_unpack;
function ns_name_unpack2 (msg, offset, len, dst, dstsiz, dstlenp) {
var n, l;
var llen = -1;
var checked = 0;
var dstn = 0;
var srcn = offset;
var dstlim = dstsiz;
var eom = offset + len;
if(srcn < 0 || srcn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
/* Fetch next label in domain name */
while((n = msg[srcn++]) != 0 && !isNaN(srcn)) {
/* Check for indirection */
switch(n & ns_cmprsflgs) {
case 0:
case ns_type_elt:
/* Limit checks */
if((l = labellen(msg, srcn - 1)) < 0) {
errno.set('EMSGSIZE');
return (-1);
}
if(dstn + l + 1 >= dstlim || srcn + l >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
checked += l + 1;
dst[dstn++] = n;
msg.copy(dst, dstn, srcn, srcn + l);
dstn += l;
srcn += l;
break;
case ns_cmprsflgs:
if(srcn >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
if(llen < 0) {
llen = (srcn - offset) + 1;
}
srcn = (((n & 0x3F) * 256) | (msg[srcn] & 0xFF));
if(srcn < 0 || srcn >= eom) { /* Out of range */
errno.set('EMSGSIZE');
return (-1);
}
checked += 2;
/* check for loops in compressed name */
if(checked >= eom) {
errno.set('EMSGSIZE');
return (-1);
}
break;
default:
errno.set('EMSGSIZE');
return (-1); // flag error
}
}
dst[dstn] = 0;
if (dstlenp != null)
dstlenp.set(dstn);
if(llen < 0)
llen = srcn - offset;
return (llen);
}
function ns_name_pack (src, dst, dstn, dstsiz, dnptrs, lastdnptr) {
var dstp;
var cpp, lpp, eob, msgp;
var srcp;
var n, l, first = 1;
srcp = 0;
dstp = dstn;
eob = dstp + dstsiz;
lpp = cpp = null;
var ndnptr = 0;
if (dnptrs != null) {
msg = dst;
//if ((msg = dnptrs[ndnptr++]) != null) {
for (cpp = 0; dnptrs[cpp] != null; cpp++);
lpp = cpp; // end of list to search
//}
} else
msg = null;
// make sure the domain we are about to add is legal
l = 0;
do {
var l0;
n = src[srcp];
if ((n & ns_cmprsflgs) == ns_cmprsflgs) {
errno.set('EMSGSIZE');
return (-1);
}
if ((l0 = labellen(src, srcp)) < 0) {
errno.set('EINVAL');
return (-1);
}
l += l0 + 1;
if (l > ns_maxcdname) {
errno.set('EMSGSIZE');
return (-1);
}
srcp += l0 + 1;
} while (n != 0);
// from here on we need to reset compression pointer array on error
srcp = 0;
var cleanup = false; // instead of goto
do {
// look to see if we can use pointers
n = src[srcp];
if (n != 0 && msg != null) {
l = dn_find(src, srcp, msg, dnptrs, ndnptr, lpp);
if (l >= 0) {
if (dstp + 1 >= eob) {
cleanup = true;
break;
}
dst[dstp++] = (l >> 8) | ns_cmprsflgs;
dst[dstp++] = l & 0xff;
return (dstp - dstn);
}
// Not found, save it.
if (lastdnptr != null && cpp < lastdnptr - 1 &&
(dstp) < 0x4000 && first) {
dnptrs[cpp++] = dstp;
dnptrs[cpp++] = null;
first = 0;
}
}
// copy label to buffer
if ((n & ns_cmprsflgs) == ns_cmprsflgs) {
// should not happen
cleanup = true;
break;
}
n = labellen(src, srcp);
if (dstp + 1 + n >= eob) {
cleanup = true;
break;
}
src.copy(dst, dstp, srcp, srcp + (n + 1));
srcp += n + 1;
dstp += n + 1;
} while (n != 0);
if (dstp > eob ||
// cleanup:
cleanup) {
if (msg != null) {
dnptrs[lpp] = null;
}
errno.set('EMSGSIZE');
return (-1);
}
return (dstp - dstn);
}
exports.ns_name_pack = ns_name_pack;
function ns_name_skip (b, ptrptr, eom) {
var cp;
var n;
var l;
cp = ptrptr.get();
while (cp < eom && (n = b[cp++]) != 0) {
switch (n & ns_cmprsflgs) {
case 0: // normal case, n == len
cp += n;
continue;
case ns_type_elt: // edns0 extended label
if ((l = labellen(b, cp - 1)) < 0) {
errno.set('EMSGSIZE');
return (-1);
}
cp += l;
continue;
case ns_cmprsflgs: // indirection
cp++;
break;
default: // illegal type
errno.set('EMSGSIZE');
return (-1);
}
break;
}
if (cp > eom) {
errno.set('EMSGSIZE');
return (-1);
}
ptrptr.set(cp);
return (0);
}
exports.ns_name_skip = ns_name_skip;
function special(ch) {
switch(ch) {
case 0x22: /* '"' */
case 0x2E: /* '.' */
case 0x3B: /* ';' */
case 0x5C: /* '\\' */
case 0x28: /* '(' */
case 0x29: /* ')' */
/* special modifiers in the zone file */
case 0x40: /* '@' */
case 0x24: /* '$' */
return (1);
default:
return (0);
}
}
function printable (ch) {
return (ch > 0x20 && ch < 0x7F);
}
function mklower (ch) {
if (ch >= 0x41 && ch <= 0x5A)
return (ch + 0x20);
return (ch);
}
function dn_find(src, domain, msg, dnptrs, ndnptr, lastdnptr) {
var dn, cp, sp;
var cpp;
var n;
var next = false; // instead of goto
for (cpp = ndnptr; cpp < lastdnptr; cpp++) {
sp = dnptrs[cpp];
//
// terminate search on:
// root label
// compression pointer
// unusable offset
//
while (msg[sp] != 0 && (msg[sp] & ns_cmprsflgs) == 0 &&
(sp) < 0x4000) {
dn = domain;
cp = sp;
while ((n = msg[cp++]) != 0) {
//
// check for indirection
//
switch (n & ns_cmprsflgs) {
case 0: // normal case, n == len
n = labellen(msg, cp - 1) // XXX
if (n != src[dn++]) {
next = true;
break;
}
for (null; n > 0; n--) {
if (mklower(src[dn++]) !=
mklower(msg[cp++])) {
next = true;
break;
}
}
if (next) {
break;
}
// Is next root for both ?
if (src[dn] == 0 && msg[cp] == 0) {
return (sp);
}
if (src[dn]) {
continue;
}
next = true;
break;
case ns_cmprsflgs: // indirection
cp = (((n & 0x3f) * 256) | msg[cp]);
break;
default: // illegal type
errno.set('EMSGSIZE');
return (-1);
}
if (next) {
break;
}
}
sp += msg[sp] + 1;
if (next)
next = false;
}
}
errno.set('ENOENT');
return (-1);
}
exports.dn_find = dn_find;
function decode_bitstring (b, cpp, d, dn, eom) {
var cp = cpp.get();
var beg = dn, tc;
var b, blen, plen, i;
if ((blen = (b[cp] & 0xff)) == 0)
blen = 256;
plen = (blen + 3) / 4;
plen += "\\[x/]".length + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
if (dn + plen >= eom)
return (-1);