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