Deleted Added
sdiff udiff text old ( 56896 ) new ( 75118 )
full compact
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

--- 4 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/contrib/tcpdump/print-domain.c 56896 2000-01-30 01:05:24Z fenner $
22 */
23
24#ifndef lint
25static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.42 1999/11/21 09:36:50 fenner Exp $ (LBL)";
27#endif
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <sys/param.h>
34#include <sys/time.h>
35#include <sys/socket.h>
36
37#if __STDC__
38struct mbuf;
39struct rtentry;
40#endif
41#include <net/if.h>
42
43#include <netinet/in.h>
44#include <net/ethernet.h>
45#include <netinet/in_systm.h>
46#include <netinet/ip.h>
47#include <netinet/ip_var.h>
48#include <netinet/udp.h>
49#include <netinet/udp_var.h>
50#include <netinet/tcp.h>
51
52#ifdef NOERROR
53#undef NOERROR /* Solaris sucks */
54#endif
55#ifdef NOERROR
56#undef T_UNSPEC /* SINIX does too */
57#endif
58#include <arpa/nameser.h>
59
60#include <stdio.h>
61#include <string.h>
62
63#include "interface.h"
64#include "addrtoname.h"
65#include "extract.h" /* must come after interface.h */
66

--- 54 unchanged lines hidden (view full) ---

121#endif
122#ifndef T_ATMA
123#define T_ATMA 34 /* ATM Address */
124#endif
125#ifndef T_NAPTR
126#define T_NAPTR 35 /* Naming Authority PoinTeR */
127#endif
128#ifndef T_A6
129#define T_A6 38 /* IP6 address (ipngwg-dns-lookups) */
130#endif
131
132#ifndef T_UNSPEC
133#define T_UNSPEC 103 /* Unspecified format (binary data) */
134#endif
135#ifndef T_UNSPECA
136#define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
137#endif
138
139#ifndef C_CHAOS

--- 19 unchanged lines hidden (view full) ---

