]>
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.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.44 2000-04-27 03:44:05 itojun Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_var.h>
46 #include <netinet/udp.h>
47 #include <netinet/udp_var.h>
48 #include <netinet/tcp.h>
51 #undef NOERROR /* Solaris sucks */
54 #undef T_UNSPEC /* SINIX does too */
56 #include <arpa/nameser.h>
61 #include "interface.h"
62 #include "addrtoname.h"
63 #include "extract.h" /* must come after interface.h */
67 #define T_TXT 16 /* text strings */
70 #define T_RP 17 /* responsible person */
73 #define T_AFSDB 18 /* AFS cell database */
76 #define T_X25 19 /* X_25 calling address */
79 #define T_ISDN 20 /* ISDN calling address */
82 #define T_RT 21 /* router */
85 #define T_NSAP 22 /* NSAP address */
88 #define T_NSAP_PTR 23 /* reverse NSAP lookup (deprecated) */
91 #define T_SIG 24 /* security signature */
94 #define T_KEY 25 /* security key */
97 #define T_PX 26 /* X.400 mail mapping */
100 #define T_GPOS 27 /* geographical position (withdrawn) */
103 #define T_AAAA 28 /* IP6 Address */
106 #define T_LOC 29 /* Location Information */
109 #define T_NXT 30 /* Next Valid Name in Zone */
112 #define T_EID 31 /* Endpoint identifier */
115 #define T_NIMLOC 32 /* Nimrod locator */
118 #define T_SRV 33 /* Server selection */
121 #define T_ATMA 34 /* ATM Address */
124 #define T_NAPTR 35 /* Naming Authority PoinTeR */
127 #define T_A6 38 /* IP6 address (ipngwg-dns-lookups) */
131 #define T_UNSPEC 103 /* Unspecified format (binary data) */
134 #define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
138 #define C_CHAOS 3 /* for chaos net (MIT) */
141 #define C_HS 4 /* for Hesiod name server (MIT) (XXX) */
144 static char *ns_ops
[] = {
145 "", " inv_q", " stat", " op3", " notify", " op5", " op6", " op7",
146 " op8", " updataA", " updateD", " updateDA",
147 " updateM", " updateMA", " zoneInit", " zoneRef",
150 static char *ns_resp
[] = {
151 "", " FormErr", " ServFail", " NXDomain",
152 " NotImp", " Refused", " Resp6", " Resp7",
153 " Resp8", " Resp9", " Resp10", " Resp11",
154 " Resp12", " Resp13", " Resp14", " NoChange",
157 /* skip over a domain name */
158 static const u_char
*
159 ns_nskip(register const u_char
*cp
, register const u_char
*bp
)
163 if (((i
= *cp
++) & INDIR_MASK
) == INDIR_MASK
)
165 while (i
&& cp
< snapend
) {
172 /* print a <domain-name> */
173 static const u_char
*
174 ns_nprint(register const u_char
*cp
, register const u_char
*bp
)
177 register const u_char
*rp
;
178 register int compress
;
180 int data_size
= snapend
- bp
;
185 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
191 while (i
&& cp
< snapend
) {
192 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
193 cp
= bp
+ (((i
<< 8) | *cp
) & 0x3fff);
198 * If we've looked at every character in
199 * the message, this pointer will make
200 * us look at some character again,
201 * which means we're looping.
203 if (chars_processed
>= data_size
) {
204 fn_printn(cp
, 6, "<LOOP>");
211 if (fn_printn(cp
, i
, snapend
))
214 chars_processed
+= i
;
226 /* print a <character-string> */
227 static const u_char
*
228 ns_cprint(register const u_char
*cp
, register const u_char
*bp
)
233 (void)fn_printn(cp
, i
, snapend
);
237 static struct tok type2str
[] = {
242 { T_CNAME
, "CNAME" },
250 { T_HINFO
, "HINFO" },
251 { T_MINFO
, "MINFO" },
255 { T_AFSDB
, "AFSDB" },
260 { T_NSAP_PTR
, "NSAP_PTR" },
269 { T_NIMLOC
, "NIMLOC " },
272 { T_NAPTR
, "NAPTR " },
277 { T_UINFO
, "UINFO" },
286 { T_UNSPEC
, "UNSPEC" },
287 { T_UNSPECA
, "UNSPECA" },
289 { T_MAILB
, "MAILB" },
290 { T_MAILA
, "MAILA" },
295 static struct tok class2str
[] = {
296 { C_IN
, "IN" }, /* Not used */
297 { C_CHAOS
, "CHAOS)" },
305 ns_qprint(register const u_char
*cp
, register const u_char
*bp
)
307 register const u_char
*np
= cp
;
310 cp
= ns_nskip(cp
, bp
);
312 if (cp
+ 4 > snapend
)
315 /* print the qtype and qclass (if it's not IN) */
318 printf(" %s", tok2str(type2str
, "Type%d", i
));
322 printf(" %s", tok2str(class2str
, "(Class %d)", i
));
329 static const u_char
*
330 ns_rprint(register const u_char
*cp
, register const u_char
*bp
)
333 register u_short typ
, len
;
334 register const u_char
*rp
;
338 cp
= ns_nprint(cp
, bp
);
340 cp
= ns_nskip(cp
, bp
);
342 if (cp
+ 10 > snapend
)
345 /* print the type/qtype and class (if it's not IN) */
351 printf(" %s", tok2str(class2str
, "(Class %d)", i
));
361 printf(" %s", tok2str(type2str
, "Type%d", typ
));
365 printf(" %s", ipaddr_string(cp
));
372 case T_DNAME
: /*XXX not checked as there's no server support yet*/
375 (void)ns_nprint(cp
, bp
);
380 (void)ns_nprint(cp
+ 2, bp
);
381 printf(" %d", EXTRACT_16BITS(cp
));
386 (void)ns_cprint(cp
, bp
);
391 printf(" %s", ip6addr_string(cp
));
394 case T_A6
: /*XXX not checked as there's no server support yet*/
399 pbyte
= (*cp
+ 7) / 8;
400 memset(&a
, 0, sizeof(a
));
401 memcpy(&a
, cp
+ 1, pbyte
);
402 printf(" %u %s ", *cp
, ip6addr_string(&a
));
403 (void)ns_nprint(cp
+ 1 + pbyte
, bp
);
408 case T_UNSPECA
: /* One long string */
409 printf(" %.*s", len
, cp
);
412 return (rp
); /* XXX This isn't always right */
416 ns_print(register const u_char
*bp
, u_int length
)
418 register const HEADER
*np
;
419 register int qdcount
, ancount
, nscount
, arcount
;
420 register const u_char
*cp
;
422 np
= (const HEADER
*)bp
;
423 /* get the byte-order right */
424 qdcount
= ntohs(np
->qdcount
);
425 ancount
= ntohs(np
->ancount
);
426 nscount
= ntohs(np
->nscount
);
427 arcount
= ntohs(np
->arcount
);
430 /* this is a response */
431 printf(" %d%s%s%s%s%s%s",
441 printf(" [%dq]", qdcount
);
442 /* Print QUESTION section on -vv */
444 fputs(" q: ", stdout
);
445 cp
= ns_nprint((const u_char
*)(np
+ 1), bp
);
447 cp
= ns_nskip((const u_char
*)(np
+ 1), bp
);
448 printf(" %d/%d/%d", ancount
, nscount
, arcount
);
450 cp
= ns_rprint(cp
+ 4, bp
);
451 while (ancount
-- && cp
< snapend
) {
453 cp
= ns_rprint(cp
, bp
);
458 /* this is a request */
466 if (*(((u_short
*)np
)+1) & htons(0x6cf))
467 printf(" [b2&3=0x%x]", ntohs(*(((u_short
*)np
)+1)));
469 if (np
->opcode
== IQUERY
) {
471 printf(" [%dq]", qdcount
);
473 printf(" [%da]", ancount
);
477 printf(" [%da]", ancount
);
479 printf(" [%dq]", qdcount
);
482 printf(" [%dn]", nscount
);
484 printf(" [%dau]", arcount
);
486 ns_qprint((const u_char
*)(np
+ 1), (const u_char
*)np
);
488 printf(" (%d)", length
);