]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
498e9bc83edc7905f87f8c9db8382e4b3a2c5d79
[tcpdump] / print-domain.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.40 1999-10-17 21:37:11 mcr Exp $ (LBL)";
25 #endif
26
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30
31 #if __STDC__
32 struct mbuf;
33 struct rtentry;
34 #endif
35 #include <net/if.h>
36
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/udp.h>
42 #include <netinet/tcp.h>
43
44 #ifdef NOERROR
45 #undef NOERROR /* Solaris sucks */
46 #endif
47 #ifdef NOERROR
48 #undef T_UNSPEC /* SINIX does too */
49 #endif
50 #include <arpa/nameser.h>
51
52 #include <stdio.h>
53
54 #include "interface.h"
55 #include "addrtoname.h"
56 #include "extract.h" /* must come after interface.h */
57
58 /* Compatibility */
59 #ifndef T_TXT
60 #define T_TXT 16 /* text strings */
61 #endif
62 #ifndef T_RP
63 #define T_RP 17 /* responsible person */
64 #endif
65 #ifndef T_AFSDB
66 #define T_AFSDB 18 /* AFS cell database */
67 #endif
68 #ifndef T_X25
69 #define T_X25 19 /* X_25 calling address */
70 #endif
71 #ifndef T_ISDN
72 #define T_ISDN 20 /* ISDN calling address */
73 #endif
74 #ifndef T_RT
75 #define T_RT 21 /* router */
76 #endif
77 #ifndef T_NSAP
78 #define T_NSAP 22 /* NSAP address */
79 #endif
80 #ifndef T_NSAP_PTR
81 #define T_NSAP_PTR 23 /* reverse NSAP lookup (deprecated) */
82 #endif
83 #ifndef T_SIG
84 #define T_SIG 24 /* security signature */
85 #endif
86 #ifndef T_KEY
87 #define T_KEY 25 /* security key */
88 #endif
89 #ifndef T_PX
90 #define T_PX 26 /* X.400 mail mapping */
91 #endif
92 #ifndef T_GPOS
93 #define T_GPOS 27 /* geographical position (withdrawn) */
94 #endif
95 #ifndef T_AAAA
96 #define T_AAAA 28 /* IP6 Address */
97 #endif
98 #ifndef T_LOC
99 #define T_LOC 29 /* Location Information */
100 #endif
101
102 #ifndef T_UNSPEC
103 #define T_UNSPEC 103 /* Unspecified format (binary data) */
104 #endif
105 #ifndef T_UNSPECA
106 #define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
107 #endif
108
109 #ifndef C_CHAOS
110 #define C_CHAOS 3 /* for chaos net (MIT) */
111 #endif
112 #ifndef C_HS
113 #define C_HS 4 /* for Hesiod name server (MIT) (XXX) */
114 #endif
115
116 static char *ns_ops[] = {
117 "", " inv_q", " stat", " op3", " notify", " op5", " op6", " op7",
118 " op8", " updataA", " updateD", " updateDA",
119 " updateM", " updateMA", " zoneInit", " zoneRef",
120 };
121
122 static char *ns_resp[] = {
123 "", " FormErr", " ServFail", " NXDomain",
124 " NotImp", " Refused", " Resp6", " Resp7",
125 " Resp8", " Resp9", " Resp10", " Resp11",
126 " Resp12", " Resp13", " Resp14", " NoChange",
127 };
128
129 /* skip over a domain name */
130 static const u_char *
131 ns_nskip(register const u_char *cp, register const u_char *bp)
132 {
133 register u_char i;
134
135 if (((i = *cp++) & INDIR_MASK) == INDIR_MASK)
136 return (cp + 1);
137 while (i && cp < snapend) {
138 cp += i;
139 i = *cp++;
140 }
141 return (cp);
142 }
143
144 /* print a <domain-name> */
145 static const u_char *
146 ns_nprint(register const u_char *cp, register const u_char *bp)
147 {
148 register u_int i;
149 register const u_char *rp;
150 register int compress;
151
152 i = *cp++;
153 rp = cp + i;
154 if ((i & INDIR_MASK) == INDIR_MASK) {
155 rp = cp + 1;
156 compress = 1;
157 } else
158 compress = 0;
159 if (i != 0)
160 while (i && cp < snapend) {
161 if ((i & INDIR_MASK) == INDIR_MASK) {
162 cp = bp + (((i << 8) | *cp) & 0x3fff);
163 i = *cp++;
164 continue;
165 }
166 if (fn_printn(cp, i, snapend))
167 break;
168 cp += i;
169 putchar('.');
170 i = *cp++;
171 if (!compress)
172 rp += i + 1;
173 }
174 else
175 putchar('.');
176 return (rp);
177 }
178
179 /* print a <character-string> */
180 static const u_char *
181 ns_cprint(register const u_char *cp, register const u_char *bp)
182 {
183 register u_int i;
184
185 i = *cp++;
186 (void)fn_printn(cp, i, snapend);
187 return (cp + i);
188 }
189
190 static struct tok type2str[] = {
191 { T_A, "A" },
192 { T_NS, "NS" },
193 { T_MD, "MD" },
194 { T_MF, "MF" },
195 { T_CNAME, "CNAME" },
196 { T_SOA, "SOA" },
197 { T_MB, "MB" },
198 { T_MG, "MG" },
199 { T_MR, "MR" },
200 { T_NULL, "NULL" },
201 { T_WKS, "WKS" },
202 { T_PTR, "PTR" },
203 { T_HINFO, "HINFO" },
204 { T_MINFO, "MINFO" },
205 { T_MX, "MX" },
206 { T_TXT, "TXT" },
207 { T_RP, "RP" },
208 { T_AFSDB, "AFSDB" },
209 { T_X25, "X25" },
210 { T_ISDN, "ISDN" },
211 { T_RT, "RT" },
212 { T_NSAP, "NSAP" },
213 { T_NSAP_PTR, "NSAP_PTR" },
214 { T_SIG, "SIG" },
215 { T_KEY, "KEY" },
216 { T_PX, "PX" },
217 { T_GPOS, "GPOS" },
218 { T_AAAA, "AAAA" },
219 { T_LOC , "LOC " },
220 #ifndef T_UINFO
221 #define T_UINFO 100
222 #endif
223 { T_UINFO, "UINFO" },
224 #ifndef T_UID
225 #define T_UID 101
226 #endif
227 { T_UID, "UID" },
228 #ifndef T_GID
229 #define T_GID 102
230 #endif
231 { T_GID, "GID" },
232 { T_UNSPEC, "UNSPEC" },
233 { T_UNSPECA, "UNSPECA" },
234 { T_AXFR, "AXFR" },
235 { T_MAILB, "MAILB" },
236 { T_MAILA, "MAILA" },
237 { T_ANY, "ANY" },
238 { 0, NULL }
239 };
240
241 static struct tok class2str[] = {
242 { C_IN, "IN" }, /* Not used */
243 { C_CHAOS, "CHAOS)" },
244 { C_HS, "HS" },
245 { C_ANY, "ANY" },
246 { 0, NULL }
247 };
248
249 /* print a query */
250 static void
251 ns_qprint(register const u_char *cp, register const u_char *bp)
252 {
253 register const u_char *np = cp;
254 register u_int i;
255
256 cp = ns_nskip(cp, bp);
257
258 if (cp + 4 > snapend)
259 return;
260
261 /* print the qtype and qclass (if it's not IN) */
262 i = *cp++ << 8;
263 i |= *cp++;
264 printf(" %s", tok2str(type2str, "Type%d", i));
265 i = *cp++ << 8;
266 i |= *cp++;
267 if (i != C_IN)
268 printf(" %s", tok2str(class2str, "(Class %d)", i));
269
270 fputs("? ", stdout);
271 ns_nprint(np, bp);
272 }
273
274 /* print a reply */
275 static const u_char *
276 ns_rprint(register const u_char *cp, register const u_char *bp)
277 {
278 register u_int i;
279 register u_short typ, len;
280 register const u_char *rp;
281
282 if (vflag) {
283 putchar(' ');
284 cp = ns_nprint(cp, bp);
285 } else
286 cp = ns_nskip(cp, bp);
287
288 if (cp + 10 > snapend)
289 return (snapend);
290
291 /* print the type/qtype and class (if it's not IN) */
292 typ = *cp++ << 8;
293 typ |= *cp++;
294 i = *cp++ << 8;
295 i |= *cp++;
296 if (i != C_IN)
297 printf(" %s", tok2str(class2str, "(Class %d)", i));
298
299 /* ignore ttl */
300 cp += 4;
301
302 len = *cp++ << 8;
303 len |= *cp++;
304
305 rp = cp + len;
306
307 printf(" %s", tok2str(type2str, "Type%d", typ));
308 switch (typ) {
309
310 case T_A:
311 printf(" %s", ipaddr_string(cp));
312 break;
313
314 case T_NS:
315 case T_CNAME:
316 case T_PTR:
317 putchar(' ');
318 (void)ns_nprint(cp, bp);
319 break;
320
321 case T_MX:
322 putchar(' ');
323 (void)ns_nprint(cp + 2, bp);
324 printf(" %d", EXTRACT_16BITS(cp));
325 break;
326
327 case T_TXT:
328 putchar(' ');
329 (void)ns_cprint(cp, bp);
330 break;
331
332 case T_UNSPECA: /* One long string */
333 printf(" %.*s", len, cp);
334 break;
335 }
336 return (rp); /* XXX This isn't always right */
337 }
338
339 void
340 ns_print(register const u_char *bp, u_int length)
341 {
342 register const HEADER *np;
343 register int qdcount, ancount, nscount, arcount;
344 register const u_char *cp;
345
346 np = (const HEADER *)bp;
347 /* get the byte-order right */
348 qdcount = ntohs(np->qdcount);
349 ancount = ntohs(np->ancount);
350 nscount = ntohs(np->nscount);
351 arcount = ntohs(np->arcount);
352
353 if (np->qr) {
354 /* this is a response */
355 printf(" %d%s%s%s%s%s",
356 ntohs(np->id),
357 ns_ops[np->opcode],
358 ns_resp[np->rcode],
359 np->aa? "*" : "",
360 np->ra? "" : "-",
361 np->tc? "|" : "");
362 if (qdcount != 1)
363 printf(" [%dq]", qdcount);
364 /* Print QUESTION section on -vv */
365 if (vflag > 1) {
366 fputs(" q: ", stdout);
367 cp = ns_nprint((const u_char *)(np + 1), bp);
368 } else
369 cp = ns_nskip((const u_char *)(np + 1), bp);
370 printf(" %d/%d/%d", ancount, nscount, arcount);
371 if (ancount--) {
372 cp = ns_rprint(cp + 4, bp);
373 while (ancount-- && cp < snapend) {
374 putchar(',');
375 cp = ns_rprint(cp, bp);
376 }
377 }
378 }
379 else {
380 /* this is a request */
381 printf(" %d%s%s",
382 ntohs(np->id),
383 ns_ops[np->opcode],
384 np->rd? "+" : "");
385
386 /* any weirdness? */
387 if (*(((u_short *)np)+1) & htons(0x6ff))
388 printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1)));
389
390 if (np->opcode == IQUERY) {
391 if (qdcount)
392 printf(" [%dq]", qdcount);
393 if (ancount != 1)
394 printf(" [%da]", ancount);
395 }
396 else {
397 if (ancount)
398 printf(" [%da]", ancount);
399 if (qdcount != 1)
400 printf(" [%dq]", qdcount);
401 }
402 if (nscount)
403 printf(" [%dn]", nscount);
404 if (arcount)
405 printf(" [%dau]", arcount);
406
407 ns_qprint((const u_char *)(np + 1), (const u_char *)np);
408 }
409 printf(" (%d)", length);
410 }