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