]>
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.65 2001-01-12 15:06:06 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 */
53 #define T_TXT 16 /* text strings */
56 #define T_RP 17 /* responsible person */
59 #define T_AFSDB 18 /* AFS cell database */
62 #define T_X25 19 /* X_25 calling address */
65 #define T_ISDN 20 /* ISDN calling address */
68 #define T_RT 21 /* router */
71 #define T_NSAP 22 /* NSAP address */
74 #define T_NSAP_PTR 23 /* reverse NSAP lookup (deprecated) */
77 #define T_SIG 24 /* security signature */
80 #define T_KEY 25 /* security key */
83 #define T_PX 26 /* X.400 mail mapping */
86 #define T_GPOS 27 /* geographical position (withdrawn) */
89 #define T_AAAA 28 /* IP6 Address */
92 #define T_LOC 29 /* Location Information */
95 #define T_NXT 30 /* Next Valid Name in Zone */
98 #define T_EID 31 /* Endpoint identifier */
101 #define T_NIMLOC 32 /* Nimrod locator */
104 #define T_SRV 33 /* Server selection */
107 #define T_ATMA 34 /* ATM Address */
110 #define T_NAPTR 35 /* Naming Authority PoinTeR */
113 #define T_A6 38 /* IP6 address */
116 #define T_DNAME 39 /* non-terminal redirection */
120 #define T_OPT 41 /* EDNS0 option (meta-RR) */
124 #define T_UNSPEC 103 /* Unspecified format (binary data) */
127 #define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
131 #define C_CHAOS 3 /* for chaos net (MIT) */
134 #define C_HS 4 /* for Hesiod name server (MIT) (XXX) */
137 static char *ns_ops
[] = {
138 "", " inv_q", " stat", " op3", " notify", " op5", " op6", " op7",
139 " op8", " updataA", " updateD", " updateDA",
140 " updateM", " updateMA", " zoneInit", " zoneRef",
143 static char *ns_resp
[] = {
144 "", " FormErr", " ServFail", " NXDomain",
145 " NotImp", " Refused", " Resp6", " Resp7",
146 " Resp8", " Resp9", " Resp10", " Resp11",
147 " Resp12", " Resp13", " Resp14", " NoChange",
150 /* skip over a domain name */
151 static const u_char
*
152 ns_nskip(register const u_char
*cp
, register const u_char
*bp
)
156 if (((i
= *cp
++) & INDIR_MASK
) == INDIR_MASK
)
160 while (i
&& cp
< snapend
) {
161 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
164 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
165 return(NULL
); /* unknown ELT */
166 if ((bitlen
= *cp
++) == 0)
168 bytelen
= (bitlen
+ 7) / 8;
179 /* print a <domain-name> */
180 static const u_char
*
181 blabel_print(const u_char
*cp
)
185 const u_char
*bitp
, *lim
;
190 if ((bitlen
= *cp
) == 0)
192 slen
= (bitlen
+ 3) / 4;
193 if ((lim
= cp
+ 1 + slen
) > snapend
) {
198 /* print the bit string as a hex string */
200 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
201 printf("%02x", *bitp
);
206 printf("%02x", tc
& (0xff << (8 - b
)));
209 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
211 printf("/%d]", bitlen
);
213 return(truncated
? NULL
: lim
);
217 labellen(const u_char
*cp
)
224 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
227 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
229 if (cp
+ 1 >= snapend
)
231 if ((bitlen
= *(cp
+ 1)) == 0)
233 return(((bitlen
+ 7) / 8) + 1);
238 static const u_char
*
239 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
242 register const u_char
*rp
= NULL
;
243 register int compress
= 0;
246 int data_size
= snapend
- bp
;
248 if ((l
= labellen(cp
)) < 0)
253 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
259 while (i
&& cp
< snapend
) {
260 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
265 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
268 if ((l
= labellen(cp
)) < 0)
274 * If we've looked at every character in
275 * the message, this pointer will make
276 * us look at some character again,
277 * which means we're looping.
279 if (chars_processed
>= data_size
) {
285 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
286 elt
= (i
& ~INDIR_MASK
);
288 case EDNS0_ELT_BITLABEL
:
293 printf("<ELT %d>", elt
);
297 if (fn_printn(cp
, l
, snapend
))
302 chars_processed
+= l
;
304 if (cp
>= snapend
|| (l
= labellen(cp
)) < 0)
316 /* print a <character-string> */
317 static const u_char
*
318 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
325 (void)fn_printn(cp
, i
, snapend
);
329 static struct tok type2str
[] = {
334 { T_CNAME
, "CNAME" },
342 { T_HINFO
, "HINFO" },
343 { T_MINFO
, "MINFO" },
347 { T_AFSDB
, "AFSDB" },
352 { T_NSAP_PTR
, "NSAP_PTR" },
361 { T_NIMLOC
, "NIMLOC " },
364 { T_NAPTR
, "NAPTR " },
366 { T_DNAME
, "DNAME " },
368 { T_UINFO
, "UINFO" },
371 { T_UNSPEC
, "UNSPEC" },
372 { T_UNSPECA
, "UNSPECA" },
374 { T_MAILB
, "MAILB" },
375 { T_MAILA
, "MAILA" },
380 static struct tok class2str
[] = {
381 { C_IN
, "IN" }, /* Not used */
382 { C_CHAOS
, "CHAOS)" },
389 static const u_char
*
390 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
392 register const u_char
*np
= cp
;
395 cp
= ns_nskip(cp
, bp
);
397 if (cp
+ 4 > snapend
|| cp
== NULL
)
400 /* print the qtype and qclass (if it's not IN) */
403 printf(" %s", tok2str(type2str
, "Type%d", i
));
407 printf(" %s", tok2str(class2str
, "(Class %d)", i
));
410 cp
= ns_nprint(np
, bp
);
411 return(cp
? cp
+ 4 : NULL
);
415 static const u_char
*
416 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
418 register u_int
class;
419 register u_short typ
, len
;
420 register const u_char
*rp
;
424 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
427 cp
= ns_nskip(cp
, bp
);
429 if (cp
+ 10 > snapend
|| cp
== NULL
)
432 /* print the type/qtype and class (if it's not IN) */
437 if (class != C_IN
&& typ
!= T_OPT
)
438 printf(" %s", tok2str(class2str
, "(Class %d)", class));
448 printf(" %s", tok2str(type2str
, "Type%d", typ
));
454 if (cp
+ sizeof(struct in_addr
) > snapend
)
456 printf(" %s", ipaddr_string(cp
));
466 if (ns_nprint(cp
, bp
) == NULL
)
474 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
477 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
479 if (cp
+ 5 * 4 > snapend
)
481 printf(" %u", EXTRACT_32BITS(cp
));
483 printf(" %u", EXTRACT_32BITS(cp
));
485 printf(" %u", EXTRACT_32BITS(cp
));
487 printf(" %u", EXTRACT_32BITS(cp
));
489 printf(" %u", EXTRACT_32BITS(cp
));
494 if (cp
+ 2 > snapend
)
496 if (ns_nprint(cp
+ 2, bp
) == NULL
)
498 printf(" %d", EXTRACT_16BITS(cp
));
503 (void)ns_cprint(cp
, bp
);
508 if (cp
+ sizeof(struct in6_addr
) > snapend
)
510 printf(" %s", ip6addr_string(cp
));
519 pbyte
= (pbit
& ~7) / 8;
521 printf(" %u(bad plen)", pbit
);
523 } else if (pbit
< 128) {
524 memset(&a
, 0, sizeof(a
));
525 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
526 printf(" %u %s", pbit
, ip6addr_string(&a
));
530 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
538 printf(" UDPsize=%u", class);
541 case T_UNSPECA
: /* One long string */
542 if (cp
+ len
> snapend
)
544 fn_printn(cp
, len
, snapend
);
547 return (rp
); /* XXX This isn't always right */
551 ns_print(register const u_char
*bp
, u_int length
)
553 register const HEADER
*np
;
554 register int qdcount
, ancount
, nscount
, arcount
;
555 register const u_char
*cp
= NULL
;
557 np
= (const HEADER
*)bp
;
558 /* get the byte-order right */
559 qdcount
= ntohs(np
->qdcount
);
560 ancount
= ntohs(np
->ancount
);
561 nscount
= ntohs(np
->nscount
);
562 arcount
= ntohs(np
->arcount
);
565 /* this is a response */
566 printf(" %d%s%s%s%s%s%s",
568 ns_ops
[DNS_OPCODE(np
)],
569 ns_resp
[DNS_RCODE(np
)],
570 DNS_AA(np
)? "*" : "",
571 DNS_RA(np
)? "" : "-",
572 DNS_TC(np
)? "|" : "",
573 DNS_CD(np
)? "%" : "");
576 printf(" [%dq]", qdcount
);
577 /* Print QUESTION section on -vv */
579 fputs(" q:", stdout
);
580 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
584 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
589 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
591 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
593 while (cp
< snapend
&& ancount
--) {
595 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
601 /* Print NS and AR sections on -vv */
603 if (cp
< snapend
&& nscount
--) {
604 fputs(" ns:", stdout
);
605 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
607 while (cp
< snapend
&& nscount
--) {
609 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
615 if (cp
< snapend
&& arcount
--) {
616 fputs(" ar:", stdout
);
617 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
619 while (cp
< snapend
&& arcount
--) {
621 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
630 /* this is a request */
631 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
632 DNS_RD(np
) ? "+" : "",
633 DNS_AD(np
) ? "$" : "");
636 if (*(((u_short
*)np
)+1) & htons(0x6cf))
637 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
639 if (DNS_OPCODE(np
) == IQUERY
) {
641 printf(" [%dq]", qdcount
);
643 printf(" [%da]", ancount
);
647 printf(" [%da]", ancount
);
649 printf(" [%dq]", qdcount
);
652 printf(" [%dn]", nscount
);
654 printf(" [%dau]", arcount
);
657 cp
= ns_qprint((const u_char
*)(np
+ 1),
661 while (cp
< snapend
&& qdcount
--) {
662 cp
= ns_qprint((const u_char
*)cp
,
671 /* Print remaining sections on -vv */
674 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
676 while (cp
< snapend
&& ancount
--) {
678 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
684 if (cp
< snapend
&& nscount
--) {
685 fputs(" ns:", stdout
);
686 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
688 while (nscount
-- && cp
< snapend
) {
690 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
696 if (cp
< snapend
&& arcount
--) {
697 fputs(" ar:", stdout
);
698 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
700 while (cp
< snapend
&& arcount
--) {
702 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
710 printf(" (%d)", length
);