genreport.c 35.7 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
12 13 14 15
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/uio.h>
Mark Andrews's avatar
Mark Andrews committed
16

17
#define ns_t_rrsig 46
Mark Andrews's avatar
Mark Andrews committed
18 19
#define ns_t_dnskey 48

20
static int eof = 0;
Mark Andrews's avatar
Mark Andrews committed
21 22
static int maxfd = -1;
static fd_set rfds, wfds;
23 24
static int outstanding;
static int maxoutstanding = 100;
Mark Andrews's avatar
Mark Andrews committed
25 26 27 28 29 30

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

static int udp4 = -1;
static int udp6 = -1;
31 32 33 34
static int debug = 0;

static union res_sockaddr_union servers[10];
static int nservers = 0;
Mark Andrews's avatar
Mark Andrews committed
35 36 37 38 39 40 41 42

#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; \
43
	(item)->link.linked = 1; \
Mark Andrews's avatar
Mark Andrews committed
44 45 46 47 48 49 50 51 52 53 54 55 56
	(list).tail = (item); \
} 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; \
57
	(item)->link.linked = 0; \
Mark Andrews's avatar
Mark Andrews committed
58 59 60 61
} while (0)

#define NEXT(item, link) (item)->link.next
#define PREV(item, link) (item)->link.next
62
#define LINKED(item, link) (item)->link.linked
Mark Andrews's avatar
Mark Andrews committed
63 64 65 66

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

Mark Andrews's avatar
Mark Andrews committed
67
static struct {
68 69 70 71 72 73 74 75
	const char *name;
	unsigned short rdlen;
	const char *rdata;
	unsigned short udpsize;
	unsigned short flags;
	unsigned short version;
	unsigned int tcp;
	unsigned int ignore;
Mark Andrews's avatar
Mark Andrews committed
76 77 78 79 80 81 82
	unsigned int rd;
	unsigned int ra;
	unsigned int cd;
	unsigned int ad;
	unsigned int aa;
	unsigned int z;
	unsigned int opcode;
83 84
	unsigned short type;
} opts[] = {
Mark Andrews's avatar
Mark Andrews committed
85 86 87 88 89 90 91 92 93 94 95 96
	{ "dns",       0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns",      0, "", 4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1",     0, "", 4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns@512",  0, "",  512, 0x0000, 0, 0, 1, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey },
	{ "ednsopt",   4, "\x00\x64\x00",
			      4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "edns1opt",  4, "\x00\x64\x00",
			      4096, 0x0000, 1, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "do",        4, "\0\144\0",
			      4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "ednsflags", 0, "", 4096, 0x0080, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "optlist",   4 + 8 + 4 + 12,
97 98 99 100
	  "\x00\x03\x00\x00" 		     /* NSID */
	  "\x00\x08\x00\x04\x00\x01\x00\x00" /* ECS */
	  "\x00\x09\x00\x00" 		     /* EXPIRE */
	  "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",	/* COOKIE */
Mark Andrews's avatar
Mark Andrews committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	                      4096, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
#ifdef common
	{ "bind",     12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",
	                      4096, 0x8000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "dig",      12, "\x00\x0a\x00\x08\x01\x02\x03\x04\x05\x06\x07\x08",
	                      4096, 0x0000, 0, 0, 0, 0, 1, 0, 0, 1, 0,  0, ns_t_soa },
#endif
#ifdef full
	{ "zflag",     0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 1,  0, ns_t_soa },
	{ "aa",        0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 1, 0,  0, ns_t_soa },
	{ "ad",        0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 1, 0, 0,  0, ns_t_soa },
	{ "cd",        0, "",    0, 0x0000, 0, 0, 0, 0, 0, 1, 0, 0, 0,  0, ns_t_soa },
	{ "ra",        0, "",    0, 0x0000, 0, 0, 0, 0, 1, 0, 0, 0, 0,  0, ns_t_soa },
	{ "rd",        0, "",    0, 0x0000, 0, 0, 0, 1, 0, 0, 0, 0, 0,  0, ns_t_soa },
	{ "opcode",    0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0 },
	{ "opcodeflg", 0, "",    0, 0x0000, 0, 0, 0, 1, 1, 1, 1, 1, 1, 15, 0 },
	{ "type666",   0, "",    0, 0x0000, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 666 },
#endif
	{ "ednstcp",   0, "",  512, 0x8000, 0, 1, 0, 0, 0, 0, 0, 0, 0,  0, ns_t_dnskey }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
};

struct summary {
	char zone[1024];
	char ns[1024];
	struct sockaddr_storage storage;
	int tests;
	int done;
	int type;
	int nodataa;
	int nodataaaa;
	int nxdomain;
	int nxdomaina;
	int nxdomainaaaa;
	int seenrrsig;
	struct summary *xlink;
	char results[sizeof(opts)/sizeof(opts[0])][100];
};

Mark Andrews's avatar
Mark Andrews committed
139 140 141 142
struct workitem {
	struct {
		struct workitem *next;
		struct workitem *prev;
143 144
		int linked;
	} link, clink, rlink, idlink;
Mark Andrews's avatar
Mark Andrews committed
145 146
	unsigned short id;
	struct timeval when;
147
	int type;
Mark Andrews's avatar
Mark Andrews committed
148 149 150 151
	int test;
	int sends;
	int buflen;
	int tcpfd;
152 153 154 155
	int outstanding;
	int havelen;
	int readlen;
	int read;
Mark Andrews's avatar
Mark Andrews committed
156
	unsigned char buf[512];
157 158 159
	unsigned char tcpbuf[0x10000];
	char result[100];
	struct summary *summary;
Mark Andrews's avatar
Mark Andrews committed
160 161 162 163 164
};

static struct {
	struct workitem *head;
	struct workitem *tail;
165
} work, connecting, reading, ids[0x10000];
Mark Andrews's avatar
Mark Andrews committed
166

167 168
static void
connecttoserver(struct workitem *item);
Mark Andrews's avatar
Mark Andrews committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

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;
			
		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);
}

