]>
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.
22 /* \summary: Domain Name System (DNS) printer */
28 #include "netdissect-stdinc.h"
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "addrtostr.h"
39 static const char *ns_ops
[] = {
40 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
41 " op8", " updateA", " updateD", " updateDA",
42 " updateM", " updateMA", " zoneInit", " zoneRef",
45 static const char *ns_resp
[] = {
46 "", " FormErr", " ServFail", " NXDomain",
47 " NotImp", " Refused", " YXDomain", " YXRRSet",
48 " NXRRSet", " NotAuth", " NotZone", " Resp11",
49 " Resp12", " Resp13", " Resp14", " NoChange",
50 " BadVers", "Resp17", " Resp18", " Resp19",
51 " Resp20", "Resp21", " Resp22", " BadCookie",
55 ns_rcode(u_int rcode
) {
56 static char buf
[sizeof(" Resp4095")];
58 if (rcode
< sizeof(ns_resp
)/sizeof(ns_resp
[0])) {
59 return (ns_resp
[rcode
]);
61 snprintf(buf
, sizeof(buf
), " Resp%u", rcode
& 0xfff);
65 /* skip over a domain name */
67 ns_nskip(netdissect_options
*ndo
,
77 if ((i
& INDIR_MASK
) == INDIR_MASK
)
79 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
82 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
83 return(NULL
); /* unknown ELT */
86 if ((bitlen
= GET_U_1(cp
)) == 0)
89 bytelen
= (bitlen
+ 7) / 8;
101 static const u_char
*
102 blabel_print(netdissect_options
*ndo
,
105 u_int bitlen
, slen
, b
;
106 const u_char
*bitp
, *lim
;
111 if ((bitlen
= GET_U_1(cp
)) == 0)
113 slen
= (bitlen
+ 3) / 4;
116 /* print the bit string as a hex string */
118 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
120 ND_PRINT("%02x", GET_U_1(bitp
));
126 ND_PRINT("%02x", tc
& (0xff << (8 - b
)));
131 ND_PRINT("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
133 ND_PRINT("/%u]", bitlen
);
136 ND_PRINT(".../%u]", bitlen
);
141 labellen(netdissect_options
*ndo
,
149 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
151 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
152 ND_PRINT("<ELT %d>", elt
);
155 if (!ND_TTEST_1(cp
+ 1))
157 if ((bitlen
= GET_U_1(cp
+ 1)) == 0)
159 return(((bitlen
+ 7) / 8) + 1);
164 /* print a <domain-name> */
166 fqdn_print(netdissect_options
*ndo
,
167 const u_char
*cp
, const u_char
*bp
)
170 const u_char
*rp
= NULL
;
173 u_int offset
, max_offset
;
175 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
179 max_offset
= (u_int
)(cp
- bp
);
182 if ((i
& INDIR_MASK
) != INDIR_MASK
) {
188 while (i
&& cp
< ndo
->ndo_snapend
) {
189 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
196 offset
= (((i
<< 8) | GET_U_1(cp
)) & 0x3fff);
198 * This must move backwards in the packet.
199 * No RFC explicitly says that, but BIND's
200 * name decompression code requires it,
201 * as a way of preventing infinite loops
202 * and other bad behavior, and it's probably
203 * what was intended (compress by pointing
204 * to domain name suffixes already seen in
207 if (offset
>= max_offset
) {
208 ND_PRINT("<BAD PTR>");
213 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
221 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
222 elt
= (i
& ~INDIR_MASK
);
224 case EDNS0_ELT_BITLABEL
:
225 if (blabel_print(ndo
, cp
) == NULL
)
230 ND_PRINT("<ELT %u>", elt
);
234 if (nd_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
240 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
254 /* print a <character-string> */
255 static const u_char
*
256 ns_cprint(netdissect_options
*ndo
,
265 if (nd_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
270 extern const struct tok ns_type2str
[];
272 /* http://www.iana.org/assignments/dns-parameters */
273 const struct tok ns_type2str
[] = {
274 { T_A
, "A" }, /* RFC 1035 */
275 { T_NS
, "NS" }, /* RFC 1035 */
276 { T_MD
, "MD" }, /* RFC 1035 */
277 { T_MF
, "MF" }, /* RFC 1035 */
278 { T_CNAME
, "CNAME" }, /* RFC 1035 */
279 { T_SOA
, "SOA" }, /* RFC 1035 */
280 { T_MB
, "MB" }, /* RFC 1035 */
281 { T_MG
, "MG" }, /* RFC 1035 */
282 { T_MR
, "MR" }, /* RFC 1035 */
283 { T_NULL
, "NULL" }, /* RFC 1035 */
284 { T_WKS
, "WKS" }, /* RFC 1035 */
285 { T_PTR
, "PTR" }, /* RFC 1035 */
286 { T_HINFO
, "HINFO" }, /* RFC 1035 */
287 { T_MINFO
, "MINFO" }, /* RFC 1035 */
288 { T_MX
, "MX" }, /* RFC 1035 */
289 { T_TXT
, "TXT" }, /* RFC 1035 */
290 { T_RP
, "RP" }, /* RFC 1183 */
291 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
292 { T_X25
, "X25" }, /* RFC 1183 */
293 { T_ISDN
, "ISDN" }, /* RFC 1183 */
294 { T_RT
, "RT" }, /* RFC 1183 */
295 { T_NSAP
, "NSAP" }, /* RFC 1706 */
296 { T_NSAP_PTR
, "NSAP_PTR" },
297 { T_SIG
, "SIG" }, /* RFC 2535 */
298 { T_KEY
, "KEY" }, /* RFC 2535 */
299 { T_PX
, "PX" }, /* RFC 2163 */
300 { T_GPOS
, "GPOS" }, /* RFC 1712 */
301 { T_AAAA
, "AAAA" }, /* RFC 1886 */
302 { T_LOC
, "LOC" }, /* RFC 1876 */
303 { T_NXT
, "NXT" }, /* RFC 2535 */
304 { T_EID
, "EID" }, /* Nimrod */
305 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
306 { T_SRV
, "SRV" }, /* RFC 2782 */
307 { T_ATMA
, "ATMA" }, /* ATM Forum */
308 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
309 { T_KX
, "KX" }, /* RFC 2230 */
310 { T_CERT
, "CERT" }, /* RFC 2538 */
311 { T_A6
, "A6" }, /* RFC 2874 */
312 { T_DNAME
, "DNAME" }, /* RFC 2672 */
314 { T_OPT
, "OPT" }, /* RFC 2671 */
315 { T_APL
, "APL" }, /* RFC 3123 */
316 { T_DS
, "DS" }, /* RFC 4034 */
317 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
318 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
319 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
320 { T_NSEC
, "NSEC" }, /* RFC 4034 */
321 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
322 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
323 { T_UINFO
, "UINFO" },
326 { T_UNSPEC
, "UNSPEC" },
327 { T_UNSPECA
, "UNSPECA" },
328 { T_TKEY
, "TKEY" }, /* RFC 2930 */
329 { T_TSIG
, "TSIG" }, /* RFC 2845 */
330 { T_IXFR
, "IXFR" }, /* RFC 1995 */
331 { T_AXFR
, "AXFR" }, /* RFC 1035 */
332 { T_MAILB
, "MAILB" }, /* RFC 1035 */
333 { T_MAILA
, "MAILA" }, /* RFC 1035 */
338 extern const struct tok ns_class2str
[];
340 const struct tok ns_class2str
[] = {
341 { C_IN
, "IN" }, /* Not used */
342 { C_CHAOS
, "CHAOS" },
349 static const u_char
*
350 ns_qprint(netdissect_options
*ndo
,
351 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
353 const u_char
*np
= cp
;
356 cp
= ns_nskip(ndo
, cp
);
358 if (cp
== NULL
|| !ND_TTEST_4(cp
))
361 /* print the qtype */
364 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", i
));
365 /* print the qclass (if it's not IN) */
373 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
375 ND_PRINT(i
& C_QU
? " (QU)" : " (QM)");
379 cp
= fqdn_print(ndo
, np
, bp
);
380 return(cp
? cp
+ 4 : NULL
);
384 static const u_char
*
385 ns_rprint(netdissect_options
*ndo
,
386 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
388 u_int i
, class, opt_flags
= 0;
392 if (ndo
->ndo_vflag
) {
394 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
397 cp
= ns_nskip(ndo
, cp
);
399 if (cp
== NULL
|| !ND_TTEST_LEN(cp
, 10))
400 return (ndo
->ndo_snapend
);
402 /* print the type/qtype */
403 typ
= GET_BE_U_2(cp
);
405 /* print the class (if it's not IN and the type isn't OPT) */
409 class = (i
& ~C_CACHE_FLUSH
);
412 if (class != C_IN
&& typ
!= T_OPT
)
413 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
415 if (i
& C_CACHE_FLUSH
)
416 ND_PRINT(" (Cache flush)");
422 opt_flags
= GET_BE_U_2(cp
);
423 /* ignore rest of ttl field */
425 } else if (ndo
->ndo_vflag
> 2) {
428 unsigned_relts_print(ndo
, GET_BE_U_4(cp
));
436 len
= GET_BE_U_2(cp
);
441 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", typ
));
442 if (rp
> ndo
->ndo_snapend
)
447 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv4
)))
449 ND_PRINT(" %s", intoa(GET_IPV4_TO_NETWORK_ORDER(cp
)));
459 if (fqdn_print(ndo
, cp
, bp
) == NULL
)
467 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
470 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
472 if (!ND_TTEST_LEN(cp
, 5 * 4))
474 ND_PRINT(" %u", GET_BE_U_4(cp
));
476 ND_PRINT(" %u", GET_BE_U_4(cp
));
478 ND_PRINT(" %u", GET_BE_U_4(cp
));
480 ND_PRINT(" %u", GET_BE_U_4(cp
));
482 ND_PRINT(" %u", GET_BE_U_4(cp
));
489 if (fqdn_print(ndo
, cp
+ 2, bp
) == NULL
)
491 ND_PRINT(" %u", GET_BE_U_2(cp
));
497 cp
= ns_cprint(ndo
, cp
);
508 if (fqdn_print(ndo
, cp
+ 6, bp
) == NULL
)
510 ND_PRINT(":%u %u %u", GET_BE_U_2(cp
+ 4),
511 GET_BE_U_2(cp
), GET_BE_U_2(cp
+ 2));
516 char ntop_buf
[INET6_ADDRSTRLEN
];
518 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv6
)))
521 addrtostr6(cp
, ntop_buf
, sizeof(ntop_buf
)));
530 char ntop_buf
[INET6_ADDRSTRLEN
];
535 pbyte
= (pbit
& ~7) / 8;
537 ND_PRINT(" %u(bad plen)", pbit
);
539 } else if (pbit
< 128) {
540 if (!ND_TTEST_LEN(cp
+ 1, sizeof(a
) - pbyte
))
542 memset(&a
, 0, sizeof(a
));
543 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
544 ND_PRINT(" %u %s", pbit
,
545 addrtostr6(&a
, ntop_buf
, sizeof(ntop_buf
)));
549 if (fqdn_print(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
556 ND_PRINT(" UDPsize=%u", class);
557 if (opt_flags
& 0x8000)
561 case T_UNSPECA
: /* One long string */
562 if (!ND_TTEST_LEN(cp
, len
))
564 if (nd_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
570 if (cp
+ len
> ndo
->ndo_snapend
)
575 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
580 ND_PRINT(" fudge=%u", GET_BE_U_2(cp
));
584 ND_PRINT(" maclen=%u", GET_BE_U_2(cp
));
585 cp
+= 2 + GET_BE_U_2(cp
);
588 ND_PRINT(" origid=%u", GET_BE_U_2(cp
));
592 ND_PRINT(" error=%u", GET_BE_U_2(cp
));
596 ND_PRINT(" otherlen=%u", GET_BE_U_2(cp
));
600 return (rp
); /* XXX This isn't always right */
604 domain_print(netdissect_options
*ndo
,
605 const u_char
*bp
, u_int length
, int is_mdns
)
607 const dns_header_t
*np
;
608 uint16_t flags
, rcode
, rdlen
, type
;
609 u_int qdcount
, ancount
, nscount
, arcount
;
614 ndo
->ndo_protocol
= "domain";
615 np
= (const dns_header_t
*)bp
;
617 flags
= GET_BE_U_2(np
->flags
);
618 /* get the byte-order right */
619 qdcount
= GET_BE_U_2(np
->qdcount
);
620 ancount
= GET_BE_U_2(np
->ancount
);
621 nscount
= GET_BE_U_2(np
->nscount
);
622 arcount
= GET_BE_U_2(np
->arcount
);
624 /* find the opt record to extract extended rcode */
625 cp
= (const u_char
*)(np
+ 1);
626 rcode
= DNS_RCODE(flags
);
627 for (i
= 0; i
< qdcount
; i
++) {
628 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
630 cp
+= 4; /* skip QTYPE and QCLASS */
631 if (cp
>= ndo
->ndo_snapend
)
634 for (i
= 0; i
< ancount
+ nscount
; i
++) {
635 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
637 cp
+= 8; /* skip TYPE, CLASS and TTL */
638 if (cp
+ 2 > ndo
->ndo_snapend
)
640 rdlen
= GET_BE_U_2(cp
);
642 if (cp
>= ndo
->ndo_snapend
)
645 for (i
= 0; i
< arcount
; i
++) {
646 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
648 if (cp
+ 2 > ndo
->ndo_snapend
)
650 type
= GET_BE_U_2(cp
);
651 cp
+= 4; /* skip TYPE and CLASS */
652 if (cp
+ 1 > ndo
->ndo_snapend
)
659 if (cp
+ 2 > ndo
->ndo_snapend
)
661 rdlen
= GET_BE_U_2(cp
);
663 if (cp
>= ndo
->ndo_snapend
)
669 /* this is a response */
670 ND_PRINT("%u%s%s%s%s%s%s",
672 ns_ops
[DNS_OPCODE(flags
)],
674 DNS_AA(flags
)? "*" : "",
675 DNS_RA(flags
)? "" : "-",
676 DNS_TC(flags
)? "|" : "",
677 DNS_AD(flags
)? "$" : "");
680 ND_PRINT(" [%uq]", qdcount
);
681 /* Print QUESTION section on -vv */
682 cp
= (const u_char
*)(np
+ 1);
683 for (i
= 0; i
< qdcount
; i
++) {
686 if (ndo
->ndo_vflag
> 1) {
688 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
691 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
693 cp
+= 4; /* skip QTYPE and QCLASS */
696 ND_PRINT(" %u/%u/%u", ancount
, nscount
, arcount
);
698 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
701 while (cp
< ndo
->ndo_snapend
&& ancount
) {
703 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
710 /* Print NS and AR sections on -vv */
711 if (ndo
->ndo_vflag
> 1) {
712 if (cp
< ndo
->ndo_snapend
&& nscount
) {
714 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
717 while (cp
< ndo
->ndo_snapend
&& nscount
) {
719 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
726 if (cp
< ndo
->ndo_snapend
&& arcount
) {
728 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
731 while (cp
< ndo
->ndo_snapend
&& arcount
) {
733 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
743 /* this is a request */
744 ND_PRINT("%u%s%s%s", GET_BE_U_2(np
->id
),
745 ns_ops
[DNS_OPCODE(flags
)],
746 DNS_RD(flags
) ? "+" : "",
747 DNS_CD(flags
) ? "%" : "");
750 b2
= GET_BE_U_2(((const u_short
*)np
) + 1);
752 ND_PRINT(" [b2&3=0x%x]", b2
);
754 if (DNS_OPCODE(flags
) == IQUERY
) {
756 ND_PRINT(" [%uq]", qdcount
);
758 ND_PRINT(" [%ua]", ancount
);
762 ND_PRINT(" [%ua]", ancount
);
764 ND_PRINT(" [%uq]", qdcount
);
767 ND_PRINT(" [%un]", nscount
);
769 ND_PRINT(" [%uau]", arcount
);
771 cp
= (const u_char
*)(np
+ 1);
773 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
777 while (cp
< ndo
->ndo_snapend
&& qdcount
) {
778 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
789 /* Print remaining sections on -vv */
790 if (ndo
->ndo_vflag
> 1) {
792 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
795 while (cp
< ndo
->ndo_snapend
&& ancount
) {
797 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
804 if (cp
< ndo
->ndo_snapend
&& nscount
) {
806 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
809 while (cp
< ndo
->ndo_snapend
&& nscount
) {
811 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
818 if (cp
< ndo
->ndo_snapend
&& arcount
) {
820 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
823 while (cp
< ndo
->ndo_snapend
&& arcount
) {
825 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
834 ND_PRINT(" (%u)", length
);