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