159/* skip over a domain name */
160static const u_char *
161ns_nskip(register const u_char *cp, register const u_char *bp)
162{
163 register u_char i;
164
165 if (((i = *cp++) & INDIR_MASK) == INDIR_MASK)
166 return (cp + 1);
167 while (i && cp < snapend) {
168 cp += i;
169 i = *cp++;
170 }
171 return (cp);
172}
173
174/* print a <domain-name> */
175static const u_char *
176ns_nprint(register const u_char *cp, register const u_char *bp)
177{
178 register u_int i;
179 register const u_char *rp;
180 register int compress;
181
182 i = *cp++;
183 rp = cp + i;
184 if ((i & INDIR_MASK) == INDIR_MASK) {
185 rp = cp + 1;
186 compress = 1;
187 } else
188 compress = 0;
189 if (i != 0)
190 while (i && cp < snapend) {
191 if ((i & INDIR_MASK) == INDIR_MASK) {
192 cp = bp + (((i << 8) | *cp) & 0x3fff);
193 i = *cp++;
194 continue;
195 }
196 if (fn_printn(cp, i, snapend))
197 break;
198 cp += i;
199 putchar('.');
200 i = *cp++;
201 if (!compress)
202 rp += i + 1;
203 }
204 else
205 putchar('.');
206 return (rp);
207}
208
209/* print a <character-string> */
210static const u_char *
211ns_cprint(register const u_char *cp, register const u_char *bp)
212{
213 register u_int i;
214
215 i = *cp++;
216 (void)fn_printn(cp, i, snapend);
217 return (cp + i);
218}
219
220static struct tok type2str[] = {
221 { T_A, "A" },
222 { T_NS, "NS" },

--- 26 unchanged lines hidden (view full) ---

249 { T_LOC, "LOC " },
250 { T_NXT, "NXT " },
251 { T_EID, "EID " },
252 { T_NIMLOC, "NIMLOC " },
253 { T_SRV, "SRV " },
254 { T_ATMA, "ATMA " },
255 { T_NAPTR, "NAPTR " },
256 { T_A6, "A6 " },
257#ifndef T_UINFO
258#define T_UINFO 100
259#endif
260 { T_UINFO, "UINFO" },
261#ifndef T_UID
262#define T_UID 101
263#endif
264 { T_UID, "UID" },
265#ifndef T_GID
266#define T_GID 102
267#endif
268 { T_GID, "GID" },
269 { T_UNSPEC, "UNSPEC" },
270 { T_UNSPECA, "UNSPECA" },
271 { T_AXFR, "AXFR" },
272 { T_MAILB, "MAILB" },
273 { T_MAILA, "MAILA" },
274 { T_ANY, "ANY" },
275 { 0, NULL }
276};
277
278static struct tok class2str[] = {
279 { C_IN, "IN" }, /* Not used */
280 { C_CHAOS, "CHAOS)" },
281 { C_HS, "HS" },
282 { C_ANY, "ANY" },
283 { 0, NULL }
284};
285
286/* print a query */
287static void
288ns_qprint(register const u_char *cp, register const u_char *bp)
289{
290 register const u_char *np = cp;
291 register u_int i;
292
293 cp = ns_nskip(cp, bp);
294
295 if (cp + 4 > snapend)
296 return;
297
298 /* print the qtype and qclass (if it's not IN) */
299 i = *cp++ << 8;
300 i |= *cp++;
301 printf(" %s", tok2str(type2str, "Type%d", i));
302 i = *cp++ << 8;
303 i |= *cp++;
304 if (i != C_IN)
305 printf(" %s", tok2str(class2str, "(Class %d)", i));
306
307 fputs("? ", stdout);
308 ns_nprint(np, bp);
309}
310
311/* print a reply */
312static const u_char *
313ns_rprint(register const u_char *cp, register const u_char *bp)
314{
315 register u_int i;
316 register u_short typ, len;
317 register const u_char *rp;
318
319 if (vflag) {
320 putchar(' ');
321 cp = ns_nprint(cp, bp);
322 } else
323 cp = ns_nskip(cp, bp);
324
325 if (cp + 10 > snapend)
326 return (snapend);
327
328 /* print the type/qtype and class (if it's not IN) */
329 typ = *cp++ << 8;
330 typ |= *cp++;
331 i = *cp++ << 8;
332 i |= *cp++;
333 if (i != C_IN)
334 printf(" %s", tok2str(class2str, "(Class %d)", i));
335
336 /* ignore ttl */
337 cp += 4;
338
339 len = *cp++ << 8;
340 len |= *cp++;
341
342 rp = cp + len;
343
344 printf(" %s", tok2str(type2str, "Type%d", typ));
345 switch (typ) {
346
347 case T_A:
348 printf(" %s", ipaddr_string(cp));
349 break;
350
351 case T_NS:
352 case T_CNAME:
353 case T_PTR:
354#ifdef T_DNAME
355 case T_DNAME: /*XXX not checked as there's no server support yet*/
356#endif
357 putchar(' ');
358 (void)ns_nprint(cp, bp);
359 break;
360
361 case T_MX:
362 putchar(' ');
363 (void)ns_nprint(cp + 2, bp);
364 printf(" %d", EXTRACT_16BITS(cp));
365 break;
366
367 case T_TXT:
368 putchar(' ');
369 (void)ns_cprint(cp, bp);
370 break;
371
372#ifdef INET6
373 case T_AAAA:
374 printf(" %s", ip6addr_string(cp));
375 break;
376
377 case T_A6: /*XXX not checked as there's no server support yet*/
378 {
379 struct in6_addr a;
380 int pbyte;
381
382 pbyte = (*cp + 7) / 8;
383 memset(&a, 0, sizeof(a));
384 memcpy(&a, cp + 1, pbyte);
385 printf(" %u %s ", *cp, ip6addr_string(&a));
386 (void)ns_nprint(cp + 1 + pbyte, bp);
387 break;
388 }
389#endif /*INET6*/
390
391 case T_UNSPECA: /* One long string */
392 printf(" %.*s", len, cp);
393 break;
394 }
395 return (rp); /* XXX This isn't always right */
396}
397
398void
399ns_print(register const u_char *bp, u_int length)
400{
401 register const HEADER *np;
402 register int qdcount, ancount, nscount, arcount;
403 register const u_char *cp;
404
405 np = (const HEADER *)bp;
406 /* get the byte-order right */
407 qdcount = ntohs(np->qdcount);
408 ancount = ntohs(np->ancount);
409 nscount = ntohs(np->nscount);
410 arcount = ntohs(np->arcount);
411
412 if (np->qr) {
413 /* this is a response */
414 printf(" %d%s%s%s%s%s",
415 ntohs(np->id),
416 ns_ops[np->opcode],
417 ns_resp[np->rcode],
418 np->aa? "*" : "",
419 np->ra? "" : "-",
420 np->tc? "|" : "");
421 if (qdcount != 1)
422 printf(" [%dq]", qdcount);
423 /* Print QUESTION section on -vv */
424 if (vflag > 1) {
425 fputs(" q: ", stdout);
426 cp = ns_nprint((const u_char *)(np + 1), bp);
427 } else
428 cp = ns_nskip((const u_char *)(np + 1), bp);
429 printf(" %d/%d/%d", ancount, nscount, arcount);
430 if (ancount--) {
431 cp = ns_rprint(cp + 4, bp);
432 while (ancount-- && cp < snapend) {
433 putchar(',');
434 cp = ns_rprint(cp, bp);
435 }
436 }
437 }
438 else {
439 /* this is a request */
440 printf(" %d%s%s",
441 ntohs(np->id),
442 ns_ops[np->opcode],
443 np->rd? "+" : "");
444
445 /* any weirdness? */
446 if (*(((u_short *)np)+1) & htons(0x6ff))
447 printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1)));
448
449 if (np->opcode == IQUERY) {
450 if (qdcount)
451 printf(" [%dq]", qdcount);
452 if (ancount != 1)
453 printf(" [%da]", ancount);
454 }
455 else {
456 if (ancount)
457 printf(" [%da]", ancount);
458 if (qdcount != 1)
459 printf(" [%dq]", qdcount);
460 }
461 if (nscount)
462 printf(" [%dn]", nscount);
463 if (arcount)
464 printf(" [%dau]", arcount);
465
466 ns_qprint((const u_char *)(np + 1), (const u_char *)np);
467 }
468 printf(" (%d)", length);
469}