genreport.c 109 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 12 13
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
Mark Andrews's avatar
Mark Andrews committed
14 15 16
#ifndef _BSD_SOURCE
#define _BSD_SOURCE 1
#endif
17

Mark Andrews's avatar
Mark Andrews committed
18
#include <config.h>
Mark Andrews's avatar
Mark Andrews committed
19

Mark Andrews's avatar
Mark Andrews committed
20
#include <stdio.h>
Mark Andrews's avatar
Mark Andrews committed
21 22
#include <string.h>
#include <stdlib.h>
23
#include <stddef.h>
Mark Andrews's avatar
Mark Andrews committed
24
#include <unistd.h>
Mark Andrews's avatar
Mark Andrews committed
25
#include <assert.h>
26
#include <ctype.h>
Mark Andrews's avatar
Mark Andrews committed
27

Mark Andrews's avatar
Mark Andrews committed
28 29 30 31 32 33 34
#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>
35 36
#include <sys/ioctl.h>
#include <sys/uio.h>
37 38 39 40 41 42
#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
43 44

#include <errno.h>
45
#include <netdb.h>
Mark Andrews's avatar
Mark Andrews committed
46
#include <resolv.h>
47
#include <signal.h>
Mark Andrews's avatar
Mark Andrews committed
48

Mark Andrews's avatar
Mark Andrews committed
49 50
#include <openssl/hmac.h>

Mark Andrews's avatar
Mark Andrews committed
51 52 53 54 55
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
#define HMAC_CTX_new() &(_ctx), HMAC_CTX_init(&_ctx)
#define HMAC_CTX_free(ptr) HMAC_CTX_cleanup(ptr)
#endif

56 57 58
#ifndef HAVE_STRLCPY
#define strlcpy(dst, src, len) snprintf(dst, len, "%s", src)
#endif
Mark Andrews's avatar
Mark Andrews committed
59

60 61 62 63
#ifndef FD_COPY
#define FD_COPY(x, y) memmove(y, x, sizeof(*x))
#endif

Mark Andrews's avatar
Mark Andrews committed
64 65 66 67 68 69
#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
70
#define ns_t_rrsig 46
Mark Andrews's avatar
Mark Andrews committed
71
#define ns_t_nsec 47
Mark Andrews's avatar
Mark Andrews committed
72
#define ns_t_dnskey 48
Mark Andrews's avatar
Mark Andrews committed
73 74 75 76
#define ns_t_dhcid 49
#define ns_t_nsec3 50
#define ns_t_nsec3param 51
#define ns_t_tlsa 52
77
#define ns_t_smimea 53
Mark Andrews's avatar
Mark Andrews committed
78 79 80 81 82
#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
83
#define ns_t_openpgpkey 61
Mark Andrews's avatar
Mark Andrews committed
84 85 86 87 88 89 90 91 92 93
#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
94 95
#define ns_t_avc 258
#define ns_t_doa 259
Mark Andrews's avatar
Mark Andrews committed
96 97
#define ns_t_ta 32768
#define ns_t_dlv 32769
Mark Andrews's avatar
Mark Andrews committed
98

99 100
#define ns_r_badcookie 23

101
static int eof = 0;
Mark Andrews's avatar
Mark Andrews committed
102 103
static int maxfd = -1;
static fd_set rfds, wfds;
Mark Andrews's avatar
Mark Andrews committed
104
static int outstanding = 0;
105
static int maxoutstanding = 100;
Mark Andrews's avatar
Mark Andrews committed
106

107
static void(*rhandlers[FD_SETSIZE])(int, int);
Mark Andrews's avatar
Mark Andrews committed
108 109 110 111
static void(*whandlers[FD_SETSIZE])(int);

static int udp4 = -1;
static int udp6 = -1;
112 113
static int icmp4 = -1;
static int icmp6 = -1;
114

Mark Andrews's avatar
Mark Andrews committed
115 116 117
static int ipv4only = 0;
static int ipv6only = 0;

Mark Andrews's avatar
Mark Andrews committed
118
static int allok  = 0;
119
static int bad  = 0;
Mark Andrews's avatar
Mark Andrews committed
120
static int badtag  = 0;
121
static int ednsonly  = 0;
122
static int debug = 0;
Mark Andrews's avatar
Mark Andrews committed
123
static int inorder = 0;
124
static int serial = 0;
125
static int printnsid = 0;
126
static int recursive = 0;
127
static long long sent;
Mark Andrews's avatar
Mark Andrews committed
128
static int json = 0;
129
static int unique = 0;
Mark Andrews's avatar
Mark Andrews committed
130 131
static int useglue = 0;
static int glueonly = 0;
132

Mark Andrews's avatar
Mark Andrews committed
133 134
static char *jdata = NULL;
static size_t jdata_len = 0;
135 136

#ifdef HAVE_RES_GETSERVERS
137
static union res_sockaddr_union servers[10];
138 139
#else
static union {
Mark Andrews's avatar
Mark Andrews committed
140 141
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
142 143 144
} servers[10];
#endif

