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