]>
The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
0c3c80e101d383c0a82b93fd611ab97acc7d7867
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.73 2001-02-20 07:22:17 itojun 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
)
70 if (((i
= *cp
++) & INDIR_MASK
) == INDIR_MASK
)
74 while (i
&& cp
< snapend
) {
75 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
78 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
79 return(NULL
); /* unknown ELT */
80 if ((bitlen
= *cp
++) == 0)
82 bytelen
= (bitlen
+ 7) / 8;
93 /* print a <domain-name> */
95 blabel_print(const u_char
*cp
)
99 const u_char
*bitp
, *lim
;
104 if ((bitlen
= *cp
) == 0)
106 slen
= (bitlen
+ 3) / 4;
107 if ((lim
= cp
+ 1 + slen
) > snapend
) {
112 /* print the bit string as a hex string */
114 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
115 printf("%02x", *bitp
);
120 printf("%02x", tc
& (0xff << (8 - b
)));
123 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
125 printf("/%d]", bitlen
);
127 return(truncated
? NULL
: lim
);
131 labellen(const u_char
*cp
)
138 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
141 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
143 if (cp
+ 1 >= snapend
)
145 if ((bitlen
= *(cp
+ 1)) == 0)
147 return(((bitlen
+ 7) / 8) + 1);
152 static const u_char
*
153 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
156 register const u_char
*rp
= NULL
;
157 register int compress
= 0;
160 int data_size
= snapend
- bp
;
162 if ((l
= labellen(cp
)) < 0)
167 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
173 while (i
&& cp
< snapend
) {
174 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
179 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
182 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
:
207 printf("<ELT %d>", elt
);
211 if (fn_printn(cp
, l
, snapend
))
216 chars_processed
+= l
;
218 if (cp
>= snapend
|| (l
= labellen(cp
)) < 0)
230 /* print a <character-string> */
231 static const u_char
*
232 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
239 (void)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
;
312 cp
= ns_nskip(cp
, bp
);
314 if (cp
+ 4 > snapend
|| cp
== NULL
)
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
)
344 cp
= ns_nskip(cp
, bp
);
346 if (cp
+ 10 > snapend
|| cp
== NULL
)
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 (cp
+ sizeof(struct in_addr
) > snapend
)
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 (cp
+ 5 * 4 > snapend
)
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
));
411 if (cp
+ 2 > snapend
)
413 if (ns_nprint(cp
+ 2, bp
) == NULL
)
415 printf(" %d", EXTRACT_16BITS(cp
));
420 (void)ns_cprint(cp
, bp
);
425 if (cp
+ sizeof(struct in6_addr
) > snapend
)
427 printf(" %s", ip6addr_string(cp
));
436 pbyte
= (pbit
& ~7) / 8;
438 printf(" %u(bad plen)", pbit
);
440 } else if (pbit
< 128) {
441 memset(&a
, 0, sizeof(a
));
442 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
443 printf(" %u %s", pbit
, ip6addr_string(&a
));
447 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
455 printf(" UDPsize=%u", class);
458 case T_UNSPECA
: /* One long string */
459 if (cp
+ len
> snapend
)
461 fn_printn(cp
, len
, snapend
);
466 if (cp
+ len
> snapend
)
471 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
474 printf(" fudge=%u", EXTRACT_16BITS(cp
));
476 printf(" maclen=%u", EXTRACT_16BITS(cp
));
477 cp
+= 2 + EXTRACT_16BITS(cp
);
478 printf(" origid=%u", EXTRACT_16BITS(cp
));
480 printf(" error=%u", EXTRACT_16BITS(cp
));
482 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
486 return (rp
); /* XXX This isn't always right */
490 ns_print(register const u_char
*bp
, u_int length
)
492 register const HEADER
*np
;
493 register int qdcount
, ancount
, nscount
, arcount
;
494 register const u_char
*cp
= NULL
;
496 np
= (const HEADER
*)bp
;
497 /* get the byte-order right */
498 qdcount
= ntohs(np
->qdcount
);
499 ancount
= ntohs(np
->ancount
);
500 nscount
= ntohs(np
->nscount
);
501 arcount
= ntohs(np
->arcount
);
504 /* this is a response */
505 printf(" %d%s%s%s%s%s%s",
507 ns_ops
[DNS_OPCODE(np
)],
508 ns_resp
[DNS_RCODE(np
)],
509 DNS_AA(np
)? "*" : "",
510 DNS_RA(np
)? "" : "-",
511 DNS_TC(np
)? "|" : "",
512 DNS_CD(np
)? "%" : "");
515 printf(" [%dq]", qdcount
);
516 /* Print QUESTION section on -vv */
517 cp
= (const u_char
*)(np
+ 1);
519 if (qdcount
< ntohs(np
->qdcount
) - 1)
522 fputs(" q:", stdout
);
523 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
527 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
530 cp
+= 4; /* skip QTYPE and QCLASS */
533 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
535 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
537 while (cp
< snapend
&& ancount
--) {
539 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
545 /* Print NS and AR sections on -vv */
547 if (cp
< snapend
&& nscount
--) {
548 fputs(" ns:", stdout
);
549 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
551 while (cp
< snapend
&& nscount
--) {
553 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
559 if (cp
< snapend
&& arcount
--) {
560 fputs(" ar:", stdout
);
561 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
563 while (cp
< snapend
&& arcount
--) {
565 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
574 /* this is a request */
575 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
576 DNS_RD(np
) ? "+" : "",
577 DNS_AD(np
) ? "$" : "");
580 if (*(((u_short
*)np
)+1) & htons(0x6cf))
581 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
583 if (DNS_OPCODE(np
) == IQUERY
) {
585 printf(" [%dq]", qdcount
);
587 printf(" [%da]", ancount
);
591 printf(" [%da]", ancount
);
593 printf(" [%dq]", qdcount
);
596 printf(" [%dn]", nscount
);
598 printf(" [%dau]", arcount
);
601 cp
= ns_qprint((const u_char
*)(np
+ 1),
605 while (cp
< snapend
&& qdcount
--) {
606 cp
= ns_qprint((const u_char
*)cp
,
615 /* Print remaining sections on -vv */
618 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
620 while (cp
< snapend
&& ancount
--) {
622 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
628 if (cp
< snapend
&& nscount
--) {
629 fputs(" ns:", stdout
);
630 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
632 while (nscount
-- && cp
< snapend
) {
634 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
640 if (cp
< snapend
&& arcount
--) {
641 fputs(" ar:", stdout
);
642 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
644 while (cp
< snapend
&& arcount
--) {
646 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
654 printf(" (%d)", length
);