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