static int
checkid(struct sockaddr_storage *storage, int id) {
	struct workitem *item;

	item = HEAD(ids[id]);
	while (item != NULL &&
205
	       !storage_equal(storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
206 207 208 209
		item = NEXT(item, idlink);
	return ((item == NULL) ? 1 : 0);
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static void
report(struct summary *summary) {
	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;

	summary->tests--;
	if (summary->tests)
		return;

	if (summary->xlink) {
		if (summary->nodataa)
			summary->xlink->nodataa = 1;
		if (summary->nodataaaa)
			summary->xlink->nodataaaa = 1;
		if (summary->nxdomaina)
			summary->xlink->nxdomaina = 1;
		if (summary->nxdomainaaaa)
			summary->xlink->nxdomainaaaa = 1;
		summary->xlink->xlink = NULL;
		summary->xlink = NULL;
		summary->done = 1;
	}

	if ((summary->type == ns_t_a || summary->type == ns_t_aaaa) &&
	    summary->nodataa && summary->nodataaaa) {
		printf("%s. %s: no address records found\n",
		       summary->zone, summary->ns);
		free(summary);
		return;
	}
	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);
		free(summary);
		return;
	}

	if (summary->done) {
		free(summary);
		return;
	}
	
	if (summary->storage.ss_family == AF_INET)
		addr = &s->sin_addr;
	else
		addr = &s6->sin6_addr;
	inet_ntop(summary->storage.ss_family, addr, addrbuf, sizeof(addrbuf));
	if (summary->type != 0 && summary->nxdomain) {
		if (summary->type == ns_t_ns) {
			printf("%s.: NS nxdomain\n", summary->zone);
			free(summary);
			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");
		free(summary);
		return;
	}
	if (summary->type == ns_t_a) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" A lookup failed\n");
		free(summary);
		return;
	}
	if (summary->type == ns_t_aaaa) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" AAAA lookup failed\n");
		free(summary);
		return;
	}
	if (summary->type == ns_t_ns) {
		printf("%s. %s:", summary->zone, summary->ns);
		printf(" NS lookup failed\n");
		free(summary);
		return;
	}
	x = -1;
	printf("%s. @%s (%s.):", summary->zone, addrbuf, summary->ns);
	for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
		if (summary->results[i][0] == 0)
			strcpy(summary->results[i], "skipped");
		if (strcmp(opts[i].name, "do") == 0)
			x = i;
		if (strcmp(opts[i].name, "ednstcp") == 0 && x != -1) {
			printf(" signed=%s", summary->results[x]);
			if (summary->seenrrsig)
				printf(",yes");
		}
		printf(" %s=%s", opts[i].name, summary->results[i]);
	}
	printf("\n");
	free(summary);
}

static void
freeitem(struct workitem * item) {
	report(item->summary);
	if (item->tcpfd != 0) {
		FD_CLR(item->tcpfd, &rfds);
		FD_CLR(item->tcpfd, &wfds);
		rhandlers[item->tcpfd] = NULL;
		whandlers[item->tcpfd] = NULL;
		close(item->tcpfd);
	}
	if (item->outstanding)
		outstanding--;
	if (LINKED(item, link))
		UNLINK(work, item, link);
	if (LINKED(item, rlink))
		UNLINK(reading, item, rlink);
	if (LINKED(item, clink))
		UNLINK(connecting, item, clink);
	if (LINKED(item, idlink))
		UNLINK(ids[item->id], item, idlink);
	free(item);
}

static void
addtag(struct workitem *item, char *tag) {
	char *result = item->summary->results[item->test];
	int i = strlen(result);
	if (i)
		result[i++] = ',';
	strcpy(result + i, tag);
}

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");
		freeitem(item);
		return;
	}

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

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

Mark Andrews's avatar
Mark Andrews committed
394 395 396 397
static void
dotest(struct workitem *item) {
	unsigned char *cp;
	unsigned int ttl;
Mark Andrews's avatar
Mark Andrews committed
398
	int n, fd, id, tries = 0, opcode;
Mark Andrews's avatar
Mark Andrews committed
399

400
	switch (item->summary->storage.ss_family) {
Mark Andrews's avatar
Mark Andrews committed
401 402 403 404 405 406 407 408 409
	case AF_INET:
		fd = udp4;
		break;
	case AF_INET6:
		fd = udp6;
		break;
	}

	if (fd == -1) {
410 411
		addtag(item, "skipped");
		freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
412 413
		return;
	}
Mark Andrews's avatar
Mark Andrews committed
414 415 416 417 418 419 420 421 422

	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,
423 424
			opts[item->test].type, NULL, 0, NULL,
			item->buf, sizeof(item->buf));
Mark Andrews's avatar
Mark Andrews committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	/* 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;
	}
	if (opts[item->test].rd)
		item->buf[2] |= 0x1;	/* set rd */
	else
		item->buf[2] &= ~0x1;	/* clear rd */
	if (opts[item->test].z)	/* set z */
		item->buf[3] |= 0x40;
		
