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$
2517680Spst */
2617680Spst#ifndef lint
27127675Sbmsstatic const char rcsid[] _U_ =
28190207Srpaulo    "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.119 2007-08-08 14:06:34 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
73172686Smlaier#ifndef ETHER_ADDR_LEN
74172686Smlaier#define ETHER_ADDR_LEN	6
75172686Smlaier#endif
76172686Smlaier
7717680Spst/*
7817680Spst * hash tables for whatever-to-name translations
79127675Sbms *
80127675Sbms * XXX there has to be error checks against strdup(3) failure
8117680Spst */
8217680Spst
8317680Spst#define HASHNAMESIZE 4096
8417680Spst
8517680Spststruct hnamemem {
8617680Spst	u_int32_t addr;
8798527Sfenner	const char *name;
8817680Spst	struct hnamemem *nxt;
8917680Spst};
9017680Spst
91214478Srpaulostatic struct hnamemem hnametable[HASHNAMESIZE];
92214478Srpaulostatic struct hnamemem tporttable[HASHNAMESIZE];
93214478Srpaulostatic struct hnamemem uporttable[HASHNAMESIZE];
94214478Srpaulostatic struct hnamemem eprototable[HASHNAMESIZE];
95214478Srpaulostatic struct hnamemem dnaddrtable[HASHNAMESIZE];
96214478Srpaulostatic struct hnamemem ipxsaptable[HASHNAMESIZE];
9717680Spst
98127675Sbms#if defined(INET6) && defined(WIN32)
99127675Sbms/*
100127675Sbms * fake gethostbyaddr for Win2k/XP
101127675Sbms * gethostbyaddr() returns incorrect value when AF_INET6 is passed
102127675Sbms * to 3rd argument.
103127675Sbms *
104127675Sbms * h_name in struct hostent is only valid.
105127675Sbms */
106127675Sbmsstatic struct hostent *
107127675Sbmswin32_gethostbyaddr(const char *addr, int len, int type)
108127675Sbms{
109127675Sbms	static struct hostent host;
110127675Sbms	static char hostbuf[NI_MAXHOST];
111127675Sbms	char hname[NI_MAXHOST];
112127675Sbms	struct sockaddr_in6 addr6;
113127675Sbms
114127675Sbms	host.h_name = hostbuf;
115127675Sbms	switch (type) {
116127675Sbms	case AF_INET:
117127675Sbms		return gethostbyaddr(addr, len, type);
118127675Sbms		break;
119127675Sbms	case AF_INET6:
120127675Sbms		memset(&addr6, 0, sizeof(addr6));
121127675Sbms		addr6.sin6_family = AF_INET6;
122127675Sbms		memcpy(&addr6.sin6_addr, addr, len);
123127675Sbms		if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
124146778Ssam		    hname, sizeof(hname), NULL, 0, 0)) {
125146778Ssam			return NULL;
126127675Sbms		} else {
127127675Sbms			strcpy(host.h_name, hname);
128127675Sbms			return &host;
129127675Sbms		}
130127675Sbms		break;
131127675Sbms	default:
132127675Sbms		return NULL;
133127675Sbms	}
134127675Sbms}
135127675Sbms#define gethostbyaddr win32_gethostbyaddr
136146778Ssam#endif /* INET6 & WIN32 */
137127675Sbms
13856896Sfenner#ifdef INET6
13956896Sfennerstruct h6namemem {
14056896Sfenner	struct in6_addr addr;
14156896Sfenner	char *name;
14256896Sfenner	struct h6namemem *nxt;
14356896Sfenner};
14456896Sfenner
145214478Srpaulostatic struct h6namemem h6nametable[HASHNAMESIZE];
14656896Sfenner#endif /* INET6 */
14756896Sfenner
14817680Spststruct enamemem {
14917680Spst	u_short e_addr0;
15017680Spst	u_short e_addr1;
15117680Spst	u_short e_addr2;
15298527Sfenner	const char *e_name;
15317680Spst	u_char *e_nsap;			/* used only for nsaptable[] */
15498527Sfenner#define e_bs e_nsap			/* for bytestringtable */
15517680Spst	struct enamemem *e_nxt;
15617680Spst};
15717680Spst
158214478Srpaulostatic struct enamemem enametable[HASHNAMESIZE];
159214478Srpaulostatic struct enamemem nsaptable[HASHNAMESIZE];
160214478Srpaulostatic struct enamemem bytestringtable[HASHNAMESIZE];
16117680Spst
16217680Spststruct protoidmem {
16317680Spst	u_int32_t p_oui;
16417680Spst	u_short p_proto;
16598527Sfenner	const char *p_name;
16617680Spst	struct protoidmem *p_nxt;
16717680Spst};
16817680Spst
169214478Srpaulostatic struct protoidmem protoidtable[HASHNAMESIZE];
17017680Spst
17117680Spst/*
17217680Spst * A faster replacement for inet_ntoa().
17317680Spst */
17498527Sfennerconst char *
17517680Spstintoa(u_int32_t addr)
17617680Spst{
17717680Spst	register char *cp;
17817680Spst	register u_int byte;
17917680Spst	register int n;
18017680Spst	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
18117680Spst
18217680Spst	NTOHL(addr);
183147904Ssam	cp = buf + sizeof(buf);
18417680Spst	*--cp = '\0';
18517680Spst
18617680Spst	n = 4;
18717680Spst	do {
18817680Spst		byte = addr & 0xff;
18917680Spst		*--cp = byte % 10 + '0';
19017680Spst		byte /= 10;
19117680Spst		if (byte > 0) {
19217680Spst			*--cp = byte % 10 + '0';
19317680Spst			byte /= 10;
19417680Spst			if (byte > 0)
19517680Spst				*--cp = byte + '0';
19617680Spst		}
19717680Spst		*--cp = '.';
19817680Spst		addr >>= 8;
19917680Spst	} while (--n > 0);
20017680Spst
20117680Spst	return cp + 1;
20217680Spst}
20317680Spst
20417680Spststatic u_int32_t f_netmask;
20517680Spststatic u_int32_t f_localnet;
20617680Spst
20717680Spst/*
20817680Spst * Return a name for the IP address pointed to by ap.  This address
20917680Spst * is assumed to be in network byte order.
210127675Sbms *
211127675Sbms * NOTE: ap is *NOT* necessarily part of the packet data (not even if
212127675Sbms * this is being called with the "ipaddr_string()" macro), so you
213127675Sbms * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it.  Furthermore,
214127675Sbms * even in cases where it *is* part of the packet data, the caller
215127675Sbms * would still have to check for a null return value, even if it's
216127675Sbms * just printing the return value with "%s" - not all versions of
217127675Sbms * printf print "(null)" with "%s" and a null pointer, some of them
218127675Sbms * don't check for a null pointer and crash in that case.
219127675Sbms *
220127675Sbms * The callers of this routine should, before handing this routine
221127675Sbms * a pointer to packet data, be sure that the data is present in
222127675Sbms * the packet buffer.  They should probably do those checks anyway,
223127675Sbms * as other data at that layer might not be IP addresses, and it
224127675Sbms * also needs to check whether they're present in the packet buffer.
22517680Spst */
22698527Sfennerconst char *
22717680Spstgetname(const u_char *ap)
22817680Spst{
22917680Spst	register struct hostent *hp;
23017680Spst	u_int32_t addr;
23117680Spst	static struct hnamemem *p;		/* static for longjmp() */
23217680Spst
23356896Sfenner	memcpy(&addr, ap, sizeof(addr));
23417680Spst	p = &hnametable[addr & (HASHNAMESIZE-1)];
23517680Spst	for (; p->nxt; p = p->nxt) {
23617680Spst		if (p->addr == addr)
23717680Spst			return (p->name);
23817680Spst	}
23917680Spst	p->addr = addr;
24017680Spst	p->nxt = newhnamemem();
24117680Spst
24217680Spst	/*
243127675Sbms	 * Print names unless:
244127675Sbms	 *	(1) -n was given.
24539300Sfenner	 *      (2) Address is foreign and -f was given. (If -f was not
246127675Sbms	 *	    given, f_netmask and f_localnet are 0 and the test
24739300Sfenner	 *	    evaluates to true)
24817680Spst	 */
24939300Sfenner	if (!nflag &&
250127675Sbms	    (addr & f_netmask) == f_localnet) {
251127675Sbms		hp = gethostbyaddr((char *)&addr, 4, AF_INET);
252127675Sbms		if (hp) {
253127675Sbms			char *dotp;
25417680Spst
255127675Sbms			p->name = strdup(hp->h_name);
256127675Sbms			if (Nflag) {
257127675Sbms				/* Remove domain qualifications */
258127675Sbms				dotp = strchr(p->name, '.');
259127675Sbms				if (dotp)
260127675Sbms					*dotp = '\0';
26117680Spst			}
262127675Sbms			return (p->name);
26317680Spst		}
26417680Spst	}
26598527Sfenner	p->name = strdup(intoa(addr));
26617680Spst	return (p->name);
26717680Spst}
26817680Spst
26956896Sfenner#ifdef INET6
27056896Sfenner/*
27156896Sfenner * Return a name for the IP6 address pointed to by ap.  This address
27256896Sfenner * is assumed to be in network byte order.
27356896Sfenner */
27498527Sfennerconst char *
27556896Sfennergetname6(const u_char *ap)
27656896Sfenner{
27756896Sfenner	register struct hostent *hp;
27856896Sfenner	struct in6_addr addr;
27956896Sfenner	static struct h6namemem *p;		/* static for longjmp() */
28098527Sfenner	register const char *cp;
28156896Sfenner	char ntop_buf[INET6_ADDRSTRLEN];
28256896Sfenner
28356896Sfenner	memcpy(&addr, ap, sizeof(addr));
28456896Sfenner	p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
28556896Sfenner	for (; p->nxt; p = p->nxt) {
28656896Sfenner		if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
28756896Sfenner			return (p->name);
28856896Sfenner	}
28956896Sfenner	p->addr = addr;
29056896Sfenner	p->nxt = newh6namemem();
29156896Sfenner
29256896Sfenner	/*
293127675Sbms	 * Do not print names if -n was given.
29456896Sfenner	 */
295127675Sbms	if (!nflag) {
296127675Sbms		hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
297127675Sbms		if (hp) {
298127675Sbms			char *dotp;
29956896Sfenner
300127675Sbms			p->name = strdup(hp->h_name);
301127675Sbms			if (Nflag) {
302127675Sbms				/* Remove domain qualifications */
303127675Sbms				dotp = strchr(p->name, '.');
304127675Sbms				if (dotp)
305127675Sbms					*dotp = '\0';
30656896Sfenner			}
307127675Sbms			return (p->name);
30856896Sfenner		}
30956896Sfenner	}
31098527Sfenner	cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
31198527Sfenner	p->name = strdup(cp);
31256896Sfenner	return (p->name);
31356896Sfenner}
31456896Sfenner#endif /* INET6 */
31556896Sfenner
316214478Srpaulostatic const char hex[] = "0123456789abcdef";
31717680Spst
31817680Spst
31917680Spst/* Find the hash node that corresponds the ether address 'ep' */
32017680Spst
32117680Spststatic inline struct enamemem *
32217680Spstlookup_emem(const u_char *ep)
32317680Spst{
32417680Spst	register u_int i, j, k;
32517680Spst	struct enamemem *tp;
32617680Spst
32717680Spst	k = (ep[0] << 8) | ep[1];
32817680Spst	j = (ep[2] << 8) | ep[3];
32917680Spst	i = (ep[4] << 8) | ep[5];
33017680Spst
33117680Spst	tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
33217680Spst	while (tp->e_nxt)
33317680Spst		if (tp->e_addr0 == i &&
33417680Spst		    tp->e_addr1 == j &&
33517680Spst		    tp->e_addr2 == k)
33617680Spst			return tp;
33717680Spst		else
33817680Spst			tp = tp->e_nxt;
33917680Spst	tp->e_addr0 = i;
34017680Spst	tp->e_addr1 = j;
34117680Spst	tp->e_addr2 = k;
34217680Spst	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
34317680Spst	if (tp->e_nxt == NULL)
34417680Spst		error("lookup_emem: calloc");
34517680Spst
34617680Spst	return tp;
34717680Spst}
34817680Spst
34998527Sfenner/*
350127675Sbms * Find the hash node that corresponds to the bytestring 'bs'
35198527Sfenner * with length 'nlen'
35298527Sfenner */
35398527Sfenner
35498527Sfennerstatic inline struct enamemem *
35598527Sfennerlookup_bytestring(register const u_char *bs, const unsigned int nlen)
35698527Sfenner{
35798527Sfenner	struct enamemem *tp;
35898527Sfenner	register u_int i, j, k;
35998527Sfenner
36098527Sfenner	if (nlen >= 6) {
36198527Sfenner		k = (bs[0] << 8) | bs[1];
36298527Sfenner		j = (bs[2] << 8) | bs[3];
36398527Sfenner		i = (bs[4] << 8) | bs[5];
36498527Sfenner	} else if (nlen >= 4) {
36598527Sfenner		k = (bs[0] << 8) | bs[1];
36698527Sfenner		j = (bs[2] << 8) | bs[3];
36798527Sfenner		i = 0;
36898527Sfenner	} else
36998527Sfenner		i = j = k = 0;
37098527Sfenner
37198527Sfenner	tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
37298527Sfenner	while (tp->e_nxt)
37398527Sfenner		if (tp->e_addr0 == i &&
37498527Sfenner		    tp->e_addr1 == j &&
37598527Sfenner		    tp->e_addr2 == k &&
37698527Sfenner		    memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
37798527Sfenner			return tp;
37898527Sfenner		else
37998527Sfenner			tp = tp->e_nxt;
38098527Sfenner
38198527Sfenner	tp->e_addr0 = i;
38298527Sfenner	tp->e_addr1 = j;
38398527Sfenner	tp->e_addr2 = k;
38498527Sfenner
38598527Sfenner	tp->e_bs = (u_char *) calloc(1, nlen + 1);
386251158Sdelphij	if (tp->e_bs == NULL)
387251158Sdelphij		error("lookup_bytestring: calloc");
388251158Sdelphij
38998527Sfenner	memcpy(tp->e_bs, bs, nlen);
39098527Sfenner	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
39198527Sfenner	if (tp->e_nxt == NULL)
39298527Sfenner		error("lookup_bytestring: calloc");
39398527Sfenner
39498527Sfenner	return tp;
39598527Sfenner}
39698527Sfenner
39717680Spst/* Find the hash node that corresponds the NSAP 'nsap' */
39817680Spst
39917680Spststatic inline struct enamemem *
40017680Spstlookup_nsap(register const u_char *nsap)
40117680Spst{
40217680Spst	register u_int i, j, k;
40398527Sfenner	unsigned int nlen = *nsap;
40417680Spst	struct enamemem *tp;
40517680Spst	const u_char *ensap = nsap + nlen - 6;
40617680Spst
40717680Spst	if (nlen > 6) {
40817680Spst		k = (ensap[0] << 8) | ensap[1];
40917680Spst		j = (ensap[2] << 8) | ensap[3];
41017680Spst		i = (ensap[4] << 8) | ensap[5];
41117680Spst	}
41217680Spst	else
41317680Spst		i = j = k = 0;
41417680Spst
41517680Spst	tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
41617680Spst	while (tp->e_nxt)
41717680Spst		if (tp->e_addr0 == i &&
41817680Spst		    tp->e_addr1 == j &&
41917680Spst		    tp->e_addr2 == k &&
42017680Spst		    tp->e_nsap[0] == nlen &&
42198527Sfenner		    memcmp((const char *)&(nsap[1]),
42217680Spst			(char *)&(tp->e_nsap[1]), nlen) == 0)
42317680Spst			return tp;
42417680Spst		else
42517680Spst			tp = tp->e_nxt;
42617680Spst	tp->e_addr0 = i;
42717680Spst	tp->e_addr1 = j;
42817680Spst	tp->e_addr2 = k;
42917680Spst	tp->e_nsap = (u_char *)malloc(nlen + 1);
43017680Spst	if (tp->e_nsap == NULL)
43117680Spst		error("lookup_nsap: malloc");
43298527Sfenner	memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
43317680Spst	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
43417680Spst	if (tp->e_nxt == NULL)
43517680Spst		error("lookup_nsap: calloc");
43617680Spst
43717680Spst	return tp;
43817680Spst}
43917680Spst
44017680Spst/* Find the hash node that corresponds the protoid 'pi'. */
44117680Spst
44217680Spststatic inline struct protoidmem *
44317680Spstlookup_protoid(const u_char *pi)
44417680Spst{
44517680Spst	register u_int i, j;
44617680Spst	struct protoidmem *tp;
44717680Spst
44817680Spst	/* 5 octets won't be aligned */
44917680Spst	i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
45017680Spst	j =   (pi[3] << 8) + pi[4];
45117680Spst	/* XXX should be endian-insensitive, but do big-endian testing  XXX */
45217680Spst
45317680Spst	tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
45417680Spst	while (tp->p_nxt)
45517680Spst		if (tp->p_oui == i && tp->p_proto == j)
45617680Spst			return tp;
45717680Spst		else
45817680Spst			tp = tp->p_nxt;
45917680Spst	tp->p_oui = i;
46017680Spst	tp->p_proto = j;
46117680Spst	tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
46217680Spst	if (tp->p_nxt == NULL)
46317680Spst		error("lookup_protoid: calloc");
46417680Spst
46517680Spst	return tp;
46617680Spst}
46717680Spst
46898527Sfennerconst char *
46917680Spstetheraddr_string(register const u_char *ep)
47017680Spst{
471162021Ssam	register int i;
47217680Spst	register char *cp;
47317680Spst	register struct enamemem *tp;
474162021Ssam	int oui;
475147904Ssam	char buf[BUFSIZE];
47617680Spst
47717680Spst	tp = lookup_emem(ep);
47817680Spst	if (tp->e_name)
47917680Spst		return (tp->e_name);
48098527Sfenner#ifdef USE_ETHER_NTOHOST
48117680Spst	if (!nflag) {
482147904Ssam		char buf2[BUFSIZE];
483146778Ssam
484146778Ssam		/*
485146778Ssam		 * We don't cast it to "const struct ether_addr *"
486162021Ssam		 * because some systems fail to declare the second
487162021Ssam		 * argument as a "const" pointer, even though they
488162021Ssam		 * don't modify what it points to.
489146778Ssam		 */
490146778Ssam		if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
491127675Sbms			tp->e_name = strdup(buf2);
49217680Spst			return (tp->e_name);
49317680Spst		}
49417680Spst	}
49517680Spst#endif
49617680Spst	cp = buf;
497162021Ssam	oui = EXTRACT_24BITS(ep);
498146778Ssam	*cp++ = hex[*ep >> 4 ];
49917680Spst	*cp++ = hex[*ep++ & 0xf];
500162021Ssam	for (i = 5; --i >= 0;) {
501162021Ssam		*cp++ = ':';
502162021Ssam		*cp++ = hex[*ep >> 4 ];
503162021Ssam		*cp++ = hex[*ep++ & 0xf];
504162021Ssam	}
505147904Ssam
506162021Ssam	if (!nflag) {
507162021Ssam		snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
508162021Ssam		    tok2str(oui_values, "Unknown", oui));
509162021Ssam	} else
510162021Ssam		*cp = '\0';
51198527Sfenner	tp->e_name = strdup(buf);
51217680Spst	return (tp->e_name);
51317680Spst}
51417680Spst
51598527Sfennerconst char *
516235530Sdelphijle64addr_string(const u_char *ep)
517235530Sdelphij{
518235530Sdelphij	const unsigned int len = 8;
519235530Sdelphij	register u_int i;
520235530Sdelphij	register char *cp;
521235530Sdelphij	register struct enamemem *tp;
522235530Sdelphij	char buf[BUFSIZE];
523235530Sdelphij
524235530Sdelphij	tp = lookup_bytestring(ep, len);
525235530Sdelphij	if (tp->e_name)
526235530Sdelphij		return (tp->e_name);
527235530Sdelphij
528235530Sdelphij	cp = buf;
529235530Sdelphij	for (i = len; i > 0 ; --i) {
530235530Sdelphij		*cp++ = hex[*(ep + i - 1) >> 4];
531235530Sdelphij		*cp++ = hex[*(ep + i - 1) & 0xf];
532235530Sdelphij		*cp++ = ':';
533235530Sdelphij	}
534235530Sdelphij	cp --;
535235530Sdelphij
536235530Sdelphij	*cp = '\0';
537235530Sdelphij
538235530Sdelphij	tp->e_name = strdup(buf);
539235530Sdelphij
540235530Sdelphij	return (tp->e_name);
541235530Sdelphij}
542235530Sdelphij
543235530Sdelphijconst char *
544190207Srpaulolinkaddr_string(const u_char *ep, const unsigned int type, const unsigned int len)
54598527Sfenner{
546146778Ssam	register u_int i;
54798527Sfenner	register char *cp;
54898527Sfenner	register struct enamemem *tp;
54998527Sfenner
550214478Srpaulo	if (len == 0)
551214478Srpaulo		return ("<empty>");
552127675Sbms
553214478Srpaulo	if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
554214478Srpaulo		return (etheraddr_string(ep));
555190207Srpaulo
556214478Srpaulo	if (type == LINKADDR_FRELAY)
557214478Srpaulo		return (q922_string(ep));
558214478Srpaulo
55998527Sfenner	tp = lookup_bytestring(ep, len);
56098527Sfenner	if (tp->e_name)
56198527Sfenner		return (tp->e_name);
56298527Sfenner
56398527Sfenner	tp->e_name = cp = (char *)malloc(len*3);
56498527Sfenner	if (tp->e_name == NULL)
56598527Sfenner		error("linkaddr_string: malloc");
566146778Ssam	*cp++ = hex[*ep >> 4];
56798527Sfenner	*cp++ = hex[*ep++ & 0xf];
56898527Sfenner	for (i = len-1; i > 0 ; --i) {
56998527Sfenner		*cp++ = ':';
570146778Ssam		*cp++ = hex[*ep >> 4];
57198527Sfenner		*cp++ = hex[*ep++ & 0xf];
57298527Sfenner	}
57398527Sfenner	*cp = '\0';
57498527Sfenner	return (tp->e_name);
57598527Sfenner}
57698527Sfenner
57798527Sfennerconst char *
57817680Spstetherproto_string(u_short port)
57917680Spst{
58017680Spst	register char *cp;
58117680Spst	register struct hnamemem *tp;
58217680Spst	register u_int32_t i = port;
58317680Spst	char buf[sizeof("0000")];
58417680Spst
58517680Spst	for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
58617680Spst		if (tp->addr == i)
58717680Spst			return (tp->name);
58817680Spst
58917680Spst	tp->addr = i;
59017680Spst	tp->nxt = newhnamemem();
59117680Spst
59217680Spst	cp = buf;
59317680Spst	NTOHS(port);
59417680Spst	*cp++ = hex[port >> 12 & 0xf];
59517680Spst	*cp++ = hex[port >> 8 & 0xf];
59617680Spst	*cp++ = hex[port >> 4 & 0xf];
59717680Spst	*cp++ = hex[port & 0xf];
59817680Spst	*cp++ = '\0';
59998527Sfenner	tp->name = strdup(buf);
60017680Spst	return (tp->name);
60117680Spst}
60217680Spst
60398527Sfennerconst char *
60417680Spstprotoid_string(register const u_char *pi)
60517680Spst{
60617680Spst	register u_int i, j;
60717680Spst	register char *cp;
60817680Spst	register struct protoidmem *tp;
60917680Spst	char buf[sizeof("00:00:00:00:00")];
61017680Spst
61117680Spst	tp = lookup_protoid(pi);
61217680Spst	if (tp->p_name)
61317680Spst		return tp->p_name;
61417680Spst
61517680Spst	cp = buf;
61617680Spst	if ((j = *pi >> 4) != 0)
61717680Spst		*cp++ = hex[j];
61817680Spst	*cp++ = hex[*pi++ & 0xf];
61917680Spst	for (i = 4; (int)--i >= 0;) {
62017680Spst		*cp++ = ':';
62117680Spst		if ((j = *pi >> 4) != 0)
62217680Spst			*cp++ = hex[j];
62317680Spst		*cp++ = hex[*pi++ & 0xf];
62417680Spst	}
62517680Spst	*cp = '\0';
62698527Sfenner	tp->p_name = strdup(buf);
62717680Spst	return (tp->p_name);
62817680Spst}
62917680Spst
630146778Ssam#define ISONSAP_MAX_LENGTH 20
63198527Sfennerconst char *
632146778Ssamisonsap_string(const u_char *nsap, register u_int nsap_length)
63317680Spst{
634146778Ssam	register u_int nsap_idx;
63517680Spst	register char *cp;
63617680Spst	register struct enamemem *tp;
63717680Spst
638146778Ssam	if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
639147904Ssam		return ("isonsap_string: illegal length");
640146778Ssam
64117680Spst	tp = lookup_nsap(nsap);
64217680Spst	if (tp->e_name)
64317680Spst		return tp->e_name;
64417680Spst
645146778Ssam	tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
64617680Spst	if (cp == NULL)
64717680Spst		error("isonsap_string: malloc");
64817680Spst
649146778Ssam	for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
65017680Spst		*cp++ = hex[*nsap >> 4];
65117680Spst		*cp++ = hex[*nsap++ & 0xf];
652146778Ssam		if (((nsap_idx & 1) == 0) &&
653146778Ssam		     (nsap_idx + 1 < nsap_length)) {
654146778Ssam		     	*cp++ = '.';
655146778Ssam		}
65617680Spst	}
65717680Spst	*cp = '\0';
65817680Spst	return (tp->e_name);
65917680Spst}
66017680Spst
66198527Sfennerconst char *
66217680Spsttcpport_string(u_short port)
66317680Spst{
66417680Spst	register struct hnamemem *tp;
66517680Spst	register u_int32_t i = port;
66617680Spst	char buf[sizeof("00000")];
66717680Spst
66817680Spst	for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
66917680Spst		if (tp->addr == i)
67017680Spst			return (tp->name);
67117680Spst
67217680Spst	tp->addr = i;
67317680Spst	tp->nxt = newhnamemem();
67417680Spst
67566644Skris	(void)snprintf(buf, sizeof(buf), "%u", i);
67698527Sfenner	tp->name = strdup(buf);
67717680Spst	return (tp->name);
67817680Spst}
67917680Spst
68098527Sfennerconst char *
68117680Spstudpport_string(register u_short port)
68217680Spst{
68317680Spst	register struct hnamemem *tp;
68417680Spst	register u_int32_t i = port;
68517680Spst	char buf[sizeof("00000")];
68617680Spst
68717680Spst	for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
68817680Spst		if (tp->addr == i)
68917680Spst			return (tp->name);
69017680Spst
69117680Spst	tp->addr = i;
69217680Spst	tp->nxt = newhnamemem();
69317680Spst
69466644Skris	(void)snprintf(buf, sizeof(buf), "%u", i);
69598527Sfenner	tp->name = strdup(buf);
69617680Spst	return (tp->name);
69717680Spst}
69817680Spst
699127675Sbmsconst char *
700127675Sbmsipxsap_string(u_short port)
701127675Sbms{
702127675Sbms	register char *cp;
703127675Sbms	register struct hnamemem *tp;
704127675Sbms	register u_int32_t i = port;
705127675Sbms	char buf[sizeof("0000")];
706127675Sbms
707127675Sbms	for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
708127675Sbms		if (tp->addr == i)
709127675Sbms			return (tp->name);
710127675Sbms
711127675Sbms	tp->addr = i;
712127675Sbms	tp->nxt = newhnamemem();
713127675Sbms
714127675Sbms	cp = buf;
715127675Sbms	NTOHS(port);
716127675Sbms	*cp++ = hex[port >> 12 & 0xf];
717127675Sbms	*cp++ = hex[port >> 8 & 0xf];
718127675Sbms	*cp++ = hex[port >> 4 & 0xf];
719127675Sbms	*cp++ = hex[port & 0xf];
720127675Sbms	*cp++ = '\0';
721127675Sbms	tp->name = strdup(buf);
722127675Sbms	return (tp->name);
723127675Sbms}
724127675Sbms
72517680Spststatic void
72617680Spstinit_servarray(void)
72717680Spst{
72817680Spst	struct servent *sv;
72917680Spst	register struct hnamemem *table;
73017680Spst	register int i;
73117680Spst	char buf[sizeof("0000000000")];
73217680Spst
73317680Spst	while ((sv = getservent()) != NULL) {
73417680Spst		int port = ntohs(sv->s_port);
73517680Spst		i = port & (HASHNAMESIZE-1);
73617680Spst		if (strcmp(sv->s_proto, "tcp") == 0)
73717680Spst			table = &tporttable[i];
73817680Spst		else if (strcmp(sv->s_proto, "udp") == 0)
73917680Spst			table = &uporttable[i];
74017680Spst		else
74117680Spst			continue;
74217680Spst
74317680Spst		while (table->name)
74417680Spst			table = table->nxt;
74517680Spst		if (nflag) {
74666644Skris			(void)snprintf(buf, sizeof(buf), "%d", port);
74798527Sfenner			table->name = strdup(buf);
74817680Spst		} else
74998527Sfenner			table->name = strdup(sv->s_name);
75017680Spst		table->addr = port;
75117680Spst		table->nxt = newhnamemem();
75217680Spst	}
75317680Spst	endservent();
75417680Spst}
75517680Spst
756146778Ssam/* in libpcap.a (nametoaddr.c) */
757146778Ssam#if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
758146778Ssam__declspec(dllimport)
759127675Sbms#else
760146778Ssamextern
761127675Sbms#endif
762146778Ssamconst struct eproto {
763146778Ssam	const char *s;
76417680Spst	u_short p;
76517680Spst} eproto_db[];
76617680Spst
76717680Spststatic void
76817680Spstinit_eprotoarray(void)
76917680Spst{
77017680Spst	register int i;
77117680Spst	register struct hnamemem *table;
77217680Spst
77317680Spst	for (i = 0; eproto_db[i].s; i++) {
774127675Sbms		int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
77517680Spst		table = &eprototable[j];
77617680Spst		while (table->name)
77717680Spst			table = table->nxt;
77817680Spst		table->name = eproto_db[i].s;
779127675Sbms		table->addr = htons(eproto_db[i].p);
78017680Spst		table->nxt = newhnamemem();
78117680Spst	}
78217680Spst}
78317680Spst
784214478Srpaulostatic const struct protoidlist {
78598527Sfenner	const u_char protoid[5];
78698527Sfenner	const char *name;
78798527Sfenner} protoidlist[] = {
78898527Sfenner	{{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
78998527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
79098527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
79198527Sfenner	{{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
79298527Sfenner	{{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
79398527Sfenner	{{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
79498527Sfenner};
79598527Sfenner
79617680Spst/*
79717680Spst * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
79817680Spst * types.
79917680Spst */
80017680Spststatic void
80117680Spstinit_protoidarray(void)
80217680Spst{
80317680Spst	register int i;
80417680Spst	register struct protoidmem *tp;
805214478Srpaulo	const struct protoidlist *pl;
80617680Spst	u_char protoid[5];
80717680Spst
80817680Spst	protoid[0] = 0;
80917680Spst	protoid[1] = 0;
81017680Spst	protoid[2] = 0;
81117680Spst	for (i = 0; eproto_db[i].s; i++) {
81217680Spst		u_short etype = htons(eproto_db[i].p);
81317680Spst
81417680Spst		memcpy((char *)&protoid[3], (char *)&etype, 2);
81517680Spst		tp = lookup_protoid(protoid);
81698527Sfenner		tp->p_name = strdup(eproto_db[i].s);
81717680Spst	}
81898527Sfenner	/* Hardwire some SNAP proto ID names */
81998527Sfenner	for (pl = protoidlist; pl->name != NULL; ++pl) {
82098527Sfenner		tp = lookup_protoid(pl->protoid);
82198527Sfenner		/* Don't override existing name */
82298527Sfenner		if (tp->p_name != NULL)
82398527Sfenner			continue;
82498527Sfenner
82598527Sfenner		tp->p_name = pl->name;
82698527Sfenner	}
82717680Spst}
82817680Spst
829214478Srpaulostatic const struct etherlist {
83098527Sfenner	const u_char addr[6];
83198527Sfenner	const char *name;
83217680Spst} etherlist[] = {
83317680Spst	{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
83417680Spst	{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
83517680Spst};
83617680Spst
83717680Spst/*
83817680Spst * Initialize the ethers hash table.  We take two different approaches
83917680Spst * depending on whether or not the system provides the ethers name
84017680Spst * service.  If it does, we just wire in a few names at startup,
84117680Spst * and etheraddr_string() fills in the table on demand.  If it doesn't,
84217680Spst * then we suck in the entire /etc/ethers file at startup.  The idea
84317680Spst * is that parsing the local file will be fast, but spinning through
84417680Spst * all the ethers entries via NIS & next_etherent might be very slow.
84517680Spst *
84617680Spst * XXX pcap_next_etherent doesn't belong in the pcap interface, but
84717680Spst * since the pcap module already does name-to-address translation,
84817680Spst * it's already does most of the work for the ethernet address-to-name
84917680Spst * translation, so we just pcap_next_etherent as a convenience.
85017680Spst */
85117680Spststatic void
85217680Spstinit_etherarray(void)
85317680Spst{
854214478Srpaulo	register const struct etherlist *el;
85517680Spst	register struct enamemem *tp;
85698527Sfenner#ifdef USE_ETHER_NTOHOST
85717680Spst	char name[256];
85817680Spst#else
85917680Spst	register struct pcap_etherent *ep;
86017680Spst	register FILE *fp;
86117680Spst
86217680Spst	/* Suck in entire ethers file */
86317680Spst	fp = fopen(PCAP_ETHERS_FILE, "r");
86417680Spst	if (fp != NULL) {
86517680Spst		while ((ep = pcap_next_etherent(fp)) != NULL) {
86617680Spst			tp = lookup_emem(ep->addr);
86798527Sfenner			tp->e_name = strdup(ep->name);
86817680Spst		}
86917680Spst		(void)fclose(fp);
87017680Spst	}
87117680Spst#endif
87217680Spst
87317680Spst	/* Hardwire some ethernet names */
87417680Spst	for (el = etherlist; el->name != NULL; ++el) {
87517680Spst		tp = lookup_emem(el->addr);
87617680Spst		/* Don't override existing name */
87717680Spst		if (tp->e_name != NULL)
87817680Spst			continue;
87917680Spst
88098527Sfenner#ifdef USE_ETHER_NTOHOST
881146778Ssam		/*
882146778Ssam		 * Use YP/NIS version of name if available.
883146778Ssam		 *
884146778Ssam		 * We don't cast it to "const struct ether_addr *"
885146778Ssam		 * because some systems don't modify the Ethernet
886146778Ssam		 * address but fail to declare the second argument
887146778Ssam		 * as a "const" pointer.
888146778Ssam		 */
889146778Ssam		if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
890146778Ssam			tp->e_name = strdup(name);
89117680Spst			continue;
89217680Spst		}
89317680Spst#endif
89417680Spst		tp->e_name = el->name;
89517680Spst	}
89617680Spst}
89717680Spst
898214478Srpaulostatic const struct tok ipxsap_db[] = {
899127675Sbms	{ 0x0000, "Unknown" },
900127675Sbms	{ 0x0001, "User" },
901127675Sbms	{ 0x0002, "User Group" },
902127675Sbms	{ 0x0003, "PrintQueue" },
903127675Sbms	{ 0x0004, "FileServer" },
904127675Sbms	{ 0x0005, "JobServer" },
905127675Sbms	{ 0x0006, "Gateway" },
906127675Sbms	{ 0x0007, "PrintServer" },
907127675Sbms	{ 0x0008, "ArchiveQueue" },
908127675Sbms	{ 0x0009, "ArchiveServer" },
909127675Sbms	{ 0x000a, "JobQueue" },
910127675Sbms	{ 0x000b, "Administration" },
911127675Sbms	{ 0x000F, "Novell TI-RPC" },
912127675Sbms	{ 0x0017, "Diagnostics" },
913127675Sbms	{ 0x0020, "NetBIOS" },
914127675Sbms	{ 0x0021, "NAS SNA Gateway" },
915127675Sbms	{ 0x0023, "NACS AsyncGateway" },
916127675Sbms	{ 0x0024, "RemoteBridge/RoutingService" },
917127675Sbms	{ 0x0026, "BridgeServer" },
918127675Sbms	{ 0x0027, "TCP/IP Gateway" },
919127675Sbms	{ 0x0028, "Point-to-point X.25 BridgeServer" },
920127675Sbms	{ 0x0029, "3270 Gateway" },
921127675Sbms	{ 0x002a, "CHI Corp" },
922127675Sbms	{ 0x002c, "PC Chalkboard" },
923127675Sbms	{ 0x002d, "TimeSynchServer" },
924127675Sbms	{ 0x002e, "ARCserve5.0/PalindromeBackup" },
925127675Sbms	{ 0x0045, "DI3270 Gateway" },
926127675Sbms	{ 0x0047, "AdvertisingPrintServer" },
927127675Sbms	{ 0x004a, "NetBlazerModems" },
928127675Sbms	{ 0x004b, "BtrieveVAP" },
929127675Sbms	{ 0x004c, "NetwareSQL" },
930127675Sbms	{ 0x004d, "XtreeNetwork" },
931127675Sbms	{ 0x0050, "BtrieveVAP4.11" },
932127675Sbms	{ 0x0052, "QuickLink" },
933127675Sbms	{ 0x0053, "PrintQueueUser" },
934127675Sbms	{ 0x0058, "Multipoint X.25 Router" },
935127675Sbms	{ 0x0060, "STLB/NLM" },
936127675Sbms	{ 0x0064, "ARCserve" },
937127675Sbms	{ 0x0066, "ARCserve3.0" },
938127675Sbms	{ 0x0072, "WAN CopyUtility" },
939127675Sbms	{ 0x007a, "TES-NetwareVMS" },
940127675Sbms	{ 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
941127675Sbms	{ 0x0095, "DDA OBGYN" },
942127675Sbms	{ 0x0098, "NetwareAccessServer" },
943127675Sbms	{ 0x009a, "Netware for VMS II/NamedPipeServer" },
944127675Sbms	{ 0x009b, "NetwareAccessServer" },
945127675Sbms	{ 0x009e, "PortableNetwareServer/SunLinkNVT" },
946127675Sbms	{ 0x00a1, "PowerchuteAPC UPS" },
947127675Sbms	{ 0x00aa, "LAWserve" },
948127675Sbms	{ 0x00ac, "CompaqIDA StatusMonitor" },
949127675Sbms	{ 0x0100, "PIPE STAIL" },
950127675Sbms	{ 0x0102, "LAN ProtectBindery" },
951127675Sbms	{ 0x0103, "OracleDataBaseServer" },
952127675Sbms	{ 0x0107, "Netware386/RSPX RemoteConsole" },
953127675Sbms	{ 0x010f, "NovellSNA Gateway" },
954127675Sbms	{ 0x0111, "TestServer" },
955127675Sbms	{ 0x0112, "HP PrintServer" },
956127675Sbms	{ 0x0114, "CSA MUX" },
957127675Sbms	{ 0x0115, "CSA LCA" },
958127675Sbms	{ 0x0116, "CSA CM" },
959127675Sbms	{ 0x0117, "CSA SMA" },
960127675Sbms	{ 0x0118, "CSA DBA" },
961127675Sbms	{ 0x0119, "CSA NMA" },
962127675Sbms	{ 0x011a, "CSA SSA" },
963127675Sbms	{ 0x011b, "CSA STATUS" },
964127675Sbms	{ 0x011e, "CSA APPC" },
965127675Sbms	{ 0x0126, "SNA TEST SSA Profile" },
966127675Sbms	{ 0x012a, "CSA TRACE" },
967127675Sbms	{ 0x012b, "NetwareSAA" },
968127675Sbms	{ 0x012e, "IKARUS VirusScan" },
969127675Sbms	{ 0x0130, "CommunicationsExecutive" },
970127675Sbms	{ 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
971127675Sbms	{ 0x0135, "NetwareNamingServicesProfile" },
972127675Sbms	{ 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
973127675Sbms	{ 0x0141, "LAN SpoolServer" },
974127675Sbms	{ 0x0152, "IRMALAN Gateway" },
975127675Sbms	{ 0x0154, "NamedPipeServer" },
976127675Sbms	{ 0x0166, "NetWareManagement" },
977127675Sbms	{ 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
978127675Sbms	{ 0x0173, "Compaq" },
979127675Sbms	{ 0x0174, "Compaq SNMP Agent" },
980127675Sbms	{ 0x0175, "Compaq" },
981127675Sbms	{ 0x0180, "XTreeServer/XTreeTools" },
982127675Sbms	{ 0x018A, "NASI ServicesBroadcastServer" },
983127675Sbms	{ 0x01b0, "GARP Gateway" },
984127675Sbms	{ 0x01b1, "Binfview" },
985127675Sbms	{ 0x01bf, "IntelLanDeskManager" },
986127675Sbms	{ 0x01ca, "AXTEC" },
987127675Sbms	{ 0x01cb, "ShivaNetModem/E" },
988127675Sbms	{ 0x01cc, "ShivaLanRover/E" },
989127675Sbms	{ 0x01cd, "ShivaLanRover/T" },
990127675Sbms	{ 0x01ce, "ShivaUniversal" },
991127675Sbms	{ 0x01d8, "CastelleFAXPressServer" },
992127675Sbms	{ 0x01da, "CastelleLANPressPrintServer" },
993127675Sbms	{ 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
994127675Sbms	{ 0x01f0, "LEGATO" },
995127675Sbms	{ 0x01f5, "LEGATO" },
996127675Sbms	{ 0x0233, "NMS Agent/NetwareManagementAgent" },
997127675Sbms	{ 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
998127675Sbms	{ 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
999127675Sbms	{ 0x023a, "LANtern" },
1000127675Sbms	{ 0x023c, "MAVERICK" },
1001127675Sbms	{ 0x023f, "NovellSMDR" },
1002127675Sbms	{ 0x024e, "NetwareConnect" },
1003127675Sbms	{ 0x024f, "NASI ServerBroadcast Cisco" },
1004127675Sbms	{ 0x026a, "NMS ServiceConsole" },
1005127675Sbms	{ 0x026b, "TimeSynchronizationServer Netware 4.x" },
1006127675Sbms	{ 0x0278, "DirectoryServer Netware 4.x" },
1007127675Sbms	{ 0x027b, "NetwareManagementAgent" },
1008127675Sbms	{ 0x0280, "Novell File and Printer Sharing Service for PC" },
1009127675Sbms	{ 0x0304, "NovellSAA Gateway" },
1010127675Sbms	{ 0x0308, "COM/VERMED" },
1011127675Sbms	{ 0x030a, "GalacticommWorldgroupServer" },
1012127675Sbms	{ 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1013127675Sbms	{ 0x0320, "AttachmateGateway" },
1014127675Sbms	{ 0x0327, "MicrosoftDiagnostiocs" },
1015127675Sbms	{ 0x0328, "WATCOM SQL Server" },
1016127675Sbms	{ 0x0335, "MultiTechSystems MultisynchCommServer" },
1017127675Sbms	{ 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1018127675Sbms	{ 0x0355, "ArcadaBackupExec" },
1019127675Sbms	{ 0x0358, "MSLCD1" },
1020127675Sbms	{ 0x0361, "NETINELO" },
1021127675Sbms	{ 0x037e, "Powerchute UPS Monitoring" },
1022127675Sbms	{ 0x037f, "ViruSafeNotify" },
1023127675Sbms	{ 0x0386, "HP Bridge" },
1024127675Sbms	{ 0x0387, "HP Hub" },
1025127675Sbms	{ 0x0394, "NetWare SAA Gateway" },
1026127675Sbms	{ 0x039b, "LotusNotes" },
1027127675Sbms	{ 0x03b7, "CertusAntiVirus" },
1028127675Sbms	{ 0x03c4, "ARCserve4.0" },
1029127675Sbms	{ 0x03c7, "LANspool3.5" },
1030127675Sbms	{ 0x03d7, "LexmarkPrinterServer" },
1031127675Sbms	{ 0x03d8, "LexmarkXLE PrinterServer" },
1032127675Sbms	{ 0x03dd, "BanyanENS NetwareClient" },
1033127675Sbms	{ 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1034127675Sbms	{ 0x03e1, "UnivelUnixware" },
1035127675Sbms	{ 0x03e4, "UnivelUnixware" },
1036127675Sbms	{ 0x03fc, "IntelNetport" },
1037127675Sbms	{ 0x03fd, "PrintServerQueue" },
1038127675Sbms	{ 0x040A, "ipnServer" },
1039127675Sbms	{ 0x040D, "LVERRMAN" },
1040127675Sbms	{ 0x040E, "LVLIC" },
1041127675Sbms	{ 0x0414, "NET Silicon (DPI)/Kyocera" },
1042127675Sbms	{ 0x0429, "SiteLockVirus" },
1043127675Sbms	{ 0x0432, "UFHELPR???" },
1044127675Sbms	{ 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1045127675Sbms	{ 0x0444, "MicrosoftNT SNA Server" },
1046127675Sbms	{ 0x0448, "Oracle" },
1047127675Sbms	{ 0x044c, "ARCserve5.01" },
1048127675Sbms	{ 0x0457, "CanonGP55" },
1049127675Sbms	{ 0x045a, "QMS Printers" },
1050127675Sbms	{ 0x045b, "DellSCSI Array" },
1051127675Sbms	{ 0x0491, "NetBlazerModems" },
1052127675Sbms	{ 0x04ac, "OnTimeScheduler" },
1053127675Sbms	{ 0x04b0, "CD-Net" },
1054127675Sbms	{ 0x0513, "EmulexNQA" },
1055127675Sbms	{ 0x0520, "SiteLockChecks" },
1056127675Sbms	{ 0x0529, "SiteLockChecks" },
1057127675Sbms	{ 0x052d, "CitrixOS2 AppServer" },
1058127675Sbms	{ 0x0535, "Tektronix" },
1059127675Sbms	{ 0x0536, "Milan" },
1060127675Sbms	{ 0x055d, "Attachmate SNA gateway" },
1061127675Sbms	{ 0x056b, "IBM8235 ModemServer" },
1062127675Sbms	{ 0x056c, "ShivaLanRover/E PLUS" },
1063127675Sbms	{ 0x056d, "ShivaLanRover/T PLUS" },
1064127675Sbms	{ 0x0580, "McAfeeNetShield" },
1065127675Sbms	{ 0x05B8, "NLM to workstation communication (Revelation Software)" },
1066127675Sbms	{ 0x05BA, "CompatibleSystemsRouters" },
1067127675Sbms	{ 0x05BE, "CheyenneHierarchicalStorageManager" },
1068127675Sbms	{ 0x0606, "JCWatermarkImaging" },
1069127675Sbms	{ 0x060c, "AXISNetworkPrinter" },
1070127675Sbms	{ 0x0610, "AdaptecSCSIManagement" },
1071127675Sbms	{ 0x0621, "IBM AntiVirus" },
1072127675Sbms	{ 0x0640, "Windows95 RemoteRegistryService" },
1073127675Sbms	{ 0x064e, "MicrosoftIIS" },
1074127675Sbms	{ 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1075127675Sbms	{ 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1076127675Sbms	{ 0x076C, "Xerox" },
1077127675Sbms	{ 0x079b, "ShivaLanRover/E 115" },
1078127675Sbms	{ 0x079c, "ShivaLanRover/T 115" },
1079127675Sbms	{ 0x07B4, "CubixWorldDesk" },
1080127675Sbms	{ 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1081127675Sbms	{ 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1082127675Sbms	{ 0x0810, "ELAN License Server Demo" },
1083127675Sbms	{ 0x0824, "ShivaLanRoverAccessSwitch/E" },
1084127675Sbms	{ 0x086a, "ISSC Collector" },
1085127675Sbms	{ 0x087f, "ISSC DAS AgentAIX" },
1086127675Sbms	{ 0x0880, "Intel Netport PRO" },
1087127675Sbms	{ 0x0881, "Intel Netport PRO" },
1088127675Sbms	{ 0x0b29, "SiteLock" },
1089127675Sbms	{ 0x0c29, "SiteLockApplications" },
1090127675Sbms	{ 0x0c2c, "LicensingServer" },
1091127675Sbms	{ 0x2101, "PerformanceTechnologyInstantInternet" },
1092127675Sbms	{ 0x2380, "LAI SiteLock" },
1093127675Sbms	{ 0x238c, "MeetingMaker" },
1094127675Sbms	{ 0x4808, "SiteLockServer/SiteLockMetering" },
1095127675Sbms	{ 0x5555, "SiteLockUser" },
1096127675Sbms	{ 0x6312, "Tapeware" },
1097127675Sbms	{ 0x6f00, "RabbitGateway" },
1098127675Sbms	{ 0x7703, "MODEM" },
1099127675Sbms	{ 0x8002, "NetPortPrinters" },
1100127675Sbms	{ 0x8008, "WordPerfectNetworkVersion" },
1101127675Sbms	{ 0x85BE, "Cisco EIGRP" },
1102127675Sbms	{ 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1103127675Sbms	{ 0x9000, "McAfeeNetShield" },
1104127675Sbms	{ 0x9604, "CSA-NT_MON" },
1105127675Sbms	{ 0xb6a8, "OceanIsleReachoutRemoteControl" },
1106127675Sbms	{ 0xf11f, "SiteLockMetering" },
1107127675Sbms	{ 0xf1ff, "SiteLock" },
1108127675Sbms	{ 0xf503, "Microsoft SQL Server" },
1109127675Sbms	{ 0xF905, "IBM TimeAndPlace" },
1110127675Sbms	{ 0xfbfb, "TopCallIII FaxServer" },
1111127675Sbms	{ 0xffff, "AnyService/Wildcard" },
1112127675Sbms	{ 0, (char *)0 }
1113127675Sbms};
1114127675Sbms
1115127675Sbmsstatic void
1116127675Sbmsinit_ipxsaparray(void)
1117127675Sbms{
1118127675Sbms	register int i;
1119127675Sbms	register struct hnamemem *table;
1120127675Sbms
1121127675Sbms	for (i = 0; ipxsap_db[i].s != NULL; i++) {
1122127675Sbms		int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1123127675Sbms		table = &ipxsaptable[j];
1124127675Sbms		while (table->name)
1125127675Sbms			table = table->nxt;
1126127675Sbms		table->name = ipxsap_db[i].s;
1127127675Sbms		table->addr = htons(ipxsap_db[i].v);
1128127675Sbms		table->nxt = newhnamemem();
1129127675Sbms	}
1130127675Sbms}
1131127675Sbms
113217680Spst/*
113317680Spst * Initialize the address to name translation machinery.  We map all
113417680Spst * non-local IP addresses to numeric addresses if fflag is true (i.e.,
113517680Spst * to prevent blocking on the nameserver).  localnet is the IP address
113617680Spst * of the local network.  mask is its subnet mask.
113717680Spst */
113817680Spstvoid
113939300Sfennerinit_addrtoname(u_int32_t localnet, u_int32_t mask)
114017680Spst{
114117680Spst	if (fflag) {
114217680Spst		f_localnet = localnet;
114317680Spst		f_netmask = mask;
114417680Spst	}
114517680Spst	if (nflag)
114617680Spst		/*
114717680Spst		 * Simplest way to suppress names.
114817680Spst		 */
114917680Spst		return;
115017680Spst
115117680Spst	init_etherarray();
115217680Spst	init_servarray();
115317680Spst	init_eprotoarray();
115417680Spst	init_protoidarray();
1155127675Sbms	init_ipxsaparray();
115617680Spst}
115717680Spst
115898527Sfennerconst char *
115917680Spstdnaddr_string(u_short dnaddr)
116017680Spst{
116117680Spst	register struct hnamemem *tp;
116217680Spst
116317680Spst	for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
116417680Spst	     tp = tp->nxt)
116517680Spst		if (tp->addr == dnaddr)
116617680Spst			return (tp->name);
116717680Spst
116817680Spst	tp->addr = dnaddr;
116917680Spst	tp->nxt = newhnamemem();
117017680Spst	if (nflag)
117117680Spst		tp->name = dnnum_string(dnaddr);
117217680Spst	else
117317680Spst		tp->name = dnname_string(dnaddr);
117417680Spst
117517680Spst	return(tp->name);
117617680Spst}
117717680Spst
117817680Spst/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
117917680Spststruct hnamemem *
118026183Sfennernewhnamemem(void)
118117680Spst{
118217680Spst	register struct hnamemem *p;
118317680Spst	static struct hnamemem *ptr = NULL;
118417680Spst	static u_int num = 0;
118517680Spst
118617680Spst	if (num  <= 0) {
118717680Spst		num = 64;
118817680Spst		ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
118917680Spst		if (ptr == NULL)
119017680Spst			error("newhnamemem: calloc");
119117680Spst	}
119217680Spst	--num;
119317680Spst	p = ptr++;
119417680Spst	return (p);
119517680Spst}
119656896Sfenner
119756896Sfenner#ifdef INET6
119856896Sfenner/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
119956896Sfennerstruct h6namemem *
120056896Sfennernewh6namemem(void)
120156896Sfenner{
120256896Sfenner	register struct h6namemem *p;
120356896Sfenner	static struct h6namemem *ptr = NULL;
120456896Sfenner	static u_int num = 0;
120556896Sfenner
120656896Sfenner	if (num  <= 0) {
120756896Sfenner		num = 64;
120856896Sfenner		ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
120956896Sfenner		if (ptr == NULL)
121056896Sfenner			error("newh6namemem: calloc");
121156896Sfenner	}
121256896Sfenner	--num;
121356896Sfenner	p = ptr++;
121456896Sfenner	return (p);
121556896Sfenner}
121656896Sfenner#endif /* INET6 */
1217