]>
The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
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.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.75 2001-02-23 08:55:21 guy Exp $ (LBL)";
31 #include <sys/param.h>
34 #include <netinet/in.h>
37 #undef NOERROR /* Solaris sucks */
40 #undef T_UNSPEC /* SINIX does too */
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h" /* must come after interface.h */
51 static char *ns_ops
[] = {
52 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
53 " op8", " updataA", " updateD", " updateDA",
54 " updateM", " updateMA", " zoneInit", " zoneRef",
57 static char *ns_resp
[] = {
58 "", " FormErr", " ServFail", " NXDomain",
59 " NotImp", " Refused", " YXDomain", " YXRRSet",
60 " NXRRSet", " NotAuth", " NotZone", " Resp11",
61 " Resp12", " Resp13", " Resp14", " NoChange",
64 /* skip over a domain name */
66 ns_nskip(register const u_char
*cp
, register const u_char
*bp
)
72 if (((i
= *cp
++) & INDIR_MASK
) == INDIR_MASK
)
75 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
78 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
79 return(NULL
); /* unknown ELT */
82 if ((bitlen
= *cp
++) == 0)
84 bytelen
= (bitlen
+ 7) / 8;
95 /* print a <domain-name> */
97 blabel_print(const u_char
*cp
)
101 const u_char
*bitp
, *lim
;
106 if ((bitlen
= *cp
) == 0)
108 slen
= (bitlen
+ 3) / 4;
109 if ((lim
= cp
+ 1 + slen
) > snapend
) {
114 /* print the bit string as a hex string */
116 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
117 printf("%02x", *bitp
);
122 printf("%02x", tc
& (0xff << (8 - b
)));
125 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
127 printf("/%d]", bitlen
);
129 return(truncated
? NULL
: lim
);
133 labellen(const u_char
*cp
)
140 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
143 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
145 if (!TTEST2(*(cp
+ 1), 1))
147 if ((bitlen
= *(cp
+ 1)) == 0)
149 return(((bitlen
+ 7) / 8) + 1);
154 static const u_char
*
155 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
158 register const u_char
*rp
= NULL
;
159 register int compress
= 0;
162 int data_size
= snapend
- bp
;
164 if ((l
= labellen(cp
)) < 0)
169 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
175 while (i
&& cp
< snapend
) {
176 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
183 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
184 if ((l
= labellen(cp
)) < 0)
192 * If we've looked at every character in
193 * the message, this pointer will make
194 * us look at some character again,
195 * which means we're looping.
197 if (chars_processed
>= data_size
) {
203 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
204 elt
= (i
& ~INDIR_MASK
);
206 case EDNS0_ELT_BITLABEL
:
207 if (blabel_print(cp
) == NULL
)
212 printf("<ELT %d>", elt
);
216 if (fn_printn(cp
, l
, snapend
))
221 chars_processed
+= l
;
223 if ((l
= labellen(cp
)) < 0)
237 /* print a <character-string> */
238 static const u_char
*
239 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
246 if (fn_printn(cp
, i
, snapend
))
251 struct tok ns_type2str
[] = {
256 { T_CNAME
, "CNAME" },
264 { T_HINFO
, "HINFO" },
265 { T_MINFO
, "MINFO" },
269 { T_AFSDB
, "AFSDB" },
274 { T_NSAP_PTR
, "NSAP_PTR" },
283 { T_NIMLOC
, "NIMLOC" },
286 { T_NAPTR
, "NAPTR" },
288 { T_DNAME
, "DNAME" },
290 { T_UINFO
, "UINFO" },
293 { T_UNSPEC
, "UNSPEC" },
294 { T_UNSPECA
, "UNSPECA" },
299 { T_MAILB
, "MAILB" },
300 { T_MAILA
, "MAILA" },
305 struct tok ns_class2str
[] = {
306 { C_IN
, "IN" }, /* Not used */
307 { C_CHAOS
, "CHAOS" },
314 static const u_char
*
315 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
317 register const u_char
*np
= cp
;
320 cp
= ns_nskip(cp
, bp
);
322 if (cp
== NULL
|| !TTEST2(*cp
, 4))
325 /* print the qtype and qclass (if it's not IN) */
328 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
332 printf(" %s", tok2str(ns_class2str
, "(Class %d)", i
));
335 cp
= ns_nprint(np
, bp
);
336 return(cp
? cp
+ 4 : NULL
);
340 static const u_char
*
341 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
343 register u_int
class;
344 register u_short typ
, len
;
345 register const u_char
*rp
;
349 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
352 cp
= ns_nskip(cp
, bp
);
354 if (cp
== NULL
|| !TTEST2(*cp
, 10))
357 /* print the type/qtype and class (if it's not IN) */
362 if (class != C_IN
&& typ
!= T_OPT
)
363 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
373 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
379 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
381 printf(" %s", ipaddr_string(cp
));
391 if (ns_nprint(cp
, bp
) == NULL
)
399 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
402 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
404 if (!TTEST2(*cp
, 5 * 4))
406 printf(" %u", EXTRACT_32BITS(cp
));
408 printf(" %u", EXTRACT_32BITS(cp
));
410 printf(" %u", EXTRACT_32BITS(cp
));
412 printf(" %u", EXTRACT_32BITS(cp
));
414 printf(" %u", EXTRACT_32BITS(cp
));
421 if (ns_nprint(cp
+ 2, bp
) == NULL
)
423 printf(" %d", EXTRACT_16BITS(cp
));
428 (void)ns_cprint(cp
, bp
);
433 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
435 printf(" %s", ip6addr_string(cp
));
446 pbyte
= (pbit
& ~7) / 8;
448 printf(" %u(bad plen)", pbit
);
450 } else if (pbit
< 128) {
451 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
453 memset(&a
, 0, sizeof(a
));
454 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
455 printf(" %u %s", pbit
, ip6addr_string(&a
));
459 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
467 printf(" UDPsize=%u", class);
470 case T_UNSPECA
: /* One long string */
471 if (!TTEST2(*cp
, len
))
473 if (fn_printn(cp
, len
, snapend
))
479 if (cp
+ len
> snapend
)
484 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
489 printf(" fudge=%u", EXTRACT_16BITS(cp
));
493 printf(" maclen=%u", EXTRACT_16BITS(cp
));
494 cp
+= 2 + EXTRACT_16BITS(cp
);
497 printf(" origid=%u", EXTRACT_16BITS(cp
));
501 printf(" error=%u", EXTRACT_16BITS(cp
));
505 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
509 return (rp
); /* XXX This isn't always right */
513 ns_print(register const u_char
*bp
, u_int length
)
515 register const HEADER
*np
;
516 register int qdcount
, ancount
, nscount
, arcount
;
517 register const u_char
*cp
= NULL
;
519 np
= (const HEADER
*)bp
;
521 /* get the byte-order right */
522 qdcount
= ntohs(np
->qdcount
);
523 ancount
= ntohs(np
->ancount
);
524 nscount
= ntohs(np
->nscount
);
525 arcount
= ntohs(np
->arcount
);
528 /* this is a response */
529 printf(" %d%s%s%s%s%s%s",
531 ns_ops
[DNS_OPCODE(np
)],
532 ns_resp
[DNS_RCODE(np
)],
533 DNS_AA(np
)? "*" : "",
534 DNS_RA(np
)? "" : "-",
535 DNS_TC(np
)? "|" : "",
536 DNS_CD(np
)? "%" : "");
539 printf(" [%dq]", qdcount
);
540 /* Print QUESTION section on -vv */
541 cp
= (const u_char
*)(np
+ 1);
543 if (qdcount
< ntohs(np
->qdcount
) - 1)
546 fputs(" q:", stdout
);
547 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
551 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
554 cp
+= 4; /* skip QTYPE and QCLASS */
557 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
559 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
561 while (cp
< snapend
&& ancount
--) {
563 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
569 /* Print NS and AR sections on -vv */
571 if (cp
< snapend
&& nscount
--) {
572 fputs(" ns:", stdout
);
573 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
575 while (cp
< snapend
&& nscount
--) {
577 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
583 if (cp
< snapend
&& arcount
--) {
584 fputs(" ar:", stdout
);
585 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
587 while (cp
< snapend
&& arcount
--) {
589 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
598 /* this is a request */
599 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
600 DNS_RD(np
) ? "+" : "",
601 DNS_AD(np
) ? "$" : "");
604 if (*(((u_short
*)np
)+1) & htons(0x6cf))
605 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
607 if (DNS_OPCODE(np
) == IQUERY
) {
609 printf(" [%dq]", qdcount
);
611 printf(" [%da]", ancount
);
615 printf(" [%da]", ancount
);
617 printf(" [%dq]", qdcount
);
620 printf(" [%dn]", nscount
);
622 printf(" [%dau]", arcount
);
625 cp
= ns_qprint((const u_char
*)(np
+ 1),
629 while (cp
< snapend
&& qdcount
--) {
630 cp
= ns_qprint((const u_char
*)cp
,
639 /* Print remaining sections on -vv */
642 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
644 while (cp
< snapend
&& ancount
--) {
646 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
652 if (cp
< snapend
&& nscount
--) {
653 fputs(" ns:", stdout
);
654 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
656 while (nscount
-- && cp
< snapend
) {
658 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
664 if (cp
< snapend
&& arcount
--) {
665 fputs(" ar:", stdout
);
666 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
668 while (cp
< snapend
&& arcount
--) {
670 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
678 printf(" (%d)", length
);