440 441
	if (n > 0) {
		char name[1024];
Mark Andrews's avatar
Mark Andrews committed
442 443 444
		/*
		 * Make zone canonical.
		 */
445 446 447 448 449 450
		dn_expand(item->buf, item->buf + n, item->buf + 12,
			  name, sizeof(name));
		strcpy(item->summary->zone, name);
	}
	
	if (n > 0 && opts[item->test].udpsize > 0) {
Mark Andrews's avatar
Mark Andrews committed
451
		cp = item->buf + n;
452 453 454 455 456 457 458 459 460 461 462
		*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
463 464
		memcpy(cp, opts[item->test].rdata, opts[item->test].rdlen);
		cp += opts[item->test].rdlen;
465 466
		item->buf[11] = 1;			/* adcount */
		n = cp - item->buf;			/* total length */
Mark Andrews's avatar
Mark Andrews committed
467 468 469 470 471
	}

	if (n > 0) {
		id = item->buf[0] << 8 | item->buf[1];

472 473
		while (!checkid(&item->summary->storage, id) &&
		       tries++ < 0xffff)
Mark Andrews's avatar
Mark Andrews committed
474 475 476
			id = (id + 1) & 0xffff;
		
		if (tries == 0xffff) {
477 478
			addtag(item, "skipped");
			freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
479 480
			return;
		}
481
 
Mark Andrews's avatar
Mark Andrews committed
482 483 484 485 486
		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		item->buflen = n;

487 488 489 490 491 492 493 494 495 496 497 498 499
		if (opts[item->test].tcp) {
			connecttoserver(item);
			return;
		}

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

Mark Andrews's avatar
Mark Andrews committed
500
		n = sendto(fd, item->buf, item->buflen, 0,
501 502
			   (struct sockaddr *)&item->summary->storage,
			   item->summary->storage.ss_len);
Mark Andrews's avatar
Mark Andrews committed
503 504 505
	}

	if (n > 0) {
506 507 508 509 510 511 512 513 514
		if (debug)
			printf("%s rdlen=%u udpsize=%u flags=%04x version=%u "
			       "tcp=%u ignore=%u id=%u\n",
			       opts[item->test].name, opts[item->test].rdlen,
			       opts[item->test].udpsize, opts[item->test].flags,
			       opts[item->test].version, opts[item->test].tcp,
			       opts[item->test].ignore, item->id);
		if (!item->outstanding++)
			outstanding++;
Mark Andrews's avatar
Mark Andrews committed
515
		gettimeofday(&item->when, NULL);
516
		item->when.tv_sec += 1;
Mark Andrews's avatar
Mark Andrews committed
517 518 519
		item->sends = 1;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
520 521 522 523
	} else {
		addtag(item, "failed");
		freeitem(item);
	}
Mark Andrews's avatar
Mark Andrews committed
524 525 526 527 528
}

static void
check(char *zone, char *ns, char *address) {
	size_t i;
Mark Andrews's avatar
Mark Andrews committed
529
	int fd;
Mark Andrews's avatar
Mark Andrews committed
530 531 532
	struct in_addr addr;
	struct in6_addr addr6;
	struct sockaddr_storage storage;
533
	struct summary *summary;
Mark Andrews's avatar
Mark Andrews committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	
	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;

556 557 558
	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return;
Mark Andrews's avatar
Mark Andrews committed
559

560
	summary->storage = storage;
Mark Andrews's avatar
Mark Andrews committed
561

562 563 564
	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;
Mark Andrews's avatar
Mark Andrews committed
565

566 567 568
	ns_makecanon(ns, summary->ns, sizeof(summary->ns));
	i = strlen(summary->ns);
	if (i) summary->ns[i-1] = 0;
Mark Andrews's avatar
Mark Andrews committed
569

Mark Andrews's avatar
Mark Andrews committed
570 571
	for (i = 0; i < sizeof(opts)/sizeof(opts[0]); i++) {
		struct workitem *item = calloc(1, sizeof(*item));
Mark Andrews's avatar
Mark Andrews committed
572

573 574 575
		if (item == NULL) {
			summary->tests++;
			report(summary);
Mark Andrews's avatar
Mark Andrews committed
576
			break;
577
		}
Mark Andrews's avatar
Mark Andrews committed
578

579 580
		summary->tests++;
		item->summary = summary;
Mark Andrews's avatar
Mark Andrews committed
581 582 583 584 585
		item->test = i;
		dotest(item);
	}
}

586 587 588
static char *
rcodetext(int code) {
	static char buf[64];
Mark Andrews's avatar
Mark Andrews committed
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	switch(code) {
	case 0: return("noerror");
	case 1: return("formerr");
	case 2: return("servfail");
	case 3: return("nxdomain");
	case 4: return("notimpl");
	case 5: return("refused");
	case 6: return("yxdomain");
	case 7: return("yxrrset");
	case 8: return("nxrrset");
	case 9: return("notauth");
	case 10: return("notzone");
	case 16: return("badvers");
	case 23: return("badcookie");
	default:
Mark Andrews's avatar
Mark Andrews committed
605
		snprintf(buf, sizeof(buf), "rcode%u", code);
606 607 608 609 610 611 612 613 614 615 616 617
		return (buf);
	}
}

