]>
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",
52 /* skip over a domain name */
54 ns_nskip(netdissect_options
*ndo
,
64 if ((i
& INDIR_MASK
) == INDIR_MASK
)
66 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
69 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
70 return(NULL
); /* unknown ELT */
73 if ((bitlen
= EXTRACT_U_1(cp
)) == 0)
76 bytelen
= (bitlen
+ 7) / 8;
88 /* print a <domain-name> */
90 blabel_print(netdissect_options
*ndo
,
93 u_int bitlen
, slen
, b
;
94 const u_char
*bitp
, *lim
;
99 if ((bitlen
= EXTRACT_U_1(cp
)) == 0)
101 slen
= (bitlen
+ 3) / 4;
104 /* print the bit string as a hex string */
106 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
108 ND_PRINT("%02x", EXTRACT_U_1(bitp
));
112 tc
= EXTRACT_U_1(bitp
);
114 ND_PRINT("%02x", tc
& (0xff << (8 - b
)));
117 tc
= EXTRACT_U_1(bitp
);
119 ND_PRINT("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
121 ND_PRINT("/%u]", bitlen
);
124 ND_PRINT(".../%u]", bitlen
);
129 labellen(netdissect_options
*ndo
,
137 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
139 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
140 ND_PRINT("<ELT %d>", elt
);
143 if (!ND_TTEST_1(cp
+ 1))
145 if ((bitlen
= EXTRACT_U_1(cp
+ 1)) == 0)
147 return(((bitlen
+ 7) / 8) + 1);
153 ns_nprint(netdissect_options
*ndo
,
154 const u_char
*cp
, const u_char
*bp
)
157 const u_char
*rp
= NULL
;
160 u_int offset
, max_offset
;
162 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
166 max_offset
= (u_int
)(cp
- bp
);
169 if ((i
& INDIR_MASK
) != INDIR_MASK
) {
175 while (i
&& cp
< ndo
->ndo_snapend
) {
176 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
183 offset
= (((i
<< 8) | EXTRACT_U_1(cp
)) & 0x3fff);
185 * This must move backwards in the packet.
186 * No RFC explicitly says that, but BIND's
187 * name decompression code requires it,
188 * as a way of preventing infinite loops
189 * and other bad behavior, and it's probably
190 * what was intended (compress by pointing
191 * to domain name suffixes already seen in
194 if (offset
>= max_offset
) {
195 ND_PRINT("<BAD PTR>");
200 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
208 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
209 elt
= (i
& ~INDIR_MASK
);
211 case EDNS0_ELT_BITLABEL
:
212 if (blabel_print(ndo
, cp
) == NULL
)
217 ND_PRINT("<ELT %u>", elt
);
221 if (nd_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
227 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
241 /* print a <character-string> */
242 static const u_char
*
243 ns_cprint(netdissect_options
*ndo
,
252 if (nd_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
257 extern const struct tok ns_type2str
[];
259 /* http://www.iana.org/assignments/dns-parameters */
260 const struct tok ns_type2str
[] = {
261 { T_A
, "A" }, /* RFC 1035 */
262 { T_NS
, "NS" }, /* RFC 1035 */
263 { T_MD
, "MD" }, /* RFC 1035 */
264 { T_MF
, "MF" }, /* RFC 1035 */
265 { T_CNAME
, "CNAME" }, /* RFC 1035 */
266 { T_SOA
, "SOA" }, /* RFC 1035 */
267 { T_MB
, "MB" }, /* RFC 1035 */
268 { T_MG
, "MG" }, /* RFC 1035 */
269 { T_MR
, "MR" }, /* RFC 1035 */
270 { T_NULL
, "NULL" }, /* RFC 1035 */
271 { T_WKS
, "WKS" }, /* RFC 1035 */
272 { T_PTR
, "PTR" }, /* RFC 1035 */
273 { T_HINFO
, "HINFO" }, /* RFC 1035 */
274 { T_MINFO
, "MINFO" }, /* RFC 1035 */
275 { T_MX
, "MX" }, /* RFC 1035 */
276 { T_TXT
, "TXT" }, /* RFC 1035 */
277 { T_RP
, "RP" }, /* RFC 1183 */
278 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
279 { T_X25
, "X25" }, /* RFC 1183 */
280 { T_ISDN
, "ISDN" }, /* RFC 1183 */
281 { T_RT
, "RT" }, /* RFC 1183 */
282 { T_NSAP
, "NSAP" }, /* RFC 1706 */
283 { T_NSAP_PTR
, "NSAP_PTR" },
284 { T_SIG
, "SIG" }, /* RFC 2535 */
285 { T_KEY
, "KEY" }, /* RFC 2535 */
286 { T_PX
, "PX" }, /* RFC 2163 */
287 { T_GPOS
, "GPOS" }, /* RFC 1712 */
288 { T_AAAA
, "AAAA" }, /* RFC 1886 */
289 { T_LOC
, "LOC" }, /* RFC 1876 */
290 { T_NXT
, "NXT" }, /* RFC 2535 */
291 { T_EID
, "EID" }, /* Nimrod */
292 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
293 { T_SRV
, "SRV" }, /* RFC 2782 */
294 { T_ATMA
, "ATMA" }, /* ATM Forum */
295 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
296 { T_KX
, "KX" }, /* RFC 2230 */
297 { T_CERT
, "CERT" }, /* RFC 2538 */
298 { T_A6
, "A6" }, /* RFC 2874 */
299 { T_DNAME
, "DNAME" }, /* RFC 2672 */
301 { T_OPT
, "OPT" }, /* RFC 2671 */
302 { T_APL
, "APL" }, /* RFC 3123 */
303 { T_DS
, "DS" }, /* RFC 4034 */
304 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
305 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
306 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
307 { T_NSEC
, "NSEC" }, /* RFC 4034 */
308 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
309 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
310 { T_UINFO
, "UINFO" },
313 { T_UNSPEC
, "UNSPEC" },
314 { T_UNSPECA
, "UNSPECA" },
315 { T_TKEY
, "TKEY" }, /* RFC 2930 */
316 { T_TSIG
, "TSIG" }, /* RFC 2845 */
317 { T_IXFR
, "IXFR" }, /* RFC 1995 */
318 { T_AXFR
, "AXFR" }, /* RFC 1035 */
319 { T_MAILB
, "MAILB" }, /* RFC 1035 */
320 { T_MAILA
, "MAILA" }, /* RFC 1035 */
325 extern const struct tok ns_class2str
[];
327 const struct tok ns_class2str
[] = {
328 { C_IN
, "IN" }, /* Not used */
329 { C_CHAOS
, "CHAOS" },
336 static const u_char
*
337 ns_qprint(netdissect_options
*ndo
,
338 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
340 const u_char
*np
= cp
;
343 cp
= ns_nskip(ndo
, cp
);
345 if (cp
== NULL
|| !ND_TTEST_4(cp
))
348 /* print the qtype */
349 i
= EXTRACT_BE_U_2(cp
);
351 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", i
));
352 /* print the qclass (if it's not IN) */
353 i
= EXTRACT_BE_U_2(cp
);
360 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
362 ND_PRINT(i
& C_QU
? " (QU)" : " (QM)");
366 cp
= ns_nprint(ndo
, np
, bp
);
367 return(cp
? cp
+ 4 : NULL
);
371 static const u_char
*
372 ns_rprint(netdissect_options
*ndo
,
373 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
375 u_int i
, class, opt_flags
= 0;
379 if (ndo
->ndo_vflag
) {
381 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
384 cp
= ns_nskip(ndo
, cp
);
386 if (cp
== NULL
|| !ND_TTEST_LEN(cp
, 10))
387 return (ndo
->ndo_snapend
);
389 /* print the type/qtype */
390 typ
= EXTRACT_BE_U_2(cp
);
392 /* print the class (if it's not IN and the type isn't OPT) */
393 i
= EXTRACT_BE_U_2(cp
);
396 class = (i
& ~C_CACHE_FLUSH
);
399 if (class != C_IN
&& typ
!= T_OPT
)
400 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
402 if (i
& C_CACHE_FLUSH
)
403 ND_PRINT(" (Cache flush)");
409 opt_flags
= EXTRACT_BE_U_2(cp
);
410 /* ignore rest of ttl field */
412 } else if (ndo
->ndo_vflag
> 2) {
415 unsigned_relts_print(ndo
, EXTRACT_BE_U_4(cp
));
423 len
= EXTRACT_BE_U_2(cp
);
428 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", typ
));
429 if (rp
> ndo
->ndo_snapend
)
434 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv4
)))
436 ND_PRINT(" %s", intoa(EXTRACT_IPV4_TO_NETWORK_ORDER(cp
)));
446 if (ns_nprint(ndo
, cp
, bp
) == NULL
)
454 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
457 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
459 if (!ND_TTEST_LEN(cp
, 5 * 4))
461 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
463 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
465 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
467 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
469 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
476 if (ns_nprint(ndo
, cp
+ 2, bp
) == NULL
)
478 ND_PRINT(" %u", EXTRACT_BE_U_2(cp
));
484 cp
= ns_cprint(ndo
, cp
);
495 if (ns_nprint(ndo
, cp
+ 6, bp
) == NULL
)
497 ND_PRINT(":%u %u %u", EXTRACT_BE_U_2(cp
+ 4),
498 EXTRACT_BE_U_2(cp
), EXTRACT_BE_U_2(cp
+ 2));
503 char ntop_buf
[INET6_ADDRSTRLEN
];
505 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv6
)))
508 addrtostr6(cp
, ntop_buf
, sizeof(ntop_buf
)));
517 char ntop_buf
[INET6_ADDRSTRLEN
];
521 pbit
= EXTRACT_U_1(cp
);
522 pbyte
= (pbit
& ~7) / 8;
524 ND_PRINT(" %u(bad plen)", pbit
);
526 } else if (pbit
< 128) {
527 if (!ND_TTEST_LEN(cp
+ 1, sizeof(a
) - pbyte
))
529 memset(&a
, 0, sizeof(a
));
530 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
531 ND_PRINT(" %u %s", pbit
,
532 addrtostr6(&a
, ntop_buf
, sizeof(ntop_buf
)));
536 if (ns_nprint(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
543 ND_PRINT(" UDPsize=%u", class);
544 if (opt_flags
& 0x8000)
548 case T_UNSPECA
: /* One long string */
549 if (!ND_TTEST_LEN(cp
, len
))
551 if (nd_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
557 if (cp
+ len
> ndo
->ndo_snapend
)
562 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
567 ND_PRINT(" fudge=%u", EXTRACT_BE_U_2(cp
));
571 ND_PRINT(" maclen=%u", EXTRACT_BE_U_2(cp
));
572 cp
+= 2 + EXTRACT_BE_U_2(cp
);
575 ND_PRINT(" origid=%u", EXTRACT_BE_U_2(cp
));
579 ND_PRINT(" error=%u", EXTRACT_BE_U_2(cp
));
583 ND_PRINT(" otherlen=%u", EXTRACT_BE_U_2(cp
));
587 return (rp
); /* XXX This isn't always right */
591 domain_print(netdissect_options
*ndo
,
592 const u_char
*bp
, u_int length
, int is_mdns
)
594 const dns_header_t
*np
;
596 u_int qdcount
, ancount
, nscount
, arcount
;
601 ndo
->ndo_protocol
= "domain";
602 np
= (const dns_header_t
*)bp
;
604 flags
= EXTRACT_BE_U_2(np
->flags
);
605 /* get the byte-order right */
606 qdcount
= EXTRACT_BE_U_2(np
->qdcount
);
607 ancount
= EXTRACT_BE_U_2(np
->ancount
);
608 nscount
= EXTRACT_BE_U_2(np
->nscount
);
609 arcount
= EXTRACT_BE_U_2(np
->arcount
);
612 /* this is a response */
613 ND_PRINT("%u%s%s%s%s%s%s",
614 EXTRACT_BE_U_2(np
->id
),
615 ns_ops
[DNS_OPCODE(flags
)],
616 ns_resp
[DNS_RCODE(flags
)],
617 DNS_AA(flags
)? "*" : "",
618 DNS_RA(flags
)? "" : "-",
619 DNS_TC(flags
)? "|" : "",
620 DNS_AD(flags
)? "$" : "");
623 ND_PRINT(" [%uq]", qdcount
);
624 /* Print QUESTION section on -vv */
625 cp
= (const u_char
*)(np
+ 1);
626 for (i
= 0; i
< qdcount
; i
++) {
629 if (ndo
->ndo_vflag
> 1) {
631 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
634 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
636 cp
+= 4; /* skip QTYPE and QCLASS */
639 ND_PRINT(" %u/%u/%u", ancount
, nscount
, arcount
);
641 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
644 while (cp
< ndo
->ndo_snapend
&& ancount
) {
646 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
653 /* Print NS and AR sections on -vv */
654 if (ndo
->ndo_vflag
> 1) {
655 if (cp
< ndo
->ndo_snapend
&& nscount
) {
657 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
660 while (cp
< ndo
->ndo_snapend
&& nscount
) {
662 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
669 if (cp
< ndo
->ndo_snapend
&& arcount
) {
671 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
674 while (cp
< ndo
->ndo_snapend
&& arcount
) {
676 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
686 /* this is a request */
687 ND_PRINT("%u%s%s%s", EXTRACT_BE_U_2(np
->id
), ns_ops
[DNS_OPCODE(flags
)],
688 DNS_RD(flags
) ? "+" : "",
689 DNS_CD(flags
) ? "%" : "");
692 b2
= EXTRACT_BE_U_2(((const u_short
*)np
) + 1);
694 ND_PRINT(" [b2&3=0x%x]", b2
);
696 if (DNS_OPCODE(flags
) == IQUERY
) {
698 ND_PRINT(" [%uq]", qdcount
);
700 ND_PRINT(" [%ua]", ancount
);
704 ND_PRINT(" [%ua]", ancount
);
706 ND_PRINT(" [%uq]", qdcount
);
709 ND_PRINT(" [%un]", nscount
);
711 ND_PRINT(" [%uau]", arcount
);
713 cp
= (const u_char
*)(np
+ 1);
715 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
719 while (cp
< ndo
->ndo_snapend
&& qdcount
) {
720 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
731 /* Print remaining sections on -vv */
732 if (ndo
->ndo_vflag
> 1) {
734 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
737 while (cp
< ndo
->ndo_snapend
&& ancount
) {
739 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
746 if (cp
< ndo
->ndo_snapend
&& nscount
) {
748 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
751 while (cp
< ndo
->ndo_snapend
&& nscount
) {
753 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
760 if (cp
< ndo
->ndo_snapend
&& arcount
) {
762 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
765 while (cp
< ndo
->ndo_snapend
&& arcount
) {
767 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
776 ND_PRINT(" (%u)", length
);