145
static int nservers = 0;
146
int ident = 0;
Mark Andrews's avatar
Mark Andrews committed
147

Mark Andrews's avatar
Mark Andrews committed
148 149 150
/*
 * Doubly linked list macros.
 */
Mark Andrews's avatar
Mark Andrews committed
151 152 153 154 155 156 157 158
#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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	(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
182
		(item)->link.linked = 1; \
Mark Andrews's avatar
Mark Andrews committed
183
	} \
Mark Andrews's avatar
Mark Andrews committed
184 185 186 187 188 189 190 191 192 193 194 195
} 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; \
196
	(item)->link.linked = 0; \
Mark Andrews's avatar
Mark Andrews committed
197 198 199
} while (0)

#define NEXT(item, link) (item)->link.next
Mark Andrews's avatar
Mark Andrews committed
200
#define PREV(item, link) (item)->link.prev
201
#define LINKED(item, link) (item)->link.linked
Mark Andrews's avatar
Mark Andrews committed
202 203 204 205

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

Mark Andrews's avatar
Mark Andrews committed
206 207 208
#define	HMACSHA256W "\013hmac-sha256"	/* cannonical form */
#define	HMACSHA256  "hmac-sha256"	/* presentation form */

Mark Andrews's avatar
Mark Andrews committed
209 210 211
/*
 * Test groupings
 */
Mark Andrews's avatar
Mark Andrews committed
212
#define NONE 0x00
Mark Andrews's avatar
Mark Andrews committed
213 214 215 216 217 218 219
#define EDNS 0x01
#define COMM 0x02
#define FULL 0x04
#define TYPE 0x08
#define EXPL 0x10

static int what = EDNS;
220