static void
dolookup(struct workitem *item, int type) {
	char name[1024];
	int n, fd = -1;

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

618
 again:
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	if (servers[item->test].sin.sin_family == AF_INET)
		memcpy(&item->summary->storage, &servers[item->test].sin,
		       sizeof(servers[item->test].sin));
	else
		memcpy(&item->summary->storage, &servers[0].sin6,
		       sizeof(servers[item->test].sin6));

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

	if (fd == -1) {
636 637
		if (++item->test < nservers)
			goto again;
638 639 640 641 642 643 644 645 646 647 648 649 650 651
		addtag(item, "skipped");
		freeitem(item);
		return;
	}
	
	if (type == ns_t_ns)
		n = res_mkquery(ns_o_query, item->summary->zone, ns_c_in,
				type, NULL, 0, NULL,
				item->buf, sizeof(item->buf));
	else
		n = res_mkquery(ns_o_query, item->summary->ns, ns_c_in,
				type, NULL, 0, NULL,
				item->buf, sizeof(item->buf));
	if (n > 0) {
Mark Andrews's avatar
Mark Andrews committed
652
		int id, tries = 0;
653

Mark Andrews's avatar
Mark Andrews committed
654 655 656
		/*
		 * Make name canonical.
		 */
657
		dn_expand(item->buf, item->buf + n, item->buf + 12,
Mark Andrews's avatar
Mark Andrews committed
658 659 660 661
			  name, sizeof(name));

		switch (type) {
		case ns_t_ns:
662
			strcpy(item->summary->zone, name);
Mark Andrews's avatar
Mark Andrews committed
663 664 665
			break;
		case ns_t_a:
		case ns_t_aaaa:
666
			strcpy(item->summary->ns, name);
Mark Andrews's avatar
Mark Andrews committed
667 668 669 670 671
			break;
		}

		item->buf[2] |= 0x1;	/* set rd */
		id = item->buf[0] << 8 | item->buf[1];
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

		while (!checkid(&item->summary->storage, id) &&
		       tries++ < 0xffff)
			id = (id + 1) & 0xffff;
		
		if (tries == 0xffff) {
			addtag(item, "skipped");
			freeitem(item);
			return;
		}

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

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
		n = sendto(fd, item->buf, item->buflen, 0,
			   (struct sockaddr *)&item->summary->storage,
			   item->summary->storage.ss_len);
	}
	if (n > 0) {
		if (debug)
			printf("lookup %u id=%u\n", item->type, item->id);
		if (!item->outstanding++)
			outstanding++;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 1;
		item->sends++;
		APPEND(work, item, link);
		APPEND(ids[item->id], item, idlink);
	} else {
		addtag(item, "skipped");
		freeitem(item);
	}
}

static struct summary *
lookupa(char *zone, char *ns) {
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return (NULL);

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

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

	item = calloc(1, sizeof(*item));
	if (item == NULL)
		free(summary);

	item->summary = summary;
	dolookup(item, ns_t_a);
	return (summary);
}

static struct summary *
lookupaaaa(char *zone, char *ns) {
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return (NULL);

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

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

	item = calloc(1, sizeof(*item));
	if (item == NULL)
		free(summary);

	item->summary = summary;
	dolookup(item, ns_t_aaaa);
	return (summary);
}

static void
lookupns(char *zone) {
	struct summary *summary;
	struct workitem *item;
	unsigned int i;

	summary = calloc(1, sizeof(*summary));
	if (summary == NULL)
		return;
Mark Andrews's avatar
Mark Andrews committed
778 779 780 781

	ns_makecanon(zone, summary->zone, sizeof(summary->zone));
	i = strlen(summary->zone);
	if (i) summary->zone[i-1] = 0;
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

	item = calloc(1, sizeof(*item));
	if (item == NULL)
		free(summary);

	item->summary = summary;
	dolookup(item, ns_t_ns);
}

