]>
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.69 2001-02-03 05:04:33 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", " op5", " op6", " op7",
53 " op8", " updataA", " updateD", " updateDA",
54 " updateM", " updateMA", " zoneInit", " zoneRef",
57 static char *ns_resp
[] = {
58 "", " FormErr", " ServFail", " NXDomain",
59 " NotImp", " Refused", " Resp6", " Resp7",
60 " Resp8", " Resp9", " Resp10", " 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" },
288 { T_MAILB
, "MAILB" },
289 { T_MAILA
, "MAILA" },
294 struct tok ns_class2str
[] = {
295 { C_IN
, "IN" }, /* Not used */
296 { C_CHAOS
, "CHAOS" },
303 static const u_char
*
304 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
306 register const u_char
*np
= cp
;
309 cp
= ns_nskip(cp
, bp
);
311 if (cp
+ 4 > snapend
|| cp
== NULL
)
314 /* print the qtype and qclass (if it's not IN) */
317 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
321 printf(" %s", tok2str(ns_class2str
, "(Class %d)", i
));
324 cp
= ns_nprint(np
, bp
);
325 return(cp
? cp
+ 4 : NULL
);
329 static const u_char
*
330 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
332 register u_int
class;
333 register u_short typ
, len
;
334 register const u_char
*rp
;
338 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
341 cp
= ns_nskip(cp
, bp
);
343 if (cp
+ 10 > snapend
|| cp
== NULL
)
346 /* print the type/qtype and class (if it's not IN) */
351 if (class != C_IN
&& typ
!= T_OPT
)
352 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
362 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
368 if (cp
+ sizeof(struct in_addr
) > snapend
)
370 printf(" %s", ipaddr_string(cp
));
380 if (ns_nprint(cp
, bp
) == NULL
)
388 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
391 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
393 if (cp
+ 5 * 4 > snapend
)
395 printf(" %u", EXTRACT_32BITS(cp
));
397 printf(" %u", EXTRACT_32BITS(cp
));
399 printf(" %u", EXTRACT_32BITS(cp
));
401 printf(" %u", EXTRACT_32BITS(cp
));
403 printf(" %u", EXTRACT_32BITS(cp
));
408 if (cp
+ 2 > snapend
)
410 if (ns_nprint(cp
+ 2, bp
) == NULL
)
412 printf(" %d", EXTRACT_16BITS(cp
));
417 (void)ns_cprint(cp
, bp
);
422 if (cp
+ sizeof(struct in6_addr
) > snapend
)
424 printf(" %s", ip6addr_string(cp
));
433 pbyte
= (pbit
& ~7) / 8;
435 printf(" %u(bad plen)", pbit
);
437 } else if (pbit
< 128) {
438 memset(&a
, 0, sizeof(a
));
439 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
440 printf(" %u %s", pbit
, ip6addr_string(&a
));
444 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
452 printf(" UDPsize=%u", class);
455 case T_UNSPECA
: /* One long string */
456 if (cp
+ len
> snapend
)
458 fn_printn(cp
, len
, snapend
);
461 return (rp
); /* XXX This isn't always right */
465 ns_print(register const u_char
*bp
, u_int length
)
467 register const HEADER
*np
;
468 register int qdcount
, ancount
, nscount
, arcount
;
469 register const u_char
*cp
= NULL
;
471 np
= (const HEADER
*)bp
;
472 /* get the byte-order right */
473 qdcount
= ntohs(np
->qdcount
);
474 ancount
= ntohs(np
->ancount
);
475 nscount
= ntohs(np
->nscount
);
476 arcount
= ntohs(np
->arcount
);
479 /* this is a response */
480 printf(" %d%s%s%s%s%s%s",
482 ns_ops
[DNS_OPCODE(np
)],
483 ns_resp
[DNS_RCODE(np
)],
484 DNS_AA(np
)? "*" : "",
485 DNS_RA(np
)? "" : "-",
486 DNS_TC(np
)? "|" : "",
487 DNS_CD(np
)? "%" : "");
490 printf(" [%dq]", qdcount
);
491 /* Print QUESTION section on -vv */
493 if (qdcount
< ntohs(np
->qdcount
) - 1)
496 fputs(" q:", stdout
);
497 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
501 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
504 cp
+= 4; /* skip QTYPE and QCLASS */
507 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
509 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
511 while (cp
< snapend
&& ancount
--) {
513 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
519 /* Print NS and AR sections on -vv */
521 if (cp
< snapend
&& nscount
--) {
522 fputs(" ns:", stdout
);
523 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
525 while (cp
< snapend
&& nscount
--) {
527 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
533 if (cp
< snapend
&& arcount
--) {
534 fputs(" ar:", stdout
);
535 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
537 while (cp
< snapend
&& arcount
--) {
539 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
548 /* this is a request */
549 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
550 DNS_RD(np
) ? "+" : "",
551 DNS_AD(np
) ? "$" : "");
554 if (*(((u_short
*)np
)+1) & htons(0x6cf))
555 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
557 if (DNS_OPCODE(np
) == IQUERY
) {
559 printf(" [%dq]", qdcount
);
561 printf(" [%da]", ancount
);
565 printf(" [%da]", ancount
);
567 printf(" [%dq]", qdcount
);
570 printf(" [%dn]", nscount
);
572 printf(" [%dau]", arcount
);
575 cp
= ns_qprint((const u_char
*)(np
+ 1),
579 while (cp
< snapend
&& qdcount
--) {
580 cp
= ns_qprint((const u_char
*)cp
,
589 /* Print remaining sections on -vv */
592 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
594 while (cp
< snapend
&& ancount
--) {
596 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
602 if (cp
< snapend
&& nscount
--) {
603 fputs(" ns:", stdout
);
604 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
606 while (nscount
-- && cp
< snapend
) {
608 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
614 if (cp
< snapend
&& arcount
--) {
615 fputs(" ar:", stdout
);
616 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
618 while (cp
< snapend
&& arcount
--) {
620 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
628 printf(" (%d)", length
);