]>
The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
b55d362da29399f3de4c377b43ba149858a4a41b
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.83 2003-04-04 00:18:54 fenner Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
38 #include "interface.h"
39 #include "addrtoname.h"
40 #include "extract.h" /* must come after interface.h */
42 static const char *ns_ops
[] = {
43 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
44 " op8", " updataA", " updateD", " updateDA",
45 " updateM", " updateMA", " zoneInit", " zoneRef",
48 static const char *ns_resp
[] = {
49 "", " FormErr", " ServFail", " NXDomain",
50 " NotImp", " Refused", " YXDomain", " YXRRSet",
51 " NXRRSet", " NotAuth", " NotZone", " Resp11",
52 " Resp12", " Resp13", " Resp14", " NoChange",
55 /* skip over a domain name */
57 ns_nskip(register const u_char
*cp
)
65 if ((i
& INDIR_MASK
) == INDIR_MASK
)
67 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
70 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
71 return(NULL
); /* unknown ELT */
74 if ((bitlen
= *cp
++) == 0)
76 bytelen
= (bitlen
+ 7) / 8;
87 /* print a <domain-name> */
89 blabel_print(const u_char
*cp
)
93 const u_char
*bitp
, *lim
;
98 if ((bitlen
= *cp
) == 0)
100 slen
= (bitlen
+ 3) / 4;
101 if ((lim
= cp
+ 1 + slen
) > snapend
) {
106 /* print the bit string as a hex string */
108 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
109 printf("%02x", *bitp
);
114 printf("%02x", tc
& (0xff << (8 - b
)));
117 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
119 printf("/%d]", bitlen
);
121 return(truncated
? NULL
: lim
);
125 labellen(const u_char
*cp
)
132 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
135 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
137 if (!TTEST2(*(cp
+ 1), 1))
139 if ((bitlen
= *(cp
+ 1)) == 0)
141 return(((bitlen
+ 7) / 8) + 1);
146 static const u_char
*
147 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
150 register const u_char
*rp
= NULL
;
151 register int compress
= 0;
154 int data_size
= snapend
- bp
;
156 if ((l
= labellen(cp
)) == (u_int
)-1)
161 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
167 while (i
&& cp
< snapend
) {
168 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
175 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
176 if ((l
= labellen(cp
)) == (u_int
)-1)
184 * If we've looked at every character in
185 * the message, this pointer will make
186 * us look at some character again,
187 * which means we're looping.
189 if (chars_processed
>= data_size
) {
195 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
196 elt
= (i
& ~INDIR_MASK
);
198 case EDNS0_ELT_BITLABEL
:
199 if (blabel_print(cp
) == NULL
)
204 printf("<ELT %d>", elt
);
208 if (fn_printn(cp
, l
, snapend
))
213 chars_processed
+= l
;
215 if ((l
= labellen(cp
)) == (u_int
)-1)
229 /* print a <character-string> */
230 static const u_char
*
231 ns_cprint(register const u_char
*cp
)
238 if (fn_printn(cp
, i
, snapend
))
243 struct tok ns_type2str
[] = {
248 { T_CNAME
, "CNAME" },
256 { T_HINFO
, "HINFO" },
257 { T_MINFO
, "MINFO" },
261 { T_AFSDB
, "AFSDB" },
266 { T_NSAP_PTR
, "NSAP_PTR" },
275 { T_NIMLOC
, "NIMLOC" },
278 { T_NAPTR
, "NAPTR" },
280 { T_DNAME
, "DNAME" },
282 { T_UINFO
, "UINFO" },
285 { T_UNSPEC
, "UNSPEC" },
286 { T_UNSPECA
, "UNSPECA" },
291 { T_MAILB
, "MAILB" },
292 { T_MAILA
, "MAILA" },
297 struct tok ns_class2str
[] = {
298 { C_IN
, "IN" }, /* Not used */
299 { C_CHAOS
, "CHAOS" },
306 static const u_char
*
307 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
309 register const u_char
*np
= cp
;
314 if (cp
== NULL
|| !TTEST2(*cp
, 4))
317 /* print the qtype and qclass (if it's not IN) */
320 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
324 printf(" %s", tok2str(ns_class2str
, "(Class %d)", i
));
327 cp
= ns_nprint(np
, bp
);
328 return(cp
? cp
+ 4 : NULL
);
332 static const u_char
*
333 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
335 register u_int
class;
336 register u_short typ
, len
;
337 register const u_char
*rp
;
341 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
346 if (cp
== NULL
|| !TTEST2(*cp
, 10))
349 /* print the type/qtype and class (if it's not IN) */
354 if (class != C_IN
&& typ
!= T_OPT
)
355 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
365 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
371 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
373 printf(" %s", ipaddr_string(cp
));
383 if (ns_nprint(cp
, bp
) == NULL
)
391 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
394 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
396 if (!TTEST2(*cp
, 5 * 4))
398 printf(" %u", EXTRACT_32BITS(cp
));
400 printf(" %u", EXTRACT_32BITS(cp
));
402 printf(" %u", EXTRACT_32BITS(cp
));
404 printf(" %u", EXTRACT_32BITS(cp
));
406 printf(" %u", EXTRACT_32BITS(cp
));
413 if (ns_nprint(cp
+ 2, bp
) == NULL
)
415 printf(" %d", EXTRACT_16BITS(cp
));
427 if (ns_nprint(cp
+ 6, bp
) == NULL
)
429 printf(":%d %d %d", EXTRACT_16BITS(cp
+ 4),
430 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2));
435 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
437 printf(" %s", ip6addr_string(cp
));
448 pbyte
= (pbit
& ~7) / 8;
450 printf(" %u(bad plen)", pbit
);
452 } else if (pbit
< 128) {
453 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
455 memset(&a
, 0, sizeof(a
));
456 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
457 printf(" %u %s", pbit
, ip6addr_string(&a
));
461 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
469 printf(" UDPsize=%u", class);
472 case T_UNSPECA
: /* One long string */
473 if (!TTEST2(*cp
, len
))
475 if (fn_printn(cp
, len
, snapend
))
481 if (cp
+ len
> snapend
)
486 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
491 printf(" fudge=%u", EXTRACT_16BITS(cp
));
495 printf(" maclen=%u", EXTRACT_16BITS(cp
));
496 cp
+= 2 + EXTRACT_16BITS(cp
);
499 printf(" origid=%u", EXTRACT_16BITS(cp
));
503 printf(" error=%u", EXTRACT_16BITS(cp
));
507 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
511 return (rp
); /* XXX This isn't always right */
515 ns_print(register const u_char
*bp
, u_int length
)
517 register const HEADER
*np
;
518 register int qdcount
, ancount
, nscount
, arcount
;
519 register const u_char
*cp
;
522 np
= (const HEADER
*)bp
;
524 /* get the byte-order right */
525 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
526 ancount
= EXTRACT_16BITS(&np
->ancount
);
527 nscount
= EXTRACT_16BITS(&np
->nscount
);
528 arcount
= EXTRACT_16BITS(&np
->arcount
);
531 /* this is a response */
532 printf(" %d%s%s%s%s%s%s",
533 EXTRACT_16BITS(&np
->id
),
534 ns_ops
[DNS_OPCODE(np
)],
535 ns_resp
[DNS_RCODE(np
)],
536 DNS_AA(np
)? "*" : "",
537 DNS_RA(np
)? "" : "-",
538 DNS_TC(np
)? "|" : "",
539 DNS_CD(np
)? "%" : "");
542 printf(" [%dq]", qdcount
);
543 /* Print QUESTION section on -vv */
544 cp
= (const u_char
*)(np
+ 1);
546 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
549 fputs(" q:", stdout
);
550 if ((cp
= ns_qprint(cp
, bp
)) == NULL
)
553 if ((cp
= ns_nskip(cp
)) == NULL
)
555 cp
+= 4; /* skip QTYPE and QCLASS */
558 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
560 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
562 while (cp
< snapend
&& ancount
--) {
564 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
570 /* Print NS and AR sections on -vv */
572 if (cp
< snapend
&& nscount
--) {
573 fputs(" ns:", stdout
);
574 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
576 while (cp
< snapend
&& nscount
--) {
578 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
584 if (cp
< snapend
&& arcount
--) {
585 fputs(" ar:", stdout
);
586 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
588 while (cp
< snapend
&& arcount
--) {
590 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
599 /* this is a request */
600 printf(" %d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
601 DNS_RD(np
) ? "+" : "",
602 DNS_AD(np
) ? "$" : "");
605 b2
= EXTRACT_16BITS(((u_short
*)np
)+1);
607 printf(" [b2&3=0x%x]", b2
);
609 if (DNS_OPCODE(np
) == IQUERY
) {
611 printf(" [%dq]", qdcount
);
613 printf(" [%da]", ancount
);
617 printf(" [%da]", ancount
);
619 printf(" [%dq]", qdcount
);
622 printf(" [%dn]", nscount
);
624 printf(" [%dau]", arcount
);
626 cp
= (const u_char
*)(np
+ 1);
628 cp
= ns_qprint(cp
, (const u_char
*)np
);
631 while (cp
< snapend
&& qdcount
--) {
632 cp
= ns_qprint((const u_char
*)cp
,
641 /* Print remaining sections on -vv */
644 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
646 while (cp
< snapend
&& ancount
--) {
648 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
654 if (cp
< snapend
&& nscount
--) {
655 fputs(" ns:", stdout
);
656 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
658 while (nscount
-- && cp
< snapend
) {
660 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
666 if (cp
< snapend
&& arcount
--) {
667 fputs(" ar:", stdout
);
668 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
670 while (cp
< snapend
&& arcount
--) {
672 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
680 printf(" (%d)", length
);