]>
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.77 2001-09-17 21:58:00 fenner Exp $ (LBL)";
31 #include <sys/param.h>
34 #include <netinet/in.h>
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
45 static const char *ns_ops
[] = {
46 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
47 " op8", " updataA", " updateD", " updateDA",
48 " updateM", " updateMA", " zoneInit", " zoneRef",
51 static const char *ns_resp
[] = {
52 "", " FormErr", " ServFail", " NXDomain",
53 " NotImp", " Refused", " YXDomain", " YXRRSet",
54 " NXRRSet", " NotAuth", " NotZone", " Resp11",
55 " Resp12", " Resp13", " Resp14", " NoChange",
58 /* skip over a domain name */
60 ns_nskip(register const u_char
*cp
, register const u_char
*bp
)
66 if (((i
= *cp
++) & 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
)
95 const u_char
*bitp
, *lim
;
100 if ((bitlen
= *cp
) == 0)
102 slen
= (bitlen
+ 3) / 4;
103 if ((lim
= cp
+ 1 + slen
) > snapend
) {
108 /* print the bit string as a hex string */
110 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
111 printf("%02x", *bitp
);
116 printf("%02x", tc
& (0xff << (8 - b
)));
119 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
121 printf("/%d]", bitlen
);
123 return(truncated
? NULL
: lim
);
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
)) < 0)
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
)) < 0)
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
)) < 0)
231 /* print a <character-string> */
232 static const u_char
*
233 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
240 if (fn_printn(cp
, i
, snapend
))
245 struct tok ns_type2str
[] = {
250 { T_CNAME
, "CNAME" },
258 { T_HINFO
, "HINFO" },
259 { T_MINFO
, "MINFO" },
263 { T_AFSDB
, "AFSDB" },
268 { T_NSAP_PTR
, "NSAP_PTR" },
277 { T_NIMLOC
, "NIMLOC" },
280 { T_NAPTR
, "NAPTR" },
282 { T_DNAME
, "DNAME" },
284 { T_UINFO
, "UINFO" },
287 { T_UNSPEC
, "UNSPEC" },
288 { T_UNSPECA
, "UNSPECA" },
293 { T_MAILB
, "MAILB" },
294 { T_MAILA
, "MAILA" },
299 struct tok ns_class2str
[] = {
300 { C_IN
, "IN" }, /* Not used */
301 { C_CHAOS
, "CHAOS" },
308 static const u_char
*
309 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
311 register const u_char
*np
= cp
;
314 cp
= ns_nskip(cp
, bp
);
316 if (cp
== NULL
|| !TTEST2(*cp
, 4))
319 /* print the qtype and qclass (if it's not IN) */
322 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
326 printf(" %s", tok2str(ns_class2str
, "(Class %d)", i
));
329 cp
= ns_nprint(np
, bp
);
330 return(cp
? cp
+ 4 : NULL
);
334 static const u_char
*
335 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
337 register u_int
class;
338 register u_short typ
, len
;
339 register const u_char
*rp
;
343 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
346 cp
= ns_nskip(cp
, bp
);
348 if (cp
== NULL
|| !TTEST2(*cp
, 10))
351 /* print the type/qtype and class (if it's not IN) */
356 if (class != C_IN
&& typ
!= T_OPT
)
357 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
367 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
373 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
375 printf(" %s", ipaddr_string(cp
));
385 if (ns_nprint(cp
, bp
) == NULL
)
393 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
396 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
398 if (!TTEST2(*cp
, 5 * 4))
400 printf(" %u", EXTRACT_32BITS(cp
));
402 printf(" %u", EXTRACT_32BITS(cp
));
404 printf(" %u", EXTRACT_32BITS(cp
));
406 printf(" %u", EXTRACT_32BITS(cp
));
408 printf(" %u", EXTRACT_32BITS(cp
));
415 if (ns_nprint(cp
+ 2, bp
) == NULL
)
417 printf(" %d", EXTRACT_16BITS(cp
));
422 (void)ns_cprint(cp
, bp
);
427 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
429 printf(" %s", ip6addr_string(cp
));
440 pbyte
= (pbit
& ~7) / 8;
442 printf(" %u(bad plen)", pbit
);
444 } else if (pbit
< 128) {
445 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
447 memset(&a
, 0, sizeof(a
));
448 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
449 printf(" %u %s", pbit
, ip6addr_string(&a
));
453 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
461 printf(" UDPsize=%u", class);
464 case T_UNSPECA
: /* One long string */
465 if (!TTEST2(*cp
, len
))
467 if (fn_printn(cp
, len
, snapend
))
473 if (cp
+ len
> snapend
)
478 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
483 printf(" fudge=%u", EXTRACT_16BITS(cp
));
487 printf(" maclen=%u", EXTRACT_16BITS(cp
));
488 cp
+= 2 + EXTRACT_16BITS(cp
);
491 printf(" origid=%u", EXTRACT_16BITS(cp
));
495 printf(" error=%u", EXTRACT_16BITS(cp
));
499 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
503 return (rp
); /* XXX This isn't always right */
507 ns_print(register const u_char
*bp
, u_int length
)
509 register const HEADER
*np
;
510 register int qdcount
, ancount
, nscount
, arcount
;
511 register const u_char
*cp
= NULL
;
513 np
= (const HEADER
*)bp
;
515 /* get the byte-order right */
516 qdcount
= ntohs(np
->qdcount
);
517 ancount
= ntohs(np
->ancount
);
518 nscount
= ntohs(np
->nscount
);
519 arcount
= ntohs(np
->arcount
);
522 /* this is a response */
523 printf(" %d%s%s%s%s%s%s",
525 ns_ops
[DNS_OPCODE(np
)],
526 ns_resp
[DNS_RCODE(np
)],
527 DNS_AA(np
)? "*" : "",
528 DNS_RA(np
)? "" : "-",
529 DNS_TC(np
)? "|" : "",
530 DNS_CD(np
)? "%" : "");
533 printf(" [%dq]", qdcount
);
534 /* Print QUESTION section on -vv */
535 cp
= (const u_char
*)(np
+ 1);
537 if (qdcount
< ntohs(np
->qdcount
) - 1)
540 fputs(" q:", stdout
);
541 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
545 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
548 cp
+= 4; /* skip QTYPE and QCLASS */
551 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
553 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
555 while (cp
< snapend
&& ancount
--) {
557 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
563 /* Print NS and AR sections on -vv */
565 if (cp
< snapend
&& nscount
--) {
566 fputs(" ns:", stdout
);
567 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
569 while (cp
< snapend
&& nscount
--) {
571 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
577 if (cp
< snapend
&& arcount
--) {
578 fputs(" ar:", stdout
);
579 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
581 while (cp
< snapend
&& arcount
--) {
583 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
592 /* this is a request */
593 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
594 DNS_RD(np
) ? "+" : "",
595 DNS_AD(np
) ? "$" : "");
598 if (*(((u_short
*)np
)+1) & htons(0x6cf))
599 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
601 if (DNS_OPCODE(np
) == IQUERY
) {
603 printf(" [%dq]", qdcount
);
605 printf(" [%da]", ancount
);
609 printf(" [%da]", ancount
);
611 printf(" [%dq]", qdcount
);
614 printf(" [%dn]", nscount
);
616 printf(" [%dau]", arcount
);
619 cp
= ns_qprint((const u_char
*)(np
+ 1),
623 while (cp
< snapend
&& qdcount
--) {
624 cp
= ns_qprint((const u_char
*)cp
,
633 /* Print remaining sections on -vv */
636 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
638 while (cp
< snapend
&& ancount
--) {
640 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
646 if (cp
< snapend
&& nscount
--) {
647 fputs(" ns:", stdout
);
648 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
650 while (nscount
-- && cp
< snapend
) {
652 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
658 if (cp
< snapend
&& arcount
--) {
659 fputs(" ar:", stdout
);
660 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
662 while (cp
< snapend
&& arcount
--) {
664 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
672 printf(" (%d)", length
);