xref: /freebsd/contrib/tcpdump/print-domain.c (revision 3157ba21)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  */
23 
24 #ifndef lint
25 static const char rcsid[] _U_ =
26     "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.97.2.1 2007-12-09 01:51:12 guy Exp $ (LBL)";
27 #endif
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <tcpdump-stdinc.h>
34 
35 #include "nameser.h"
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h"                    /* must come after interface.h */
43 
44 static const char *ns_ops[] = {
45 	"", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
46 	" op8", " updataA", " updateD", " updateDA",
47 	" updateM", " updateMA", " zoneInit", " zoneRef",
48 };
49 
50 static const char *ns_resp[] = {
51 	"", " FormErr", " ServFail", " NXDomain",
52 	" NotImp", " Refused", " YXDomain", " YXRRSet",
53 	" NXRRSet", " NotAuth", " NotZone", " Resp11",
54 	" Resp12", " Resp13", " Resp14", " NoChange",
55 };
56 
57 /* skip over a domain name */
58 static const u_char *
59 ns_nskip(register const u_char *cp)
60 {
61 	register u_char i;
62 
63 	if (!TTEST2(*cp, 1))
64 		return (NULL);
65 	i = *cp++;
66 	while (i) {
67 		if ((i & INDIR_MASK) == INDIR_MASK)
68 			return (cp + 1);
69 		if ((i & INDIR_MASK) == EDNS0_MASK) {
70 			int bitlen, bytelen;
71 
72 			if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
73 				return(NULL); /* unknown ELT */
74 			if (!TTEST2(*cp, 1))
75 				return (NULL);
76 			if ((bitlen = *cp++) == 0)
77 				bitlen = 256;
78 			bytelen = (bitlen + 7) / 8;
79 			cp += bytelen;
80 		} else
81 			cp += i;
82 		if (!TTEST2(*cp, 1))
83 			return (NULL);
84 		i = *cp++;
85 	}
86 	return (cp);
87 }
88 
89 /* print a <domain-name> */
90 static const u_char *
91 blabel_print(const u_char *cp)
92 {
93 	int bitlen, slen, b;
94 	const u_char *bitp, *lim;
95 	char tc;
96 
97 	if (!TTEST2(*cp, 1))
98 		return(NULL);
99 	if ((bitlen = *cp) == 0)
100 		bitlen = 256;
101 	slen = (bitlen + 3) / 4;
102 	lim = cp + 1 + slen;
103 
104 	/* print the bit string as a hex string */
105 	printf("\\[x");
106 	for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
107 		TCHECK(*bitp);
108 		printf("%02x", *bitp);
109 	}
110 	if (b > 4) {
111 		TCHECK(*bitp);
112 		tc = *bitp++;
113 		printf("%02x", tc & (0xff << (8 - b)));
114 	} else if (b > 0) {
115 		TCHECK(*bitp);
116 		tc = *bitp++;
117 		printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
118 	}
119 	printf("/%d]", bitlen);
120 	return lim;
121 trunc:
122 	printf(".../%d]", bitlen);
123 	return NULL;
124 }
125 
126 static int
127 labellen(const u_char *cp)
128 {
129 	register u_int i;
130 
131 	if (!TTEST2(*cp, 1))
132 		return(-1);
133 	i = *cp;
134 	if ((i & INDIR_MASK) == EDNS0_MASK) {
135 		int bitlen, elt;
136 		if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
137 			printf("<ELT %d>", elt);
138 			return(-1);
139 		}
140 		if (!TTEST2(*(cp + 1), 1))
141 			return(-1);
142 		if ((bitlen = *(cp + 1)) == 0)
143 			bitlen = 256;
144 		return(((bitlen + 7) / 8) + 1);
145 	} else
146 		return(i);
147 }
148 
149 static const u_char *
150 ns_nprint(register const u_char *cp, register const u_char *bp)
151 {
152 	register u_int i, l;
153 	register const u_char *rp = NULL;
154 	register int compress = 0;
155 	int chars_processed;
156 	int elt;
157 	int data_size = snapend - bp;
158 
159 	if ((l = labellen(cp)) == (u_int)-1)
160 		return(NULL);
161 	if (!TTEST2(*cp, 1))
162 		return(NULL);
163 	chars_processed = 1;
164 	if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
165 		compress = 0;
166 		rp = cp + l;
167 	}
168 
169 	if (i != 0)
170 		while (i && cp < snapend) {
171 			if ((i & INDIR_MASK) == INDIR_MASK) {
172 				if (!compress) {
173 					rp = cp + 1;
174 					compress = 1;
175 				}
176 				if (!TTEST2(*cp, 1))
177 					return(NULL);
178 				cp = bp + (((i << 8) | *cp) & 0x3fff);
179 				if ((l = labellen(cp)) == (u_int)-1)
180 					return(NULL);
181 				if (!TTEST2(*cp, 1))
182 					return(NULL);
183 				i = *cp++;
184 				chars_processed++;
185 
186 				/*
187 				 * If we've looked at every character in
188 				 * the message, this pointer will make
189 				 * us look at some character again,
190 				 * which means we're looping.
191 				 */
192 				if (chars_processed >= data_size) {
193 					printf("<LOOP>");
194 					return (NULL);
195 				}
196 				continue;
197 			}
198 			if ((i & INDIR_MASK) == EDNS0_MASK) {
199 				elt = (i & ~INDIR_MASK);
200 				switch(elt) {
201 				case EDNS0_ELT_BITLABEL:
202 					if (blabel_print(cp) == NULL)
203 						return (NULL);
204 					break;
205 				default:
206 					/* unknown ELT */
207 					printf("<ELT %d>", elt);
208 					return(NULL);
209 				}
210 			} else {
211 				if (fn_printn(cp, l, snapend))
212 					return(NULL);
213 			}
214 
215 			cp += l;
216 			chars_processed += l;
217 			putchar('.');
218 			if ((l = labellen(cp)) == (u_int)-1)
219 				return(NULL);
220 			if (!TTEST2(*cp, 1))
221 				return(NULL);
222 			i = *cp++;
223 			chars_processed++;
224 			if (!compress)
225 				rp += l + 1;
226 		}
227 	else
228 		putchar('.');
229 	return (rp);
230 }
231 
232 /* print a <character-string> */
233 static const u_char *
234 ns_cprint(register const u_char *cp)
235 {
236 	register u_int i;
237 
238 	if (!TTEST2(*cp, 1))
239 		return (NULL);
240 	i = *cp++;
241 	if (fn_printn(cp, i, snapend))
242 		return (NULL);
243 	return (cp + i);
244 }
245 
246 /* http://www.iana.org/assignments/dns-parameters */
247 struct tok ns_type2str[] = {
248 	{ T_A,		"A" },			/* RFC 1035 */
249 	{ T_NS,		"NS" },			/* RFC 1035 */
250 	{ T_MD,		"MD" },			/* RFC 1035 */
251 	{ T_MF,		"MF" },			/* RFC 1035 */
252 	{ T_CNAME,	"CNAME" },		/* RFC 1035 */
253 	{ T_SOA,	"SOA" },		/* RFC 1035 */
254 	{ T_MB,		"MB" },			/* RFC 1035 */
255 	{ T_MG,		"MG" },			/* RFC 1035 */
256 	{ T_MR,		"MR" },			/* RFC 1035 */
257 	{ T_NULL,	"NULL" },		/* RFC 1035 */
258 	{ T_WKS,	"WKS" },		/* RFC 1035 */
259 	{ T_PTR,	"PTR" },		/* RFC 1035 */
260 	{ T_HINFO,	"HINFO" },		/* RFC 1035 */
261 	{ T_MINFO,	"MINFO" },		/* RFC 1035 */
262 	{ T_MX,		"MX" },			/* RFC 1035 */
263 	{ T_TXT,	"TXT" },		/* RFC 1035 */
264 	{ T_RP,		"RP" },			/* RFC 1183 */
265 	{ T_AFSDB,	"AFSDB" },		/* RFC 1183 */
266 	{ T_X25,	"X25" },		/* RFC 1183 */
267 	{ T_ISDN,	"ISDN" },		/* RFC 1183 */
268 	{ T_RT,		"RT" },			/* RFC 1183 */
269 	{ T_NSAP,	"NSAP" },		/* RFC 1706 */
270 	{ T_NSAP_PTR,	"NSAP_PTR" },
271 	{ T_SIG,	"SIG" },		/* RFC 2535 */
272 	{ T_KEY,	"KEY" },		/* RFC 2535 */
273 	{ T_PX,		"PX" },			/* RFC 2163 */
274 	{ T_GPOS,	"GPOS" },		/* RFC 1712 */
275 	{ T_AAAA,	"AAAA" },		/* RFC 1886 */
276 	{ T_LOC,	"LOC" },		/* RFC 1876 */
277 	{ T_NXT,	"NXT" },		/* RFC 2535 */
278 	{ T_EID,	"EID" },		/* Nimrod */
279 	{ T_NIMLOC,	"NIMLOC" },		/* Nimrod */
280 	{ T_SRV,	"SRV" },		/* RFC 2782 */
281 	{ T_ATMA,	"ATMA" },		/* ATM Forum */
282 	{ T_NAPTR,	"NAPTR" },		/* RFC 2168, RFC 2915 */
283 	{ T_KX,		"KX" },			/* RFC 2230 */
284 	{ T_CERT,	"CERT" },		/* RFC 2538 */
285 	{ T_A6,		"A6" },			/* RFC 2874 */
286 	{ T_DNAME,	"DNAME" },		/* RFC 2672 */
287 	{ T_SINK, 	"SINK" },
288 	{ T_OPT,	"OPT" },		/* RFC 2671 */
289 	{ T_APL, 	"APL" },		/* RFC 3123 */
290 	{ T_DS,		"DS" },			/* RFC 4034 */
291 	{ T_SSHFP,	"SSHFP" },		/* RFC 4255 */
292 	{ T_IPSECKEY,	"IPSECKEY" },		/* RFC 4025 */
293 	{ T_RRSIG, 	"RRSIG" },		/* RFC 4034 */
294 	{ T_NSEC,	"NSEC" },		/* RFC 4034 */
295 	{ T_DNSKEY,	"DNSKEY" },		/* RFC 4034 */
296 	{ T_SPF,	"SPF" },		/* RFC-schlitt-spf-classic-02.txt */
297 	{ T_UINFO,	"UINFO" },
298 	{ T_UID,	"UID" },
299 	{ T_GID,	"GID" },
300 	{ T_UNSPEC,	"UNSPEC" },
301 	{ T_UNSPECA,	"UNSPECA" },
302 	{ T_TKEY,	"TKEY" },		/* RFC 2930 */
303 	{ T_TSIG,	"TSIG" },		/* RFC 2845 */
304 	{ T_IXFR,	"IXFR" },		/* RFC 1995 */
305 	{ T_AXFR,	"AXFR" },		/* RFC 1035 */
306 	{ T_MAILB,	"MAILB" },		/* RFC 1035 */
307 	{ T_MAILA,	"MAILA" },		/* RFC 1035 */
308 	{ T_ANY,	"ANY" },
309 	{ 0,		NULL }
310 };
311 
312 struct tok ns_class2str[] = {
313 	{ C_IN,		"IN" },		/* Not used */
314 	{ C_CHAOS,	"CHAOS" },
315 	{ C_HS,		"HS" },
316 	{ C_ANY,	"ANY" },
317 	{ 0,		NULL }
318 };
319 
320 /* print a query */
321 static const u_char *
322 ns_qprint(register const u_char *cp, register const u_char *bp, int is_mdns)
323 {
324 	register const u_char *np = cp;
325 	register u_int i, class;
326 
327 	cp = ns_nskip(cp);
328 
329 	if (cp == NULL || !TTEST2(*cp, 4))
330 		return(NULL);
331 
332 	/* print the qtype */
333 	i = EXTRACT_16BITS(cp);
334 	cp += 2;
335 	printf(" %s", tok2str(ns_type2str, "Type%d", i));
336 	/* print the qclass (if it's not IN) */
337 	i = EXTRACT_16BITS(cp);
338 	cp += 2;
339 	if (is_mdns)
340 		class = (i & ~C_QU);
341 	else
342 		class = i;
343 	if (class != C_IN)
344 		printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
345 	if (is_mdns) {
346 		if (i & C_QU)
347 			printf(" (QU)");
348 		else
349 			printf(" (QM)");
350 	}
351 
352 	fputs("? ", stdout);
353 	cp = ns_nprint(np, bp);
354 	return(cp ? cp + 4 : NULL);
355 }
356 
357 /* print a reply */
358 static const u_char *
359 ns_rprint(register const u_char *cp, register const u_char *bp, int is_mdns)
360 {
361 	register u_int i, class, opt_flags = 0;
362 	register u_short typ, len;
363 	register const u_char *rp;
364 
365 	if (vflag) {
366 		putchar(' ');
367 		if ((cp = ns_nprint(cp, bp)) == NULL)
368 			return NULL;
369 	} else
370 		cp = ns_nskip(cp);
371 
372 	if (cp == NULL || !TTEST2(*cp, 10))
373 		return (snapend);
374 
375 	/* print the type/qtype */
376 	typ = EXTRACT_16BITS(cp);
377 	cp += 2;
378 	/* print the class (if it's not IN and the type isn't OPT) */
379 	i = EXTRACT_16BITS(cp);
380 	cp += 2;
381 	if (is_mdns)
382 		class = (i & ~C_CACHE_FLUSH);
383 	else
384 		class = i;
385 	if (class != C_IN && typ != T_OPT)
386 		printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
387 	if (is_mdns) {
388 		if (i & C_CACHE_FLUSH)
389 			printf(" (Cache flush)");
390 	}
391 
392 	if (typ == T_OPT) {
393 		/* get opt flags */
394 		cp += 2;
395 		opt_flags = EXTRACT_16BITS(cp);
396 		/* ignore rest of ttl field */
397 		cp += 2;
398 	} else if (vflag > 2) {
399 		/* print ttl */
400 		printf(" [");
401 		relts_print(EXTRACT_32BITS(cp));
402 		printf("]");
403 		cp += 4;
404 	} else {
405 		/* ignore ttl */
406 		cp += 4;
407 	}
408 
409 	len = EXTRACT_16BITS(cp);
410 	cp += 2;
411 
412 	rp = cp + len;
413 
414 	printf(" %s", tok2str(ns_type2str, "Type%d", typ));
415 	if (rp > snapend)
416 		return(NULL);
417 
418 	switch (typ) {
419 	case T_A:
420 		if (!TTEST2(*cp, sizeof(struct in_addr)))
421 			return(NULL);
422 		printf(" %s", intoa(htonl(EXTRACT_32BITS(cp))));
423 		break;
424 
425 	case T_NS:
426 	case T_CNAME:
427 	case T_PTR:
428 #ifdef T_DNAME
429 	case T_DNAME:
430 #endif
431 		putchar(' ');
432 		if (ns_nprint(cp, bp) == NULL)
433 			return(NULL);
434 		break;
435 
436 	case T_SOA:
437 		if (!vflag)
438 			break;
439 		putchar(' ');
440 		if ((cp = ns_nprint(cp, bp)) == NULL)
441 			return(NULL);
442 		putchar(' ');
443 		if ((cp = ns_nprint(cp, bp)) == NULL)
444 			return(NULL);
445 		if (!TTEST2(*cp, 5 * 4))
446 			return(NULL);
447 		printf(" %u", EXTRACT_32BITS(cp));
448 		cp += 4;
449 		printf(" %u", EXTRACT_32BITS(cp));
450 		cp += 4;
451 		printf(" %u", EXTRACT_32BITS(cp));
452 		cp += 4;
453 		printf(" %u", EXTRACT_32BITS(cp));
454 		cp += 4;
455 		printf(" %u", EXTRACT_32BITS(cp));
456 		cp += 4;
457 		break;
458 	case T_MX:
459 		putchar(' ');
460 		if (!TTEST2(*cp, 2))
461 			return(NULL);
462 		if (ns_nprint(cp + 2, bp) == NULL)
463 			return(NULL);
464 		printf(" %d", EXTRACT_16BITS(cp));
465 		break;
466 
467 	case T_TXT:
468 		while (cp < rp) {
469 			printf(" \"");
470 			cp = ns_cprint(cp);
471 			if (cp == NULL)
472 				return(NULL);
473 			putchar('"');
474 		}
475 		break;
476 
477 	case T_SRV:
478 		putchar(' ');
479 		if (!TTEST2(*cp, 6))
480 			return(NULL);
481 		if (ns_nprint(cp + 6, bp) == NULL)
482 			return(NULL);
483 		printf(":%d %d %d", EXTRACT_16BITS(cp + 4),
484 			EXTRACT_16BITS(cp), EXTRACT_16BITS(cp + 2));
485 		break;
486 
487 #ifdef INET6
488 	case T_AAAA:
489 	    {
490 		struct in6_addr addr;
491 		char ntop_buf[INET6_ADDRSTRLEN];
492 
493 		if (!TTEST2(*cp, sizeof(struct in6_addr)))
494 			return(NULL);
495 		memcpy(&addr, cp, sizeof(struct in6_addr));
496 		printf(" %s",
497 		    inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf)));
498 
499 		break;
500 	    }
501 
502 	case T_A6:
503 	    {
504 		struct in6_addr a;
505 		int pbit, pbyte;
506 		char ntop_buf[INET6_ADDRSTRLEN];
507 
508 		if (!TTEST2(*cp, 1))
509 			return(NULL);
510 		pbit = *cp;
511 		pbyte = (pbit & ~7) / 8;
512 		if (pbit > 128) {
513 			printf(" %u(bad plen)", pbit);
514 			break;
515 		} else if (pbit < 128) {
516 			if (!TTEST2(*(cp + 1), sizeof(a) - pbyte))
517 				return(NULL);
518 			memset(&a, 0, sizeof(a));
519 			memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
520 			printf(" %u %s", pbit,
521 			    inet_ntop(AF_INET6, &a, ntop_buf, sizeof(ntop_buf)));
522 		}
523 		if (pbit > 0) {
524 			putchar(' ');
525 			if (ns_nprint(cp + 1 + sizeof(a) - pbyte, bp) == NULL)
526 				return(NULL);
527 		}
528 		break;
529 	    }
530 #endif /*INET6*/
531 
532 	case T_OPT:
533 		printf(" UDPsize=%u", class);
534 		if (opt_flags & 0x8000)
535 			printf(" OK");
536 		break;
537 
538 	case T_UNSPECA:		/* One long string */
539 		if (!TTEST2(*cp, len))
540 			return(NULL);
541 		if (fn_printn(cp, len, snapend))
542 			return(NULL);
543 		break;
544 
545 	case T_TSIG:
546 	    {
547 		if (cp + len > snapend)
548 			return(NULL);
549 		if (!vflag)
550 			break;
551 		putchar(' ');
552 		if ((cp = ns_nprint(cp, bp)) == NULL)
553 			return(NULL);
554 		cp += 6;
555 		if (!TTEST2(*cp, 2))
556 			return(NULL);
557 		printf(" fudge=%u", EXTRACT_16BITS(cp));
558 		cp += 2;
559 		if (!TTEST2(*cp, 2))
560 			return(NULL);
561 		printf(" maclen=%u", EXTRACT_16BITS(cp));
562 		cp += 2 + EXTRACT_16BITS(cp);
563 		if (!TTEST2(*cp, 2))
564 			return(NULL);
565 		printf(" origid=%u", EXTRACT_16BITS(cp));
566 		cp += 2;
567 		if (!TTEST2(*cp, 2))
568 			return(NULL);
569 		printf(" error=%u", EXTRACT_16BITS(cp));
570 		cp += 2;
571 		if (!TTEST2(*cp, 2))
572 			return(NULL);
573 		printf(" otherlen=%u", EXTRACT_16BITS(cp));
574 		cp += 2;
575 	    }
576 	}
577 	return (rp);		/* XXX This isn't always right */
578 }
579 
580 void
581 ns_print(register const u_char *bp, u_int length, int is_mdns)
582 {
583 	register const HEADER *np;
584 	register int qdcount, ancount, nscount, arcount;
585 	register const u_char *cp;
586 	u_int16_t b2;
587 
588 	np = (const HEADER *)bp;
589 	TCHECK(*np);
590 	/* get the byte-order right */
591 	qdcount = EXTRACT_16BITS(&np->qdcount);
592 	ancount = EXTRACT_16BITS(&np->ancount);
593 	nscount = EXTRACT_16BITS(&np->nscount);
594 	arcount = EXTRACT_16BITS(&np->arcount);
595 
596 	if (DNS_QR(np)) {
597 		/* this is a response */
598 		printf("%d%s%s%s%s%s%s",
599 			EXTRACT_16BITS(&np->id),
600 			ns_ops[DNS_OPCODE(np)],
601 			ns_resp[DNS_RCODE(np)],
602 			DNS_AA(np)? "*" : "",
603 			DNS_RA(np)? "" : "-",
604 			DNS_TC(np)? "|" : "",
605 			DNS_AD(np)? "$" : "");
606 
607 		if (qdcount != 1)
608 			printf(" [%dq]", qdcount);
609 		/* Print QUESTION section on -vv */
610 		cp = (const u_char *)(np + 1);
611 		while (qdcount--) {
612 			if (qdcount < EXTRACT_16BITS(&np->qdcount) - 1)
613 				putchar(',');
614 			if (vflag > 1) {
615 				fputs(" q:", stdout);
616 				if ((cp = ns_qprint(cp, bp, is_mdns)) == NULL)
617 					goto trunc;
618 			} else {
619 				if ((cp = ns_nskip(cp)) == NULL)
620 					goto trunc;
621 				cp += 4;	/* skip QTYPE and QCLASS */
622 			}
623 		}
624 		printf(" %d/%d/%d", ancount, nscount, arcount);
625 		if (ancount--) {
626 			if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
627 				goto trunc;
628 			while (cp < snapend && ancount--) {
629 				putchar(',');
630 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
631 					goto trunc;
632 			}
633 		}
634 		if (ancount > 0)
635 			goto trunc;
636 		/* Print NS and AR sections on -vv */
637 		if (vflag > 1) {
638 			if (cp < snapend && nscount--) {
639 				fputs(" ns:", stdout);
640 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
641 					goto trunc;
642 				while (cp < snapend && nscount--) {
643 					putchar(',');
644 					if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
645 						goto trunc;
646 				}
647 			}
648 			if (nscount > 0)
649 				goto trunc;
650 			if (cp < snapend && arcount--) {
651 				fputs(" ar:", stdout);
652 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
653 					goto trunc;
654 				while (cp < snapend && arcount--) {
655 					putchar(',');
656 					if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
657 						goto trunc;
658 				}
659 			}
660 			if (arcount > 0)
661 				goto trunc;
662 		}
663 	}
664 	else {
665 		/* this is a request */
666 		printf("%d%s%s%s", EXTRACT_16BITS(&np->id), ns_ops[DNS_OPCODE(np)],
667 		    DNS_RD(np) ? "+" : "",
668 		    DNS_CD(np) ? "%" : "");
669 
670 		/* any weirdness? */
671 		b2 = EXTRACT_16BITS(((u_short *)np)+1);
672 		if (b2 & 0x6cf)
673 			printf(" [b2&3=0x%x]", b2);
674 
675 		if (DNS_OPCODE(np) == IQUERY) {
676 			if (qdcount)
677 				printf(" [%dq]", qdcount);
678 			if (ancount != 1)
679 				printf(" [%da]", ancount);
680 		}
681 		else {
682 			if (ancount)
683 				printf(" [%da]", ancount);
684 			if (qdcount != 1)
685 				printf(" [%dq]", qdcount);
686 		}
687 		if (nscount)
688 			printf(" [%dn]", nscount);
689 		if (arcount)
690 			printf(" [%dau]", arcount);
691 
692 		cp = (const u_char *)(np + 1);
693 		if (qdcount--) {
694 			cp = ns_qprint(cp, (const u_char *)np, is_mdns);
695 			if (!cp)
696 				goto trunc;
697 			while (cp < snapend && qdcount--) {
698 				cp = ns_qprint((const u_char *)cp,
699 					       (const u_char *)np,
700 					       is_mdns);
701 				if (!cp)
702 					goto trunc;
703 			}
704 		}
705 		if (qdcount > 0)
706 			goto trunc;
707 
708 		/* Print remaining sections on -vv */
709 		if (vflag > 1) {
710 			if (ancount--) {
711 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
712 					goto trunc;
713 				while (cp < snapend && ancount--) {
714 					putchar(',');
715 					if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
716 						goto trunc;
717 				}
718 			}
719 			if (ancount > 0)
720 				goto trunc;
721 			if (cp < snapend && nscount--) {
722 				fputs(" ns:", stdout);
723 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
724 					goto trunc;
725 				while (nscount-- && cp < snapend) {
726 					putchar(',');
727 					if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
728 						goto trunc;
729 				}
730 			}
731 			if (nscount > 0)
732 				goto trunc;
733 			if (cp < snapend && arcount--) {
734 				fputs(" ar:", stdout);
735 				if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
736 					goto trunc;
737 				while (cp < snapend && arcount--) {
738 					putchar(',');
739 					if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
740 						goto trunc;
741 				}
742 			}
743 			if (arcount > 0)
744 				goto trunc;
745 		}
746 	}
747 	printf(" (%d)", length);
748 	return;
749 
750   trunc:
751 	printf("[|domain]");
752 	return;
753 }
754