genreport.c 57.9 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
2
3
4
5
6
7
8
/*
 * Copyright (C) 2016  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/.
 */

Mark Andrews's avatar
Mark Andrews committed
9
#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
10
11
12
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
Mark Andrews's avatar
Mark Andrews committed
13
#include <assert.h>
Mark Andrews's avatar
Mark Andrews committed
14

Mark Andrews's avatar
Mark Andrews committed
15
16
17
18
19
20
21
#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>
22
23
#include <sys/ioctl.h>
#include <sys/uio.h>
Mark Andrews's avatar
Mark Andrews committed
24
25

#include <errno.h>
26
#include <netdb.h>
Mark Andrews's avatar
Mark Andrews committed
27
#include <resolv.h>
Mark Andrews's avatar
Mark Andrews committed
28

Mark Andrews's avatar
Mark Andrews committed
29
30
31
32
33
34
#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
35
#define ns_t_rrsig 46
Mark Andrews's avatar
Mark Andrews committed
36
#define ns_t_nsec 47
Mark Andrews's avatar
Mark Andrews committed
37
#define ns_t_dnskey 48
Mark Andrews's avatar
Mark Andrews committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#define ns_t_dhcid 49
#define ns_t_nsec3 50
#define ns_t_nsec3param 51
#define ns_t_tlsa 52
#define ns_t_hip 55
#define ns_t_ninfo 56
#define ns_t_talink 58
#define ns_t_openpgpkey 59
#define ns_t_cds 59
#define ns_t_cdnskey 60
#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_ta 32768
#define ns_t_dlv 32769
Mark Andrews's avatar
Mark Andrews committed
60

61
static int eof = 0;
Mark Andrews's avatar
Mark Andrews committed
62
63
static int maxfd = -1;
static fd_set rfds, wfds;
Mark Andrews's avatar
Mark Andrews committed
64
static int outstanding = 0;
65
static int maxoutstanding = 100;
Mark Andrews's avatar
Mark Andrews committed
66
67
68
69
70
71

static void(*rhandlers[FD_SETSIZE])(int);
static void(*whandlers[FD_SETSIZE])(int);

static int udp4 = -1;
static int udp6 = -1;
72

Mark Andrews's avatar
Mark Andrews committed
73
74
75
static int ipv4only = 0;
static int ipv6only = 0;

76
77
static int bad  = 0;
static int ednsonly  = 0;
78
static int debug = 0;
Mark Andrews's avatar
Mark Andrews committed
79
static int inorder = 0;
Mark Andrews's avatar
Mark Andrews committed
80
static int serial  = 0;
81
82
83

static union res_sockaddr_union servers[10];
static int nservers = 0;
Mark Andrews's avatar
Mark Andrews committed
84

Mark Andrews's avatar
Mark Andrews committed
85
86
87
/*
 * Doubly linked list macros.
 */
Mark Andrews's avatar
Mark Andrews committed
88
89
90
91
92
93
94
95
#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); \
Mark Andrews's avatar
Mark Andrews committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	(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); \
Mark Andrews's avatar
Mark Andrews committed
119
		(item)->link.linked = 1; \
Mark Andrews's avatar
Mark Andrews committed
120
	} \
Mark Andrews's avatar
Mark Andrews committed
121
122
123
124
125
126
127
128
129
130
131
132
} 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; \
133
	(item)->link.linked = 0; \
Mark Andrews's avatar
Mark Andrews committed
134
135
136
} while (0)

#define NEXT(item, link) (item)->link.next
Mark Andrews's avatar
Mark Andrews committed
137
#define PREV(item, link) (item)->link.prev
138
#define LINKED(item, link) (item)->link.linked
Mark Andrews's avatar
Mark Andrews committed
139
140
141
142

#define HEAD(list) (list).head
#define TAIL(list) (list).tail

Mark Andrews's avatar
Mark Andrews committed
143
144
145
/*
 * Test groupings
 */
Mark Andrews's avatar
Mark Andrews committed
146
147
148
149
150
151
152
#define EDNS 0x01
#define COMM 0x02
#define FULL 0x04
#define TYPE 0x08
#define EXPL 0x10

static int what = EDNS;
153

