diff options
Diffstat (limited to 'contrib/tcpdump/addrtoname.c')
-rw-r--r-- | contrib/tcpdump/addrtoname.c | 142 |
1 files changed, 108 insertions, 34 deletions
diff --git a/contrib/tcpdump/addrtoname.c b/contrib/tcpdump/addrtoname.c index 4067f96..4ed24fc 100644 --- a/contrib/tcpdump/addrtoname.c +++ b/contrib/tcpdump/addrtoname.c @@ -20,10 +20,16 @@ * * Internet, ethernet, port, and protocol string to address * and address to string conversion routines + * + * $FreeBSD$ */ #ifndef lint static const char rcsid[] = - "@(#) $Header: addrtoname.c,v 1.61 97/06/15 13:20:18 leres Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.64 1999/11/21 09:36:44 fenner Exp $ (LBL)"; +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" #endif #include <sys/types.h> @@ -39,6 +45,10 @@ struct rtentry; #include <netinet/in.h> #include <net/ethernet.h> +#ifdef INET6 +#include <netinet6/ip6.h> +#endif + #include <arpa/inet.h> #include <ctype.h> @@ -85,6 +95,16 @@ struct hnamemem eprototable[HASHNAMESIZE]; struct hnamemem dnaddrtable[HASHNAMESIZE]; struct hnamemem llcsaptable[HASHNAMESIZE]; +#ifdef INET6 +struct h6namemem { + struct in6_addr addr; + char *name; + struct h6namemem *nxt; +}; + +struct h6namemem h6nametable[HASHNAMESIZE]; +#endif /* INET6 */ + struct enamemem { u_short e_addr0; u_short e_addr1; @@ -171,39 +191,7 @@ getname(const u_char *ap) #ifndef LBL_ALIGN addr = *(const u_int32_t *)ap; #else - /* - * Extract 32 bits in network order, dealing with alignment. - */ - switch ((long)ap & 3) { - - case 0: - addr = *(u_int32_t *)ap; - break; - - case 2: -#ifdef WORDS_BIGENDIAN - addr = ((u_int32_t)*(u_short *)ap << 16) | - (u_int32_t)*(u_short *)(ap + 2); -#else - addr = ((u_int32_t)*(u_short *)(ap + 2) << 16) | - (u_int32_t)*(u_short *)ap; -#endif - break; - - default: -#ifdef WORDS_BIGENDIAN - addr = ((u_int32_t)ap[0] << 24) | - ((u_int32_t)ap[1] << 16) | - ((u_int32_t)ap[2] << 8) | - (u_int32_t)ap[3]; -#else - addr = ((u_int32_t)ap[3] << 24) | - ((u_int32_t)ap[2] << 16) | - ((u_int32_t)ap[1] << 8) | - (u_int32_t)ap[0]; -#endif - break; - } + memcpy(&addr, ap, sizeof(addr)); #endif p = &hnametable[addr & (HASHNAMESIZE-1)]; for (; p->nxt; p = p->nxt) { @@ -249,6 +237,71 @@ getname(const u_char *ap) return (p->name); } +#ifdef INET6 +/* + * Return a name for the IP6 address pointed to by ap. This address + * is assumed to be in network byte order. + */ +char * +getname6(const u_char *ap) +{ + register struct hostent *hp; + struct in6_addr addr; + static struct h6namemem *p; /* static for longjmp() */ + register char *cp; + char ntop_buf[INET6_ADDRSTRLEN]; + + memcpy(&addr, ap, sizeof(addr)); + p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)]; + for (; p->nxt; p = p->nxt) { + if (memcmp(&p->addr, &addr, sizeof(addr)) == 0) + return (p->name); + } + p->addr = addr; + p->nxt = newh6namemem(); + + /* + * Only print names when: + * (1) -n was not given. + * (2) Address is foreign and -f was given. (If -f was not + * give, f_netmask and f_local are 0 and the test + * evaluates to true) + * (3) -a was given or the host portion is not all ones + * nor all zeros (i.e. not a network or broadcast address) + */ + if (!nflag +#if 0 + && + (addr & f_netmask) == f_localnet && + (aflag || + !((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff)) +#endif + ) { + if (!setjmp(getname_env)) { + (void)setsignal(SIGALRM, nohostname); + (void)alarm(20); + hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6); + (void)alarm(0); + if (hp) { + char *dotp; + + p->name = savestr(hp->h_name); + if (Nflag) { + /* Remove domain qualifications */ + dotp = strchr(p->name, '.'); + if (dotp) + *dotp = '\0'; + } + return (p->name); + } + } + } + cp = (char *)inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf)); + p->name = savestr(cp); + return (p->name); +} +#endif /* INET6 */ + static char hex[] = "0123456789abcdef"; @@ -765,3 +818,24 @@ newhnamemem(void) p = ptr++; return (p); } + +#ifdef INET6 +/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */ +struct h6namemem * +newh6namemem(void) +{ + register struct h6namemem *p; + static struct h6namemem *ptr = NULL; + static u_int num = 0; + + if (num <= 0) { + num = 64; + ptr = (struct h6namemem *)calloc(num, sizeof (*ptr)); + if (ptr == NULL) + error("newh6namemem: calloc"); + } + --num; + p = ptr++; + return (p); +} +#endif /* INET6 */ |