4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
27 static const char rcsid
[] _U_
=
28 "@(#) Header: /tcpdump/master/tcpdump/print-domain.c,v 1.89.2.8 2007/02/13 19:19:27 guy Exp (LBL)";
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
38 #include <tcpdump-stdinc.h>
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "extract.h" /* must come after interface.h */
49 static const char *ns_ops
[] = {
50 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
51 " op8", " updataA", " updateD", " updateDA",
52 " updateM", " updateMA", " zoneInit", " zoneRef",
55 static const char *ns_resp
[] = {
56 "", " FormErr", " ServFail", " NXDomain",
57 " NotImp", " Refused", " YXDomain", " YXRRSet",
58 " NXRRSet", " NotAuth", " NotZone", " Resp11",
59 " Resp12", " Resp13", " Resp14", " NoChange",
62 /* skip over a domain name */
64 ns_nskip(register const u_char
*cp
)
72 if ((i
& INDIR_MASK
) == INDIR_MASK
)
74 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
77 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
78 return(NULL
); /* unknown ELT */
81 if ((bitlen
= *cp
++) == 0)
83 bytelen
= (bitlen
+ 7) / 8;
94 /* print a <domain-name> */
96 blabel_print(const u_char
*cp
)
99 const u_char
*bitp
, *lim
;
104 if ((bitlen
= *cp
) == 0)
106 slen
= (bitlen
+ 3) / 4;
109 /* print the bit string as a hex string */
111 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
113 printf("%02x", *bitp
);
118 printf("%02x", tc
& (0xff << (8 - b
)));
122 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
124 printf("/%d]", bitlen
);
127 printf(".../%d]", bitlen
);
132 labellen(const u_char
*cp
)
139 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
141 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
142 printf("<ELT %d>", elt
);
145 if (!TTEST2(*(cp
+ 1), 1))
147 if ((bitlen
= *(cp
+ 1)) == 0)
149 return(((bitlen
+ 7) / 8) + 1);
154 static const u_char
*
155 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
158 register const u_char
*rp
= NULL
;
159 register int compress
= 0;
162 int data_size
= snapend
- bp
;
164 if ((l
= labellen(cp
)) == (u_int
)-1)
169 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
175 while (i
&& cp
< snapend
) {
176 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
183 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
184 if ((l
= labellen(cp
)) == (u_int
)-1)
192 * If we've looked at every character in
193 * the message, this pointer will make
194 * us look at some character again,
195 * which means we're looping.
197 if (chars_processed
>= data_size
) {
203 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
204 elt
= (i
& ~INDIR_MASK
);
206 case EDNS0_ELT_BITLABEL
:
207 if (blabel_print(cp
) == NULL
)
212 printf("<ELT %d>", elt
);
216 if (fn_printn(cp
, l
, snapend
))
221 chars_processed
+= l
;
223 if ((l
= labellen(cp
)) == (u_int
)-1)
237 /* print a <character-string> */
238 static const u_char
*
239 ns_cprint(register const u_char
*cp
)
246 if (fn_printn(cp
, i
, snapend
))
251 /* http://www.iana.org/assignments/dns-parameters */
252 struct tok ns_type2str
[] = {
253 { T_A
, "A" }, /* RFC 1035 */
254 { T_NS
, "NS" }, /* RFC 1035 */
255 { T_MD
, "MD" }, /* RFC 1035 */
256 { T_MF
, "MF" }, /* RFC 1035 */
257 { T_CNAME
, "CNAME" }, /* RFC 1035 */
258 { T_SOA
, "SOA" }, /* RFC 1035 */
259 { T_MB
, "MB" }, /* RFC 1035 */
260 { T_MG
, "MG" }, /* RFC 1035 */
261 { T_MR
, "MR" }, /* RFC 1035 */
262 { T_NULL
, "NULL" }, /* RFC 1035 */
263 { T_WKS
, "WKS" }, /* RFC 1035 */
264 { T_PTR
, "PTR" }, /* RFC 1035 */
265 { T_HINFO
, "HINFO" }, /* RFC 1035 */
266 { T_MINFO
, "MINFO" }, /* RFC 1035 */
267 { T_MX
, "MX" }, /* RFC 1035 */
268 { T_TXT
, "TXT" }, /* RFC 1035 */
269 { T_RP
, "RP" }, /* RFC 1183 */
270 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
271 { T_X25
, "X25" }, /* RFC 1183 */
272 { T_ISDN
, "ISDN" }, /* RFC 1183 */
273 { T_RT
, "RT" }, /* RFC 1183 */
274 { T_NSAP
, "NSAP" }, /* RFC 1706 */
275 { T_NSAP_PTR
, "NSAP_PTR" },
276 { T_SIG
, "SIG" }, /* RFC 2535 */
277 { T_KEY
, "KEY" }, /* RFC 2535 */
278 { T_PX
, "PX" }, /* RFC 2163 */
279 { T_GPOS
, "GPOS" }, /* RFC 1712 */
280 { T_AAAA
, "AAAA" }, /* RFC 1886 */
281 { T_LOC
, "LOC" }, /* RFC 1876 */
282 { T_NXT
, "NXT" }, /* RFC 2535 */
283 { T_EID
, "EID" }, /* Nimrod */
284 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
285 { T_SRV
, "SRV" }, /* RFC 2782 */
286 { T_ATMA
, "ATMA" }, /* ATM Forum */
287 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
288 { T_KX
, "KX" }, /* RFC 2230 */
289 { T_CERT
, "CERT" }, /* RFC 2538 */
290 { T_A6
, "A6" }, /* RFC 2874 */
291 { T_DNAME
, "DNAME" }, /* RFC 2672 */
293 { T_OPT
, "OPT" }, /* RFC 2671 */
294 { T_APL
, "APL" }, /* RFC 3123 */
295 { T_DS
, "DS" }, /* RFC 4034 */
296 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
297 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
298 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
299 { T_NSEC
, "NSEC" }, /* RFC 4034 */
300 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
301 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
302 { T_UINFO
, "UINFO" },
305 { T_UNSPEC
, "UNSPEC" },
306 { T_UNSPECA
, "UNSPECA" },
307 { T_TKEY
, "TKEY" }, /* RFC 2930 */
308 { T_TSIG
, "TSIG" }, /* RFC 2845 */
309 { T_IXFR
, "IXFR" }, /* RFC 1995 */
310 { T_AXFR
, "AXFR" }, /* RFC 1035 */
311 { T_MAILB
, "MAILB" }, /* RFC 1035 */
312 { T_MAILA
, "MAILA" }, /* RFC 1035 */
317 struct tok ns_class2str
[] = {
318 { C_IN
, "IN" }, /* Not used */
319 { C_CHAOS
, "CHAOS" },
326 static const u_char
*
327 ns_qprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
329 register const u_char
*np
= cp
;
330 register u_int i
, class;
334 if (cp
== NULL
|| !TTEST2(*cp
, 4))
337 /* print the qtype */
338 i
= EXTRACT_16BITS(cp
);
340 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
341 /* print the qclass (if it's not IN) */
342 i
= EXTRACT_16BITS(cp
);
349 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
358 cp
= ns_nprint(np
, bp
);
359 return(cp
? cp
+ 4 : NULL
);
363 static const u_char
*
364 ns_rprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
366 register u_int i
, class, opt_flags
= 0;
367 register u_short typ
, len
;
368 register const u_char
*rp
;
372 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
377 if (cp
== NULL
|| !TTEST2(*cp
, 10))
380 /* print the type/qtype */
381 typ
= EXTRACT_16BITS(cp
);
383 /* print the class (if it's not IN and the type isn't OPT) */
384 i
= EXTRACT_16BITS(cp
);
387 class = (i
& ~C_CACHE_FLUSH
);
390 if (class != C_IN
&& typ
!= T_OPT
)
391 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
393 if (i
& C_CACHE_FLUSH
)
394 printf(" (Cache flush)");
399 /* if T_OPT, save opt_flags */
401 opt_flags
= EXTRACT_16BITS(cp
);
402 /* ignore rest of ttl */
405 len
= EXTRACT_16BITS(cp
);
410 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
416 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
418 printf(" %s", ipaddr_string(cp
));
428 if (ns_nprint(cp
, bp
) == NULL
)
436 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
439 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
441 if (!TTEST2(*cp
, 5 * 4))
443 printf(" %u", EXTRACT_32BITS(cp
));
445 printf(" %u", EXTRACT_32BITS(cp
));
447 printf(" %u", EXTRACT_32BITS(cp
));
449 printf(" %u", EXTRACT_32BITS(cp
));
451 printf(" %u", EXTRACT_32BITS(cp
));
458 if (ns_nprint(cp
+ 2, bp
) == NULL
)
460 printf(" %d", EXTRACT_16BITS(cp
));
477 if (ns_nprint(cp
+ 6, bp
) == NULL
)
479 printf(":%d %d %d", EXTRACT_16BITS(cp
+ 4),
480 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2));
485 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
487 printf(" %s", ip6addr_string(cp
));
498 pbyte
= (pbit
& ~7) / 8;
500 printf(" %u(bad plen)", pbit
);
502 } else if (pbit
< 128) {
503 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
505 memset(&a
, 0, sizeof(a
));
506 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
507 printf(" %u %s", pbit
, ip6addr_string(&a
));
511 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
519 printf(" UDPsize=%u", class);
520 if (opt_flags
& 0x8000)
524 case T_UNSPECA
: /* One long string */
525 if (!TTEST2(*cp
, len
))
527 if (fn_printn(cp
, len
, snapend
))
533 if (cp
+ len
> snapend
)
538 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
543 printf(" fudge=%u", EXTRACT_16BITS(cp
));
547 printf(" maclen=%u", EXTRACT_16BITS(cp
));
548 cp
+= 2 + EXTRACT_16BITS(cp
);
551 printf(" origid=%u", EXTRACT_16BITS(cp
));
555 printf(" error=%u", EXTRACT_16BITS(cp
));
559 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
563 return (rp
); /* XXX This isn't always right */
567 ns_print(register const u_char
*bp
, u_int length
, int is_mdns
)
569 register const HEADER
*np
;
570 register int qdcount
, ancount
, nscount
, arcount
;
571 register const u_char
*cp
;
574 np
= (const HEADER
*)bp
;
576 /* get the byte-order right */
577 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
578 ancount
= EXTRACT_16BITS(&np
->ancount
);
579 nscount
= EXTRACT_16BITS(&np
->nscount
);
580 arcount
= EXTRACT_16BITS(&np
->arcount
);
583 /* this is a response */
584 printf("%d%s%s%s%s%s%s",
585 EXTRACT_16BITS(&np
->id
),
586 ns_ops
[DNS_OPCODE(np
)],
587 ns_resp
[DNS_RCODE(np
)],
588 DNS_AA(np
)? "*" : "",
589 DNS_RA(np
)? "" : "-",
590 DNS_TC(np
)? "|" : "",
591 DNS_AD(np
)? "$" : "");
594 printf(" [%dq]", qdcount
);
595 /* Print QUESTION section on -vv */
596 cp
= (const u_char
*)(np
+ 1);
598 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
601 fputs(" q:", stdout
);
602 if ((cp
= ns_qprint(cp
, bp
, is_mdns
)) == NULL
)
605 if ((cp
= ns_nskip(cp
)) == NULL
)
607 cp
+= 4; /* skip QTYPE and QCLASS */
610 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
612 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
614 while (cp
< snapend
&& ancount
--) {
616 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
622 /* Print NS and AR sections on -vv */
624 if (cp
< snapend
&& nscount
--) {
625 fputs(" ns:", stdout
);
626 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
628 while (cp
< snapend
&& nscount
--) {
630 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
636 if (cp
< snapend
&& arcount
--) {
637 fputs(" ar:", stdout
);
638 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
640 while (cp
< snapend
&& arcount
--) {
642 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
651 /* this is a request */
652 printf("%d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
653 DNS_RD(np
) ? "+" : "",
654 DNS_CD(np
) ? "%" : "");
657 b2
= EXTRACT_16BITS(((u_short
*)np
)+1);
659 printf(" [b2&3=0x%x]", b2
);
661 if (DNS_OPCODE(np
) == IQUERY
) {
663 printf(" [%dq]", qdcount
);
665 printf(" [%da]", ancount
);
669 printf(" [%da]", ancount
);
671 printf(" [%dq]", qdcount
);
674 printf(" [%dn]", nscount
);
676 printf(" [%dau]", arcount
);
678 cp
= (const u_char
*)(np
+ 1);
680 cp
= ns_qprint(cp
, (const u_char
*)np
, is_mdns
);
683 while (cp
< snapend
&& qdcount
--) {
684 cp
= ns_qprint((const u_char
*)cp
,
694 /* Print remaining sections on -vv */
697 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
699 while (cp
< snapend
&& ancount
--) {
701 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
707 if (cp
< snapend
&& nscount
--) {
708 fputs(" ns:", stdout
);
709 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
711 while (nscount
-- && cp
< snapend
) {
713 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
719 if (cp
< snapend
&& arcount
--) {
720 fputs(" ar:", stdout
);
721 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
723 while (cp
< snapend
&& arcount
--) {
725 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
733 printf(" (%d)", length
);