addrtoname.c revision 162021
117680Spst/*
239300Sfenner * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
317680Spst *	The Regents of the University of California.  All rights reserved.
417680Spst *
517680Spst * Redistribution and use in source and binary forms, with or without
617680Spst * modification, are permitted provided that: (1) source code distributions
717680Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817680Spst * distributions including binary code include the above copyright notice and
917680Spst * this paragraph in its entirety in the documentation or other materials
1017680Spst * provided with the distribution, and (3) all advertising materials mentioning
1117680Spst * features or use of this software display the following acknowledgement:
1217680Spst * ``This product includes software developed by the University of California,
1317680Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417680Spst * the University nor the names of its contributors may be used to endorse
1517680Spst * or promote products derived from this software without specific prior
1617680Spst * written permission.
1717680Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817680Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917680Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2017680Spst *
2117680Spst *  Internet, ethernet, port, and protocol string to address
2217680Spst *  and address to string conversion routines
2356896Sfenner *
2456896Sfenner * $FreeBSD: head/contrib/tcpdump/addrtoname.c 162021 2006-09-04 20:25:04Z sam $
2517680Spst */
2617680Spst#ifndef lint
27127675Sbmsstatic const char rcsid[] _U_ =
28162021Ssam    "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.108.2.7 2005/09/29 07:46:45 hannes Exp $ (LBL)";
2917680Spst#endif
3017680Spst
3156896Sfenner#ifdef HAVE_CONFIG_H
3256896Sfenner#include "config.h"
3356896Sfenner#endif
3456896Sfenner
35127675Sbms#include <tcpdump-stdinc.h>
3617680Spst
37111729Sfenner#ifdef USE_ETHER_NTOHOST
3875118Sfenner#ifdef HAVE_NETINET_IF_ETHER_H
39111729Sfennerstruct mbuf;		/* Squelch compiler warnings on some platforms for */
40111729Sfennerstruct rtentry;		/* declarations in <net/if.h> */
41111729Sfenner#include <net/if.h>	/* for "struct ifnet" in "struct arpcom" on Solaris */
4275118Sfenner#include <netinet/if_ether.h>
43111729Sfenner#endif /* HAVE_NETINET_IF_ETHER_H */
44146778Ssam#ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
45146778Ssam#include <netinet/ether.h>
46146778Ssam#endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
4756896Sfenner
48146778Ssam#if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
49147904Ssam#ifndef HAVE_STRUCT_ETHER_ADDR
50147904Ssamstruct ether_addr {
51147904Ssam	unsigned char ether_addr_octet[6];
52147904Ssam};
53147904Ssam#endif
54146778Ssamextern int ether_ntohost(char *, const struct ether_addr *);
55146778Ssam#endif
56146778Ssam
57147904Ssam#endif /* USE_ETHER_NTOHOST */
58147904Ssam
5917680Spst#include <pcap.h>
6017680Spst#include <pcap-namedb.h>
6117680Spst#include <signal.h>
6217680Spst#include <stdio.h>
6317680Spst#include <string.h>
6417680Spst#include <stdlib.h>
6517680Spst
6617680Spst#include "interface.h"
6717680Spst#include "addrtoname.h"
6817680Spst#include "llc.h"
6939300Sfenner#include "setsignal.h"
70147904Ssam#include "extract.h"
71147904Ssam#include "oui.h"
7217680Spst
7317680Spst/*
7417680Spst * hash tables for whatever-to-name translations
75127675Sbms *
76127675Sbms * XXX there has to be error checks against strdup(3) failure
7717680Spst */
7817680Spst
7917680Spst#define HASHNAMESIZE 4096
80147904Ssam#define BUFSIZE 128
8117680Spst
8217680Spststruct hnamemem {
8317680Spst	u_int32_t addr;
8498527Sfenner	const char *name;
8517680Spst	struct hnamemem *nxt;
8617680Spst};
8717680Spst
8817680Spststruct hnamemem hnametable[HASHNAMESIZE];
8917680Spststruct hnamemem tporttable[HASHNAMESIZE];
9017680Spststruct hnamemem uporttable[HASHNAMESIZE];
9117680Spststruct hnamemem eprototable[HASHNAMESIZE];
9217680Spststruct hnamemem dnaddrtable[HASHNAMESIZE];
93127675Sbmsstruct hnamemem ipxsaptable[HASHNAMESIZE];
9417680Spst
95127675Sbms#if defined(INET6) && defined(WIN32)
96127675Sbms/*
97127675Sbms * fake gethostbyaddr for Win2k/XP
98127675Sbms * gethostbyaddr() returns incorrect value when AF_INET6 is passed
99127675Sbms * to 3rd argument.
100127675Sbms *
101127675Sbms * h_name in struct hostent is only valid.
102127675Sbms */
103127675Sbmsstatic struct hostent *
104127675Sbmswin32_gethostbyaddr(const char *addr, int len, int type)
105127675Sbms{
106127675Sbms	static struct hostent host;
107127675Sbms	static char hostbuf[NI_MAXHOST];
108127675Sbms	char hname[NI_MAXHOST];
109127675Sbms	struct sockaddr_in6 addr6;
110127675Sbms
111127675Sbms	host.h_name = hostbuf;
112127675Sbms	switch (type) {
113127675Sbms	case AF_INET:
114127675Sbms		return gethostbyaddr(addr, len, type);
115127675Sbms		break;
116127675Sbms	case AF_INET6:
117127675Sbms		memset(&addr6, 0, sizeof(addr6));
118127675Sbms		addr6.sin6_family = AF_INET6;
119127675Sbms		memcpy(&addr6.sin6_addr, addr, len);
120127675Sbms		if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
121146778Ssam		    hname, sizeof(hname), NULL, 0, 0)) {
122146778Ssam			return NULL;
123127675Sbms		} else {
124127675Sbms			strcpy(host.h_name, hname);
125127675Sbms			return &host;
126127675Sbms		}
127127675Sbms		break;
128127675Sbms	default:
129127675Sbms		return NULL;
130127675Sbms	}
131127675Sbms}
132127675Sbms#define gethostbyaddr win32_gethostbyaddr
133146778Ssam#endif /* INET6 & WIN32 */
134127675Sbms
13556896Sfenner#ifdef INET6
13656896Sfennerstruct h6namemem {
13756896Sfenner	struct in6_addr addr;
13856896Sfenner	char *name;
13956896Sfenner	struct h6namemem *nxt;
14056896Sfenner};
14156896Sfenner
14256896Sfennerstruct h6namemem h6nametable[HASHNAMESIZE];
14356896Sfenner#endif /* INET6 */
14456896Sfenner
14517680Spststruct enamemem {
14617680Spst	u_short e_addr0;
14717680Spst	u_short e_addr1;
14817680Spst	u_short e_addr2;
14998527Sfenner	const char *e_name;
15017680Spst	u_char *e_nsap;			/* used only for nsaptable[] */
15198527Sfenner#define e_bs e_nsap			/* for bytestringtable */
15217680Spst	struct enamemem *e_nxt;
15317680Spst};
15417680Spst
15517680Spststruct enamemem enametable[HASHNAMESIZE];
15617680Spststruct enamemem nsaptable[HASHNAMESIZE];
15798527Sfennerstruct enamemem bytestringtable[HASHNAMESIZE];
15817680Spst
15917680Spststruct protoidmem {
16017680Spst	u_int32_t p_oui;
16117680Spst	u_short p_proto;
16298527Sfenner	const char *p_name;
16317680Spst	struct protoidmem *p_nxt;
16417680Spst};
16517680Spst
16617680Spststruct protoidmem protoidtable[HASHNAMESIZE];
16717680Spst
16817680Spst/*
16917680Spst * A faster replacement for inet_ntoa().
17017680Spst */
17198527Sfennerconst char *
17217680Spstintoa(u_int32_t addr)
17317680Spst{
17417680Spst	register char *cp;
17517680Spst	register u_int byte;
17617680Spst	register int n;
17717680Spst	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
17817680Spst
17917680Spst	NTOHL(addr);
180147904Ssam	cp = buf + sizeof(buf);
18117680Spst	*--cp = '\0';
18217680Spst
18317680Spst	n = 4;
18417680Spst	do {
18517680Spst		byte = addr & 0xff;
18617680Spst		*--cp = byte % 10 + '0';
18717680Spst		byte /= 10;
18817680Spst		if (byte > 0) {
18917680Spst			*--cp = byte % 10 + '0';
19017680Spst			byte /= 10;
19117680Spst			if (byte > 0)
19217680Spst				*--cp = byte + '0';
19317680Spst		}
19417680Spst		*--cp = '.';
19517680Spst		addr >>= 8;
19617680Spst	} while (--n > 0);
19717680Spst
19817680Spst	return cp + 1;
19917680Spst}
20017680Spst
20117680Spststatic u_int32_t f_netmask;
20217680Spststatic u_int32_t f_localnet;
20317680Spst
20417680Spst/*
20517680Spst * Return a name for the IP address pointed to by ap.  This address
20617680Spst * is assumed to be in network byte order.
207127675Sbms *
208127675Sbms * NOTE: ap is *NOT* necessarily part of the packet data (not even if
209127675Sbms * this is being called with the "ipaddr_string()" macro), so you
210127675Sbms * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it.  Furthermore,
211127675Sbms * even in cases where it *is* part of the packet data, the caller
212127675Sbms * would still have to check for a null return value, even if it's
213127675Sbms * just printing the return value with "%s" - not all versions of
214127675Sbms * printf print "(null)" with "%s" and a null pointer, some of them
215127675Sbms * don't check for a null pointer and crash in that case.
216127675Sbms *
217127675Sbms * The callers of this routine should, before handing this routine
218127675Sbms * a pointer to packet data, be sure that the data is present in
219127675Sbms * the packet buffer.  They should probably do those checks anyway,
220127675Sbms * as other data at that layer might not be IP addresses, and it
221127675Sbms * also needs to check whether they're present in the packet buffer.
22217680Spst */
22398527Sfennerconst char *
22417680Spstgetname(const u_char *ap)
22517680Spst{
22617680Spst	register struct hostent *hp;
22717680Spst	u_int32_t addr;
22817680Spst	static struct hnamemem *p;		/* static for longjmp() */
22917680Spst
23056896Sfenner	memcpy(&addr, ap, sizeof(addr));
23117680Spst	p = &hnametable[addr & (HASHNAMESIZE-1)];
23217680Spst	for (; p->nxt; p = p->nxt) {
23317680Spst		if (p->addr == addr)
23417680Spst			return (p->name);
23517680Spst	}
23617680Spst	p->addr = addr;
23717680Spst	p->nxt = newhnamemem();
23817680Spst
23917680Spst	/*
240127675Sbms	 * Print names unless:
241127675Sbms	 *	(1) -n was given.
24239300Sfenner	 *      (2) Address is foreign and -f was given. (If -f was not
243127675Sbms	 *	    given, f_netmask and f_localnet are 0 and the test
24439300Sfenner	 *	    evaluates to true)
24517680Spst	 */
24639300Sfenner	if (!nflag &&
247127675Sbms	    (addr & f_netmask) == f_localnet) {
248127675Sbms		hp = gethostbyaddr((char *)&addr, 4, AF_INET);
249127675Sbms		if (hp) {
250127675Sbms			char *dotp;
25117680Spst
252127675Sbms			p->name = strdup(hp->h_name);
253127675Sbms			if (Nflag) {
254127675Sbms				/* Remove domain qualifications */
255127675Sbms				dotp = strchr(p->name, '.');
256127675Sbms				if (dotp)
257127675Sbms					*dotp = '\0';
25817680Spst			}
259127675Sbms			return (p->name);
26017680Spst		}
26117680Spst	}
26298527Sfenner	p->name = strdup(intoa(addr));
26317680Spst	return (p->name);
26417680Spst}
26517680Spst
26656896Sfenner#ifdef INET6
26756896Sfenner/*
26856896Sfenner * Return a name for the IP6 address pointed to by ap.  This address
26956896Sfenner * is assumed to be in network byte order.
27056896Sfenner */
27198527Sfennerconst char *
27256896Sfennergetname6(const u_char *ap)
27356896Sfenner{
27456896Sfenner	register struct hostent *hp;
27556896Sfenner	struct in6_addr addr;
27656896Sfenner	static struct h6namemem *p;		/* static for longjmp() */
27798527Sfenner	register const char *cp;
27856896Sfenner	char ntop_buf[INET6_ADDRSTRLEN];
27956896Sfenner
28056896Sfenner	memcpy(&addr, ap, sizeof(addr));
28156896Sfenner	p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
28256896Sfenner	for (; p->nxt; p = p->nxt) {
28356896Sfenner		if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
28456896Sfenner			return (p->name);
28556896Sfenner	}
28656896Sfenner	p->addr = addr;
28756896Sfenner	p->nxt = newh6namemem();
28856896Sfenner
28956896Sfenner	/*
290127675Sbms	 * Do not print names if -n was given.
29156896Sfenner	 */
292127675Sbms	if (!nflag) {
293127675Sbms		hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
294127675Sbms		if (hp) {
295127675Sbms			char *dotp;
29656896Sfenner
297127675Sbms			p->name = strdup(hp->h_name);
298127675Sbms			if (Nflag) {
299127675Sbms				/* Remove domain qualifications */
300127675Sbms				dotp = strchr(p->name, '.');
301127675Sbms				if (dotp)
302127675Sbms					*dotp = '\0';
30356896Sfenner			}
304127675Sbms			return (p->name);
30556896Sfenner		}
30656896Sfenner	}
30798527Sfenner	cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
30898527Sfenner	p->name = strdup(cp);
30956896Sfenner	return (p->name);
31056896Sfenner}
31156896Sfenner#endif /* INET6 */
31256896Sfenner
31317680Spststatic char hex[] = "0123456789abcdef";
31417680Spst
31517680Spst
31617680Spst/* Find the hash node that corresponds the ether address 'ep' */
31717680Spst
31817680Spststatic inline struct enamemem *
31917680Spstlookup_emem(const u_char *ep)
32017680Spst{
32117680Spst	register u_int i, j, k;
32217680Spst	struct enamemem *tp;
32317680Spst
32417680Spst	k = (ep[0] << 8) | ep[1];
32517680Spst	j = (ep[2] << 8) | ep[3];
32617680Spst	i = (ep[4] << 8) | ep[5];
32717680Spst
32817680Spst	tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
32917680Spst	while (tp->e_nxt)
33017680Spst		if (tp->e_addr0 == i &&
33117680Spst		    tp->e_addr1 == j &&
33217680Spst		    tp->e_addr2 == k)
33317680Spst			return tp;
33417680Spst		else
33517680Spst			tp = tp->e_nxt;
33617680Spst	tp->e_addr0 = i;
33717680Spst	tp->e_addr1 = j;
33817680Spst	tp->e_addr2 = k;
33917680Spst	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
34017680Spst	if (tp->e_nxt == NULL)
34117680Spst		error("lookup_emem: calloc");
34217680Spst
34317680Spst	return tp;
34417680Spst}
34517680Spst
34698527Sfenner/*
347127675Sbms * Find the hash node that corresponds to the bytestring 'bs'
34898527Sfenner * with length 'nlen'
34998527Sfenner */
35098527Sfenner
35198527Sfennerstatic inline struct enamemem *
35298527Sfennerlookup_bytestring(register const u_char *bs, const unsigned int nlen)
35398527Sfenner{
35498527Sfenner	struct enamemem *tp;
35598527Sfenner	register u_int i, j, k;
35698527Sfenner
35798527Sfenner	if (nlen >= 6) {
35898527Sfenner		k = (bs[0] << 8) | bs[1];
35998527Sfenner		j = (bs[2] << 8) | bs[3];
36098527Sfenner		i = (bs[4] << 8) | bs[5];
36198527Sfenner	} else if (nlen >= 4) {
36298527Sfenner		k = (bs[0] << 8) | bs[1];
36398527Sfenner		j = (bs[2] << 8) | bs[3];
36498527Sfenner		i = 0;
36598527Sfenner	} else
36698527Sfenner		i = j = k = 0;
36798527Sfenner
36898527Sfenner	tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
36998527Sfenner	while (tp->e_nxt)
37098527Sfenner		if (tp->e_addr0 == i &&
37198527Sfenner		    tp->e_addr1 == j &&
37298527Sfenner		    tp->e_addr2 == k &&
37398527Sfenner		    memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
37498527Sfenner			return tp;
37598527Sfenner		else
37698527Sfenner			tp = tp->e_nxt;
37798527Sfenner
37898527Sfenner	tp->e_addr0 = i;
37998527Sfenner	tp->e_addr1 = j;
38098527Sfenner	tp->e_addr2 = k;
38198527Sfenner
38298527Sfenner	tp->e_bs = (u_char *) calloc(1, nlen + 1);
38398527Sfenner	memcpy(tp->e_bs, bs, nlen);
38498527Sfenner	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
38598527Sfenner	if (tp->e_nxt == NULL)
38698527Sfenner		error("lookup_bytestring: calloc");
38798527Sfenner
38898527Sfenner	return tp;
38998527Sfenner}
39098527Sfenner
39117680Spst/* Find the hash node that corresponds the NSAP 'nsap' */
39217680Spst
39317680Spststatic inline struct enamemem *
39417680Spstlookup_nsap(register const u_char *nsap)
39517680Spst{
39617680Spst	register u_int i, j, k;
39798527Sfenner	unsigned int nlen = *nsap;
39817680Spst	struct enamemem *tp;
39917680Spst	const u_char *ensap = nsap + nlen - 6;
40017680Spst
40117680Spst	if (nlen > 6) {
40217680Spst		k = (ensap[0] << 8) | ensap[1];
40317680Spst		j = (ensap[2] << 8) | ensap[3];
40417680Spst		i = (ensap[4] << 8) | ensap[5];
40517680Spst	}
40617680Spst	else
40717680Spst		i = j = k = 0;
40817680Spst
40917680Spst	tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
41017680Spst	while (tp->e_nxt)
41117680Spst		if (tp->e_addr0 == i &&
41217680Spst		    tp->e_addr1 == j &&
41317680Spst		    tp->e_addr2 == k &&
41417680Spst		    tp->e_nsap[0] == nlen &&
41598527Sfenner		    memcmp((const char *)&(nsap[1]),
41617680Spst			(char *)&(tp->e_nsap[1]), nlen) == 0)
41717680Spst			return tp;
41817680Spst		else
41917680Spst			tp = tp->e_nxt;
42017680Spst	tp->e_addr0 = i;
42117680Spst	tp->e_addr1 = j;
42217680Spst	tp->e_addr2 = k;
42317680Spst	tp->e_nsap = (u_char *)malloc(nlen + 1);
42417680Spst	if (tp->e_nsap == NULL)
42517680Spst		error("lookup_nsap: malloc");
42698527Sfenner	memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
42717680Spst	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
42817680Spst	if (tp->e_nxt == NULL)
42917680Spst		error("lookup_nsap: calloc");
43017680Spst
43117680Spst	return tp;
43217680Spst}
43317680Spst
43417680Spst/* Find the hash node that corresponds the protoid 'pi'. */
43517680Spst
43617680Spststatic inline struct protoidmem *
43717680Spstlookup_protoid(const u_char *pi)
43817680Spst{
43917680Spst	register u_int i, j;
44017680Spst	struct protoidmem *tp;
44117680Spst
44217680Spst	/* 5 octets won't be aligned */
44317680Spst	i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
44417680Spst	j =   (pi[3] << 8) + pi[4];
44517680Spst	/* XXX should be endian-insensitive, but do big-endian testing  XXX */
44617680Spst
44717680Spst	tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
44817680Spst	while (tp->p_nxt)
44917680Spst		if (tp->p_oui == i && tp->p_proto == j)
45017680Spst			return tp;
45117680Spst		else
45217680Spst			tp = tp->p_nxt;
45317680Spst	tp->p_oui = i;
45417680Spst	tp->p_proto = j;
45517680Spst	tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
45617680Spst	if (tp->p_nxt == NULL)
45717680Spst		error("lookup_protoid: calloc");
45817680Spst
45917680Spst	return tp;
46017680Spst}
46117680Spst
46298527Sfennerconst char *
46317680Spstetheraddr_string(register const u_char *ep)
46417680Spst{
465162021Ssam	register int i;
46617680Spst	register char *cp;
46717680Spst	register struct enamemem *tp;
468162021Ssam	int oui;
469147904Ssam	char buf[BUFSIZE];
47017680Spst
47117680Spst	tp = lookup_emem(ep);
47217680Spst	if (tp->e_name)
47317680Spst		return (tp->e_name);
47498527Sfenner#ifdef USE_ETHER_NTOHOST
47517680Spst	if (!nflag) {
476147904Ssam		char buf2[BUFSIZE];
477146778Ssam
478146778Ssam		/*
479146778Ssam		 * We don't cast it to "const struct ether_addr *"
480162021Ssam		 * because some systems fail to declare the second
481162021Ssam		 * argument as a "const" pointer, even though they
482162021Ssam		 * don't modify what it points to.
483146778Ssam		 */
484146778Ssam		if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
485127675Sbms			tp->e_name = strdup(buf2);
48617680Spst			return (tp->e_name);
48717680Spst		}
48817680Spst	}
48917680Spst#endif
49017680Spst	cp = buf;
491162021Ssam	oui = EXTRACT_24BITS(ep);
492146778Ssam	*cp++ = hex[*ep >> 4 ];
49317680Spst	*cp++ = hex[*ep++ & 0xf];
494162021Ssam	for (i = 5; --i >= 0;) {
495162021Ssam		*cp++ = ':';
496162021Ssam		*cp++ = hex[*ep >> 4 ];
497162021Ssam		*cp++ = hex[*ep++ & 0xf];
498162021Ssam	}
499147904Ssam
500162021Ssam	if (!nflag) {
501162021Ssam		snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
502162021Ssam		    tok2str(oui_values, "Unknown", oui));
503162021Ssam	} else
504162021Ssam		*cp = '\0';
50598527Sfenner	tp->e_name = strdup(buf);
50617680Spst	return (tp->e_name);
50717680Spst}
50817680Spst
50998527Sfennerconst char *
51098527Sfennerlinkaddr_string(const u_char *ep, const unsigned int len)
51198527Sfenner{
512146778Ssam	register u_int i;
51398527Sfenner	register char *cp;
51498527Sfenner	register struct enamemem *tp;
51598527Sfenner
51698527Sfenner	if (len == 6)	/* XXX not totally correct... */
51798527Sfenner		return etheraddr_string(ep);
518127675Sbms
51998527Sfenner	tp = lookup_bytestring(ep, len);
52098527Sfenner	if (tp->e_name)
52198527Sfenner		return (tp->e_name);
52298527Sfenner
52398527Sfenner	tp->e_name = cp = (char *)malloc(len*3);
52498527Sfenner	if (tp->e_name == NULL)
52598527Sfenner		error("linkaddr_string: malloc");
526146778Ssam	*cp++ = hex[*ep >> 4];
52798527Sfenner	*cp++ = hex[*ep++ & 0xf];
52898527Sfenner	for (i = len-1; i > 0 ; --i) {
52998527Sfenner		*cp++ = ':';
530146778Ssam		*cp++ = hex[*ep >> 4];
53198527Sfenner		*cp++ = hex[*ep++ & 0xf];
53298527Sfenner	}
53398527Sfenner	*cp = '\0';
53498527Sfenner	return (tp->e_name);
53598527Sfenner}
53698527Sfenner
53798527Sfennerconst char *
53817680Spstetherproto_string(u_short port)
53917680Spst{
54017680Spst	register char *cp;
54117680Spst	register struct hnamemem *tp;
54217680Spst	register u_int32_t i = port;
54317680Spst	char buf[sizeof("0000")];
54417680Spst
54517680Spst	for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
54617680Spst		if (tp->addr == i)
54717680Spst			return (tp->name);
54817680Spst
54917680Spst	tp->addr = i;
55017680Spst	tp->nxt = newhnamemem();
55117680Spst
55217680Spst	cp = buf;
55317680Spst	NTOHS(port);
55417680Spst	*cp++ = hex[port >> 12 & 0xf];
55517680Spst	*cp++ = hex[port >> 8 & 0xf];
55617680Spst	*cp++ = hex[port >> 4 & 0xf];
55717680Spst	*cp++ = hex[port & 0xf];
55817680Spst	*cp++ = '\0';
55998527Sfenner	tp->name = strdup(buf);
56017680Spst	return (tp->name);
56117680Spst}
56217680Spst
56398527Sfennerconst char *
56417680Spstprotoid_string(register const u_char *pi)
56517680Spst{
56617680Spst	register u_int i, j;
56717680Spst	register char *cp;
56817680Spst	register struct protoidmem *tp;
56917680Spst	char buf[sizeof("00:00:00:00:00")];
57017680Spst
57117680Spst	tp = lookup_protoid(pi);
57217680Spst	if (tp->p_name)
57317680Spst		return tp->p_name;
57417680Spst
57517680Spst	cp = buf;
57617680Spst	if ((j = *pi >> 4) != 0)
57717680Spst		*cp++ = hex[j];
57817680Spst	*cp++ = hex[*pi++ & 0xf];
57917680Spst	for (i = 4; (int)--i >= 0;) {
58017680Spst		*cp++ = ':';
58117680Spst		if ((j = *pi >> 4) != 0)
58217680Spst			*cp++ = hex[j];
58317680Spst		*cp++ = hex[*pi++ & 0xf];
58417680Spst	}
58517680Spst	*cp = '\0';
58698527Sfenner	tp->p_name = strdup(buf);
58717680Spst	return (tp->p_name);
58817680Spst}
58917680Spst
590146778Ssam#define ISONSAP_MAX_LENGTH 20
59198527Sfennerconst char *
592146778Ssamisonsap_string(const u_char *nsap, register u_int nsap_length)
59317680Spst{
594146778Ssam	register u_int nsap_idx;
59517680Spst	register char *cp;
59617680Spst	register struct enamemem *tp;
59717680Spst
598146778Ssam	if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
599147904Ssam		return ("isonsap_string: illegal length");
600146778Ssam
60117680Spst	tp = lookup_nsap(nsap);
60217680Spst	if (tp->e_name)
60317680Spst		return tp->e_name;
60417680Spst
605146778Ssam	tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
60617680Spst	if (cp == NULL)
60717680Spst		error("isonsap_string: malloc");
60817680Spst
609146778Ssam	for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
61017680Spst		*cp++ = hex[*nsap >> 4];
61117680Spst		*cp++ = hex[*nsap++ & 0xf];
612146778Ssam		if (((nsap_idx & 1) == 0) &&
613146778Ssam		     (nsap_idx + 1 < nsap_length)) {
614146778Ssam		     	*cp++ = '.';
615146778Ssam		}
61617680Spst	}
61717680Spst	*cp = '\0';
61817680Spst	return (tp->e_name);
61917680Spst}
62017680Spst
62198527Sfennerconst char *
62217680Spsttcpport_string(u_short port)
62317680Spst{
62417680Spst	register struct hnamemem *tp;
62517680Spst	register u_int32_t i = port;
62617680Spst	char buf[sizeof("00000")];
62717680Spst
62817680Spst	for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
62917680Spst		if (tp->addr == i)
63017680Spst			return (tp->name);
63117680Spst
63217680Spst	tp->addr = i;
63317680Spst	tp->nxt = newhnamemem();
63417680Spst
63566644Skris	(void)snprintf(buf, sizeof(buf), "%u", i);
63698527Sfenner	tp->name = strdup(buf);
63717680Spst	return (tp->name);
63817680Spst}
63917680Spst
64098527Sfennerconst char *
64117680Spstudpport_string(register u_short port)
64217680Spst{
64317680Spst	register struct hnamemem *tp;
64417680Spst	register u_int32_t i = port;
64517680Spst	char buf[sizeof("00000")];
64617680Spst
64717680Spst	for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
64817680Spst		if (tp->addr == i)
64917680Spst			return (tp->name);
65017680Spst
65117680Spst	tp->addr = i;
65217680Spst	tp->nxt = newhnamemem();
65317680Spst
65466644Skris	(void)snprintf(buf, sizeof(buf), "%u", i);
65598527Sfenner	tp->name = strdup(buf);
65617680Spst	return (tp->name);
65717680Spst}
65817680Spst
659127675Sbmsconst char *
660127675Sbmsipxsap_string(u_short port)
661127675Sbms{
662127675Sbms	register char *cp;
663127675Sbms	register struct hnamemem *tp;
664127675Sbms	register u_int32_t i = port;
665127675Sbms	char buf[sizeof("0000")];
666127675Sbms
667127675Sbms	for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
668127675Sbms		if (tp->addr == i)
669127675Sbms			return (tp->name);
670127675Sbms
671127675Sbms	tp->addr = i;
672127675Sbms	tp->nxt = newhnamemem();
673127675Sbms
674127675Sbms	cp = buf;
675127675Sbms	NTOHS(port);
676127675Sbms	*cp++ = hex[port >> 12 & 0xf];
677127675Sbms	*cp++ = hex[port >> 8 & 0xf];
678127675Sbms	*cp++ = hex[port >> 4 & 0xf];
679127675Sbms	*cp++ = hex[port & 0xf];
680127675Sbms	*cp++ = '\0';
681127675Sbms	tp->name = strdup(buf);
682127675Sbms	return (tp->name);
683127675Sbms}
684127675Sbms
68517680Spststatic void
68617680Spstinit_servarray(void)
68717680Spst{
68817680Spst	struct servent *sv;
68917680Spst	register struct hnamemem *table;
69017680Spst	register int i;
69117680Spst	char buf[sizeof("0000000000")];
69217680Spst
69317680Spst	while ((sv = getservent()) != NULL) {
69417680Spst		int port = ntohs(sv->s_port);
69517680Spst		i = port & (HASHNAMESIZE-1);
69617680Spst		if (strcmp(sv->s_proto, "tcp") == 0)
69717680Spst			table = &tporttable[i];
69817680Spst		else if (strcmp(sv->s_proto, "udp") == 0)
69917680Spst			table = &uporttable[i];
70017680Spst		else
70117680Spst			continue;
70217680Spst
70317680Spst		while (table->name)
70417680Spst			table = table->nxt;
70517680Spst		if (nflag) {
70666644Skris			(void)snprintf(buf, sizeof(buf), "%d", port);
70798527Sfenner			table->name = strdup(buf);
70817680Spst		} else
70998527Sfenner			table->name = strdup(sv->s_name);
71017680Spst		table->addr = port;
71117680Spst		table->nxt = newhnamemem();
71217680Spst	}
71317680Spst	endservent();
71417680Spst}
71517680Spst
716146778Ssam/* in libpcap.a (nametoaddr.c) */
717146778Ssam#if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
718146778Ssam__declspec(dllimport)
719127675Sbms#else
720146778Ssamextern
721127675Sbms#endif
722146778Ssamconst struct eproto {
723146778Ssam	const char *s;
72417680Spst	u_short p;
72517680Spst} eproto_db[];
72617680Spst
72717680Spststatic void
72817680Spstinit_eprotoarray(void)
72917680Spst{
73017680Spst	register int i;
73117680Spst	register struct hnamemem *table;
73217680Spst
73317680Spst	for (i = 0; eproto_db[i].s; i++) {
734127675Sbms		int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
73517680Spst		table = &eprototable[j];
73617680Spst		while (table->name)
73717680Spst			table = table->nxt;
73817680Spst		table->name = eproto_db[i].s;
739127675Sbms		table->addr = htons(eproto_db[i].p);
74017680Spst		table->nxt = newhnamemem();
74117680Spst	}
74217680Spst}
74317680Spst
74498527Sfennerstatic struct protoidlist {
74598527Sfenner	const u_char protoid[5];
74698527Sfenner	const char *name;
74798527Sfenner} protoidlist[] = {
74898527Sfenner	{{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
74998527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
75098527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
75198527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
75298527Sfenner	{{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
75398527Sfenner	{{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
75498527Sfenner};
75598527Sfenner
75617680Spst/*
75717680Spst * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
75817680Spst * types.
75917680Spst */
76017680Spststatic void
76117680Spstinit_protoidarray(void)
76217680Spst{
76317680Spst	register int i;
76417680Spst	register struct protoidmem *tp;
76598527Sfenner	struct protoidlist *pl;
76617680Spst	u_char protoid[5];
76717680Spst
76817680Spst	protoid[0] = 0;
76917680Spst	protoid[1] = 0;
77017680Spst	protoid[2] = 0;
77117680Spst	for (i = 0; eproto_db[i].s; i++) {
77217680Spst		u_short etype = htons(eproto_db[i].p);
77317680Spst
77417680Spst		memcpy((char *)&protoid[3], (char *)&etype, 2);
77517680Spst		tp = lookup_protoid(protoid);
77698527Sfenner		tp->p_name = strdup(eproto_db[i].s);
77717680Spst	}
77898527Sfenner	/* Hardwire some SNAP proto ID names */
77998527Sfenner	for (pl = protoidlist; pl->name != NULL; ++pl) {
78098527Sfenner		tp = lookup_protoid(pl->protoid);
78198527Sfenner		/* Don't override existing name */
78298527Sfenner		if (tp->p_name != NULL)
78398527Sfenner			continue;
78498527Sfenner
78598527Sfenner		tp->p_name = pl->name;
78698527Sfenner	}
78717680Spst}
78817680Spst
78917680Spststatic struct etherlist {
79098527Sfenner	const u_char addr[6];
79198527Sfenner	const char *name;
79217680Spst} etherlist[] = {
79317680Spst	{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
79417680Spst	{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
79517680Spst};
79617680Spst
79717680Spst/*
79817680Spst * Initialize the ethers hash table.  We take two different approaches
79917680Spst * depending on whether or not the system provides the ethers name
80017680Spst * service.  If it does, we just wire in a few names at startup,
80117680Spst * and etheraddr_string() fills in the table on demand.  If it doesn't,
80217680Spst * then we suck in the entire /etc/ethers file at startup.  The idea
80317680Spst * is that parsing the local file will be fast, but spinning through
80417680Spst * all the ethers entries via NIS & next_etherent might be very slow.
80517680Spst *
80617680Spst * XXX pcap_next_etherent doesn't belong in the pcap interface, but
80717680Spst * since the pcap module already does name-to-address translation,
80817680Spst * it's already does most of the work for the ethernet address-to-name
80917680Spst * translation, so we just pcap_next_etherent as a convenience.
81017680Spst */
81117680Spststatic void
81217680Spstinit_etherarray(void)
81317680Spst{
81417680Spst	register struct etherlist *el;
81517680Spst	register struct enamemem *tp;
81698527Sfenner#ifdef USE_ETHER_NTOHOST
81717680Spst	char name[256];
81817680Spst#else
81917680Spst	register struct pcap_etherent *ep;
82017680Spst	register FILE *fp;
82117680Spst
82217680Spst	/* Suck in entire ethers file */
82317680Spst	fp = fopen(PCAP_ETHERS_FILE, "r");
82417680Spst	if (fp != NULL) {
82517680Spst		while ((ep = pcap_next_etherent(fp)) != NULL) {
82617680Spst			tp = lookup_emem(ep->addr);
82798527Sfenner			tp->e_name = strdup(ep->name);
82817680Spst		}
82917680Spst		(void)fclose(fp);
83017680Spst	}
83117680Spst#endif
83217680Spst
83317680Spst	/* Hardwire some ethernet names */
83417680Spst	for (el = etherlist; el->name != NULL; ++el) {
83517680Spst		tp = lookup_emem(el->addr);
83617680Spst		/* Don't override existing name */
83717680Spst		if (tp->e_name != NULL)
83817680Spst			continue;
83917680Spst
84098527Sfenner#ifdef USE_ETHER_NTOHOST
841146778Ssam		/*
842146778Ssam		 * Use YP/NIS version of name if available.
843146778Ssam		 *
844146778Ssam		 * We don't cast it to "const struct ether_addr *"
845146778Ssam		 * because some systems don't modify the Ethernet
846146778Ssam		 * address but fail to declare the second argument
847146778Ssam		 * as a "const" pointer.
848146778Ssam		 */
849146778Ssam		if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
850146778Ssam			tp->e_name = strdup(name);
85117680Spst			continue;
85217680Spst		}
85317680Spst#endif
85417680Spst		tp->e_name = el->name;
85517680Spst	}
85617680Spst}
85717680Spst
858127675Sbmsstatic struct tok ipxsap_db[] = {
859127675Sbms	{ 0x0000, "Unknown" },
860127675Sbms	{ 0x0001, "User" },
861127675Sbms	{ 0x0002, "User Group" },
862127675Sbms	{ 0x0003, "PrintQueue" },
863127675Sbms	{ 0x0004, "FileServer" },
864127675Sbms	{ 0x0005, "JobServer" },
865127675Sbms	{ 0x0006, "Gateway" },
866127675Sbms	{ 0x0007, "PrintServer" },
867127675Sbms	{ 0x0008, "ArchiveQueue" },
868127675Sbms	{ 0x0009, "ArchiveServer" },
869127675Sbms	{ 0x000a, "JobQueue" },
870127675Sbms	{ 0x000b, "Administration" },
871127675Sbms	{ 0x000F, "Novell TI-RPC" },
872127675Sbms	{ 0x0017, "Diagnostics" },
873127675Sbms	{ 0x0020, "NetBIOS" },
874127675Sbms	{ 0x0021, "NAS SNA Gateway" },
875127675Sbms	{ 0x0023, "NACS AsyncGateway" },
876127675Sbms	{ 0x0024, "RemoteBridge/RoutingService" },
877127675Sbms	{ 0x0026, "BridgeServer" },
878127675Sbms	{ 0x0027, "TCP/IP Gateway" },
879127675Sbms	{ 0x0028, "Point-to-point X.25 BridgeServer" },
880127675Sbms	{ 0x0029, "3270 Gateway" },
881127675Sbms	{ 0x002a, "CHI Corp" },
882127675Sbms	{ 0x002c, "PC Chalkboard" },
883127675Sbms	{ 0x002d, "TimeSynchServer" },
884127675Sbms	{ 0x002e, "ARCserve5.0/PalindromeBackup" },
885127675Sbms	{ 0x0045, "DI3270 Gateway" },
886127675Sbms	{ 0x0047, "AdvertisingPrintServer" },
887127675Sbms	{ 0x004a, "NetBlazerModems" },
888127675Sbms	{ 0x004b, "BtrieveVAP" },
889127675Sbms	{ 0x004c, "NetwareSQL" },
890127675Sbms	{ 0x004d, "XtreeNetwork" },
891127675Sbms	{ 0x0050, "BtrieveVAP4.11" },
892127675Sbms	{ 0x0052, "QuickLink" },
893127675Sbms	{ 0x0053, "PrintQueueUser" },
894127675Sbms	{ 0x0058, "Multipoint X.25 Router" },
895127675Sbms	{ 0x0060, "STLB/NLM" },
896127675Sbms	{ 0x0064, "ARCserve" },
897127675Sbms	{ 0x0066, "ARCserve3.0" },
898127675Sbms	{ 0x0072, "WAN CopyUtility" },
899127675Sbms	{ 0x007a, "TES-NetwareVMS" },
900127675Sbms	{ 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
901127675Sbms	{ 0x0095, "DDA OBGYN" },
902127675Sbms	{ 0x0098, "NetwareAccessServer" },
903127675Sbms	{ 0x009a, "Netware for VMS II/NamedPipeServer" },
904127675Sbms	{ 0x009b, "NetwareAccessServer" },
905127675Sbms	{ 0x009e, "PortableNetwareServer/SunLinkNVT" },
906127675Sbms	{ 0x00a1, "PowerchuteAPC UPS" },
907127675Sbms	{ 0x00aa, "LAWserve" },
908127675Sbms	{ 0x00ac, "CompaqIDA StatusMonitor" },
909127675Sbms	{ 0x0100, "PIPE STAIL" },
910127675Sbms	{ 0x0102, "LAN ProtectBindery" },
911127675Sbms	{ 0x0103, "OracleDataBaseServer" },
912127675Sbms	{ 0x0107, "Netware386/RSPX RemoteConsole" },
913127675Sbms	{ 0x010f, "NovellSNA Gateway" },
914127675Sbms	{ 0x0111, "TestServer" },
915127675Sbms	{ 0x0112, "HP PrintServer" },
916127675Sbms	{ 0x0114, "CSA MUX" },
917127675Sbms	{ 0x0115, "CSA LCA" },
918127675Sbms	{ 0x0116, "CSA CM" },
919127675Sbms	{ 0x0117, "CSA SMA" },
920127675Sbms	{ 0x0118, "CSA DBA" },
921127675Sbms	{ 0x0119, "CSA NMA" },
922127675Sbms	{ 0x011a, "CSA SSA" },
923127675Sbms	{ 0x011b, "CSA STATUS" },
924127675Sbms	{ 0x011e, "CSA APPC" },
925127675Sbms	{ 0x0126, "SNA TEST SSA Profile" },
926127675Sbms	{ 0x012a, "CSA TRACE" },
927127675Sbms	{ 0x012b, "NetwareSAA" },
928127675Sbms	{ 0x012e, "IKARUS VirusScan" },
929127675Sbms	{ 0x0130, "CommunicationsExecutive" },
930127675Sbms	{ 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
931127675Sbms	{ 0x0135, "NetwareNamingServicesProfile" },
932127675Sbms	{ 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
933127675Sbms	{ 0x0141, "LAN SpoolServer" },
934127675Sbms	{ 0x0152, "IRMALAN Gateway" },
935127675Sbms	{ 0x0154, "NamedPipeServer" },
936127675Sbms	{ 0x0166, "NetWareManagement" },
937127675Sbms	{ 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
938127675Sbms	{ 0x0173, "Compaq" },
939127675Sbms	{ 0x0174, "Compaq SNMP Agent" },
940127675Sbms	{ 0x0175, "Compaq" },
941127675Sbms	{ 0x0180, "XTreeServer/XTreeTools" },
942127675Sbms	{ 0x018A, "NASI ServicesBroadcastServer" },
943127675Sbms	{ 0x01b0, "GARP Gateway" },
944127675Sbms	{ 0x01b1, "Binfview" },
945127675Sbms	{ 0x01bf, "IntelLanDeskManager" },
946127675Sbms	{ 0x01ca, "AXTEC" },
947127675Sbms	{ 0x01cb, "ShivaNetModem/E" },
948127675Sbms	{ 0x01cc, "ShivaLanRover/E" },
949127675Sbms	{ 0x01cd, "ShivaLanRover/T" },
950127675Sbms	{ 0x01ce, "ShivaUniversal" },
951127675Sbms	{ 0x01d8, "CastelleFAXPressServer" },
952127675Sbms	{ 0x01da, "CastelleLANPressPrintServer" },
953127675Sbms	{ 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
954127675Sbms	{ 0x01f0, "LEGATO" },
955127675Sbms	{ 0x01f5, "LEGATO" },
956127675Sbms	{ 0x0233, "NMS Agent/NetwareManagementAgent" },
957127675Sbms	{ 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
958127675Sbms	{ 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
959127675Sbms	{ 0x023a, "LANtern" },
960127675Sbms	{ 0x023c, "MAVERICK" },
961127675Sbms	{ 0x023f, "NovellSMDR" },
962127675Sbms	{ 0x024e, "NetwareConnect" },
963127675Sbms	{ 0x024f, "NASI ServerBroadcast Cisco" },
964127675Sbms	{ 0x026a, "NMS ServiceConsole" },
965127675Sbms	{ 0x026b, "TimeSynchronizationServer Netware 4.x" },
966127675Sbms	{ 0x0278, "DirectoryServer Netware 4.x" },
967127675Sbms	{ 0x027b, "NetwareManagementAgent" },
968127675Sbms	{ 0x0280, "Novell File and Printer Sharing Service for PC" },
969127675Sbms	{ 0x0304, "NovellSAA Gateway" },
970127675Sbms	{ 0x0308, "COM/VERMED" },
971127675Sbms	{ 0x030a, "GalacticommWorldgroupServer" },
972127675Sbms	{ 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
973127675Sbms	{ 0x0320, "AttachmateGateway" },
974127675Sbms	{ 0x0327, "MicrosoftDiagnostiocs" },
975127675Sbms	{ 0x0328, "WATCOM SQL Server" },
976127675Sbms	{ 0x0335, "MultiTechSystems MultisynchCommServer" },
977127675Sbms	{ 0x0343, "Xylogics RemoteAccessServer/LANModem" },
978127675Sbms	{ 0x0355, "ArcadaBackupExec" },
979127675Sbms	{ 0x0358, "MSLCD1" },
980127675Sbms	{ 0x0361, "NETINELO" },
981127675Sbms	{ 0x037e, "Powerchute UPS Monitoring" },
982127675Sbms	{ 0x037f, "ViruSafeNotify" },
983127675Sbms	{ 0x0386, "HP Bridge" },
984127675Sbms	{ 0x0387, "HP Hub" },
985127675Sbms	{ 0x0394, "NetWare SAA Gateway" },
986127675Sbms	{ 0x039b, "LotusNotes" },
987127675Sbms	{ 0x03b7, "CertusAntiVirus" },
988127675Sbms	{ 0x03c4, "ARCserve4.0" },
989127675Sbms	{ 0x03c7, "LANspool3.5" },
990127675Sbms	{ 0x03d7, "LexmarkPrinterServer" },
991127675Sbms	{ 0x03d8, "LexmarkXLE PrinterServer" },
992127675Sbms	{ 0x03dd, "BanyanENS NetwareClient" },
993127675Sbms	{ 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
994127675Sbms	{ 0x03e1, "UnivelUnixware" },
995127675Sbms	{ 0x03e4, "UnivelUnixware" },
996127675Sbms	{ 0x03fc, "IntelNetport" },
997127675Sbms	{ 0x03fd, "PrintServerQueue" },
998127675Sbms	{ 0x040A, "ipnServer" },
999127675Sbms	{ 0x040D, "LVERRMAN" },
1000127675Sbms	{ 0x040E, "LVLIC" },
1001127675Sbms	{ 0x0414, "NET Silicon (DPI)/Kyocera" },
1002127675Sbms	{ 0x0429, "SiteLockVirus" },
1003127675Sbms	{ 0x0432, "UFHELPR???" },
1004127675Sbms	{ 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1005127675Sbms	{ 0x0444, "MicrosoftNT SNA Server" },
1006127675Sbms	{ 0x0448, "Oracle" },
1007127675Sbms	{ 0x044c, "ARCserve5.01" },
1008127675Sbms	{ 0x0457, "CanonGP55" },
1009127675Sbms	{ 0x045a, "QMS Printers" },
1010127675Sbms	{ 0x045b, "DellSCSI Array" },
1011127675Sbms	{ 0x0491, "NetBlazerModems" },
1012127675Sbms	{ 0x04ac, "OnTimeScheduler" },
1013127675Sbms	{ 0x04b0, "CD-Net" },
1014127675Sbms	{ 0x0513, "EmulexNQA" },
1015127675Sbms	{ 0x0520, "SiteLockChecks" },
1016127675Sbms	{ 0x0529, "SiteLockChecks" },
1017127675Sbms	{ 0x052d, "CitrixOS2 AppServer" },
1018127675Sbms	{ 0x0535, "Tektronix" },
1019127675Sbms	{ 0x0536, "Milan" },
1020127675Sbms	{ 0x055d, "Attachmate SNA gateway" },
1021127675Sbms	{ 0x056b, "IBM8235 ModemServer" },
1022127675Sbms	{ 0x056c, "ShivaLanRover/E PLUS" },
1023127675Sbms	{ 0x056d, "ShivaLanRover/T PLUS" },
1024127675Sbms	{ 0x0580, "McAfeeNetShield" },
1025127675Sbms	{ 0x05B8, "NLM to workstation communication (Revelation Software)" },
1026127675Sbms	{ 0x05BA, "CompatibleSystemsRouters" },
1027127675Sbms	{ 0x05BE, "CheyenneHierarchicalStorageManager" },
1028127675Sbms	{ 0x0606, "JCWatermarkImaging" },
1029127675Sbms	{ 0x060c, "AXISNetworkPrinter" },
1030127675Sbms	{ 0x0610, "AdaptecSCSIManagement" },
1031127675Sbms	{ 0x0621, "IBM AntiVirus" },
1032127675Sbms	{ 0x0640, "Windows95 RemoteRegistryService" },
1033127675Sbms	{ 0x064e, "MicrosoftIIS" },
1034127675Sbms	{ 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1035127675Sbms	{ 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1036127675Sbms	{ 0x076C, "Xerox" },
1037127675Sbms	{ 0x079b, "ShivaLanRover/E 115" },
1038127675Sbms	{ 0x079c, "ShivaLanRover/T 115" },
1039127675Sbms	{ 0x07B4, "CubixWorldDesk" },
1040127675Sbms	{ 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1041127675Sbms	{ 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1042127675Sbms	{ 0x0810, "ELAN License Server Demo" },
1043127675Sbms	{ 0x0824, "ShivaLanRoverAccessSwitch/E" },
1044127675Sbms	{ 0x086a, "ISSC Collector" },
1045127675Sbms	{ 0x087f, "ISSC DAS AgentAIX" },
1046127675Sbms	{ 0x0880, "Intel Netport PRO" },
1047127675Sbms	{ 0x0881, "Intel Netport PRO" },
1048127675Sbms	{ 0x0b29, "SiteLock" },
1049127675Sbms	{ 0x0c29, "SiteLockApplications" },
1050127675Sbms	{ 0x0c2c, "LicensingServer" },
1051127675Sbms	{ 0x2101, "PerformanceTechnologyInstantInternet" },
1052127675Sbms	{ 0x2380, "LAI SiteLock" },
1053127675Sbms	{ 0x238c, "MeetingMaker" },
1054127675Sbms	{ 0x4808, "SiteLockServer/SiteLockMetering" },
1055127675Sbms	{ 0x5555, "SiteLockUser" },
1056127675Sbms	{ 0x6312, "Tapeware" },
1057127675Sbms	{ 0x6f00, "RabbitGateway" },
1058127675Sbms	{ 0x7703, "MODEM" },
1059127675Sbms	{ 0x8002, "NetPortPrinters" },
1060127675Sbms	{ 0x8008, "WordPerfectNetworkVersion" },
1061127675Sbms	{ 0x85BE, "Cisco EIGRP" },
1062127675Sbms	{ 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1063127675Sbms	{ 0x9000, "McAfeeNetShield" },
1064127675Sbms	{ 0x9604, "CSA-NT_MON" },
1065127675Sbms	{ 0xb6a8, "OceanIsleReachoutRemoteControl" },
1066127675Sbms	{ 0xf11f, "SiteLockMetering" },
1067127675Sbms	{ 0xf1ff, "SiteLock" },
1068127675Sbms	{ 0xf503, "Microsoft SQL Server" },
1069127675Sbms	{ 0xF905, "IBM TimeAndPlace" },
1070127675Sbms	{ 0xfbfb, "TopCallIII FaxServer" },
1071127675Sbms	{ 0xffff, "AnyService/Wildcard" },
1072127675Sbms	{ 0, (char *)0 }
1073127675Sbms};
1074127675Sbms
1075127675Sbmsstatic void
1076127675Sbmsinit_ipxsaparray(void)
1077127675Sbms{
1078127675Sbms	register int i;
1079127675Sbms	register struct hnamemem *table;
1080127675Sbms
1081127675Sbms	for (i = 0; ipxsap_db[i].s != NULL; i++) {
1082127675Sbms		int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1083127675Sbms		table = &ipxsaptable[j];
1084127675Sbms		while (table->name)
1085127675Sbms			table = table->nxt;
1086127675Sbms		table->name = ipxsap_db[i].s;
1087127675Sbms		table->addr = htons(ipxsap_db[i].v);
1088127675Sbms		table->nxt = newhnamemem();
1089127675Sbms	}
1090127675Sbms}
1091127675Sbms
109217680Spst/*
109317680Spst * Initialize the address to name translation machinery.  We map all
109417680Spst * non-local IP addresses to numeric addresses if fflag is true (i.e.,
109517680Spst * to prevent blocking on the nameserver).  localnet is the IP address
109617680Spst * of the local network.  mask is its subnet mask.
109717680Spst */
109817680Spstvoid
109939300Sfennerinit_addrtoname(u_int32_t localnet, u_int32_t mask)
110017680Spst{
110117680Spst	if (fflag) {
110217680Spst		f_localnet = localnet;
110317680Spst		f_netmask = mask;
110417680Spst	}
110517680Spst	if (nflag)
110617680Spst		/*
110717680Spst		 * Simplest way to suppress names.
110817680Spst		 */
110917680Spst		return;
111017680Spst
111117680Spst	init_etherarray();
111217680Spst	init_servarray();
111317680Spst	init_eprotoarray();
111417680Spst	init_protoidarray();
1115127675Sbms	init_ipxsaparray();
111617680Spst}
111717680Spst
111898527Sfennerconst char *
111917680Spstdnaddr_string(u_short dnaddr)
112017680Spst{
112117680Spst	register struct hnamemem *tp;
112217680Spst
112317680Spst	for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
112417680Spst	     tp = tp->nxt)
112517680Spst		if (tp->addr == dnaddr)
112617680Spst			return (tp->name);
112717680Spst
112817680Spst	tp->addr = dnaddr;
112917680Spst	tp->nxt = newhnamemem();
113017680Spst	if (nflag)
113117680Spst		tp->name = dnnum_string(dnaddr);
113217680Spst	else
113317680Spst		tp->name = dnname_string(dnaddr);
113417680Spst
113517680Spst	return(tp->name);
113617680Spst}
113717680Spst
113817680Spst/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
113917680Spststruct hnamemem *
114026183Sfennernewhnamemem(void)
114117680Spst{
114217680Spst	register struct hnamemem *p;
114317680Spst	static struct hnamemem *ptr = NULL;
114417680Spst	static u_int num = 0;
114517680Spst
114617680Spst	if (num  <= 0) {
114717680Spst		num = 64;
114817680Spst		ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
114917680Spst		if (ptr == NULL)
115017680Spst			error("newhnamemem: calloc");
115117680Spst	}
115217680Spst	--num;
115317680Spst	p = ptr++;
115417680Spst	return (p);
115517680Spst}
115656896Sfenner
115756896Sfenner#ifdef INET6
115856896Sfenner/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
115956896Sfennerstruct h6namemem *
116056896Sfennernewh6namemem(void)
116156896Sfenner{
116256896Sfenner	register struct h6namemem *p;
116356896Sfenner	static struct h6namemem *ptr = NULL;
116456896Sfenner	static u_int num = 0;
116556896Sfenner
116656896Sfenner	if (num  <= 0) {
116756896Sfenner		num = 64;
116856896Sfenner		ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
116956896Sfenner		if (ptr == NULL)
117056896Sfenner			error("newh6namemem: calloc");
117156896Sfenner	}
117256896Sfenner	--num;
117356896Sfenner	p = ptr++;
117456896Sfenner	return (p);
117556896Sfenner}
117656896Sfenner#endif /* INET6 */
1177