]>
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
)
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
,
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 */
105 ND_PRINT((ndo
, "\\[x"));
106 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
108 ND_PRINT((ndo
, "%02x", *bitp
));
112 tc
= EXTRACT_U_1(bitp
);
114 ND_PRINT((ndo
, "%02x", tc
& (0xff << (8 - b
))));
117 tc
= EXTRACT_U_1(bitp
);
119 ND_PRINT((ndo
, "%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
121 ND_PRINT((ndo
, "/%d]", bitlen
));
124 ND_PRINT((ndo
, ".../%d]", 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((ndo
, "<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 register const u_char
*cp
, register const u_char
*bp
)
157 register const u_char
*rp
= NULL
;
158 register int compress
= 0;
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((ndo
, "<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((ndo
, "<ELT %d>", elt
));
221 if (fn_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
226 ND_PRINT((ndo
, "."));
227 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
237 ND_PRINT((ndo
, "."));
241 /* print a <character-string> */
242 static const u_char
*
243 ns_cprint(netdissect_options
*ndo
,
244 register const u_char
*cp
)
252 if (fn_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
257 /* http://www.iana.org/assignments/dns-parameters */
258 const struct tok ns_type2str
[] = {
259 { T_A
, "A" }, /* RFC 1035 */
260 { T_NS
, "NS" }, /* RFC 1035 */
261 { T_MD
, "MD" }, /* RFC 1035 */
262 { T_MF
, "MF" }, /* RFC 1035 */
263 { T_CNAME
, "CNAME" }, /* RFC 1035 */
264 { T_SOA
, "SOA" }, /* RFC 1035 */
265 { T_MB
, "MB" }, /* RFC 1035 */
266 { T_MG
, "MG" }, /* RFC 1035 */
267 { T_MR
, "MR" }, /* RFC 1035 */
268 { T_NULL
, "NULL" }, /* RFC 1035 */
269 { T_WKS
, "WKS" }, /* RFC 1035 */
270 { T_PTR
, "PTR" }, /* RFC 1035 */
271 { T_HINFO
, "HINFO" }, /* RFC 1035 */
272 { T_MINFO
, "MINFO" }, /* RFC 1035 */
273 { T_MX
, "MX" }, /* RFC 1035 */
274 { T_TXT
, "TXT" }, /* RFC 1035 */
275 { T_RP
, "RP" }, /* RFC 1183 */
276 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
277 { T_X25
, "X25" }, /* RFC 1183 */
278 { T_ISDN
, "ISDN" }, /* RFC 1183 */
279 { T_RT
, "RT" }, /* RFC 1183 */
280 { T_NSAP
, "NSAP" }, /* RFC 1706 */
281 { T_NSAP_PTR
, "NSAP_PTR" },
282 { T_SIG
, "SIG" }, /* RFC 2535 */
283 { T_KEY
, "KEY" }, /* RFC 2535 */
284 { T_PX
, "PX" }, /* RFC 2163 */
285 { T_GPOS
, "GPOS" }, /* RFC 1712 */
286 { T_AAAA
, "AAAA" }, /* RFC 1886 */
287 { T_LOC
, "LOC" }, /* RFC 1876 */
288 { T_NXT
, "NXT" }, /* RFC 2535 */
289 { T_EID
, "EID" }, /* Nimrod */
290 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
291 { T_SRV
, "SRV" }, /* RFC 2782 */
292 { T_ATMA
, "ATMA" }, /* ATM Forum */
293 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
294 { T_KX
, "KX" }, /* RFC 2230 */
295 { T_CERT
, "CERT" }, /* RFC 2538 */
296 { T_A6
, "A6" }, /* RFC 2874 */
297 { T_DNAME
, "DNAME" }, /* RFC 2672 */
299 { T_OPT
, "OPT" }, /* RFC 2671 */
300 { T_APL
, "APL" }, /* RFC 3123 */
301 { T_DS
, "DS" }, /* RFC 4034 */
302 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
303 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
304 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
305 { T_NSEC
, "NSEC" }, /* RFC 4034 */
306 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
307 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
308 { T_UINFO
, "UINFO" },
311 { T_UNSPEC
, "UNSPEC" },
312 { T_UNSPECA
, "UNSPECA" },
313 { T_TKEY
, "TKEY" }, /* RFC 2930 */
314 { T_TSIG
, "TSIG" }, /* RFC 2845 */
315 { T_IXFR
, "IXFR" }, /* RFC 1995 */
316 { T_AXFR
, "AXFR" }, /* RFC 1035 */
317 { T_MAILB
, "MAILB" }, /* RFC 1035 */
318 { T_MAILA
, "MAILA" }, /* RFC 1035 */
323 const struct tok ns_class2str
[] = {
324 { C_IN
, "IN" }, /* Not used */
325 { C_CHAOS
, "CHAOS" },
332 static const u_char
*
333 ns_qprint(netdissect_options
*ndo
,
334 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
336 register const u_char
*np
= cp
;
337 register u_int i
, class;
339 cp
= ns_nskip(ndo
, cp
);
341 if (cp
== NULL
|| !ND_TTEST2(*cp
, 4))
344 /* print the qtype */
345 i
= EXTRACT_BE_U_2(cp
);
347 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", i
)));
348 /* print the qclass (if it's not IN) */
349 i
= EXTRACT_BE_U_2(cp
);
356 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
358 ND_PRINT((ndo
, i
& C_QU
? " (QU)" : " (QM)"));
361 ND_PRINT((ndo
, "? "));
362 cp
= ns_nprint(ndo
, np
, bp
);
363 return(cp
? cp
+ 4 : NULL
);
367 static const u_char
*
368 ns_rprint(netdissect_options
*ndo
,
369 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
371 register u_int i
, class, opt_flags
= 0;
372 register u_short typ
, len
;
373 register const u_char
*rp
;
375 if (ndo
->ndo_vflag
) {
376 ND_PRINT((ndo
, " "));
377 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
380 cp
= ns_nskip(ndo
, cp
);
382 if (cp
== NULL
|| !ND_TTEST2(*cp
, 10))
383 return (ndo
->ndo_snapend
);
385 /* print the type/qtype */
386 typ
= EXTRACT_BE_U_2(cp
);
388 /* print the class (if it's not IN and the type isn't OPT) */
389 i
= EXTRACT_BE_U_2(cp
);
392 class = (i
& ~C_CACHE_FLUSH
);
395 if (class != C_IN
&& typ
!= T_OPT
)
396 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
398 if (i
& C_CACHE_FLUSH
)
399 ND_PRINT((ndo
, " (Cache flush)"));
405 opt_flags
= EXTRACT_BE_U_2(cp
);
406 /* ignore rest of ttl field */
408 } else if (ndo
->ndo_vflag
> 2) {
410 ND_PRINT((ndo
, " ["));
411 unsigned_relts_print(ndo
, EXTRACT_BE_U_4(cp
));
412 ND_PRINT((ndo
, "]"));
419 len
= EXTRACT_BE_U_2(cp
);
424 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", typ
)));
425 if (rp
> ndo
->ndo_snapend
)
430 if (!ND_TTEST2(*cp
, sizeof(struct in_addr
)))
432 ND_PRINT((ndo
, " %s", intoa(htonl(EXTRACT_BE_U_4(cp
)))));
441 ND_PRINT((ndo
, " "));
442 if (ns_nprint(ndo
, cp
, bp
) == NULL
)
449 ND_PRINT((ndo
, " "));
450 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
452 ND_PRINT((ndo
, " "));
453 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
455 if (!ND_TTEST2(*cp
, 5 * 4))
457 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(cp
)));
459 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(cp
)));
461 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(cp
)));
463 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(cp
)));
465 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(cp
)));
469 ND_PRINT((ndo
, " "));
470 if (!ND_TTEST2(*cp
, 2))
472 if (ns_nprint(ndo
, cp
+ 2, bp
) == NULL
)
474 ND_PRINT((ndo
, " %d", EXTRACT_BE_U_2(cp
)));
479 ND_PRINT((ndo
, " \""));
480 cp
= ns_cprint(ndo
, cp
);
483 ND_PRINT((ndo
, "\""));
488 ND_PRINT((ndo
, " "));
489 if (!ND_TTEST2(*cp
, 6))
491 if (ns_nprint(ndo
, cp
+ 6, bp
) == NULL
)
493 ND_PRINT((ndo
, ":%d %d %d", EXTRACT_BE_U_2(cp
+ 4),
494 EXTRACT_BE_U_2(cp
), EXTRACT_BE_U_2(cp
+ 2)));
499 char ntop_buf
[INET6_ADDRSTRLEN
];
501 if (!ND_TTEST2(*cp
, sizeof(struct in6_addr
)))
503 ND_PRINT((ndo
, " %s",
504 addrtostr6(cp
, ntop_buf
, sizeof(ntop_buf
))));
513 char ntop_buf
[INET6_ADDRSTRLEN
];
517 pbit
= EXTRACT_U_1(cp
);
518 pbyte
= (pbit
& ~7) / 8;
520 ND_PRINT((ndo
, " %u(bad plen)", pbit
));
522 } else if (pbit
< 128) {
523 if (!ND_TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
525 memset(&a
, 0, sizeof(a
));
526 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
527 ND_PRINT((ndo
, " %u %s", pbit
,
528 addrtostr6(&a
, ntop_buf
, sizeof(ntop_buf
))));
531 ND_PRINT((ndo
, " "));
532 if (ns_nprint(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
539 ND_PRINT((ndo
, " UDPsize=%u", class));
540 if (opt_flags
& 0x8000)
541 ND_PRINT((ndo
, " DO"));
544 case T_UNSPECA
: /* One long string */
545 if (!ND_TTEST2(*cp
, len
))
547 if (fn_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
553 if (cp
+ len
> ndo
->ndo_snapend
)
557 ND_PRINT((ndo
, " "));
558 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
563 ND_PRINT((ndo
, " fudge=%u", EXTRACT_BE_U_2(cp
)));
567 ND_PRINT((ndo
, " maclen=%u", EXTRACT_BE_U_2(cp
)));
568 cp
+= 2 + EXTRACT_BE_U_2(cp
);
571 ND_PRINT((ndo
, " origid=%u", EXTRACT_BE_U_2(cp
)));
575 ND_PRINT((ndo
, " error=%u", EXTRACT_BE_U_2(cp
)));
579 ND_PRINT((ndo
, " otherlen=%u", EXTRACT_BE_U_2(cp
)));
583 return (rp
); /* XXX This isn't always right */
587 domain_print(netdissect_options
*ndo
,
588 register const u_char
*bp
, u_int length
, int is_mdns
)
590 register const HEADER
*np
;
591 register int qdcount
, ancount
, nscount
, arcount
;
592 register const u_char
*cp
;
595 np
= (const HEADER
*)bp
;
597 /* get the byte-order right */
598 qdcount
= EXTRACT_BE_U_2(&np
->qdcount
);
599 ancount
= EXTRACT_BE_U_2(&np
->ancount
);
600 nscount
= EXTRACT_BE_U_2(&np
->nscount
);
601 arcount
= EXTRACT_BE_U_2(&np
->arcount
);
604 /* this is a response */
605 ND_PRINT((ndo
, "%d%s%s%s%s%s%s",
606 EXTRACT_BE_U_2(&np
->id
),
607 ns_ops
[DNS_OPCODE(np
)],
608 ns_resp
[DNS_RCODE(np
)],
609 DNS_AA(np
)? "*" : "",
610 DNS_RA(np
)? "" : "-",
611 DNS_TC(np
)? "|" : "",
612 DNS_AD(np
)? "$" : ""));
615 ND_PRINT((ndo
, " [%dq]", qdcount
));
616 /* Print QUESTION section on -vv */
617 cp
= (const u_char
*)(np
+ 1);
619 if (qdcount
< EXTRACT_BE_U_2(&np
->qdcount
) - 1)
620 ND_PRINT((ndo
, ","));
621 if (ndo
->ndo_vflag
> 1) {
622 ND_PRINT((ndo
, " q:"));
623 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
626 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
628 cp
+= 4; /* skip QTYPE and QCLASS */
631 ND_PRINT((ndo
, " %d/%d/%d", ancount
, nscount
, arcount
));
633 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
635 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
636 ND_PRINT((ndo
, ","));
637 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
643 /* Print NS and AR sections on -vv */
644 if (ndo
->ndo_vflag
> 1) {
645 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
646 ND_PRINT((ndo
, " ns:"));
647 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
649 while (cp
< ndo
->ndo_snapend
&& nscount
--) {
650 ND_PRINT((ndo
, ","));
651 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
657 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
658 ND_PRINT((ndo
, " ar:"));
659 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
661 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
662 ND_PRINT((ndo
, ","));
663 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
672 /* this is a request */
673 ND_PRINT((ndo
, "%d%s%s%s", EXTRACT_BE_U_2(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
674 DNS_RD(np
) ? "+" : "",
675 DNS_CD(np
) ? "%" : ""));
678 b2
= EXTRACT_BE_U_2(((const u_short
*)np
) + 1);
680 ND_PRINT((ndo
, " [b2&3=0x%x]", b2
));
682 if (DNS_OPCODE(np
) == IQUERY
) {
684 ND_PRINT((ndo
, " [%dq]", qdcount
));
686 ND_PRINT((ndo
, " [%da]", ancount
));
690 ND_PRINT((ndo
, " [%da]", ancount
));
692 ND_PRINT((ndo
, " [%dq]", qdcount
));
695 ND_PRINT((ndo
, " [%dn]", nscount
));
697 ND_PRINT((ndo
, " [%dau]", arcount
));
699 cp
= (const u_char
*)(np
+ 1);
701 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
704 while (cp
< ndo
->ndo_snapend
&& qdcount
--) {
705 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
715 /* Print remaining sections on -vv */
716 if (ndo
->ndo_vflag
> 1) {
718 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
720 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
721 ND_PRINT((ndo
, ","));
722 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
728 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
729 ND_PRINT((ndo
, " ns:"));
730 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
732 while (nscount
-- && cp
< ndo
->ndo_snapend
) {
733 ND_PRINT((ndo
, ","));
734 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
740 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
741 ND_PRINT((ndo
, " ar:"));
742 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
744 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
745 ND_PRINT((ndo
, ","));
746 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
754 ND_PRINT((ndo
, " (%d)", length
));
758 ND_PRINT((ndo
, "[|domain]"));