]>
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 #define NETDISSECT_REWORKED
27 #include <tcpdump-stdinc.h>
33 #include "interface.h"
34 #include "addrtoname.h"
35 #include "extract.h" /* must come after interface.h */
37 static const char *ns_ops
[] = {
38 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
39 " op8", " updateA", " updateD", " updateDA",
40 " updateM", " updateMA", " zoneInit", " zoneRef",
43 static const char *ns_resp
[] = {
44 "", " FormErr", " ServFail", " NXDomain",
45 " NotImp", " Refused", " YXDomain", " YXRRSet",
46 " NXRRSet", " NotAuth", " NotZone", " Resp11",
47 " Resp12", " Resp13", " Resp14", " NoChange",
50 /* skip over a domain name */
52 ns_nskip(netdissect_options
*ndo
,
53 register const u_char
*cp
)
57 if (!ND_TTEST2(*cp
, 1))
61 if ((i
& INDIR_MASK
) == INDIR_MASK
)
63 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
66 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
67 return(NULL
); /* unknown ELT */
68 if (!ND_TTEST2(*cp
, 1))
70 if ((bitlen
= *cp
++) == 0)
72 bytelen
= (bitlen
+ 7) / 8;
76 if (!ND_TTEST2(*cp
, 1))
83 /* print a <domain-name> */
85 blabel_print(netdissect_options
*ndo
,
89 const u_char
*bitp
, *lim
;
92 if (!ND_TTEST2(*cp
, 1))
94 if ((bitlen
= *cp
) == 0)
96 slen
= (bitlen
+ 3) / 4;
99 /* print the bit string as a hex string */
100 ND_PRINT((ndo
, "\\[x"));
101 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
103 ND_PRINT((ndo
, "%02x", *bitp
));
108 ND_PRINT((ndo
, "%02x", tc
& (0xff << (8 - b
))));
112 ND_PRINT((ndo
, "%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
))));
114 ND_PRINT((ndo
, "/%d]", bitlen
));
117 ND_PRINT((ndo
, ".../%d]", bitlen
));
122 labellen(netdissect_options
*ndo
,
127 if (!ND_TTEST2(*cp
, 1))
130 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
132 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
133 ND_PRINT((ndo
, "<ELT %d>", elt
));
136 if (!ND_TTEST2(*(cp
+ 1), 1))
138 if ((bitlen
= *(cp
+ 1)) == 0)
140 return(((bitlen
+ 7) / 8) + 1);
146 ns_nprint(netdissect_options
*ndo
,
147 register const u_char
*cp
, register const u_char
*bp
)
150 register const u_char
*rp
= NULL
;
151 register int compress
= 0;
154 int data_size
= ndo
->ndo_snapend
- bp
;
156 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
158 if (!ND_TTEST2(*cp
, 1))
161 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
167 while (i
&& cp
< ndo
->ndo_snapend
) {
168 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
173 if (!ND_TTEST2(*cp
, 1))
175 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
176 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
178 if (!ND_TTEST2(*cp
, 1))
184 * If we've looked at every character in
185 * the message, this pointer will make
186 * us look at some character again,
187 * which means we're looping.
189 if (chars_processed
>= data_size
) {
190 ND_PRINT((ndo
, "<LOOP>"));
195 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
196 elt
= (i
& ~INDIR_MASK
);
198 case EDNS0_ELT_BITLABEL
:
199 if (blabel_print(ndo
, cp
) == NULL
)
204 ND_PRINT((ndo
, "<ELT %d>", elt
));
208 if (fn_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
213 chars_processed
+= l
;
214 ND_PRINT((ndo
, "."));
215 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
217 if (!ND_TTEST2(*cp
, 1))
225 ND_PRINT((ndo
, "."));
229 /* print a <character-string> */
230 static const u_char
*
231 ns_cprint(netdissect_options
*ndo
,
232 register const u_char
*cp
)
236 if (!ND_TTEST2(*cp
, 1))
239 if (fn_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
244 /* http://www.iana.org/assignments/dns-parameters */
245 const 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 const struct tok ns_class2str
[] = {
311 { C_IN
, "IN" }, /* Not used */
312 { C_CHAOS
, "CHAOS" },
319 static const u_char
*
320 ns_qprint(netdissect_options
*ndo
,
321 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
323 register const u_char
*np
= cp
;
324 register u_int i
, class;
326 cp
= ns_nskip(ndo
, cp
);
328 if (cp
== NULL
|| !ND_TTEST2(*cp
, 4))
331 /* print the qtype */
332 i
= EXTRACT_16BITS(cp
);
334 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", i
)));
335 /* print the qclass (if it's not IN) */
336 i
= EXTRACT_16BITS(cp
);
343 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
345 ND_PRINT((ndo
, i
& C_QU
? " (QU)" : " (QM)"));
348 ND_PRINT((ndo
, "? "));
349 cp
= ns_nprint(ndo
, np
, bp
);
350 return(cp
? cp
+ 4 : NULL
);
354 static const u_char
*
355 ns_rprint(netdissect_options
*ndo
,
356 register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
358 register u_int i
, class, opt_flags
= 0;
359 register u_short typ
, len
;
360 register const u_char
*rp
;
362 if (ndo
->ndo_vflag
) {
363 ND_PRINT((ndo
, " "));
364 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
367 cp
= ns_nskip(ndo
, cp
);
369 if (cp
== NULL
|| !ND_TTEST2(*cp
, 10))
370 return (ndo
->ndo_snapend
);
372 /* print the type/qtype */
373 typ
= EXTRACT_16BITS(cp
);
375 /* print the class (if it's not IN and the type isn't OPT) */
376 i
= EXTRACT_16BITS(cp
);
379 class = (i
& ~C_CACHE_FLUSH
);
382 if (class != C_IN
&& typ
!= T_OPT
)
383 ND_PRINT((ndo
, " %s", tok2str(ns_class2str
, "(Class %d)", class)));
385 if (i
& C_CACHE_FLUSH
)
386 ND_PRINT((ndo
, " (Cache flush)"));
392 opt_flags
= EXTRACT_16BITS(cp
);
393 /* ignore rest of ttl field */
395 } else if (ndo
->ndo_vflag
> 2) {
397 ND_PRINT((ndo
, " ["));
398 relts_print(ndo
, EXTRACT_32BITS(cp
));
399 ND_PRINT((ndo
, "]"));
406 len
= EXTRACT_16BITS(cp
);
411 ND_PRINT((ndo
, " %s", tok2str(ns_type2str
, "Type%d", typ
)));
412 if (rp
> ndo
->ndo_snapend
)
417 if (!ND_TTEST2(*cp
, sizeof(struct in_addr
)))
419 ND_PRINT((ndo
, " %s", intoa(htonl(EXTRACT_32BITS(cp
)))));
428 ND_PRINT((ndo
, " "));
429 if (ns_nprint(ndo
, cp
, bp
) == NULL
)
436 ND_PRINT((ndo
, " "));
437 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
439 ND_PRINT((ndo
, " "));
440 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
442 if (!ND_TTEST2(*cp
, 5 * 4))
444 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
446 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
448 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
450 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
452 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
456 ND_PRINT((ndo
, " "));
457 if (!ND_TTEST2(*cp
, 2))
459 if (ns_nprint(ndo
, cp
+ 2, bp
) == NULL
)
461 ND_PRINT((ndo
, " %d", EXTRACT_16BITS(cp
)));
466 ND_PRINT((ndo
, " \""));
467 cp
= ns_cprint(ndo
, cp
);
470 ND_PRINT((ndo
, "\""));
475 ND_PRINT((ndo
, " "));
476 if (!ND_TTEST2(*cp
, 6))
478 if (ns_nprint(ndo
, cp
+ 6, bp
) == NULL
)
480 ND_PRINT((ndo
, ":%d %d %d", EXTRACT_16BITS(cp
+ 4),
481 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2)));
487 struct in6_addr addr
;
488 char ntop_buf
[INET6_ADDRSTRLEN
];
490 if (!ND_TTEST2(*cp
, sizeof(struct in6_addr
)))
492 memcpy(&addr
, cp
, sizeof(struct in6_addr
));
493 ND_PRINT((ndo
, " %s",
494 inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
))));
503 char ntop_buf
[INET6_ADDRSTRLEN
];
505 if (!ND_TTEST2(*cp
, 1))
508 pbyte
= (pbit
& ~7) / 8;
510 ND_PRINT((ndo
, " %u(bad plen)", pbit
));
512 } else if (pbit
< 128) {
513 if (!ND_TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
515 memset(&a
, 0, sizeof(a
));
516 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
517 ND_PRINT((ndo
, " %u %s", pbit
,
518 inet_ntop(AF_INET6
, &a
, ntop_buf
, sizeof(ntop_buf
))));
521 ND_PRINT((ndo
, " "));
522 if (ns_nprint(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
530 ND_PRINT((ndo
, " UDPsize=%u", class));
531 if (opt_flags
& 0x8000)
532 ND_PRINT((ndo
, " OK"));
535 case T_UNSPECA
: /* One long string */
536 if (!ND_TTEST2(*cp
, len
))
538 if (fn_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
544 if (cp
+ len
> ndo
->ndo_snapend
)
548 ND_PRINT((ndo
, " "));
549 if ((cp
= ns_nprint(ndo
, cp
, bp
)) == NULL
)
552 if (!ND_TTEST2(*cp
, 2))
554 ND_PRINT((ndo
, " fudge=%u", EXTRACT_16BITS(cp
)));
556 if (!ND_TTEST2(*cp
, 2))
558 ND_PRINT((ndo
, " maclen=%u", EXTRACT_16BITS(cp
)));
559 cp
+= 2 + EXTRACT_16BITS(cp
);
560 if (!ND_TTEST2(*cp
, 2))
562 ND_PRINT((ndo
, " origid=%u", EXTRACT_16BITS(cp
)));
564 if (!ND_TTEST2(*cp
, 2))
566 ND_PRINT((ndo
, " error=%u", EXTRACT_16BITS(cp
)));
568 if (!ND_TTEST2(*cp
, 2))
570 ND_PRINT((ndo
, " otherlen=%u", EXTRACT_16BITS(cp
)));
574 return (rp
); /* XXX This isn't always right */
578 ns_print(netdissect_options
*ndo
,
579 register const u_char
*bp
, u_int length
, int is_mdns
)
581 register const HEADER
*np
;
582 register int qdcount
, ancount
, nscount
, arcount
;
583 register const u_char
*cp
;
586 np
= (const HEADER
*)bp
;
588 /* get the byte-order right */
589 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
590 ancount
= EXTRACT_16BITS(&np
->ancount
);
591 nscount
= EXTRACT_16BITS(&np
->nscount
);
592 arcount
= EXTRACT_16BITS(&np
->arcount
);
595 /* this is a response */
596 ND_PRINT((ndo
, "%d%s%s%s%s%s%s",
597 EXTRACT_16BITS(&np
->id
),
598 ns_ops
[DNS_OPCODE(np
)],
599 ns_resp
[DNS_RCODE(np
)],
600 DNS_AA(np
)? "*" : "",
601 DNS_RA(np
)? "" : "-",
602 DNS_TC(np
)? "|" : "",
603 DNS_AD(np
)? "$" : ""));
606 ND_PRINT((ndo
, " [%dq]", qdcount
));
607 /* Print QUESTION section on -vv */
608 cp
= (const u_char
*)(np
+ 1);
610 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
611 ND_PRINT((ndo
, ","));
612 if (ndo
->ndo_vflag
> 1) {
613 ND_PRINT((ndo
, " q:"));
614 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
617 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
619 cp
+= 4; /* skip QTYPE and QCLASS */
622 ND_PRINT((ndo
, " %d/%d/%d", ancount
, nscount
, arcount
));
624 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
626 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
627 ND_PRINT((ndo
, ","));
628 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
634 /* Print NS and AR sections on -vv */
635 if (ndo
->ndo_vflag
> 1) {
636 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
637 ND_PRINT((ndo
, " ns:"));
638 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
640 while (cp
< ndo
->ndo_snapend
&& nscount
--) {
641 ND_PRINT((ndo
, ","));
642 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
648 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
649 ND_PRINT((ndo
, " ar:"));
650 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
652 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
653 ND_PRINT((ndo
, ","));
654 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
663 /* this is a request */
664 ND_PRINT((ndo
, "%d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
665 DNS_RD(np
) ? "+" : "",
666 DNS_CD(np
) ? "%" : ""));
669 b2
= EXTRACT_16BITS(((u_short
*)np
)+1);
671 ND_PRINT((ndo
, " [b2&3=0x%x]", b2
));
673 if (DNS_OPCODE(np
) == IQUERY
) {
675 ND_PRINT((ndo
, " [%dq]", qdcount
));
677 ND_PRINT((ndo
, " [%da]", ancount
));
681 ND_PRINT((ndo
, " [%da]", ancount
));
683 ND_PRINT((ndo
, " [%dq]", qdcount
));
686 ND_PRINT((ndo
, " [%dn]", nscount
));
688 ND_PRINT((ndo
, " [%dau]", arcount
));
690 cp
= (const u_char
*)(np
+ 1);
692 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
695 while (cp
< ndo
->ndo_snapend
&& qdcount
--) {
696 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
706 /* Print remaining sections on -vv */
707 if (ndo
->ndo_vflag
> 1) {
709 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
711 while (cp
< ndo
->ndo_snapend
&& ancount
--) {
712 ND_PRINT((ndo
, ","));
713 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
719 if (cp
< ndo
->ndo_snapend
&& nscount
--) {
720 ND_PRINT((ndo
, " ns:"));
721 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
723 while (nscount
-- && cp
< ndo
->ndo_snapend
) {
724 ND_PRINT((ndo
, ","));
725 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
731 if (cp
< ndo
->ndo_snapend
&& arcount
--) {
732 ND_PRINT((ndo
, " ar:"));
733 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
735 while (cp
< ndo
->ndo_snapend
&& arcount
--) {
736 ND_PRINT((ndo
, ","));
737 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
745 ND_PRINT((ndo
, " (%d)", length
));
749 ND_PRINT((ndo
, "[|domain]"));