diff options
Diffstat (limited to 'contrib/tcpdump/print-domain.c')
-rw-r--r-- | contrib/tcpdump/print-domain.c | 407 |
1 files changed, 323 insertions, 84 deletions
diff --git a/contrib/tcpdump/print-domain.c b/contrib/tcpdump/print-domain.c index 50f2ce2..91fc3f6 100644 --- a/contrib/tcpdump/print-domain.c +++ b/contrib/tcpdump/print-domain.c @@ -23,7 +23,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.42 1999/11/21 09:36:50 fenner Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/print-domain.c,v 1.64 2001/01/02 23:24:51 guy Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -32,22 +32,8 @@ static const char rcsid[] = #include <sys/param.h> #include <sys/time.h> -#include <sys/socket.h> - -#if __STDC__ -struct mbuf; -struct rtentry; -#endif -#include <net/if.h> #include <netinet/in.h> -#include <net/ethernet.h> -#include <netinet/in_systm.h> -#include <netinet/ip.h> -#include <netinet/ip_var.h> -#include <netinet/udp.h> -#include <netinet/udp_var.h> -#include <netinet/tcp.h> #ifdef NOERROR #undef NOERROR /* Solaris sucks */ @@ -55,7 +41,7 @@ struct rtentry; #ifdef NOERROR #undef T_UNSPEC /* SINIX does too */ #endif -#include <arpa/nameser.h> +#include "nameser.h" #include <stdio.h> #include <string.h> @@ -126,7 +112,14 @@ struct rtentry; #define T_NAPTR 35 /* Naming Authority PoinTeR */ #endif #ifndef T_A6 -#define T_A6 38 /* IP6 address (ipngwg-dns-lookups) */ +#define T_A6 38 /* IP6 address */ +#endif +#ifndef T_DNAME +#define T_DNAME 39 /* non-terminal redirection */ +#endif + +#ifndef T_OPT +#define T_OPT 41 /* EDNS0 option (meta-RR) */ #endif #ifndef T_UNSPEC @@ -164,8 +157,22 @@ ns_nskip(register const u_char *cp, register const u_char *bp) if (((i = *cp++) & INDIR_MASK) == INDIR_MASK) return (cp + 1); + if (cp >= snapend) + return(NULL); while (i && cp < snapend) { - cp += i; + if ((i & INDIR_MASK) == EDNS0_MASK) { + int bitlen, bytelen; + + if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL) + return(NULL); /* unknown ELT */ + if ((bitlen = *cp++) == 0) + bitlen = 256; + bytelen = (bitlen + 7) / 8; + cp += bytelen; + } else + cp += i; + if (cp >= snapend) + return(NULL); i = *cp++; } return (cp); @@ -173,33 +180,135 @@ ns_nskip(register const u_char *cp, register const u_char *bp) /* print a <domain-name> */ static const u_char * -ns_nprint(register const u_char *cp, register const u_char *bp) +blabel_print(const u_char *cp) +{ + int bitlen, slen, b; + int truncated = 0; + const u_char *bitp, *lim; + char tc; + + if (cp >= snapend) + return(NULL); + if ((bitlen = *cp) == 0) + bitlen = 256; + slen = (bitlen + 3) / 4; + if ((lim = cp + 1 + slen) > snapend) { + truncated = 1; + lim = snapend; + } + + /* print the bit string as a hex string */ + printf("\\[x"); + for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) + printf("%02x", *bitp); + if (bitp == lim) + printf("..."); + else if (b > 4) { + tc = *bitp++; + printf("%02x", tc & (0xff << (8 - b))); + } else if (b > 0) { + tc = *bitp++; + printf("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b))); + } + printf("/%d]", bitlen); + + return(truncated ? NULL : lim); +} + +static int +labellen(const u_char *cp) { register u_int i; - register const u_char *rp; - register int compress; - i = *cp++; - rp = cp + i; - if ((i & INDIR_MASK) == INDIR_MASK) { - rp = cp + 1; - compress = 1; + if (cp >= snapend) + return(-1); + i = *cp; + if ((i & INDIR_MASK) == EDNS0_MASK) { + int bitlen, elt; + + if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) + return(-1); + if (cp + 1 >= snapend) + return(-1); + if ((bitlen = *(cp + 1)) == 0) + bitlen = 256; + return(((bitlen + 7) / 8) + 1); } else + return(i); +} + +static const u_char * +ns_nprint(register const u_char *cp, register const u_char *bp) +{ + register u_int i, l; + register const u_char *rp = NULL; + register int compress = 0; + int chars_processed; + int elt; + int data_size = snapend - bp; + + if ((l = labellen(cp)) < 0) + return(NULL); + if (cp >= snapend) + return(NULL); + chars_processed = 1; + if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) { compress = 0; + rp = cp + l; + } + if (i != 0) while (i && cp < snapend) { if ((i & INDIR_MASK) == INDIR_MASK) { + if (!compress) { + rp = cp + 1; + compress = 1; + } cp = bp + (((i << 8) | *cp) & 0x3fff); + if (cp >= snapend) + return(NULL); + if ((l = labellen(cp)) < 0) + return(NULL); i = *cp++; + chars_processed++; + + /* + * If we've looked at every character in + * the message, this pointer will make + * us look at some character again, + * which means we're looping. + */ + if (chars_processed >= data_size) { + printf("<LOOP>"); + return (NULL); + } continue; } - if (fn_printn(cp, i, snapend)) - break; - cp += i; + if ((i & INDIR_MASK) == EDNS0_MASK) { + elt = (i & ~INDIR_MASK); + switch(elt) { + case EDNS0_ELT_BITLABEL: + blabel_print(cp); + break; + default: + /* unknown ELT */ + printf("<ELT %d>", elt); + return(NULL); + } + } else { + if (fn_printn(cp, l, snapend)) + break; + } + + cp += l; + chars_processed += l; putchar('.'); + if (cp >= snapend || (l = labellen(cp)) < 0) + return(NULL); i = *cp++; + chars_processed++; if (!compress) - rp += i + 1; + rp += l + 1; } else putchar('.'); @@ -212,6 +321,8 @@ ns_cprint(register const u_char *cp, register const u_char *bp) { register u_int i; + if (cp >= snapend) + return NULL; i = *cp++; (void)fn_printn(cp, i, snapend); return (cp + i); @@ -254,17 +365,10 @@ static struct tok type2str[] = { { T_ATMA, "ATMA " }, { T_NAPTR, "NAPTR " }, { T_A6, "A6 " }, -#ifndef T_UINFO -#define T_UINFO 100 -#endif + { T_DNAME, "DNAME " }, + { T_OPT, "OPT " }, { T_UINFO, "UINFO" }, -#ifndef T_UID -#define T_UID 101 -#endif { T_UID, "UID" }, -#ifndef T_GID -#define T_GID 102 -#endif { T_GID, "GID" }, { T_UNSPEC, "UNSPEC" }, { T_UNSPECA, "UNSPECA" }, @@ -284,7 +388,7 @@ static struct tok class2str[] = { }; /* print a query */ -static void +static const u_char * ns_qprint(register const u_char *cp, register const u_char *bp) { register const u_char *np = cp; @@ -292,8 +396,8 @@ ns_qprint(register const u_char *cp, register const u_char *bp) cp = ns_nskip(cp, bp); - if (cp + 4 > snapend) - return; + if (cp + 4 > snapend || cp == NULL) + return(NULL); /* print the qtype and qclass (if it's not IN) */ i = *cp++ << 8; @@ -305,33 +409,35 @@ ns_qprint(register const u_char *cp, register const u_char *bp) printf(" %s", tok2str(class2str, "(Class %d)", i)); fputs("? ", stdout); - ns_nprint(np, bp); + cp = ns_nprint(np, bp); + return(cp ? cp + 4 : NULL); } /* print a reply */ static const u_char * ns_rprint(register const u_char *cp, register const u_char *bp) { - register u_int i; + register u_int class; register u_short typ, len; register const u_char *rp; if (vflag) { putchar(' '); - cp = ns_nprint(cp, bp); + if ((cp = ns_nprint(cp, bp)) == NULL) + return NULL; } else cp = ns_nskip(cp, bp); - if (cp + 10 > snapend) + if (cp + 10 > snapend || cp == NULL) return (snapend); /* print the type/qtype and class (if it's not IN) */ typ = *cp++ << 8; typ |= *cp++; - i = *cp++ << 8; - i |= *cp++; - if (i != C_IN) - printf(" %s", tok2str(class2str, "(Class %d)", i)); + class = *cp++ << 8; + class |= *cp++; + if (class != C_IN && typ != T_OPT) + printf(" %s", tok2str(class2str, "(Class %d)", class)); /* ignore ttl */ cp += 4; @@ -342,9 +448,13 @@ ns_rprint(register const u_char *cp, register const u_char *bp) rp = cp + len; printf(" %s", tok2str(type2str, "Type%d", typ)); - switch (typ) { + if (rp > snapend) + return(NULL); + switch (typ) { case T_A: + if (cp + sizeof(struct in_addr) > snapend) + return(NULL); printf(" %s", ipaddr_string(cp)); break; @@ -352,15 +462,41 @@ ns_rprint(register const u_char *cp, register const u_char *bp) case T_CNAME: case T_PTR: #ifdef T_DNAME - case T_DNAME: /*XXX not checked as there's no server support yet*/ + case T_DNAME: #endif putchar(' '); - (void)ns_nprint(cp, bp); + if (ns_nprint(cp, bp) == NULL) + return(NULL); break; + case T_SOA: + if (!vflag) + break; + putchar(' '); + if ((cp = ns_nprint(cp, bp)) == NULL) + return(NULL); + putchar(' '); + if ((cp = ns_nprint(cp, bp)) == NULL) + return(NULL); + if (cp + 5 * 4 > snapend) + return(NULL); + printf(" %u", EXTRACT_32BITS(cp)); + cp += 4; + printf(" %u", EXTRACT_32BITS(cp)); + cp += 4; + printf(" %u", EXTRACT_32BITS(cp)); + cp += 4; + printf(" %u", EXTRACT_32BITS(cp)); + cp += 4; + printf(" %u", EXTRACT_32BITS(cp)); + cp += 4; + break; case T_MX: putchar(' '); - (void)ns_nprint(cp + 2, bp); + if (cp + 2 > snapend) + return(NULL); + if (ns_nprint(cp + 2, bp) == NULL) + return(NULL); printf(" %d", EXTRACT_16BITS(cp)); break; @@ -371,25 +507,43 @@ ns_rprint(register const u_char *cp, register const u_char *bp) #ifdef INET6 case T_AAAA: + if (cp + sizeof(struct in6_addr) > snapend) + return(NULL); printf(" %s", ip6addr_string(cp)); break; - case T_A6: /*XXX not checked as there's no server support yet*/ + case T_A6: { struct in6_addr a; - int pbyte; - - pbyte = (*cp + 7) / 8; - memset(&a, 0, sizeof(a)); - memcpy(&a, cp + 1, pbyte); - printf(" %u %s ", *cp, ip6addr_string(&a)); - (void)ns_nprint(cp + 1 + pbyte, bp); + int pbit, pbyte; + + pbit = *cp; + pbyte = (pbit & ~7) / 8; + if (pbit > 128) { + printf(" %u(bad plen)", pbit); + break; + } else if (pbit < 128) { + memset(&a, 0, sizeof(a)); + memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte); + printf(" %u %s", pbit, ip6addr_string(&a)); + } + if (pbit > 0) { + putchar(' '); + if (ns_nprint(cp + 1 + sizeof(a) - pbyte, bp) == NULL) + return(NULL); + } break; } #endif /*INET6*/ + case T_OPT: + printf(" UDPsize=%u", class); + break; + case T_UNSPECA: /* One long string */ - printf(" %.*s", len, cp); + if (cp + len > snapend) + return(NULL); + fn_printn(cp, len, snapend); break; } return (rp); /* XXX This isn't always right */ @@ -400,7 +554,7 @@ ns_print(register const u_char *bp, u_int length) { register const HEADER *np; register int qdcount, ancount, nscount, arcount; - register const u_char *cp; + register const u_char *cp = NULL; np = (const HEADER *)bp; /* get the byte-order right */ @@ -409,44 +563,76 @@ ns_print(register const u_char *bp, u_int length) nscount = ntohs(np->nscount); arcount = ntohs(np->arcount); - if (np->qr) { + if (DNS_QR(np)) { /* this is a response */ - printf(" %d%s%s%s%s%s", + printf(" %d%s%s%s%s%s%s", ntohs(np->id), - ns_ops[np->opcode], - ns_resp[np->rcode], - np->aa? "*" : "", - np->ra? "" : "-", - np->tc? "|" : ""); + ns_ops[DNS_OPCODE(np)], + ns_resp[DNS_RCODE(np)], + DNS_AA(np)? "*" : "", + DNS_RA(np)? "" : "-", + DNS_TC(np)? "|" : "", + DNS_CD(np)? "%" : ""); + if (qdcount != 1) printf(" [%dq]", qdcount); /* Print QUESTION section on -vv */ if (vflag > 1) { - fputs(" q: ", stdout); - cp = ns_nprint((const u_char *)(np + 1), bp); - } else - cp = ns_nskip((const u_char *)(np + 1), bp); + fputs(" q:", stdout); + if ((cp = ns_qprint((const u_char *)(np + 1), bp)) + == NULL) + goto trunc; + } else { + if ((cp = ns_nskip((const u_char *)(np + 1), bp)) + == NULL) + goto trunc; + cp += 4; + } printf(" %d/%d/%d", ancount, nscount, arcount); if (ancount--) { - cp = ns_rprint(cp + 4, bp); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; while (ancount-- && cp < snapend) { putchar(','); - cp = ns_rprint(cp, bp); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + /* Print NS and AR sections on -vv */ + if (vflag > 1) { + if (nscount-- && cp < snapend) { + fputs(" ns:", stdout); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (nscount-- && cp < snapend) { + putchar(','); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + if (arcount-- && cp < snapend) { + fputs(" ar:", stdout); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (arcount-- && cp < snapend) { + putchar(','); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } } } } else { /* this is a request */ - printf(" %d%s%s", - ntohs(np->id), - ns_ops[np->opcode], - np->rd? "+" : ""); + printf(" %d%s%s%s", ntohs(np->id), ns_ops[DNS_OPCODE(np)], + DNS_RD(np) ? "+" : "", + DNS_AD(np) ? "$" : ""); /* any weirdness? */ - if (*(((u_short *)np)+1) & htons(0x6ff)) + if (*(((u_short *)np)+1) & htons(0x6cf)) printf(" [b2&3=0x%x]", ntohs(*(((u_short *)np)+1))); - if (np->opcode == IQUERY) { + if (DNS_OPCODE(np) == IQUERY) { if (qdcount) printf(" [%dq]", qdcount); if (ancount != 1) @@ -463,7 +649,60 @@ ns_print(register const u_char *bp, u_int length) if (arcount) printf(" [%dau]", arcount); - ns_qprint((const u_char *)(np + 1), (const u_char *)np); + if (qdcount--) { + cp = ns_qprint((const u_char *)(np + 1), + (const u_char *)np); + if (!cp) + goto trunc; + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (qdcount-- && cp < snapend) { + cp = ns_qprint((const u_char *)cp, + (const u_char *)np); + if (!cp) + goto trunc; + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + + /* Print remaining sections on -vv */ + if (vflag > 1) { + if (ancount--) { + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (ancount-- && cp < snapend) { + putchar(','); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + if (nscount-- && cp < snapend) { + fputs(" ns:", stdout); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (nscount-- && cp < snapend) { + putchar(','); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + if (arcount-- && cp < snapend) { + fputs(" ar:", stdout); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + while (arcount-- && cp < snapend) { + putchar(','); + if ((cp = ns_rprint(cp, bp)) == NULL) + goto trunc; + } + } + } } printf(" (%d)", length); + return; + + trunc: + printf("[|domain]"); + return; } |