]>
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.
26 #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", " updataA", " 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(register const u_char
*cp
)
60 if ((i
& INDIR_MASK
) == INDIR_MASK
)
62 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
65 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
66 return(NULL
); /* unknown ELT */
69 if ((bitlen
= *cp
++) == 0)
71 bytelen
= (bitlen
+ 7) / 8;
82 /* print a <domain-name> */
84 blabel_print(const u_char
*cp
)
87 const u_char
*bitp
, *lim
;
92 if ((bitlen
= *cp
) == 0)
94 slen
= (bitlen
+ 3) / 4;
97 /* print the bit string as a hex string */
99 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
101 printf("%02x", *bitp
);
106 printf("%02x", tc
& (0xff << (8 - b
)));
110 printf("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
112 printf("/%d]", bitlen
);
115 printf(".../%d]", bitlen
);
120 labellen(const u_char
*cp
)
127 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
129 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
130 printf("<ELT %d>", elt
);
133 if (!TTEST2(*(cp
+ 1), 1))
135 if ((bitlen
= *(cp
+ 1)) == 0)
137 return(((bitlen
+ 7) / 8) + 1);
143 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
146 register const u_char
*rp
= NULL
;
147 register int compress
= 0;
150 int data_size
= snapend
- bp
;
152 if ((l
= labellen(cp
)) == (u_int
)-1)
157 if (((i
= *cp
++) & INDIR_MASK
) != INDIR_MASK
) {
163 while (i
&& cp
< snapend
) {
164 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
171 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
172 if ((l
= labellen(cp
)) == (u_int
)-1)
180 * If we've looked at every character in
181 * the message, this pointer will make
182 * us look at some character again,
183 * which means we're looping.
185 if (chars_processed
>= data_size
) {
191 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
192 elt
= (i
& ~INDIR_MASK
);
194 case EDNS0_ELT_BITLABEL
:
195 if (blabel_print(cp
) == NULL
)
200 printf("<ELT %d>", elt
);
204 if (fn_printn(cp
, l
, snapend
))
209 chars_processed
+= l
;
211 if ((l
= labellen(cp
)) == (u_int
)-1)
225 /* print a <character-string> */
226 static const u_char
*
227 ns_cprint(register const u_char
*cp
)
234 if (fn_printn(cp
, i
, snapend
))
239 /* http://www.iana.org/assignments/dns-parameters */
240 const struct tok ns_type2str
[] = {
241 { T_A
, "A" }, /* RFC 1035 */
242 { T_NS
, "NS" }, /* RFC 1035 */
243 { T_MD
, "MD" }, /* RFC 1035 */
244 { T_MF
, "MF" }, /* RFC 1035 */
245 { T_CNAME
, "CNAME" }, /* RFC 1035 */
246 { T_SOA
, "SOA" }, /* RFC 1035 */
247 { T_MB
, "MB" }, /* RFC 1035 */
248 { T_MG
, "MG" }, /* RFC 1035 */
249 { T_MR
, "MR" }, /* RFC 1035 */
250 { T_NULL
, "NULL" }, /* RFC 1035 */
251 { T_WKS
, "WKS" }, /* RFC 1035 */
252 { T_PTR
, "PTR" }, /* RFC 1035 */
253 { T_HINFO
, "HINFO" }, /* RFC 1035 */
254 { T_MINFO
, "MINFO" }, /* RFC 1035 */
255 { T_MX
, "MX" }, /* RFC 1035 */
256 { T_TXT
, "TXT" }, /* RFC 1035 */
257 { T_RP
, "RP" }, /* RFC 1183 */
258 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
259 { T_X25
, "X25" }, /* RFC 1183 */
260 { T_ISDN
, "ISDN" }, /* RFC 1183 */
261 { T_RT
, "RT" }, /* RFC 1183 */
262 { T_NSAP
, "NSAP" }, /* RFC 1706 */
263 { T_NSAP_PTR
, "NSAP_PTR" },
264 { T_SIG
, "SIG" }, /* RFC 2535 */
265 { T_KEY
, "KEY" }, /* RFC 2535 */
266 { T_PX
, "PX" }, /* RFC 2163 */
267 { T_GPOS
, "GPOS" }, /* RFC 1712 */
268 { T_AAAA
, "AAAA" }, /* RFC 1886 */
269 { T_LOC
, "LOC" }, /* RFC 1876 */
270 { T_NXT
, "NXT" }, /* RFC 2535 */
271 { T_EID
, "EID" }, /* Nimrod */
272 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
273 { T_SRV
, "SRV" }, /* RFC 2782 */
274 { T_ATMA
, "ATMA" }, /* ATM Forum */
275 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
276 { T_KX
, "KX" }, /* RFC 2230 */
277 { T_CERT
, "CERT" }, /* RFC 2538 */
278 { T_A6
, "A6" }, /* RFC 2874 */
279 { T_DNAME
, "DNAME" }, /* RFC 2672 */
281 { T_OPT
, "OPT" }, /* RFC 2671 */
282 { T_APL
, "APL" }, /* RFC 3123 */
283 { T_DS
, "DS" }, /* RFC 4034 */
284 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
285 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
286 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
287 { T_NSEC
, "NSEC" }, /* RFC 4034 */
288 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
289 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
290 { T_UINFO
, "UINFO" },
293 { T_UNSPEC
, "UNSPEC" },
294 { T_UNSPECA
, "UNSPECA" },
295 { T_TKEY
, "TKEY" }, /* RFC 2930 */
296 { T_TSIG
, "TSIG" }, /* RFC 2845 */
297 { T_IXFR
, "IXFR" }, /* RFC 1995 */
298 { T_AXFR
, "AXFR" }, /* RFC 1035 */
299 { T_MAILB
, "MAILB" }, /* RFC 1035 */
300 { T_MAILA
, "MAILA" }, /* RFC 1035 */
305 const struct tok ns_class2str
[] = {
306 { C_IN
, "IN" }, /* Not used */
307 { C_CHAOS
, "CHAOS" },
314 static const u_char
*
315 ns_qprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
317 register const u_char
*np
= cp
;
318 register u_int i
, class;
322 if (cp
== NULL
|| !TTEST2(*cp
, 4))
325 /* print the qtype */
326 i
= EXTRACT_16BITS(cp
);
328 printf(" %s", tok2str(ns_type2str
, "Type%d", i
));
329 /* print the qclass (if it's not IN) */
330 i
= EXTRACT_16BITS(cp
);
337 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
346 cp
= ns_nprint(np
, bp
);
347 return(cp
? cp
+ 4 : NULL
);
351 static const u_char
*
352 ns_rprint(register const u_char
*cp
, register const u_char
*bp
, int is_mdns
)
354 register u_int i
, class, opt_flags
= 0;
355 register u_short typ
, len
;
356 register const u_char
*rp
;
360 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
365 if (cp
== NULL
|| !TTEST2(*cp
, 10))
368 /* print the type/qtype */
369 typ
= EXTRACT_16BITS(cp
);
371 /* print the class (if it's not IN and the type isn't OPT) */
372 i
= EXTRACT_16BITS(cp
);
375 class = (i
& ~C_CACHE_FLUSH
);
378 if (class != C_IN
&& typ
!= T_OPT
)
379 printf(" %s", tok2str(ns_class2str
, "(Class %d)", class));
381 if (i
& C_CACHE_FLUSH
)
382 printf(" (Cache flush)");
388 opt_flags
= EXTRACT_16BITS(cp
);
389 /* ignore rest of ttl field */
391 } else if (vflag
> 2) {
394 relts_print(EXTRACT_32BITS(cp
));
402 len
= EXTRACT_16BITS(cp
);
407 printf(" %s", tok2str(ns_type2str
, "Type%d", typ
));
413 if (!TTEST2(*cp
, sizeof(struct in_addr
)))
415 printf(" %s", intoa(htonl(EXTRACT_32BITS(cp
))));
425 if (ns_nprint(cp
, bp
) == NULL
)
433 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
436 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
438 if (!TTEST2(*cp
, 5 * 4))
440 printf(" %u", EXTRACT_32BITS(cp
));
442 printf(" %u", EXTRACT_32BITS(cp
));
444 printf(" %u", EXTRACT_32BITS(cp
));
446 printf(" %u", EXTRACT_32BITS(cp
));
448 printf(" %u", EXTRACT_32BITS(cp
));
455 if (ns_nprint(cp
+ 2, bp
) == NULL
)
457 printf(" %d", EXTRACT_16BITS(cp
));
474 if (ns_nprint(cp
+ 6, bp
) == NULL
)
476 printf(":%d %d %d", EXTRACT_16BITS(cp
+ 4),
477 EXTRACT_16BITS(cp
), EXTRACT_16BITS(cp
+ 2));
483 struct in6_addr addr
;
484 char ntop_buf
[INET6_ADDRSTRLEN
];
486 if (!TTEST2(*cp
, sizeof(struct in6_addr
)))
488 memcpy(&addr
, cp
, sizeof(struct in6_addr
));
490 inet_ntop(AF_INET6
, &addr
, ntop_buf
, sizeof(ntop_buf
)));
499 char ntop_buf
[INET6_ADDRSTRLEN
];
504 pbyte
= (pbit
& ~7) / 8;
506 printf(" %u(bad plen)", pbit
);
508 } else if (pbit
< 128) {
509 if (!TTEST2(*(cp
+ 1), sizeof(a
) - pbyte
))
511 memset(&a
, 0, sizeof(a
));
512 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
513 printf(" %u %s", pbit
,
514 inet_ntop(AF_INET6
, &a
, ntop_buf
, sizeof(ntop_buf
)));
518 if (ns_nprint(cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
526 printf(" UDPsize=%u", class);
527 if (opt_flags
& 0x8000)
531 case T_UNSPECA
: /* One long string */
532 if (!TTEST2(*cp
, len
))
534 if (fn_printn(cp
, len
, snapend
))
540 if (cp
+ len
> snapend
)
545 if ((cp
= ns_nprint(cp
, bp
)) == NULL
)
550 printf(" fudge=%u", EXTRACT_16BITS(cp
));
554 printf(" maclen=%u", EXTRACT_16BITS(cp
));
555 cp
+= 2 + EXTRACT_16BITS(cp
);
558 printf(" origid=%u", EXTRACT_16BITS(cp
));
562 printf(" error=%u", EXTRACT_16BITS(cp
));
566 printf(" otherlen=%u", EXTRACT_16BITS(cp
));
570 return (rp
); /* XXX This isn't always right */
574 ns_print(register const u_char
*bp
, u_int length
, int is_mdns
)
576 register const HEADER
*np
;
577 register int qdcount
, ancount
, nscount
, arcount
;
578 register const u_char
*cp
;
581 np
= (const HEADER
*)bp
;
583 /* get the byte-order right */
584 qdcount
= EXTRACT_16BITS(&np
->qdcount
);
585 ancount
= EXTRACT_16BITS(&np
->ancount
);
586 nscount
= EXTRACT_16BITS(&np
->nscount
);
587 arcount
= EXTRACT_16BITS(&np
->arcount
);
590 /* this is a response */
591 printf("%d%s%s%s%s%s%s",
592 EXTRACT_16BITS(&np
->id
),
593 ns_ops
[DNS_OPCODE(np
)],
594 ns_resp
[DNS_RCODE(np
)],
595 DNS_AA(np
)? "*" : "",
596 DNS_RA(np
)? "" : "-",
597 DNS_TC(np
)? "|" : "",
598 DNS_AD(np
)? "$" : "");
601 printf(" [%dq]", qdcount
);
602 /* Print QUESTION section on -vv */
603 cp
= (const u_char
*)(np
+ 1);
605 if (qdcount
< EXTRACT_16BITS(&np
->qdcount
) - 1)
608 fputs(" q:", stdout
);
609 if ((cp
= ns_qprint(cp
, bp
, is_mdns
)) == NULL
)
612 if ((cp
= ns_nskip(cp
)) == NULL
)
614 cp
+= 4; /* skip QTYPE and QCLASS */
617 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
619 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
621 while (cp
< snapend
&& ancount
--) {
623 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
629 /* Print NS and AR sections on -vv */
631 if (cp
< snapend
&& nscount
--) {
632 fputs(" ns:", stdout
);
633 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
635 while (cp
< snapend
&& nscount
--) {
637 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
643 if (cp
< snapend
&& arcount
--) {
644 fputs(" ar:", stdout
);
645 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
647 while (cp
< snapend
&& arcount
--) {
649 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
658 /* this is a request */
659 printf("%d%s%s%s", EXTRACT_16BITS(&np
->id
), ns_ops
[DNS_OPCODE(np
)],
660 DNS_RD(np
) ? "+" : "",
661 DNS_CD(np
) ? "%" : "");
664 b2
= EXTRACT_16BITS(((u_short
*)np
)+1);
666 printf(" [b2&3=0x%x]", b2
);
668 if (DNS_OPCODE(np
) == IQUERY
) {
670 printf(" [%dq]", qdcount
);
672 printf(" [%da]", ancount
);
676 printf(" [%da]", ancount
);
678 printf(" [%dq]", qdcount
);
681 printf(" [%dn]", nscount
);
683 printf(" [%dau]", arcount
);
685 cp
= (const u_char
*)(np
+ 1);
687 cp
= ns_qprint(cp
, (const u_char
*)np
, is_mdns
);
690 while (cp
< snapend
&& qdcount
--) {
691 cp
= ns_qprint((const u_char
*)cp
,
701 /* Print remaining sections on -vv */
704 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
706 while (cp
< snapend
&& ancount
--) {
708 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
714 if (cp
< snapend
&& nscount
--) {
715 fputs(" ns:", stdout
);
716 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
718 while (nscount
-- && cp
< snapend
) {
720 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
726 if (cp
< snapend
&& arcount
--) {
727 fputs(" ar:", stdout
);
728 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
730 while (cp
< snapend
&& arcount
--) {
732 if ((cp
= ns_rprint(cp
, bp
, is_mdns
)) == NULL
)
740 printf(" (%d)", length
);