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
[] _U_
=
24 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.89.2.8 2007/02/13 19:19:27 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
)
65 if ((i
& INDIR_MASK
) == INDIR_MASK
)
67 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
70 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
71 return(NULL
); /* unknown ELT */
74 if ((bitlen
= *cp
++) == 0)
76 bytelen
= (bitlen
+ 7) / 8;
87 /* print a <domain-name> */
89 blabel_print(const u_char
*cp
)
92 const u_char
*bitp
, *lim
;
97 if ((bitlen
= *cp
) == 0)
99 slen
= (bitlen
+ 3) / 4;
102 /* print the bit string as a hex string */
104 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
106 printf("%02x", *bitp
);
111 printf("%02x", tc
& (0xff << (8 - b
)));
115 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
117 printf("/%d]", bitlen
);
120 printf(".../%d]", bitlen
);
125 labellen(const u_char
*cp
)
132 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
134 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
135 printf("<ELT %d>", elt
);
138 if (!TTEST2(*(cp
+ 1), 1))
140 if ((bitlen
= *(cp
+ 1)) == 0)
142 return(((bitlen
+ 7) / 8) + 1);
147 static const u_char
*
148 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
151 register const u_char
*rp
= NULL
;
152 register int compress
= 0;
155 int data_size
= snapend
- bp
;
157 if ((l
= labellen(cp
)) == (u_int
)-1)
162 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
168 while (i
&& cp
< snapend
) {
169 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
176 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
177 if ((l
= labellen(cp
)) == (u_int
)-1)
185 * If we've looked at every character in
186 * the message, this pointer will make
187 * us look at some character again,
188 * which means we're looping.
190 if (chars_processed
>= data_size
) {
196 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
197 elt
= (i
& ~INDIR_MASK
);
199 case EDNS0_ELT_BITLABEL
:
200 if (blabel_print(cp
) == NULL
)
205 printf("<ELT %d>", elt
);
209 if (fn_printn(cp
, l
, snapend
))
214 chars_processed
+= l
;
216 if ((l
= labellen(cp
)) == (u_int
)-1)
230 /* print a <character-string> */
231 static const u_char
*
232 ns_cprint(register const u_char
*cp
)
239 if (fn_printn(cp
, i
, snapend
))
244 /* http://www.iana.org/assignments/dns-parameters */
245 struct tok ns_type2str
[] = {
246 { T_A
, "A" }, /* RFC 1035 */
247 { T_NS
, "NS" }, /* RFC 1035 */
248 { T_MD
, "MD" }, /* RFC 1035 */
249 { T_MF
, "MF" }, /* RFC 1035 */
250 { T_CNAME
, "CNAME" }, /* RFC 1035 */
251 { T_SOA
, "SOA" }, /* RFC 1035 */
252 { T_MB
, "MB" }, /* RFC 1035 */
253 { T_MG
, "MG" }, /* RFC 1035 */
254 { T_MR
, "MR" }, /* RFC 1035 */
255 { T_NULL
, "NULL" }, /* RFC 1035 */
256 { T_WKS
, "WKS" }, /* RFC 1035 */
257 { T_PTR
, "PTR" }, /* RFC 1035 */
258 { T_HINFO
, "HINFO" }, /* RFC 1035 */
259 { T_MINFO
, "MINFO" }, /* RFC 1035 */
260 { T_MX
, "MX" }, /* RFC 1035 */
261 { T_TXT
, "TXT" }, /* RFC 1035 */
262 { T_RP
, "RP" }, /* RFC 1183 */
263 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
264 { T_X25
, "X25" }, /* RFC 1183 */
265 { T_ISDN
, "ISDN" }, /* RFC 1183 */
266 { T_RT
, "RT" }, /* RFC 1183 */
267 { T_NSAP
, "NSAP" }, /* RFC 1706 */
268 { T_NSAP_PTR
, "NSAP_PTR" },
269 { T_SIG
, "SIG" }, /* RFC 2535 */
270 { T_KEY
, "KEY" }, /* RFC 2535 */
271 { T_PX
, "PX" }, /* RFC 2163 */
272 { T_GPOS
, "GPOS" }, /* RFC 1712 */
273 { T_AAAA
, "AAAA" }, /* RFC 1886 */
274 { T_LOC
, "LOC" }, /* RFC 1876 */
275 { T_NXT
, "NXT" }, /* RFC 2535 */
276 { T_EID
, "EID" }, /* Nimrod */
277 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
278 { T_SRV
, "SRV" }, /* RFC 2782 */
279 { T_ATMA
, "ATMA" }, /* ATM Forum */
280 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
281 { T_KX
, "KX" }, /* RFC 2230 */
282 { T_CERT
, "CERT" }, /* RFC 2538 */
283 { T_A6
, "A6" }, /* RFC 2874 */
284 { T_DNAME
, "DNAME" }, /* RFC 2672 */
286 { T_OPT
, "OPT" }, /* RFC 2671 */
287 { T_APL
, "APL" }, /* RFC 3123 */
288 { T_DS
, "DS" }, /* RFC 4034 */
289 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
290 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
291 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
292 { T_NSEC
, "NSEC" }, /* RFC 4034 */
293 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
294 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
295 { T_UINFO
, "UINFO" },
298 { T_UNSPEC
, "UNSPEC" },
299 { T_UNSPECA
, "UNSPECA" },
300 { T_TKEY
, "TKEY" }, /* RFC 2930 */
301 { T_TSIG
, "TSIG" }, /* RFC 2845 */
302 { T_IXFR
, "IXFR" }, /* RFC 1995 */
303 { T_AXFR
, "AXFR" }, /* RFC 1035 */
304 { T_MAILB
, "MAILB" }, /* RFC 1035 */
305 { T_MAILA
, "MAILA" }, /* RFC 1035 */
310 struct tok ns_class2str
[] = {
311 { C_IN
, "IN" }, /* Not used */
312 { C_CHAOS
, "CHAOS" },
319 static const u_char
*
320 ns_qprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
322 register const u_char
*np
= cp
;
323 register u_int i
, class;
327 if (cp
== NULL
|| !TTEST2(*cp
, 4))
330 /* print the qtype */
331 i
= EXTRACT_16BITS(cp
);
333 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
334 /* print the qclass (if it's not IN) */
335 i
= EXTRACT_16BITS(cp
);
342 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
351 cp
= ns_nprint(np
, bp
);
352 return(cp
? cp
+ 4 : NULL
);
356 static const u_char
*
357 ns_rprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
359 register u_int i
, class, opt_flags
= 0;
360 register u_short typ
, len
;
361 register const u_char
*rp
;
365 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
370 if (cp
== NULL
|| !TTEST2(*cp
, 10))
373 /* print the type/qtype */
374 typ
= EXTRACT_16BITS(cp
);
376 /* print the class (if it's not IN and the type isn't OPT) */
377 i
= EXTRACT_16BITS(cp
);
380 class = (i
& ~C_CACHE_FLUSH
);
383 if (class != C_IN
&& typ
!= T_OPT
)
384 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
386 if (i
& C_CACHE_FLUSH
)
387 printf(" (Cache flush)");
392 /* if T_OPT, save opt_flags */
394 opt_flags
= EXTRACT_16BITS(cp
);
395 /* ignore rest of ttl */
398 len
= EXTRACT_16BITS(cp
);
403 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
409 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
411 printf(" %s", ipaddr_string(cp
));
421 if (ns_nprint(cp
, bp
) == NULL
)
429 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
432 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
434 if (!TTEST2(*cp
, 5 * 4))
436 printf(" %u", EXTRACT_32BITS(cp
));
438 printf(" %u", EXTRACT_32BITS(cp
));
440 printf(" %u", EXTRACT_32BITS(cp
));
442 printf(" %u", EXTRACT_32BITS(cp
));
444 printf(" %u", EXTRACT_32BITS(cp
));
451 if (ns_nprint(cp
+ 2, bp
) == NULL
)
453 printf(" %d", EXTRACT_16BITS(cp
));
470 if (ns_nprint(cp
+ 6, bp
) == NULL
)
472 printf(":%d %d %d", EXTRACT_16BITS(cp
+ 4),
473 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2));
478 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
480 printf(" %s", ip6addr_string(cp
));
491 pbyte
= (pbit
& ~7) / 8;
493 printf(" %u(bad plen)", pbit
);
495 } else if (pbit
< 128) {
496 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
498 memset(&a
, 0, sizeof(a
));
499 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
500 printf(" %u %s", pbit
, ip6addr_string(&a
));
504 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
512 printf(" UDPsize=%u", class);
513 if (opt_flags
& 0x8000)
517 case T_UNSPECA
: /* One long string */
518 if (!TTEST2(*cp
, len
))
520 if (fn_printn(cp
, len
, snapend
))
526 if (cp
+ len
> snapend
)
531 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
536 printf(" fudge=%u", EXTRACT_16BITS(cp
));
540 printf(" maclen=%u", EXTRACT_16BITS(cp
));
541 cp
+= 2 + EXTRACT_16BITS(cp
);
544 printf(" origid=%u", EXTRACT_16BITS(cp
));
548 printf(" error=%u", EXTRACT_16BITS(cp
));
552 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
556 return (rp
); /* XXX This isn't always right */
560 ns_print(register const u_char
*bp
, u_int length
, int is_mdns
)
562 register const HEADER
*np
;
563 register int qdcount
, ancount
, nscount
, arcount
;
564 register const u_char
*cp
;
567 np
= (const HEADER
*)bp
;
569 /* get the byte-order right */
570 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
571 ancount
= EXTRACT_16BITS(&np
->ancount
);
572 nscount
= EXTRACT_16BITS(&np
->nscount
);
573 arcount
= EXTRACT_16BITS(&np
->arcount
);
576 /* this is a response */
577 printf("%d%s%s%s%s%s%s",
578 EXTRACT_16BITS(&np
->id
),
579 ns_ops
[DNS_OPCODE(np
)],
580 ns_resp
[DNS_RCODE(np
)],
581 DNS_AA(np
)? "*" : "",
582 DNS_RA(np
)? "" : "-",
583 DNS_TC(np
)? "|" : "",
584 DNS_AD(np
)? "$" : "");
587 printf(" [%dq]", qdcount
);
588 /* Print QUESTION section on -vv */
589 cp
= (const u_char
*)(np
+ 1);
591 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
594 fputs(" q:", stdout
);
595 if ((cp
= ns_qprint(cp
, bp
, is_mdns
)) == NULL
)
598 if ((cp
= ns_nskip(cp
)) == NULL
)
600 cp
+= 4; /* skip QTYPE and QCLASS */
603 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
605 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
607 while (cp
< snapend
&& ancount
--) {
609 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
615 /* Print NS and AR sections on -vv */
617 if (cp
< snapend
&& nscount
--) {
618 fputs(" ns:", stdout
);
619 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
621 while (cp
< snapend
&& nscount
--) {
623 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
629 if (cp
< snapend
&& arcount
--) {
630 fputs(" ar:", stdout
);
631 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
633 while (cp
< snapend
&& arcount
--) {
635 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
644 /* this is a request */
645 printf("%d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
646 DNS_RD(np
) ? "+" : "",
647 DNS_CD(np
) ? "%" : "");
650 b2
= EXTRACT_16BITS(((u_short
*)np
)+1);
652 printf(" [b2&3=0x%x]", b2
);
654 if (DNS_OPCODE(np
) == IQUERY
) {
656 printf(" [%dq]", qdcount
);
658 printf(" [%da]", ancount
);
662 printf(" [%da]", ancount
);
664 printf(" [%dq]", qdcount
);
667 printf(" [%dn]", nscount
);
669 printf(" [%dau]", arcount
);
671 cp
= (const u_char
*)(np
+ 1);
673 cp
= ns_qprint(cp
, (const u_char
*)np
, is_mdns
);
676 while (cp
< snapend
&& qdcount
--) {
677 cp
= ns_qprint((const u_char
*)cp
,
687 /* Print remaining sections on -vv */
690 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
692 while (cp
< snapend
&& ancount
--) {
694 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
700 if (cp
< snapend
&& nscount
--) {
701 fputs(" ns:", stdout
);
702 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
704 while (nscount
-- && cp
< snapend
) {
706 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
712 if (cp
< snapend
&& arcount
--) {
713 fputs(" ar:", stdout
);
714 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
716 while (cp
< snapend
&& arcount
--) {
718 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
726 printf(" (%d)", length
);