genreport.c 86.8 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2016, 2017  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3 4 5 6 7 8
 *
 * 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/.
 */

9 10
#define FD_SETSIZE 1600

11 12
#include <config.h>

Mark Andrews's avatar
Mark Andrews committed
13
#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
14 15
#include <string.h>
#include <stdlib.h>
16
#include <stddef.h>
Mark Andrews's avatar
Mark Andrews committed
17
#include <unistd.h>
Mark Andrews's avatar
Mark Andrews committed
18
#include <assert.h>
19
#include <ctype.h>
Mark Andrews's avatar
Mark Andrews committed
20

Mark Andrews's avatar
Mark Andrews committed
21 22 23 24 25 26 27
#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>
28 29
#include <sys/ioctl.h>
#include <sys/uio.h>
30 31 32 33 34 35
#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>
Mark Andrews's avatar
Mark Andrews committed
36 37

#include <errno.h>
38
#include <netdb.h>
Mark Andrews's avatar
Mark Andrews committed
39
#include <resolv.h>
40
#include <signal.h>
Mark Andrews's avatar
Mark Andrews committed
41

42 43 44 45
#ifndef FD_COPY
#define FD_COPY(x, y) memmove(y, x, sizeof(*x))
#endif

Mark Andrews's avatar
Mark Andrews committed
46 47 48 49 50 51
#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
52
#define ns_t_rrsig 46
Mark Andrews's avatar
Mark Andrews committed
53
#define ns_t_nsec 47
Mark Andrews's avatar
Mark Andrews committed
54
#define ns_t_dnskey 48
Mark Andrews's avatar
Mark Andrews committed
55 56 57 58
#define ns_t_dhcid 49
#define ns_t_nsec3 50
#define ns_t_nsec3param 51
#define ns_t_tlsa 52
59
#define ns_t_smimea 53
Mark Andrews's avatar
Mark Andrews committed
60 61 62 63 64
#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
65
#define ns_t_openpgpkey 61
Mark Andrews's avatar
Mark Andrews committed
66 67 68 69 70 71 72 73 74 75
#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
76 77
#define ns_t_avc 258
#define ns_t_doa 259
Mark Andrews's avatar
Mark Andrews committed
78 79
#define ns_t_ta 32768
#define ns_t_dlv 32769
Mark Andrews's avatar
Mark Andrews committed
80

81 82
#define ns_r_badcookie 23

83
static int eof = 0;
Mark Andrews's avatar
Mark Andrews committed
84 85
static int maxfd = -1;
static fd_set rfds, wfds;
Mark Andrews's avatar
Mark Andrews committed
86
static int outstanding = 0;
87
static int maxoutstanding = 100;
Mark Andrews's avatar
Mark Andrews committed
88 89 90 91 92 93

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

static int udp4 = -1;
static int udp6 = -1;
94 95
static int icmp4 = -1;
static int icmp6 = -1;
96

Mark Andrews's avatar
Mark Andrews committed
97 98 99
static int ipv4only = 0;
static int ipv6only = 0;

Mark Andrews's avatar
Mark Andrews committed
100
static int allok  = 0;
101
static int bad  = 0;
Mark Andrews's avatar
Mark Andrews committed
102
static int badtag  = 0;
103
static int ednsonly  = 0;
104
static int debug = 0;
Mark Andrews's avatar
Mark Andrews committed
105
static int inorder = 0;
106
static int serial = 0;
107
static int printnsid = 0;
108
static long long sent;
109

110 111

#ifdef HAVE_RES_GETSERVERS
112
static union res_sockaddr_union servers[10];
113 114 115 116 117 118 119
#else
static union {
        struct sockaddr_in sin;
        struct sockaddr_in6 sin6;
} servers[10];
#endif

120
static int nservers = 0;
121
int ident = 0;
Mark Andrews's avatar
Mark Andrews committed
122

Mark Andrews's avatar
Mark Andrews committed
123 124 125
/*
 * Doubly linked list macros.
 */
Mark Andrews's avatar
Mark Andrews committed
126 127 128 129 130 131 132 133
#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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	(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
157
		(item)->link.linked = 1; \
Mark Andrews's avatar
Mark Andrews committed
158
	} \
