]>
The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
74e635477bd60295848396bde3d414fbef61a5b5
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.60 2000-12-30 09:07:40 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
;
189 if ((bitlen
= *cp
) == 0)
191 bytelen
= (bitlen
+ 7) / 8;
192 if ((lim
= cp
+ 1 + bytelen
) > snapend
) {
197 /* print the bit string as a hex string */
199 for (bitp
= cp
+ 1; bitp
< lim
; bitp
++)
200 printf("%02x", *bitp
);
203 printf("/%d]", bitlen
);
205 return(truncated
? NULL
: lim
);
209 labellen(const u_char
*cp
)
216 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
219 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
)
221 if (cp
+ 1 >= snapend
)
223 if ((bitlen
= *(cp
+ 1)) == 0)
225 return(((bitlen
+ 7) / 8) + 1);
230 static const u_char
*
231 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
234 register const u_char
*rp
;
235 register int compress
= 0;
238 int data_size
= snapend
- bp
;
240 if ((l
= labellen(cp
)) < 0)
245 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
251 while (i
&& cp
< snapend
) {
252 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
257 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
260 if ((l
= labellen(cp
)) < 0)
266 * If we've looked at every character in
267 * the message, this pointer will make
268 * us look at some character again,
269 * which means we're looping.
271 if (chars_processed
>= data_size
) {
277 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
278 elt
= (i
& ~INDIR_MASK
);
280 case EDNS0_ELT_BITLABEL
:
285 printf("<ELT %d>", elt
);
289 if (fn_printn(cp
, l
, snapend
))
294 chars_processed
+= l
;
296 if (cp
>= snapend
|| (l
= labellen(cp
)) < 0)
308 /* print a <character-string> */
309 static const u_char
*
310 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
317 (void)fn_printn(cp
, i
, snapend
);
321 static struct tok type2str
[] = {
326 { T_CNAME
, "CNAME" },
334 { T_HINFO
, "HINFO" },
335 { T_MINFO
, "MINFO" },
339 { T_AFSDB
, "AFSDB" },
344 { T_NSAP_PTR
, "NSAP_PTR" },
353 { T_NIMLOC
, "NIMLOC " },
356 { T_NAPTR
, "NAPTR " },
358 { T_DNAME
, "DNAME " },
360 { T_UINFO
, "UINFO" },
363 { T_UNSPEC
, "UNSPEC" },
364 { T_UNSPECA
, "UNSPECA" },
366 { T_MAILB
, "MAILB" },
367 { T_MAILA
, "MAILA" },
372 static struct tok class2str
[] = {
373 { C_IN
, "IN" }, /* Not used */
374 { C_CHAOS
, "CHAOS)" },
381 static const u_char
*
382 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
384 register const u_char
*np
= cp
;
387 cp
= ns_nskip(cp
, bp
);
389 if (cp
+ 4 > snapend
|| cp
== NULL
)
392 /* print the qtype and qclass (if it's not IN) */
395 printf(" %s", tok2str(type2str
, "Type%d", i
));
399 printf(" %s", tok2str(class2str
, "(Class %d)", i
));
402 cp
= ns_nprint(np
, bp
);
403 return(cp
? cp
+ 4 : NULL
);
407 static const u_char
*
408 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
410 register u_int
class;
411 register u_short typ
, len
;
412 register const u_char
*rp
;
416 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
419 cp
= ns_nskip(cp
, bp
);
421 if (cp
+ 10 > snapend
|| cp
== NULL
)
424 /* print the type/qtype and class (if it's not IN) */
429 if (class != C_IN
&& typ
!= T_OPT
)
430 printf(" %s", tok2str(class2str
, "(Class %d)", class));
440 printf(" %s", tok2str(type2str
, "Type%d", typ
));
446 if (cp
+ sizeof(struct in_addr
) > snapend
)
448 printf(" %s", ipaddr_string(cp
));
458 if (ns_nprint(cp
, bp
) == NULL
)
466 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
469 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
471 if (cp
+ 5 * 4 > snapend
)
473 printf(" %u", EXTRACT_32BITS(cp
));
475 printf(" %u", EXTRACT_32BITS(cp
));
477 printf(" %u", EXTRACT_32BITS(cp
));
479 printf(" %u", EXTRACT_32BITS(cp
));
481 printf(" %u", EXTRACT_32BITS(cp
));
486 if (cp
+ 2 > snapend
)
488 if (ns_nprint(cp
+ 2, bp
) == NULL
)
490 printf(" %d", EXTRACT_16BITS(cp
));
495 (void)ns_cprint(cp
, bp
);
500 if (cp
+ sizeof(struct in6_addr
) > snapend
)
502 printf(" %s", ip6addr_string(cp
));
511 pbyte
= (pbit
& ~7) / 8;
513 printf(" %u(bad plen)", pbit
);
515 } else if (pbit
< 128) {
516 memset(&a
, 0, sizeof(a
));
517 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
518 printf(" %u %s", pbit
, ip6addr_string(&a
));
522 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
530 printf(" UDPsize=%u", class);
533 case T_UNSPECA
: /* One long string */
534 if (cp
+ len
> snapend
)
536 fn_printn(cp
, len
, snapend
);
539 return (rp
); /* XXX This isn't always right */
543 ns_print(register const u_char
*bp
, u_int length
)
545 register const HEADER
*np
;
546 register int qdcount
, ancount
, nscount
, arcount
;
547 register const u_char
*cp
;
549 np
= (const HEADER
*)bp
;
550 /* get the byte-order right */
551 qdcount
= ntohs(np
->qdcount
);
552 ancount
= ntohs(np
->ancount
);
553 nscount
= ntohs(np
->nscount
);
554 arcount
= ntohs(np
->arcount
);
557 /* this is a response */
558 printf(" %d%s%s%s%s%s%s",
560 ns_ops
[DNS_OPCODE(np
)],
561 ns_resp
[DNS_RCODE(np
)],
562 DNS_AA(np
)? "*" : "",
563 DNS_RA(np
)? "" : "-",
564 DNS_TC(np
)? "|" : "",
565 DNS_CD(np
)? "%" : "");
568 printf(" [%dq]", qdcount
);
569 /* Print QUESTION section on -vv */
571 fputs(" q:", stdout
);
572 if ((cp
= ns_qprint((const u_char
*)(np
+ 1), bp
))
576 if ((cp
= ns_nskip((const u_char
*)(np
+ 1), bp
))
581 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
583 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
585 while (ancount
-- && cp
< snapend
) {
587 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
591 /* Print NS and AR sections on -vv */
593 if (nscount
-- && cp
< snapend
) {
594 fputs(" ns:", stdout
);
595 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
597 while (nscount
-- && cp
< snapend
) {
599 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
603 if (arcount
-- && cp
< snapend
) {
604 fputs(" ar:", stdout
);
605 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
607 while (arcount
-- && cp
< snapend
) {
609 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
616 /* this is a request */
617 printf(" %d%s%s%s", ntohs(np
->id
), ns_ops
[DNS_OPCODE(np
)],
618 DNS_RD(np
) ? "+" : "",
619 DNS_AD(np
) ? "$" : "");
622 if (*(((u_short
*)np
)+1) & htons(0x6cf))
623 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
625 if (DNS_OPCODE(np
) == IQUERY
) {
627 printf(" [%dq]", qdcount
);
629 printf(" [%da]", ancount
);
633 printf(" [%da]", ancount
);
635 printf(" [%dq]", qdcount
);
638 printf(" [%dn]", nscount
);
640 printf(" [%dau]", arcount
);
643 cp
= ns_qprint((const u_char
*)(np
+ 1),
645 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
647 while (qdcount
-- && cp
< snapend
) {
648 cp
= ns_qprint((const u_char
*)cp
,
650 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
655 /* Print remaining sections on -vv */
658 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
660 while (ancount
-- && cp
< snapend
) {
662 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
666 if (nscount
-- && cp
< snapend
) {
667 fputs(" ns:", stdout
);
668 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
670 while (nscount
-- && cp
< snapend
) {
672 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
676 if (arcount
-- && cp
< snapend
) {
677 fputs(" ar:", stdout
);
678 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
680 while (arcount
-- && cp
< snapend
) {
682 if ((cp
= ns_rprint(cp
, bp
)) == NULL
)
688 printf(" (%d)", length
);