Mark Andrews's avatar
Mark Andrews committed
154
static struct {
Mark Andrews's avatar
Mark Andrews committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	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 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 */
173
} opts[] = {
Mark Andrews's avatar
Mark Andrews committed
174
	/*                           size   eflgs vr  T ig tc rd ra cd ad aa  z  op  type */
175
	{ "dns",       EDNS,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
176
177
178
179
180
181
182
183
184
	{ "aa",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,  0, ns_t_soa },
	{ "ad",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,  0, ns_t_soa },
	{ "cd",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,  0, ns_t_soa },
	{ "ra",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,  0, ns_t_soa },
	{ "rd",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "tc",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "zflag",     FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,  0, ns_t_soa },
	{ "opcode",    FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0 },
	{ "opcodeflg", FULL,  0, "",    0, 0x0000, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 15, 0 },
185
	{ "type666",   FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 666 },
186
	{ "tcp",       FULL,  0, "",    0, 0x0000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
187
188

	/*                           size   eflgs vr  T ig tc rd ra cd ad aa  z  op  type */
189
190
191
192
	{ "edns",      EDNS,  0, "", 4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1",     EDNS,  0, "", 4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns@512",  EDNS,  0, "",  512, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },
	{ "ednsopt",   EDNS,  4, "\x00\x64\x00",
Mark Andrews's avatar
Mark Andrews committed
193
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
194
	{ "edns1opt",  EDNS,  4, "\x00\x64\x00",
Mark Andrews's avatar
Mark Andrews committed
195
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
196
	{ "do",        EDNS,  4, "\0\144\0",
Mark Andrews's avatar
Mark Andrews committed
197
				     4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
198
	{ "edns1do",   FULL,  0, "", 4096, 0x8000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
199
200
	{ "ednsflags", EDNS,  0, "", 4096, 0x0080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "optlist",   EDNS,  4 + 8 + 4 + 12,
201
202
203
204
	  "\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 */
Mark Andrews's avatar
Mark Andrews committed
205
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
206
207
208
209
	{ "edns1ecs",  FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1nsid", FULL,  4, "\x00\x03\x00\x00",
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
210
211
	{ "edns1cook", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
212
213
214
	{ "ednstcp",   EDNS,  0, "",  512, 0x8000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },

	/*                           size   eflgs vr  T ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
style    
Mark Andrews committed
215
	{ "bind11",    COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",
Mark Andrews's avatar
Mark Andrews committed
216
				     4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
217
	{ "dig11",     COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",
Mark Andrews's avatar
Mark Andrews committed
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
				     4096, 0x0000, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0,  0, ns_t_soa },
	{ "A",         TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_a },
	{ "NS",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ns },
	{ "MD",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_md },
	{ "MF",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mf },
	{ "CNAME",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cname },
	{ "SOA",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "MB",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mb },
	{ "MG",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mg },
	{ "MR",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mr },
	{ "NULL",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_null },
	{ "WKS",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_wks },
	{ "PTR",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ptr },
	{ "HINFO",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_hinfo },
	{ "MINFO",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_minfo },
	{ "MX",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mx },
	{ "TXT",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_txt },
	{ "RP",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rp },
	{ "AFSDB",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_afsdb },
	{ "X25",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_x25 },
	{ "ISDN",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_isdn },
	{ "RT",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rt },
	{ "NSAP",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsap },
	{ "NSAP-PTR",  TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsap_ptr },
	{ "SIG",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_sig },
	{ "KEY",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_key },
	{ "PX",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_px },
	{ "GPOS",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_gpos },
	{ "AAAA",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_aaaa },
	{ "LOC",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_loc },
	{ "NXT",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nxt },
	{ "SRV",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_srv },
	{ "NAPTR",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_naptr },
	{ "KX",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_kx },
	{ "CERT",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cert },
	{ "A6",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_a6 },
	{ "DNAME",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dname },
	{ "APL",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_apl },
	{ "DS",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ds },
	{ "SSHFP",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_sshfp },
	{ "IPSECKEY",  TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ipseckey },
	{ "RRSIG",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rrsig },
	{ "NSEC",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec },
	{ "DNSKEY",    TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },
	{ "DHCID",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dhcid },
	{ "NSEC3",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec3 },
	{ "NSEC3PARAM",TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec3param },
	{ "TLSA",      TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_tlsa },
	{ "HIP",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_hip },
	{ "CDS",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cds },
	{ "CDNSKEY",   TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cdnskey },
	{ "OPENPGPKEY",TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_openpgpkey },
	{ "SPF",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_spf },
	{ "NID",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nid },
	{ "L32",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_l32 },
	{ "L64",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_l34 },
	{ "LP",        TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_lp },
	{ "EUI48",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_eui48 },
	{ "EUI64",     TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_eui64 },
	{ "URI",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_uri },
	{ "CAA",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_caa },
	{ "DLV",       TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dlv },
	{ "TYPE1000",  TYPE,  0, "",    0, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 1000 }
281
282
};

Mark Andrews's avatar
Mark Andrews committed
283
284
285
/*
 * Summary structure where results from multiple lookups are recorded.
 */
286
struct summary {
Mark Andrews's avatar
Mark Andrews committed
287
288
289
290
291
	struct {
		struct summary *prev;
		struct summary *next;
		int linked;
	} link;
Mark Andrews's avatar
Mark Andrews committed
292
293
294
	char zone[1024];		/* the zone's name */
	char ns[1024];			/* the server's name */
	struct sockaddr_storage storage;/* server we are talking to */
Mark Andrews's avatar
Mark Andrews committed
295
	int tests;			/* number of outstanding tests */
Mark Andrews's avatar
Mark Andrews committed
296
	int last;			/* last test sent */
Mark Andrews's avatar
Mark Andrews committed
297
	int deferred;			/* was the printing deferred */
Mark Andrews's avatar
Mark Andrews committed
298
	int done;			/* we are done */
Mark Andrews's avatar
Mark Andrews committed
299
300
301
302
303
304
	int type;			/* recursive query lookup type */
	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 */
305
306
	int faileda;
	int failedaaaa;
Mark Andrews's avatar
Mark Andrews committed
307
	int seenrrsig;			/* a rrsig was seen in "do" test */
308
309
310
311
312
313
	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 */
Mark Andrews's avatar
Mark Andrews committed
314
	struct summary *xlink;		/* cross link of recursive A/AAAA */
315
316
317
	char results[sizeof(opts)/sizeof(opts[0])][100];
};

Mark Andrews's avatar
Mark Andrews committed
318
319
320
321
322
static struct {
	struct summary *head;
	struct summary *tail;
} summaries;

Mark Andrews's avatar
Mark Andrews committed
323
324
325
326
struct workitem {
	struct {
		struct workitem *next;
		struct workitem *prev;
327
328
		int linked;
	} link, clink, rlink, idlink;
Mark Andrews's avatar
Mark Andrews committed
329
330
331
	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 */
Mark Andrews's avatar
Mark Andrews committed
332
333
334
	int test;			/* test number / server number */
	int sends;			/* number of times this UDP request
					 * has been sent */
Mark Andrews's avatar
Mark Andrews committed
335
336
	int buflen;			/* the size of the request to be sent */
	int tcpfd;			/* the tcp file descriptor */
Mark Andrews's avatar
Mark Andrews committed
337
338
	int outstanding;		/* outstanding has been set */
	int havelen;			/* readlen is tcp message length */
Mark Andrews's avatar
Mark Andrews committed
339
340
341
342
343
	int readlen;			/* how much we need to read */
	int read;			/* how much has been read so far */
	unsigned char buf[512];		/* the question we sent */
	unsigned char tcpbuf[0x10000];	/* where to accumulate the tcp response */
	struct summary *summary;	/* where this test is summaried */
Mark Andrews's avatar
Mark Andrews committed
344
345
};

346
/*
Mark Andrews's avatar
Mark Andrews committed
347
 * Work queues:
348
349
350
 *	'work' udp qeries;
 *	'connecting' tcp qeries;
 *	'reading' tcp qeries;
Mark Andrews's avatar
Mark Andrews committed
351
352
353
 *
 * Outstanding queries by qid.
 *	'ids'
354
 */
Mark Andrews's avatar
Mark Andrews committed
355
356
357
static struct {
	struct workitem *head;
	struct workitem *tail;
358
} work, connecting, reading, ids[0x10000];
Mark Andrews's avatar
Mark Andrews committed
359

Mark Andrews's avatar
Mark Andrews committed
360
361
362
static void
dotest(struct workitem *item);

363
364
365
static void
nextserver(struct workitem *item);

366
367
static void
connecttoserver(struct workitem *item);
Mark Andrews's avatar
Mark Andrews committed
368

Mark Andrews's avatar
Mark Andrews committed
369
370
371
static void
report(struct summary *summary);

Mark Andrews's avatar
Mark Andrews committed
372
373
374
375
376
377
378
379
380
381
382
383
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_len != s2->ss_len || 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;
Mark Andrews's avatar
Mark Andrews committed
384

Mark Andrews's avatar
Mark Andrews committed
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
		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);
}

Mark Andrews's avatar
Mark Andrews committed
401
402
403
/*
 * Check if it is safe to use this id to this address.
 */
Mark Andrews's avatar
Mark Andrews committed
404
405
406
407
408
409
static int
checkid(struct sockaddr_storage *storage, int id) {
	struct workitem *item;

	item = HEAD(ids[id]);
	while (item != NULL &&
410
	       !storage_equal(storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
411
412
413
414
		item = NEXT(item, idlink);
	return ((item == NULL) ? 1 : 0);
}

Mark Andrews's avatar
Mark Andrews committed
415
416
417
418
419
420
421
static void
freesummary(struct summary *summary) {
	if (LINKED(summary, link))
		UNLINK(summaries, summary, link);
	free(summary);
}

Mark Andrews's avatar
Mark Andrews committed
422
423
424
/*
 * Generate a report line.
 */
425
static void
Mark Andrews's avatar
Mark Andrews committed
426
printandfree(struct summary *summary) {
427
428
429
430
431
432
433
434
	struct sockaddr_in *s = (struct sockaddr_in *)&summary->storage;
	struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&summary->storage;;
	char addrbuf[64];
	void *addr;
	unsigned int i;
	int x;

	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
Mark Andrews's avatar
Mark Andrews committed
435
	    summary->nodataa && summary->nodataaaaa) {
436
437
		printf("%s. %s: no address records found\n",
		       summary->zone, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
438
		freesummary(summary);
439
440
		return;
	}
441

442
443
444
445
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    summary->nxdomaina && summary->nxdomainaaaa) {
		printf("%s. %s: no address records found (NXDOMAIN)\n",
		       summary->zone, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
446
		freesummary(summary);
447
448
		return;
	}
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
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    summary->faileda && summary->failedaaaa) {
		printf("%s. %s: address lookups failed\n",
		       summary->zone, summary->ns);
		freesummary(summary);
		return;
	}

	/*
	 * Do deferred xlink failure reports.
	 */
	if (summary->type == ns_t_a && summary->nodataa && summary->failedaaaa) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA lookup failed\n");
		freesummary(summary);
		return;
	}
	if (summary->type == ns_t_aaaa && summary->nodataaaaa && summary->faileda) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A lookup failed\n");
		freesummary(summary);
		return;
	}
	if (summary->type == ns_t_a && summary->faileda && summary->nxdomainaaaa) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA nxdomain\n");
		freesummary(summary);
		return;
	}
	if (summary->type == ns_t_aaaa && summary->failedaaaa && summary->nxdomaina) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A nxdomain\n");
		freesummary(summary);
		return;
	}
484

Mark Andrews's avatar
Mark Andrews committed
485
	if (summary->done || summary->nodataa || summary->nodataaaaa) {
Mark Andrews's avatar
Mark Andrews committed
486
		freesummary(summary);
487
488
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
489

490
491
492
	if (summary->type != 0 && summary->nxdomain) {
		if (summary->type == ns_t_ns) {
			printf("%s.: NS nxdomain\n", summary->zone);
Mark Andrews's avatar
Mark Andrews committed
493
			freesummary(summary);
494
495
496
497
498
499
			return;
		}
		printf("%s. %s:", summary->zone, summary->ns);
		if (summary->type == ns_t_a) printf(" A");
		if (summary->type == ns_t_aaaa) printf(" AAAA");
		printf(" nxdomain\n");
Mark Andrews's avatar
Mark Andrews committed
500
		freesummary(summary);
501
502
503
504
505
		return;
	}
	if (summary->type == ns_t_a) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A lookup failed\n");
Mark Andrews's avatar
Mark Andrews committed
506
		freesummary(summary);
507
508
509
510
511
		return;
	}
	if (summary->type == ns_t_aaaa) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAA lookup failed\n");
Mark Andrews's avatar
Mark Andrews committed
512
		freesummary(summary);
513
514
515
		return;
	}
	if (summary->type == ns_t_ns) {
Mark Andrews's avatar
Mark Andrews committed
516
		printf("%s.:", summary->zone);
517
		printf(" NS lookup failed\n");
Mark Andrews's avatar
Mark Andrews committed
518
		freesummary(summary);
519
520
		return;
	}
521

522
523
524
525
526
	if (summary->seenopt && (summary->allrefused && summary->allservfail))
		summary->seenedns = 1;

	if (summary->type != 0 || (summary->allok && bad) ||
	    (!summary->seenfailure && !summary->seenedns && ednsonly)) {
Mark Andrews's avatar
Mark Andrews committed
527
		freesummary(summary);
Mark Andrews's avatar
Mark Andrews committed
528
529
530
		return;
	}

531
532
533
534
535
	switch (summary->storage.ss_family) {
	case AF_INET: addr = &s->sin_addr; break;
	case AF_INET6: addr = &s6->sin6_addr; break;
	default: addr = NULL; break;
	}
Mark Andrews's avatar
Mark Andrews committed
536

537
538
539
540
541
542
	if (addr == NULL)
		strlcpy(addrbuf, "<unknown>", sizeof(addrbuf));
	else
		inet_ntop(summary->storage.ss_family, addr,
			  addrbuf, sizeof(addrbuf));

543
544
545
	x = -1;
	printf("%s. @%s (%s.):", summary->zone, addrbuf, summary->ns);
	for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
546
547
		if (opts[i].what != 0 && (opts[i].what & what) == 0)
			continue;
548
		if (summary->results[i][0] == 0)
549
			strlcpy(summary->results[i], "skipped", 100);
550
551
552
553
554
555
556
557
558
559
		if (strcmp(opts[i].name, "do") == 0)
			x = i;
		if (strcmp(opts[i].name, "ednstcp") == 0 && x != -1) {
			printf(" signed=%s", summary->results[x]);
			if (summary->seenrrsig)
				printf(",yes");
		}
		printf(" %s=%s", opts[i].name, summary->results[i]);
	}
	printf("\n");
Mark Andrews's avatar
Mark Andrews committed
560
561
562
563
564
565
	freesummary(summary);
}

static void
report(struct summary *summary) {

Mark Andrews's avatar
Mark Andrews committed
566
567
568
569
	/*
	 * Send the next test now that we have completed the last test.
	 */
	if (serial && summary->type == 0 && summary->tests == 1) {
570
571
572
		for (summary->last++;
		     summary->last < sizeof(opts)/sizeof(opts[0]);
		     summary->last++) {
Mark Andrews's avatar
Mark Andrews committed
573
			struct workitem *item;
574
575
			if (opts[summary->last].what != 0 &&
			    (opts[summary->last].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
576
577
578
579
580
				continue;
			item = calloc(1, sizeof(*item));
			if (item == NULL)
				continue;
			item->summary = summary;
581
			item->test = item->summary->last;
Mark Andrews's avatar
Mark Andrews committed
582
583
584
585
586
			dotest(item);
			return;
		}
	}

Mark Andrews's avatar
Mark Andrews committed
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
	/*
	 * Have all the tests completed?
	 */
	summary->tests--;
	if (summary->tests)
		return;

	/*
	 * If we are cross linked record the lookup details on the other
	 * structure.
	 */
	if (summary->xlink) {
		if (summary->nodataa) {
			summary->xlink->nodataa = 1;
			summary->done = 1;
		}
		if (summary->nodataaaaa) {
			summary->xlink->nodataaaaa = 1;
			summary->done = 1;
		}
		if (summary->nxdomaina) {
			summary->xlink->nxdomaina = 1;
			summary->done = 1;
		}
		if (summary->nxdomainaaaa) {
			summary->xlink->nxdomainaaaa = 1;
			summary->done = 1;
		}
615
616
617
618
619
620
621
622
623
624
		if (!summary->done) {
			if (summary->type == ns_t_a) {
				summary->xlink->faileda = 1;
				summary->done = 1;
			}
			if (summary->type == ns_t_aaaa) {
				summary->xlink->failedaaaa = 1;
				summary->done = 1;
			}
		}
Mark Andrews's avatar
Mark Andrews committed
625
626
627
628
629
630
631
632
633
634
635

		/*
		 * Remove the cross link.
		 */
		summary->xlink->xlink = NULL;
		summary->xlink = NULL;
		if (summary->done) {
			freesummary(summary);
			goto print_deferred;
		}
	}
636
637
638
639
640
641
	if (!summary->done) {
		if (summary->type == ns_t_a)
			summary->faileda = 1;
		if (summary->type == ns_t_aaaa)
			summary->failedaaaa = 1;
	}
Mark Andrews's avatar
Mark Andrews committed
642
643
644
645
646
647
648
649
650
651

	if (inorder && PREV(summary, link)) {
		summary->deferred = 1;
		return;
	}
	printandfree(summary);

 print_deferred:
	while ((summary = HEAD(summaries)) && summary->deferred)
		printandfree(summary);
652
653
}

Mark Andrews's avatar
Mark Andrews committed
654
655
656
/*
 * Free a work item and unlink.
 */
657
658
static void
freeitem(struct workitem * item) {
Mark Andrews's avatar
Mark Andrews committed
659
660
	if (item->summary)
		report(item->summary);
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
	if (item->tcpfd != 0) {
		FD_CLR(item->tcpfd, &rfds);
		FD_CLR(item->tcpfd, &wfds);
		rhandlers[item->tcpfd] = NULL;
		whandlers[item->tcpfd] = NULL;
		close(item->tcpfd);
	}
	if (item->outstanding)
		outstanding--;
	if (LINKED(item, link))
		UNLINK(work, item, link);
	if (LINKED(item, rlink))
		UNLINK(reading, item, rlink);
	if (LINKED(item, clink))
		UNLINK(connecting, item, clink);
	if (LINKED(item, idlink))
		UNLINK(ids[item->id], item, idlink);
	free(item);
}

Mark Andrews's avatar
Mark Andrews committed
681
682
683
/*
 * Add a tag to the report.
 */
684
685
686
static void
addtag(struct workitem *item, char *tag) {
	char *result = item->summary->results[item->test];
687
688
	if (result[0]) strlcat(result, ",", 100);
	strlcat(result, tag, 100);
689
690
}

Mark Andrews's avatar
Mark Andrews committed
691
692
693
/*
 * Resend a UDP request.
 */
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
static void
resend(struct workitem *item) {
	int n, fd = -1;

	switch (item->summary->storage.ss_family) {
	case AF_INET:
		fd = udp4;
		break;
	case AF_INET6:
		fd = udp6;
		break;
	}

	if (fd == -1) {
		addtag(item, "skipped");
709
		item->summary->allok = 0;
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
		freeitem(item);
		return;
	}

	if (outstanding > maxoutstanding) {
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		UNLINK(work, item, link);
		APPEND(work, item, link);
		return;
	}

	n = sendto(fd, item->buf, item->buflen, 0,
		   (struct sockaddr *)&item->summary->storage,
		   item->summary->storage.ss_len);
	if (n > 0) {
		if (debug)
			printf("resend %s rdlen=%u udpsize=%u flags=%04x "
			       "version=%u tcp=%u ignore=%u id=%u\n",
			       opts[item->test].name, opts[item->test].rdlen,
			       opts[item->test].udpsize, opts[item->test].flags,
			       opts[item->test].version, opts[item->test].tcp,
			       opts[item->test].ignore, item->id);
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
		UNLINK(work, item, link);
		APPEND(work, item, link);
740
741
	} else if (item->summary->type) {
		nextserver(item);
742
743
	} else {
		addtag(item, "failed");
744
745
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
746
747
748
749
		freeitem(item);
	}
}

Mark Andrews's avatar
Mark Andrews committed
750
751
752
/*
 * Start a individual test.
 */
Mark Andrews's avatar
Mark Andrews committed
753
754
755
756
static void
dotest(struct workitem *item) {
	unsigned char *cp;
	unsigned int ttl;
Mark Andrews's avatar
Mark Andrews committed
757
	int n, fd, id, tries = 0, opcode;
Mark Andrews's avatar
Mark Andrews committed
758

759
	switch (item->summary->storage.ss_family) {
Mark Andrews's avatar
Mark Andrews committed
760
761
762
763
764
765
766
767
768
	case AF_INET:
		fd = udp4;
		break;
	case AF_INET6:
		fd = udp6;
		break;
	}

	if (fd == -1) {
769
		addtag(item, "skipped");
770
		item->summary->allok = 0;
771
		freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
772
773
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
774

Mark Andrews's avatar
Mark Andrews committed
775
776
777
778
	/*
	 * res_mkquery only really knows about QUERY but it is useful
	 * for initalising the header when the opcode isn't QUERY.
	 */
Mark Andrews's avatar
Mark Andrews committed
779
780
781
782
783
784
785
786
	opcode = opts[item->test].opcode;
	switch (opcode) {
	case 0: break;
	default:
		opcode = ns_o_query;
	}

	n = res_mkquery(opcode, item->summary->zone, ns_c_in,
787
788
			opts[item->test].type, NULL, 0, NULL,
			item->buf, sizeof(item->buf));
Mark Andrews's avatar
Mark Andrews committed
789
790
791
792
793
794
795
796
	/* fixup opcode? */
	if (n > 0 && opts[item->test].opcode != opcode) {
		item->buf[2] &= 0x17;
		item->buf[2] |= (opts[item->test].opcode & 0x0f) << 3;
		/* Zero question section. */
		if (opts[item->test].opcode == 15)
			item->buf[4] = item->buf[5] = 0;
	}
Mark Andrews's avatar
Mark Andrews committed
797
798
799
800

	/*
	 * Set DNS flags as specified by test.
	 */
801
802
	if (opts[item->test].tc)
		item->buf[2] |= 0x2;	/* set tc */
Mark Andrews's avatar
Mark Andrews committed
803
804
805
806
	if (opts[item->test].rd)
		item->buf[2] |= 0x1;	/* set rd */
	else
		item->buf[2] &= ~0x1;	/* clear rd */
807
808
809
810
811
812
813
814
	if (opts[item->test].ra)
		item->buf[3] |= 0x80;	/* set ra */
	if (opts[item->test].z)
		item->buf[3] |= 0x40;	/* set z */
	if (opts[item->test].ad)
		item->buf[3] |= 0x20;	/* set ad */
	if (opts[item->test].cd)
		item->buf[3] |= 0x10;	/* set cd */
Mark Andrews's avatar
Mark Andrews committed
815

816
817
	if (n > 0) {
		char name[1024];
Mark Andrews's avatar
Mark Andrews committed
818
819
820
		/*
		 * Make zone canonical.
		 */
821
822
		dn_expand(item->buf, item->buf + n, item->buf + 12,
			  name, sizeof(name));
823
824
		strlcpy(item->summary->zone, name,
			sizeof(item->summary->zone));
825
	}
Mark Andrews's avatar
Mark Andrews committed
826

Mark Andrews's avatar
Mark Andrews committed
827
828
829
830
831
	/*
	 * Add OPT record if required by test.
	 */
	if (n > 0 && opts[item->test].udpsize > 0 &&
	    11 + opts[item->test].rdlen < 512 - n) {
Mark Andrews's avatar
Mark Andrews committed
832
		cp = item->buf + n;
833
834
835
836
837
838
839
840
841
842
843
		*cp++ = 0;				/* name */
		ns_put16(ns_t_opt, cp);			/* type */
		cp += 2;
		ns_put16(opts[item->test].udpsize, cp);	/* class */
		cp += 2;
		ttl = (opts[item->test].version << 16) |
		      opts[item->test].flags;
		ns_put32(ttl, cp);			/* ttl */
		cp += 4;
		ns_put16(opts[item->test].rdlen, cp);	/* rdlen */
		cp += 2;
Mark Andrews's avatar
Mark Andrews committed
844
845
		memcpy(cp, opts[item->test].rdata, opts[item->test].rdlen);
		cp += opts[item->test].rdlen;
846
847
		item->buf[11] = 1;			/* adcount */
		n = cp - item->buf;			/* total length */
Mark Andrews's avatar
Mark Andrews committed
848
849
850
	}

	if (n > 0) {
Mark Andrews's avatar
Mark Andrews committed
851
852
853
		/*
		 * Adjust id if it clashes with a outstanding request.
		 */
Mark Andrews's avatar
Mark Andrews committed
854
855
		id = item->buf[0] << 8 | item->buf[1];

856
857
		while (!checkid(&item->summary->storage, id) &&
		       tries++ < 0xffff)
Mark Andrews's avatar
Mark Andrews committed
858
			id = (id + 1) & 0xffff;
Mark Andrews's avatar
Mark Andrews committed
859

Mark Andrews's avatar
Mark Andrews committed
860
		if (tries == 0xffff) {
861
			addtag(item, "skipped");
862
			item->summary->allok = 0;
863
			freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
864
865
			return;
		}
Mark Andrews's avatar
Mark Andrews committed
866

Mark Andrews's avatar
Mark Andrews committed
867
868
869
870
871
		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		item->buflen = n;

872
873
874
875
876
		if (opts[item->test].tcp) {
			connecttoserver(item);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
877
878
879
		/*
		 * If there is too much outstanding work queue this item.
		 */
880
881
882
883
884
885
886
887
		if (outstanding > maxoutstanding) {
			gettimeofday(&item->when, NULL);
			item->when.tv_sec += 1;
			APPEND(work, item, link);
			APPEND(ids[item->id], item, idlink);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
888
		n = sendto(fd, item->buf, item->buflen, 0,
889
890
			   (struct sockaddr *)&item->summary->storage,
			   item->summary->storage.ss_len);
Mark Andrews's avatar
Mark Andrews committed
891
892
893
	}

	if (n > 0) {
894
895
896
897
898
899
900
901
902
		if (debug)
			printf("%s rdlen=%u udpsize=%u flags=%04x version=%u "
			       "tcp=%u ignore=%u id=%u\n",
			       opts[item->test].name, opts[item->test].rdlen,
			       opts[item->test].udpsize, opts[item->test].flags,
			       opts[item->test].version, opts[item->test].tcp,
			       opts[item->test].ignore, item->id);
		if (!item->outstanding++)
			outstanding++;
Mark Andrews's avatar
Mark Andrews committed
903
		gettimeofday(&item->when, NULL);
904
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
905
906
907
		item->sends = 1;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
908
909
	} else {
		addtag(item, "failed");
910
911
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
912
913
		freeitem(item);
	}
Mark Andrews's avatar
Mark Andrews committed
914
915
}

Mark Andrews's avatar
Mark Andrews committed
916
917
918
/*
 * Start a series of tests.
 */
Mark Andrews's avatar
Mark Andrews committed
919
static void
Mark Andrews's avatar
Mark Andrews committed
920
check(char *zone, char *ns, char *address, struct summary *parent) {
Mark Andrews's avatar
Mark Andrews committed
921
	size_t i;
Mark Andrews's avatar
Mark Andrews committed
922
	int fd;
Mark Andrews's avatar
Mark Andrews committed
923
924
925
	struct in_addr addr;
	struct in6_addr addr6;
	struct sockaddr_storage storage;
926
	struct summary *summary;
Mark Andrews's avatar
Mark Andrews committed
927

Mark Andrews's avatar
Mark Andrews committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
	memset(&storage, 0, sizeof(storage));
	if (inet_pton(AF_INET6, address, &addr6) == 1) {
		struct sockaddr_in6 *s = (struct sockaddr_in6 *)&storage;
		s->sin6_len = sizeof(struct sockaddr_in6);
		s->sin6_family = AF_INET6;
		s->sin6_port = htons(53);
		s->sin6_addr = addr6;
		fd = udp6;
	} else if (inet_pton(AF_INET, address, &addr) == 1) {
		struct sockaddr_in *s = (struct sockaddr_in *)&storage;
		s->sin_len = sizeof(struct sockaddr_in);
		s->sin_family = AF_INET;
		s->sin_port = htons(53);
		s->sin_addr = addr;
		fd = udp4;
	} else
		return;

	if (fd == -1)
		return;

949
950
951
	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return;
Mark Andrews's avatar
Mark Andrews committed
952

Mark Andrews's avatar
Mark Andrews committed
953
954
955
	/*
	 * Hold a reference until all the tests have been initiated.
	 */
Mark Andrews's avatar
Mark Andrews committed
956
957
958
959
960
	summary->tests++;
	if (parent)
		INSERTBEFORE(summaries, parent, summary, link);
	else
		APPEND(summaries, summary, link);
Mark Andrews's avatar
Mark Andrews committed
961

962
	summary->storage = storage;
963
964
965
	summary->allok = 1;
	summary->allrefused = 1;
	summary->allservfail = 1;
Mark Andrews's avatar
Mark Andrews committed
966

967
968
969
	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;
Mark Andrews's avatar
Mark Andrews committed
970

971
972
973
	ns_makecanon(ns, summary->ns, sizeof(summary->ns));
	i = strlen(summary->ns);
	if (i) summary->ns[i-1] = 0;
Mark Andrews's avatar
Mark Andrews committed
974

Mark Andrews's avatar
Mark Andrews committed
975
	for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
Mark Andrews's avatar
style    
Mark Andrews committed
976
		struct workitem *item;
977
978
		if (opts[i].what != 0 && (opts[i].what & what) == 0)
			continue;
Mark Andrews's avatar
Mark Andrews committed
979

Mark Andrews's avatar
style    
Mark Andrews committed
980
		item = calloc(1, sizeof(*item));
Mark Andrews's avatar
Mark Andrews committed
981
		if (item == NULL)
Mark Andrews's avatar
Mark Andrews committed
982
			break;
983
		item->summary = summary;
Mark Andrews's avatar
Mark Andrews committed
984
		item->summary->tests++;
Mark Andrews's avatar
Mark Andrews committed
985
		item->summary->last = item->test = i;
Mark Andrews's avatar
Mark Andrews committed
986
		dotest(item);
Mark Andrews's avatar
Mark Andrews committed
987
988
		if (serial)
			break;
Mark Andrews's avatar
Mark Andrews committed
989
	}
Mark Andrews's avatar
Mark Andrews committed
990
	report(summary);	/* Release reference. */
Mark Andrews's avatar
Mark Andrews committed
991
992
}

993
994
995
static char *
rcodetext(int code) {
	static char buf[64];
Mark Andrews's avatar
Mark Andrews committed
996

997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
	switch(code) {
	case 0: return("noerror");
	case 1: return("formerr");
	case 2: return("servfail");
	case 3: return("nxdomain");
	case 4: return("notimpl");
	case 5: return("refused");
	case 6: return("yxdomain");
	case 7: return("yxrrset");
	case 8: return("nxrrset");
	case 9: return("notauth");
	case 10: return("notzone");
	case 16: return("badvers");
	case 23: return("badcookie");
	default:
Mark Andrews's avatar
Mark Andrews committed
1012
		snprintf(buf, sizeof(buf), "rcode%u", code);
1013
1014
1015
1016
		return (buf);
	}
}

Mark Andrews's avatar
Mark Andrews committed
1017
1018
1019
/*
 * Start a lookup using the recursive server(s).
 */
1020
1021
1022
1023
1024
1025
1026
1027
static void
dolookup(struct workitem *item, int type) {
	char name[1024];
	int n, fd = -1;

	item->summary->tests++;
	item->summary->type = item->type = type;

1028
 again:
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
	if (servers[item->test].sin.sin_family == AF_INET)
		memcpy(&item->summary->storage, &servers[item->test].sin,
		       sizeof(servers[item->test].sin));
	else
		memcpy(&item->summary->storage, &servers[0].sin6,
		       sizeof(servers[item->test].sin6));

	switch (item->summary->storage.ss_family) {
	case AF_INET:
		fd = udp4;
		break;
	case AF_INET6:
		fd = udp6;
		break;
	}

	if (fd == -1) {
1046
1047
		if (++item->test < nservers)
			goto again;
1048
		addtag(item, "skipped");
1049
1050
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
1051
1052
1053
		freeitem(item);
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
1054

1055
1056
1057
1058
1059
1060
1061
1062
1063
	if (type == ns_t_ns)
		n = res_mkquery(ns_o_query, item->summary->zone, ns_c_in,
				type, NULL, 0, NULL,
				item->buf, sizeof(item->buf));
	else
		n = res_mkquery(ns_o_query, item->summary->ns, ns_c_in,
				type, NULL, 0, NULL,
				item->buf, sizeof(item->buf));
	if (n > 0) {
Mark Andrews's avatar
Mark Andrews committed
1064
		int id, tries = 0;
1065

Mark Andrews's avatar
Mark Andrews committed
1066
1067
1068
		/*
		 * Make name canonical.
		 */
1069
		dn_expand(item->buf, item->buf + n, item->buf + 12,
Mark Andrews's avatar
Mark Andrews committed
1070
1071
1072
1073
			  name, sizeof(name));

		switch (type) {
		case ns_t_ns:
1074
1075
			strlcpy(item->summary->zone, name,
				sizeof(item->summary->zone));
Mark Andrews's avatar
Mark Andrews committed
1076
1077
1078
			break;
		case ns_t_a:
		case ns_t_aaaa:
1079
1080
			strlcpy(item->summary->ns, name,
				sizeof(item->summary->zone));
Mark Andrews's avatar
Mark Andrews committed
1081
1082
1083
1084
1085
			break;
		}

		item->buf[2] |= 0x1;	/* set rd */
		id = item->buf[0] << 8 | item->buf[1];
1086
1087
1088
1089

		while (!checkid(&item->summary->storage, id) &&
		       tries++ < 0xffff)
			id = (id + 1) & 0xffff;
Mark Andrews's avatar
Mark Andrews committed
1090

1091
		if (tries == 0xffff) {
1092
			nextserver(item);
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
			return;
		}

		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		item->buflen = n;
		if (outstanding > maxoutstanding) {
			gettimeofday(&item->when, NULL);
			item->when.tv_sec += 1;
			APPEND(work, item, link);
			APPEND(ids[item->id], item, idlink);
			return;
		}
Mark Andrews's avatar
Mark Andrews committed
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
		n = sendto(fd, item->buf, item->buflen, 0,
			   (struct sockaddr *)&item->summary->storage,
			   item->summary->storage.ss_len);
	}
	if (n > 0) {
		if (debug)
			printf("lookup %u id=%u\n", item->type, item->id);
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
1122
1123
	} else 
		nextserver(item);
1124
1125
}

Mark Andrews's avatar
Mark Andrews committed
1126
1127
1128
/*
 * Start a A lookup.
 */
1129
static struct summary *
Mark Andrews's avatar
Mark Andrews committed
1130
lookupa(char *zone, char *ns, struct summary *parent) {
1131
1132
1133
1134
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

Mark Andrews's avatar
Mark Andrews committed
1135
1136
1137
	if (ipv6only)
		return (NULL);

1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return (NULL);

	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;

	ns_makecanon(ns, summary->ns, sizeof(summary->ns));
	i = strlen(summary->ns);
	if (i) summary->ns[i-1] = 0;

	item = calloc(1, sizeof(*item));
Mark Andrews's avatar
Mark Andrews committed
1151
	if (item == NULL) {
Mark Andrews's avatar
Mark Andrews committed
1152
1153
		free(summary);
		return (NULL);
Mark Andrews's avatar
Mark Andrews committed
1154
	}
Mark Andrews's avatar
Mark Andrews committed
1155
1156
1157
1158
	if (parent)
		INSERTBEFORE(summaries, parent, summary, link);
	else
		APPEND(summaries, summary, link);
1159
1160

	item->summary = summary;
Mark Andrews's avatar
Mark Andrews committed
1161
1162
1163
	/*
	 * Hold a reference so that caller can xlink.
	 */
Mark Andrews's avatar
Mark Andrews committed
1164
	summary->tests++;
1165
1166
1167
1168
	dolookup(item, ns_t_a);
	return (summary);
}

Mark Andrews's avatar
Mark Andrews committed
1169
1170
1171
/*
 * Start a AAAA lookup.
 */
1172
static struct summary *
Mark Andrews's avatar
Mark Andrews committed
1173
lookupaaaa(char *zone, char *ns, struct summary *parent) {
1174
1175
1176
1177
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

Mark Andrews's avatar
Mark Andrews committed
1178
1179
1180
	if (ipv6only)
		return (NULL);

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return (NULL);

	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;

	ns_makecanon(ns, summary->ns, sizeof(summary->ns));
	i = strlen(summary->ns);
	if (i) summary->ns[i-1] = 0;

	item = calloc(1, sizeof(*item));
Mark Andrews's avatar
Mark Andrews committed
1194
	if (item == NULL) {
Mark Andrews's avatar
Mark Andrews committed
1195
1196
		free(summary);
		return (NULL);
Mark Andrews's avatar
Mark Andrews committed
1197
	}
Mark Andrews's avatar
Mark Andrews committed
1198
1199
1200
1201
	if (parent)
		INSERTBEFORE(summaries, parent, summary, link);
	else
		APPEND(summaries, summary, link);
1202
1203

	item->summary = summary;
Mark Andrews's avatar
Mark Andrews committed
1204
1205
1206
	/*
	 * Hold a reference so that caller can xlink.
	 */
Mark Andrews's avatar
Mark Andrews committed
1207
	summary->tests++;
1208
1209
1210
1211
	dolookup(item, ns_t_aaaa);
	return (summary);
}

Mark Andrews's avatar
Mark Andrews committed
1212
1213
1214
/*
 * Start a NS lookup.
 */
1215
1216
1217
1218
1219
1220
1221
1222
1223
static void
lookupns(char *zone) {
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return;
Mark Andrews's avatar
Mark Andrews committed
1224
1225
1226
1227

	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;
1228
1229

	item = calloc(1, sizeof(*item));
Mark Andrews's avatar
Mark Andrews committed
1230
	if (item == NULL) {
Mark Andrews's avatar
Mark Andrews committed
1231
1232
		free(summary);
		return;
Mark Andrews's avatar
Mark Andrews committed
1233
	}
1234

Mark Andrews's avatar
Mark Andrews committed
1235
	APPEND(summaries, summary, link);
1236
1237
1238
1239
	item->summary = summary;
	dolookup(item, ns_t_ns);
}

Mark Andrews's avatar
Mark Andrews committed
1240
1241
1242
/*
 * Process a recieved response.
 */
1243