]>
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.81 2002-09-05 21:25:39 guy 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
)
63 if (((i
= *cp
++) & INDIR_MASK
) == INDIR_MASK
)
66 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
69 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
70 return(NULL
); /* unknown ELT */
73 if ((bitlen
= *cp
++) == 0)
75 bytelen
= (bitlen
+ 7) / 8;
86 /* print a <domain-name> */
88 blabel_print(const u_char
*cp
)
92 const u_char
*bitp
, *lim
;
97 if ((bitlen
= *cp
) == 0)
99 slen
= (bitlen
+ 3) / 4;
100 if ((lim
= cp
+ 1 + slen
) > snapend
) {
105 /* print the bit string as a hex string */
107 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++)
108 printf("%02x", *bitp
);
113 printf("%02x", tc
& (0xff << (8 - b
)));
116 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
118 printf("/%d]", bitlen
);
120 return(truncated
? NULL
: lim
);
124 labellen(const u_char
*cp
)
131 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
134 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
136 if (!TTEST2(*(cp
+ 1), 1))
138 if ((bitlen
= *(cp
+ 1)) == 0)
140 return(((bitlen
+ 7) / 8) + 1);
145 static const u_char
*
146 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
149 register const u_char
*rp
= NULL
;
150 register int compress
= 0;
153 int data_size
= snapend
- bp
;
155 if ((l
= labellen(cp
)) == (u_int
)-1)
160 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
166 while (i
&& cp
< snapend
) {
167 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
174 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
175 if ((l
= labellen(cp
)) == (u_int
)-1)
183 * If we've looked at every character in
184 * the message, this pointer will make
185 * us look at some character again,
186 * which means we're looping.
188 if (chars_processed
>= data_size
) {
194 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
195 elt
= (i
& ~INDIR_MASK
);
197 case EDNS0_ELT_BITLABEL
:
198 if (blabel_print(cp
) == NULL
)
203 printf("<ELT %d>", elt
);
207 if (fn_printn(cp
, l
, snapend
))
212 chars_processed
+= l
;
214 if ((l
= labellen(cp
)) == (u_int
)-1)
228 /* print a <character-string> */
229 static const u_char
*
230 ns_cprint(register const u_char
*cp
)
237 if (fn_printn(cp
, i
, snapend
))
242 struct tok ns_type2str
[] = {
247 { T_CNAME
, "CNAME" },
255 { T_HINFO
, "HINFO" },
256 { T_MINFO
, "MINFO" },
260 { T_AFSDB
, "AFSDB" },
265 { T_NSAP_PTR
, "NSAP_PTR" },
274 { T_NIMLOC
, "NIMLOC" },
277 { T_NAPTR
, "NAPTR" },
279 { T_DNAME
, "DNAME" },
281 { T_UINFO
, "UINFO" },
284 { T_UNSPEC
, "UNSPEC" },
285 { T_UNSPECA
, "UNSPECA" },
290 { T_MAILB
, "MAILB" },
291 { T_MAILA
, "MAILA" },
296 struct tok ns_class2str
[] = {
297 { C_IN
, "IN" }, /* Not used */
298 { C_CHAOS
, "CHAOS" },
305 static const u_char
*
306 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
308 register const u_char
*np
= cp
;
313 if (cp
== NULL
|| !TTEST2(*cp
, 4))
316 /* print the qtype and qclass (if it's not IN) */
319 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
323 printf(" %s", tok2str(ns_class2str
, "(Class %d)", i
));
326 cp
= ns_nprint(np
, bp
);
327 return(cp
? cp
+ 4 : NULL
);
331 static const u_char
*
332 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
334 register u_int
class;
335 register u_short typ
, len
;
336 register const u_char
*rp
;
340 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
345 if (cp
== NULL
|| !TTEST2(*cp
, 10))
348 /* print the type/qtype and class (if it's not IN) */
353 if (class != C_IN
&& typ
!= T_OPT
)
354 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
364 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
370 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
372 printf(" %s", ipaddr_string(cp
));
382 if (ns_nprint(cp
, bp
) == NULL
)
390 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
393 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
395 if (!TTEST2(*cp
, 5 * 4))
397 printf(" %u", EXTRACT_32BITS(cp
));
399 printf(" %u", EXTRACT_32BITS(cp
));
401 printf(" %u", EXTRACT_32BITS(cp
));
403 printf(" %u", EXTRACT_32BITS(cp
));
405 printf(" %u", EXTRACT_32BITS(cp
));
412 if (ns_nprint(cp
+ 2, bp
) == NULL
)
414 printf(" %d", EXTRACT_16BITS(cp
));
424 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
426 printf(" %s", ip6addr_string(cp
));
437 pbyte
= (pbit
& ~7) / 8;
439 printf(" %u(bad plen)", pbit
);
441 } else if (pbit
< 128) {
442 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
444 memset(&a
, 0, sizeof(a
));
445 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
446 printf(" %u %s", pbit
, ip6addr_string(&a
));
450 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
458 printf(" UDPsize=%u", class);
461 case T_UNSPECA
: /* One long string */
462 if (!TTEST2(*cp
, len
))
464 if (fn_printn(cp
, len
, snapend
))
470 if (cp
+ len
> snapend
)
475 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
480 printf(" fudge=%u", EXTRACT_16BITS(cp
));
484 printf(" maclen=%u", EXTRACT_16BITS(cp
));
485 cp
+= 2 + EXTRACT_16BITS(cp
);
488 printf(" origid=%u", EXTRACT_16BITS(cp
));
492 printf(" error=%u", EXTRACT_16BITS(cp
));
496 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
500 return (rp
); /* XXX This isn't always right */
504 ns_print(register const u_char
*bp
, u_int length
)
506 register const HEADER
*np
;
507 register int qdcount
, ancount
, nscount
, arcount
;
508 register const u_char
*cp
;
510 np
= (const HEADER
*)bp
;
512 /* get the byte-order right */
513 qdcount
= ntohs(np
->qdcount
);
514 ancount
= ntohs(np
->ancount
);
515 nscount
= ntohs(np
->nscount
);
516 arcount
= ntohs(np
->arcount
);
519 /* this is a response */
520 printf(" %d%s%s%s%s%s%s",
522 ns_ops
[DNS_OPCODE(np
)],
523 ns_resp
[DNS_RCODE(np
)],
524 DNS_AA(np
)? "*" : "",
525 DNS_RA(np
)? "" : "-",
526 DNS_TC(np
)? "|" : "",
527 DNS_CD(np
)? "%" : "");
530 printf(" [%dq]", qdcount
);
531 /* Print QUESTION section on -vv */
532 cp
= (const u_char
*)(np
+ 1);
534 if (qdcount
< ntohs(np
->qdcount
) - 1)
537 fputs(" q:", stdout
);
538 if ((cp
= ns_qprint(cp
, bp
)) == NULL
)
541 if ((cp
= ns_nskip(cp
)) == NULL
)
543 cp
+= 4; /* skip QTYPE and QCLASS */
546 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
548 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
550 while (cp
< snapend
&& ancount
--) {
552 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
558 /* Print NS and AR sections on -vv */
560 if (cp
< snapend
&& nscount
--) {
561 fputs(" ns:", stdout
);
562 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
564 while (cp
< snapend
&& nscount
--) {
566 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
572 if (cp
< snapend
&& arcount
--) {
573 fputs(" ar:", stdout
);
574 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
576 while (cp
< snapend
&& arcount
--) {
578 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
587 /* this is a request */
588 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
589 DNS_RD(np
) ? "+" : "",
590 DNS_AD(np
) ? "$" : "");
593 if (*(((u_short
*)np
)+1) & htons(0x6cf))
594 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
596 if (DNS_OPCODE(np
) == IQUERY
) {
598 printf(" [%dq]", qdcount
);
600 printf(" [%da]", ancount
);
604 printf(" [%da]", ancount
);
606 printf(" [%dq]", qdcount
);
609 printf(" [%dn]", nscount
);
611 printf(" [%dau]", arcount
);
613 cp
= (const u_char
*)(np
+ 1);
615 cp
= ns_qprint(cp
, (const u_char
*)np
);
618 while (cp
< snapend
&& qdcount
--) {
619 cp
= ns_qprint((const u_char
*)cp
,
628 /* Print remaining sections on -vv */
631 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
633 while (cp
< snapend
&& ancount
--) {
635 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
641 if (cp
< snapend
&& nscount
--) {
642 fputs(" ns:", stdout
);
643 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
645 while (nscount
-- && cp
< snapend
) {
647 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
653 if (cp
< snapend
&& arcount
--) {
654 fputs(" ar:", stdout
);
655 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
657 while (cp
< snapend
&& arcount
--) {
659 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
667 printf(" (%d)", length
);