Mark Andrews's avatar
Mark Andrews committed
221
static struct {
Mark Andrews's avatar
Mark Andrews committed
222 223 224 225 226 227 228 229
	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 */
230
	unsigned int cookie;		/* opt record has cookie */
Mark Andrews's avatar
Mark Andrews committed
231 232 233 234 235 236 237 238 239 240
	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 */
241
	const char *dig;		/* dig command */
242
} opts[] = {
243
	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
244 245 246 247 248 249 250
	{ "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
251
	  "dig +noedns +ad +norec SOA <zone>"
252 253 254 255 256
	},
	{ "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,
257
	  "### dig +noedns +noad +norec +raflag SOA <zone> ###"
258 259 260 261 262
	},
	{ "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,
263
	  "### dig +noedns +noad +norec +tcflag SOA <zone> ###"
264 265 266 267 268 269 270 271
	},
	{ "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,
272
	  "### dig +noedns +header-only +opcode=15 +tcflag +rec +raflag +cd +ad +aaflag +zflag ###"
273 274 275 276 277 278 279
	},
	{ "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>"
	},
280 281

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
282 283 284 285 286 287 288 289 290
	{ "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
291
	{ "ednsopt",   EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
292 293 294
				     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
295
	{ "edns1opt",  EDNS,  4, "\x00\x64\x00\x00",	/* 100 */
296 297 298
				     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>"
	},
299
	{ "do",        EDNS,  0, "",
300 301 302
				     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>"
	},
Mark Andrews's avatar
Mark Andrews committed
303 304 305 306
	{ "docd",      FULL,  0, "",
				     4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,  0, ns_t_soa,
	  "dig +edns=0 +nocookie +noad +norec +dnssec SOA <zone>"
	},
307 308 309 310 311 312
	{ "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>"
	},
313
	{ "optlist",   EDNS,  4 + 8 + 4 + 12,
314 315 316 317
	  "\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 */
318 319 320
				     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>"
	},
321 322

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
323
	{ "ednsnsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
324 325 326
				     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
327
	{ "ednscookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
328 329 330
				     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
331
	{ "ednsexpire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
332 333 334
				     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
335
	{ "ednssubnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
336 337 338 339
				     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
340
	{ "edns1nsid", FULL,  4, "\x00\x03\x00\x00",	/* NSID */
341 342 343
				     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
344
	{ "edns1cookie", FULL, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
345 346 347
				     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
348
	{ "edns1expire", FULL, 4, "\x00\x09\x00\x00",	/* EXPIRE */
349 350 351
				     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
352
	{ "edns1subnet", FULL,  8, "\x00\x08\x00\x04\x00\x01\x00\x00",	/* ECS */
353 354 355 356 357 358
				     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>"
	},
359 360

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
Mark Andrews's avatar
Mark Andrews committed
361
	{ "bind11",    COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
362 363 364
				     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
365
	{ "dig11",     COMM, 12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08", /* COOKIE */
366 367 368
				     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>"
	},
369

Mark Andrews's avatar
Mark Andrews committed
370 371 372 373
	{ "dnswkk",     NONE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa,
	  "dig +noedns +noad +norec -y hmac-sha256:.:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= SOA <zone>"
	},

374 375 376 377
	{ "icmp",	NONE,  0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0,
	  "ping / ping6"
	},

378
	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	{ "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>"
	},
421 422

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	{ "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>"
	},
468 469

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

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	{ "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>"
	},
550 551

	/*                           size   eflgs vr  T ck ig tc rd ra cd ad aa  z  op  type */
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	{ "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>"
	}
582 583
};

Mark Andrews's avatar
Mark Andrews committed
584 585 586
/*
 * Summary structure where results from multiple lookups are recorded.
 */
587
struct summary {
Mark Andrews's avatar
Mark Andrews committed
588 589 590 591 592
	struct {
		struct summary *prev;
		struct summary *next;
		int linked;
	} link;
Mark Andrews's avatar
Mark Andrews committed
593 594
	char zone[1024];		/* the zone's name */
	char ns[1024];			/* the server's name */
595
	char target[1024];		/* the server's real name */
Mark Andrews's avatar
Mark Andrews committed
596
	struct sockaddr_storage storage;/* server we are talking to */
Mark Andrews's avatar
Mark Andrews committed
597
	int tests;			/* number of outstanding tests */
Mark Andrews's avatar
Mark Andrews committed
598
	unsigned int last;		/* last test sent */
Mark Andrews's avatar
Mark Andrews committed
599
	int deferred;			/* was the printing deferred */
Mark Andrews's avatar
Mark Andrews committed
600
	int done;			/* we are done */
Mark Andrews's avatar
Mark Andrews committed
601
	int type;			/* recursive query lookup type */
602
	int nodata;			/* recursive query got nodata */
Mark Andrews's avatar
Mark Andrews committed
603 604 605 606 607
	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 */
608 609
	int faileda;
	int failedaaaa;
Mark Andrews's avatar
Mark Andrews committed
610
	int cname;			/* NS is CNAME */
611 612
	int cnamea;			/* Nameserver is CNAME */
	int cnameaaaa;			/* Nameserver is CNAME */
Mark Andrews's avatar
Mark Andrews committed
613
	int seenrrsig;			/* a rrsig was seen in "do" test */
614 615 616 617 618 619
	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 */
620
	int targetok;			/* target is valid */
Mark Andrews's avatar
Mark Andrews committed
621
	struct summary *xlink;		/* cross link of recursive A/AAAA */
Mark Andrews's avatar
Mark Andrews committed
622
	unsigned int nsidlen;
623
	char nsid[100];			/* nsid if found */
624 625 626
	char results[sizeof(opts)/sizeof(opts[0])][100];
};

Mark Andrews's avatar
Mark Andrews committed
627 628 629 630 631
static struct {
	struct summary *head;
	struct summary *tail;
} summaries;

Mark Andrews's avatar
Mark Andrews committed
632 633 634 635
struct workitem {
	struct {
		struct workitem *next;
		struct workitem *prev;
636
		int linked;
637
	} link, clink, plink, rlink, idlink, seqlink;
Mark Andrews's avatar
Mark Andrews committed
638 639 640
	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
641 642 643
	int test;			/* test number / server number */
	int sends;			/* number of times this UDP request
					 * has been sent */
Mark Andrews's avatar
Mark Andrews committed
644 645
	int buflen;			/* the size of the request to be sent */
	int tcpfd;			/* the tcp file descriptor */
Mark Andrews's avatar
Mark Andrews committed
646 647
	int outstanding;		/* outstanding has been set */
	int havelen;			/* readlen is tcp message length */
Mark Andrews's avatar
Mark Andrews committed
648 649
	int readlen;			/* how much we need to read */
	int read;			/* how much has been read so far */
650
	int icmp;			/* this is a icmp echo request */
Mark Andrews's avatar
Mark Andrews committed
651 652
	unsigned char buf[512];		/* the question we sent */
	unsigned char tcpbuf[0x10000];	/* where to accumulate the tcp response */
Mark Andrews's avatar
Mark Andrews committed
653
	unsigned char mac[32];		/* tsig hmac-sha256 mac */
Mark Andrews's avatar
Mark Andrews committed
654
	struct summary *summary;	/* where this test is summaried */
Mark Andrews's avatar
Mark Andrews committed
655 656
};

657
/*
Mark Andrews's avatar
Mark Andrews committed
658
 * Work queues:
659 660 661
 *	'work' udp qeries;
 *	'connecting' tcp qeries;
 *	'reading' tcp qeries;
Mark Andrews's avatar
Mark Andrews committed
662
 *	'pending' deferred work items;
Mark Andrews's avatar
Mark Andrews committed
663 664 665
 *
 * Outstanding queries by qid.
 *	'ids'
666 667 668
 *
 * Outstanding icmp by seq.
 *	'ids'
669
 */
Mark Andrews's avatar
Mark Andrews committed
670 671 672
static struct {
	struct workitem *head;
	struct workitem *tail;
673
} work, connecting, reading, pending, ids[0x10000], seq[0x10000];
Mark Andrews's avatar
Mark Andrews committed
674

Mark Andrews's avatar
Mark Andrews committed
675 676 677
static void
dotest(struct workitem *item);

678 679 680
static void
nextserver(struct workitem *item);

681 682
static void
connecttoserver(struct workitem *item);
Mark Andrews's avatar
Mark Andrews committed
683

Mark Andrews's avatar
Mark Andrews committed
684 685 686
static void
report(struct summary *summary);

Mark Andrews's avatar
Mark Andrews committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
void
jsonadd(char **json, const char *str, size_t *len) {
	if (*json == NULL) {
		*json = calloc(1, 10240);
		if (*json == NULL)
			return;
		*len = 10240;
	}
	if (strlen(*json) + strlen(str) + 1 > *len) {
		char *tmp = realloc(*json, *len + 10240);
		if (tmp == NULL)
			return;
		*json = tmp;
		*len += 10240;
	}
702
#ifdef HAVE_STRLCAT
Mark Andrews's avatar
Mark Andrews committed
703
	strlcat(*json, str, *len);
704 705 706
#else
	strncat(*json, str, *len - strlen(*json) - 1);
#endif
Mark Andrews's avatar
Mark Andrews committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
}

void
jsonsafe(const char *str, char *safe, size_t len) {
	char c;

	while (len > 1) {
		switch ((c = *str++)) {
		case '\\':
			strlcpy(safe, "\\\\", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '"':
			strlcpy(safe, "\\\"", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\b':
			strlcpy(safe, "\\b", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\f':
			strlcpy(safe, "\\f", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\n':
			strlcpy(safe, "\\n", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\r':
			strlcpy(safe, "\\r", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\t':
			strlcpy(safe, "\\t", len);
			len -= strlen(safe);
			safe += strlen(safe);
			break;
		case '\0':
			*safe = c;
			len = 0;
			break;
		default:
			if (c > 0 && c < ' ') {
				snprintf(safe, len, "\\u%04x", (c & 0xff));
				len -= strlen(safe);
				safe += strlen(safe);
				break;
			} else {
				*safe++ = c;
				*safe = '\0';
				len--;
			}
			break;
		}
	}
	if (len > 0)
		*safe = '\0';
}

Mark Andrews's avatar
Mark Andrews committed
772 773 774 775 776
static int
storage_equal(struct sockaddr_storage *s1, struct sockaddr_storage *s2) {
	struct sockaddr_in *sin1, *sin2;
	struct sockaddr_in6 *sin61, *sin62;

777
	if (s1->ss_family != s2->ss_family)
Mark Andrews's avatar
Mark Andrews committed
778 779 780 781 782 783
		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
784

Mark Andrews's avatar
Mark Andrews committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
		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);
}

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
static int
sentto(struct sockaddr_storage *s) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	static struct listitem {
		struct listitem *next;
		struct sockaddr_storage s;
	} *table[100000] = { NULL }, *item = NULL;
	unsigned int hash = 0;
	
	switch (s->ss_family) {
	case AF_INET:
		sin = (struct sockaddr_in *)s;
		hash = sin->sin_addr.s_addr;
		break;
	case AF_INET6:
		sin6 = (struct sockaddr_in6 *)s;
		
#ifndef s6_addr32
#  if defined(__sun)
#    define s6_addr32   _S6_un._S6_u32
#  elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)|| defined(__DragonFly__)
#    define s6_addr32   __u6_addr.__u6_addr32
#  else
#    error s6_addr32 needs to be defined.
#  endif
#endif
		hash = sin6->sin6_addr.s6_addr32[0] ^ sin6->sin6_addr.s6_addr32[1] ^
		       sin6->sin6_addr.s6_addr32[2] ^ sin6->sin6_addr.s6_addr32[3];
		break;
	}

	hash %= 100000;
	for (item = table[hash]; item != NULL; item = item->next) {
		if (storage_equal(&item->s, s))
			return (1);
	}
	item = calloc(1, sizeof(*item));
	if (item == NULL)
		return (0);
	item->next = table[hash];
	item->s = *s;
	table[hash] = item;
	return (0);
}

Mark Andrews's avatar
Mark Andrews committed
847 848 849
/*
 * Check if it is safe to use this id to this address.
 */
Mark Andrews's avatar
Mark Andrews committed
850 851 852 853 854 855
static int
checkid(struct sockaddr_storage *storage, int id) {
	struct workitem *item;

	item = HEAD(ids[id]);
	while (item != NULL &&
856
	       !storage_equal(storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
857 858 859 860
		item = NEXT(item, idlink);
	return ((item == NULL) ? 1 : 0);
}

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
/*
 * 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
876 877 878 879 880 881 882
static void
freesummary(struct summary *summary) {
	if (LINKED(summary, link))
		UNLINK(summaries, summary, link);
	free(summary);
}

Mark Andrews's avatar
Mark Andrews committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
static void
emiterr(const char *zone, const char *ns, const char *str) {
	char safe[1024];

	if (json) {
		if (jdata)
			jsonadd(&jdata, ", ", &jdata_len);
		jsonsafe(zone[0] ? zone : ".", safe, sizeof(safe));
		jsonadd(&jdata, "{ \"zone\": \"", &jdata_len);
		jsonadd(&jdata, safe, &jdata_len);
		jsonadd(&jdata, "\"", &jdata_len);
	
		if (ns) {
			jsonsafe(ns, safe, sizeof(safe));
			jsonadd(&jdata, ", \"servername\": \"", &jdata_len);
			jsonadd(&jdata, safe, &jdata_len);
			jsonadd(&jdata, "\"", &jdata_len);
		}

902
		jsonsafe(str, safe, sizeof(safe));
Mark Andrews's avatar
Mark Andrews committed
903
		jsonadd(&jdata, ", \"error\": \"", &jdata_len);
904
		jsonadd(&jdata, safe, &jdata_len);
Mark Andrews's avatar
Mark Andrews committed
905 906 907 908 909 910 911
		jsonadd(&jdata, "\" }", &jdata_len);
	} else {
		printf("%s.%s%s: %s\n",
		       zone, ns ? " " : "", ns ? ns : "", str);
	}
}

Mark Andrews's avatar
Mark Andrews committed
912 913 914
/*
 * Generate a report line.
 */
915
static void
Mark Andrews's avatar
Mark Andrews committed
916
printandfree(struct summary *summary) {
917 918 919
	struct sockaddr_in *s = (struct sockaddr_in *)&summary->storage;
	struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&summary->storage;;
	char addrbuf[64];
Mark Andrews's avatar
Mark Andrews committed
920 921
	char buf[2048];
	char safe[1024];
922 923 924 925 926
	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
927
	    summary->nodataa && summary->nodataaaaa) {
Mark Andrews's avatar
Mark Andrews committed
928
		emiterr(summary->zone, summary->ns, "no address records found");
Mark Andrews's avatar
Mark Andrews committed
929
		freesummary(summary);
930 931
		return;
	}
932

933 934
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    summary->nxdomaina && summary->nxdomainaaaa) {
935 936
		emiterr(summary->zone, summary->ns,
			"no address records found (NXDOMAIN)");
Mark Andrews's avatar
Mark Andrews committed
937
		freesummary(summary);
938 939
		return;
	}
940

941 942
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    summary->faileda && summary->failedaaaa) {
Mark Andrews's avatar
Mark Andrews committed
943
		emiterr(summary->zone, summary->ns, "address lookups failed");
944 945 946 947
		freesummary(summary);
		return;
	}

948 949
	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    (summary->cnamea || summary->cnameaaaa)) {
950
		snprintf(buf, sizeof(buf), "nameserver is a CNAME%s%s%s",
951
			 summary->targetok ? " '": "",
Mark Andrews's avatar
Mark Andrews committed
952 953 954
			 summary->targetok ? summary->target : "",
			 summary->targetok ? "'" : "");
		emiterr(summary->zone, summary->ns, buf);
955 956 957 958
		freesummary(summary);
		return;
	}

Mark Andrews's avatar
Mark Andrews committed
959
	if ((summary->type == ns_t_ns) && summary->cname) {
Mark Andrews's avatar
Mark Andrews committed
960
		emiterr(summary->zone, summary->ns, "zone is a CNAME");
Mark Andrews's avatar
Mark Andrews committed
961 962 963 964
		freesummary(summary);
		return;
	}

965 966 967
	/*
	 * Do deferred xlink failure reports.
	 */
968 969
	if (summary->type == ns_t_a &&
	    summary->nodataa && summary->failedaaaa) {
Mark Andrews's avatar
Mark Andrews committed
970
		emiterr(summary->zone, summary->ns, "AAAA lookup failed");
971 972 973
		freesummary(summary);
		return;
	}
974 975
	if (summary->type == ns_t_aaaa &&
	    summary->nodataaaaa && summary->faileda) {
Mark Andrews's avatar
Mark Andrews committed
976
		emiterr(summary->zone, summary->ns, "A lookup failed");
977 978 979
		freesummary(summary);
		return;
	}
980 981
	if (summary->type == ns_t_a &&
	    summary->faileda && summary->nxdomainaaaa) {
Mark Andrews's avatar
Mark Andrews committed
982
		emiterr(summary->zone, summary->ns, "AAAA nxdomain");
983 984 985
		freesummary(summary);
		return;
	}
986 987
	if (summary->type == ns_t_aaaa &&
	    summary->failedaaaa && summary->nxdomaina) {
Mark Andrews's avatar
Mark Andrews committed
988
		emiterr(summary->zone, summary->ns, "A nxdomain");
989 990 991
		freesummary(summary);
		return;
	}
992
	if (summary->type == ns_t_ns && summary->nodata) {
Mark Andrews's avatar
Mark Andrews committed
993
		emiterr(summary->zone, NULL, "no NS records found");
994 995 996
		freesummary(summary);
		return;
	}
997

Mark Andrews's avatar
Mark Andrews committed
998
	if (summary->done || summary->nodataa || summary->nodataaaaa) {
Mark Andrews's avatar
Mark Andrews committed
999
		freesummary(summary);
1000 1001
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
1002

1003
	if (summary->type != 0 && summary->nxdomain) {
Mark Andrews's avatar
Mark Andrews committed
1004 1005 1006 1007 1008 1009
		if (summary->type == ns_t_a)
			emiterr(summary->zone, NULL, "NS nxdomain");
		if (summary->type == ns_t_a)
			emiterr(summary->zone, summary->ns, "A nxdomain");
		if (summary->type == ns_t_aaaa)
			emiterr(summary->zone, summary->ns, "AAAA nxdomain");
Mark Andrews's avatar
Mark Andrews committed
1010
		freesummary(summary);
1011 1012 1013
		return;
	}
	if (summary->type == ns_t_a) {
Mark Andrews's avatar
Mark Andrews committed
1014
		emiterr(summary->zone, summary->ns, "A lookup failed");
Mark Andrews's avatar
Mark Andrews committed
1015
		freesummary(summary);
1016 1017 1018
		return;
	}
	if (summary->type == ns_t_aaaa) {
Mark Andrews's avatar
Mark Andrews committed
1019
		emiterr(summary->zone, summary->ns, "AAAA lookup failed");
Mark Andrews's avatar
Mark Andrews committed
1020
		freesummary(summary);
1021 1022 1023
		return;
	}
	if (summary->type == ns_t_ns) {
Mark Andrews's avatar
Mark Andrews committed
1024
		emiterr(summary->zone, NULL, "NS lookup failed");
Mark Andrews's avatar
Mark Andrews committed
1025
		freesummary(summary);
1026 1027
		return;
	}
1028

1029 1030 1031 1032 1033
	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
1034
		freesummary(summary);
Mark Andrews's avatar
Mark Andrews committed
1035 1036 1037
		return;
	}

1038 1039 1040 1041 1042
	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
1043

1044
	if (addr == NULL)
1045
		strncpy(addrbuf, "<unknown>", sizeof(addrbuf));
1046 1047 1048 1049
	else
		inet_ntop(summary->storage.ss_family, addr,
			  addrbuf, sizeof(addrbuf));

1050
	x = -1;
Mark Andrews's avatar
Mark Andrews committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	if (json) {
		int first = 1;
		if (jdata)
			jsonadd(&jdata, ",\n", &jdata_len);
		jsonsafe(summary->zone[0] ? summary->zone : ".", safe, sizeof(safe));
		snprintf(buf, sizeof(buf), "{ \"zone\": \"%s\"", safe);
		jsonadd(&jdata, buf, &jdata_len);
		snprintf(buf, sizeof(buf), ", \"address\": \"%s\"", addrbuf);
		jsonadd(&jdata, buf, &jdata_len);
		if (strcmp(summary->ns, ".") != 0) {
			jsonsafe(summary->ns, safe, sizeof(safe));
				snprintf(buf, sizeof(buf), ", \"servername\": \"%s\"", safe);
				jsonadd(&jdata, buf, &jdata_len);
		}
		if (allok && summary->allok) {
			jsonadd(&jdata, ", \"summary\": \"all ok\" }", &jdata_len);
			freesummary(summary);
			return;
		}
		jsonadd(&jdata, ", \"tests\": { ", &jdata_len);
Mark Andrews's avatar
Mark Andrews committed
1071
		for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
1072
			if ((opts[i].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
1073
				continue;
Mark Andrews's avatar
Mark Andrews committed
1074
			if (summary->results[i][0] == 0)
Mark Andrews's avatar
Mark Andrews committed
1075
				strncat(summary->results[i], "skipped", 100);
Mark Andrews's avatar
Mark Andrews committed
1076 1077 1078 1079 1080 1081
			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)))
			{
Mark Andrews's avatar
Mark Andrews committed
1082 1083 1084 1085 1086 1087 1088
				if (!first)
					jsonadd(&jdata, ", ", &jdata_len);
				snprintf(buf, sizeof(buf), "\"signed\": \"%s%s\"",
					 summary->results[x],
					 summary->seenrrsig ? ",yes" : "");
				jsonadd(&jdata, buf, &jdata_len);
				first = 0;
Mark Andrews's avatar
Mark Andrews committed
1089 1090 1091 1092 1093 1094
			}
			if (badtag) {
				if (strcmp(summary->results[i], "ok") == 0 ||
				    strncmp(summary->results[i], "ok,", 3) == 0)
					continue;
			}
Mark Andrews's avatar
Mark Andrews committed
1095 1096
			if (!first)
				jsonadd(&jdata, ", ", &jdata_len);
1097 1098
			snprintf(buf, sizeof(buf), "\"%s\": \"%s\"", opts[i].name,
				 summary->results[i]);
Mark Andrews's avatar
Mark Andrews committed
1099 1100
			jsonadd(&jdata, buf, &jdata_len);
			first = 0;
Mark Andrews's avatar
Mark Andrews committed
1101
		}
Mark Andrews's avatar
Mark Andrews committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
		jsonadd(&jdata, " }", &jdata_len);
		if (printnsid && summary->nsidlen != 0U) {
                        jsonsafe(summary->nsid, safe, sizeof(safe));
                        snprintf(buf, sizeof(buf), ", \"nsid\": \"%s\"", safe);
                        jsonadd(&jdata, buf, &jdata_len);
                }
		jsonadd(&jdata, " }", &jdata_len);
		freesummary(summary);
		return;
	}
	printf("%s. @%s (%s.):", summary->zone, addrbuf, summary->ns);
	if (allok && summary->allok)
		printf(" all ok");
	else
		for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
		if ((opts[i].what & what) == 0)
			continue;
		if (summary->results[i][0] == 0)
			strncpy(summary->results[i], "skipped", 100);
		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
1138
	if (printnsid && summary->nsidlen != 0U) {
1139 1140 1141 1142 1143 1144 1145 1146 1147
		printf(" (");
		for (i = 0; i < summary->nsidlen; i++) {
			if (isprint(summary->nsid[i] & 0xff))
				putchar(summary->nsid[i]);
			else
				putchar('.');
		}
		printf(")");
	}
1148
	printf("\n");
Mark Andrews's avatar
Mark Andrews committed
1149 1150 1151 1152 1153 1154
	freesummary(summary);
}

static void
report(struct summary *summary) {

Mark Andrews's avatar
Mark Andrews committed
1155 1156 1157 1158
	/*
	 * Send the next test now that we have completed the last test.
	 */
	if (serial && summary->type == 0 && summary->tests == 1) {
1159 1160 1161
		for (summary->last++;
		     summary->last < sizeof(opts)/sizeof(opts[0]);
		     summary->last++) {
Mark Andrews's avatar
Mark Andrews committed
1162
			struct workitem *item;
1163
			if ((opts[summary->last].what & what) == 0)
Mark Andrews's avatar
Mark Andrews committed
1164 1165 1166 1167 1168
				continue;
			item = calloc(1, sizeof(*item));
			if (item == NULL)
				continue;
			item->summary = summary;
1169
			item->test = item->summary->last;
1170
			item->tcpfd = -1;
Mark Andrews's avatar
Mark Andrews committed
1171 1172 1173 1174 1175
			dotest(item);
			return;
		}
	}

Mark Andrews's avatar
Mark Andrews committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	/*
	 * 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) {
1188 1189 1190 1191 1192 1193 1194 1195
		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
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		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;
		}
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
		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
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

		/*
		 * Remove the cross link.
		 */
		summary->xlink->xlink = NULL;
		summary->xlink = NULL;
		if (summary->done) {
			freesummary(summary);
			goto print_deferred;
		}
	}
1233 1234 1235 1236 1237 1238
	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
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

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

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

Mark Andrews's avatar
Mark Andrews committed
1251 1252 1253
/*
 * Free a work item and unlink.
 */
1254 1255
static void
freeitem(struct workitem * item) {
Mark Andrews's avatar
Mark Andrews committed
1256 1257
	if (item->summary)
		report(item->summary);
1258
	if (item->tcpfd != -1) {
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		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
1269 1270
	if (LINKED(item, plink))
		UNLINK(pending, item, plink);
1271 1272 1273 1274 1275 1276
	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);
1277 1278
	if (LINKED(item, seqlink))
		UNLINK(seq[item->id], item, seqlink);
1279 1280 1281
	free(item);
}

Mark Andrews's avatar
Mark Andrews committed
1282 1283 1284
/*
 * Add a tag to the report.
 */
1285
static void
1286
addtag(struct workitem *item, const char *tag) {
1287
	char *result = item->summary->results[item->test];
1288 1289
	if (result[0]) strncat(result, ",", 100);
	strncat(result, tag, 100);
1290 1291
}

Mark Andrews's avatar
Mark Andrews committed
1292
/*
1293
 * Resend a UDP/ICMP request.
Mark Andrews's avatar
Mark Andrews committed
1294
 */
1295 1296 1297
static void
resend(struct workitem *item) {
	int n, fd = -1;
1298
	socklen_t ss_len;
1299 1300 1301

	switch (item->summary->storage.ss_family) {
	case AF_INET:
1302 1303 1304 1305
		if (item->icmp)
			fd = icmp4;
		else
			fd = udp4;
1306
		ss_len = sizeof(struct sockaddr_in);
1307 1308
		break;
	case AF_INET6:
1309 1310 1311 1312
		if (item->icmp)
			fd = icmp6;
		else
			fd = udp6;
1313
		ss_len = sizeof(struct sockaddr_in6);
1314 1315 1316 1317 1318
		break;
	}

	if (fd == -1) {
		addtag(item, "skipped");
1319
		item->summary->allok = 0;
1320 1321 1322 1323
		freeitem(item);
		return;
	}

1324
	if (!item->outstanding && outstanding > maxoutstanding) {
1325 1326
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
1327 1328 1329
		if (LINKED(item, link))
			UNLINK(work, item, link);
		APPEND(pending, item, plink);
1330 1331 1332 1333
		return;
	}

	n = sendto(fd, item->buf, item->buflen, 0,
1334
		   (struct sockaddr *)&item->summary->storage, ss_len);
1335 1336 1337 1338 1339 1340 1341 1342
	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);
1343
		sent++;
1344 1345 1346 1347 1348
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
Mark Andrews's avatar
Mark Andrews committed
1349 1350
		if (LINKED(item, link))
			UNLINK(work, item, link);
1351
		APPEND(work, item, link);
1352 1353
	} else if (item->summary->type) {
		nextserver(item);
1354 1355
	} else {
		addtag(item, "failed");
1356 1357
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
1358 1359 1360 1361
		freeitem(item);
	}
}

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
unsigned short
cksum(void *ptr, size_t len) {
	unsigned char *data = ptr;
	unsigned int sum = 0;

	while (len > 1) {
		sum += *(unsigned short *)(void *)data;
		if (sum & 0x80000000)
			sum = (sum & 0xFFFF) + (sum >> 16);
		data += 2;
		len -= 2;
	}

	if (len != 0)
		sum += *(unsigned char*)data;

	while (sum >> 16)
		sum = (sum & 0xFFFF) + (sum >> 16);

	return (~sum);
}

static void
send_icmp4(struct workitem *item) {
	struct icmp icmp;
	int id = random() & 0xffff;
	int tries = 0;
	int n;

	while (!checkseq(&item->summary->storage, id) &&
	       tries++ < 0xffff)
		id = (id + 1) & 0xffff;

	if (tries == 0xffff) {
		addtag(item, "skipped");
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
		freeitem(item);
		return;
	}

	item->id = id;

	memset(&icmp, 0, sizeof(icmp));
	icmp.icmp_type = ICMP_ECHO;
	icmp.icmp_code = 0;
	icmp.icmp_id = ident;
	icmp.icmp_seq = htons(item->id);
	icmp.icmp_cksum = cksum(&icmp, sizeof(icmp));
	memcpy(item->buf, &icmp, sizeof(icmp));
	item->buflen = sizeof(icmp);

	if (!item->outstanding && outstanding > maxoutstanding) {
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		APPEND(pending, item, plink);
		APPEND(seq[item->id], item, seqlink);
		return;
	}

	gettimeofday(&item->when, NULL);
	item->when.tv_sec += 1;

	APPEND(work, item, link);
	APPEND(seq[item->id], item, seqlink);

	n = sendto(icmp4, &icmp, sizeof(icmp), 0,
		   (struct sockaddr *)&item->summary->storage,
		   sizeof(struct sockaddr_in));
	if (n < 0) {
		addtag(item, "skipped");
		freeitem(item);
	}
}

static void
send_icmp6(struct workitem *item) {
	struct icmp6_hdr icmp;
	int id = random() & 0xffff;
	int tries = 0;
	int n;

	while (!checkseq(&item->summary->storage, id) &&
	       tries++ < 0xffff)
		id = (id + 1) & 0xffff;

	if (tries == 0xffff) {
		addtag(item, "skipped");
		item->summary->allok = 0;
		item->summary->seenfailure = 1;
		freeitem(item);
		return;
	}

	item->id = id;

	memset(&icmp, 0, sizeof(icmp));
	icmp.icmp6_type = ICMP6_ECHO_REQUEST;
	icmp.icmp6_code = 0;
	icmp.icmp6_id = ident;
	icmp.icmp6_seq = htons(item->id);
	icmp.icmp6_cksum = cksum(&icmp, sizeof(icmp));
	memcpy(item->buf, &icmp, sizeof(icmp));
	item->buflen = sizeof(icmp);

	if (!item->outstanding && outstanding > maxoutstanding) {
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		APPEND(pending, item, plink);
		APPEND(seq[item->id], item, seqlink);
		return;
	}

	gettimeofday(&item->when, NULL);
	item->when.tv_sec += 1;

	APPEND(work, item, link);
	APPEND(seq[item->id], item, seqlink);

	n = sendto(icmp6, &icmp, sizeof(icmp), 0,
		   (struct sockaddr *)&item->summary->storage,
		   sizeof(struct sockaddr_in6));
	if (n < 0) {
		perror("send_icmp6: sendto");
		addtag(item, "skipped");
		freeitem(item);
	}
}

static void
send_icmp(struct workitem *item) {

	item->icmp = 1;

	switch (item->summary->storage.ss_family) {
	case AF_INET:
		if (icmp4 != -1) {
			send_icmp4(item);
			return;
		}
		break;
	case AF_INET6:
		if (icmp6 != -1) {
			send_icmp6(item);
			return;
		}
		break;
	}

	addtag(item, "skipped");
	freeitem(item);
}

Mark Andrews's avatar
Mark Andrews committed
1515 1516 1517
/*
 * Start a individual test.
 */
Mark Andrews's avatar
Mark Andrews committed
1518 1519 1520 1521
static void
dotest(struct workitem *item) {
	unsigned char *cp;
	unsigned int ttl;
Mark Andrews's avatar
Mark Andrews committed
1522 1523