static void
process(struct workitem *item, unsigned char *buf, int n) {
	char name[1024], ns[1024];
	unsigned int i, id, qr, aa, tc, rd, ra, z, ad, cd;
	unsigned int qrcount, ancount, aucount, adcount;
	unsigned int opcode, rcode;
	unsigned int type, ednssize = 0, class, ednsttl = 0, ttl, rdlen;
	unsigned char *cp, *eom; 	
	int seenopt = 0, seensoa = 0, seenrrsig = 0;
	int seennsid = 0, seenecs = 0, seenexpire = 0, seencookie = 0;
	int seenecho = 0;
	char addrbuf[64];
	int ok = 1;

	/* process message header */

	id = buf[0] << 8| buf[1];
	qr = (buf[2] >> 7) & 0x1;
	opcode = (buf[2] >> 3) & 0xf;
	aa = (buf[2] >> 2) & 0x1;
	tc = (buf[2] >> 1) & 0x1;
	rd = buf[2] & 0x1;
	ra = (buf[3] >> 7) & 0x1;
	z = (buf[3] >> 6) & 0x1;
	ad = (buf[3] >> 5) & 0x1;
	cd = (buf[3] >> 4) & 0x1;
	rcode = buf[3] & 0xf;
	qrcount = buf[4] << 8 | buf[5];
	ancount = buf[6] << 8 | buf[7];
	aucount = buf[8] << 8 | buf[9];
	adcount = buf[10] << 8 | buf[11];

Mark Andrews's avatar
Mark Andrews committed
823 824
	if (tc && item->tcpfd == 0 &&
	    (item->summary->type || !opts[item->test].ignore)) {
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
		if (LINKED(item, link))
			UNLINK(work, item, link);
		connecttoserver(item);
		return;
	}

	/* process message body */

	if (item->type == ns_t_a || item->type == ns_t_aaaa)
		strcpy(ns, item->summary->ns);

	cp = buf + 12;
	eom = buf + n;
	for (i = 0; i < qrcount; i++) {
		n = dn_expand(buf, eom, cp, name, sizeof(name));
		if (n < 0 || (eom - cp) < n)
			goto err;
		cp += n;
		if ((eom - cp) < 4)
			goto err;
		type = ns_get16(cp);
		cp += 2;
		class = ns_get16(cp);
		cp += 2;
		if (debug)
			printf("QR: %s./%u/%u\n", name, type, class);

		/*
		 * No address / NS records?
		 */
		if (item->type == ns_t_a && type == ns_t_a &&
		    strcasecmp(item->summary->ns, name) == 0 &&
		    rcode == 0 && ancount == 0) {
			item->summary->done = 1;
			item->summary->nodataa = 1;
		}
		if (item->type == ns_t_aaaa && type == ns_t_aaaa &&
		    strcasecmp(item->summary->ns, name) == 0 &&
		    rcode == 0 && ancount == 0) {
			item->summary->done = 1;
			item->summary->nodataaaa = 1;
		}
		if (item->type == ns_t_ns && type == ns_t_ns &&
		    strcasecmp(item->summary->zone, name) == 0 &&
		    rcode == 0 && ancount == 0)
			item->summary->done = 1;

		/*
		 * NXDOMAIN?
		 */
		if (item->type == ns_t_a && type == ns_t_a &&
		    strcasecmp(item->summary->ns, name) == 0 &&
		    rcode == 3 && ancount == 0)
			item->summary->nxdomaina = 1;
		if (item->type == ns_t_aaaa && type == ns_t_aaaa &&
		    strcasecmp(item->summary->ns, name) == 0 &&
		    rcode == 3 && ancount == 0)
			item->summary->nxdomainaaaa = 1;
		if (item->type == ns_t_ns && type == ns_t_ns &&
		    strcasecmp(item->summary->zone, name) == 0 &&
		    rcode == 3 && ancount == 0)
			item->summary->nxdomain = 1;
	}

	for (i = 0; i < ancount; i++) {
		n = dn_expand(buf, eom, cp, name, sizeof(name));
		if (n < 0 || (eom - cp) < n)
			goto err;
		cp += n;
		if ((eom - cp) < 8)
			goto err;
		type = ns_get16(cp);
		cp += 2;
		class = ns_get16(cp);
		cp += 2;
		ttl = ns_get32(cp);
		cp += 4;
		rdlen = ns_get16(cp);
		cp += 2;
		if ((eom - cp) < rdlen)
			goto err;
		if ((item->type == ns_t_a || item->type == ns_t_aaaa) &&
		    type == ns_t_cname && strcasecmp(ns, name) == 0) {
			n = dn_expand(buf, eom, cp, ns, sizeof(ns));
			if (n != rdlen)
				goto err;
		}
		if (item->type == ns_t_a && type == ns_t_a &&
		    strcasecmp(ns, name) == 0)
		{
			if (rdlen != 4)
				goto err;
			inet_ntop(AF_INET, cp, addrbuf, sizeof(addrbuf));
			check(item->summary->zone, item->summary->ns, addrbuf);
			item->summary->done = 1;
		}
		if (item->type == ns_t_aaaa && type == ns_t_aaaa &&
		    strcasecmp(ns, name) == 0)
		{
			if (rdlen != 16)
				goto err;
			inet_ntop(AF_INET6, cp, addrbuf, sizeof(addrbuf));
			check(item->summary->zone, item->summary->ns, addrbuf);
			item->summary->done = 1;
		}
		if (item->type == ns_t_ns && type == ns_t_ns &&
		    strcasecmp(item->summary->zone, name) == 0)
		{
			struct summary *summarya, *summaryaaaa;
			n = dn_expand(buf, eom, cp, ns, sizeof(ns));
			if (n != rdlen)
				goto err;
			item->summary->done = 1;
			summarya = lookupa(item->summary->zone, ns);
			summaryaaaa = lookupaaaa(item->summary->zone, ns);
			if (summarya && summaryaaaa) {
				summarya->xlink = summaryaaaa;
				summaryaaaa->xlink = summarya;
			}
		}
		cp += rdlen;
		if (type == ns_t_soa &&
		    strcasecmp(item->summary->zone, name) == 0)
			seensoa = 1;
		else if (type == ns_t_soa)
		    printf("%s %s\n", item->summary->zone, name);
		if (type == ns_t_rrsig)
			seenrrsig = 1;
		if (debug)
			printf("AN: %s./%u/%u/%u/%u\n",
			       name, type, class, ttl, rdlen);
	}

	for (i = 0; i < aucount; i++) {
		n = dn_expand(buf, eom, cp, name, sizeof(name));
		if (n < 0 || (eom - cp) < n)
			goto err;
		cp += n;
		if ((eom - cp) < 8)
			goto err;
		type = ns_get16(cp);
		cp += 2;
		class = ns_get16(cp);
		cp += 2;
		ttl = ns_get32(cp);
		cp += 4;
		rdlen = ns_get16(cp);
		cp += 2;
		if ((eom - cp) < rdlen)
			goto err;
		cp += rdlen;
		if (debug)
			printf("AU: %s./%u/%u/%u/%u\n",
			       name, type, class, ttl, rdlen);
	}

	for (i = 0; i < adcount; i++) {
		n = dn_expand(buf, eom, cp, name, sizeof(name));
		if (n < 0 || (eom - cp) < n)
			goto err;
		cp += n;
		if ((eom - cp) < 8)
			goto err;
		type = ns_get16(cp);
		cp += 2;
		class = ns_get16(cp);
		cp += 2;
		ttl = ns_get32(cp);
		cp += 4;
		rdlen = ns_get16(cp);
		cp += 2;
		if ((eom - cp) < rdlen)
			goto err;
		if (type == ns_t_opt && !seenopt) {
			unsigned char *options;
			ednsttl = ttl;
			ednssize = class;
			seenopt = 1;
			options = cp;
			while (((cp + rdlen) - options) >= 4) {
				unsigned int code, optlen;
				code = ns_get16(options);
				options += 2;
				optlen = ns_get16(options);
				options += 2;
				if (code == 3 && optlen > 0)
					seennsid = 1;
				if (code == 8)
					seenecs = 1;
				if (code == 9 && optlen == 4)
					seenexpire = 1;
				if (code == 10 && optlen >= 16 && optlen <= 24)
					seencookie = 1;
				if (code == 100)
					seenecho = 1;
				options += optlen;
			}
			if (options != (cp + rdlen))
				goto err;
		}
		cp += rdlen;
		if (debug)
			printf("AD: %s./%u/%u/%u/%u\n",
			       name, type, class, ttl, rdlen);
	}
	if (cp != eom)
		goto err;

	rcode += (ednsttl & 0xff000000) >> 20;

	if (debug)
		printf("id=%-5u %-9s opcode=%u rcode=%u aa=%u tc=%u rd=%u "
		       "ra=%u z=%u ad=%u cd=%u qrcount=%u ancount=%u "
		       "aucount=%u adcount=%u\n"
		       "\tseensoa=%u seenrrsig=%u seenopt=%u "
		       "seennsid=%u seenecs=%u seenexpire=%u seencookie=%u\n",
		       id, opts[item->test].name, opcode, rcode,
		       aa, tc, rd, ra, z, ad, cd,
		       qrcount, ancount, aucount, adcount,
		       seensoa, seenrrsig, seenopt,
		       seennsid, seenecs, seenexpire, seencookie);
Mark Andrews's avatar
Mark Andrews committed
1046 1047 1048 1049 1050 1051 1052 1053

	if (item->summary->type)
		goto done;

	if (opts[item->test].version == 0) {
		if (opts[item->test].opcode == 0 && rcode != 0)
			addtag(item, rcodetext(rcode)), ok = 0;
		if (opts[item->test].opcode != 0 && rcode != 4)
1054
			addtag(item, rcodetext(rcode)), ok = 0;
Mark Andrews's avatar
Mark Andrews committed
1055
	}
1056 1057 1058 1059 1060
	if (opts[item->test].version != 0)
		if (rcode != 16) /* badvers */
			addtag(item, rcodetext(rcode)), ok = 0;
	if ((ednsttl & 0xff0000) != 0)
		addtag(item, "badversion"), ok = 0;
Mark Andrews's avatar
Mark Andrews committed
1061 1062 1063 1064
	if (!seenopt && opts[item->test].udpsize)
		addtag(item, "noopt"), ok = 0;
	if (seenopt && opts[item->test].udpsize == 0)
		addtag(item, "opt"), ok = 0;
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	if (opts[item->test].type == ns_t_soa)
		if (opts[item->test].version == 0 &&
		    !opts[item->test].ignore && !seensoa)
			addtag(item, "nosoa"), ok = 0;
	if (opts[item->test].type == ns_t_soa)
		if (opts[item->test].version != 0 && seensoa)
			addtag(item, "soa"), ok = 0;
	if (seenecho)
		addtag(item, "echoed"), ok = 0;
	if ((ednsttl & 0x8000) == 0 && seenrrsig)
		addtag(item, "nodo"), ok = 0;
Mark Andrews's avatar
Mark Andrews committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	if (!aa && opts[item->test].version == 0 &&
	    opts[item->test].opcode == 0)
		addtag(item, "noaa"), ok = 0;
	if (aa && opts[item->test].opcode != 0)
		addtag(item, "aa"), ok = 0;
	if (ra && opts[item->test].opcode)
		addtag(item, "ra"), ok = 0;
	if (rd &&
	    (opts[item->test].opcode || !opts[item->test].rd))
		addtag(item, "rd"), ok = 0;
	if (!rd && opts[item->test].rd)
		addtag(item, "nord"), ok = 0;
	if (ad &&
	    (opts[item->test].opcode || !opts[item->test].ad))
		addtag(item, "ad"), ok = 0;
	if (cd)
		addtag(item, "cd"), ok = 0;
	if (z)
		addtag(item, "z"), ok = 0;
1095 1096 1097 1098 1099 1100 1101 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 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	if ((ednsttl & 0x7fff) != 0)
		addtag(item, "mbz"), ok = 0;
	if (seenrrsig)
		item->summary->seenrrsig = 1;
	if (ok)
		addtag(item, "ok");
	if (seennsid)
		addtag(item, "nsid");
	if (seenexpire)
		addtag(item, "expire");
	if (seencookie)
		addtag(item, "cookie");
	if (seenecs)
		addtag(item, "subnet");
		
	goto done;
 err:
	addtag(item, "malformed");
 done:
	freeitem(item);
}

