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