]>
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>
34 #include "netdissect.h"
35 #include "addrtoname.h"
36 #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
,
55 register const u_char
*cp
)
59 if (!ND_TTEST2(*cp
, 1))
63 switch (i
& TYPE_MASK
) {
71 if ((i
& ~TYPE_MASK
) != EDNS0_ELT_BITLABEL
)
72 return(NULL
); /* unknown ELT */
73 if (!ND_TTEST2(*cp
, 1))
75 if ((bitlen
= *cp
++) == 0)
77 bytelen
= (bitlen
+ 7) / 8;
89 if (!ND_TTEST2(*cp
, 1))
96 /* print a <domain-name> */
98 blabel_print(netdissect_options
*ndo
,
102 const u_char
*bitp
, *lim
;
105 if (!ND_TTEST2(*cp
, 1))
107 if ((bitlen
= *cp
) == 0)
109 slen
= (bitlen
+ 3) / 4;
112 /* print the bit string as a hex string */
113 ND_PRINT((ndo
, "\\[x"));
114 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
116 ND_PRINT((ndo
, "%02x", *bitp
));
121 ND_PRINT((ndo
, "%02x", tc
& (0xff << (8 - b
))));
125 ND_PRINT((ndo
, "%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
127 ND_PRINT((ndo
, "/%d]", bitlen
));
130 ND_PRINT((ndo
, ".../%d]", bitlen
));
135 labellen(netdissect_options
*ndo
,
140 if (!ND_TTEST2(*cp
, 1))
143 switch (i
& TYPE_MASK
) {
147 if ((elt
= (i
& ~TYPE_MASK
)) != EDNS0_ELT_BITLABEL
) {
148 ND_PRINT((ndo
, "<ELT %d>", elt
));
151 if (!ND_TTEST2(*(cp
+ 1), 1))
153 if ((bitlen
= *(cp
+ 1)) == 0)
155 return(((bitlen
+ 7) / 8) + 1);
164 * TYPE_RESERVED, but we use default to suppress compiler
165 * warnings about falling out of the switch statement.
167 ND_PRINT((ndo
, "<BAD LABEL TYPE>"));
173 ns_nprint(netdissect_options
*ndo
,
174 register const u_char
*cp
, register const u_char
*bp
)
177 register const u_char
*rp
= NULL
;
178 register int compress
= 0;
180 u_int offset
, max_offset
;
181 u_int name_chars
= 0;
183 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
185 if (!ND_TTEST2(*cp
, 1))
187 max_offset
= (u_int
)(cp
- bp
);
188 if (((i
= *cp
++) & TYPE_MASK
) != TYPE_INDIR
) {
194 while (i
&& cp
< ndo
->ndo_snapend
) {
195 switch (i
& TYPE_MASK
) {
202 if (!ND_TTEST2(*cp
, 1))
204 offset
= (((i
<< 8) | *cp
) & 0x3fff);
206 * This must move backwards in the packet.
207 * No RFC explicitly says that, but BIND's
208 * name decompression code requires it,
209 * as a way of preventing infinite loops
210 * and other bad behavior, and it's probably
211 * what was intended (compress by pointing
212 * to domain name suffixes already seen in
215 if (offset
>= max_offset
) {
216 ND_PRINT((ndo
, "<BAD PTR>"));
221 if (!ND_TTEST2(*cp
, 1))
223 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
229 elt
= (i
& ~TYPE_MASK
);
231 case EDNS0_ELT_BITLABEL
:
232 if (blabel_print(ndo
, cp
) == NULL
)
237 ND_PRINT((ndo
, "<ELT %d>", elt
));
243 ND_PRINT((ndo
, "<BAD LABEL TYPE>"));
247 if (name_chars
+ l
<= MAXCDNAME
) {
248 if (fn_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
250 } else if (name_chars
< MAXCDNAME
) {
251 if (fn_printn(ndo
, cp
,
252 MAXCDNAME
- name_chars
, ndo
->ndo_snapend
))
260 if (name_chars
<= MAXCDNAME
)
261 ND_PRINT((ndo
, "."));
263 if (!ND_TTEST2(*cp
, 1))
265 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
271 if (name_chars
> MAXCDNAME
)
272 ND_PRINT((ndo
, "<DOMAIN NAME TOO LONG>"));
274 ND_PRINT((ndo
, "."));
278 /* print a <character-string> */
279 static const u_char
*
280 ns_cprint(netdissect_options
*ndo
,
281 register const u_char
*cp
)
285 if (!ND_TTEST2(*cp
, 1))
288 if (fn_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
293 /* http://www.iana.org/assignments/dns-parameters */
294 const struct tok ns_type2str
[] = {
295 { T_A
, "A" }, /* RFC 1035 */
296 { T_NS
, "NS" }, /* RFC 1035 */
297 { T_MD
, "MD" }, /* RFC 1035 */
298 { T_MF
, "MF" }, /* RFC 1035 */
299 { T_CNAME
, "CNAME" }, /* RFC 1035 */
300 { T_SOA
, "SOA" }, /* RFC 1035 */
301 { T_MB
, "MB" }, /* RFC 1035 */
302 { T_MG
, "MG" }, /* RFC 1035 */
303 { T_MR
, "MR" }, /* RFC 1035 */
304 { T_NULL
, "NULL" }, /* RFC 1035 */
305 { T_WKS
, "WKS" }, /* RFC 1035 */
306 { T_PTR
, "PTR" }, /* RFC 1035 */
307 { T_HINFO
, "HINFO" }, /* RFC 1035 */
308 { T_MINFO
, "MINFO" }, /* RFC 1035 */
309 { T_MX
, "MX" }, /* RFC 1035 */
310 { T_TXT
, "TXT" }, /* RFC 1035 */
311 { T_RP
, "RP" }, /* RFC 1183 */
312 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
313 { T_X25
, "X25" }, /* RFC 1183 */
314 { T_ISDN
, "ISDN" }, /* RFC 1183 */
315 { T_RT
, "RT" }, /* RFC 1183 */
316 { T_NSAP
, "NSAP" }, /* RFC 1706 */
317 { T_NSAP_PTR
, "NSAP_PTR" },
318 { T_SIG
, "SIG" }, /* RFC 2535 */
319 { T_KEY
, "KEY" }, /* RFC 2535 */
320 { T_PX
, "PX" }, /* RFC 2163 */
321 { T_GPOS
, "GPOS" }, /* RFC 1712 */
322 { T_AAAA
, "AAAA" }, /* RFC 1886 */
323 { T_LOC
, "LOC" }, /* RFC 1876 */
324 { T_NXT
, "NXT" }, /* RFC 2535 */
325 { T_EID
, "EID" }, /* Nimrod */
326 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
327 { T_SRV
, "SRV" }, /* RFC 2782 */
328 { T_ATMA
, "ATMA" }, /* ATM Forum */
329 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
330 { T_KX
, "KX" }, /* RFC 2230 */
331 { T_CERT
, "CERT" }, /* RFC 2538 */
332 { T_A6
, "A6" }, /* RFC 2874 */
333 { T_DNAME
, "DNAME" }, /* RFC 2672 */
335 { T_OPT
, "OPT" }, /* RFC 2671 */
336 { T_APL
, "APL" }, /* RFC 3123 */
337 { T_DS
, "DS" }, /* RFC 4034 */
338 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
339 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
340 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
341 { T_NSEC
, "NSEC" }, /* RFC 4034 */
342 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
343 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
344 { T_UINFO
, "UINFO" },
347 { T_UNSPEC
, "UNSPEC" },
348 { T_UNSPECA
, "UNSPECA" },
349 { T_TKEY
, "TKEY" }, /* RFC 2930 */
350 { T_TSIG
, "TSIG" }, /* RFC 2845 */
351 { T_IXFR
, "IXFR" }, /* RFC 1995 */
352 { T_AXFR
, "AXFR" }, /* RFC 1035 */
353 { T_MAILB
, "MAILB" }, /* RFC 1035 */
354 { T_MAILA
, "MAILA" }, /* RFC 1035 */
359 const struct tok ns_class2str
[] = {
360 { C_IN
, "IN" }, /* Not used */
361 { C_CHAOS
, "CHAOS" },
368 static const u_char
*
369 ns_qprint(netdissect_options
*ndo
,
370 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
372 register const u_char
*np
= cp
;
373 register u_int i
, class;
375 cp
= ns_nskip(ndo
, cp
);
377 if (cp
== NULL
|| !ND_TTEST2(*cp
, 4))
380 /* print the qtype */
381 i
= EXTRACT_16BITS(cp
);
383 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", i
)));
384 /* print the qclass (if it's not IN) */
385 i
= EXTRACT_16BITS(cp
);
392 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
394 ND_PRINT((ndo
, i
& C_QU
? " (QU)" : " (QM)"));
397 ND_PRINT((ndo
, "? "));
398 cp
= ns_nprint(ndo
, np
, bp
);
399 return(cp
? cp
+ 4 : NULL
);
403 static const u_char
*
404 ns_rprint(netdissect_options
*ndo
,
405 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
407 register u_int i
, class, opt_flags
= 0;
408 register u_short typ
, len
;
409 register const u_char
*rp
;
411 if (ndo
->ndo_vflag
) {
412 ND_PRINT((ndo
, " "));
413 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
416 cp
= ns_nskip(ndo
, cp
);
418 if (cp
== NULL
|| !ND_TTEST2(*cp
, 10))
419 return (ndo
->ndo_snapend
);
421 /* print the type/qtype */
422 typ
= EXTRACT_16BITS(cp
);
424 /* print the class (if it's not IN and the type isn't OPT) */
425 i
= EXTRACT_16BITS(cp
);
428 class = (i
& ~C_CACHE_FLUSH
);
431 if (class != C_IN
&& typ
!= T_OPT
)
432 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
434 if (i
& C_CACHE_FLUSH
)
435 ND_PRINT((ndo
, " (Cache flush)"));
441 opt_flags
= EXTRACT_16BITS(cp
);
442 /* ignore rest of ttl field */
444 } else if (ndo
->ndo_vflag
> 2) {
446 ND_PRINT((ndo
, " ["));
447 unsigned_relts_print(ndo
, EXTRACT_32BITS(cp
));
448 ND_PRINT((ndo
, "]"));
455 len
= EXTRACT_16BITS(cp
);
460 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", typ
)));
461 if (rp
> ndo
->ndo_snapend
)
466 if (!ND_TTEST2(*cp
, sizeof(struct in_addr
)))
468 ND_PRINT((ndo
, " %s", intoa(htonl(EXTRACT_32BITS(cp
)))));
477 ND_PRINT((ndo
, " "));
478 if (ns_nprint(ndo
, cp
, bp
) == NULL
)
485 ND_PRINT((ndo
, " "));
486 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
488 ND_PRINT((ndo
, " "));
489 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
491 if (!ND_TTEST2(*cp
, 5 * 4))
493 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
495 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
497 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
499 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
501 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
505 ND_PRINT((ndo
, " "));
506 if (!ND_TTEST2(*cp
, 2))
508 if (ns_nprint(ndo
, cp
+ 2, bp
) == NULL
)
510 ND_PRINT((ndo
, " %d", EXTRACT_16BITS(cp
)));
515 ND_PRINT((ndo
, " \""));
516 cp
= ns_cprint(ndo
, cp
);
519 ND_PRINT((ndo
, "\""));
524 ND_PRINT((ndo
, " "));
525 if (!ND_TTEST2(*cp
, 6))
527 if (ns_nprint(ndo
, cp
+ 6, bp
) == NULL
)
529 ND_PRINT((ndo
, ":%d %d %d", EXTRACT_16BITS(cp
+ 4),
530 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2)));
535 char ntop_buf
[INET6_ADDRSTRLEN
];
537 if (!ND_TTEST2(*cp
, sizeof(struct in6_addr
)))
539 ND_PRINT((ndo
, " %s",
540 addrtostr6(cp
, ntop_buf
, sizeof(ntop_buf
))));
549 char ntop_buf
[INET6_ADDRSTRLEN
];
551 if (!ND_TTEST2(*cp
, 1))
554 pbyte
= (pbit
& ~7) / 8;
556 ND_PRINT((ndo
, " %u(bad plen)", pbit
));
558 } else if (pbit
< 128) {
559 if (!ND_TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
561 memset(&a
, 0, sizeof(a
));
562 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
563 ND_PRINT((ndo
, " %u %s", pbit
,
564 addrtostr6(&a
, ntop_buf
, sizeof(ntop_buf
))));
567 ND_PRINT((ndo
, " "));
568 if (ns_nprint(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
575 ND_PRINT((ndo
, " UDPsize=%u", class));
576 if (opt_flags
& 0x8000)
577 ND_PRINT((ndo
, " DO"));
580 case T_UNSPECA
: /* One long string */
581 if (!ND_TTEST2(*cp
, len
))
583 if (fn_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
589 if (cp
+ len
> ndo
->ndo_snapend
)
593 ND_PRINT((ndo
, " "));
594 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
597 if (!ND_TTEST2(*cp
, 2))
599 ND_PRINT((ndo
, " fudge=%u", EXTRACT_16BITS(cp
)));
601 if (!ND_TTEST2(*cp
, 2))
603 ND_PRINT((ndo
, " maclen=%u", EXTRACT_16BITS(cp
)));
604 cp
+= 2 + EXTRACT_16BITS(cp
);
605 if (!ND_TTEST2(*cp
, 2))
607 ND_PRINT((ndo
, " origid=%u", EXTRACT_16BITS(cp
)));
609 if (!ND_TTEST2(*cp
, 2))
611 ND_PRINT((ndo
, " error=%u", EXTRACT_16BITS(cp
)));
613 if (!ND_TTEST2(*cp
, 2))
615 ND_PRINT((ndo
, " otherlen=%u", EXTRACT_16BITS(cp
)));
619 return (rp
); /* XXX This isn't always right */
623 ns_print(netdissect_options
*ndo
,
624 register const u_char
*bp
, u_int length
, int is_mdns
)
626 register const HEADER
*np
;
627 register int qdcount
, ancount
, nscount
, arcount
;
628 register const u_char
*cp
;
631 if(length
< sizeof(*np
)) {
632 ND_PRINT((ndo
, "domain"));
633 ND_PRINT((ndo
, " [length %u < %zu]", length
, sizeof(*np
)));
634 ND_PRINT((ndo
, " (invalid)"));
638 np
= (const HEADER
*)bp
;
640 /* get the byte-order right */
641 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
642 ancount
= EXTRACT_16BITS(&np
->ancount
);
643 nscount
= EXTRACT_16BITS(&np
->nscount
);
644 arcount
= EXTRACT_16BITS(&np
->arcount
);
647 /* this is a response */
648 ND_PRINT((ndo
, "%d%s%s%s%s%s%s",
649 EXTRACT_16BITS(&np
->id
),
650 ns_ops
[DNS_OPCODE(np
)],
651 ns_resp
[DNS_RCODE(np
)],
652 DNS_AA(np
)? "*" : "",
653 DNS_RA(np
)? "" : "-",
654 DNS_TC(np
)? "|" : "",
655 DNS_AD(np
)? "$" : ""));
658 ND_PRINT((ndo
, " [%dq]", qdcount
));
659 /* Print QUESTION section on -vv */
660 cp
= (const u_char
*)(np
+ 1);
662 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
663 ND_PRINT((ndo
, ","));
664 if (ndo
->ndo_vflag
> 1) {
665 ND_PRINT((ndo
, " q:"));
666 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
669 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
671 cp
+= 4; /* skip QTYPE and QCLASS */
674 ND_PRINT((ndo
, " %d/%d/%d", ancount
, nscount
, arcount
));
676 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
678 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
679 ND_PRINT((ndo
, ","));
680 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
686 /* Print NS and AR sections on -vv */
687 if (ndo
->ndo_vflag
> 1) {
688 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
689 ND_PRINT((ndo
, " ns:"));
690 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
692 while (cp
< ndo
->ndo_snapend
&& nscount
--) {
693 ND_PRINT((ndo
, ","));
694 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
700 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
701 ND_PRINT((ndo
, " ar:"));
702 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
704 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
705 ND_PRINT((ndo
, ","));
706 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
715 /* this is a request */
716 ND_PRINT((ndo
, "%d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
717 DNS_RD(np
) ? "+" : "",
718 DNS_CD(np
) ? "%" : ""));
721 b2
= EXTRACT_16BITS(((const u_short
*)np
)+1);
723 ND_PRINT((ndo
, " [b2&3=0x%x]", b2
));
725 if (DNS_OPCODE(np
) == IQUERY
) {
727 ND_PRINT((ndo
, " [%dq]", qdcount
));
729 ND_PRINT((ndo
, " [%da]", ancount
));
733 ND_PRINT((ndo
, " [%da]", ancount
));
735 ND_PRINT((ndo
, " [%dq]", qdcount
));
738 ND_PRINT((ndo
, " [%dn]", nscount
));
740 ND_PRINT((ndo
, " [%dau]", arcount
));
742 cp
= (const u_char
*)(np
+ 1);
744 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
747 while (cp
< ndo
->ndo_snapend
&& qdcount
--) {
748 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
758 /* Print remaining sections on -vv */
759 if (ndo
->ndo_vflag
> 1) {
761 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
763 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
764 ND_PRINT((ndo
, ","));
765 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
771 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
772 ND_PRINT((ndo
, " ns:"));
773 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
775 while (nscount
-- && cp
< ndo
->ndo_snapend
) {
776 ND_PRINT((ndo
, ","));
777 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
783 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
784 ND_PRINT((ndo
, " ar:"));
785 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
787 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
788 ND_PRINT((ndo
, ","));
789 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
797 ND_PRINT((ndo
, " (%d)", length
));
801 ND_PRINT((ndo
, "[|domain]"));