static void
tcpread(int fd) {
	struct workitem *item;
	int n;

	item = HEAD(reading);
	while (item && item->tcpfd != fd)
		item = NEXT(item, rlink);
	if (item == NULL)
		return;
 again:
	n = read(fd, item->tcpbuf + item->read, item->readlen - item->read);
	if (n == 0) {
		addtag(item, "eof");
		freeitem(item);
		return;
	}
	if (n < 0 && (errno == EAGAIN || errno == EINTR))
		return;
	if (n < 0) {
		if (errno == ECONNRESET)
			addtag(item, "reset");
		else if (errno == EPIPE)
			addtag(item, "pipe");
		else 
			addtag(item, "read");
		freeitem(item);
		return;
	} 
	item->read += n;
	if (item->read == item->readlen) {
		if (!item->havelen) {
			item->readlen = item->tcpbuf[0] << 8 | item->tcpbuf[1];
			item->havelen = 1;
			item->read = 0;
			goto again;
		}
		process(item, item->tcpbuf, item->readlen);
	}
}

static void
startread(struct workitem *item) {
	struct iovec iov[2];
	int iovcnt = 2;
	unsigned char buf[2];
	int n;

	FD_SET(item->tcpfd, &rfds);
	if (item->tcpfd > maxfd)
		maxfd = item->tcpfd;
	rhandlers[item->tcpfd] = tcpread;
	gettimeofday(&item->when, NULL);
	item->when.tv_sec += 10;
	APPEND(reading, item, rlink);
	item->havelen = 0;
	item->readlen = 2;
	item->read = 0;

	buf[0] = item->buflen>>8;
	buf[1] = item->buflen&0xff;
	iov[0].iov_base = &buf;
	iov[0].iov_len = 2;
	iov[1].iov_base = &item->buf;
	iov[1].iov_len = item->buflen;
	n = writev(item->tcpfd, iov, iovcnt);
	if (n != 2 + item->buflen) {
		addtag(item, "writev");
		freeitem(item);
	}
}