Mark Andrews's avatar
Mark Andrews committed
159 160 161 162 163 164 165 166 167 168 169 170
} 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; \
171
	(item)->link.linked = 0; \
Mark Andrews's avatar
Mark Andrews committed
172 173 174
} while (0)

#define NEXT(item, link) (item)->link.next
Mark Andrews's avatar
Mark Andrews committed
175
#define PREV(item, link) (item)->link.prev
176
#define LINKED(item, link) (item)->link.linked
Mark Andrews's avatar
Mark Andrews committed
177 178 179 180

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

Mark Andrews's avatar
Mark Andrews committed
181 182 183
/*
 * Test groupings
 */
Mark Andrews's avatar
Mark Andrews committed
184 185 186 187 188 189 190
#define EDNS 0x01
#define COMM 0x02
#define FULL 0x04
#define TYPE 0x08
#define EXPL 0x10

static int what = EDNS;
191

Mark Andrews's avatar
Mark Andrews committed
192
static struct {
Mark Andrews's avatar
Mark Andrews committed
193 194 195 196 197 198 199 200
	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 */
201
	unsigned int cookie;		/* opt record has cookie */
Mark Andrews's avatar
Mark Andrews committed
202 203 204 205 206 207 208 209 210 211
	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 */
Mark Andrews's avatar
Mark Andrews committed
212
	const char *dig;		/* dig command */
213
} opts[] = {
214
	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
215 216 217 218 219 220 221
	{ "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,
Mark Andrews's avatar
Mark Andrews committed
222
	  "dig +noedns +ad +norec SOA <zone>"
Mark Andrews's avatar
Mark Andrews committed
223 224 225 226 227
	},
	{ "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,
228
	  "### dig +noedns +noad +norec +raflag SOA <zone> ###"
Mark Andrews's avatar
Mark Andrews committed
229 230 231 232 233
	},
	{ "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,
234
	  "### dig +noedns +noad +norec +tcflag SOA <zone> ###"
Mark Andrews's avatar
Mark Andrews committed
235 236 237 238 239 240 241 242
	},
	{ "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,
243
	  "### dig +noedns +header-only +opcode=15 +tcflag +rec +raflag +cd +ad +aaflag +zflag ###"
Mark Andrews's avatar
Mark Andrews committed
244 245 246 247 248 249 250
	},
	{ "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>"
	},
251 252

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
253 254 255 256 257 258 259 260 261
	{ "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>"
	},
Mark Andrews's avatar
Mark Andrews committed
262
	{ "ednsopt",   EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
Mark Andrews's avatar
Mark Andrews committed
263 264 265
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
266
	{ "edns1opt",  EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
Mark Andrews's avatar
Mark Andrews committed
267 268 269
				     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>"
	},
270
	{ "do",        EDNS,  0, "",
Mark Andrews's avatar
Mark Andrews committed
271 272 273 274 275 276 277 278 279
				     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>"
	},
	{ "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>"
	},
280
	{ "optlist",   EDNS,  4 + 8 + 4 + 12,
281 282 283 284
	  "\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
285 286 287
				     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>"
	},
288 289

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
290
	{ "ednsnsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
Mark Andrews's avatar
Mark Andrews committed
291 292 293
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
294
	{ "ednscookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
Mark Andrews's avatar
Mark Andrews committed
295 296 297
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
298
	{ "ednsexpire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
Mark Andrews's avatar
Mark Andrews committed
299 300 301
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
302
	{ "ednssubnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
Mark Andrews's avatar
Mark Andrews committed
303 304 305 306
				     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>"
	},

Mark Andrews's avatar
Mark Andrews committed
307
	{ "edns1nsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
Mark Andrews's avatar
Mark Andrews committed
308 309 310
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
311
	{ "edns1cookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
Mark Andrews's avatar
Mark Andrews committed
312 313 314
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
315
	{ "edns1expire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
Mark Andrews's avatar
Mark Andrews committed
316 317 318
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
319
	{ "edns1subnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
Mark Andrews's avatar
Mark Andrews committed
320 321 322 323 324 325
				     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>"
	},
326 327

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
328
	{ "bind11",    COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
Mark Andrews's avatar
Mark Andrews committed
329 330 331
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
332
	{ "dig11",     COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
Mark Andrews's avatar
Mark Andrews committed
333 334 335
				     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>"
	},
336 337

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
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
	{ "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>"
	},
380 381

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
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
	{ "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>"
	},
427 428

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
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
	{ "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>"
	},
468 469

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
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
	{ "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>"
	},
509 510

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
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
	{ "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>"
	}
541 542
};

Mark Andrews's avatar
Mark Andrews committed
543 544 545
/*
 * Summary structure where results from multiple lookups are recorded.
 */
546
struct summary {
Mark Andrews's avatar
Mark Andrews committed
547 548 549 550 551
	struct {
		struct summary *prev;
		struct summary *next;
		int linked;
	} link;
Mark Andrews's avatar
Mark Andrews committed
552 553 554
	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
555
	int tests;			/* number of outstanding tests */
Mark Andrews's avatar
Mark Andrews committed
556
	int last;			/* last test sent */
Mark Andrews's avatar
Mark Andrews committed
557
	int deferred;			/* was the printing deferred */
Mark Andrews's avatar
Mark Andrews committed
558
	int done;			/* we are done */
Mark Andrews's avatar
Mark Andrews committed
559 560 561 562 563 564
	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 */
565 566
	int faileda;
	int failedaaaa;
567 568
	int cnamea;			/* Nameserver is CNAME */
	int cnameaaaa;			/* Nameserver is CNAME */
Mark Andrews's avatar
Mark Andrews committed
569
	int seenrrsig;			/* a rrsig was seen in "do" test */
570 571 572 573 574 575
	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
576
	struct summary *xlink;		/* cross link of recursive A/AAAA */
577 578
	int nsidlen;
	char nsid[100];			/* nsid if found */
579 580 581
	char results[sizeof(opts)/sizeof(opts[0])][100];
};

Mark Andrews's avatar
Mark Andrews committed
582 583 584 585 586
static struct {
	struct summary *head;
	struct summary *tail;
} summaries;

Mark Andrews's avatar
Mark Andrews committed
587 588 589 590
struct workitem {
	struct {
		struct workitem *next;
		struct workitem *prev;
591
		int linked;
592
	} link, clink, plink, rlink, idlink, seqlink;
Mark Andrews's avatar
Mark Andrews committed
593 594 595
	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
596 597 598
	int test;			/* test number / server number */
	int sends;			/* number of times this UDP request
					 * has been sent */
Mark Andrews's avatar
Mark Andrews committed
599 600
	int buflen;			/* the size of the request to be sent */
	int tcpfd;			/* the tcp file descriptor */
Mark Andrews's avatar
Mark Andrews committed
601 602
	int outstanding;		/* outstanding has been set */
	int havelen;			/* readlen is tcp message length */
Mark Andrews's avatar
Mark Andrews committed
603 604 605 606 607
	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
608 609
};

610
/*
Mark Andrews's avatar
Mark Andrews committed
611
 * Work queues:
612 613 614
 *	'work' udp qeries;
 *	'connecting' tcp qeries;
 *	'reading' tcp qeries;
Mark Andrews's avatar
Mark Andrews committed
615
 *	'pending' deferred work items;
Mark Andrews's avatar
Mark Andrews committed
616 617 618
 *
 * Outstanding queries by qid.
 *	'ids'
619 620 621
 *
 * Outstanding icmp by seq.
 *	'ids'
622
 */
Mark Andrews's avatar
Mark Andrews committed
623 624 625
static struct {
	struct workitem *head;
	struct workitem *tail;
626
} work, connecting, reading, pending, ids[0x10000], seq[0x10000];
Mark Andrews's avatar
Mark Andrews committed
627

Mark Andrews's avatar
Mark Andrews committed
628 629 630
static void
dotest(struct workitem *item);

631 632 633
static void
nextserver(struct workitem *item);

634 635
static void
connecttoserver(struct workitem *item);
Mark Andrews's avatar
Mark Andrews committed
636

Mark Andrews's avatar
Mark Andrews committed
637 638 639
static void
report(struct summary *summary);

Mark Andrews's avatar
Mark Andrews committed
640 641 642 643 644
static int
storage_equal(struct sockaddr_storage *s1, struct sockaddr_storage *s2) {
	struct sockaddr_in *sin1, *sin2;
	struct sockaddr_in6 *sin61, *sin62;

645
	if (s1->ss_family != s2->ss_family)
Mark Andrews's avatar
Mark Andrews committed
646 647 648 649 650 651
		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
652

Mark Andrews's avatar
Mark Andrews committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
		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
669 670 671
/*
 * Check if it is safe to use this id to this address.
 */
Mark Andrews's avatar
Mark Andrews committed
672 673 674 675 676 677
static int
checkid(struct sockaddr_storage *storage, int id) {
	struct workitem *item;

	item = HEAD(ids[id]);
	while (item != NULL &&
678
	       !storage_equal(storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
679 680 681 682
		item = NEXT(item, idlink);
	return ((item == NULL) ? 1 : 0);
}

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
/*
 * 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);
}

Mark Andrews's avatar
Mark Andrews committed
698 699 700 701 702 703 704
static void
freesummary(struct summary *summary) {
	if (LINKED(summary, link))
		UNLINK(summaries, summary, link);
	free(summary);
}

Mark Andrews's avatar
Mark Andrews committed
705 706 707
/*
 * Generate a report line.
 */
708
static void
Mark Andrews's avatar
Mark Andrews committed
709
printandfree(struct summary *summary) {
710 711 712 713 714 715 716 717
	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
718
	    summary->nodataa && summary->nodataaaaa) {
719 720
		printf("%s. %s: no address records found\n",
		       summary->zone, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
721
		freesummary(summary);
722 723
		return;
	}
724

725 726 727 728
	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
729
		freesummary(summary);
730 731
		return;
	}
732

733 734 735 736 737 738 739 740
	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;
	}

741 742 743 744 745 746 747 748
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    (summary->cnamea || summary->cnameaaaa)) {
		printf("%s. %s: nameserver is a CNAME\n",
		       summary->zone, summary->ns);
		freesummary(summary);
		return;
	}

749 750 751
	/*
	 * Do deferred xlink failure reports.
	 */
752 753
	if (summary->type == ns_t_a &&
	    summary->nodataa && summary->failedaaaa) {
754 755 756 757 758
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA lookup failed\n");
		freesummary(summary);
		return;
	}
759 760
	if (summary->type == ns_t_aaaa &&
	    summary->nodataaaaa && summary->faileda) {
761 762 763 764 765
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A lookup failed\n");
		freesummary(summary);
		return;
	}
766 767
	if (summary->type == ns_t_a &&
	    summary->faileda && summary->nxdomainaaaa) {
768 769 770 771 772
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA nxdomain\n");
		freesummary(summary);
		return;
	}
773 774
	if (summary->type == ns_t_aaaa &&
	    summary->failedaaaa && summary->nxdomaina) {
775 776 777 778 779
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A nxdomain\n");
		freesummary(summary);
		return;
	}
780

Mark Andrews's avatar
Mark Andrews committed
781
	if (summary->done || summary->nodataa || summary->nodataaaaa) {
Mark Andrews's avatar
Mark Andrews committed
782
		freesummary(summary);
783 784
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
785

786 787 788
	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
789
			freesummary(summary);
790 791 792 793 794 795
			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
796
		freesummary(summary);
797 798 799 800 801
		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
802
		freesummary(summary);
803 804 805 806 807
		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
808
		freesummary(summary);
809 810 811
		return;
	}
	if (summary->type == ns_t_ns) {
Mark Andrews's avatar
Mark Andrews committed
812
		printf("%s.:", summary->zone);
813
		printf(" NS lookup failed\n");
Mark Andrews's avatar
Mark Andrews committed
814
		freesummary(summary);
815 816
		return;
	}
817

818 819 820 821 822
	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
823
		freesummary(summary);
Mark Andrews's avatar
Mark Andrews committed
824 825 826
		return;
	}

827 828 829 830 831
	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
832

833
	if (addr == NULL)
834
		strncpy(addrbuf, "<unknown>", sizeof(addrbuf));
835 836 837 838
	else
		inet_ntop(summary->storage.ss_family, addr,
			  addrbuf, sizeof(addrbuf));

839 840
	x = -1;
	printf("%s. @%s (%s.):", summary->zone, addrbuf, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
841 842 843 844 845
	if (allok && summary->allok)
		printf(" all ok");
	else
		for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
			if (opts[i].what != 0 && (opts[i].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
846
				continue;
Mark Andrews's avatar
Mark Andrews committed
847
			if (summary->results[i][0] == 0)
848
				strncpy(summary->results[i], "skipped", 100);
Mark Andrews's avatar
Mark Andrews committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
			if (strcmp(opts[i].name, "do") == 0)
				x = i;
			if (strcmp(opts[i].name, "ednstcp") == 0 && x != -1 &&
			    (!badtag || (strcmp(summary->results[x], "ok") != 0 &&
					 strncmp(summary->results[x], "ok,", 3) != 0)))
			{
				printf(" signed=%s", summary->results[x]);
				if (summary->seenrrsig)
					printf(",yes");
			}
			if (badtag) {
				if (strcmp(summary->results[i], "ok") == 0 ||
				    strncmp(summary->results[i], "ok,", 3) == 0)
					continue;
			}
			printf(" %s=%s", opts[i].name, summary->results[i]);
Mark Andrews's avatar
Mark Andrews committed
865
		}
866 867 868 869 870 871 872 873 874 875
	if (printnsid && summary->nsidlen != 0) {
		printf(" (");
		for (i = 0; i < summary->nsidlen; i++) {
			if (isprint(summary->nsid[i] & 0xff))
				putchar(summary->nsid[i]);
			else
				putchar('.');
		}
		printf(")");
	}
876
	printf("\n");
Mark Andrews's avatar
Mark Andrews committed
877 878 879 880 881 882
	freesummary(summary);
}

static void
report(struct summary *summary) {

Mark Andrews's avatar
Mark Andrews committed
883 884 885 886
	/*
	 * Send the next test now that we have completed the last test.
	 */
	if (serial && summary->type == 0 && summary->tests == 1) {
887 888 889
		for (summary->last++;
		     summary->last < sizeof(opts)/sizeof(opts[0]);
		     summary->last++) {
Mark Andrews's avatar
Mark Andrews committed
890
			struct workitem *item;
891 892
			if (opts[summary->last].what != 0 &&
			    (opts[summary->last].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
893 894 895 896 897
				continue;
			item = calloc(1, sizeof(*item));
			if (item == NULL)
				continue;
			item->summary = summary;
898
			item->test = item->summary->last;
899
			item->tcpfd = -1;
Mark Andrews's avatar
Mark Andrews committed
900 901 902 903 904
			dotest(item);
			return;
		}
	}

Mark Andrews's avatar
Mark Andrews committed
905 906 907 908 909 910 911 912 913 914 915 916
	/*
	 * 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) {
917 918 919 920 921 922 923 924
		if (summary->cnamea) {
			summary->xlink->cnamea = 1;
			summary->done = 1;
		}
		if (summary->cnameaaaa) {
			summary->xlink->cnameaaaa = 1;
			summary->done = 1;
		}
Mark Andrews's avatar
Mark Andrews committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
		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;
		}
941 942 943 944 945 946 947 948 949 950
		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
951 952 953 954 955 956 957 958 959 960 961

		/*
		 * Remove the cross link.
		 */
		summary->xlink->xlink = NULL;
		summary->xlink = NULL;
		if (summary->done) {
			freesummary(summary);
			goto print_deferred;
		}
	}
962 963 964 965 966 967
	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
968 969 970 971 972 973 974 975 976 977

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

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

Mark Andrews's avatar
Mark Andrews committed
980 981 982
/*
 * Free a work item and unlink.
 */
983 984
static void
freeitem(struct workitem * item) {
Mark Andrews's avatar
Mark Andrews committed
985 986
	if (item->summary)
		report(item->summary);
987
	if (item->tcpfd != -1) {
988 989 990 991 992 993 994 995 996 997
		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);
Mark Andrews's avatar
Mark Andrews committed
998 999
	if (LINKED(item, plink))
		UNLINK(pending, item, plink);
1000 1001 1002 1003 1004 1005
	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);
1006 1007
	if (LINKED(item, seqlink))
		UNLINK(seq[item->id], item, seqlink);
1008 1009 1010
	free(item);
}

Mark Andrews's avatar
Mark Andrews committed
1011 1012 1013
/*
 * Add a tag to the report.
 */
1014
static void
1015
addtag(struct workitem *item, const char *tag) {
1016
	char *result = item->summary->results[item->test];
1017 1018
	if (result[0]) strncat(result, ",", 100);
	strncat(result, tag, 100);
1019 1020
}

Mark Andrews's avatar
Mark Andrews committed
1021 1022 1023
/*
 * Resend a UDP request.
 */
1024 1025 1026
static void
resend(struct workitem *item) {
	int n, fd = -1;
1027
	socklen_t ss_len;
1028 1029 1030 1031

	switch (item->summary->storage.ss_family) {
	case AF_INET:
		fd = udp4;
1032
		ss_len = sizeof(struct sockaddr_in);
1033 1034 1035
		break;
	case AF_INET6:
		fd = udp6;
1036
		ss_len = sizeof(struct sockaddr_in6);
1037 1038 1039 1040 1041
		break;
	}

	if (fd == -1) {
		addtag(item, "skipped");
1042
		item->summary->allok = 0;
1043 1044 1045 1046
		freeitem(item);
		return;
	}

1047
	if (!item->outstanding && outstanding > maxoutstanding) {
1048 1049
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
1050 1051 1052
		if (LINKED(item, link))
			UNLINK(work, item, link);
		APPEND(pending, item, plink);
1053 1054 1055 1056
		return;
	}

	n = sendto(fd, item->buf, item->buflen, 0,
1057
		   (struct sockaddr *)&item->summary->storage, ss_len);
1058 1059 1060 1061 1062 1063 1064 1065
	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);
1066
		sent++;
1067 1068 1069 1070 1071
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
Mark Andrews's avatar
Mark Andrews committed
1072 1073
		if (LINKED(item, link))
			UNLINK(work, item, link);
1074
		APPEND(work, item, link);
1075 1076
	} else if (item->summary->type) {
		nextserver(item);
1077 1078
	} else {
		addtag(item, "failed");
1079 1080
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
1081 1082 1083 1084
		freeitem(item);
	}
}

Mark Andrews's avatar
Mark Andrews committed
1085 1086 1087
/*
 * Start a individual test.
 */
Mark Andrews's avatar
Mark Andrews committed
1088 1089 1090 1091
static void
dotest(struct workitem *item) {
	unsigned char *cp;
	unsigned int ttl;
Mark Andrews's avatar
Mark Andrews committed
1092
	int n, fd, id, tries = 0, opcode;
1093 1094
	socklen_t ss_len;
	
Mark Andrews's avatar
Mark Andrews committed
1095

1096
	switch (item->summary->storage.ss_family) {
Mark Andrews's avatar
Mark Andrews committed
1097 1098
	case AF_INET:
		fd = udp4;
1099
		ss_len = sizeof(struct sockaddr_in);
Mark Andrews's avatar
Mark Andrews committed
1100 1101 1102
		break;
	case AF_INET6:
		fd = udp6;
1103
		ss_len = sizeof(struct sockaddr_in6);
Mark Andrews's avatar
Mark Andrews committed
1104 1105 1106 1107
		break;
	}

	if (fd == -1) {
1108
		addtag(item, "skipped");
1109
		item->summary->allok = 0;
1110
		freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
1111 1112
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
1113

Mark Andrews's avatar
Mark Andrews committed
1114 1115 1116 1117
	/*
	 * 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
1118 1119 1120 1121 1122 1123 1124 1125
	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,
1126 1127
			opts[item->test].type, NULL, 0, NULL,
			item->buf, sizeof(item->buf));
Mark Andrews's avatar
Mark Andrews committed
1128 1129 1130 1131 1132 1133 1134
	/* 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;
1135
		n = 12;
Mark Andrews's avatar
Mark Andrews committed
1136
	}
Mark Andrews's avatar
Mark Andrews committed
1137 1138 1139 1140

	/*
	 * Set DNS flags as specified by test.
	 */
1141 1142
	if (opts[item->test].tc)
		item->buf[2] |= 0x2;	/* set tc */
Mark Andrews's avatar
Mark Andrews committed
1143 1144 1145 1146
	if (opts[item->test].rd)
		item->buf[2] |= 0x1;	/* set rd */
	else
		item->buf[2] &= ~0x1;	/* clear rd */
1147 1148 1149 1150 1151 1152 1153 1154
	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
1155

1156
	if (n > 12) {
1157
		char name[1024];
Mark Andrews's avatar
Mark Andrews committed
1158 1159 1160
		/*
		 * Make zone canonical.
		 */
1161 1162
		dn_expand(item->buf, item->buf + n, item->buf + 12,
			  name, sizeof(name));
1163
		strncpy(item->summary->zone, name,
1164
			sizeof(item->summary->zone));
1165
	}
Mark Andrews's avatar
Mark Andrews committed
1166

Mark Andrews's avatar
Mark Andrews committed
1167 1168 1169 1170 1171
	/*
	 * 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
1172
		cp = item->buf + n;
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
		*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
1184 1185
		memcpy(cp, opts[item->test].rdata, opts[item->test].rdlen);
		cp += opts[item->test].rdlen;
1186 1187
		item->buf[11] = 1;			/* adcount */
		n = cp - item->buf;			/* total length */
Mark Andrews's avatar
Mark Andrews committed
1188 1189 1190
	}

	if (n > 0) {
Mark Andrews's avatar
Mark Andrews committed
1191 1192 1193
		/*
		 * Adjust id if it clashes with a outstanding request.
		 */
Mark Andrews's avatar
Mark Andrews committed
1194 1195
		id = item->buf[0] << 8 | item->buf[1];

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

Mark Andrews's avatar
Mark Andrews committed
1200
		if (tries == 0xffff) {
1201
			addtag(item, "skipped");
1202
			item->summary->allok = 0;
1203
			item->summary->seenfailure = 1;
1204
			freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
1205 1206
			return;
		}
Mark Andrews's avatar
Mark Andrews committed
1207

Mark Andrews's avatar
Mark Andrews committed
1208 1209 1210 1211 1212
		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		item->buflen = n;

1213 1214 1215 1216 1217
		if (opts[item->test].tcp) {
			connecttoserver(item);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
1218 1219 1220
		/*
		 * If there is too much outstanding work queue this item.
		 */
1221
		if (!item->outstanding && outstanding > maxoutstanding) {
1222 1223
			gettimeofday(&item->when, NULL);
			item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
1224
			APPEND(pending, item, plink);
1225 1226 1227 1228
			APPEND(ids[item->id], item, idlink);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
1229
		n = sendto(fd, item->buf, item->buflen, 0,
1230
			   (struct sockaddr *)&item->summary->storage, ss_len);
Mark Andrews's avatar
Mark Andrews committed
1231 1232 1233
	}

	if (n > 0) {
1234 1235 1236 1237 1238 1239 1240 1241 1242
		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++;
1243
		sent++;
Mark Andrews's avatar
Mark Andrews committed
1244
		gettimeofday(&item->when, NULL);
1245
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
1246 1247 1248
		item->sends = 1;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
1249 1250
	} else {
		addtag(item, "failed");
1251 1252
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
1253 1254
		freeitem(item);
	}
Mark Andrews's avatar
Mark Andrews committed
1255 1256
}

Mark Andrews's avatar
Mark Andrews committed
1257 1258 1259
/*
 * Start a series of tests.
 */
Mark Andrews's avatar
Mark Andrews committed
1260
static void
Mark Andrews's avatar
Mark Andrews committed
1261
check(char *zone, char *ns, char *address, struct summary *parent) {
Mark Andrews's avatar
Mark Andrews committed
1262
	size_t i;
Mark Andrews's avatar
Mark Andrews committed
1263
	int fd;
Mark Andrews's avatar
Mark Andrews committed
1264 1265 1266
	struct in_addr addr;
	struct in6_addr addr6;
	struct sockaddr_storage storage;