genreport.c 78.4 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

Mark Andrews's avatar
Mark Andrews committed
11
#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
12 13
#include <string.h>
#include <stdlib.h>
14
#include <stddef.h>
Mark Andrews's avatar
Mark Andrews committed
15
#include <unistd.h>
Mark Andrews's avatar
Mark Andrews committed
16
#include <assert.h>
Mark Andrews's avatar
Mark Andrews committed
17

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

#include <errno.h>
35
#include <netdb.h>
Mark Andrews's avatar
Mark Andrews committed
36
#include <resolv.h>
37
#include <signal.h>
Mark Andrews's avatar
Mark Andrews committed
38

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

74 75
#define ns_r_badcookie 23

76
static int eof = 0;
Mark Andrews's avatar
Mark Andrews committed
77 78
static int maxfd = -1;
static fd_set rfds, wfds;
Mark Andrews's avatar
Mark Andrews committed
79
static int outstanding = 0;
80
static int maxoutstanding = 100;
Mark Andrews's avatar
Mark Andrews committed
81 82 83 84 85 86

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

static int udp4 = -1;
static int udp6 = -1;
87 88
static int icmp4 = -1;
static int icmp6 = -1;
89

Mark Andrews's avatar
Mark Andrews committed
90 91 92
static int ipv4only = 0;
static int ipv6only = 0;

Mark Andrews's avatar
Mark Andrews committed
93
static int allok  = 0;
94
static int bad  = 0;
Mark Andrews's avatar
fixups  
Mark Andrews committed
95
static int badtag  = 0;
96
static int ednsonly  = 0;
97
static int debug = 0;
Mark Andrews's avatar
Mark Andrews committed
98
static int inorder = 0;
99 100
static int serial = 0;
static long long sent;
101 102 103

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

Mark Andrews's avatar
Mark Andrews committed
106 107 108
/*
 * Doubly linked list macros.
 */
Mark Andrews's avatar
Mark Andrews committed
109 110 111 112 113 114 115 116
#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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	(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
140
		(item)->link.linked = 1; \
Mark Andrews's avatar
Mark Andrews committed
141
	} \
Mark Andrews's avatar
Mark Andrews committed
142 143 144 145 146 147 148 149 150 151 152 153
} 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; \
154
	(item)->link.linked = 0; \
Mark Andrews's avatar
Mark Andrews committed
155 156 157
} while (0)

#define NEXT(item, link) (item)->link.next
Mark Andrews's avatar
Mark Andrews committed
158
#define PREV(item, link) (item)->link.prev
159
#define LINKED(item, link) (item)->link.linked
Mark Andrews's avatar
Mark Andrews committed
160 161 162 163

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

Mark Andrews's avatar
Mark Andrews committed
164 165 166
/*
 * Test groupings
 */
Mark Andrews's avatar
Mark Andrews committed
167 168 169 170 171 172 173
#define EDNS 0x01
#define COMM 0x02
#define FULL 0x04
#define TYPE 0x08
#define EXPL 0x10

static int what = EDNS;
174

Mark Andrews's avatar
Mark Andrews committed
175
static struct {
Mark Andrews's avatar
Mark Andrews committed
176 177 178 179 180 181 182 183
	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 */
184
	unsigned int cookie;		/* opt record has cookie */
Mark Andrews's avatar
Mark Andrews committed
185 186 187 188 189 190 191 192 193 194
	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 */
195
} opts[] = {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "dns",       EDNS,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "aa",        FULL,  0, "",    0, 0x0000, 0, 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, 0, 1, 0, 0,  0, ns_t_soa },
	{ "cd",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,  0, ns_t_soa },
	{ "ra",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,  0, ns_t_soa },
	{ "rd",        FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "tc",        FULL,  0, "",    0, 0x0000, 0, 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, 0, 1,  0, ns_t_soa },
	{ "opcode",    FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0 },
	{ "opcodeflg", FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 15, 0 },
	{ "type666",   FULL,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 666 },
	{ "tcp",       FULL,  0, "",    0, 0x0000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "edns",      EDNS,  0, "", 4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1",     EDNS,  0, "", 4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns@512",  EDNS,  0, "",  512, 0x0000, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },
