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[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.89 2004/03/23 19:03:03 fenner Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h" /* must come after interface.h */
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",
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",
57 /* skip over a domain name */
59 ns_nskip(register const u_char *cp)
67 if ((i & INDIR_MASK) == INDIR_MASK)
69 if ((i & INDIR_MASK) == EDNS0_MASK) {
72 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
73 return(NULL); /* unknown ELT */
76 if ((bitlen = *cp++) == 0)
78 bytelen = (bitlen + 7) / 8;
89 /* print a <domain-name> */
91 blabel_print(const u_char *cp)
94 const u_char *bitp, *lim;
99 if ((bitlen = *cp) == 0)
101 slen = (bitlen + 3) / 4;
104 /* print the bit string as a hex string */
106 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
108 printf("%02x", *bitp);
113 printf("%02x", tc & (0xff << (8 - b)));
117 printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
119 printf("/%d]", bitlen);
122 printf(".../%d]", bitlen);
127 labellen(const u_char *cp)
134 if ((i & INDIR_MASK) == EDNS0_MASK) {
137 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL)
139 if (!TTEST2(*(cp + 1), 1))
141 if ((bitlen = *(cp + 1)) == 0)
143 return(((bitlen + 7) / 8) + 1);
148 static const u_char *
149 ns_nprint(register const u_char *cp, register const u_char *bp)
152 register const u_char *rp = NULL;
153 register int compress = 0;
156 int data_size = snapend - bp;
158 if ((l = labellen(cp)) == (u_int)-1)
163 if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
169 while (i && cp < snapend) {
170 if ((i & INDIR_MASK) == INDIR_MASK) {
177 cp = bp + (((i << 8) | *cp) & 0x3fff);
178 if ((l = labellen(cp)) == (u_int)-1)
186 * If we've looked at every character in
187 * the message, this pointer will make
188 * us look at some character again,
189 * which means we're looping.
191 if (chars_processed >= data_size) {
197 if ((i & INDIR_MASK) == EDNS0_MASK) {
198 elt = (i & ~INDIR_MASK);
200 case EDNS0_ELT_BITLABEL:
201 if (blabel_print(cp) == NULL)
206 printf("<ELT %d>", elt);
210 if (fn_printn(cp, l, snapend))
215 chars_processed += l;
217 if ((l = labellen(cp)) == (u_int)-1)
231 /* print a <character-string> */
232 static const u_char *
233 ns_cprint(register const u_char *cp)
240 if (fn_printn(cp, i, snapend))
245 /* http://www.iana.org/assignments/dns-parameters */
246 struct tok ns_type2str[] = {
247 { T_A, "A" }, /* RFC 1035 */
248 { T_NS, "NS" }, /* RFC 1035 */
249 { T_MD, "MD" }, /* RFC 1035 */
250 { T_MF, "MF" }, /* RFC 1035 */
251 { T_CNAME, "CNAME" }, /* RFC 1035 */
252 { T_SOA, "SOA" }, /* RFC 1035 */
253 { T_MB, "MB" }, /* RFC 1035 */
254 { T_MG, "MG" }, /* RFC 1035 */
255 { T_MR, "MR" }, /* RFC 1035 */
256 { T_NULL, "NULL" }, /* RFC 1035 */
257 { T_WKS, "WKS" }, /* RFC 1035 */
258 { T_PTR, "PTR" }, /* RFC 1035 */
259 { T_HINFO, "HINFO" }, /* RFC 1035 */
260 { T_MINFO, "MINFO" }, /* RFC 1035 */
261 { T_MX, "MX" }, /* RFC 1035 */
262 { T_TXT, "TXT" }, /* RFC 1035 */
263 { T_RP, "RP" }, /* RFC 1183 */
264 { T_AFSDB, "AFSDB" }, /* RFC 1183 */
265 { T_X25, "X25" }, /* RFC 1183 */
266 { T_ISDN, "ISDN" }, /* RFC 1183 */
267 { T_RT, "RT" }, /* RFC 1183 */
268 { T_NSAP, "NSAP" }, /* RFC 1706 */
269 { T_NSAP_PTR, "NSAP_PTR" },
270 { T_SIG, "SIG" }, /* RFC 2535 */
271 { T_KEY, "KEY" }, /* RFC 2535 */
272 { T_PX, "PX" }, /* RFC 2163 */
273 { T_GPOS, "GPOS" }, /* RFC 1712 */
274 { T_AAAA, "AAAA" }, /* RFC 1886 */
275 { T_LOC, "LOC" }, /* RFC 1876 */
276 { T_NXT, "NXT" }, /* RFC 2535 */
277 { T_EID, "EID" }, /* Nimrod */
278 { T_NIMLOC, "NIMLOC" }, /* Nimrod */
279 { T_SRV, "SRV" }, /* RFC 2782 */
280 { T_ATMA, "ATMA" }, /* ATM Forum */
281 { T_NAPTR, "NAPTR" }, /* RFC 2168, RFC 2915 */
282 { T_A6, "A6" }, /* RFC 2874 */
283 { T_DNAME, "DNAME" }, /* RFC 2672 */
284 { T_OPT, "OPT" }, /* RFC 2671 */
285 { T_UINFO, "UINFO" },
288 { T_UNSPEC, "UNSPEC" },
289 { T_UNSPECA, "UNSPECA" },
290 { T_TKEY, "TKEY" }, /* RFC 2930 */
291 { T_TSIG, "TSIG" }, /* RFC 2845 */
292 { T_IXFR, "IXFR" }, /* RFC 1995 */
293 { T_AXFR, "AXFR" }, /* RFC 1035 */
294 { T_MAILB, "MAILB" }, /* RFC 1035 */
295 { T_MAILA, "MAILA" }, /* RFC 1035 */
300 struct tok ns_class2str[] = {
301 { C_IN, "IN" }, /* Not used */
302 { C_CHAOS, "CHAOS" },
309 static const u_char *
310 ns_qprint(register const u_char *cp, register const u_char *bp, int is_mdns)
312 register const u_char *np = cp;
317 if (cp == NULL || !TTEST2(*cp, 4))
320 /* print the qtype and qclass (if it's not IN) */
321 i = EXTRACT_16BITS(cp);
323 printf(" %s", tok2str(ns_type2str, "Type%d", i));
324 i = EXTRACT_16BITS(cp);
326 if (is_mdns && i == (C_IN|C_CACHE_FLUSH))
327 printf(" (Cache flush)");
329 printf(" %s", tok2str(ns_class2str, "(Class %d)", i));
332 cp = ns_nprint(np, bp);
333 return(cp ? cp + 4 : NULL);
337 static const u_char *
338 ns_rprint(register const u_char *cp, register const u_char *bp, int is_mdns)
340 register u_int class;
341 register u_short typ, len;
342 register const u_char *rp;
346 if ((cp = ns_nprint(cp, bp)) == NULL)
351 if (cp == NULL || !TTEST2(*cp, 10))
354 /* print the type/qtype and class (if it's not IN) */
355 typ = EXTRACT_16BITS(cp);
357 class = EXTRACT_16BITS(cp);
359 if (is_mdns && class == (C_IN|C_CACHE_FLUSH))
360 printf(" (Cache flush)");
361 else if (class != C_IN && typ != T_OPT)
362 printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
367 len = EXTRACT_16BITS(cp);
372 printf(" %s", tok2str(ns_type2str, "Type%d", typ));
378 if (!TTEST2(*cp, sizeof(struct in_addr)))
380 printf(" %s", ipaddr_string(cp));
390 if (ns_nprint(cp, bp) == NULL)
398 if ((cp = ns_nprint(cp, bp)) == NULL)
401 if ((cp = ns_nprint(cp, bp)) == NULL)
403 if (!TTEST2(*cp, 5 * 4))
405 printf(" %u", EXTRACT_32BITS(cp));
407 printf(" %u", EXTRACT_32BITS(cp));
409 printf(" %u", EXTRACT_32BITS(cp));
411 printf(" %u", EXTRACT_32BITS(cp));
413 printf(" %u", EXTRACT_32BITS(cp));
420 if (ns_nprint(cp + 2, bp) == NULL)
422 printf(" %d", EXTRACT_16BITS(cp));
439 if (ns_nprint(cp + 6, bp) == NULL)
441 printf(":%d %d %d", EXTRACT_16BITS(cp + 4),
442 EXTRACT_16BITS(cp), EXTRACT_16BITS(cp + 2));
447 if (!TTEST2(*cp, sizeof(struct in6_addr)))
449 printf(" %s", ip6addr_string(cp));
460 pbyte = (pbit & ~7) / 8;
462 printf(" %u(bad plen)", pbit);
464 } else if (pbit < 128) {
465 if (!TTEST2(*(cp + 1), sizeof(a) - pbyte))
467 memset(&a, 0, sizeof(a));
468 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
469 printf(" %u %s", pbit, ip6addr_string(&a));
473 if (ns_nprint(cp + 1 + sizeof(a) - pbyte, bp) == NULL)
481 printf(" UDPsize=%u", class);
484 case T_UNSPECA: /* One long string */
485 if (!TTEST2(*cp, len))
487 if (fn_printn(cp, len, snapend))
493 if (cp + len > snapend)
498 if ((cp = ns_nprint(cp, bp)) == NULL)
503 printf(" fudge=%u", EXTRACT_16BITS(cp));
507 printf(" maclen=%u", EXTRACT_16BITS(cp));
508 cp += 2 + EXTRACT_16BITS(cp);
511 printf(" origid=%u", EXTRACT_16BITS(cp));
515 printf(" error=%u", EXTRACT_16BITS(cp));
519 printf(" otherlen=%u", EXTRACT_16BITS(cp));
523 return (rp); /* XXX This isn't always right */
527 ns_print(register const u_char *bp, u_int length, int is_mdns)
529 register const HEADER *np;
530 register int qdcount, ancount, nscount, arcount;
531 register const u_char *cp;
534 np = (const HEADER *)bp;
536 /* get the byte-order right */
537 qdcount = EXTRACT_16BITS(&np->qdcount);
538 ancount = EXTRACT_16BITS(&np->ancount);
539 nscount = EXTRACT_16BITS(&np->nscount);
540 arcount = EXTRACT_16BITS(&np->arcount);
543 /* this is a response */
544 printf(" %d%s%s%s%s%s%s",
545 EXTRACT_16BITS(&np->id),
546 ns_ops[DNS_OPCODE(np)],
547 ns_resp[DNS_RCODE(np)],
548 DNS_AA(np)? "*" : "",
549 DNS_RA(np)? "" : "-",
550 DNS_TC(np)? "|" : "",
551 DNS_AD(np)? "$" : "");
554 printf(" [%dq]", qdcount);
555 /* Print QUESTION section on -vv */
556 cp = (const u_char *)(np + 1);
558 if (qdcount < EXTRACT_16BITS(&np->qdcount) - 1)
561 fputs(" q:", stdout);
562 if ((cp = ns_qprint(cp, bp, is_mdns)) == NULL)
565 if ((cp = ns_nskip(cp)) == NULL)
567 cp += 4; /* skip QTYPE and QCLASS */
570 printf(" %d/%d/%d", ancount, nscount, arcount);
572 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
574 while (cp < snapend && ancount--) {
576 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
582 /* Print NS and AR sections on -vv */
584 if (cp < snapend && nscount--) {
585 fputs(" ns:", stdout);
586 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
588 while (cp < snapend && nscount--) {
590 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
596 if (cp < snapend && arcount--) {
597 fputs(" ar:", stdout);
598 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
600 while (cp < snapend && arcount--) {
602 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
611 /* this is a request */
612 printf(" %d%s%s%s", EXTRACT_16BITS(&np->id), ns_ops[DNS_OPCODE(np)],
613 DNS_RD(np) ? "+" : "",
614 DNS_CD(np) ? "%" : "");
617 b2 = EXTRACT_16BITS(((u_short *)np)+1);
619 printf(" [b2&3=0x%x]", b2);
621 if (DNS_OPCODE(np) == IQUERY) {
623 printf(" [%dq]", qdcount);
625 printf(" [%da]", ancount);
629 printf(" [%da]", ancount);
631 printf(" [%dq]", qdcount);
634 printf(" [%dn]", nscount);
636 printf(" [%dau]", arcount);
638 cp = (const u_char *)(np + 1);
640 cp = ns_qprint(cp, (const u_char *)np, is_mdns);
643 while (cp < snapend && qdcount--) {
644 cp = ns_qprint((const u_char *)cp,
654 /* Print remaining sections on -vv */
657 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
659 while (cp < snapend && ancount--) {
661 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
667 if (cp < snapend && nscount--) {
668 fputs(" ns:", stdout);
669 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
671 while (nscount-- && cp < snapend) {
673 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
679 if (cp < snapend && arcount--) {
680 fputs(" ar:", stdout);
681 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
683 while (cp < snapend && arcount--) {
685 if ((cp = ns_rprint(cp, bp, is_mdns)) == NULL)
693 printf(" (%d)", length);