2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.78 2001/10/19 09:00:48 guy Exp $ (LBL)";
33 #include <sys/param.h>
36 #include <netinet/in.h>
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h" /* must come after interface.h */
47 static const char *ns_ops[] = {
48 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
49 " op8", " updataA", " updateD", " updateDA",
50 " updateM", " updateMA", " zoneInit", " zoneRef",
53 static const char *ns_resp[] = {
54 "", " FormErr", " ServFail", " NXDomain",
55 " NotImp", " Refused", " YXDomain", " YXRRSet",
56 " NXRRSet", " NotAuth", " NotZone", " Resp11",
57 " Resp12", " Resp13", " Resp14", " NoChange",
60 /* skip over a domain name */
62 ns_nskip(register const u_char *cp, register const u_char *bp)
68 if (((i = *cp++) & INDIR_MASK) == INDIR_MASK)
71 if ((i & INDIR_MASK) == EDNS0_MASK) {
74 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
75 return(NULL); /* unknown ELT */
78 if ((bitlen = *cp++) == 0)
80 bytelen = (bitlen + 7) / 8;
91 /* print a <domain-name> */
93 blabel_print(const u_char *cp)
97 const u_char *bitp, *lim;
102 if ((bitlen = *cp) == 0)
104 slen = (bitlen + 3) / 4;
105 if ((lim = cp + 1 + slen) > snapend) {
110 /* print the bit string as a hex string */
112 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++)
113 printf("%02x", *bitp);
118 printf("%02x", tc & (0xff << (8 - b)));
121 printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
123 printf("/%d]", bitlen);
125 return(truncated ? NULL : lim);
129 labellen(const u_char *cp)
136 if ((i & INDIR_MASK) == EDNS0_MASK) {
139 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL)
141 if (!TTEST2(*(cp + 1), 1))
143 if ((bitlen = *(cp + 1)) == 0)
145 return(((bitlen + 7) / 8) + 1);
150 static const u_char *
151 ns_nprint(register const u_char *cp, register const u_char *bp)
154 register const u_char *rp = NULL;
155 register int compress = 0;
158 int data_size = snapend - bp;
160 if ((l = labellen(cp)) < 0)
165 if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
171 while (i && cp < snapend) {
172 if ((i & INDIR_MASK) == INDIR_MASK) {
179 cp = bp + (((i << 8) | *cp) & 0x3fff);
180 if ((l = labellen(cp)) < 0)
188 * If we've looked at every character in
189 * the message, this pointer will make
190 * us look at some character again,
191 * which means we're looping.
193 if (chars_processed >= data_size) {
199 if ((i & INDIR_MASK) == EDNS0_MASK) {
200 elt = (i & ~INDIR_MASK);
202 case EDNS0_ELT_BITLABEL:
203 if (blabel_print(cp) == NULL)
208 printf("<ELT %d>", elt);
212 if (fn_printn(cp, l, snapend))
217 chars_processed += l;
219 if ((l = labellen(cp)) < 0)
233 /* print a <character-string> */
234 static const u_char *
235 ns_cprint(register const u_char *cp, register const u_char *bp)
242 if (fn_printn(cp, i, snapend))
247 struct tok ns_type2str[] = {
252 { T_CNAME, "CNAME" },
260 { T_HINFO, "HINFO" },
261 { T_MINFO, "MINFO" },
265 { T_AFSDB, "AFSDB" },
270 { T_NSAP_PTR, "NSAP_PTR" },
279 { T_NIMLOC, "NIMLOC" },
282 { T_NAPTR, "NAPTR" },
284 { T_DNAME, "DNAME" },
286 { T_UINFO, "UINFO" },
289 { T_UNSPEC, "UNSPEC" },
290 { T_UNSPECA, "UNSPECA" },
295 { T_MAILB, "MAILB" },
296 { T_MAILA, "MAILA" },
301 struct tok ns_class2str[] = {
302 { C_IN, "IN" }, /* Not used */
303 { C_CHAOS, "CHAOS" },
310 static const u_char *
311 ns_qprint(register const u_char *cp, register const u_char *bp)
313 register const u_char *np = cp;
316 cp = ns_nskip(cp, bp);
318 if (cp == NULL || !TTEST2(*cp, 4))
321 /* print the qtype and qclass (if it's not IN) */
324 printf(" %s", tok2str(ns_type2str, "Type%d", i));
328 printf(" %s", tok2str(ns_class2str, "(Class %d)", i));
331 cp = ns_nprint(np, bp);
332 return(cp ? cp + 4 : NULL);
336 static const u_char *
337 ns_rprint(register const u_char *cp, register const u_char *bp)
339 register u_int class;
340 register u_short typ, len;
341 register const u_char *rp;
345 if ((cp = ns_nprint(cp, bp)) == NULL)
348 cp = ns_nskip(cp, bp);
350 if (cp == NULL || !TTEST2(*cp, 10))
353 /* print the type/qtype and class (if it's not IN) */
358 if (class != C_IN && typ != T_OPT)
359 printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
369 printf(" %s", tok2str(ns_type2str, "Type%d", typ));
375 if (!TTEST2(*cp, sizeof(struct in_addr)))
377 printf(" %s", ipaddr_string(cp));
387 if (ns_nprint(cp, bp) == NULL)
395 if ((cp = ns_nprint(cp, bp)) == NULL)
398 if ((cp = ns_nprint(cp, bp)) == NULL)
400 if (!TTEST2(*cp, 5 * 4))
402 printf(" %u", EXTRACT_32BITS(cp));
404 printf(" %u", EXTRACT_32BITS(cp));
406 printf(" %u", EXTRACT_32BITS(cp));
408 printf(" %u", EXTRACT_32BITS(cp));
410 printf(" %u", EXTRACT_32BITS(cp));
417 if (ns_nprint(cp + 2, bp) == NULL)
419 printf(" %d", EXTRACT_16BITS(cp));
424 (void)ns_cprint(cp, bp);
429 if (!TTEST2(*cp, sizeof(struct in6_addr)))
431 printf(" %s", ip6addr_string(cp));
442 pbyte = (pbit & ~7) / 8;
444 printf(" %u(bad plen)", pbit);
446 } else if (pbit < 128) {
447 if (!TTEST2(*(cp + 1), sizeof(a) - pbyte))
449 memset(&a, 0, sizeof(a));
450 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
451 printf(" %u %s", pbit, ip6addr_string(&a));
455 if (ns_nprint(cp + 1 + sizeof(a) - pbyte, bp) == NULL)
463 printf(" UDPsize=%u", class);
466 case T_UNSPECA: /* One long string */
467 if (!TTEST2(*cp, len))
469 if (fn_printn(cp, len, snapend))
475 if (cp + len > snapend)
480 if ((cp = ns_nprint(cp, bp)) == NULL)
485 printf(" fudge=%u", EXTRACT_16BITS(cp));
489 printf(" maclen=%u", EXTRACT_16BITS(cp));
490 cp += 2 + EXTRACT_16BITS(cp);
493 printf(" origid=%u", EXTRACT_16BITS(cp));
497 printf(" error=%u", EXTRACT_16BITS(cp));
501 printf(" otherlen=%u", EXTRACT_16BITS(cp));
505 return (rp); /* XXX This isn't always right */
509 ns_print(register const u_char *bp, u_int length)
511 register const HEADER *np;
512 register int qdcount, ancount, nscount, arcount;
513 register const u_char *cp;
515 np = (const HEADER *)bp;
517 /* get the byte-order right */
518 qdcount = ntohs(np->qdcount);
519 ancount = ntohs(np->ancount);
520 nscount = ntohs(np->nscount);
521 arcount = ntohs(np->arcount);
524 /* this is a response */
525 printf(" %d%s%s%s%s%s%s",
527 ns_ops[DNS_OPCODE(np)],
528 ns_resp[DNS_RCODE(np)],
529 DNS_AA(np)? "*" : "",
530 DNS_RA(np)? "" : "-",
531 DNS_TC(np)? "|" : "",
532 DNS_CD(np)? "%" : "");
535 printf(" [%dq]", qdcount);
536 /* Print QUESTION section on -vv */
537 cp = (const u_char *)(np + 1);
539 if (qdcount < ntohs(np->qdcount) - 1)
542 fputs(" q:", stdout);
543 if ((cp = ns_qprint(cp, bp)) == NULL)
546 if ((cp = ns_nskip(cp, bp)) == NULL)
548 cp += 4; /* skip QTYPE and QCLASS */
551 printf(" %d/%d/%d", ancount, nscount, arcount);
553 if ((cp = ns_rprint(cp, bp)) == NULL)
555 while (cp < snapend && ancount--) {
557 if ((cp = ns_rprint(cp, bp)) == NULL)
563 /* Print NS and AR sections on -vv */
565 if (cp < snapend && nscount--) {
566 fputs(" ns:", stdout);
567 if ((cp = ns_rprint(cp, bp)) == NULL)
569 while (cp < snapend && nscount--) {
571 if ((cp = ns_rprint(cp, bp)) == NULL)
577 if (cp < snapend && arcount--) {
578 fputs(" ar:", stdout);
579 if ((cp = ns_rprint(cp, bp)) == NULL)
581 while (cp < snapend && arcount--) {
583 if ((cp = ns_rprint(cp, bp)) == NULL)
592 /* this is a request */
593 printf(" %d%s%s%s", ntohs(np->id), ns_ops[DNS_OPCODE(np)],
594 DNS_RD(np) ? "+" : "",
595 DNS_AD(np) ? "$" : "");
598 if (*(((u_short *)np)+1) & htons(0x6cf))
599 printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1)));
601 if (DNS_OPCODE(np) == IQUERY) {
603 printf(" [%dq]", qdcount);
605 printf(" [%da]", ancount);
609 printf(" [%da]", ancount);
611 printf(" [%dq]", qdcount);
614 printf(" [%dn]", nscount);
616 printf(" [%dau]", arcount);
618 cp = (const u_char *)(np + 1);
620 cp = ns_qprint(cp, (const u_char *)np);
623 while (cp < snapend && qdcount--) {
624 cp = ns_qprint((const u_char *)cp,
633 /* Print remaining sections on -vv */
636 if ((cp = ns_rprint(cp, bp)) == NULL)
638 while (cp < snapend && ancount--) {
640 if ((cp = ns_rprint(cp, bp)) == NULL)
646 if (cp < snapend && nscount--) {
647 fputs(" ns:", stdout);
648 if ((cp = ns_rprint(cp, bp)) == NULL)
650 while (nscount-- && cp < snapend) {
652 if ((cp = ns_rprint(cp, bp)) == NULL)
658 if (cp < snapend && arcount--) {
659 fputs(" ar:", stdout);
660 if ((cp = ns_rprint(cp, bp)) == NULL)
662 while (cp < snapend && arcount--) {
664 if ((cp = ns_rprint(cp, bp)) == NULL)
672 printf(" (%d)", length);