Mark Andrews's avatar
Mark Andrews committed
214
	{ "ednsopt",   EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
215
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
216
	{ "edns1opt",  EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
217 218 219 220 221
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "do",        EDNS,  0, "",
				     4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1do",   FULL,  0, "", 4096, 0x8000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "ednsflags", EDNS,  0, "", 4096, 0x0080, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
222
	{ "optlist",   EDNS,  4 + 8 + 4 + 12,
223 224 225 226
	  "\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 */
227 228 229
				     4096, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
230
	{ "ednsnsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
231
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
232
	{ "ednscookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
233
				     4096, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
234
	{ "ednsexpire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
235
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
236
	{ "ednssubnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
237
				     4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
238
	{ "edns1nsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
239
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
240
	{ "edns1cookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
241
				     4096, 0x0000, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
242
	{ "edns1expire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
243
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
244
	{ "edns1subnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
245 246 247 248
				     4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "ednstcp",   EDNS,  0, "",  512, 0x8000, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
249
	{ "bind11",    COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
250
				     4096, 0x8000, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
Mark Andrews's avatar
Mark Andrews committed
251
	{ "dig11",     COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
				     4096, 0x0000, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,  0, ns_t_soa },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "A",         TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_a },
	{ "NS",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ns },
	{ "MD",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_md },
	{ "MF",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mf },
	{ "CNAME",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cname },
	{ "SOA",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "MB",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mb },
	{ "MG",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mg },
	{ "MR",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mr },
	{ "NULL",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_null },
	{ "WKS",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_wks },
	{ "PTR",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ptr },
	{ "HINFO",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_hinfo },
	{ "MINFO",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_minfo },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "MX",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_mx },
	{ "TXT",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_txt },
	{ "RP",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rp },
	{ "AFSDB",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_afsdb },
	{ "X25",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_x25 },
	{ "ISDN",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_isdn },
	{ "RT",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rt },
	{ "NSAP",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsap },
	{ "NSAP-PTR",  TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsap_ptr },
	{ "SIG",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_sig },
	{ "KEY",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_key },
	{ "PX",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_px },
	{ "GPOS",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_gpos },
	{ "AAAA",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_aaaa },
	{ "LOC",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_loc },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "NXT",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nxt },
	{ "SRV",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_srv },
	{ "NAPTR",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_naptr },
	{ "KX",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_kx },
	{ "CERT",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cert },
	{ "A6",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_a6 },
	{ "DNAME",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dname },
	{ "APL",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_apl },
	{ "DS",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ds },
	{ "SSHFP",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_sshfp },
	{ "IPSECKEY",  TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_ipseckey },
	{ "RRSIG",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_rrsig },
	{ "NSEC",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "DNSKEY",    TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },
	{ "DHCID",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dhcid },
	{ "NSEC3",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec3 },
	{ "NSEC3PARAM",TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nsec3param },
	{ "TLSA",      TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_tlsa },
	{ "SMIMEA",    TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_smimea },
	{ "HIP",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_hip },
	{ "CDS",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cds },
	{ "CDNSKEY",   TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_cdnskey },
	{ "OPENPGPKEY",TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_openpgpkey },
	{ "SPF",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_spf },
	{ "NID",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_nid },
	{ "L32",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_l32 },

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
	{ "L64",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_l34 },
	{ "LP",        TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_lp },
	{ "EUI48",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_eui48 },
	{ "EUI64",     TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_eui64 },
	{ "URI",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_uri },
	{ "CAA",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_caa },
324 325
	{ "AVC",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_avc },
	{ "DOA",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_doa },
326 327
	{ "DLV",       TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dlv },
	{ "TYPE1000",  TYPE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 1000 }
328 329
};

Mark Andrews's avatar
Mark Andrews committed
330 331 332
/*
 * Summary structure where results from multiple lookups are recorded.
 */
333
struct summary {
Mark Andrews's avatar
Mark Andrews committed
334 335 336 337 338
	struct {
		struct summary *prev;
		struct summary *next;
		int linked;
	} link;
Mark Andrews's avatar
Mark Andrews committed
339 340 341
	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
342
	int tests;			/* number of outstanding tests */
Mark Andrews's avatar
Mark Andrews committed
343
	int last;			/* last test sent */
Mark Andrews's avatar
Mark Andrews committed
344
	int deferred;			/* was the printing deferred */
Mark Andrews's avatar
Mark Andrews committed
345
	int done;			/* we are done */
Mark Andrews's avatar
Mark Andrews committed
346 347 348 349 350 351
	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 */
352 353
	int faileda;
	int failedaaaa;
354 355
	int cnamea;			/* Nameserver is CNAME */
	int cnameaaaa;			/* Nameserver is CNAME */
Mark Andrews's avatar
Mark Andrews committed
356
	int seenrrsig;			/* a rrsig was seen in "do" test */
357 358 359 360 361 362
	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
363
	struct summary *xlink;		/* cross link of recursive A/AAAA */
364 365 366
	char results[sizeof(opts)/sizeof(opts[0])][100];
};

Mark Andrews's avatar
Mark Andrews committed
367 368 369 370 371
static struct {
	struct summary *head;
	struct summary *tail;
} summaries;

Mark Andrews's avatar
Mark Andrews committed
372 373 374 375
struct workitem {
	struct {
		struct workitem *next;
		struct workitem *prev;
376
		int linked;
377
	} link, clink, plink, rlink, idlink, seqlink;
Mark Andrews's avatar
Mark Andrews committed
378 379 380
	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
381 382 383
	int test;			/* test number / server number */
	int sends;			/* number of times this UDP request
					 * has been sent */
Mark Andrews's avatar
Mark Andrews committed
384 385
	int buflen;			/* the size of the request to be sent */
	int tcpfd;			/* the tcp file descriptor */
Mark Andrews's avatar
Mark Andrews committed
386 387
	int outstanding;		/* outstanding has been set */
	int havelen;			/* readlen is tcp message length */
Mark Andrews's avatar
Mark Andrews committed
388 389 390 391 392
	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
393 394
};

395
/*
Mark Andrews's avatar
Mark Andrews committed
396
 * Work queues:
397 398 399
 *	'work' udp qeries;
 *	'connecting' tcp qeries;
 *	'reading' tcp qeries;
Mark Andrews's avatar
Mark Andrews committed
400
 *	'pending' deferred work items;
Mark Andrews's avatar
Mark Andrews committed
401 402 403
 *
 * Outstanding queries by qid.
 *	'ids'
404 405 406
 *
 * Outstanding icmp by seq.
 *	'ids'
407
 */
Mark Andrews's avatar
Mark Andrews committed
408 409 410
static struct {
	struct workitem *head;
	struct workitem *tail;
411
} work, connecting, reading, pending, ids[0x10000], seq[0x10000];
Mark Andrews's avatar
Mark Andrews committed
412

Mark Andrews's avatar
Mark Andrews committed
413 414 415
static void
dotest(struct workitem *item);

416 417 418
static void
nextserver(struct workitem *item);

419 420
static void
connecttoserver(struct workitem *item);
Mark Andrews's avatar
Mark Andrews committed
421

Mark Andrews's avatar
Mark Andrews committed
422 423 424
static void
report(struct summary *summary);

Mark Andrews's avatar
Mark Andrews committed
425 426 427 428 429 430 431 432 433 434 435 436
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
437

Mark Andrews's avatar
Mark Andrews committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		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
454 455 456
/*
 * Check if it is safe to use this id to this address.
 */
Mark Andrews's avatar
Mark Andrews committed
457 458 459 460 461 462
static int
checkid(struct sockaddr_storage *storage, int id) {
	struct workitem *item;

	item = HEAD(ids[id]);
	while (item != NULL &&
463
	       !storage_equal(storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
464 465 466 467
		item = NEXT(item, idlink);
	return ((item == NULL) ? 1 : 0);
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/*
 * 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
483 484 485 486 487 488 489
static void
freesummary(struct summary *summary) {
	if (LINKED(summary, link))
		UNLINK(summaries, summary, link);
	free(summary);
}

Mark Andrews's avatar
Mark Andrews committed
490 491 492
/*
 * Generate a report line.
 */
493
static void
Mark Andrews's avatar
Mark Andrews committed
494
printandfree(struct summary *summary) {
495 496 497 498 499 500 501 502
	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
503
	    summary->nodataa && summary->nodataaaaa) {
504 505
		printf("%s. %s: no address records found\n",
		       summary->zone, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
506
		freesummary(summary);
507 508
		return;
	}
509

510 511 512 513
	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
514
		freesummary(summary);
515 516
		return;
	}
517

518 519 520 521 522 523 524 525
	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;
	}

526 527 528 529 530 531 532 533
	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;
	}

534 535 536
	/*
	 * Do deferred xlink failure reports.
	 */
537 538
	if (summary->type == ns_t_a &&
	    summary->nodataa && summary->failedaaaa) {
539 540 541 542 543
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA lookup failed\n");
		freesummary(summary);
		return;
	}
544 545
	if (summary->type == ns_t_aaaa &&
	    summary->nodataaaaa && summary->faileda) {
546 547 548 549 550
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A lookup failed\n");
		freesummary(summary);
		return;
	}
551 552
	if (summary->type == ns_t_a &&
	    summary->faileda && summary->nxdomainaaaa) {
553 554 555 556 557
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAAA nxdomain\n");
		freesummary(summary);
		return;
	}
558 559
	if (summary->type == ns_t_aaaa &&
	    summary->failedaaaa && summary->nxdomaina) {
560 561 562 563 564
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A nxdomain\n");
		freesummary(summary);
		return;
	}
565

Mark Andrews's avatar
Mark Andrews committed
566
	if (summary->done || summary->nodataa || summary->nodataaaaa) {
Mark Andrews's avatar
Mark Andrews committed
567
		freesummary(summary);
568 569
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
570

571 572 573
	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
574
			freesummary(summary);
575 576 577 578 579 580
			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
581
		freesummary(summary);
582 583 584 585 586
		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
587
		freesummary(summary);
588 589 590 591 592
		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
593
		freesummary(summary);
594 595 596
		return;
	}
	if (summary->type == ns_t_ns) {
Mark Andrews's avatar
Mark Andrews committed
597
		printf("%s.:", summary->zone);
598
		printf(" NS lookup failed\n");
Mark Andrews's avatar
Mark Andrews committed
599
		freesummary(summary);
600 601
		return;
	}
602

603 604 605 606 607
	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
608
		freesummary(summary);
Mark Andrews's avatar
Mark Andrews committed
609 610 611
		return;
	}

612 613 614 615 616
	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
617

618
	if (addr == NULL)
619
		strncpy(addrbuf, "<unknown>", sizeof(addrbuf));
620 621 622 623
	else
		inet_ntop(summary->storage.ss_family, addr,
			  addrbuf, sizeof(addrbuf));

624 625
	x = -1;
	printf("%s. @%s (%s.):", summary->zone, addrbuf, summary->ns);
Mark Andrews's avatar
Mark Andrews committed
626 627 628 629 630
	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
fixups  
Mark Andrews committed
631
				continue;
Mark Andrews's avatar
Mark Andrews committed
632
			if (summary->results[i][0] == 0)
633
				strncpy(summary->results[i], "skipped", 100);
Mark Andrews's avatar
Mark Andrews committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
			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
fixups  
Mark Andrews committed
650
		}
651
	printf("\n");
Mark Andrews's avatar
Mark Andrews committed
652 653 654 655 656 657
	freesummary(summary);
}

static void
report(struct summary *summary) {

Mark Andrews's avatar
Mark Andrews committed
658 659 660 661
	/*
	 * Send the next test now that we have completed the last test.
	 */
	if (serial && summary->type == 0 && summary->tests == 1) {
662 663 664
		for (summary->last++;
		     summary->last < sizeof(opts)/sizeof(opts[0]);
		     summary->last++) {
Mark Andrews's avatar
Mark Andrews committed
665
			struct workitem *item;
666 667
			if (opts[summary->last].what != 0 &&
			    (opts[summary->last].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
668 669 670 671 672
				continue;
			item = calloc(1, sizeof(*item));
			if (item == NULL)
				continue;
			item->summary = summary;
673
			item->test = item->summary->last;
674
			item->tcpfd = -1;
Mark Andrews's avatar
Mark Andrews committed
675 676 677 678 679
			dotest(item);
			return;
		}
	}

Mark Andrews's avatar
Mark Andrews committed
680 681 682 683 684 685 686 687 688 689 690 691
	/*
	 * 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) {
692 693 694 695 696 697 698 699
		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
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
		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;
		}
716 717 718 719 720 721 722 723 724 725
		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
726 727 728 729 730 731 732 733 734 735 736

		/*
		 * Remove the cross link.
		 */
		summary->xlink->xlink = NULL;
		summary->xlink = NULL;
		if (summary->done) {
			freesummary(summary);
			goto print_deferred;
		}
	}
737 738 739 740 741 742
	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
743 744 745 746 747 748 749 750 751 752

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

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

Mark Andrews's avatar
Mark Andrews committed
755 756 757
/*
 * Free a work item and unlink.
 */
758 759
static void
freeitem(struct workitem * item) {
Mark Andrews's avatar
Mark Andrews committed
760 761
	if (item->summary)
		report(item->summary);
762
	if (item->tcpfd != -1) {
763 764 765 766 767 768 769 770 771 772
		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
773 774
	if (LINKED(item, plink))
		UNLINK(pending, item, plink);
775 776 777 778 779 780
	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);
781 782
	if (LINKED(item, seqlink))
		UNLINK(seq[item->id], item, seqlink);
783 784 785
	free(item);
}

Mark Andrews's avatar
Mark Andrews committed
786 787 788
/*
 * Add a tag to the report.
 */
789
static void
790
addtag(struct workitem *item, const char *tag) {
791
	char *result = item->summary->results[item->test];
792 793
	if (result[0]) strncat(result, ",", 100);
	strncat(result, tag, 100);
794 795
}

Mark Andrews's avatar
Mark Andrews committed
796 797 798
/*
 * Resend a UDP request.
 */
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
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");
814
		item->summary->allok = 0;
815 816 817 818
		freeitem(item);
		return;
	}

819
	if (!item->outstanding && outstanding > maxoutstanding) {
820 821
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
822 823 824
		if (LINKED(item, link))
			UNLINK(work, item, link);
		APPEND(pending, item, plink);
825 826 827 828 829 830 831 832 833 834 835 836 837 838
		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);
839
		sent++;
840 841 842 843 844
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
Mark Andrews's avatar
Mark Andrews committed
845 846
		if (LINKED(item, link))
			UNLINK(work, item, link);
847
		APPEND(work, item, link);
848 849
	} else if (item->summary->type) {
		nextserver(item);
850 851
	} else {
		addtag(item, "failed");
852 853
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
854 855 856 857
		freeitem(item);
	}
}

Mark Andrews's avatar
Mark Andrews committed
858 859 860
/*
 * Start a individual test.
 */
Mark Andrews's avatar
Mark Andrews committed
861 862 863 864
static void
dotest(struct workitem *item) {
	unsigned char *cp;
	unsigned int ttl;
Mark Andrews's avatar
Mark Andrews committed
865
	int n, fd, id, tries = 0, opcode;
Mark Andrews's avatar
Mark Andrews committed
866

867
	switch (item->summary->storage.ss_family) {
Mark Andrews's avatar
Mark Andrews committed
868 869 870 871 872 873 874 875 876
	case AF_INET:
		fd = udp4;
		break;
	case AF_INET6:
		fd = udp6;
		break;
	}

	if (fd == -1) {
877
		addtag(item, "skipped");
878
		item->summary->allok = 0;
879
		freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
880 881
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
882

Mark Andrews's avatar
Mark Andrews committed
883 884 885 886
	/*
	 * 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
887 888 889 890 891 892 893 894
	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,
895 896
			opts[item->test].type, NULL, 0, NULL,
			item->buf, sizeof(item->buf));
Mark Andrews's avatar
Mark Andrews committed
897 898 899 900 901 902 903
	/* 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;
904
		n = 12;
Mark Andrews's avatar
Mark Andrews committed
905
	}
Mark Andrews's avatar
Mark Andrews committed
906 907 908 909

	/*
	 * Set DNS flags as specified by test.
	 */
910 911
	if (opts[item->test].tc)
		item->buf[2] |= 0x2;	/* set tc */
Mark Andrews's avatar
Mark Andrews committed
912 913 914 915
	if (opts[item->test].rd)
		item->buf[2] |= 0x1;	/* set rd */
	else
		item->buf[2] &= ~0x1;	/* clear rd */
916 917 918 919 920 921 922 923
	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
924

925
	if (n > 12) {
926
		char name[1024];
Mark Andrews's avatar
Mark Andrews committed
927 928 929
		/*
		 * Make zone canonical.
		 */
930 931
		dn_expand(item->buf, item->buf + n, item->buf + 12,
			  name, sizeof(name));
932
		strncpy(item->summary->zone, name,
933
			sizeof(item->summary->zone));
934
	}
Mark Andrews's avatar
Mark Andrews committed
935

Mark Andrews's avatar
Mark Andrews committed
936 937 938 939 940
	/*
	 * 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
941
		cp = item->buf + n;
942 943 944 945 946 947 948 949 950 951 952
		*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
953 954
		memcpy(cp, opts[item->test].rdata, opts[item->test].rdlen);
		cp += opts[item->test].rdlen;
955 956
		item->buf[11] = 1;			/* adcount */
		n = cp - item->buf;			/* total length */
Mark Andrews's avatar
Mark Andrews committed
957 958 959
	}

	if (n > 0) {
Mark Andrews's avatar
Mark Andrews committed
960 961 962
		/*
		 * Adjust id if it clashes with a outstanding request.
		 */
Mark Andrews's avatar
Mark Andrews committed
963 964
		id = item->buf[0] << 8 | item->buf[1];

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

Mark Andrews's avatar
Mark Andrews committed
969
		if (tries == 0xffff) {
970
			addtag(item, "skipped");
971
			item->summary->allok = 0;
972
			item->summary->seenfailure = 1;
973
			freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
974 975
			return;
		}
Mark Andrews's avatar
Mark Andrews committed
976

Mark Andrews's avatar
Mark Andrews committed
977 978 979 980 981
		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		item->buflen = n;

982 983 984 985 986
		if (opts[item->test].tcp) {
			connecttoserver(item);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
987 988 989
		/*
		 * If there is too much outstanding work queue this item.
		 */
990
		if (!item->outstanding && outstanding > maxoutstanding) {
991 992
			gettimeofday(&item->when, NULL);
			item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
993
			APPEND(pending, item, plink);
994 995 996 997
			APPEND(ids[item->id], item, idlink);
			return;
		}

Mark Andrews's avatar
Mark Andrews committed
998
		n = sendto(fd, item->buf, item->buflen, 0,
999 1000
			   (struct sockaddr *)&item->summary->storage,
			   item->summary->storage.ss_len);
Mark Andrews's avatar
Mark Andrews committed
1001 1002 1003
	}

	if (n > 0) {
1004 1005 1006 1007 1008 1009 1010 1011 1012
		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++;
1013
		sent++;
Mark Andrews's avatar
Mark Andrews committed
1014
		gettimeofday(&item->when, NULL);
1015
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
1016 1017 1018
		item->sends = 1;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
1019 1020
	} else {
		addtag(item, "failed");
1021 1022
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
1023 1024
		freeitem(item);
	}
Mark Andrews's avatar
Mark Andrews committed
1025 1026
}

Mark Andrews's avatar
Mark Andrews committed
1027 1028 1029
/*
 * Start a series of tests.
 */
Mark Andrews's avatar
Mark Andrews committed
1030
static void
Mark Andrews's avatar
Mark Andrews committed
1031
check(char *zone, char *ns, char *address, struct summary *parent) {
Mark Andrews's avatar
Mark Andrews committed
1032
	size_t i;
Mark Andrews's avatar
Mark Andrews committed
1033
	int fd;
Mark Andrews's avatar
Mark Andrews committed
1034 1035 1036
	struct in_addr addr;
	struct in6_addr addr6;
	struct sockaddr_storage storage;
1037
	struct summary *summary;
Mark Andrews's avatar
Mark Andrews committed
1038

Mark Andrews's avatar
Mark Andrews committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	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;

1060 1061 1062
	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return;
Mark Andrews's avatar
Mark Andrews committed
1063

Mark Andrews's avatar
Mark Andrews committed
1064