xref: /freebsd/contrib/tcpdump/addrtoname.c (revision b01988a5f58a72c7827daebe9d885364791f3ae8)
14edb46e9SPaul Traina /*
2699fc314SBill Fenner  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
34edb46e9SPaul Traina  *	The Regents of the University of California.  All rights reserved.
44edb46e9SPaul Traina  *
54edb46e9SPaul Traina  * Redistribution and use in source and binary forms, with or without
64edb46e9SPaul Traina  * modification, are permitted provided that: (1) source code distributions
74edb46e9SPaul Traina  * retain the above copyright notice and this paragraph in its entirety, (2)
84edb46e9SPaul Traina  * distributions including binary code include the above copyright notice and
94edb46e9SPaul Traina  * this paragraph in its entirety in the documentation or other materials
104edb46e9SPaul Traina  * provided with the distribution, and (3) all advertising materials mentioning
114edb46e9SPaul Traina  * features or use of this software display the following acknowledgement:
124edb46e9SPaul Traina  * ``This product includes software developed by the University of California,
134edb46e9SPaul Traina  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
144edb46e9SPaul Traina  * the University nor the names of its contributors may be used to endorse
154edb46e9SPaul Traina  * or promote products derived from this software without specific prior
164edb46e9SPaul Traina  * written permission.
174edb46e9SPaul Traina  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
184edb46e9SPaul Traina  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
194edb46e9SPaul Traina  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
204edb46e9SPaul Traina  *
214edb46e9SPaul Traina  *  Internet, ethernet, port, and protocol string to address
224edb46e9SPaul Traina  *  and address to string conversion routines
234edb46e9SPaul Traina  */
24a88113a8SBill Fenner 
25a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
26a88113a8SBill Fenner #include "config.h"
274edb46e9SPaul Traina #endif
284edb46e9SPaul Traina 
29*b01988a5SMariusz Zaborski #ifdef HAVE_CASPER
30c501d73cSMariusz Zaborski #include <libcasper.h>
31c501d73cSMariusz Zaborski #include <casper/cap_dns.h>
32*b01988a5SMariusz Zaborski #endif /* HAVE_CASPER */
333340d773SGleb Smirnoff 
343340d773SGleb Smirnoff #include <netdissect-stdinc.h>
350e0def19SBill Fenner 
360e0def19SBill Fenner #ifdef USE_ETHER_NTOHOST
37943ee2b1SBill Fenner #ifdef HAVE_NETINET_IF_ETHER_H
380e0def19SBill Fenner struct mbuf;		/* Squelch compiler warnings on some platforms for */
390e0def19SBill Fenner struct rtentry;		/* declarations in <net/if.h> */
400e0def19SBill Fenner #include <net/if.h>	/* for "struct ifnet" in "struct arpcom" on Solaris */
41943ee2b1SBill Fenner #include <netinet/if_ether.h>
420e0def19SBill Fenner #endif /* HAVE_NETINET_IF_ETHER_H */
43c1ad1296SSam Leffler #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
44c1ad1296SSam Leffler #include <netinet/ether.h>
45c1ad1296SSam Leffler #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
46a88113a8SBill Fenner 
47c1ad1296SSam Leffler #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
4829292c17SSam Leffler #ifndef HAVE_STRUCT_ETHER_ADDR
4929292c17SSam Leffler struct ether_addr {
5029292c17SSam Leffler 	unsigned char ether_addr_octet[6];
5129292c17SSam Leffler };
5229292c17SSam Leffler #endif
53c1ad1296SSam Leffler extern int ether_ntohost(char *, const struct ether_addr *);
54c1ad1296SSam Leffler #endif
55c1ad1296SSam Leffler 
5629292c17SSam Leffler #endif /* USE_ETHER_NTOHOST */
5729292c17SSam Leffler 
584edb46e9SPaul Traina #include <pcap.h>
594edb46e9SPaul Traina #include <pcap-namedb.h>
604edb46e9SPaul Traina #include <signal.h>
614edb46e9SPaul Traina #include <stdio.h>
624edb46e9SPaul Traina #include <string.h>
634edb46e9SPaul Traina #include <stdlib.h>
644edb46e9SPaul Traina 
653340d773SGleb Smirnoff #include "netdissect.h"
664edb46e9SPaul Traina #include "addrtoname.h"
673340d773SGleb Smirnoff #include "addrtostr.h"
683340d773SGleb Smirnoff #include "ethertype.h"
694edb46e9SPaul Traina #include "llc.h"
70699fc314SBill Fenner #include "setsignal.h"
7129292c17SSam Leffler #include "extract.h"
7229292c17SSam Leffler #include "oui.h"
734edb46e9SPaul Traina 
74abf25193SMax Laier #ifndef ETHER_ADDR_LEN
75abf25193SMax Laier #define ETHER_ADDR_LEN	6
76abf25193SMax Laier #endif
77abf25193SMax Laier 
784edb46e9SPaul Traina /*
794edb46e9SPaul Traina  * hash tables for whatever-to-name translations
80cc391cceSBruce M Simpson  *
813340d773SGleb Smirnoff  * ndo_error() called on strdup(3) failure
824edb46e9SPaul Traina  */
834edb46e9SPaul Traina 
844edb46e9SPaul Traina #define HASHNAMESIZE 4096
854edb46e9SPaul Traina 
864edb46e9SPaul Traina struct hnamemem {
873c602fabSXin LI 	uint32_t addr;
88a1c2090eSBill Fenner 	const char *name;
894edb46e9SPaul Traina 	struct hnamemem *nxt;
904edb46e9SPaul Traina };
914edb46e9SPaul Traina 
9227df3f5dSRui Paulo static struct hnamemem hnametable[HASHNAMESIZE];
9327df3f5dSRui Paulo static struct hnamemem tporttable[HASHNAMESIZE];
9427df3f5dSRui Paulo static struct hnamemem uporttable[HASHNAMESIZE];
9527df3f5dSRui Paulo static struct hnamemem eprototable[HASHNAMESIZE];
9627df3f5dSRui Paulo static struct hnamemem dnaddrtable[HASHNAMESIZE];
9727df3f5dSRui Paulo static struct hnamemem ipxsaptable[HASHNAMESIZE];
98cc391cceSBruce M Simpson 
993340d773SGleb Smirnoff #ifdef _WIN32
100cc391cceSBruce M Simpson /*
101cc391cceSBruce M Simpson  * fake gethostbyaddr for Win2k/XP
102cc391cceSBruce M Simpson  * gethostbyaddr() returns incorrect value when AF_INET6 is passed
103cc391cceSBruce M Simpson  * to 3rd argument.
104cc391cceSBruce M Simpson  *
105cc391cceSBruce M Simpson  * h_name in struct hostent is only valid.
106cc391cceSBruce M Simpson  */
107cc391cceSBruce M Simpson static struct hostent *
108cc391cceSBruce M Simpson win32_gethostbyaddr(const char *addr, int len, int type)
109cc391cceSBruce M Simpson {
110cc391cceSBruce M Simpson 	static struct hostent host;
111cc391cceSBruce M Simpson 	static char hostbuf[NI_MAXHOST];
112cc391cceSBruce M Simpson 	char hname[NI_MAXHOST];
113cc391cceSBruce M Simpson 	struct sockaddr_in6 addr6;
114cc391cceSBruce M Simpson 
115cc391cceSBruce M Simpson 	host.h_name = hostbuf;
116cc391cceSBruce M Simpson 	switch (type) {
117cc391cceSBruce M Simpson 	case AF_INET:
118cc391cceSBruce M Simpson 		return gethostbyaddr(addr, len, type);
119cc391cceSBruce M Simpson 		break;
120cc391cceSBruce M Simpson 	case AF_INET6:
121cc391cceSBruce M Simpson 		memset(&addr6, 0, sizeof(addr6));
122cc391cceSBruce M Simpson 		addr6.sin6_family = AF_INET6;
123cc391cceSBruce M Simpson 		memcpy(&addr6.sin6_addr, addr, len);
124cc391cceSBruce M Simpson 		if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
125cc391cceSBruce M Simpson 		    hname, sizeof(hname), NULL, 0, 0)) {
126cc391cceSBruce M Simpson 			return NULL;
127cc391cceSBruce M Simpson 		} else {
128cc391cceSBruce M Simpson 			strcpy(host.h_name, hname);
129cc391cceSBruce M Simpson 			return &host;
130cc391cceSBruce M Simpson 		}
131cc391cceSBruce M Simpson 		break;
132cc391cceSBruce M Simpson 	default:
133cc391cceSBruce M Simpson 		return NULL;
134cc391cceSBruce M Simpson 	}
135cc391cceSBruce M Simpson }
136cc391cceSBruce M Simpson #define gethostbyaddr win32_gethostbyaddr
1373340d773SGleb Smirnoff #endif /* _WIN32 */
1384edb46e9SPaul Traina 
139a88113a8SBill Fenner struct h6namemem {
140a88113a8SBill Fenner 	struct in6_addr addr;
141a88113a8SBill Fenner 	char *name;
142a88113a8SBill Fenner 	struct h6namemem *nxt;
143a88113a8SBill Fenner };
144a88113a8SBill Fenner 
14527df3f5dSRui Paulo static struct h6namemem h6nametable[HASHNAMESIZE];
146a88113a8SBill Fenner 
1474edb46e9SPaul Traina struct enamemem {
1484edb46e9SPaul Traina 	u_short e_addr0;
1494edb46e9SPaul Traina 	u_short e_addr1;
1504edb46e9SPaul Traina 	u_short e_addr2;
151a1c2090eSBill Fenner 	const char *e_name;
1524edb46e9SPaul Traina 	u_char *e_nsap;			/* used only for nsaptable[] */
153a1c2090eSBill Fenner #define e_bs e_nsap			/* for bytestringtable */
1544edb46e9SPaul Traina 	struct enamemem *e_nxt;
1554edb46e9SPaul Traina };
1564edb46e9SPaul Traina 
15727df3f5dSRui Paulo static struct enamemem enametable[HASHNAMESIZE];
15827df3f5dSRui Paulo static struct enamemem nsaptable[HASHNAMESIZE];
15927df3f5dSRui Paulo static struct enamemem bytestringtable[HASHNAMESIZE];
1604edb46e9SPaul Traina 
1614edb46e9SPaul Traina struct protoidmem {
1623c602fabSXin LI 	uint32_t p_oui;
1634edb46e9SPaul Traina 	u_short p_proto;
164a1c2090eSBill Fenner 	const char *p_name;
1654edb46e9SPaul Traina 	struct protoidmem *p_nxt;
1664edb46e9SPaul Traina };
1674edb46e9SPaul Traina 
16827df3f5dSRui Paulo static struct protoidmem protoidtable[HASHNAMESIZE];
1694edb46e9SPaul Traina 
1704edb46e9SPaul Traina /*
1714edb46e9SPaul Traina  * A faster replacement for inet_ntoa().
1724edb46e9SPaul Traina  */
173a1c2090eSBill Fenner const char *
1743c602fabSXin LI intoa(uint32_t addr)
1754edb46e9SPaul Traina {
1764edb46e9SPaul Traina 	register char *cp;
1774edb46e9SPaul Traina 	register u_int byte;
1784edb46e9SPaul Traina 	register int n;
1794edb46e9SPaul Traina 	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
1804edb46e9SPaul Traina 
1814edb46e9SPaul Traina 	NTOHL(addr);
18229292c17SSam Leffler 	cp = buf + sizeof(buf);
1834edb46e9SPaul Traina 	*--cp = '\0';
1844edb46e9SPaul Traina 
1854edb46e9SPaul Traina 	n = 4;
1864edb46e9SPaul Traina 	do {
1874edb46e9SPaul Traina 		byte = addr & 0xff;
1884edb46e9SPaul Traina 		*--cp = byte % 10 + '0';
1894edb46e9SPaul Traina 		byte /= 10;
1904edb46e9SPaul Traina 		if (byte > 0) {
1914edb46e9SPaul Traina 			*--cp = byte % 10 + '0';
1924edb46e9SPaul Traina 			byte /= 10;
1934edb46e9SPaul Traina 			if (byte > 0)
1944edb46e9SPaul Traina 				*--cp = byte + '0';
1954edb46e9SPaul Traina 		}
1964edb46e9SPaul Traina 		*--cp = '.';
1974edb46e9SPaul Traina 		addr >>= 8;
1984edb46e9SPaul Traina 	} while (--n > 0);
1994edb46e9SPaul Traina 
2004edb46e9SPaul Traina 	return cp + 1;
2014edb46e9SPaul Traina }
2024edb46e9SPaul Traina 
2033c602fabSXin LI static uint32_t f_netmask;
2043c602fabSXin LI static uint32_t f_localnet;
205*b01988a5SMariusz Zaborski #ifdef HAVE_CASPER
206197731f6SPawel Jakub Dawidek extern cap_channel_t *capdns;
207197731f6SPawel Jakub Dawidek #endif
2084edb46e9SPaul Traina 
2094edb46e9SPaul Traina /*
2104edb46e9SPaul Traina  * Return a name for the IP address pointed to by ap.  This address
2114edb46e9SPaul Traina  * is assumed to be in network byte order.
212cc391cceSBruce M Simpson  *
213cc391cceSBruce M Simpson  * NOTE: ap is *NOT* necessarily part of the packet data (not even if
214cc391cceSBruce M Simpson  * this is being called with the "ipaddr_string()" macro), so you
2153340d773SGleb Smirnoff  * *CANNOT* use the ND_TCHECK{2}/ND_TTEST{2} macros on it.  Furthermore,
216cc391cceSBruce M Simpson  * even in cases where it *is* part of the packet data, the caller
217cc391cceSBruce M Simpson  * would still have to check for a null return value, even if it's
218cc391cceSBruce M Simpson  * just printing the return value with "%s" - not all versions of
219cc391cceSBruce M Simpson  * printf print "(null)" with "%s" and a null pointer, some of them
220cc391cceSBruce M Simpson  * don't check for a null pointer and crash in that case.
221cc391cceSBruce M Simpson  *
222cc391cceSBruce M Simpson  * The callers of this routine should, before handing this routine
223cc391cceSBruce M Simpson  * a pointer to packet data, be sure that the data is present in
224cc391cceSBruce M Simpson  * the packet buffer.  They should probably do those checks anyway,
225cc391cceSBruce M Simpson  * as other data at that layer might not be IP addresses, and it
226cc391cceSBruce M Simpson  * also needs to check whether they're present in the packet buffer.
2274edb46e9SPaul Traina  */
228a1c2090eSBill Fenner const char *
2293c602fabSXin LI getname(netdissect_options *ndo, const u_char *ap)
2304edb46e9SPaul Traina {
2314edb46e9SPaul Traina 	register struct hostent *hp;
2323c602fabSXin LI 	uint32_t addr;
2333340d773SGleb Smirnoff 	struct hnamemem *p;
2344edb46e9SPaul Traina 
235a88113a8SBill Fenner 	memcpy(&addr, ap, sizeof(addr));
2364edb46e9SPaul Traina 	p = &hnametable[addr & (HASHNAMESIZE-1)];
2374edb46e9SPaul Traina 	for (; p->nxt; p = p->nxt) {
2384edb46e9SPaul Traina 		if (p->addr == addr)
2394edb46e9SPaul Traina 			return (p->name);
2404edb46e9SPaul Traina 	}
2414edb46e9SPaul Traina 	p->addr = addr;
2423340d773SGleb Smirnoff 	p->nxt = newhnamemem(ndo);
2434edb46e9SPaul Traina 
2444edb46e9SPaul Traina 	/*
245cc391cceSBruce M Simpson 	 * Print names unless:
246cc391cceSBruce M Simpson 	 *	(1) -n was given.
247699fc314SBill Fenner 	 *      (2) Address is foreign and -f was given. (If -f was not
248cc391cceSBruce M Simpson 	 *	    given, f_netmask and f_localnet are 0 and the test
249699fc314SBill Fenner 	 *	    evaluates to true)
2504edb46e9SPaul Traina 	 */
2513c602fabSXin LI 	if (!ndo->ndo_nflag &&
252cc391cceSBruce M Simpson 	    (addr & f_netmask) == f_localnet) {
253*b01988a5SMariusz Zaborski #ifdef HAVE_CASPER
254197731f6SPawel Jakub Dawidek 		if (capdns != NULL) {
255197731f6SPawel Jakub Dawidek 			hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
256197731f6SPawel Jakub Dawidek 			    AF_INET);
257197731f6SPawel Jakub Dawidek 		} else
258197731f6SPawel Jakub Dawidek #endif
2594edb46e9SPaul Traina 			hp = gethostbyaddr((char *)&addr, 4, AF_INET);
2604edb46e9SPaul Traina 		if (hp) {
2614edb46e9SPaul Traina 			char *dotp;
2624edb46e9SPaul Traina 
263a1c2090eSBill Fenner 			p->name = strdup(hp->h_name);
2643340d773SGleb Smirnoff 			if (p->name == NULL)
2653340d773SGleb Smirnoff 				(*ndo->ndo_error)(ndo,
2663340d773SGleb Smirnoff 						  "getname: strdup(hp->h_name)");
2673c602fabSXin LI 			if (ndo->ndo_Nflag) {
2684edb46e9SPaul Traina 				/* Remove domain qualifications */
2694edb46e9SPaul Traina 				dotp = strchr(p->name, '.');
2704edb46e9SPaul Traina 				if (dotp)
2714edb46e9SPaul Traina 					*dotp = '\0';
2724edb46e9SPaul Traina 			}
2734edb46e9SPaul Traina 			return (p->name);
2744edb46e9SPaul Traina 		}
2754edb46e9SPaul Traina 	}
276a1c2090eSBill Fenner 	p->name = strdup(intoa(addr));
2773340d773SGleb Smirnoff 	if (p->name == NULL)
2783340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "getname: strdup(intoa(addr))");
2794edb46e9SPaul Traina 	return (p->name);
2804edb46e9SPaul Traina }
2814edb46e9SPaul Traina 
282a88113a8SBill Fenner /*
283a88113a8SBill Fenner  * Return a name for the IP6 address pointed to by ap.  This address
284a88113a8SBill Fenner  * is assumed to be in network byte order.
285a88113a8SBill Fenner  */
286a1c2090eSBill Fenner const char *
2873c602fabSXin LI getname6(netdissect_options *ndo, const u_char *ap)
288a88113a8SBill Fenner {
289a88113a8SBill Fenner 	register struct hostent *hp;
2903c602fabSXin LI 	union {
291a88113a8SBill Fenner 		struct in6_addr addr;
2923c602fabSXin LI 		struct for_hash_addr {
2933c602fabSXin LI 			char fill[14];
2943c602fabSXin LI 			uint16_t d;
2953c602fabSXin LI 		} addra;
2963c602fabSXin LI 	} addr;
2973340d773SGleb Smirnoff 	struct h6namemem *p;
298a1c2090eSBill Fenner 	register const char *cp;
299a88113a8SBill Fenner 	char ntop_buf[INET6_ADDRSTRLEN];
300a88113a8SBill Fenner 
301a88113a8SBill Fenner 	memcpy(&addr, ap, sizeof(addr));
3023c602fabSXin LI 	p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
303a88113a8SBill Fenner 	for (; p->nxt; p = p->nxt) {
304a88113a8SBill Fenner 		if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
305a88113a8SBill Fenner 			return (p->name);
306a88113a8SBill Fenner 	}
3073c602fabSXin LI 	p->addr = addr.addr;
3083340d773SGleb Smirnoff 	p->nxt = newh6namemem(ndo);
309a88113a8SBill Fenner 
310a88113a8SBill Fenner 	/*
311cc391cceSBruce M Simpson 	 * Do not print names if -n was given.
312a88113a8SBill Fenner 	 */
3133c602fabSXin LI 	if (!ndo->ndo_nflag) {
314*b01988a5SMariusz Zaborski #ifdef HAVE_CASPER
315197731f6SPawel Jakub Dawidek 		if (capdns != NULL) {
316197731f6SPawel Jakub Dawidek 			hp = cap_gethostbyaddr(capdns, (char *)&addr,
317197731f6SPawel Jakub Dawidek 			    sizeof(addr), AF_INET6);
318197731f6SPawel Jakub Dawidek 		} else
319197731f6SPawel Jakub Dawidek #endif
3203340d773SGleb Smirnoff 			hp = gethostbyaddr((char *)&addr, sizeof(addr),
3213340d773SGleb Smirnoff 			    AF_INET6);
322a88113a8SBill Fenner 		if (hp) {
323a88113a8SBill Fenner 			char *dotp;
324a88113a8SBill Fenner 
325a1c2090eSBill Fenner 			p->name = strdup(hp->h_name);
3263340d773SGleb Smirnoff 			if (p->name == NULL)
3273340d773SGleb Smirnoff 				(*ndo->ndo_error)(ndo,
3283340d773SGleb Smirnoff 						  "getname6: strdup(hp->h_name)");
3293c602fabSXin LI 			if (ndo->ndo_Nflag) {
330a88113a8SBill Fenner 				/* Remove domain qualifications */
331a88113a8SBill Fenner 				dotp = strchr(p->name, '.');
332a88113a8SBill Fenner 				if (dotp)
333a88113a8SBill Fenner 					*dotp = '\0';
334a88113a8SBill Fenner 			}
335a88113a8SBill Fenner 			return (p->name);
336a88113a8SBill Fenner 		}
337a88113a8SBill Fenner 	}
3383340d773SGleb Smirnoff 	cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
339a1c2090eSBill Fenner 	p->name = strdup(cp);
3403340d773SGleb Smirnoff 	if (p->name == NULL)
3413340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "getname6: strdup(cp)");
342a88113a8SBill Fenner 	return (p->name);
343a88113a8SBill Fenner }
344a88113a8SBill Fenner 
34527df3f5dSRui Paulo static const char hex[] = "0123456789abcdef";
3464edb46e9SPaul Traina 
3474edb46e9SPaul Traina 
3484edb46e9SPaul Traina /* Find the hash node that corresponds the ether address 'ep' */
3494edb46e9SPaul Traina 
3504edb46e9SPaul Traina static inline struct enamemem *
3513340d773SGleb Smirnoff lookup_emem(netdissect_options *ndo, const u_char *ep)
3524edb46e9SPaul Traina {
3534edb46e9SPaul Traina 	register u_int i, j, k;
3544edb46e9SPaul Traina 	struct enamemem *tp;
3554edb46e9SPaul Traina 
3564edb46e9SPaul Traina 	k = (ep[0] << 8) | ep[1];
3574edb46e9SPaul Traina 	j = (ep[2] << 8) | ep[3];
3584edb46e9SPaul Traina 	i = (ep[4] << 8) | ep[5];
3594edb46e9SPaul Traina 
3604edb46e9SPaul Traina 	tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
3614edb46e9SPaul Traina 	while (tp->e_nxt)
3624edb46e9SPaul Traina 		if (tp->e_addr0 == i &&
3634edb46e9SPaul Traina 		    tp->e_addr1 == j &&
3644edb46e9SPaul Traina 		    tp->e_addr2 == k)
3654edb46e9SPaul Traina 			return tp;
3664edb46e9SPaul Traina 		else
3674edb46e9SPaul Traina 			tp = tp->e_nxt;
3684edb46e9SPaul Traina 	tp->e_addr0 = i;
3694edb46e9SPaul Traina 	tp->e_addr1 = j;
3704edb46e9SPaul Traina 	tp->e_addr2 = k;
3714edb46e9SPaul Traina 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
3724edb46e9SPaul Traina 	if (tp->e_nxt == NULL)
3733340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_emem: calloc");
3744edb46e9SPaul Traina 
3754edb46e9SPaul Traina 	return tp;
3764edb46e9SPaul Traina }
3774edb46e9SPaul Traina 
378a1c2090eSBill Fenner /*
379a1c2090eSBill Fenner  * Find the hash node that corresponds to the bytestring 'bs'
380a1c2090eSBill Fenner  * with length 'nlen'
381a1c2090eSBill Fenner  */
382a1c2090eSBill Fenner 
383a1c2090eSBill Fenner static inline struct enamemem *
3843340d773SGleb Smirnoff lookup_bytestring(netdissect_options *ndo, register const u_char *bs,
3853340d773SGleb Smirnoff 		  const unsigned int nlen)
386a1c2090eSBill Fenner {
387a1c2090eSBill Fenner 	struct enamemem *tp;
388a1c2090eSBill Fenner 	register u_int i, j, k;
389a1c2090eSBill Fenner 
390a1c2090eSBill Fenner 	if (nlen >= 6) {
391a1c2090eSBill Fenner 		k = (bs[0] << 8) | bs[1];
392a1c2090eSBill Fenner 		j = (bs[2] << 8) | bs[3];
393a1c2090eSBill Fenner 		i = (bs[4] << 8) | bs[5];
394a1c2090eSBill Fenner 	} else if (nlen >= 4) {
395a1c2090eSBill Fenner 		k = (bs[0] << 8) | bs[1];
396a1c2090eSBill Fenner 		j = (bs[2] << 8) | bs[3];
397a1c2090eSBill Fenner 		i = 0;
398a1c2090eSBill Fenner 	} else
399a1c2090eSBill Fenner 		i = j = k = 0;
400a1c2090eSBill Fenner 
401a1c2090eSBill Fenner 	tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
402a1c2090eSBill Fenner 	while (tp->e_nxt)
403a1c2090eSBill Fenner 		if (tp->e_addr0 == i &&
404a1c2090eSBill Fenner 		    tp->e_addr1 == j &&
405a1c2090eSBill Fenner 		    tp->e_addr2 == k &&
406a1c2090eSBill Fenner 		    memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
407a1c2090eSBill Fenner 			return tp;
408a1c2090eSBill Fenner 		else
409a1c2090eSBill Fenner 			tp = tp->e_nxt;
410a1c2090eSBill Fenner 
411a1c2090eSBill Fenner 	tp->e_addr0 = i;
412a1c2090eSBill Fenner 	tp->e_addr1 = j;
413a1c2090eSBill Fenner 	tp->e_addr2 = k;
414a1c2090eSBill Fenner 
415a1c2090eSBill Fenner 	tp->e_bs = (u_char *) calloc(1, nlen + 1);
416d03c0883SXin LI 	if (tp->e_bs == NULL)
4173340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_bytestring: calloc");
418d03c0883SXin LI 
419a1c2090eSBill Fenner 	memcpy(tp->e_bs, bs, nlen);
420a1c2090eSBill Fenner 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
421a1c2090eSBill Fenner 	if (tp->e_nxt == NULL)
4223340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_bytestring: calloc");
423a1c2090eSBill Fenner 
424a1c2090eSBill Fenner 	return tp;
425a1c2090eSBill Fenner }
426a1c2090eSBill Fenner 
4274edb46e9SPaul Traina /* Find the hash node that corresponds the NSAP 'nsap' */
4284edb46e9SPaul Traina 
4294edb46e9SPaul Traina static inline struct enamemem *
4303340d773SGleb Smirnoff lookup_nsap(netdissect_options *ndo, register const u_char *nsap,
4313340d773SGleb Smirnoff 	    register u_int nsap_length)
4324edb46e9SPaul Traina {
4334edb46e9SPaul Traina 	register u_int i, j, k;
4344edb46e9SPaul Traina 	struct enamemem *tp;
4353340d773SGleb Smirnoff 	const u_char *ensap;
4364edb46e9SPaul Traina 
4373340d773SGleb Smirnoff 	if (nsap_length > 6) {
4383340d773SGleb Smirnoff 		ensap = nsap + nsap_length - 6;
4394edb46e9SPaul Traina 		k = (ensap[0] << 8) | ensap[1];
4404edb46e9SPaul Traina 		j = (ensap[2] << 8) | ensap[3];
4414edb46e9SPaul Traina 		i = (ensap[4] << 8) | ensap[5];
4424edb46e9SPaul Traina 	}
4434edb46e9SPaul Traina 	else
4444edb46e9SPaul Traina 		i = j = k = 0;
4454edb46e9SPaul Traina 
4464edb46e9SPaul Traina 	tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
4474edb46e9SPaul Traina 	while (tp->e_nxt)
4484edb46e9SPaul Traina 		if (tp->e_addr0 == i &&
4494edb46e9SPaul Traina 		    tp->e_addr1 == j &&
4504edb46e9SPaul Traina 		    tp->e_addr2 == k &&
4513340d773SGleb Smirnoff 		    tp->e_nsap[0] == nsap_length &&
452a1c2090eSBill Fenner 		    memcmp((const char *)&(nsap[1]),
4533340d773SGleb Smirnoff 			(char *)&(tp->e_nsap[1]), nsap_length) == 0)
4544edb46e9SPaul Traina 			return tp;
4554edb46e9SPaul Traina 		else
4564edb46e9SPaul Traina 			tp = tp->e_nxt;
4574edb46e9SPaul Traina 	tp->e_addr0 = i;
4584edb46e9SPaul Traina 	tp->e_addr1 = j;
4594edb46e9SPaul Traina 	tp->e_addr2 = k;
4603340d773SGleb Smirnoff 	tp->e_nsap = (u_char *)malloc(nsap_length + 1);
4614edb46e9SPaul Traina 	if (tp->e_nsap == NULL)
4623340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_nsap: malloc");
4633340d773SGleb Smirnoff 	tp->e_nsap[0] = (u_char)nsap_length;	/* guaranteed < ISONSAP_MAX_LENGTH */
4643340d773SGleb Smirnoff 	memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
4654edb46e9SPaul Traina 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
4664edb46e9SPaul Traina 	if (tp->e_nxt == NULL)
4673340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_nsap: calloc");
4684edb46e9SPaul Traina 
4694edb46e9SPaul Traina 	return tp;
4704edb46e9SPaul Traina }
4714edb46e9SPaul Traina 
4724edb46e9SPaul Traina /* Find the hash node that corresponds the protoid 'pi'. */
4734edb46e9SPaul Traina 
4744edb46e9SPaul Traina static inline struct protoidmem *
4753340d773SGleb Smirnoff lookup_protoid(netdissect_options *ndo, const u_char *pi)
4764edb46e9SPaul Traina {
4774edb46e9SPaul Traina 	register u_int i, j;
4784edb46e9SPaul Traina 	struct protoidmem *tp;
4794edb46e9SPaul Traina 
4804edb46e9SPaul Traina 	/* 5 octets won't be aligned */
4814edb46e9SPaul Traina 	i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
4824edb46e9SPaul Traina 	j =   (pi[3] << 8) + pi[4];
4834edb46e9SPaul Traina 	/* XXX should be endian-insensitive, but do big-endian testing  XXX */
4844edb46e9SPaul Traina 
4854edb46e9SPaul Traina 	tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
4864edb46e9SPaul Traina 	while (tp->p_nxt)
4874edb46e9SPaul Traina 		if (tp->p_oui == i && tp->p_proto == j)
4884edb46e9SPaul Traina 			return tp;
4894edb46e9SPaul Traina 		else
4904edb46e9SPaul Traina 			tp = tp->p_nxt;
4914edb46e9SPaul Traina 	tp->p_oui = i;
4924edb46e9SPaul Traina 	tp->p_proto = j;
4934edb46e9SPaul Traina 	tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
4944edb46e9SPaul Traina 	if (tp->p_nxt == NULL)
4953340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "lookup_protoid: calloc");
4964edb46e9SPaul Traina 
4974edb46e9SPaul Traina 	return tp;
4984edb46e9SPaul Traina }
4994edb46e9SPaul Traina 
500a1c2090eSBill Fenner const char *
5013c602fabSXin LI etheraddr_string(netdissect_options *ndo, register const u_char *ep)
5024edb46e9SPaul Traina {
50317cb103cSSam Leffler 	register int i;
5044edb46e9SPaul Traina 	register char *cp;
5054edb46e9SPaul Traina 	register struct enamemem *tp;
50617cb103cSSam Leffler 	int oui;
50729292c17SSam Leffler 	char buf[BUFSIZE];
5084edb46e9SPaul Traina 
5093340d773SGleb Smirnoff 	tp = lookup_emem(ndo, ep);
5104edb46e9SPaul Traina 	if (tp->e_name)
5114edb46e9SPaul Traina 		return (tp->e_name);
512a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
5133c602fabSXin LI 	if (!ndo->ndo_nflag) {
51429292c17SSam Leffler 		char buf2[BUFSIZE];
515c1ad1296SSam Leffler 
5163340d773SGleb Smirnoff 		if (ether_ntohost(buf2, (const struct ether_addr *)ep) == 0) {
517cc391cceSBruce M Simpson 			tp->e_name = strdup(buf2);
5183340d773SGleb Smirnoff 			if (tp->e_name == NULL)
5193340d773SGleb Smirnoff 				(*ndo->ndo_error)(ndo,
5203340d773SGleb Smirnoff 						  "etheraddr_string: strdup(buf2)");
5214edb46e9SPaul Traina 			return (tp->e_name);
5224edb46e9SPaul Traina 		}
5234edb46e9SPaul Traina 	}
5244edb46e9SPaul Traina #endif
5254edb46e9SPaul Traina 	cp = buf;
52629292c17SSam Leffler 	oui = EXTRACT_24BITS(ep);
527cc391cceSBruce M Simpson 	*cp++ = hex[*ep >> 4 ];
5284edb46e9SPaul Traina 	*cp++ = hex[*ep++ & 0xf];
52917cb103cSSam Leffler 	for (i = 5; --i >= 0;) {
5304edb46e9SPaul Traina 		*cp++ = ':';
531cc391cceSBruce M Simpson 		*cp++ = hex[*ep >> 4 ];
5324edb46e9SPaul Traina 		*cp++ = hex[*ep++ & 0xf];
5334edb46e9SPaul Traina 	}
53429292c17SSam Leffler 
5353c602fabSXin LI 	if (!ndo->ndo_nflag) {
53617cb103cSSam Leffler 		snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
53729292c17SSam Leffler 		    tok2str(oui_values, "Unknown", oui));
53829292c17SSam Leffler 	} else
5394edb46e9SPaul Traina 		*cp = '\0';
540a1c2090eSBill Fenner 	tp->e_name = strdup(buf);
5413340d773SGleb Smirnoff 	if (tp->e_name == NULL)
5423340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "etheraddr_string: strdup(buf)");
5434edb46e9SPaul Traina 	return (tp->e_name);
5444edb46e9SPaul Traina }
5454edb46e9SPaul Traina 
546a1c2090eSBill Fenner const char *
5473340d773SGleb Smirnoff le64addr_string(netdissect_options *ndo, const u_char *ep)
548cac3dcd5SXin LI {
549cac3dcd5SXin LI 	const unsigned int len = 8;
550cac3dcd5SXin LI 	register u_int i;
551cac3dcd5SXin LI 	register char *cp;
552cac3dcd5SXin LI 	register struct enamemem *tp;
553cac3dcd5SXin LI 	char buf[BUFSIZE];
554cac3dcd5SXin LI 
5553340d773SGleb Smirnoff 	tp = lookup_bytestring(ndo, ep, len);
556cac3dcd5SXin LI 	if (tp->e_name)
557cac3dcd5SXin LI 		return (tp->e_name);
558cac3dcd5SXin LI 
559cac3dcd5SXin LI 	cp = buf;
560cac3dcd5SXin LI 	for (i = len; i > 0 ; --i) {
561cac3dcd5SXin LI 		*cp++ = hex[*(ep + i - 1) >> 4];
562cac3dcd5SXin LI 		*cp++ = hex[*(ep + i - 1) & 0xf];
563cac3dcd5SXin LI 		*cp++ = ':';
564cac3dcd5SXin LI 	}
565cac3dcd5SXin LI 	cp --;
566cac3dcd5SXin LI 
567cac3dcd5SXin LI 	*cp = '\0';
568cac3dcd5SXin LI 
569cac3dcd5SXin LI 	tp->e_name = strdup(buf);
5703340d773SGleb Smirnoff 	if (tp->e_name == NULL)
5713340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "le64addr_string: strdup(buf)");
572cac3dcd5SXin LI 
573cac3dcd5SXin LI 	return (tp->e_name);
574cac3dcd5SXin LI }
575cac3dcd5SXin LI 
576cac3dcd5SXin LI const char *
5773340d773SGleb Smirnoff linkaddr_string(netdissect_options *ndo, const u_char *ep,
5783340d773SGleb Smirnoff 		const unsigned int type, const unsigned int len)
579a1c2090eSBill Fenner {
580c1ad1296SSam Leffler 	register u_int i;
581a1c2090eSBill Fenner 	register char *cp;
582a1c2090eSBill Fenner 	register struct enamemem *tp;
583a1c2090eSBill Fenner 
58427df3f5dSRui Paulo 	if (len == 0)
58527df3f5dSRui Paulo 		return ("<empty>");
586a5779b6eSRui Paulo 
58727df3f5dSRui Paulo 	if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
5883c602fabSXin LI 		return (etheraddr_string(ndo, ep));
58927df3f5dSRui Paulo 
59027df3f5dSRui Paulo 	if (type == LINKADDR_FRELAY)
5918bdc5a62SPatrick Kelsey 		return (q922_string(ndo, ep, len));
592a1c2090eSBill Fenner 
5933340d773SGleb Smirnoff 	tp = lookup_bytestring(ndo, ep, len);
594a1c2090eSBill Fenner 	if (tp->e_name)
595a1c2090eSBill Fenner 		return (tp->e_name);
596a1c2090eSBill Fenner 
597a1c2090eSBill Fenner 	tp->e_name = cp = (char *)malloc(len*3);
598a1c2090eSBill Fenner 	if (tp->e_name == NULL)
5993340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "linkaddr_string: malloc");
600c1ad1296SSam Leffler 	*cp++ = hex[*ep >> 4];
601a1c2090eSBill Fenner 	*cp++ = hex[*ep++ & 0xf];
602a1c2090eSBill Fenner 	for (i = len-1; i > 0 ; --i) {
603a1c2090eSBill Fenner 		*cp++ = ':';
604c1ad1296SSam Leffler 		*cp++ = hex[*ep >> 4];
605a1c2090eSBill Fenner 		*cp++ = hex[*ep++ & 0xf];
606a1c2090eSBill Fenner 	}
607a1c2090eSBill Fenner 	*cp = '\0';
608a1c2090eSBill Fenner 	return (tp->e_name);
609a1c2090eSBill Fenner }
610a1c2090eSBill Fenner 
611a1c2090eSBill Fenner const char *
6123340d773SGleb Smirnoff etherproto_string(netdissect_options *ndo, u_short port)
6134edb46e9SPaul Traina {
6144edb46e9SPaul Traina 	register char *cp;
6154edb46e9SPaul Traina 	register struct hnamemem *tp;
6163c602fabSXin LI 	register uint32_t i = port;
6174edb46e9SPaul Traina 	char buf[sizeof("0000")];
6184edb46e9SPaul Traina 
6194edb46e9SPaul Traina 	for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
6204edb46e9SPaul Traina 		if (tp->addr == i)
6214edb46e9SPaul Traina 			return (tp->name);
6224edb46e9SPaul Traina 
6234edb46e9SPaul Traina 	tp->addr = i;
6243340d773SGleb Smirnoff 	tp->nxt = newhnamemem(ndo);
6254edb46e9SPaul Traina 
6264edb46e9SPaul Traina 	cp = buf;
6274edb46e9SPaul Traina 	NTOHS(port);
6284edb46e9SPaul Traina 	*cp++ = hex[port >> 12 & 0xf];
6294edb46e9SPaul Traina 	*cp++ = hex[port >> 8 & 0xf];
6304edb46e9SPaul Traina 	*cp++ = hex[port >> 4 & 0xf];
6314edb46e9SPaul Traina 	*cp++ = hex[port & 0xf];
6324edb46e9SPaul Traina 	*cp++ = '\0';
633a1c2090eSBill Fenner 	tp->name = strdup(buf);
6343340d773SGleb Smirnoff 	if (tp->name == NULL)
6353340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "etherproto_string: strdup(buf)");
6364edb46e9SPaul Traina 	return (tp->name);
6374edb46e9SPaul Traina }
6384edb46e9SPaul Traina 
639a1c2090eSBill Fenner const char *
6403340d773SGleb Smirnoff protoid_string(netdissect_options *ndo, register const u_char *pi)
6414edb46e9SPaul Traina {
6424edb46e9SPaul Traina 	register u_int i, j;
6434edb46e9SPaul Traina 	register char *cp;
6444edb46e9SPaul Traina 	register struct protoidmem *tp;
6454edb46e9SPaul Traina 	char buf[sizeof("00:00:00:00:00")];
6464edb46e9SPaul Traina 
6473340d773SGleb Smirnoff 	tp = lookup_protoid(ndo, pi);
6484edb46e9SPaul Traina 	if (tp->p_name)
6494edb46e9SPaul Traina 		return tp->p_name;
6504edb46e9SPaul Traina 
6514edb46e9SPaul Traina 	cp = buf;
6524edb46e9SPaul Traina 	if ((j = *pi >> 4) != 0)
6534edb46e9SPaul Traina 		*cp++ = hex[j];
6544edb46e9SPaul Traina 	*cp++ = hex[*pi++ & 0xf];
6554edb46e9SPaul Traina 	for (i = 4; (int)--i >= 0;) {
6564edb46e9SPaul Traina 		*cp++ = ':';
6574edb46e9SPaul Traina 		if ((j = *pi >> 4) != 0)
6584edb46e9SPaul Traina 			*cp++ = hex[j];
6594edb46e9SPaul Traina 		*cp++ = hex[*pi++ & 0xf];
6604edb46e9SPaul Traina 	}
6614edb46e9SPaul Traina 	*cp = '\0';
662a1c2090eSBill Fenner 	tp->p_name = strdup(buf);
6633340d773SGleb Smirnoff 	if (tp->p_name == NULL)
6643340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "protoid_string: strdup(buf)");
6654edb46e9SPaul Traina 	return (tp->p_name);
6664edb46e9SPaul Traina }
6674edb46e9SPaul Traina 
668c1ad1296SSam Leffler #define ISONSAP_MAX_LENGTH 20
669a1c2090eSBill Fenner const char *
6703340d773SGleb Smirnoff isonsap_string(netdissect_options *ndo, const u_char *nsap,
6713340d773SGleb Smirnoff 	       register u_int nsap_length)
6724edb46e9SPaul Traina {
673c1ad1296SSam Leffler 	register u_int nsap_idx;
6744edb46e9SPaul Traina 	register char *cp;
6754edb46e9SPaul Traina 	register struct enamemem *tp;
6764edb46e9SPaul Traina 
677c1ad1296SSam Leffler 	if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
67829292c17SSam Leffler 		return ("isonsap_string: illegal length");
679c1ad1296SSam Leffler 
6803340d773SGleb Smirnoff 	tp = lookup_nsap(ndo, nsap, nsap_length);
6814edb46e9SPaul Traina 	if (tp->e_name)
6824edb46e9SPaul Traina 		return tp->e_name;
6834edb46e9SPaul Traina 
684c1ad1296SSam Leffler 	tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
6854edb46e9SPaul Traina 	if (cp == NULL)
6863340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "isonsap_string: malloc");
6874edb46e9SPaul Traina 
688c1ad1296SSam Leffler 	for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
6894edb46e9SPaul Traina 		*cp++ = hex[*nsap >> 4];
6904edb46e9SPaul Traina 		*cp++ = hex[*nsap++ & 0xf];
691c1ad1296SSam Leffler 		if (((nsap_idx & 1) == 0) &&
692c1ad1296SSam Leffler 		     (nsap_idx + 1 < nsap_length)) {
69301bd0dbcSPaul Traina 		     	*cp++ = '.';
6944edb46e9SPaul Traina 		}
695c1ad1296SSam Leffler 	}
6964edb46e9SPaul Traina 	*cp = '\0';
6974edb46e9SPaul Traina 	return (tp->e_name);
6984edb46e9SPaul Traina }
6994edb46e9SPaul Traina 
700a1c2090eSBill Fenner const char *
7013340d773SGleb Smirnoff tcpport_string(netdissect_options *ndo, u_short port)
7024edb46e9SPaul Traina {
7034edb46e9SPaul Traina 	register struct hnamemem *tp;
7043c602fabSXin LI 	register uint32_t i = port;
7054edb46e9SPaul Traina 	char buf[sizeof("00000")];
7064edb46e9SPaul Traina 
7074edb46e9SPaul Traina 	for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
7084edb46e9SPaul Traina 		if (tp->addr == i)
7094edb46e9SPaul Traina 			return (tp->name);
7104edb46e9SPaul Traina 
7114edb46e9SPaul Traina 	tp->addr = i;
7123340d773SGleb Smirnoff 	tp->nxt = newhnamemem(ndo);
7134edb46e9SPaul Traina 
7147524a079SKris Kennaway 	(void)snprintf(buf, sizeof(buf), "%u", i);
715a1c2090eSBill Fenner 	tp->name = strdup(buf);
7163340d773SGleb Smirnoff 	if (tp->name == NULL)
7173340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "tcpport_string: strdup(buf)");
7184edb46e9SPaul Traina 	return (tp->name);
7194edb46e9SPaul Traina }
7204edb46e9SPaul Traina 
721a1c2090eSBill Fenner const char *
7223340d773SGleb Smirnoff udpport_string(netdissect_options *ndo, register u_short port)
7234edb46e9SPaul Traina {
7244edb46e9SPaul Traina 	register struct hnamemem *tp;
7253c602fabSXin LI 	register uint32_t i = port;
7264edb46e9SPaul Traina 	char buf[sizeof("00000")];
7274edb46e9SPaul Traina 
7284edb46e9SPaul Traina 	for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
7294edb46e9SPaul Traina 		if (tp->addr == i)
7304edb46e9SPaul Traina 			return (tp->name);
7314edb46e9SPaul Traina 
7324edb46e9SPaul Traina 	tp->addr = i;
7333340d773SGleb Smirnoff 	tp->nxt = newhnamemem(ndo);
7344edb46e9SPaul Traina 
7357524a079SKris Kennaway 	(void)snprintf(buf, sizeof(buf), "%u", i);
736a1c2090eSBill Fenner 	tp->name = strdup(buf);
7373340d773SGleb Smirnoff 	if (tp->name == NULL)
7383340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "udpport_string: strdup(buf)");
7394edb46e9SPaul Traina 	return (tp->name);
7404edb46e9SPaul Traina }
7414edb46e9SPaul Traina 
742cc391cceSBruce M Simpson const char *
7433340d773SGleb Smirnoff ipxsap_string(netdissect_options *ndo, u_short port)
744cc391cceSBruce M Simpson {
745cc391cceSBruce M Simpson 	register char *cp;
746cc391cceSBruce M Simpson 	register struct hnamemem *tp;
7473c602fabSXin LI 	register uint32_t i = port;
748cc391cceSBruce M Simpson 	char buf[sizeof("0000")];
749cc391cceSBruce M Simpson 
750cc391cceSBruce M Simpson 	for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
751cc391cceSBruce M Simpson 		if (tp->addr == i)
752cc391cceSBruce M Simpson 			return (tp->name);
753cc391cceSBruce M Simpson 
754cc391cceSBruce M Simpson 	tp->addr = i;
7553340d773SGleb Smirnoff 	tp->nxt = newhnamemem(ndo);
756cc391cceSBruce M Simpson 
757cc391cceSBruce M Simpson 	cp = buf;
758cc391cceSBruce M Simpson 	NTOHS(port);
759cc391cceSBruce M Simpson 	*cp++ = hex[port >> 12 & 0xf];
760cc391cceSBruce M Simpson 	*cp++ = hex[port >> 8 & 0xf];
761cc391cceSBruce M Simpson 	*cp++ = hex[port >> 4 & 0xf];
762cc391cceSBruce M Simpson 	*cp++ = hex[port & 0xf];
763cc391cceSBruce M Simpson 	*cp++ = '\0';
764cc391cceSBruce M Simpson 	tp->name = strdup(buf);
7653340d773SGleb Smirnoff 	if (tp->name == NULL)
7663340d773SGleb Smirnoff 		(*ndo->ndo_error)(ndo, "ipxsap_string: strdup(buf)");
767cc391cceSBruce M Simpson 	return (tp->name);
768cc391cceSBruce M Simpson }
769cc391cceSBruce M Simpson 
7704edb46e9SPaul Traina static void
7713c602fabSXin LI init_servarray(netdissect_options *ndo)
7724edb46e9SPaul Traina {
7734edb46e9SPaul Traina 	struct servent *sv;
7744edb46e9SPaul Traina 	register struct hnamemem *table;
7754edb46e9SPaul Traina 	register int i;
7764edb46e9SPaul Traina 	char buf[sizeof("0000000000")];
7774edb46e9SPaul Traina 
7784edb46e9SPaul Traina 	while ((sv = getservent()) != NULL) {
7794edb46e9SPaul Traina 		int port = ntohs(sv->s_port);
7804edb46e9SPaul Traina 		i = port & (HASHNAMESIZE-1);
7814edb46e9SPaul Traina 		if (strcmp(sv->s_proto, "tcp") == 0)
7824edb46e9SPaul Traina 			table = &tporttable[i];
7834edb46e9SPaul Traina 		else if (strcmp(sv->s_proto, "udp") == 0)
7844edb46e9SPaul Traina 			table = &uporttable[i];
7854edb46e9SPaul Traina 		else
7864edb46e9SPaul Traina 			continue;
7874edb46e9SPaul Traina 
7884edb46e9SPaul Traina 		while (table->name)
7894edb46e9SPaul Traina 			table = table->nxt;
7903c602fabSXin LI 		if (ndo->ndo_nflag) {
7917524a079SKris Kennaway 			(void)snprintf(buf, sizeof(buf), "%d", port);
792a1c2090eSBill Fenner 			table->name = strdup(buf);
7934edb46e9SPaul Traina 		} else
794a1c2090eSBill Fenner 			table->name = strdup(sv->s_name);
7953340d773SGleb Smirnoff 		if (table->name == NULL)
7963340d773SGleb Smirnoff 			(*ndo->ndo_error)(ndo, "init_servarray: strdup");
7973340d773SGleb Smirnoff 
7984edb46e9SPaul Traina 		table->addr = port;
7993340d773SGleb Smirnoff 		table->nxt = newhnamemem(ndo);
8004edb46e9SPaul Traina 	}
8014edb46e9SPaul Traina 	endservent();
8024edb46e9SPaul Traina }
8034edb46e9SPaul Traina 
8043340d773SGleb Smirnoff static const struct eproto {
805c1ad1296SSam Leffler 	const char *s;
8064edb46e9SPaul Traina 	u_short p;
8073340d773SGleb Smirnoff } eproto_db[] = {
8083340d773SGleb Smirnoff 	{ "pup", ETHERTYPE_PUP },
8093340d773SGleb Smirnoff 	{ "xns", ETHERTYPE_NS },
8103340d773SGleb Smirnoff 	{ "ip", ETHERTYPE_IP },
8113340d773SGleb Smirnoff 	{ "ip6", ETHERTYPE_IPV6 },
8123340d773SGleb Smirnoff 	{ "arp", ETHERTYPE_ARP },
8133340d773SGleb Smirnoff 	{ "rarp", ETHERTYPE_REVARP },
8143340d773SGleb Smirnoff 	{ "sprite", ETHERTYPE_SPRITE },
8153340d773SGleb Smirnoff 	{ "mopdl", ETHERTYPE_MOPDL },
8163340d773SGleb Smirnoff 	{ "moprc", ETHERTYPE_MOPRC },
8173340d773SGleb Smirnoff 	{ "decnet", ETHERTYPE_DN },
8183340d773SGleb Smirnoff 	{ "lat", ETHERTYPE_LAT },
8193340d773SGleb Smirnoff 	{ "sca", ETHERTYPE_SCA },
8203340d773SGleb Smirnoff 	{ "lanbridge", ETHERTYPE_LANBRIDGE },
8213340d773SGleb Smirnoff 	{ "vexp", ETHERTYPE_VEXP },
8223340d773SGleb Smirnoff 	{ "vprod", ETHERTYPE_VPROD },
8233340d773SGleb Smirnoff 	{ "atalk", ETHERTYPE_ATALK },
8243340d773SGleb Smirnoff 	{ "atalkarp", ETHERTYPE_AARP },
8253340d773SGleb Smirnoff 	{ "loopback", ETHERTYPE_LOOPBACK },
8263340d773SGleb Smirnoff 	{ "decdts", ETHERTYPE_DECDTS },
8273340d773SGleb Smirnoff 	{ "decdns", ETHERTYPE_DECDNS },
8283340d773SGleb Smirnoff 	{ (char *)0, 0 }
8293340d773SGleb Smirnoff };
8304edb46e9SPaul Traina 
8314edb46e9SPaul Traina static void
8323340d773SGleb Smirnoff init_eprotoarray(netdissect_options *ndo)
8334edb46e9SPaul Traina {
8344edb46e9SPaul Traina 	register int i;
8354edb46e9SPaul Traina 	register struct hnamemem *table;
8364edb46e9SPaul Traina 
8374edb46e9SPaul Traina 	for (i = 0; eproto_db[i].s; i++) {
838cc391cceSBruce M Simpson 		int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
8394edb46e9SPaul Traina 		table = &eprototable[j];
8404edb46e9SPaul Traina 		while (table->name)
8414edb46e9SPaul Traina 			table = table->nxt;
8424edb46e9SPaul Traina 		table->name = eproto_db[i].s;
843cc391cceSBruce M Simpson 		table->addr = htons(eproto_db[i].p);
8443340d773SGleb Smirnoff 		table->nxt = newhnamemem(ndo);
8454edb46e9SPaul Traina 	}
8464edb46e9SPaul Traina }
8474edb46e9SPaul Traina 
84827df3f5dSRui Paulo static const struct protoidlist {
849a1c2090eSBill Fenner 	const u_char protoid[5];
850a1c2090eSBill Fenner 	const char *name;
851a1c2090eSBill Fenner } protoidlist[] = {
852a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
853a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
854a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
855a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
856a1c2090eSBill Fenner 	{{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
857a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
858a1c2090eSBill Fenner };
859a1c2090eSBill Fenner 
8604edb46e9SPaul Traina /*
8614edb46e9SPaul Traina  * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
8624edb46e9SPaul Traina  * types.
8634edb46e9SPaul Traina  */
8644edb46e9SPaul Traina static void
8653340d773SGleb Smirnoff init_protoidarray(netdissect_options *ndo)
8664edb46e9SPaul Traina {
8674edb46e9SPaul Traina 	register int i;
8684edb46e9SPaul Traina 	register struct protoidmem *tp;
86927df3f5dSRui Paulo 	const struct protoidlist *pl;
8704edb46e9SPaul Traina 	u_char protoid[5];
8714edb46e9SPaul Traina 
8724edb46e9SPaul Traina 	protoid[0] = 0;
8734edb46e9SPaul Traina 	protoid[1] = 0;
8744edb46e9SPaul Traina 	protoid[2] = 0;
8754edb46e9SPaul Traina 	for (i = 0; eproto_db[i].s; i++) {
8764edb46e9SPaul Traina 		u_short etype = htons(eproto_db[i].p);
8774edb46e9SPaul Traina 
8784edb46e9SPaul Traina 		memcpy((char *)&protoid[3], (char *)&etype, 2);
8793340d773SGleb Smirnoff 		tp = lookup_protoid(ndo, protoid);
880a1c2090eSBill Fenner 		tp->p_name = strdup(eproto_db[i].s);
8813340d773SGleb Smirnoff 		if (tp->p_name == NULL)
8823340d773SGleb Smirnoff 			(*ndo->ndo_error)(ndo,
8833340d773SGleb Smirnoff 					  "init_protoidarray: strdup(eproto_db[i].s)");
884a1c2090eSBill Fenner 	}
885a1c2090eSBill Fenner 	/* Hardwire some SNAP proto ID names */
886a1c2090eSBill Fenner 	for (pl = protoidlist; pl->name != NULL; ++pl) {
8873340d773SGleb Smirnoff 		tp = lookup_protoid(ndo, pl->protoid);
888a1c2090eSBill Fenner 		/* Don't override existing name */
889a1c2090eSBill Fenner 		if (tp->p_name != NULL)
890a1c2090eSBill Fenner 			continue;
891a1c2090eSBill Fenner 
892a1c2090eSBill Fenner 		tp->p_name = pl->name;
8934edb46e9SPaul Traina 	}
8944edb46e9SPaul Traina }
8954edb46e9SPaul Traina 
89627df3f5dSRui Paulo static const struct etherlist {
897a1c2090eSBill Fenner 	const u_char addr[6];
898a1c2090eSBill Fenner 	const char *name;
8994edb46e9SPaul Traina } etherlist[] = {
9004edb46e9SPaul Traina 	{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
9014edb46e9SPaul Traina 	{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
9024edb46e9SPaul Traina };
9034edb46e9SPaul Traina 
9044edb46e9SPaul Traina /*
9054edb46e9SPaul Traina  * Initialize the ethers hash table.  We take two different approaches
9064edb46e9SPaul Traina  * depending on whether or not the system provides the ethers name
9074edb46e9SPaul Traina  * service.  If it does, we just wire in a few names at startup,
9084edb46e9SPaul Traina  * and etheraddr_string() fills in the table on demand.  If it doesn't,
9094edb46e9SPaul Traina  * then we suck in the entire /etc/ethers file at startup.  The idea
9104edb46e9SPaul Traina  * is that parsing the local file will be fast, but spinning through
9114edb46e9SPaul Traina  * all the ethers entries via NIS & next_etherent might be very slow.
9124edb46e9SPaul Traina  *
9134edb46e9SPaul Traina  * XXX pcap_next_etherent doesn't belong in the pcap interface, but
9144edb46e9SPaul Traina  * since the pcap module already does name-to-address translation,
9154edb46e9SPaul Traina  * it's already does most of the work for the ethernet address-to-name
9164edb46e9SPaul Traina  * translation, so we just pcap_next_etherent as a convenience.
9174edb46e9SPaul Traina  */
9184edb46e9SPaul Traina static void
9193340d773SGleb Smirnoff init_etherarray(netdissect_options *ndo)
9204edb46e9SPaul Traina {
92127df3f5dSRui Paulo 	register const struct etherlist *el;
9224edb46e9SPaul Traina 	register struct enamemem *tp;
923a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
9244edb46e9SPaul Traina 	char name[256];
9254edb46e9SPaul Traina #else
9264edb46e9SPaul Traina 	register struct pcap_etherent *ep;
9274edb46e9SPaul Traina 	register FILE *fp;
9284edb46e9SPaul Traina 
9294edb46e9SPaul Traina 	/* Suck in entire ethers file */
9304edb46e9SPaul Traina 	fp = fopen(PCAP_ETHERS_FILE, "r");
9314edb46e9SPaul Traina 	if (fp != NULL) {
9324edb46e9SPaul Traina 		while ((ep = pcap_next_etherent(fp)) != NULL) {
9333340d773SGleb Smirnoff 			tp = lookup_emem(ndo, ep->addr);
934a1c2090eSBill Fenner 			tp->e_name = strdup(ep->name);
9353340d773SGleb Smirnoff 			if (tp->e_name == NULL)
9363340d773SGleb Smirnoff 				(*ndo->ndo_error)(ndo,
9373340d773SGleb Smirnoff 						  "init_etherarray: strdup(ep->addr)");
9384edb46e9SPaul Traina 		}
9394edb46e9SPaul Traina 		(void)fclose(fp);
9404edb46e9SPaul Traina 	}
9414edb46e9SPaul Traina #endif
9424edb46e9SPaul Traina 
9434edb46e9SPaul Traina 	/* Hardwire some ethernet names */
9444edb46e9SPaul Traina 	for (el = etherlist; el->name != NULL; ++el) {
9453340d773SGleb Smirnoff 		tp = lookup_emem(ndo, el->addr);
9464edb46e9SPaul Traina 		/* Don't override existing name */
9474edb46e9SPaul Traina 		if (tp->e_name != NULL)
9484edb46e9SPaul Traina 			continue;
9494edb46e9SPaul Traina 
950a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
951c1ad1296SSam Leffler 		/*
952c1ad1296SSam Leffler 		 * Use YP/NIS version of name if available.
953c1ad1296SSam Leffler 		 */
9543340d773SGleb Smirnoff 		if (ether_ntohost(name, (const struct ether_addr *)el->addr) == 0) {
955a1c2090eSBill Fenner 			tp->e_name = strdup(name);
9563340d773SGleb Smirnoff 			if (tp->e_name == NULL)
9573340d773SGleb Smirnoff 				(*ndo->ndo_error)(ndo,
9583340d773SGleb Smirnoff 						  "init_etherarray: strdup(name)");
9594edb46e9SPaul Traina 			continue;
9604edb46e9SPaul Traina 		}
9614edb46e9SPaul Traina #endif
9624edb46e9SPaul Traina 		tp->e_name = el->name;
9634edb46e9SPaul Traina 	}
9644edb46e9SPaul Traina }
9654edb46e9SPaul Traina 
96627df3f5dSRui Paulo static const struct tok ipxsap_db[] = {
967cc391cceSBruce M Simpson 	{ 0x0000, "Unknown" },
968cc391cceSBruce M Simpson 	{ 0x0001, "User" },
969cc391cceSBruce M Simpson 	{ 0x0002, "User Group" },
970cc391cceSBruce M Simpson 	{ 0x0003, "PrintQueue" },
971cc391cceSBruce M Simpson 	{ 0x0004, "FileServer" },
972cc391cceSBruce M Simpson 	{ 0x0005, "JobServer" },
973cc391cceSBruce M Simpson 	{ 0x0006, "Gateway" },
974cc391cceSBruce M Simpson 	{ 0x0007, "PrintServer" },
975cc391cceSBruce M Simpson 	{ 0x0008, "ArchiveQueue" },
976cc391cceSBruce M Simpson 	{ 0x0009, "ArchiveServer" },
977cc391cceSBruce M Simpson 	{ 0x000a, "JobQueue" },
978cc391cceSBruce M Simpson 	{ 0x000b, "Administration" },
979cc391cceSBruce M Simpson 	{ 0x000F, "Novell TI-RPC" },
980cc391cceSBruce M Simpson 	{ 0x0017, "Diagnostics" },
981cc391cceSBruce M Simpson 	{ 0x0020, "NetBIOS" },
982cc391cceSBruce M Simpson 	{ 0x0021, "NAS SNA Gateway" },
983cc391cceSBruce M Simpson 	{ 0x0023, "NACS AsyncGateway" },
984cc391cceSBruce M Simpson 	{ 0x0024, "RemoteBridge/RoutingService" },
985cc391cceSBruce M Simpson 	{ 0x0026, "BridgeServer" },
986cc391cceSBruce M Simpson 	{ 0x0027, "TCP/IP Gateway" },
987cc391cceSBruce M Simpson 	{ 0x0028, "Point-to-point X.25 BridgeServer" },
988cc391cceSBruce M Simpson 	{ 0x0029, "3270 Gateway" },
989cc391cceSBruce M Simpson 	{ 0x002a, "CHI Corp" },
990cc391cceSBruce M Simpson 	{ 0x002c, "PC Chalkboard" },
991cc391cceSBruce M Simpson 	{ 0x002d, "TimeSynchServer" },
992cc391cceSBruce M Simpson 	{ 0x002e, "ARCserve5.0/PalindromeBackup" },
993cc391cceSBruce M Simpson 	{ 0x0045, "DI3270 Gateway" },
994cc391cceSBruce M Simpson 	{ 0x0047, "AdvertisingPrintServer" },
995cc391cceSBruce M Simpson 	{ 0x004a, "NetBlazerModems" },
996cc391cceSBruce M Simpson 	{ 0x004b, "BtrieveVAP" },
997cc391cceSBruce M Simpson 	{ 0x004c, "NetwareSQL" },
998cc391cceSBruce M Simpson 	{ 0x004d, "XtreeNetwork" },
999cc391cceSBruce M Simpson 	{ 0x0050, "BtrieveVAP4.11" },
1000cc391cceSBruce M Simpson 	{ 0x0052, "QuickLink" },
1001cc391cceSBruce M Simpson 	{ 0x0053, "PrintQueueUser" },
1002cc391cceSBruce M Simpson 	{ 0x0058, "Multipoint X.25 Router" },
1003cc391cceSBruce M Simpson 	{ 0x0060, "STLB/NLM" },
1004cc391cceSBruce M Simpson 	{ 0x0064, "ARCserve" },
1005cc391cceSBruce M Simpson 	{ 0x0066, "ARCserve3.0" },
1006cc391cceSBruce M Simpson 	{ 0x0072, "WAN CopyUtility" },
1007cc391cceSBruce M Simpson 	{ 0x007a, "TES-NetwareVMS" },
1008cc391cceSBruce M Simpson 	{ 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
1009cc391cceSBruce M Simpson 	{ 0x0095, "DDA OBGYN" },
1010cc391cceSBruce M Simpson 	{ 0x0098, "NetwareAccessServer" },
1011cc391cceSBruce M Simpson 	{ 0x009a, "Netware for VMS II/NamedPipeServer" },
1012cc391cceSBruce M Simpson 	{ 0x009b, "NetwareAccessServer" },
1013cc391cceSBruce M Simpson 	{ 0x009e, "PortableNetwareServer/SunLinkNVT" },
1014cc391cceSBruce M Simpson 	{ 0x00a1, "PowerchuteAPC UPS" },
1015cc391cceSBruce M Simpson 	{ 0x00aa, "LAWserve" },
1016cc391cceSBruce M Simpson 	{ 0x00ac, "CompaqIDA StatusMonitor" },
1017cc391cceSBruce M Simpson 	{ 0x0100, "PIPE STAIL" },
1018cc391cceSBruce M Simpson 	{ 0x0102, "LAN ProtectBindery" },
1019cc391cceSBruce M Simpson 	{ 0x0103, "OracleDataBaseServer" },
1020cc391cceSBruce M Simpson 	{ 0x0107, "Netware386/RSPX RemoteConsole" },
1021cc391cceSBruce M Simpson 	{ 0x010f, "NovellSNA Gateway" },
1022cc391cceSBruce M Simpson 	{ 0x0111, "TestServer" },
1023cc391cceSBruce M Simpson 	{ 0x0112, "HP PrintServer" },
1024cc391cceSBruce M Simpson 	{ 0x0114, "CSA MUX" },
1025cc391cceSBruce M Simpson 	{ 0x0115, "CSA LCA" },
1026cc391cceSBruce M Simpson 	{ 0x0116, "CSA CM" },
1027cc391cceSBruce M Simpson 	{ 0x0117, "CSA SMA" },
1028cc391cceSBruce M Simpson 	{ 0x0118, "CSA DBA" },
1029cc391cceSBruce M Simpson 	{ 0x0119, "CSA NMA" },
1030cc391cceSBruce M Simpson 	{ 0x011a, "CSA SSA" },
1031cc391cceSBruce M Simpson 	{ 0x011b, "CSA STATUS" },
1032cc391cceSBruce M Simpson 	{ 0x011e, "CSA APPC" },
1033cc391cceSBruce M Simpson 	{ 0x0126, "SNA TEST SSA Profile" },
1034cc391cceSBruce M Simpson 	{ 0x012a, "CSA TRACE" },
1035cc391cceSBruce M Simpson 	{ 0x012b, "NetwareSAA" },
1036cc391cceSBruce M Simpson 	{ 0x012e, "IKARUS VirusScan" },
1037cc391cceSBruce M Simpson 	{ 0x0130, "CommunicationsExecutive" },
1038cc391cceSBruce M Simpson 	{ 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1039cc391cceSBruce M Simpson 	{ 0x0135, "NetwareNamingServicesProfile" },
1040cc391cceSBruce M Simpson 	{ 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1041cc391cceSBruce M Simpson 	{ 0x0141, "LAN SpoolServer" },
1042cc391cceSBruce M Simpson 	{ 0x0152, "IRMALAN Gateway" },
1043cc391cceSBruce M Simpson 	{ 0x0154, "NamedPipeServer" },
1044cc391cceSBruce M Simpson 	{ 0x0166, "NetWareManagement" },
1045cc391cceSBruce M Simpson 	{ 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1046cc391cceSBruce M Simpson 	{ 0x0173, "Compaq" },
1047cc391cceSBruce M Simpson 	{ 0x0174, "Compaq SNMP Agent" },
1048cc391cceSBruce M Simpson 	{ 0x0175, "Compaq" },
1049cc391cceSBruce M Simpson 	{ 0x0180, "XTreeServer/XTreeTools" },
1050cc391cceSBruce M Simpson 	{ 0x018A, "NASI ServicesBroadcastServer" },
1051cc391cceSBruce M Simpson 	{ 0x01b0, "GARP Gateway" },
1052cc391cceSBruce M Simpson 	{ 0x01b1, "Binfview" },
1053cc391cceSBruce M Simpson 	{ 0x01bf, "IntelLanDeskManager" },
1054cc391cceSBruce M Simpson 	{ 0x01ca, "AXTEC" },
1055cc391cceSBruce M Simpson 	{ 0x01cb, "ShivaNetModem/E" },
1056cc391cceSBruce M Simpson 	{ 0x01cc, "ShivaLanRover/E" },
1057cc391cceSBruce M Simpson 	{ 0x01cd, "ShivaLanRover/T" },
1058cc391cceSBruce M Simpson 	{ 0x01ce, "ShivaUniversal" },
1059cc391cceSBruce M Simpson 	{ 0x01d8, "CastelleFAXPressServer" },
1060cc391cceSBruce M Simpson 	{ 0x01da, "CastelleLANPressPrintServer" },
1061cc391cceSBruce M Simpson 	{ 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1062cc391cceSBruce M Simpson 	{ 0x01f0, "LEGATO" },
1063cc391cceSBruce M Simpson 	{ 0x01f5, "LEGATO" },
1064cc391cceSBruce M Simpson 	{ 0x0233, "NMS Agent/NetwareManagementAgent" },
1065cc391cceSBruce M Simpson 	{ 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1066cc391cceSBruce M Simpson 	{ 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1067cc391cceSBruce M Simpson 	{ 0x023a, "LANtern" },
1068cc391cceSBruce M Simpson 	{ 0x023c, "MAVERICK" },
1069cc391cceSBruce M Simpson 	{ 0x023f, "NovellSMDR" },
1070cc391cceSBruce M Simpson 	{ 0x024e, "NetwareConnect" },
1071cc391cceSBruce M Simpson 	{ 0x024f, "NASI ServerBroadcast Cisco" },
1072cc391cceSBruce M Simpson 	{ 0x026a, "NMS ServiceConsole" },
1073cc391cceSBruce M Simpson 	{ 0x026b, "TimeSynchronizationServer Netware 4.x" },
1074cc391cceSBruce M Simpson 	{ 0x0278, "DirectoryServer Netware 4.x" },
1075cc391cceSBruce M Simpson 	{ 0x027b, "NetwareManagementAgent" },
1076cc391cceSBruce M Simpson 	{ 0x0280, "Novell File and Printer Sharing Service for PC" },
1077cc391cceSBruce M Simpson 	{ 0x0304, "NovellSAA Gateway" },
1078cc391cceSBruce M Simpson 	{ 0x0308, "COM/VERMED" },
1079cc391cceSBruce M Simpson 	{ 0x030a, "GalacticommWorldgroupServer" },
1080cc391cceSBruce M Simpson 	{ 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1081cc391cceSBruce M Simpson 	{ 0x0320, "AttachmateGateway" },
1082cc391cceSBruce M Simpson 	{ 0x0327, "MicrosoftDiagnostiocs" },
1083cc391cceSBruce M Simpson 	{ 0x0328, "WATCOM SQL Server" },
1084cc391cceSBruce M Simpson 	{ 0x0335, "MultiTechSystems MultisynchCommServer" },
1085cc391cceSBruce M Simpson 	{ 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1086cc391cceSBruce M Simpson 	{ 0x0355, "ArcadaBackupExec" },
1087cc391cceSBruce M Simpson 	{ 0x0358, "MSLCD1" },
1088cc391cceSBruce M Simpson 	{ 0x0361, "NETINELO" },
1089cc391cceSBruce M Simpson 	{ 0x037e, "Powerchute UPS Monitoring" },
1090cc391cceSBruce M Simpson 	{ 0x037f, "ViruSafeNotify" },
1091cc391cceSBruce M Simpson 	{ 0x0386, "HP Bridge" },
1092cc391cceSBruce M Simpson 	{ 0x0387, "HP Hub" },
1093cc391cceSBruce M Simpson 	{ 0x0394, "NetWare SAA Gateway" },
1094cc391cceSBruce M Simpson 	{ 0x039b, "LotusNotes" },
1095cc391cceSBruce M Simpson 	{ 0x03b7, "CertusAntiVirus" },
1096cc391cceSBruce M Simpson 	{ 0x03c4, "ARCserve4.0" },
1097cc391cceSBruce M Simpson 	{ 0x03c7, "LANspool3.5" },
1098cc391cceSBruce M Simpson 	{ 0x03d7, "LexmarkPrinterServer" },
1099cc391cceSBruce M Simpson 	{ 0x03d8, "LexmarkXLE PrinterServer" },
1100cc391cceSBruce M Simpson 	{ 0x03dd, "BanyanENS NetwareClient" },
1101cc391cceSBruce M Simpson 	{ 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1102cc391cceSBruce M Simpson 	{ 0x03e1, "UnivelUnixware" },
1103cc391cceSBruce M Simpson 	{ 0x03e4, "UnivelUnixware" },
1104cc391cceSBruce M Simpson 	{ 0x03fc, "IntelNetport" },
1105cc391cceSBruce M Simpson 	{ 0x03fd, "PrintServerQueue" },
1106cc391cceSBruce M Simpson 	{ 0x040A, "ipnServer" },
1107cc391cceSBruce M Simpson 	{ 0x040D, "LVERRMAN" },
1108cc391cceSBruce M Simpson 	{ 0x040E, "LVLIC" },
1109cc391cceSBruce M Simpson 	{ 0x0414, "NET Silicon (DPI)/Kyocera" },
1110cc391cceSBruce M Simpson 	{ 0x0429, "SiteLockVirus" },
1111cc391cceSBruce M Simpson 	{ 0x0432, "UFHELPR???" },
1112cc391cceSBruce M Simpson 	{ 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1113cc391cceSBruce M Simpson 	{ 0x0444, "MicrosoftNT SNA Server" },
1114cc391cceSBruce M Simpson 	{ 0x0448, "Oracle" },
1115cc391cceSBruce M Simpson 	{ 0x044c, "ARCserve5.01" },
1116cc391cceSBruce M Simpson 	{ 0x0457, "CanonGP55" },
1117cc391cceSBruce M Simpson 	{ 0x045a, "QMS Printers" },
1118cc391cceSBruce M Simpson 	{ 0x045b, "DellSCSI Array" },
1119cc391cceSBruce M Simpson 	{ 0x0491, "NetBlazerModems" },
1120cc391cceSBruce M Simpson 	{ 0x04ac, "OnTimeScheduler" },
1121cc391cceSBruce M Simpson 	{ 0x04b0, "CD-Net" },
1122cc391cceSBruce M Simpson 	{ 0x0513, "EmulexNQA" },
1123cc391cceSBruce M Simpson 	{ 0x0520, "SiteLockChecks" },
1124cc391cceSBruce M Simpson 	{ 0x0529, "SiteLockChecks" },
1125cc391cceSBruce M Simpson 	{ 0x052d, "CitrixOS2 AppServer" },
1126cc391cceSBruce M Simpson 	{ 0x0535, "Tektronix" },
1127cc391cceSBruce M Simpson 	{ 0x0536, "Milan" },
1128cc391cceSBruce M Simpson 	{ 0x055d, "Attachmate SNA gateway" },
1129cc391cceSBruce M Simpson 	{ 0x056b, "IBM8235 ModemServer" },
1130cc391cceSBruce M Simpson 	{ 0x056c, "ShivaLanRover/E PLUS" },
1131cc391cceSBruce M Simpson 	{ 0x056d, "ShivaLanRover/T PLUS" },
1132cc391cceSBruce M Simpson 	{ 0x0580, "McAfeeNetShield" },
1133cc391cceSBruce M Simpson 	{ 0x05B8, "NLM to workstation communication (Revelation Software)" },
1134cc391cceSBruce M Simpson 	{ 0x05BA, "CompatibleSystemsRouters" },
1135cc391cceSBruce M Simpson 	{ 0x05BE, "CheyenneHierarchicalStorageManager" },
1136cc391cceSBruce M Simpson 	{ 0x0606, "JCWatermarkImaging" },
1137cc391cceSBruce M Simpson 	{ 0x060c, "AXISNetworkPrinter" },
1138cc391cceSBruce M Simpson 	{ 0x0610, "AdaptecSCSIManagement" },
1139cc391cceSBruce M Simpson 	{ 0x0621, "IBM AntiVirus" },
1140cc391cceSBruce M Simpson 	{ 0x0640, "Windows95 RemoteRegistryService" },
1141cc391cceSBruce M Simpson 	{ 0x064e, "MicrosoftIIS" },
1142cc391cceSBruce M Simpson 	{ 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1143cc391cceSBruce M Simpson 	{ 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1144cc391cceSBruce M Simpson 	{ 0x076C, "Xerox" },
1145cc391cceSBruce M Simpson 	{ 0x079b, "ShivaLanRover/E 115" },
1146cc391cceSBruce M Simpson 	{ 0x079c, "ShivaLanRover/T 115" },
1147cc391cceSBruce M Simpson 	{ 0x07B4, "CubixWorldDesk" },
1148cc391cceSBruce M Simpson 	{ 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1149cc391cceSBruce M Simpson 	{ 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1150cc391cceSBruce M Simpson 	{ 0x0810, "ELAN License Server Demo" },
1151cc391cceSBruce M Simpson 	{ 0x0824, "ShivaLanRoverAccessSwitch/E" },
1152cc391cceSBruce M Simpson 	{ 0x086a, "ISSC Collector" },
1153cc391cceSBruce M Simpson 	{ 0x087f, "ISSC DAS AgentAIX" },
1154cc391cceSBruce M Simpson 	{ 0x0880, "Intel Netport PRO" },
1155cc391cceSBruce M Simpson 	{ 0x0881, "Intel Netport PRO" },
1156cc391cceSBruce M Simpson 	{ 0x0b29, "SiteLock" },
1157cc391cceSBruce M Simpson 	{ 0x0c29, "SiteLockApplications" },
1158cc391cceSBruce M Simpson 	{ 0x0c2c, "LicensingServer" },
1159cc391cceSBruce M Simpson 	{ 0x2101, "PerformanceTechnologyInstantInternet" },
1160cc391cceSBruce M Simpson 	{ 0x2380, "LAI SiteLock" },
1161cc391cceSBruce M Simpson 	{ 0x238c, "MeetingMaker" },
1162cc391cceSBruce M Simpson 	{ 0x4808, "SiteLockServer/SiteLockMetering" },
1163cc391cceSBruce M Simpson 	{ 0x5555, "SiteLockUser" },
1164cc391cceSBruce M Simpson 	{ 0x6312, "Tapeware" },
1165cc391cceSBruce M Simpson 	{ 0x6f00, "RabbitGateway" },
1166cc391cceSBruce M Simpson 	{ 0x7703, "MODEM" },
1167cc391cceSBruce M Simpson 	{ 0x8002, "NetPortPrinters" },
1168cc391cceSBruce M Simpson 	{ 0x8008, "WordPerfectNetworkVersion" },
1169cc391cceSBruce M Simpson 	{ 0x85BE, "Cisco EIGRP" },
1170cc391cceSBruce M Simpson 	{ 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1171cc391cceSBruce M Simpson 	{ 0x9000, "McAfeeNetShield" },
1172cc391cceSBruce M Simpson 	{ 0x9604, "CSA-NT_MON" },
1173cc391cceSBruce M Simpson 	{ 0xb6a8, "OceanIsleReachoutRemoteControl" },
1174cc391cceSBruce M Simpson 	{ 0xf11f, "SiteLockMetering" },
1175cc391cceSBruce M Simpson 	{ 0xf1ff, "SiteLock" },
1176cc391cceSBruce M Simpson 	{ 0xf503, "Microsoft SQL Server" },
1177cc391cceSBruce M Simpson 	{ 0xF905, "IBM TimeAndPlace" },
1178cc391cceSBruce M Simpson 	{ 0xfbfb, "TopCallIII FaxServer" },
1179cc391cceSBruce M Simpson 	{ 0xffff, "AnyService/Wildcard" },
1180cc391cceSBruce M Simpson 	{ 0, (char *)0 }
1181cc391cceSBruce M Simpson };
1182cc391cceSBruce M Simpson 
1183cc391cceSBruce M Simpson static void
11843340d773SGleb Smirnoff init_ipxsaparray(netdissect_options *ndo)
1185cc391cceSBruce M Simpson {
1186cc391cceSBruce M Simpson 	register int i;
1187cc391cceSBruce M Simpson 	register struct hnamemem *table;
1188cc391cceSBruce M Simpson 
1189cc391cceSBruce M Simpson 	for (i = 0; ipxsap_db[i].s != NULL; i++) {
1190cc391cceSBruce M Simpson 		int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1191cc391cceSBruce M Simpson 		table = &ipxsaptable[j];
1192cc391cceSBruce M Simpson 		while (table->name)
1193cc391cceSBruce M Simpson 			table = table->nxt;
1194cc391cceSBruce M Simpson 		table->name = ipxsap_db[i].s;
1195cc391cceSBruce M Simpson 		table->addr = htons(ipxsap_db[i].v);
11963340d773SGleb Smirnoff 		table->nxt = newhnamemem(ndo);
1197cc391cceSBruce M Simpson 	}
1198cc391cceSBruce M Simpson }
1199cc391cceSBruce M Simpson 
12004edb46e9SPaul Traina /*
12014edb46e9SPaul Traina  * Initialize the address to name translation machinery.  We map all
12023c602fabSXin LI  * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
12033c602fabSXin LI  * (i.e., to prevent blocking on the nameserver).  localnet is the IP address
12044edb46e9SPaul Traina  * of the local network.  mask is its subnet mask.
12054edb46e9SPaul Traina  */
12064edb46e9SPaul Traina void
12073c602fabSXin LI init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
12084edb46e9SPaul Traina {
12093c602fabSXin LI 	if (ndo->ndo_fflag) {
12104edb46e9SPaul Traina 		f_localnet = localnet;
12114edb46e9SPaul Traina 		f_netmask = mask;
12124edb46e9SPaul Traina 	}
12133c602fabSXin LI 	if (ndo->ndo_nflag)
12144edb46e9SPaul Traina 		/*
12154edb46e9SPaul Traina 		 * Simplest way to suppress names.
12164edb46e9SPaul Traina 		 */
12174edb46e9SPaul Traina 		return;
12184edb46e9SPaul Traina 
12193340d773SGleb Smirnoff 	init_etherarray(ndo);
12203c602fabSXin LI 	init_servarray(ndo);
12213340d773SGleb Smirnoff 	init_eprotoarray(ndo);
12223340d773SGleb Smirnoff 	init_protoidarray(ndo);
12233340d773SGleb Smirnoff 	init_ipxsaparray(ndo);
12244edb46e9SPaul Traina }
12254edb46e9SPaul Traina 
1226a1c2090eSBill Fenner const char *
12273c602fabSXin LI dnaddr_string(netdissect_options *ndo, u_short dnaddr)
12284edb46e9SPaul Traina {
12294edb46e9SPaul Traina 	register struct hnamemem *tp;
12304edb46e9SPaul Traina 
12313340d773SGleb Smirnoff 	for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
12324edb46e9SPaul Traina 	     tp = tp->nxt)
12334edb46e9SPaul Traina 		if (tp->addr == dnaddr)
12344edb46e9SPaul Traina 			return (tp->name);
12354edb46e9SPaul Traina 
12364edb46e9SPaul Traina 	tp->addr = dnaddr;
12373340d773SGleb Smirnoff 	tp->nxt = newhnamemem(ndo);
12383c602fabSXin LI 	if (ndo->ndo_nflag)
12393340d773SGleb Smirnoff 		tp->name = dnnum_string(ndo, dnaddr);
12404edb46e9SPaul Traina 	else
12413340d773SGleb Smirnoff 		tp->name = dnname_string(ndo, dnaddr);
12424edb46e9SPaul Traina 
12434edb46e9SPaul Traina 	return(tp->name);
12444edb46e9SPaul Traina }
12454edb46e9SPaul Traina 
12464edb46e9SPaul Traina /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
12474edb46e9SPaul Traina struct hnamemem *
12483340d773SGleb Smirnoff newhnamemem(netdissect_options *ndo)
12494edb46e9SPaul Traina {
12504edb46e9SPaul Traina 	register struct hnamemem *p;
12514edb46e9SPaul Traina 	static struct hnamemem *ptr = NULL;
12524edb46e9SPaul Traina 	static u_int num = 0;
12534edb46e9SPaul Traina 
12544edb46e9SPaul Traina 	if (num  <= 0) {
12554edb46e9SPaul Traina 		num = 64;
12564edb46e9SPaul Traina 		ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
12574edb46e9SPaul Traina 		if (ptr == NULL)
12583340d773SGleb Smirnoff 			(*ndo->ndo_error)(ndo, "newhnamemem: calloc");
12594edb46e9SPaul Traina 	}
12604edb46e9SPaul Traina 	--num;
12614edb46e9SPaul Traina 	p = ptr++;
12624edb46e9SPaul Traina 	return (p);
12634edb46e9SPaul Traina }
1264a88113a8SBill Fenner 
1265a88113a8SBill Fenner /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1266a88113a8SBill Fenner struct h6namemem *
12673340d773SGleb Smirnoff newh6namemem(netdissect_options *ndo)
1268a88113a8SBill Fenner {
1269a88113a8SBill Fenner 	register struct h6namemem *p;
1270a88113a8SBill Fenner 	static struct h6namemem *ptr = NULL;
1271a88113a8SBill Fenner 	static u_int num = 0;
1272a88113a8SBill Fenner 
1273a88113a8SBill Fenner 	if (num  <= 0) {
1274a88113a8SBill Fenner 		num = 64;
1275a88113a8SBill Fenner 		ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1276a88113a8SBill Fenner 		if (ptr == NULL)
12773340d773SGleb Smirnoff 			(*ndo->ndo_error)(ndo, "newh6namemem: calloc");
1278a88113a8SBill Fenner 	}
1279a88113a8SBill Fenner 	--num;
1280a88113a8SBill Fenner 	p = ptr++;
1281a88113a8SBill Fenner 	return (p);
1282a88113a8SBill Fenner }
12838bdc5a62SPatrick Kelsey 
12848bdc5a62SPatrick Kelsey /* Represent TCI part of the 802.1Q 4-octet tag as text. */
12858bdc5a62SPatrick Kelsey const char *
12868bdc5a62SPatrick Kelsey ieee8021q_tci_string(const uint16_t tci)
12878bdc5a62SPatrick Kelsey {
12888bdc5a62SPatrick Kelsey 	static char buf[128];
12898bdc5a62SPatrick Kelsey 	snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
12908bdc5a62SPatrick Kelsey 	         tci & 0xfff,
12918bdc5a62SPatrick Kelsey 	         tci >> 13,
12928bdc5a62SPatrick Kelsey 	         (tci & 0x1000) ? ", DEI" : "");
12938bdc5a62SPatrick Kelsey 	return buf;
12948bdc5a62SPatrick Kelsey }
1295