-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenreport.c
4339 lines (3991 loc) · 111 KB
/
genreport.c
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
/*
* Copyright (C) 2016-2019 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#define FD_SETSIZE 1600
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <openssl/hmac.h>
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define HMAC_CTX_new() &(_ctx), HMAC_CTX_init(&_ctx)
#define HMAC_CTX_free(ptr) HMAC_CTX_cleanup(ptr)
#endif
#ifndef HAVE_STRLCPY
#define strlcpy(dst, src, len) snprintf(dst, len, "%s", src)
#endif
#ifndef FD_COPY
#define FD_COPY(x, y) memmove(y, x, sizeof(*x))
#endif
#define ns_t_dname 39
#define ns_t_sink 40
#define ns_t_apl 42
#define ns_t_ds 43
#define ns_t_sshfp 44
#define ns_t_ipseckey 45
#define ns_t_rrsig 46
#define ns_t_nsec 47
#define ns_t_dnskey 48
#define ns_t_dhcid 49
#define ns_t_nsec3 50
#define ns_t_nsec3param 51
#define ns_t_tlsa 52
#define ns_t_smimea 53
#define ns_t_hip 55
#define ns_t_ninfo 56
#define ns_t_talink 58
#define ns_t_cds 59
#define ns_t_cdnskey 60
#define ns_t_openpgpkey 61
#define ns_t_csync 62
#define ns_t_spf 99
#define ns_t_nid 104
#define ns_t_l32 105
#define ns_t_l34 106
#define ns_t_lp 107
#define ns_t_eui48 108
#define ns_t_eui64 109
#define ns_t_uri 256
#define ns_t_caa 257
#define ns_t_avc 258
#define ns_t_doa 259
#define ns_t_ta 32768
#define ns_t_dlv 32769
#define ns_r_badcookie 23
static int eof = 0;
static int maxfd = -1;
static fd_set rfds, wfds;
static int outstanding = 0;
static int maxoutstanding = 100;
static void(*rhandlers[FD_SETSIZE])(int, int);
static void(*whandlers[FD_SETSIZE])(int);
static int udp4 = -1;
static int udp6 = -1;
static int icmp4 = -1;
static int icmp6 = -1;
static int ipv4only = 0;
static int ipv6only = 0;
static int allok = 0;
static int bad = 0;
static int badtag = 0;
static int ednsonly = 0;
static int debug = 0;
static int inorder = 0;
static int serial = 0;
static int printnsid = 0;
static int recursive = 0;
static long long sent;
static int json = 0;
static int unique = 0;
static int useglue = 0;
static int glueonly = 0;
static const char *jrec = "{ \"data\": [ ";
static const char *jfin = "{ }";
#ifdef HAVE_RES_GETSERVERS
static union res_sockaddr_union servers[10];
#else
static union {
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} servers[10];
#endif
static int nservers = 0;
int ident = 0;
/*
* Doubly linked list macros.
*/
#define APPEND(list, item, link) do { \
if ((list).tail) \
(list).tail->link.next = (item); \
else \
(list).head = (item); \
(item)->link.prev = list.tail; \
(item)->link.next = NULL; \
(list).tail = (item); \
(item)->link.linked = 1; \
} while (0)
#define PREPEND(list, item, link) do { \
if ((list).head) \
(list).head->link.prev = (item); \
else \
(list).tail = (item); \
(item)->link.prev = NULL; \
(item)->link.next = list.head; \
(list).head = (item); \
(item)->link.linked = 1; \
} while (0)
#define INSERTBEFORE(list, before, item, link) do { \
assert(LINKED(before, link)); \
if ((before)->link.prev == NULL) \
PREPEND(list, item, link); \
else { \
(item)->link.prev = (before)->link.prev; \
(before)->link.prev = (item); \
(item)->link.prev->link.next = (item); \
(item)->link.next = (before); \
(item)->link.linked = 1; \
} \
} while (0)
#define UNLINK(list, item, link) do { \
if ((item)->link.next) \
(item)->link.next->link.prev = (item)->link.prev; \
else \
list.tail = (item)->link.prev; \
if ((item)->link.prev) \
(item)->link.prev->link.next = (item)->link.next; \
else \
list.head = (item)->link.next; \
(item)->link.next = (item)->link.prev = NULL; \
(item)->link.linked = 0; \
} while (0)
#define NEXT(item, link) (item)->link.next
#define PREV(item, link) (item)->link.prev
#define LINKED(item, link) (item)->link.linked
#define HEAD(list) (list).head
#define TAIL(list) (list).tail
#define HMACSHA256W "\013hmac-sha256" /* cannonical form */
#define HMACSHA256 "hmac-sha256" /* presentation form */
/*
* Test groupings
*/
#define NONE 0x00
#define EDNS 0x01
#define COMM 0x02
#define FULL 0x04
#define TYPE 0x08
#define EXPL 0x10
static int what = EDNS;
static struct {
const char *name; /* test nmemonic */
unsigned int what; /* select what test to make / report */
unsigned short rdlen; /* edns rdata length */
const char *rdata; /* edns rdata */
unsigned short udpsize; /* edns UDP size (0 == no EDNS) */
unsigned short flags; /* edns flags to be set */
unsigned short version; /* edns version */
unsigned int tcp; /* use tcp */
unsigned int cookie; /* opt record has cookie */
unsigned int ignore; /* ignore tc in response */
unsigned int tc; /* set tc in request */
unsigned int rd; /* set rd in request */
unsigned int ra; /* set ra in request */
unsigned int cd; /* set cd in request */
unsigned int ad; /* set ad in request */
unsigned int aa; /* set aa in request */
unsigned int z; /* set z in request */
unsigned int opcode; /* use opcode for request */
unsigned short type; /* query type code */
const char *dig; /* dig command */
} opts[] = {
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "dns", EDNS, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +norec SOA <zone>"
},
{ "aa", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, ns_t_soa,
"dig +noedns +noad +norec +aaflag SOA <zone>"
},
{ "ad", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, ns_t_soa,
"dig +noedns +ad +norec SOA <zone>"
},
{ "cd", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +norec +cd SOA <zone>"
},
{ "ra", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, ns_t_soa,
"### dig +noedns +noad +norec +raflag SOA <zone> ###"
},
{ "rd", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +rec SOA <zone>"
},
{ "tc", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"### dig +noedns +noad +norec +tcflag SOA <zone> ###"
},
{ "zflag", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, ns_t_soa,
"dig +noedns +noad +norec +zflag SOA <zone>"
},
{ "opcode", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0,
"dig +noedns +noad +norec +header-only +opcode=15"
},
{ "opcodeflg", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 15, 0,
"### dig +noedns +header-only +opcode=15 +tcflag +rec +raflag +cd +ad +aaflag +zflag ###"
},
{ "type666", FULL, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 666,
"dig +noedns +noad +norec TYPE666 <zone>"
},
{ "tcp", FULL, 0, "", 0, 0x0000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +norec +tcp SOA <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "edns", EDNS, 0, "", 4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec SOA <zone>"
},
{ "edns1", EDNS, 0, "", 4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec SOA <zone>"
},
{ "edns@512", EDNS, 0, "", 512, 0x0000, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dnskey,
"dig +edns=0 +nocookie +noad +norec +dnssec +ignoretc +bufsize=512 DNSKEY <zone>"
},
{ "ednsopt", EDNS, 4, "\x00\x64\x00\x00", /* 100 */
4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +ednsopt=100 SOA <zone>"
},
{ "edns1opt", EDNS, 4, "\x00\x64\x00\x00", /* 100 */
4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec +ednsopt=100 SOA <zone>"
},
{ "do", EDNS, 0, "",
4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +dnssec SOA <zone>"
},
{ "docd", FULL, 0, "",
4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +dnssec SOA <zone>"
},
{ "edns1do", FULL, 0, "", 4096, 0x8000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec +dnssec SOA <zone>"
},
{ "ednsflags", EDNS, 0, "", 4096, 0x0080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +ednsflags=0x0080 SOA <zone>"
},
{ "optlist", EDNS, 4 + 8 + 4 + 12,
"\x00\x03\x00\x00" /* NSID */
"\x00\x08\x00\x04\x00\x01\x00\x00" /* ECS */
"\x00\x09\x00\x00" /* EXPIRE */
"\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
4096, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +noad +norec +nsid +subnet=0.0.0.0/0 +expire +cookie=0102030405060708 SOA <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "ednsnsid", FULL, 4, "\x00\x03\x00\x00", /* NSID */
4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +nsid SOA <zone>"
},
{ "ednscookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
4096, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +noad +norec +cookie=0102030405060708 SOA <zone>"
},
{ "ednsexpire", FULL, 4, "\x00\x09\x00\x00", /* EXPIRE */
4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +expire SOA <zone>"
},
{ "ednssubnet", FULL, 8, "\x00\x08\x00\x04\x00\x01\x00\x00", /* ECS */
4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +nocookie +noad +norec +subnet=0.0.0.0/0 SOA <zone>"
},
{ "edns1nsid", FULL, 4, "\x00\x03\x00\x00", /* NSID */
4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec +nsid SOA <zone>"
},
{ "edns1cookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
4096, 0x0000, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +noad +norec +cookie=0102030405060708 SOA <zone>"
},
{ "edns1expire", FULL, 4, "\x00\x09\x00\x00", /* EXPIRE */
4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec +expire SOA <zone>"
},
{ "edns1subnet", FULL, 8, "\x00\x08\x00\x04\x00\x01\x00\x00", /* ECS */
4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=1 +noednsneg +nocookie +noad +norec +subnet=0.0.0.0/0 SOA <zone>"
},
{ "ednstcp", EDNS, 0, "", 512, 0x8000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dnskey,
"dig +edns=0 +nocookie +noad +norec +dnssec +bufsize=512 +tcp DNSKEY <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "bind11", COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
4096, 0x8000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +edns=0 +cookie=0102030405060708 +noad +norec +dnssec SOA <zone>"
},
{ "dig11", COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
4096, 0x0000, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, ns_t_soa,
"dig +edns=0 +cookie=0102030405060708 +ad +rec SOA <zone>"
},
{ "dnswkk", NONE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +norec -y hmac-sha256:.:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= SOA <zone>"
},
{ "icmp", NONE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"ping / ping6"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "A", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_a,
"dig +noedns +noad +norec A <zone>"
},
{ "NS", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_ns,
"dig +noedns +noad +norec NS <zone>"
},
{ "MD", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_md,
"dig +noedns +noad +norec MD <zone>"
},
{ "MF", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_mf,
"dig +noedns +noad +norec MF <zone>"
},
{ "CNAME", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_cname,
"dig +noedns +noad +norec CNAME <zone>"
},
{ "SOA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_soa,
"dig +noedns +noad +norec SOA <zone>"
},
{ "MB", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_mb,
"dig +noedns +noad +norec MB <zone>"
},
{ "MG", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_mg,
"dig +noedns +noad +norec MG <zone>"
},
{ "MR", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_mr,
"dig +noedns +noad +norec MR <zone>"
},
{ "NULL", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_null,
"dig +noedns +noad +norec NULL <zone>"
},
{ "WKS", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_wks,
"dig +noedns +noad +norec WKS <zone>"
},
{ "PTR", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_ptr,
"dig +noedns +noad +norec PTR <zone>"
},
{ "HINFO", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_hinfo,
"dig +noedns +noad +norec HINFO <zone>"
},
{ "MINFO", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_minfo,
"dig +noedns +noad +norec MINFO <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "MX", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_mx,
"dig +noedns +noad +norec MX <zone>"
},
{ "TXT", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_txt,
"dig +noedns +noad +norec TXT <zone>"
},
{ "RP", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_rp,
"dig +noedns +noad +norec RP <zone>"
},
{ "AFSDB", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_afsdb,
"dig +noedns +noad +norec AFSDB <zone>"
},
{ "X25", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_x25,
"dig +noedns +noad +norec X25 <zone>"
},
{ "ISDN", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_isdn,
"dig +noedns +noad +norec ISDN <zone>"
},
{ "RT", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_rt,
"dig +noedns +noad +norec RT <zone>"
},
{ "NSAP", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nsap,
"dig +noedns +noad +norec NSAP <zone>"
},
{ "NSAP-PTR", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nsap_ptr,
"dig +noedns +noad +norec NSAP-PTR <zone>"
},
{ "SIG", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_sig,
"dig +noedns +noad +norec SIG <zone>"
},
{ "KEY", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_key,
"dig +noedns +noad +norec KEY <zone>"
},
{ "PX", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_px,
"dig +noedns +noad +norec PX <zone>"
},
{ "GPOS", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_gpos,
"dig +noedns +noad +norec GPOS <zone>"
},
{ "AAAA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_aaaa,
"dig +noedns +noad +norec AAAA <zone>"
},
{ "LOC", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_loc,
"dig +noedns +noad +norec LOC <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "NXT", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nxt,
"dig +noedns +noad +norec NXT <zone>"
},
{ "SRV", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_srv,
"dig +noedns +noad +norec SRV <zone>"
},
{ "NAPTR", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_naptr,
"dig +noedns +noad +norec NAPTR <zone>"
},
{ "KX", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_kx,
"dig +noedns +noad +norec KX <zone>"
},
{ "CERT", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_cert,
"dig +noedns +noad +norec CERT <zone>"
},
{ "A6", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_a6,
"dig +noedns +noad +norec A6 <zone>"
},
{ "DNAME", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dname,
"dig +noedns +noad +norec DNAME <zone>"
},
{ "APL", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_apl,
"dig +noedns +noad +norec APL <zone>"
},
{ "DS", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_ds,
"dig +noedns +noad +norec DS <zone>"
},
{ "SSHFP", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_sshfp,
"dig +noedns +noad +norec SSHFP <zone>"
},
{ "IPSECKEY", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_ipseckey,
"dig +noedns +noad +norec IPSECKEY <zone>"
},
{ "RRSIG", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_rrsig,
"dig +noedns +noad +norec RRSIG <zone>"
},
{ "NSEC", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nsec,
"dig +noedns +noad +norec NSEC <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "DNSKEY", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dnskey,
"dig +noedns +noad +norec DNSKEY <zone>"
},
{ "DHCID", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dhcid,
"dig +noedns +noad +norec DHCID <zone>"
},
{ "NSEC3", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nsec3,
"dig +noedns +noad +norec NSEC3 <zone>"
},
{ "NSEC3PARAM",TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nsec3param,
"dig +noedns +noad +norec NSEC3PARAM <zone>"
},
{ "TLSA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_tlsa,
"dig +noedns +noad +norec TLSA <zone>"
},
{ "SMIMEA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_smimea,
"dig +noedns +noad +norec SMIMEA <zone>"
},
{ "HIP", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_hip,
"dig +noedns +noad +norec HIP <zone>"
},
{ "CDS", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_cds,
"dig +noedns +noad +norec CDS <zone>"
},
{ "CDNSKEY", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_cdnskey,
"dig +noedns +noad +norec CDNSKEY <zone>"
},
{ "OPENPGPKEY",TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_openpgpkey,
"dig +noedns +noad +norec OPENPGPKEY <zone>"
},
{ "SPF", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_spf,
"dig +noedns +noad +norec SPF <zone>"
},
{ "NID", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_nid,
"dig +noedns +noad +norec NID <zone>"
},
{ "L32", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_l32,
"dig +noedns +noad +norec L32 <zone>"
},
/* size eflgs vr T ck ig tc rd ra cd ad aa z op type */
{ "L64", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_l34,
"dig +noedns +noad +norec L64 <zone>"
},
{ "LP", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_lp,
"dig +noedns +noad +norec LP <zone>"
},
{ "EUI48", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_eui48,
"dig +noedns +noad +norec EUI48 <zone>"
},
{ "EUI64", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_eui64,
"dig +noedns +noad +norec EUI64 <zone>"
},
{ "URI", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_uri,
"dig +noedns +noad +norec URI <zone>"
},
{ "CAA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_caa,
"dig +noedns +noad +norec CAA <zone>"
},
{ "AVC", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_avc,
"dig +noedns +noad +norec AVC <zone>"
},
{ "DOA", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_doa,
"dig +noedns +noad +norec DOA <zone>"
},
{ "DLV", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ns_t_dlv,
"dig +noedns +noad +norec DLV <zone>"
},
{ "TYPE1000", TYPE, 0, "", 0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000,
"dig +noedns +noad +norec TYPE1000 <zone>"
}
};
/*
* Summary structure where results from multiple lookups are recorded.
*/
struct summary {
struct {
struct summary *prev;
struct summary *next;
int linked;
} link;
char zone[1024]; /* the zone's name */
char ns[1024]; /* the server's name */
char target[1024]; /* the server's real name */
char soaname[1024]; /* the server's real name */
struct sockaddr_storage storage;/* server we are talking to */
int tests; /* number of outstanding tests */
unsigned int last; /* last test sent */
int deferred; /* was the printing deferred */
int done; /* we are done */
int type; /* recursive query lookup type */
int nodata; /* recursive query got nodata */
int nodataa; /* recursive query got nodata */
int nodataaaaa; /* recursive query got nodata */
int nxdomain; /* recursive query got nxdomain */
int nxdomaina; /* recursive query got nxdomain */
int nxdomainaaaa; /* recursive query got nxdomain */
int faileda;
int failedaaaa;
int cname; /* NS is CNAME */
int cnamea; /* Nameserver is CNAME */
int cnameaaaa; /* Nameserver is CNAME */
int seenrrsig; /* a rrsig was seen in "do" test */
int seenopt; /* see a EDNS response */
int seenedns; /* see a EDNS response */
int seenfailure; /* see a lookup failure */
int allok; /* all answers are current ok */
int allrefused; /* all answers are current ok */
int allservfail; /* all answers are current ok */
int targetok; /* target is valid */
int soaok; /* soaname is valid */
struct summary *xlink; /* cross link of recursive A/AAAA */
unsigned int nsidlen;
char nsid[100]; /* nsid if found */
char results[sizeof(opts)/sizeof(opts[0])][100];
};
static struct {
struct summary *head;
struct summary *tail;
} summaries;
struct workitem {
struct {
struct workitem *next;
struct workitem *prev;
int linked;
} link, clink, plink, rlink, idlink, seqlink;
unsigned short id; /* the query id we are waiting for */
struct timeval when; /* when we will timeout */
int type; /* the query type being looked up */
int test; /* test number / server number */
int sends; /* number of times this UDP request
* has been sent */
int buflen; /* the size of the request to be sent */
int tcpfd; /* the tcp file descriptor */
int outstanding; /* outstanding has been set */
int havelen; /* readlen is tcp message length */
int readlen; /* how much we need to read */
int read; /* how much has been read so far */
int icmp; /* this is a icmp echo request */
int onheap;
unsigned char buf[512]; /* the question we sent */
unsigned char tcpbuf[0x10000]; /* where to accumulate the tcp response */
unsigned char mac[32]; /* tsig hmac-sha256 mac */
struct summary *summary; /* where this test is summaried */
};
/*
* Work queues:
* 'work' udp qeries;
* 'connecting' tcp qeries;
* 'reading' tcp qeries;
*
* Outstanding queries by qid.
* 'ids'
*
* Outstanding icmp by seq.
* 'ids'
*/
static struct {
struct workitem *head;
struct workitem *tail;
} work, connecting, reading, ids[0x10000], seq[0x10000];
static void
dotest(struct workitem *item, int usec);
static void
nextserver(struct workitem *item);
static void
connecttoserver(struct workitem *item);
static void
report(struct summary *summary);
void
jsonsafe(const char *str, char *safe, size_t len) {
char c;
while (len > 1) {
switch ((c = *str++)) {
case '\\':
strlcpy(safe, "\\\\", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '"':
strlcpy(safe, "\\\"", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\b':
strlcpy(safe, "\\b", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\f':
strlcpy(safe, "\\f", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\n':
strlcpy(safe, "\\n", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\r':
strlcpy(safe, "\\r", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\t':
strlcpy(safe, "\\t", len);
len -= strlen(safe);
safe += strlen(safe);
break;
case '\0':
*safe = c;
len = 0;
break;
default:
if (c > 0 && c < ' ') {
snprintf(safe, len, "\\u%04x", (c & 0xff));
len -= strlen(safe);
safe += strlen(safe);
break;
} else {
*safe++ = c;
*safe = '\0';
len--;
}
break;
}
}
if (len > 0)
*safe = '\0';
}
static int
storage_equal(struct sockaddr_storage *s1, struct sockaddr_storage *s2) {
struct sockaddr_in *sin1, *sin2;
struct sockaddr_in6 *sin61, *sin62;
if (s1->ss_family != s2->ss_family)
return (0);
switch (s1->ss_family) {
case AF_INET:
sin1 = (struct sockaddr_in *)s1;
sin2 = (struct sockaddr_in *)s2;
if (sin1->sin_port != sin2->sin_port ||
sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
return (0);
return (1);
case AF_INET6:
sin61 = (struct sockaddr_in6 *)s1;
sin62 = (struct sockaddr_in6 *)s2;
if (sin61->sin6_port != sin62->sin6_port ||
memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0)
return (0);
return (1);
}
return (0);
}
struct itemheap {
size_t size;
size_t last;
struct workitem ** array;
} pending = { 0, 0, NULL };
#define heap_parent(i) ((i) >> 1)
#define heap_left(i) ((i) << 1)
static int
heap_compare(struct workitem *a, struct workitem *b) {
if (a->when.tv_sec < b->when.tv_sec)
return (1);
if (a->when.tv_sec == b->when.tv_sec &&
a->when.tv_usec < b->when.tv_usec)
return (1);
return (0);
}
static void
heap_grow(struct itemheap *heap) {
struct workitem **new_array;
size_t new_size;
new_size = heap->size += 1024;
new_array = realloc(heap->array, new_size);
if (new_array == NULL) {
perror("realloc");
exit(1);
}
heap->size = new_size;
heap->array = new_array;
}
static void
heap_floatup(struct itemheap *heap, size_t i, struct workitem *item) {
size_t p;
for (p = heap_parent(i) ;
i > 1 && heap_compare(item, heap->array[p]) ;
i = p, p = heap_parent(i)) {
heap->array[i] = heap->array[p];
}
heap->array[i] = item;
}
static void
heap_sinkdown(struct itemheap *heap, size_t i, struct workitem *item) {
size_t j, size, half_size;
size = heap->last;
half_size = size / 2;
while (i <= half_size) {
j = heap_left(i);
if (j < size &&
heap_compare(heap->array[j+1], heap->array[j]))
j++;
if (heap_compare(item, heap->array[j]))
break;
heap->array[i] = heap->array[j];
i = j;
}
heap->array[i] = item;
}
static void
heap_insert(struct itemheap *heap, struct workitem *item) {
size_t new_last;
new_last = heap->last + 1;
if (new_last >= heap->size)
heap_grow(heap);
heap->last = new_last;
item->onheap = 1;
heap_floatup(heap, new_last, item);
}
static void
heap_delete(struct itemheap *heap) {
if (heap->last > 0) {
heap->array[1]->onheap = 0;
heap->array[1] = heap->array[heap->last];
heap->array[heap->last] = NULL;
heap->last--;
if (heap->last > 1)
heap_sinkdown(heap, 1, heap->array[1]);
}
}
struct workitem *
heap_item(struct itemheap *heap) {
if (heap->last)
return (heap->array[1]);
return (NULL);
}
static int
sentto(struct sockaddr_storage *s) {
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
static struct listitem {
struct listitem *next;
struct sockaddr_storage s;
} *table[100000] = { NULL }, *item = NULL;
unsigned int hash = 0;
switch (s->ss_family) {
case AF_INET:
sin = (struct sockaddr_in *)s;
hash = sin->sin_addr.s_addr;
break;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)s;
#ifndef s6_addr32
# if defined(__sun)
# define s6_addr32 _S6_un._S6_u32
# elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)|| defined(__DragonFly__)
# define s6_addr32 __u6_addr.__u6_addr32
# else
# error s6_addr32 needs to be defined.
# endif
#endif
hash = sin6->sin6_addr.s6_addr32[0] ^ sin6->sin6_addr.s6_addr32[1] ^
sin6->sin6_addr.s6_addr32[2] ^ sin6->sin6_addr.s6_addr32[3];
break;
}
hash %= 100000;
for (item = table[hash]; item != NULL; item = item->next) {
if (storage_equal(&item->s, s))
return (1);
}
item = calloc(1, sizeof(*item));
if (item == NULL)
return (0);
item->next = table[hash];
item->s = *s;
table[hash] = item;
return (0);
}
/*
* Check if it is safe to use this id to this address.
*/
static int
checkid(struct sockaddr_storage *storage, int id) {
struct workitem *item;
item = HEAD(ids[id]);
while (item != NULL &&
!storage_equal(storage, &item->summary->storage))
item = NEXT(item, idlink);
return ((item == NULL) ? 1 : 0);
}
/*
* Check if we have a outstanding icmp with this sequence number
* to this address.
*/
static int
checkseq(struct sockaddr_storage *storage, int id) {
struct workitem *item;
item = HEAD(seq[id]);
while (item != NULL &&
!storage_equal(storage, &item->summary->storage))
item = NEXT(item, seqlink);
return ((item == NULL) ? 1 : 0);
}
static void
freesummary(struct summary *summary) {
if (LINKED(summary, link))
UNLINK(summaries, summary, link);
free(summary);
fflush(NULL);
}
static void
emiterr(const char *zone, const char *ns, const char *str) {
char safe[1024];
if (json) {
printf("%s\n", jrec);
jrec = ",";
jfin = "\n] }";
jsonsafe(zone[0] ? zone : ".", safe, sizeof(safe));
printf("{ \"zone\": \"%s\"", safe);
if (ns) {
jsonsafe(ns, safe, sizeof(safe));
printf(", \"servername\": \"%s\"", safe);
}
jsonsafe(str, safe, sizeof(safe));
printf(", \"error\": \"%s\" }", safe);
} else {
printf("%s.%s%s: %s\n",
zone, ns ? " " : "", ns ? ns : "", str);
}
}
/*
* Generate a report line.
*/
static void
printandfree(struct summary *summary) {
struct sockaddr_in *s = (struct sockaddr_in *)&summary->storage;
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&summary->storage;;
char addrbuf[64];
char buf[2048];
char safe[1024];
void *addr;
unsigned int i;
int x;
if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&