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