static void
connectdone(int fd) {
	struct workitem *item;
	socklen_t optlen;
	int cc;

	item = HEAD(connecting);
	while (item && item->tcpfd != fd)
		item = NEXT(item, clink);
	if (item == NULL)
		return;

1201 1202 1203
	optlen = sizeof(cc);
	if (getsockopt(item->tcpfd, SOL_SOCKET, SO_ERROR,
		       (void *)&cc, (void *)&optlen) < 0)
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		cc = errno;
	if (cc != 0) {
		if (cc == ECONNRESET)
			addtag(item, "reset");
		else if (cc == ECONNREFUSED)
			addtag(item, "connection-refused");
		else
			addtag(item, "failed");
		freeitem(item);
		return;
	}
	UNLINK(connecting, item, clink);
	FD_CLR(item->tcpfd, &wfds);
	whandlers[item->tcpfd] = NULL;
	startread(item);
}

static void
connecttoserver(struct workitem *item) {
	int fd, n, on = 1;

	fd = socket(item->summary->storage.ss_family,
		    SOCK_STREAM, IPPROTO_TCP);
	if (fd == -1) {
		addtag(item, "failed");
		freeitem(item);
		return;
	}
	n = ioctl(fd, FIONBIO, (void *)&on);
	if (n == -1) {
		close(fd);
		addtag(item, "failed");
		freeitem(item);
		return;
	}
	n = setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
	if (n == -1) {
		close(fd);
		addtag(item, "failed");
		freeitem(item);
		return;
	}
	n = connect(fd, (struct sockaddr *)&item->summary->storage,
		    item->summary->storage.ss_len);
	if (n == -1 && errno == EINPROGRESS) {
		if (!item->outstanding++)
			outstanding++;
		item->tcpfd = fd;
		whandlers[fd] = connectdone;
		FD_SET(fd, &wfds);
		if (fd > maxfd)
			maxfd = fd;
		gettimeofday(&item->when, NULL);
		item->when.tv_sec += 60;
		APPEND(connecting, item, clink);
		return;
	} 
	if (n == -1) {
		if (errno == ECONNRESET)
			addtag(item, "reset");
		else if (errno == ECONNREFUSED)
			addtag(item, "connection-refused");
		else
			addtag(item, "failed");
		close(fd);
		freeitem(item);
		return;
	}
	if (!item->outstanding++)
		outstanding++;
	item->tcpfd = fd;
	startread(item);
}

Mark Andrews's avatar
Mark Andrews committed
1278 1279
static void
readstdin(int fd) {
1280
	char line[4096];
Mark Andrews's avatar
Mark Andrews committed
1281 1282 1283 1284 1285
	char zone[1204];
	char ns[1204];
	char address[1204];
	int n;

1286 1287 1288 1289
	if (outstanding > maxoutstanding / 2)
		return;

	if (fgets(line, sizeof(line), stdin) == NULL) {
Mark Andrews's avatar
Mark Andrews committed
1290 1291 1292 1293 1294 1295 1296 1297
		eof = 1;
		FD_CLR(0, &rfds);
		for (fd = maxfd; fd >= 0; fd--) {
			if (FD_ISSET(fd, &rfds) || FD_ISSET(fd, &wfds)) {
				break;
			}
		}
		maxfd = fd;
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
		return;
	}
	n = sscanf(line, "%1024s%1024s%1024s", zone, ns, address);
	if (n == 3)
		check(zone, ns, address);
	if (n == 2) {
		struct summary *summarya, *summaryaaaa;
		summarya = lookupa(zone, ns);
		summaryaaaa = lookupaaaa(zone, ns);
		if (summarya && summaryaaaa) {
			summarya->xlink = summaryaaaa;
			summaryaaaa->xlink = summarya;
		}
Mark Andrews's avatar
Mark Andrews committed
1311
	}
1312 1313
	if (n == 1)
		lookupns(zone);
Mark Andrews's avatar
Mark Andrews committed
1314 1315 1316 1317 1318 1319 1320 1321
}

static void
udpread(int fd) {
	struct workitem *item;
	struct sockaddr_storage storage;
	socklen_t len = sizeof(storage);
	unsigned char buf[4096];
1322 1323 1324
	int n;
	unsigned int id, qr;

Mark Andrews's avatar
Mark Andrews committed
1325 1326
	n = recvfrom(fd, buf, sizeof(buf), 0,
		     (struct sockaddr *)&storage, &len);
1327 1328 1329 1330 1331 1332 1333
	if (n < 12)
		return;

	qr = (buf[2] & 0x80) != 0;
	if (!qr)
		return;

Mark Andrews's avatar
Mark Andrews committed
1334 1335 1336
	id = buf[0] << 8| buf[1];
	item = HEAD(ids[id]);
	while (item != NULL &&
1337
	       !storage_equal(&storage, &item->summary->storage))
Mark Andrews's avatar
Mark Andrews committed
1338 1339 1340 1341 1342 1343
		item = NEXT(item, idlink);

	/* Late response? */
	if (item == NULL)
		return;

1344
	process(item, buf, n);
Mark Andrews's avatar
Mark Andrews committed
1345 1346
}

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 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
static void
nextserver(struct workitem *item) {
	struct sockaddr_storage storage;
	int id, tries;
	
 again:
	if (++item->test > nservers) {
		addtag(item, "timeout");
		freeitem(item);
		return;
	}

	switch(servers[item->test].sin.sin_family) {
	case AF_INET:
		if (udp4 == -1)
			goto again;
		memcpy(&storage, &servers[item->test].sin,
		       sizeof(servers[item->test].sin));
		break;
	case AF_INET6:
		if (udp6 == -1)
			goto again;
		memcpy(&storage, &servers[0].sin6,
		       sizeof(servers[item->test].sin6));
		break;
	default:
		goto again;
	}

	/*
	 * Find a new query id if required.
	 */
	id = item->id;
	tries = 0;
	while (!checkid(&storage, id) && tries++ < 0xffff)
		id = (id + 1) & 0xffff;
	if (tries == 0xffff) 
		goto again;

	if (id != item->id) {
		UNLINK(ids[item->id], item, idlink);
		item->buf[0] = id >> 8;
		item->buf[1] = id & 0xff;
		item->id = id;
		APPEND(ids[item->id], item, idlink);
	}

	item->summary->storage = storage;
	item->sends = 0;
	resend(item);
}

