]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
0c3c80e101d383c0a82b93fd611ab97acc7d7867
[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.73 2001-02-20 07:22:17 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
34 #include <netinet/in.h>
35
36 #ifdef NOERROR
37 #undef NOERROR /* Solaris sucks */
38 #endif
39 #ifdef NOERROR
40 #undef T_UNSPEC /* SINIX does too */
41 #endif
42 #include "nameser.h"
43
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "extract.h" /* must come after interface.h */
50
51 static char *ns_ops[] = {
52 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
53 " op8", " updataA", " updateD", " updateDA",
54 " updateM", " updateMA", " zoneInit", " zoneRef",
55 };
56
57 static char *ns_resp[] = {
58 "", " FormErr", " ServFail", " NXDomain",
59 " NotImp", " Refused", " YXDomain", " YXRRSet",
60 " NXRRSet", " NotAuth", " NotZone", " Resp11",
61 " Resp12", " Resp13", " Resp14", " NoChange",
62 };
63
64 /* skip over a domain name */
65 static const u_char *
66 ns_nskip(register const u_char *cp, register const u_char *bp)
67 {
68 register u_char i;
69
70 if (((i = *cp++) & INDIR_MASK) == INDIR_MASK)
71 return (cp + 1);
72 if (cp >= snapend)
73 return(NULL);
74 while (i && cp < snapend) {
75 if ((i & INDIR_MASK) == EDNS0_MASK) {
76 int bitlen, bytelen;
77
78 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
79 return(NULL); /* unknown ELT */
80 if ((bitlen = *cp++) == 0)
81 bitlen = 256;
82 bytelen = (bitlen + 7) / 8;
83 cp += bytelen;
84 } else
85 cp += i;
86 if (cp >= snapend)
87 return(NULL);
88 i = *cp++;
89 }
90 return (cp);
91 }
92
93 /* print a <domain-name> */
94 static const u_char *
95 blabel_print(const u_char *cp)
96 {
97 int bitlen, slen, b;
98 int truncated = 0;
99 const u_char *bitp, *lim;
100 char tc;
101
102 if (cp >= snapend)
103 return(NULL);
104 if ((bitlen = *cp) == 0)
105 bitlen = 256;
106 slen = (bitlen + 3) / 4;
107 if ((lim = cp + 1 + slen) > snapend) {
108 truncated = 1;
109 lim = snapend;
110 }
111
112 /* print the bit string as a hex string */
113 printf("\\[x");
114 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++)
115 printf("%02x", *bitp);
116 if (bitp == lim)
117 printf("...");
118 else if (b > 4) {
119 tc = *bitp++;
120 printf("%02x", tc & (0xff << (8 - b)));
121 } else if (b > 0) {
122 tc = *bitp++;
123 printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
124 }
125 printf("/%d]", bitlen);
126
127 return(truncated ? NULL : lim);
128 }
129
130 static int
131 labellen(const u_char *cp)
132 {
133 register u_int i;
134
135 if (cp >= snapend)
136 return(-1);
137 i = *cp;
138 if ((i & INDIR_MASK) == EDNS0_MASK) {
139 int bitlen, elt;
140
141 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL)
142 return(-1);
143 if (cp + 1 >= snapend)
144 return(-1);
145 if ((bitlen = *(cp + 1)) == 0)
146 bitlen = 256;
147 return(((bitlen + 7) / 8) + 1);
148 } else
149 return(i);
150 }
151
152 static const u_char *
153 ns_nprint(register const u_char *cp, register const u_char *bp)
154 {
155 register u_int i, l;
156 register const u_char *rp = NULL;
157 register int compress = 0;
158 int chars_processed;
159 int elt;
160 int data_size = snapend - bp;
161
162 if ((l = labellen(cp)) < 0)
163 return(NULL);
164 if (cp >= snapend)
165 return(NULL);
166 chars_processed = 1;
167 if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
168 compress = 0;
169 rp = cp + l;
170 }
171
172 if (i != 0)
173 while (i && cp < snapend) {
174 if ((i & INDIR_MASK) == INDIR_MASK) {
175 if (!compress) {
176 rp = cp + 1;
177 compress = 1;
178 }
179 cp = bp + (((i << 8) | *cp) & 0x3fff);
180 if (cp >= snapend)
181 return(NULL);
182 if ((l = labellen(cp)) < 0)
183 return(NULL);
184 i = *cp++;
185 chars_processed++;
186
187 /*
188 * If we've looked at every character in
189 * the message, this pointer will make
190 * us look at some character again,
191 * which means we're looping.
192 */
193 if (chars_processed >= data_size) {
194 printf("<LOOP>");
195 return (NULL);
196 }
197 continue;
198 }
199 if ((i & INDIR_MASK) == EDNS0_MASK) {
200 elt = (i & ~INDIR_MASK);
201 switch(elt) {
202 case EDNS0_ELT_BITLABEL:
203 blabel_print(cp);
204 break;
205 default:
206 /* unknown ELT */
207 printf("<ELT %d>", elt);
208 return(NULL);
209 }
210 } else {
211 if (fn_printn(cp, l, snapend))
212 break;
213 }
214
215 cp += l;
216 chars_processed += l;
217 putchar('.');
218 if (cp >= snapend || (l = labellen(cp)) < 0)
219 return(NULL);
220 i = *cp++;
221 chars_processed++;
222 if (!compress)
223 rp += l + 1;
224 }
225 else
226 putchar('.');
227 return (rp);
228 }
229
230 /* print a <character-string> */
231 static const u_char *
232 ns_cprint(register const u_char *cp, register const u_char *bp)
233 {
234 register u_int i;
235
236 if (cp >= snapend)
237 return NULL;
238 i = *cp++;
239 (void)fn_printn(cp, i, snapend);
240 return (cp + i);
241 }
242
243 struct tok ns_type2str[] = {
244 { T_A, "A" },
245 { T_NS, "NS" },
246 { T_MD, "MD" },
247 { T_MF, "MF" },
248 { T_CNAME, "CNAME" },
249 { T_SOA, "SOA" },
250 { T_MB, "MB" },
251 { T_MG, "MG" },
252 { T_MR, "MR" },
253 { T_NULL, "NULL" },
254 { T_WKS, "WKS" },
255 { T_PTR, "PTR" },
256 { T_HINFO, "HINFO" },
257 { T_MINFO, "MINFO" },
258 { T_MX, "MX" },
259 { T_TXT, "TXT" },
260 { T_RP, "RP" },
261 { T_AFSDB, "AFSDB" },
262 { T_X25, "X25" },
263 { T_ISDN, "ISDN" },
264 { T_RT, "RT" },
265 { T_NSAP, "NSAP" },
266 { T_NSAP_PTR, "NSAP_PTR" },
267 { T_SIG, "SIG" },
268 { T_KEY, "KEY" },
269 { T_PX, "PX" },
270 { T_GPOS, "GPOS" },
271 { T_AAAA, "AAAA" },
272 { T_LOC, "LOC" },
273 { T_NXT, "NXT" },
274 { T_EID, "EID" },
275 { T_NIMLOC, "NIMLOC" },
276 { T_SRV, "SRV" },
277 { T_ATMA, "ATMA" },
278 { T_NAPTR, "NAPTR" },
279 { T_A6, "A6" },
280 { T_DNAME, "DNAME" },
281 { T_OPT, "OPT" },
282 { T_UINFO, "UINFO" },
283 { T_UID, "UID" },
284 { T_GID, "GID" },
285 { T_UNSPEC, "UNSPEC" },
286 { T_UNSPECA, "UNSPECA" },
287 { T_TKEY, "TKEY" },
288 { T_TSIG, "TSIG" },
289 { T_IXFR, "IXFR" },
290 { T_AXFR, "AXFR" },
291 { T_MAILB, "MAILB" },
292 { T_MAILA, "MAILA" },
293 { T_ANY, "ANY" },
294 { 0, NULL }
295 };
296
297 struct tok ns_class2str[] = {
298 { C_IN, "IN" }, /* Not used */
299 { C_CHAOS, "CHAOS" },
300 { C_HS, "HS" },
301 { C_ANY, "ANY" },
302 { 0, NULL }
303 };
304
305 /* print a query */
306 static const u_char *
307 ns_qprint(register const u_char *cp, register const u_char *bp)
308 {
309 register const u_char *np = cp;
310 register u_int i;
311
312 cp = ns_nskip(cp, bp);
313
314 if (cp + 4 > snapend || cp == NULL)
315 return(NULL);
316
317 /* print the qtype and qclass (if it's not IN) */
318 i = *cp++ << 8;
319 i |= *cp++;
320 printf(" %s", tok2str(ns_type2str, "Type%d", i));
321 i = *cp++ << 8;
322 i |= *cp++;
323 if (i != C_IN)
324 printf(" %s", tok2str(ns_class2str, "(Class %d)", i));
325
326 fputs("? ", stdout);
327 cp = ns_nprint(np, bp);
328 return(cp ? cp + 4 : NULL);
329 }
330
331 /* print a reply */
332 static const u_char *
333 ns_rprint(register const u_char *cp, register const u_char *bp)
334 {
335 register u_int class;
336 register u_short typ, len;
337 register const u_char *rp;
338
339 if (vflag) {
340 putchar(' ');
341 if ((cp = ns_nprint(cp, bp)) == NULL)
342 return NULL;
343 } else
344 cp = ns_nskip(cp, bp);
345
346 if (cp + 10 > snapend || cp == NULL)
347 return (snapend);
348
349 /* print the type/qtype and class (if it's not IN) */
350 typ = *cp++ << 8;
351 typ |= *cp++;
352 class = *cp++ << 8;
353 class |= *cp++;
354 if (class != C_IN && typ != T_OPT)
355 printf(" %s", tok2str(ns_class2str, "(Class %d)", class));
356
357 /* ignore ttl */
358 cp += 4;
359
360 len = *cp++ << 8;
361 len |= *cp++;
362
363 rp = cp + len;
364
365 printf(" %s", tok2str(ns_type2str, "Type%d", typ));
366 if (rp > snapend)
367 return(NULL);
368
369 switch (typ) {
370 case T_A:
371 if (cp + sizeof(struct in_addr) > snapend)
372 return(NULL);
373 printf(" %s", ipaddr_string(cp));
374 break;
375
376 case T_NS:
377 case T_CNAME:
378 case T_PTR:
379 #ifdef T_DNAME
380 case T_DNAME:
381 #endif
382 putchar(' ');
383 if (ns_nprint(cp, bp) == NULL)
384 return(NULL);
385 break;
386
387 case T_SOA:
388 if (!vflag)
389 break;
390 putchar(' ');
391 if ((cp = ns_nprint(cp, bp)) == NULL)
392 return(NULL);
393 putchar(' ');
394 if ((cp = ns_nprint(cp, bp)) == NULL)
395 return(NULL);
396 if (cp + 5 * 4 > snapend)
397 return(NULL);
398 printf(" %u", EXTRACT_32BITS(cp));
399 cp += 4;
400 printf(" %u", EXTRACT_32BITS(cp));
401 cp += 4;
402 printf(" %u", EXTRACT_32BITS(cp));
403 cp += 4;
404 printf(" %u", EXTRACT_32BITS(cp));
405 cp += 4;
406 printf(" %u", EXTRACT_32BITS(cp));
407 cp += 4;
408 break;
409 case T_MX:
410 putchar(' ');
411 if (cp + 2 > snapend)
412 return(NULL);
413 if (ns_nprint(cp + 2, bp) == NULL)
414 return(NULL);
415 printf(" %d", EXTRACT_16BITS(cp));
416 break;
417
418 case T_TXT:
419 putchar(' ');
420 (void)ns_cprint(cp, bp);
421 break;
422
423 #ifdef INET6
424 case T_AAAA:
425 if (cp + sizeof(struct in6_addr) > snapend)
426 return(NULL);
427 printf(" %s", ip6addr_string(cp));
428 break;
429
430 case T_A6:
431 {
432 struct in6_addr a;
433 int pbit, pbyte;
434
435 pbit = *cp;
436 pbyte = (pbit & ~7) / 8;
437 if (pbit > 128) {
438 printf(" %u(bad plen)", pbit);
439 break;
440 } else if (pbit < 128) {
441 memset(&a, 0, sizeof(a));
442 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
443 printf(" %u %s", pbit, ip6addr_string(&a));
444 }
445 if (pbit > 0) {
446 putchar(' ');
447 if (ns_nprint(cp + 1 + sizeof(a) - pbyte, bp) == NULL)
448 return(NULL);
449 }
450 break;
451 }
452 #endif /*INET6*/
453
454 case T_OPT:
455 printf(" UDPsize=%u", class);
456 break;
457
458 case T_UNSPECA: /* One long string */
459 if (cp + len > snapend)
460 return(NULL);
461 fn_printn(cp, len, snapend);
462 break;
463
464 case T_TSIG:
465 {
466 if (cp + len > snapend)
467 return(NULL);
468 if (!vflag)
469 break;
470 putchar(' ');
471 if ((cp = ns_nprint(cp, bp)) == NULL)
472 return(NULL);
473 cp += 6;
474 printf(" fudge=%u", EXTRACT_16BITS(cp));
475 cp += 2;
476 printf(" maclen=%u", EXTRACT_16BITS(cp));
477 cp += 2 + EXTRACT_16BITS(cp);
478 printf(" origid=%u", EXTRACT_16BITS(cp));
479 cp += 2;
480 printf(" error=%u", EXTRACT_16BITS(cp));
481 cp += 2;
482 printf(" otherlen=%u", EXTRACT_16BITS(cp));
483 cp += 2;
484 }
485 }
486 return (rp); /* XXX This isn't always right */
487 }
488
489 void
490 ns_print(register const u_char *bp, u_int length)
491 {
492 register const HEADER *np;
493 register int qdcount, ancount, nscount, arcount;
494 register const u_char *cp = NULL;
495
496 np = (const HEADER *)bp;
497 /* get the byte-order right */
498 qdcount = ntohs(np->qdcount);
499 ancount = ntohs(np->ancount);
500 nscount = ntohs(np->nscount);
501 arcount = ntohs(np->arcount);
502
503 if (DNS_QR(np)) {
504 /* this is a response */
505 printf(" %d%s%s%s%s%s%s",
506 ntohs(np->id),
507 ns_ops[DNS_OPCODE(np)],
508 ns_resp[DNS_RCODE(np)],
509 DNS_AA(np)? "*" : "",
510 DNS_RA(np)? "" : "-",
511 DNS_TC(np)? "|" : "",
512 DNS_CD(np)? "%" : "");
513
514 if (qdcount != 1)
515 printf(" [%dq]", qdcount);
516 /* Print QUESTION section on -vv */
517 cp = (const u_char *)(np + 1);
518 while (qdcount--) {
519 if (qdcount < ntohs(np->qdcount) - 1)
520 putchar(',');
521 if (vflag > 1) {
522 fputs(" q:", stdout);
523 if ((cp = ns_qprint((const u_char *)(np + 1), bp))
524 == NULL)
525 goto trunc;
526 } else {
527 if ((cp = ns_nskip((const u_char *)(np + 1), bp))
528 == NULL)
529 goto trunc;
530 cp += 4; /* skip QTYPE and QCLASS */
531 }
532 }
533 printf(" %d/%d/%d", ancount, nscount, arcount);
534 if (ancount--) {
535 if ((cp = ns_rprint(cp, bp)) == NULL)
536 goto trunc;
537 while (cp < snapend && ancount--) {
538 putchar(',');
539 if ((cp = ns_rprint(cp, bp)) == NULL)
540 goto trunc;
541 }
542 }
543 if (ancount > 0)
544 goto trunc;
545 /* Print NS and AR sections on -vv */
546 if (vflag > 1) {
547 if (cp < snapend && nscount--) {
548 fputs(" ns:", stdout);
549 if ((cp = ns_rprint(cp, bp)) == NULL)
550 goto trunc;
551 while (cp < snapend && nscount--) {
552 putchar(',');
553 if ((cp = ns_rprint(cp, bp)) == NULL)
554 goto trunc;
555 }
556 }
557 if (nscount > 0)
558 goto trunc;
559 if (cp < snapend && arcount--) {
560 fputs(" ar:", stdout);
561 if ((cp = ns_rprint(cp, bp)) == NULL)
562 goto trunc;
563 while (cp < snapend && arcount--) {
564 putchar(',');
565 if ((cp = ns_rprint(cp, bp)) == NULL)
566 goto trunc;
567 }
568 }
569 if (arcount > 0)
570 goto trunc;
571 }
572 }
573 else {
574 /* this is a request */
575 printf(" %d%s%s%s", ntohs(np->id), ns_ops[DNS_OPCODE(np)],
576 DNS_RD(np) ? "+" : "",
577 DNS_AD(np) ? "$" : "");
578
579 /* any weirdness? */
580 if (*(((u_short *)np)+1) & htons(0x6cf))
581 printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1)));
582
583 if (DNS_OPCODE(np) == IQUERY) {
584 if (qdcount)
585 printf(" [%dq]", qdcount);
586 if (ancount != 1)
587 printf(" [%da]", ancount);
588 }
589 else {
590 if (ancount)
591 printf(" [%da]", ancount);
592 if (qdcount != 1)
593 printf(" [%dq]", qdcount);
594 }
595 if (nscount)
596 printf(" [%dn]", nscount);
597 if (arcount)
598 printf(" [%dau]", arcount);
599
600 if (qdcount--) {
601 cp = ns_qprint((const u_char *)(np + 1),
602 (const u_char *)np);
603 if (!cp)
604 goto trunc;
605 while (cp < snapend && qdcount--) {
606 cp = ns_qprint((const u_char *)cp,
607 (const u_char *)np);
608 if (!cp)
609 goto trunc;
610 }
611 }
612 if (qdcount > 0)
613 goto trunc;
614
615 /* Print remaining sections on -vv */
616 if (vflag > 1) {
617 if (ancount--) {
618 if ((cp = ns_rprint(cp, bp)) == NULL)
619 goto trunc;
620 while (cp < snapend && ancount--) {
621 putchar(',');
622 if ((cp = ns_rprint(cp, bp)) == NULL)
623 goto trunc;
624 }
625 }
626 if (ancount > 0)
627 goto trunc;
628 if (cp < snapend && nscount--) {
629 fputs(" ns:", stdout);
630 if ((cp = ns_rprint(cp, bp)) == NULL)
631 goto trunc;
632 while (nscount-- && cp < snapend) {
633 putchar(',');
634 if ((cp = ns_rprint(cp, bp)) == NULL)
635 goto trunc;
636 }
637 }
638 if (nscount > 0)
639 goto trunc;
640 if (cp < snapend && arcount--) {
641 fputs(" ar:", stdout);
642 if ((cp = ns_rprint(cp, bp)) == NULL)
643 goto trunc;
644 while (cp < snapend && arcount--) {
645 putchar(',');
646 if ((cp = ns_rprint(cp, bp)) == NULL)
647 goto trunc;
648 }
649 }
650 if (arcount > 0)
651 goto trunc;
652 }
653 }
654 printf(" (%d)", length);
655 return;
656
657 trunc:
658 printf("[|domain]");
659 return;
660 }