1997-10-03 23:54:07 +04:00
|
|
|
/* $NetBSD: addrtoname.c,v 1.6 1997/10/03 19:54:13 christos Exp $ */
|
1995-03-06 22:09:42 +03:00
|
|
|
|
1993-11-15 00:19:50 +03:00
|
|
|
/*
|
1997-10-03 23:54:07 +04:00
|
|
|
* Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
|
1995-03-06 22:09:42 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-11-15 00:19:50 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that: (1) source code distributions
|
|
|
|
* retain the above copyright notice and this paragraph in its entirety, (2)
|
|
|
|
* distributions including binary code include the above copyright notice and
|
|
|
|
* this paragraph in its entirety in the documentation or other materials
|
|
|
|
* provided with the distribution, and (3) all advertising materials mentioning
|
|
|
|
* features or use of this software display the following acknowledgement:
|
|
|
|
* ``This product includes software developed by the University of California,
|
|
|
|
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
|
|
|
|
* the University nor the names of its contributors may be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* Internet, ethernet, port, and protocol string to address
|
|
|
|
* and address to string conversion routines
|
|
|
|
*/
|
1997-10-03 23:54:07 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-11-15 00:19:50 +03:00
|
|
|
#ifndef lint
|
1997-10-03 23:54:07 +04:00
|
|
|
#if 0
|
|
|
|
static const char rcsid[] =
|
|
|
|
"@(#) Header: addrtoname.c,v 1.61 97/06/15 13:20:18 leres Exp (LBL)";
|
|
|
|
#else
|
|
|
|
__RCSID("$NetBSD: addrtoname.c,v 1.6 1997/10/03 19:54:13 christos Exp $");
|
|
|
|
#endif
|
1993-11-15 00:19:50 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
1995-03-06 22:09:42 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
#if __STDC__
|
|
|
|
struct mbuf;
|
|
|
|
struct rtentry;
|
|
|
|
#endif
|
1993-11-15 00:19:50 +03:00
|
|
|
#include <net/if.h>
|
1995-03-06 22:09:42 +03:00
|
|
|
|
1993-11-15 00:19:50 +03:00
|
|
|
#include <netinet/in.h>
|
1997-03-15 21:37:27 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#else
|
1993-11-15 00:19:50 +03:00
|
|
|
#include <netinet/if_ether.h>
|
1997-03-15 21:37:27 +03:00
|
|
|
#endif
|
1995-03-06 22:09:42 +03:00
|
|
|
|
1993-11-15 00:19:50 +03:00
|
|
|
#include <arpa/inet.h>
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <pcap.h>
|
|
|
|
#include <pcap-namedb.h>
|
1997-10-03 23:54:07 +04:00
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MEMORY_H
|
|
|
|
#include <memory.h>
|
|
|
|
#endif
|
1993-11-15 00:19:50 +03:00
|
|
|
#include <signal.h>
|
1995-03-06 22:09:42 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
#include "interface.h"
|
|
|
|
#include "addrtoname.h"
|
1995-03-06 22:09:42 +03:00
|
|
|
#include "llc.h"
|
1997-10-03 23:54:07 +04:00
|
|
|
#include "savestr.h"
|
|
|
|
#include "setsignal.h"
|
1995-03-06 22:09:42 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
/* Forwards */
|
|
|
|
static RETSIGTYPE nohostname(int);
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hash tables for whatever-to-name translations
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define HASHNAMESIZE 4096
|
|
|
|
|
|
|
|
struct hnamemem {
|
1997-10-03 23:54:07 +04:00
|
|
|
u_int32_t addr;
|
1993-11-15 00:19:50 +03:00
|
|
|
char *name;
|
|
|
|
struct hnamemem *nxt;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hnamemem hnametable[HASHNAMESIZE];
|
|
|
|
struct hnamemem tporttable[HASHNAMESIZE];
|
|
|
|
struct hnamemem uporttable[HASHNAMESIZE];
|
|
|
|
struct hnamemem eprototable[HASHNAMESIZE];
|
1995-03-06 22:09:42 +03:00
|
|
|
struct hnamemem dnaddrtable[HASHNAMESIZE];
|
|
|
|
struct hnamemem llcsaptable[HASHNAMESIZE];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
struct enamemem {
|
|
|
|
u_short e_addr0;
|
|
|
|
u_short e_addr1;
|
|
|
|
u_short e_addr2;
|
|
|
|
char *e_name;
|
1995-03-06 22:09:42 +03:00
|
|
|
u_char *e_nsap; /* used only for nsaptable[] */
|
1997-03-15 21:37:27 +03:00
|
|
|
#define e_bs e_nsap /* for bytestringtable */
|
1993-11-15 00:19:50 +03:00
|
|
|
struct enamemem *e_nxt;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct enamemem enametable[HASHNAMESIZE];
|
1995-03-06 22:09:42 +03:00
|
|
|
struct enamemem nsaptable[HASHNAMESIZE];
|
1997-03-15 21:37:27 +03:00
|
|
|
struct enamemem bytestringtable[HASHNAMESIZE];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
struct protoidmem {
|
1997-10-03 23:54:07 +04:00
|
|
|
u_int32_t p_oui;
|
1995-03-06 22:09:42 +03:00
|
|
|
u_short p_proto;
|
|
|
|
char *p_name;
|
|
|
|
struct protoidmem *p_nxt;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct protoidmem protoidtable[HASHNAMESIZE];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A faster replacement for inet_ntoa().
|
|
|
|
*/
|
|
|
|
char *
|
1997-10-03 23:54:07 +04:00
|
|
|
intoa(u_int32_t addr)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register char *cp;
|
|
|
|
register u_int byte;
|
|
|
|
register int n;
|
|
|
|
static char buf[sizeof(".xxx.xxx.xxx.xxx")];
|
|
|
|
|
|
|
|
NTOHL(addr);
|
|
|
|
cp = &buf[sizeof buf];
|
|
|
|
*--cp = '\0';
|
|
|
|
|
|
|
|
n = 4;
|
|
|
|
do {
|
|
|
|
byte = addr & 0xff;
|
|
|
|
*--cp = byte % 10 + '0';
|
|
|
|
byte /= 10;
|
|
|
|
if (byte > 0) {
|
|
|
|
*--cp = byte % 10 + '0';
|
|
|
|
byte /= 10;
|
|
|
|
if (byte > 0)
|
|
|
|
*--cp = byte + '0';
|
|
|
|
}
|
|
|
|
*--cp = '.';
|
|
|
|
addr >>= 8;
|
|
|
|
} while (--n > 0);
|
|
|
|
|
|
|
|
return cp + 1;
|
|
|
|
}
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
static u_int32_t f_netmask;
|
|
|
|
static u_int32_t f_localnet;
|
|
|
|
static u_int32_t netmask;
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "getname" is written in this atrocious way to make sure we don't
|
|
|
|
* wait forever while trying to get hostnames from yp.
|
|
|
|
*/
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
jmp_buf getname_env;
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
static RETSIGTYPE
|
1995-03-06 22:09:42 +03:00
|
|
|
nohostname(int signo)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
longjmp(getname_env, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a name for the IP address pointed to by ap. This address
|
|
|
|
* is assumed to be in network byte order.
|
|
|
|
*/
|
|
|
|
char *
|
1995-03-06 22:09:42 +03:00
|
|
|
getname(const u_char *ap)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register struct hostent *hp;
|
1997-10-03 23:54:07 +04:00
|
|
|
u_int32_t addr;
|
1995-03-06 22:09:42 +03:00
|
|
|
static struct hnamemem *p; /* static for longjmp() */
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
#ifndef LBL_ALIGN
|
|
|
|
addr = *(const u_int32_t *)ap;
|
1993-11-15 00:19:50 +03:00
|
|
|
#else
|
|
|
|
/*
|
1997-10-03 23:54:07 +04:00
|
|
|
* Extract 32 bits in network order, dealing with alignment.
|
1993-11-15 00:19:50 +03:00
|
|
|
*/
|
1995-04-24 17:27:39 +04:00
|
|
|
switch ((long)ap & 3) {
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
case 0:
|
1997-10-03 23:54:07 +04:00
|
|
|
addr = *(u_int32_t *)ap;
|
1993-11-15 00:19:50 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
1997-10-03 23:54:07 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
addr = ((u_int32_t)*(u_short *)ap << 16) |
|
|
|
|
(u_int32_t)*(u_short *)(ap + 2);
|
1993-11-15 00:19:50 +03:00
|
|
|
#else
|
1997-10-03 23:54:07 +04:00
|
|
|
addr = ((u_int32_t)*(u_short *)(ap + 2) << 16) |
|
|
|
|
(u_int32_t)*(u_short *)ap;
|
1993-11-15 00:19:50 +03:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1997-10-03 23:54:07 +04:00
|
|
|
#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];
|
1993-11-15 00:19:50 +03:00
|
|
|
#else
|
1997-10-03 23:54:07 +04:00
|
|
|
addr = ((u_int32_t)ap[3] << 24) |
|
|
|
|
((u_int32_t)ap[2] << 16) |
|
|
|
|
((u_int32_t)ap[1] << 8) |
|
|
|
|
(u_int32_t)ap[0];
|
1993-11-15 00:19:50 +03:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
1995-03-06 22:09:42 +03:00
|
|
|
p = &hnametable[addr & (HASHNAMESIZE-1)];
|
1993-11-15 00:19:50 +03:00
|
|
|
for (; p->nxt; p = p->nxt) {
|
|
|
|
if (p->addr == addr)
|
|
|
|
return (p->name);
|
|
|
|
}
|
|
|
|
p->addr = addr;
|
1997-10-03 23:54:07 +04:00
|
|
|
p->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only print names when:
|
1995-03-06 22:09:42 +03:00
|
|
|
* (1) -n was not given.
|
1997-10-03 23:54:07 +04:00
|
|
|
* (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)
|
1993-11-15 00:19:50 +03:00
|
|
|
*/
|
1997-10-03 23:54:07 +04:00
|
|
|
if (!nflag &&
|
|
|
|
(addr & f_netmask) == f_localnet &&
|
|
|
|
(aflag ||
|
|
|
|
!((addr & ~netmask) == 0 || (addr | netmask) == 0xffffffff))) {
|
1993-11-15 00:19:50 +03:00
|
|
|
if (!setjmp(getname_env)) {
|
1997-10-03 23:54:07 +04:00
|
|
|
(void)setsignal(SIGALRM, nohostname);
|
1993-11-15 00:19:50 +03:00
|
|
|
(void)alarm(20);
|
|
|
|
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
|
|
|
|
(void)alarm(0);
|
|
|
|
if (hp) {
|
1995-03-06 22:09:42 +03:00
|
|
|
char *dotp;
|
|
|
|
|
|
|
|
p->name = savestr(hp->h_name);
|
1993-11-15 00:19:50 +03:00
|
|
|
if (Nflag) {
|
|
|
|
/* Remove domain qualifications */
|
1995-03-06 22:09:42 +03:00
|
|
|
dotp = strchr(p->name, '.');
|
1993-11-15 00:19:50 +03:00
|
|
|
if (dotp)
|
1997-10-03 23:54:07 +04:00
|
|
|
*dotp = '\0';
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
return (p->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-10-03 23:54:07 +04:00
|
|
|
p->name = savestr(intoa(addr));
|
1993-11-15 00:19:50 +03:00
|
|
|
return (p->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char hex[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
/* Find the hash node that corresponds the ether address 'ep' */
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
static inline struct enamemem *
|
1995-03-06 22:09:42 +03:00
|
|
|
lookup_emem(const u_char *ep)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register u_int i, j, k;
|
|
|
|
struct enamemem *tp;
|
|
|
|
|
|
|
|
k = (ep[0] << 8) | ep[1];
|
|
|
|
j = (ep[2] << 8) | ep[3];
|
|
|
|
i = (ep[4] << 8) | ep[5];
|
|
|
|
|
|
|
|
tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
|
|
|
|
while (tp->e_nxt)
|
|
|
|
if (tp->e_addr0 == i &&
|
|
|
|
tp->e_addr1 == j &&
|
|
|
|
tp->e_addr2 == k)
|
|
|
|
return tp;
|
|
|
|
else
|
|
|
|
tp = tp->e_nxt;
|
|
|
|
tp->e_addr0 = i;
|
|
|
|
tp->e_addr1 = j;
|
|
|
|
tp->e_addr2 = k;
|
|
|
|
tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
|
1997-10-03 23:54:07 +04:00
|
|
|
if (tp->e_nxt == NULL)
|
|
|
|
error("lookup_emem: calloc");
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
1997-03-15 21:37:27 +03:00
|
|
|
/*
|
|
|
|
* Find the hash node that corresponds to the bytestring 'bs'
|
|
|
|
* with length 'nlen'
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline struct enamemem *
|
|
|
|
lookup_bytestring(register const u_char *bs, const int nlen)
|
|
|
|
{
|
|
|
|
struct enamemem *tp;
|
|
|
|
register u_int i, j, k;
|
|
|
|
|
|
|
|
if (nlen >= 6) {
|
|
|
|
k = (bs[0] << 8) | bs[1];
|
|
|
|
j = (bs[2] << 8) | bs[3];
|
|
|
|
i = (bs[4] << 8) | bs[5];
|
|
|
|
} else if (nlen >= 4) {
|
|
|
|
k = (bs[0] << 8) | bs[1];
|
|
|
|
j = (bs[2] << 8) | bs[3];
|
|
|
|
i = 0;
|
|
|
|
} else
|
|
|
|
i = j = k = 0;
|
|
|
|
|
|
|
|
tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
|
|
|
|
while (tp->e_nxt)
|
|
|
|
if (tp->e_addr0 == i &&
|
|
|
|
tp->e_addr1 == j &&
|
|
|
|
tp->e_addr2 == k &&
|
|
|
|
bcmp((char *)bs, (char *)(tp->e_bs), nlen) == 0)
|
|
|
|
return tp;
|
|
|
|
else
|
|
|
|
tp = tp->e_nxt;
|
|
|
|
|
|
|
|
tp->e_addr0 = i;
|
|
|
|
tp->e_addr1 = j;
|
|
|
|
tp->e_addr2 = k;
|
|
|
|
|
|
|
|
tp->e_bs = (u_char *) calloc(1, nlen + 1);
|
|
|
|
bcopy(bs, tp->e_bs, nlen);
|
|
|
|
tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
|
1997-10-03 23:54:07 +04:00
|
|
|
if (tp->e_nxt == NULL)
|
|
|
|
error("lookup_bytestring: calloc");
|
1997-03-15 21:37:27 +03:00
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
/* Find the hash node that corresponds the NSAP 'nsap' */
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
static inline struct enamemem *
|
|
|
|
lookup_nsap(register const u_char *nsap)
|
|
|
|
{
|
|
|
|
register u_int i, j, k;
|
|
|
|
int nlen = *nsap;
|
|
|
|
struct enamemem *tp;
|
|
|
|
const u_char *ensap = nsap + nlen - 6;
|
|
|
|
|
|
|
|
if (nlen > 6) {
|
|
|
|
k = (ensap[0] << 8) | ensap[1];
|
|
|
|
j = (ensap[2] << 8) | ensap[3];
|
|
|
|
i = (ensap[4] << 8) | ensap[5];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i = j = k = 0;
|
|
|
|
|
|
|
|
tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
|
|
|
|
while (tp->e_nxt)
|
|
|
|
if (tp->e_addr0 == i &&
|
|
|
|
tp->e_addr1 == j &&
|
|
|
|
tp->e_addr2 == k &&
|
|
|
|
tp->e_nsap[0] == nlen &&
|
1997-10-03 23:54:07 +04:00
|
|
|
memcmp((char *)&(nsap[1]),
|
1995-03-06 22:09:42 +03:00
|
|
|
(char *)&(tp->e_nsap[1]), nlen) == 0)
|
|
|
|
return tp;
|
|
|
|
else
|
|
|
|
tp = tp->e_nxt;
|
|
|
|
tp->e_addr0 = i;
|
|
|
|
tp->e_addr1 = j;
|
|
|
|
tp->e_addr2 = k;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->e_nsap = (u_char *)malloc(nlen + 1);
|
|
|
|
if (tp->e_nsap == NULL)
|
|
|
|
error("lookup_nsap: malloc");
|
|
|
|
memcpy((char *)tp->e_nsap, (char *)nsap, nlen + 1);
|
1995-03-06 22:09:42 +03:00
|
|
|
tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
|
1997-10-03 23:54:07 +04:00
|
|
|
if (tp->e_nxt == NULL)
|
|
|
|
error("lookup_nsap: calloc");
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the hash node that corresponds the protoid 'pi'. */
|
|
|
|
|
|
|
|
static inline struct protoidmem *
|
|
|
|
lookup_protoid(const u_char *pi)
|
|
|
|
{
|
|
|
|
register u_int i, j;
|
|
|
|
struct protoidmem *tp;
|
|
|
|
|
|
|
|
/* 5 octets won't be aligned */
|
|
|
|
i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
|
|
|
|
j = (pi[3] << 8) + pi[4];
|
|
|
|
/* XXX should be endian-insensitive, but do big-endian testing XXX */
|
|
|
|
|
|
|
|
tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
|
|
|
|
while (tp->p_nxt)
|
|
|
|
if (tp->p_oui == i && tp->p_proto == j)
|
|
|
|
return tp;
|
|
|
|
else
|
|
|
|
tp = tp->p_nxt;
|
|
|
|
tp->p_oui = i;
|
|
|
|
tp->p_proto = j;
|
|
|
|
tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
|
1997-10-03 23:54:07 +04:00
|
|
|
if (tp->p_nxt == NULL)
|
|
|
|
error("lookup_protoid: calloc");
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
1993-11-15 00:19:50 +03:00
|
|
|
char *
|
1995-03-06 22:09:42 +03:00
|
|
|
etheraddr_string(register const u_char *ep)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register u_int i, j;
|
|
|
|
register char *cp;
|
|
|
|
register struct enamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
char buf[sizeof("00:00:00:00:00:00")];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
tp = lookup_emem(ep);
|
|
|
|
if (tp->e_name)
|
1995-03-06 22:09:42 +03:00
|
|
|
return (tp->e_name);
|
1997-10-03 23:54:07 +04:00
|
|
|
#ifdef HAVE_ETHER_NTOHOST
|
1993-11-15 00:19:50 +03:00
|
|
|
if (!nflag) {
|
1995-03-06 22:09:42 +03:00
|
|
|
char buf[128];
|
|
|
|
if (ether_ntohost(buf, (struct ether_addr *)ep) == 0) {
|
|
|
|
tp->e_name = savestr(buf);
|
1995-04-24 17:27:39 +04:00
|
|
|
return (tp->e_name);
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
1995-03-06 22:09:42 +03:00
|
|
|
#endif
|
1997-10-03 23:54:07 +04:00
|
|
|
cp = buf;
|
1995-03-06 22:09:42 +03:00
|
|
|
if ((j = *ep >> 4) != 0)
|
1993-11-15 00:19:50 +03:00
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*ep++ & 0xf];
|
|
|
|
for (i = 5; (int)--i >= 0;) {
|
|
|
|
*cp++ = ':';
|
1995-03-06 22:09:42 +03:00
|
|
|
if ((j = *ep >> 4) != 0)
|
1993-11-15 00:19:50 +03:00
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*ep++ & 0xf];
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->e_name = savestr(buf);
|
1993-11-15 00:19:50 +03:00
|
|
|
return (tp->e_name);
|
|
|
|
}
|
|
|
|
|
1997-03-15 21:37:27 +03:00
|
|
|
char *
|
|
|
|
linkaddr_string(const u_char *ep, const int len)
|
|
|
|
{
|
|
|
|
register u_int i, j;
|
|
|
|
register char *cp;
|
|
|
|
register struct enamemem *tp;
|
|
|
|
|
|
|
|
if (len == 6) /* XXX not totally correct... */
|
|
|
|
return etheraddr_string(ep);
|
|
|
|
|
|
|
|
tp = lookup_bytestring(ep, len);
|
|
|
|
if (tp->e_name)
|
|
|
|
return (tp->e_name);
|
|
|
|
|
|
|
|
tp->e_name = cp = (char *)malloc(len*3);
|
1997-10-03 23:54:07 +04:00
|
|
|
if (tp->e_name == NULL)
|
|
|
|
error("linkaddr_string: malloc");
|
1997-03-15 21:37:27 +03:00
|
|
|
if ((j = *ep >> 4) != 0)
|
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*ep++ & 0xf];
|
|
|
|
for (i = len-1; i > 0 ; --i) {
|
|
|
|
*cp++ = ':';
|
|
|
|
if ((j = *ep >> 4) != 0)
|
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*ep++ & 0xf];
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
return (tp->e_name);
|
|
|
|
}
|
|
|
|
|
1993-11-15 00:19:50 +03:00
|
|
|
char *
|
1995-03-06 22:09:42 +03:00
|
|
|
etherproto_string(u_short port)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register char *cp;
|
|
|
|
register struct hnamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
register u_int32_t i = port;
|
|
|
|
char buf[sizeof("0000")];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
|
|
|
|
if (tp->addr == i)
|
|
|
|
return (tp->name);
|
|
|
|
|
|
|
|
tp->addr = i;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
cp = buf;
|
1993-11-15 00:19:50 +03:00
|
|
|
NTOHS(port);
|
|
|
|
*cp++ = hex[port >> 12 & 0xf];
|
|
|
|
*cp++ = hex[port >> 8 & 0xf];
|
|
|
|
*cp++ = hex[port >> 4 & 0xf];
|
|
|
|
*cp++ = hex[port & 0xf];
|
|
|
|
*cp++ = '\0';
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->name = savestr(buf);
|
1993-11-15 00:19:50 +03:00
|
|
|
return (tp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1995-03-06 22:09:42 +03:00
|
|
|
protoid_string(register const u_char *pi)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
1995-03-06 22:09:42 +03:00
|
|
|
register u_int i, j;
|
|
|
|
register char *cp;
|
|
|
|
register struct protoidmem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
char buf[sizeof("00:00:00:00:00")];
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
tp = lookup_protoid(pi);
|
|
|
|
if (tp->p_name)
|
|
|
|
return tp->p_name;
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
cp = buf;
|
1995-03-06 22:09:42 +03:00
|
|
|
if ((j = *pi >> 4) != 0)
|
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*pi++ & 0xf];
|
|
|
|
for (i = 4; (int)--i >= 0;) {
|
|
|
|
*cp++ = ':';
|
|
|
|
if ((j = *pi >> 4) != 0)
|
|
|
|
*cp++ = hex[j];
|
|
|
|
*cp++ = hex[*pi++ & 0xf];
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->p_name = savestr(buf);
|
1995-03-06 22:09:42 +03:00
|
|
|
return (tp->p_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
llcsap_string(u_char sap)
|
|
|
|
{
|
|
|
|
register char *cp;
|
1993-11-15 00:19:50 +03:00
|
|
|
register struct hnamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
register u_int32_t i = sap;
|
|
|
|
char buf[sizeof("sap 00")];
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
for (tp = &llcsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
|
|
|
|
if (tp->addr == i)
|
|
|
|
return (tp->name);
|
|
|
|
|
|
|
|
tp->addr = i;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->nxt = newhnamemem();
|
1995-03-06 22:09:42 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
cp = buf;
|
1995-03-06 22:09:42 +03:00
|
|
|
(void)strcpy(cp, "sap ");
|
|
|
|
cp += strlen(cp);
|
|
|
|
*cp++ = hex[sap >> 4 & 0xf];
|
|
|
|
*cp++ = hex[sap & 0xf];
|
|
|
|
*cp++ = '\0';
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->name = savestr(buf);
|
1995-03-06 22:09:42 +03:00
|
|
|
return (tp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
isonsap_string(const u_char *nsap)
|
|
|
|
{
|
|
|
|
register u_int i, nlen = nsap[0];
|
|
|
|
register char *cp;
|
|
|
|
register struct enamemem *tp;
|
|
|
|
|
|
|
|
tp = lookup_nsap(nsap);
|
|
|
|
if (tp->e_name)
|
|
|
|
return tp->e_name;
|
|
|
|
|
|
|
|
tp->e_name = cp = (char *)malloc(nlen * 2 + 2);
|
1997-10-03 23:54:07 +04:00
|
|
|
if (cp == NULL)
|
|
|
|
error("isonsap_string: malloc");
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
nsap++;
|
|
|
|
*cp++ = '/';
|
|
|
|
for (i = nlen; (int)--i >= 0;) {
|
|
|
|
*cp++ = hex[*nsap >> 4];
|
|
|
|
*cp++ = hex[*nsap++ & 0xf];
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
return (tp->e_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
tcpport_string(u_short port)
|
|
|
|
{
|
|
|
|
register struct hnamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
register u_int32_t i = port;
|
|
|
|
char buf[sizeof("00000")];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
|
|
|
|
if (tp->addr == i)
|
|
|
|
return (tp->name);
|
|
|
|
|
|
|
|
tp->addr = i;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
(void)sprintf(buf, "%u", i);
|
|
|
|
tp->name = savestr(buf);
|
1993-11-15 00:19:50 +03:00
|
|
|
return (tp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
1995-03-06 22:09:42 +03:00
|
|
|
udpport_string(register u_short port)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register struct hnamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
register u_int32_t i = port;
|
|
|
|
char buf[sizeof("00000")];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
|
|
|
|
if (tp->addr == i)
|
|
|
|
return (tp->name);
|
|
|
|
|
|
|
|
tp->addr = i;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
(void)sprintf(buf, "%u", i);
|
|
|
|
tp->name = savestr(buf);
|
1993-11-15 00:19:50 +03:00
|
|
|
return (tp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1995-03-06 22:09:42 +03:00
|
|
|
init_servarray(void)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
struct servent *sv;
|
|
|
|
register struct hnamemem *table;
|
|
|
|
register int i;
|
1997-10-03 23:54:07 +04:00
|
|
|
char buf[sizeof("0000000000")];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
while ((sv = getservent()) != NULL) {
|
|
|
|
int port = ntohs(sv->s_port);
|
|
|
|
i = port & (HASHNAMESIZE-1);
|
1993-11-15 00:19:50 +03:00
|
|
|
if (strcmp(sv->s_proto, "tcp") == 0)
|
|
|
|
table = &tporttable[i];
|
|
|
|
else if (strcmp(sv->s_proto, "udp") == 0)
|
|
|
|
table = &uporttable[i];
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (table->name)
|
|
|
|
table = table->nxt;
|
|
|
|
if (nflag) {
|
1995-03-06 22:09:42 +03:00
|
|
|
(void)sprintf(buf, "%d", port);
|
|
|
|
table->name = savestr(buf);
|
|
|
|
} else
|
|
|
|
table->name = savestr(sv->s_name);
|
|
|
|
table->addr = port;
|
1997-10-03 23:54:07 +04:00
|
|
|
table->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
endservent();
|
|
|
|
}
|
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
/*XXX from libbpfc.a */
|
|
|
|
extern struct eproto {
|
|
|
|
char *s;
|
|
|
|
u_short p;
|
|
|
|
} eproto_db[];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
|
|
|
static void
|
1995-03-06 22:09:42 +03:00
|
|
|
init_eprotoarray(void)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register struct hnamemem *table;
|
|
|
|
|
|
|
|
for (i = 0; eproto_db[i].s; i++) {
|
|
|
|
int j = ntohs(eproto_db[i].p) & (HASHNAMESIZE-1);
|
|
|
|
table = &eprototable[j];
|
|
|
|
while (table->name)
|
|
|
|
table = table->nxt;
|
|
|
|
table->name = eproto_db[i].s;
|
|
|
|
table->addr = ntohs(eproto_db[i].p);
|
1997-10-03 23:54:07 +04:00
|
|
|
table->nxt = newhnamemem();
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
/*
|
|
|
|
* SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
|
|
|
|
* types.
|
|
|
|
*/
|
1993-11-15 00:19:50 +03:00
|
|
|
static void
|
1995-03-06 22:09:42 +03:00
|
|
|
init_protoidarray(void)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
1995-03-06 22:09:42 +03:00
|
|
|
register int i;
|
|
|
|
register struct protoidmem *tp;
|
|
|
|
u_char protoid[5];
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
protoid[0] = 0;
|
|
|
|
protoid[1] = 0;
|
|
|
|
protoid[2] = 0;
|
|
|
|
for (i = 0; eproto_db[i].s; i++) {
|
|
|
|
u_short etype = htons(eproto_db[i].p);
|
1997-10-03 23:54:07 +04:00
|
|
|
|
|
|
|
memcpy((char *)&protoid[3], (char *)&etype, 2);
|
1995-03-06 22:09:42 +03:00
|
|
|
tp = lookup_protoid(protoid);
|
|
|
|
tp->p_name = savestr(eproto_db[i].s);
|
|
|
|
}
|
|
|
|
}
|
1993-11-15 00:19:50 +03:00
|
|
|
|
1995-03-06 22:09:42 +03:00
|
|
|
static struct etherlist {
|
|
|
|
u_char addr[6];
|
|
|
|
char *name;
|
|
|
|
} etherlist[] = {
|
|
|
|
{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
|
|
|
|
{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the ethers hash table. We take two different approaches
|
|
|
|
* depending on whether or not the system provides the ethers name
|
|
|
|
* service. If it does, we just wire in a few names at startup,
|
|
|
|
* and etheraddr_string() fills in the table on demand. If it doesn't,
|
|
|
|
* then we suck in the entire /etc/ethers file at startup. The idea
|
|
|
|
* is that parsing the local file will be fast, but spinning through
|
|
|
|
* all the ethers entries via NIS & next_etherent might be very slow.
|
|
|
|
*
|
|
|
|
* XXX pcap_next_etherent doesn't belong in the pcap interface, but
|
|
|
|
* since the pcap module already does name-to-address translation,
|
|
|
|
* it's already does most of the work for the ethernet address-to-name
|
|
|
|
* translation, so we just pcap_next_etherent as a convenience.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_etherarray(void)
|
|
|
|
{
|
|
|
|
register struct etherlist *el;
|
|
|
|
register struct enamemem *tp;
|
1997-10-03 23:54:07 +04:00
|
|
|
#ifdef HAVE_ETHER_NTOHOST
|
|
|
|
char name[256];
|
|
|
|
#else
|
1995-03-06 22:09:42 +03:00
|
|
|
register struct pcap_etherent *ep;
|
|
|
|
register FILE *fp;
|
|
|
|
|
|
|
|
/* Suck in entire ethers file */
|
|
|
|
fp = fopen(PCAP_ETHERS_FILE, "r");
|
|
|
|
if (fp != NULL) {
|
|
|
|
while ((ep = pcap_next_etherent(fp)) != NULL) {
|
|
|
|
tp = lookup_emem(ep->addr);
|
|
|
|
tp->e_name = savestr(ep->name);
|
|
|
|
}
|
|
|
|
(void)fclose(fp);
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
#endif
|
1995-03-06 22:09:42 +03:00
|
|
|
|
|
|
|
/* Hardwire some ethernet names */
|
|
|
|
for (el = etherlist; el->name != NULL; ++el) {
|
1997-10-03 23:54:07 +04:00
|
|
|
tp = lookup_emem(el->addr);
|
|
|
|
/* Don't override existing name */
|
|
|
|
if (tp->e_name != NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef HAVE_ETHER_NTOHOST
|
1995-03-06 22:09:42 +03:00
|
|
|
/* Use yp/nis version of name if available */
|
1997-10-03 23:54:07 +04:00
|
|
|
if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
|
|
|
|
tp->e_name = savestr(name);
|
|
|
|
continue;
|
1995-03-06 22:09:42 +03:00
|
|
|
}
|
|
|
|
#endif
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->e_name = el->name;
|
1995-03-06 22:09:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-03 23:54:07 +04:00
|
|
|
static struct tok llcsap_db[] = {
|
1995-03-06 22:09:42 +03:00
|
|
|
{ LLCSAP_NULL, "null" },
|
|
|
|
{ LLCSAP_8021B_I, "802.1b-gsap" },
|
|
|
|
{ LLCSAP_8021B_G, "802.1b-isap" },
|
|
|
|
{ LLCSAP_IP, "ip-sap" },
|
|
|
|
{ LLCSAP_PROWAYNM, "proway-nm" },
|
|
|
|
{ LLCSAP_8021D, "802.1d" },
|
|
|
|
{ LLCSAP_RS511, "eia-rs511" },
|
|
|
|
{ LLCSAP_ISO8208, "x.25/llc2" },
|
|
|
|
{ LLCSAP_PROWAY, "proway" },
|
|
|
|
{ LLCSAP_ISONS, "iso-clns" },
|
|
|
|
{ LLCSAP_GLOBAL, "global" },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_llcsaparray(void)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register struct hnamemem *table;
|
|
|
|
|
|
|
|
for (i = 0; llcsap_db[i].s != NULL; i++) {
|
|
|
|
table = &llcsaptable[llcsap_db[i].v];
|
|
|
|
while (table->name)
|
|
|
|
table = table->nxt;
|
|
|
|
table->name = llcsap_db[i].s;
|
|
|
|
table->addr = llcsap_db[i].v;
|
1997-10-03 23:54:07 +04:00
|
|
|
table->nxt = newhnamemem();
|
1995-03-06 22:09:42 +03:00
|
|
|
}
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the address to name translation machinery. We map all
|
|
|
|
* non-local IP addresses to numeric addresses if fflag is true (i.e.,
|
|
|
|
* to prevent blocking on the nameserver). localnet is the IP address
|
|
|
|
* of the local network. mask is its subnet mask.
|
|
|
|
*/
|
|
|
|
void
|
1997-10-03 23:54:07 +04:00
|
|
|
init_addrtoname(u_int32_t localnet, u_int32_t mask)
|
1993-11-15 00:19:50 +03:00
|
|
|
{
|
|
|
|
netmask = mask;
|
|
|
|
if (fflag) {
|
|
|
|
f_localnet = localnet;
|
|
|
|
f_netmask = mask;
|
|
|
|
}
|
|
|
|
if (nflag)
|
|
|
|
/*
|
|
|
|
* Simplest way to suppress names.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
|
|
|
|
init_etherarray();
|
|
|
|
init_servarray();
|
|
|
|
init_eprotoarray();
|
1995-03-06 22:09:42 +03:00
|
|
|
init_llcsaparray();
|
|
|
|
init_protoidarray();
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
dnaddr_string(u_short dnaddr)
|
|
|
|
{
|
|
|
|
register struct hnamemem *tp;
|
|
|
|
|
|
|
|
for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
|
|
|
|
tp = tp->nxt)
|
|
|
|
if (tp->addr == dnaddr)
|
|
|
|
return (tp->name);
|
|
|
|
|
|
|
|
tp->addr = dnaddr;
|
1997-10-03 23:54:07 +04:00
|
|
|
tp->nxt = newhnamemem();
|
1995-03-06 22:09:42 +03:00
|
|
|
if (nflag)
|
|
|
|
tp->name = dnnum_string(dnaddr);
|
|
|
|
else
|
|
|
|
tp->name = dnname_string(dnaddr);
|
|
|
|
|
|
|
|
return(tp->name);
|
1993-11-15 00:19:50 +03:00
|
|
|
}
|
1997-10-03 23:54:07 +04:00
|
|
|
|
|
|
|
/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
|
|
|
|
struct hnamemem *
|
|
|
|
newhnamemem(void)
|
|
|
|
{
|
|
|
|
register struct hnamemem *p;
|
|
|
|
static struct hnamemem *ptr = NULL;
|
|
|
|
static u_int num = 0;
|
|
|
|
|
|
|
|
if (num <= 0) {
|
|
|
|
num = 64;
|
|
|
|
ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
|
|
|
|
if (ptr == NULL)
|
|
|
|
error("newhnamemem: calloc");
|
|
|
|
}
|
|
|
|
--num;
|
|
|
|
p = ptr++;
|
|
|
|
return (p);
|
|
|
|
}
|