Mark Andrews's avatar
Mark Andrews committed
1399
int
1400
main(int argc, char **argv) {
Mark Andrews's avatar
Mark Andrews committed
1401
	struct timeval now, to, *tpo = NULL;
1402
	struct workitem *item = NULL, *citem, *ritem;
Mark Andrews's avatar
Mark Andrews committed
1403 1404 1405 1406 1407 1408
	fd_set myrfds, mywfds;
	int n;
	int fd;
	int nfds = 0;
	int done = 0;

1409 1410 1411
	if (argc > 1)
		debug = 1;

Mark Andrews's avatar
Mark Andrews committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
	FD_ZERO(&rfds);
	FD_ZERO(&wfds);

	FD_SET(0, &rfds);
	maxfd = 0;
	rhandlers[0] = readstdin;

	udp4 = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (udp4 >= 0) {
		FD_SET(udp4, &rfds);
		if (udp4 > maxfd)
			maxfd = udp4;
		rhandlers[udp4] = udpread;
	}

	udp6 = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
	if (udp6 >= 0) {
		FD_SET(udp6, &rfds);
		if (udp6 > maxfd)
			maxfd = udp6;
		rhandlers[udp6] = udpread;
	}

1435 1436 1437 1438
	res_init();
	nservers = res_getservers(&_res, servers,
				  sizeof(servers)/sizeof(servers[0]));

Mark Andrews's avatar
Mark Andrews committed
1439 1440 1441 1442 1443
	do {
		FD_COPY(&rfds, &myrfds);
		FD_COPY(&wfds, &mywfds);
		nfds = maxfd + 1;
		if (item) {
1444
			to.tv_sec = item->when.tv_sec - now.tv_sec;
Mark Andrews's avatar
Mark Andrews committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
			to.tv_usec = item->when.tv_usec - now.tv_usec;
			if (to.tv_usec < 0) {
				to.tv_usec += 1000000;
				to.tv_sec -= 1;
			}
			if (to.tv_sec < 0) {
				to.tv_sec = 0;
				to.tv_usec = 0;
			}
			tpo = &to;
		} else
			tpo = NULL;
1457 1458
		if (outstanding > maxoutstanding/2)
			FD_CLR(0, &myrfds);
Mark Andrews's avatar
Mark Andrews committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
		n = select(nfds, &myrfds, &mywfds, NULL, tpo);
		if (n > 0) {
			for (fd = 0; fd <= maxfd; fd++) {
				if (FD_ISSET(fd, &myrfds) &&
				    rhandlers[fd] != NULL) 
					(*rhandlers[fd])(fd);
				if (FD_ISSET(fd, &mywfds) &&
				    whandlers[fd] != NULL) 
					(*whandlers[fd])(fd);
			}
		}
		item = HEAD(work);
1471 1472 1473
		ritem = HEAD(reading);
		citem = HEAD(connecting);
		if (item || citem || ritem)
Mark Andrews's avatar
Mark Andrews committed
1474 1475
			gettimeofday(&now, NULL);
		while (item) {
1476 1477
			if (item->when.tv_sec > now.tv_sec ||
			    (item->when.tv_sec == now.tv_sec &&
Mark Andrews's avatar
Mark Andrews committed
1478 1479 1480 1481
			     item->when.tv_usec > now.tv_usec))
				break;
			if (item->sends < 3) {
				resend(item);
1482 1483
			} else if (item->type) {
				nextserver(item);
Mark Andrews's avatar
Mark Andrews committed
1484
			} else {
1485 1486
				addtag(item, "timeout");
				freeitem(item);
Mark Andrews's avatar
Mark Andrews committed
1487 1488 1489
			}
			item = HEAD(work);
		}
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 1515 1516 1517 1518 1519 1520 1521 1522
		while (citem) {
			if (citem->when.tv_sec > now.tv_sec ||
			    (citem->when.tv_sec == now.tv_sec &&
			     citem->when.tv_usec > now.tv_usec))
				break;
			addtag(citem, "timeout");
			freeitem(citem);
			citem = HEAD(connecting);
		}
		if (item && citem) {
			if (citem->when.tv_sec < item->when.tv_sec ||
			    (citem->when.tv_sec == item->when.tv_sec &&
			     citem->when.tv_usec < item->when.tv_usec))
				item = citem;
		} else if (item == NULL)
			item = citem;
		while (ritem) {
			if (ritem->when.tv_sec > now.tv_sec ||
			    (ritem->when.tv_sec == now.tv_sec &&
			     ritem->when.tv_usec > now.tv_usec))
				break;
			addtag(ritem, "timeout");
			freeitem(ritem);
			ritem = HEAD(reading);
		}
		if (item && ritem) {
			if (ritem->when.tv_sec < item->when.tv_sec ||
			    (ritem->when.tv_sec == item->when.tv_sec &&
			     ritem->when.tv_usec < item->when.tv_usec))
				item = ritem;
		} else if (item == NULL)
			item = ritem;

Mark Andrews's avatar
Mark Andrews committed
1523 1524 1525 1526
		if (eof && item == NULL)
			done = 1;
	} while (!done);
}