print-domain.c revision 190207
10Sstevel@tonic-gate/*
20Sstevel@tonic-gate * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
30Sstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
40Sstevel@tonic-gate *
51617Sgovinda * Redistribution and use in source and binary forms, with or without
61617Sgovinda * modification, are permitted provided that: (1) source code distributions
70Sstevel@tonic-gate * retain the above copyright notice and this paragraph in its entirety, (2)
80Sstevel@tonic-gate * distributions including binary code include the above copyright notice and
90Sstevel@tonic-gate * this paragraph in its entirety in the documentation or other materials
100Sstevel@tonic-gate * provided with the distribution, and (3) all advertising materials mentioning
110Sstevel@tonic-gate * features or use of this software display the following acknowledgement:
120Sstevel@tonic-gate * ``This product includes software developed by the University of California,
130Sstevel@tonic-gate * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
140Sstevel@tonic-gate * the University nor the names of its contributors may be used to endorse
150Sstevel@tonic-gate * or promote products derived from this software without specific prior
160Sstevel@tonic-gate * written permission.
170Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
180Sstevel@tonic-gate * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
190Sstevel@tonic-gate * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
200Sstevel@tonic-gate *
210Sstevel@tonic-gate * $FreeBSD: head/contrib/tcpdump/print-domain.c 190207 2009-03-21 18:30:25Z rpaulo $
2212619Sandrew.rutz@sun.com */
230Sstevel@tonic-gate
240Sstevel@tonic-gate#ifndef lint
250Sstevel@tonic-gatestatic const char rcsid[] _U_ =
260Sstevel@tonic-gate    "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.97.2.1 2007-12-09 01:51:12 guy Exp $ (LBL)";
270Sstevel@tonic-gate#endif
280Sstevel@tonic-gate
290Sstevel@tonic-gate#ifdef HAVE_CONFIG_H
300Sstevel@tonic-gate#include "config.h"
310Sstevel@tonic-gate#endif
320Sstevel@tonic-gate
330Sstevel@tonic-gate#include <tcpdump-stdinc.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate#include "nameser.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate#include <stdio.h>
380Sstevel@tonic-gate#include <string.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate#include "interface.h"
410Sstevel@tonic-gate#include "addrtoname.h"
420Sstevel@tonic-gate#include "extract.h"                    /* must come after interface.h */
430Sstevel@tonic-gate
440Sstevel@tonic-gatestatic const char *ns_ops[] = {
4527Sjchu	"", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
4627Sjchu	" op8", " updataA", " updateD", " updateDA",
4727Sjchu	" updateM", " updateMA", " zoneInit", " zoneRef",
4827Sjchu};
4927Sjchu
5027Sjchustatic const char *ns_resp[] = {
5127Sjchu	"", " FormErr", " ServFail", " NXDomain",
5227Sjchu	" NotImp", " Refused", " YXDomain", " YXRRSet",
5327Sjchu	" NXRRSet", " NotAuth", " NotZone", " Resp11",
5427Sjchu	" Resp12", " Resp13", " Resp14", " NoChange",
5527Sjchu};
5627Sjchu
5727Sjchu/* skip over a domain name */
5827Sjchustatic const u_char *
5927Sjchuns_nskip(register const u_char *cp)
6027Sjchu{
6127Sjchu	register u_char i;
620Sstevel@tonic-gate
630Sstevel@tonic-gate	if (!TTEST2(*cp, 1))
641648Sjchu		return (NULL);
651648Sjchu	i = *cp++;
661648Sjchu	while (i) {
671648Sjchu		if ((i & INDIR_MASK) == INDIR_MASK)
681648Sjchu			return (cp + 1);
691648Sjchu		if ((i & INDIR_MASK) == EDNS0_MASK) {
701648Sjchu			int bitlen, bytelen;
712426Sschwartz
722426Sschwartz			if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
732426Sschwartz				return(NULL); /* unknown ELT */
742426Sschwartz			if (!TTEST2(*cp, 1))
752426Sschwartz				return (NULL);
762426Sschwartz			if ((bitlen = *cp++) == 0)
772426Sschwartz				bitlen = 256;
782426Sschwartz			bytelen = (bitlen + 7) / 8;
792426Sschwartz			cp += bytelen;
801648Sjchu		} else
811648Sjchu			cp += i;
821648Sjchu		if (!TTEST2(*cp, 1))
831648Sjchu			return (NULL);
841648Sjchu		i = *cp++;
851648Sjchu	}
861648Sjchu	return (cp);
871648Sjchu}
881648Sjchu
890Sstevel@tonic-gate/* print a <domain-name> */
902426Sschwartzstatic const u_char *
910Sstevel@tonic-gateblabel_print(const u_char *cp)
920Sstevel@tonic-gate{
930Sstevel@tonic-gate	int bitlen, slen, b;
940Sstevel@tonic-gate	const u_char *bitp, *lim;
951772Sjl139090	char tc;
964701Sgovinda
971772Sjl139090	if (!TTEST2(*cp, 1))
980Sstevel@tonic-gate		return(NULL);
991648Sjchu	if ((bitlen = *cp) == 0)
1000Sstevel@tonic-gate		bitlen = 256;
1010Sstevel@tonic-gate	slen = (bitlen + 3) / 4;
1020Sstevel@tonic-gate	lim = cp + 1 + slen;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate	/* print the bit string as a hex string */
1050Sstevel@tonic-gate	printf("\\[x");
1060Sstevel@tonic-gate	for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
1070Sstevel@tonic-gate		TCHECK(*bitp);
1083274Set142600		printf("%02x", *bitp);
10927Sjchu	}
11027Sjchu	if (b > 4) {
11127Sjchu		TCHECK(*bitp);
11227Sjchu		tc = *bitp++;
11312619Sandrew.rutz@sun.com		printf("%02x", tc & (0xff << (8 - b)));
11412619Sandrew.rutz@sun.com	} else if (b > 0) {
1152276Sschwartz		TCHECK(*bitp);
1162276Sschwartz		tc = *bitp++;
1172276Sschwartz		printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
1180Sstevel@tonic-gate	}
1190Sstevel@tonic-gate	printf("/%d]", bitlen);
1201648Sjchu	return lim;
1211648Sjchutrunc:
1223274Set142600	printf(".../%d]", bitlen);
1233274Set142600	return NULL;
1243274Set142600}
1253274Set142600
1260Sstevel@tonic-gatestatic int
1270Sstevel@tonic-gatelabellen(const u_char *cp)
1280Sstevel@tonic-gate{
1290Sstevel@tonic-gate	register u_int i;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate	if (!TTEST2(*cp, 1))
1320Sstevel@tonic-gate		return(-1);
1330Sstevel@tonic-gate	i = *cp;
1340Sstevel@tonic-gate	if ((i & INDIR_MASK) == EDNS0_MASK) {
1350Sstevel@tonic-gate		int bitlen, elt;
1360Sstevel@tonic-gate		if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
1370Sstevel@tonic-gate			printf("<ELT %d>", elt);
1380Sstevel@tonic-gate			return(-1);
1390Sstevel@tonic-gate		}
1400Sstevel@tonic-gate		if (!TTEST2(*(cp + 1), 1))
1410Sstevel@tonic-gate			return(-1);
1420Sstevel@tonic-gate		if ((bitlen = *(cp + 1)) == 0)
1430Sstevel@tonic-gate			bitlen = 256;
1440Sstevel@tonic-gate		return(((bitlen + 7) / 8) + 1);
1450Sstevel@tonic-gate	} else
1460Sstevel@tonic-gate		return(i);
1470Sstevel@tonic-gate}
1480Sstevel@tonic-gate
1490Sstevel@tonic-gatestatic const u_char *
1500Sstevel@tonic-gatens_nprint(register const u_char *cp, register const u_char *bp)
1510Sstevel@tonic-gate{
1520Sstevel@tonic-gate	register u_int i, l;
1530Sstevel@tonic-gate	register const u_char *rp = NULL;
1540Sstevel@tonic-gate	register int compress = 0;
1550Sstevel@tonic-gate	int chars_processed;
15610923SEvan.Yan@Sun.COM	int elt;
15710923SEvan.Yan@Sun.COM	int data_size = snapend - bp;
15810923SEvan.Yan@Sun.COM
15910923SEvan.Yan@Sun.COM	if ((l = labellen(cp)) == (u_int)-1)
16010923SEvan.Yan@Sun.COM		return(NULL);
16110923SEvan.Yan@Sun.COM	if (!TTEST2(*cp, 1))
16210923SEvan.Yan@Sun.COM		return(NULL);
16310923SEvan.Yan@Sun.COM	chars_processed = 1;
1640Sstevel@tonic-gate	if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
1650Sstevel@tonic-gate		compress = 0;
1661772Sjl139090		rp = cp + l;
1671772Sjl139090	}
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate	if (i != 0)
1700Sstevel@tonic-gate		while (i && cp < snapend) {
1710Sstevel@tonic-gate			if ((i & INDIR_MASK) == INDIR_MASK) {
1720Sstevel@tonic-gate				if (!compress) {
1730Sstevel@tonic-gate					rp = cp + 1;
1740Sstevel@tonic-gate					compress = 1;
1750Sstevel@tonic-gate				}
1760Sstevel@tonic-gate				if (!TTEST2(*cp, 1))
1771772Sjl139090					return(NULL);
1780Sstevel@tonic-gate				cp = bp + (((i << 8) | *cp) & 0x3fff);
1791772Sjl139090				if ((l = labellen(cp)) == (u_int)-1)
1801772Sjl139090					return(NULL);
1811772Sjl139090				if (!TTEST2(*cp, 1))
1821772Sjl139090					return(NULL);
1831772Sjl139090				i = *cp++;
1841772Sjl139090				chars_processed++;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate				/*
1870Sstevel@tonic-gate				 * If we've looked at every character in
1881772Sjl139090				 * the message, this pointer will make
1890Sstevel@tonic-gate				 * us look at some character again,
1901772Sjl139090				 * which means we're looping.
1914886Sdwoods				 */
1921772Sjl139090				if (chars_processed >= data_size) {
1931772Sjl139090					printf("<LOOP>");
1944886Sdwoods					return (NULL);
1951772Sjl139090				}
1968691SLida.Horn@Sun.COM				continue;
1978691SLida.Horn@Sun.COM			}
1981772Sjl139090			if ((i & INDIR_MASK) == EDNS0_MASK) {
1990Sstevel@tonic-gate				elt = (i & ~INDIR_MASK);
2000Sstevel@tonic-gate				switch(elt) {
2010Sstevel@tonic-gate				case EDNS0_ELT_BITLABEL:
2020Sstevel@tonic-gate					if (blabel_print(cp) == NULL)
2030Sstevel@tonic-gate						return (NULL);
2040Sstevel@tonic-gate					break;
2050Sstevel@tonic-gate				default:
2060Sstevel@tonic-gate					/* unknown ELT */
2071772Sjl139090					printf("<ELT %d>", elt);
2080Sstevel@tonic-gate					return(NULL);
2090Sstevel@tonic-gate				}
2100Sstevel@tonic-gate			} else {
2112091Sam139583				if (fn_printn(cp, l, snapend))
2122091Sam139583					return(NULL);
2130Sstevel@tonic-gate			}
2142091Sam139583
2152091Sam139583			cp += l;
2162091Sam139583			chars_processed += l;
2172091Sam139583			putchar('.');
2182091Sam139583			if ((l = labellen(cp)) == (u_int)-1)
2192091Sam139583				return(NULL);
2202091Sam139583			if (!TTEST2(*cp, 1))
2212091Sam139583				return(NULL);
2220Sstevel@tonic-gate			i = *cp++;
2230Sstevel@tonic-gate			chars_processed++;
2240Sstevel@tonic-gate			if (!compress)
2250Sstevel@tonic-gate				rp += l + 1;
2260Sstevel@tonic-gate		}
2270Sstevel@tonic-gate	else
2280Sstevel@tonic-gate		putchar('.');
2290Sstevel@tonic-gate	return (rp);
2300Sstevel@tonic-gate}
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate/* print a <character-string> */
2330Sstevel@tonic-gatestatic const u_char *
2340Sstevel@tonic-gatens_cprint(register const u_char *cp)
2350Sstevel@tonic-gate{
2360Sstevel@tonic-gate	register u_int i;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate	if (!TTEST2(*cp, 1))
2390Sstevel@tonic-gate		return (NULL);
2400Sstevel@tonic-gate	i = *cp++;
2410Sstevel@tonic-gate	if (fn_printn(cp, i, snapend))
2420Sstevel@tonic-gate		return (NULL);
2430Sstevel@tonic-gate	return (cp + i);
2440Sstevel@tonic-gate}
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate/* http://www.iana.org/assignments/dns-parameters */
2470Sstevel@tonic-gatestruct tok ns_type2str[] = {
2480Sstevel@tonic-gate	{ T_A,		"A" },			/* RFC 1035 */
2490Sstevel@tonic-gate	{ T_NS,		"NS" },			/* RFC 1035 */
2500Sstevel@tonic-gate	{ T_MD,		"MD" },			/* RFC 1035 */
2510Sstevel@tonic-gate	{ T_MF,		"MF" },			/* RFC 1035 */
2520Sstevel@tonic-gate	{ T_CNAME,	"CNAME" },		/* RFC 1035 */
2530Sstevel@tonic-gate	{ T_SOA,	"SOA" },		/* RFC 1035 */
2540Sstevel@tonic-gate	{ T_MB,		"MB" },			/* RFC 1035 */
2550Sstevel@tonic-gate	{ T_MG,		"MG" },			/* RFC 1035 */
2560Sstevel@tonic-gate	{ T_MR,		"MR" },			/* RFC 1035 */
2570Sstevel@tonic-gate	{ T_NULL,	"NULL" },		/* RFC 1035 */
2580Sstevel@tonic-gate	{ T_WKS,	"WKS" },		/* RFC 1035 */
2590Sstevel@tonic-gate	{ T_PTR,	"PTR" },		/* RFC 1035 */
2600Sstevel@tonic-gate	{ T_HINFO,	"HINFO" },		/* RFC 1035 */
2610Sstevel@tonic-gate	{ T_MINFO,	"MINFO" },		/* RFC 1035 */
2620Sstevel@tonic-gate	{ T_MX,		"MX" },			/* RFC 1035 */
2630Sstevel@tonic-gate	{ T_TXT,	"TXT" },		/* RFC 1035 */
2640Sstevel@tonic-gate	{ T_RP,		"RP" },			/* RFC 1183 */
2650Sstevel@tonic-gate	{ T_AFSDB,	"AFSDB" },		/* RFC 1183 */
2660Sstevel@tonic-gate	{ T_X25,	"X25" },		/* RFC 1183 */
2670Sstevel@tonic-gate	{ T_ISDN,	"ISDN" },		/* RFC 1183 */
2680Sstevel@tonic-gate	{ T_RT,		"RT" },			/* RFC 1183 */
2690Sstevel@tonic-gate	{ T_NSAP,	"NSAP" },		/* RFC 1706 */
2700Sstevel@tonic-gate	{ T_NSAP_PTR,	"NSAP_PTR" },
2710Sstevel@tonic-gate	{ T_SIG,	"SIG" },		/* RFC 2535 */
2720Sstevel@tonic-gate	{ T_KEY,	"KEY" },		/* RFC 2535 */
2730Sstevel@tonic-gate	{ T_PX,		"PX" },			/* RFC 2163 */
2740Sstevel@tonic-gate	{ T_GPOS,	"GPOS" },		/* RFC 1712 */
2750Sstevel@tonic-gate	{ T_AAAA,	"AAAA" },		/* RFC 1886 */
2760Sstevel@tonic-gate	{ T_LOC,	"LOC" },		/* RFC 1876 */
277118Sjchu	{ T_NXT,	"NXT" },		/* RFC 2535 */
278118Sjchu	{ T_EID,	"EID" },		/* Nimrod */
279118Sjchu	{ T_NIMLOC,	"NIMLOC" },		/* Nimrod */
280118Sjchu	{ T_SRV,	"SRV" },		/* RFC 2782 */
281118Sjchu	{ T_ATMA,	"ATMA" },		/* ATM Forum */
282118Sjchu	{ T_NAPTR,	"NAPTR" },		/* RFC 2168, RFC 2915 */
283118Sjchu	{ T_KX,		"KX" },			/* RFC 2230 */
2840Sstevel@tonic-gate	{ T_CERT,	"CERT" },		/* RFC 2538 */
2852426Sschwartz	{ T_A6,		"A6" },			/* RFC 2874 */
2862426Sschwartz	{ T_DNAME,	"DNAME" },		/* RFC 2672 */
2870Sstevel@tonic-gate	{ T_SINK, 	"SINK" },
2882426Sschwartz	{ T_OPT,	"OPT" },		/* RFC 2671 */
2890Sstevel@tonic-gate	{ T_APL, 	"APL" },		/* RFC 3123 */
2900Sstevel@tonic-gate	{ T_DS,		"DS" },			/* RFC 4034 */
2912426Sschwartz	{ T_SSHFP,	"SSHFP" },		/* RFC 4255 */
2920Sstevel@tonic-gate	{ T_IPSECKEY,	"IPSECKEY" },		/* RFC 4025 */
2931772Sjl139090	{ T_RRSIG, 	"RRSIG" },		/* RFC 4034 */
2940Sstevel@tonic-gate	{ T_NSEC,	"NSEC" },		/* RFC 4034 */
2952276Sschwartz	{ T_DNSKEY,	"DNSKEY" },		/* RFC 4034 */
2962276Sschwartz	{ T_SPF,	"SPF" },		/* RFC-schlitt-spf-classic-02.txt */
2972276Sschwartz	{ T_UINFO,	"UINFO" },
2982276Sschwartz	{ T_UID,	"UID" },
2992276Sschwartz	{ T_GID,	"GID" },
3000Sstevel@tonic-gate	{ T_UNSPEC,	"UNSPEC" },
3010Sstevel@tonic-gate	{ T_UNSPECA,	"UNSPECA" },
3020Sstevel@tonic-gate	{ T_TKEY,	"TKEY" },		/* RFC 2930 */
3030Sstevel@tonic-gate	{ T_TSIG,	"TSIG" },		/* RFC 2845 */
3040Sstevel@tonic-gate	{ T_IXFR,	"IXFR" },		/* RFC 1995 */
3050Sstevel@tonic-gate	{ T_AXFR,	"AXFR" },		/* RFC 1035 */
3060Sstevel@tonic-gate	{ T_MAILB,	"MAILB" },		/* RFC 1035 */
3070Sstevel@tonic-gate	{ T_MAILA,	"MAILA" },		/* RFC 1035 */
3080Sstevel@tonic-gate	{ T_ANY,	"ANY" },
3090Sstevel@tonic-gate	{ 0,		NULL }
3100Sstevel@tonic-gate};
3110Sstevel@tonic-gate
3120Sstevel@tonic-gatestruct tok ns_class2str[] = {
3130Sstevel@tonic-gate	{ C_IN,		"IN" },		/* Not used */
3140Sstevel@tonic-gate	{ C_CHAOS,	"CHAOS" },
3151772Sjl139090	{ C_HS,		"HS" },
3161772Sjl139090	{ C_ANY,	"ANY" },
3171772Sjl139090	{ 0,		NULL }
3181772Sjl139090};
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate/* print a query */
3211617Sgovindastatic const u_char *
3221617Sgovindans_qprint(register const u_char *cp, register const u_char *bp, int is_mdns)
3230Sstevel@tonic-gate{
3240Sstevel@tonic-gate	register const u_char *np = cp;
3250Sstevel@tonic-gate	register u_int i, class;
3261617Sgovinda
3271772Sjl139090	cp = ns_nskip(cp);
3281772Sjl139090
3291772Sjl139090	if (cp == NULL || !TTEST2(*cp, 4))
3301772Sjl139090		return(NULL);
33110923SEvan.Yan@Sun.COM
33212619Sandrew.rutz@sun.com	/* print the qtype */
33312619Sandrew.rutz@sun.com	i = EXTRACT_16BITS(cp);
3341772Sjl139090	cp += 2;
3350Sstevel@tonic-gate	printf(" %s", tok2str(ns_type2str, "Type%d", i));
3360Sstevel@tonic-gate	/* print the qclass (if it's not IN) */
3370Sstevel@tonic-gate	i = EXTRACT_16BITS(cp);
3380Sstevel@tonic-gate	cp += 2;
3390Sstevel@tonic-gate	if (is_mdns)
3400Sstevel@tonic-gate		class = (i & ~C_QU);
3410Sstevel@tonic-gate	else
3420Sstevel@tonic-gate		class = i;
3430Sstevel@tonic-gate	if (class != C_IN)
3440Sstevel@tonic-gate		printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
3450Sstevel@tonic-gate	if (is_mdns) {
3460Sstevel@tonic-gate		if (i & C_QU)
3470Sstevel@tonic-gate			printf(" (QU)");
3480Sstevel@tonic-gate		else
3490Sstevel@tonic-gate			printf(" (QM)");
3500Sstevel@tonic-gate	}
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate	fputs("? ", stdout);
3530Sstevel@tonic-gate	cp = ns_nprint(np, bp);
3540Sstevel@tonic-gate	return(cp ? cp + 4 : NULL);
3550Sstevel@tonic-gate}
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate/* print a reply */
3580Sstevel@tonic-gatestatic const u_char *
3590Sstevel@tonic-gatens_rprint(register const u_char *cp, register const u_char *bp, int is_mdns)
3600Sstevel@tonic-gate{
3610Sstevel@tonic-gate	register u_int i, class, opt_flags = 0;
3620Sstevel@tonic-gate	register u_short typ, len;
3630Sstevel@tonic-gate	register const u_char *rp;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate	if (vflag) {
3660Sstevel@tonic-gate		putchar(' ');
3670Sstevel@tonic-gate		if ((cp = ns_nprint(cp, bp)) == NULL)
3680Sstevel@tonic-gate			return NULL;
3690Sstevel@tonic-gate	} else
3700Sstevel@tonic-gate		cp = ns_nskip(cp);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate	if (cp == NULL || !TTEST2(*cp, 10))
3730Sstevel@tonic-gate		return (snapend);
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate	/* print the type/qtype */
3760Sstevel@tonic-gate	typ = EXTRACT_16BITS(cp);
3770Sstevel@tonic-gate	cp += 2;
3780Sstevel@tonic-gate	/* print the class (if it's not IN and the type isn't OPT) */
3790Sstevel@tonic-gate	i = EXTRACT_16BITS(cp);
3800Sstevel@tonic-gate	cp += 2;
3810Sstevel@tonic-gate	if (is_mdns)
3820Sstevel@tonic-gate		class = (i & ~C_CACHE_FLUSH);
3830Sstevel@tonic-gate	else
3840Sstevel@tonic-gate		class = i;
3850Sstevel@tonic-gate	if (class != C_IN && typ != T_OPT)
3860Sstevel@tonic-gate		printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
3870Sstevel@tonic-gate	if (is_mdns) {
3880Sstevel@tonic-gate		if (i & C_CACHE_FLUSH)
3890Sstevel@tonic-gate			printf(" (Cache flush)");
3900Sstevel@tonic-gate	}
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate	if (typ == T_OPT) {
3930Sstevel@tonic-gate		/* get opt flags */
3940Sstevel@tonic-gate		cp += 2;
395118Sjchu		opt_flags = EXTRACT_16BITS(cp);
396118Sjchu		/* ignore rest of ttl field */
397118Sjchu		cp += 2;
3980Sstevel@tonic-gate	} else if (vflag > 2) {
3993274Set142600		/* print ttl */
400624Sschwartz		printf(" [");
4011772Sjl139090		relts_print(EXTRACT_32BITS(cp));
4021772Sjl139090		printf("]");
4031772Sjl139090		cp += 4;
4041772Sjl139090	} else {
4051772Sjl139090		/* ignore ttl */
4061772Sjl139090		cp += 4;
4070Sstevel@tonic-gate	}
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate	len = EXTRACT_16BITS(cp);
4100Sstevel@tonic-gate	cp += 2;
4110Sstevel@tonic-gate
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
580void
581ns_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