xref: /freebsd/contrib/tcpdump/addrtoname.c (revision 3c602fabf9b894ff79f08a80cbb7ad3b1eb84e62)
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
23a88113a8SBill Fenner  *
24a88113a8SBill Fenner  * $FreeBSD$
254edb46e9SPaul Traina  */
26a88113a8SBill Fenner 
27*3c602fabSXin LI #define NETDISSECT_REWORKED
28a88113a8SBill Fenner #ifdef HAVE_CONFIG_H
29a88113a8SBill Fenner #include "config.h"
304edb46e9SPaul Traina #endif
314edb46e9SPaul Traina 
32*3c602fabSXin LI #ifdef HAVE_CAPSICUM
33197731f6SPawel Jakub Dawidek #include <libcapsicum.h>
34197731f6SPawel Jakub Dawidek #include <libcapsicum_dns.h>
35*3c602fabSXin LI #endif /* HAVE_CAPSICUM */
36cc391cceSBruce M Simpson #include <tcpdump-stdinc.h>
370e0def19SBill Fenner 
380e0def19SBill Fenner #ifdef USE_ETHER_NTOHOST
39943ee2b1SBill Fenner #ifdef HAVE_NETINET_IF_ETHER_H
400e0def19SBill Fenner struct mbuf;		/* Squelch compiler warnings on some platforms for */
410e0def19SBill Fenner struct rtentry;		/* declarations in <net/if.h> */
420e0def19SBill Fenner #include <net/if.h>	/* for "struct ifnet" in "struct arpcom" on Solaris */
43943ee2b1SBill Fenner #include <netinet/if_ether.h>
440e0def19SBill Fenner #endif /* HAVE_NETINET_IF_ETHER_H */
45c1ad1296SSam Leffler #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
46c1ad1296SSam Leffler #include <netinet/ether.h>
47c1ad1296SSam Leffler #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
48a88113a8SBill Fenner 
49c1ad1296SSam Leffler #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
5029292c17SSam Leffler #ifndef HAVE_STRUCT_ETHER_ADDR
5129292c17SSam Leffler struct ether_addr {
5229292c17SSam Leffler 	unsigned char ether_addr_octet[6];
5329292c17SSam Leffler };
5429292c17SSam Leffler #endif
55c1ad1296SSam Leffler extern int ether_ntohost(char *, const struct ether_addr *);
56c1ad1296SSam Leffler #endif
57c1ad1296SSam Leffler 
5829292c17SSam Leffler #endif /* USE_ETHER_NTOHOST */
5929292c17SSam Leffler 
604edb46e9SPaul Traina #include <pcap.h>
614edb46e9SPaul Traina #include <pcap-namedb.h>
624edb46e9SPaul Traina #include <signal.h>
634edb46e9SPaul Traina #include <stdio.h>
644edb46e9SPaul Traina #include <string.h>
654edb46e9SPaul Traina #include <stdlib.h>
664edb46e9SPaul Traina 
674edb46e9SPaul Traina #include "interface.h"
684edb46e9SPaul Traina #include "addrtoname.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  *
81cc391cceSBruce M Simpson  * XXX there has to be error checks against strdup(3) failure
824edb46e9SPaul Traina  */
834edb46e9SPaul Traina 
844edb46e9SPaul Traina #define HASHNAMESIZE 4096
854edb46e9SPaul Traina 
864edb46e9SPaul Traina struct hnamemem {
87*3c602fabSXin 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 
99cc391cceSBruce M Simpson #if defined(INET6) && defined(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
137cc391cceSBruce M Simpson #endif /* INET6 & WIN32 */
1384edb46e9SPaul Traina 
139a88113a8SBill Fenner #ifdef INET6
140a88113a8SBill Fenner struct h6namemem {
141a88113a8SBill Fenner 	struct in6_addr addr;
142a88113a8SBill Fenner 	char *name;
143a88113a8SBill Fenner 	struct h6namemem *nxt;
144a88113a8SBill Fenner };
145a88113a8SBill Fenner 
14627df3f5dSRui Paulo static struct h6namemem h6nametable[HASHNAMESIZE];
147a88113a8SBill Fenner #endif /* INET6 */
148a88113a8SBill Fenner 
1494edb46e9SPaul Traina struct enamemem {
1504edb46e9SPaul Traina 	u_short e_addr0;
1514edb46e9SPaul Traina 	u_short e_addr1;
1524edb46e9SPaul Traina 	u_short e_addr2;
153a1c2090eSBill Fenner 	const char *e_name;
1544edb46e9SPaul Traina 	u_char *e_nsap;			/* used only for nsaptable[] */
155a1c2090eSBill Fenner #define e_bs e_nsap			/* for bytestringtable */
1564edb46e9SPaul Traina 	struct enamemem *e_nxt;
1574edb46e9SPaul Traina };
1584edb46e9SPaul Traina 
15927df3f5dSRui Paulo static struct enamemem enametable[HASHNAMESIZE];
16027df3f5dSRui Paulo static struct enamemem nsaptable[HASHNAMESIZE];
16127df3f5dSRui Paulo static struct enamemem bytestringtable[HASHNAMESIZE];
1624edb46e9SPaul Traina 
1634edb46e9SPaul Traina struct protoidmem {
164*3c602fabSXin LI 	uint32_t p_oui;
1654edb46e9SPaul Traina 	u_short p_proto;
166a1c2090eSBill Fenner 	const char *p_name;
1674edb46e9SPaul Traina 	struct protoidmem *p_nxt;
1684edb46e9SPaul Traina };
1694edb46e9SPaul Traina 
17027df3f5dSRui Paulo static struct protoidmem protoidtable[HASHNAMESIZE];
1714edb46e9SPaul Traina 
1724edb46e9SPaul Traina /*
1734edb46e9SPaul Traina  * A faster replacement for inet_ntoa().
1744edb46e9SPaul Traina  */
175a1c2090eSBill Fenner const char *
176*3c602fabSXin LI intoa(uint32_t addr)
1774edb46e9SPaul Traina {
1784edb46e9SPaul Traina 	register char *cp;
1794edb46e9SPaul Traina 	register u_int byte;
1804edb46e9SPaul Traina 	register int n;
1814edb46e9SPaul Traina 	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
1824edb46e9SPaul Traina 
1834edb46e9SPaul Traina 	NTOHL(addr);
18429292c17SSam Leffler 	cp = buf + sizeof(buf);
1854edb46e9SPaul Traina 	*--cp = '\0';
1864edb46e9SPaul Traina 
1874edb46e9SPaul Traina 	n = 4;
1884edb46e9SPaul Traina 	do {
1894edb46e9SPaul Traina 		byte = addr & 0xff;
1904edb46e9SPaul Traina 		*--cp = byte % 10 + '0';
1914edb46e9SPaul Traina 		byte /= 10;
1924edb46e9SPaul Traina 		if (byte > 0) {
1934edb46e9SPaul Traina 			*--cp = byte % 10 + '0';
1944edb46e9SPaul Traina 			byte /= 10;
1954edb46e9SPaul Traina 			if (byte > 0)
1964edb46e9SPaul Traina 				*--cp = byte + '0';
1974edb46e9SPaul Traina 		}
1984edb46e9SPaul Traina 		*--cp = '.';
1994edb46e9SPaul Traina 		addr >>= 8;
2004edb46e9SPaul Traina 	} while (--n > 0);
2014edb46e9SPaul Traina 
2024edb46e9SPaul Traina 	return cp + 1;
2034edb46e9SPaul Traina }
2044edb46e9SPaul Traina 
205*3c602fabSXin LI static uint32_t f_netmask;
206*3c602fabSXin LI static uint32_t f_localnet;
207*3c602fabSXin LI #ifdef HAVE_CAPSICUM
208197731f6SPawel Jakub Dawidek extern cap_channel_t *capdns;
209197731f6SPawel Jakub Dawidek #endif
2104edb46e9SPaul Traina 
2114edb46e9SPaul Traina /*
2124edb46e9SPaul Traina  * Return a name for the IP address pointed to by ap.  This address
2134edb46e9SPaul Traina  * is assumed to be in network byte order.
214cc391cceSBruce M Simpson  *
215cc391cceSBruce M Simpson  * NOTE: ap is *NOT* necessarily part of the packet data (not even if
216cc391cceSBruce M Simpson  * this is being called with the "ipaddr_string()" macro), so you
217cc391cceSBruce M Simpson  * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it.  Furthermore,
218cc391cceSBruce M Simpson  * even in cases where it *is* part of the packet data, the caller
219cc391cceSBruce M Simpson  * would still have to check for a null return value, even if it's
220cc391cceSBruce M Simpson  * just printing the return value with "%s" - not all versions of
221cc391cceSBruce M Simpson  * printf print "(null)" with "%s" and a null pointer, some of them
222cc391cceSBruce M Simpson  * don't check for a null pointer and crash in that case.
223cc391cceSBruce M Simpson  *
224cc391cceSBruce M Simpson  * The callers of this routine should, before handing this routine
225cc391cceSBruce M Simpson  * a pointer to packet data, be sure that the data is present in
226cc391cceSBruce M Simpson  * the packet buffer.  They should probably do those checks anyway,
227cc391cceSBruce M Simpson  * as other data at that layer might not be IP addresses, and it
228cc391cceSBruce M Simpson  * also needs to check whether they're present in the packet buffer.
2294edb46e9SPaul Traina  */
230a1c2090eSBill Fenner const char *
231*3c602fabSXin LI getname(netdissect_options *ndo, const u_char *ap)
2324edb46e9SPaul Traina {
2334edb46e9SPaul Traina 	register struct hostent *hp;
234*3c602fabSXin LI 	uint32_t addr;
2354edb46e9SPaul Traina 	static struct hnamemem *p;		/* static for longjmp() */
2364edb46e9SPaul Traina 
237a88113a8SBill Fenner 	memcpy(&addr, ap, sizeof(addr));
2384edb46e9SPaul Traina 	p = &hnametable[addr & (HASHNAMESIZE-1)];
2394edb46e9SPaul Traina 	for (; p->nxt; p = p->nxt) {
2404edb46e9SPaul Traina 		if (p->addr == addr)
2414edb46e9SPaul Traina 			return (p->name);
2424edb46e9SPaul Traina 	}
2434edb46e9SPaul Traina 	p->addr = addr;
2444edb46e9SPaul Traina 	p->nxt = newhnamemem();
2454edb46e9SPaul Traina 
2464edb46e9SPaul Traina 	/*
247cc391cceSBruce M Simpson 	 * Print names unless:
248cc391cceSBruce M Simpson 	 *	(1) -n was given.
249699fc314SBill Fenner 	 *      (2) Address is foreign and -f was given. (If -f was not
250cc391cceSBruce M Simpson 	 *	    given, f_netmask and f_localnet are 0 and the test
251699fc314SBill Fenner 	 *	    evaluates to true)
2524edb46e9SPaul Traina 	 */
253*3c602fabSXin LI 	if (!ndo->ndo_nflag &&
254cc391cceSBruce M Simpson 	    (addr & f_netmask) == f_localnet) {
255*3c602fabSXin LI #ifdef HAVE_CAPSICUM
256197731f6SPawel Jakub Dawidek 		if (capdns != NULL) {
257197731f6SPawel Jakub Dawidek 			hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
258197731f6SPawel Jakub Dawidek 			    AF_INET);
259197731f6SPawel Jakub Dawidek 		} else
260197731f6SPawel Jakub Dawidek #endif
2614edb46e9SPaul Traina 			hp = gethostbyaddr((char *)&addr, 4, AF_INET);
2624edb46e9SPaul Traina 		if (hp) {
2634edb46e9SPaul Traina 			char *dotp;
2644edb46e9SPaul Traina 
265a1c2090eSBill Fenner 			p->name = strdup(hp->h_name);
266*3c602fabSXin LI 			if (ndo->ndo_Nflag) {
2674edb46e9SPaul Traina 				/* Remove domain qualifications */
2684edb46e9SPaul Traina 				dotp = strchr(p->name, '.');
2694edb46e9SPaul Traina 				if (dotp)
2704edb46e9SPaul Traina 					*dotp = '\0';
2714edb46e9SPaul Traina 			}
2724edb46e9SPaul Traina 			return (p->name);
2734edb46e9SPaul Traina 		}
2744edb46e9SPaul Traina 	}
275a1c2090eSBill Fenner 	p->name = strdup(intoa(addr));
2764edb46e9SPaul Traina 	return (p->name);
2774edb46e9SPaul Traina }
2784edb46e9SPaul Traina 
279a88113a8SBill Fenner #ifdef INET6
280a88113a8SBill Fenner /*
281a88113a8SBill Fenner  * Return a name for the IP6 address pointed to by ap.  This address
282a88113a8SBill Fenner  * is assumed to be in network byte order.
283a88113a8SBill Fenner  */
284a1c2090eSBill Fenner const char *
285*3c602fabSXin LI getname6(netdissect_options *ndo, const u_char *ap)
286a88113a8SBill Fenner {
287a88113a8SBill Fenner 	register struct hostent *hp;
288*3c602fabSXin LI 	union {
289a88113a8SBill Fenner 		struct in6_addr addr;
290*3c602fabSXin LI 		struct for_hash_addr {
291*3c602fabSXin LI 			char fill[14];
292*3c602fabSXin LI 			uint16_t d;
293*3c602fabSXin LI 		} addra;
294*3c602fabSXin LI 	} addr;
295a88113a8SBill Fenner 	static struct h6namemem *p;		/* static for longjmp() */
296a1c2090eSBill Fenner 	register const char *cp;
297a88113a8SBill Fenner 	char ntop_buf[INET6_ADDRSTRLEN];
298a88113a8SBill Fenner 
299a88113a8SBill Fenner 	memcpy(&addr, ap, sizeof(addr));
300*3c602fabSXin LI 	p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
301a88113a8SBill Fenner 	for (; p->nxt; p = p->nxt) {
302a88113a8SBill Fenner 		if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
303a88113a8SBill Fenner 			return (p->name);
304a88113a8SBill Fenner 	}
305*3c602fabSXin LI 	p->addr = addr.addr;
306a88113a8SBill Fenner 	p->nxt = newh6namemem();
307a88113a8SBill Fenner 
308a88113a8SBill Fenner 	/*
309cc391cceSBruce M Simpson 	 * Do not print names if -n was given.
310a88113a8SBill Fenner 	 */
311*3c602fabSXin LI 	if (!ndo->ndo_nflag) {
312*3c602fabSXin LI #ifdef HAVE_CAPSICUM
313197731f6SPawel Jakub Dawidek 		if (capdns != NULL) {
314197731f6SPawel Jakub Dawidek 			hp = cap_gethostbyaddr(capdns, (char *)&addr,
315197731f6SPawel Jakub Dawidek 			    sizeof(addr), AF_INET6);
316197731f6SPawel Jakub Dawidek 		} else
317197731f6SPawel Jakub Dawidek #endif
318a88113a8SBill Fenner 			hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
319a88113a8SBill Fenner 		if (hp) {
320a88113a8SBill Fenner 			char *dotp;
321a88113a8SBill Fenner 
322a1c2090eSBill Fenner 			p->name = strdup(hp->h_name);
323*3c602fabSXin LI 			if (ndo->ndo_Nflag) {
324a88113a8SBill Fenner 				/* Remove domain qualifications */
325a88113a8SBill Fenner 				dotp = strchr(p->name, '.');
326a88113a8SBill Fenner 				if (dotp)
327a88113a8SBill Fenner 					*dotp = '\0';
328a88113a8SBill Fenner 			}
329a88113a8SBill Fenner 			return (p->name);
330a88113a8SBill Fenner 		}
331a88113a8SBill Fenner 	}
332a1c2090eSBill Fenner 	cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
333a1c2090eSBill Fenner 	p->name = strdup(cp);
334a88113a8SBill Fenner 	return (p->name);
335a88113a8SBill Fenner }
336a88113a8SBill Fenner #endif /* INET6 */
337a88113a8SBill Fenner 
33827df3f5dSRui Paulo static const char hex[] = "0123456789abcdef";
3394edb46e9SPaul Traina 
3404edb46e9SPaul Traina 
3414edb46e9SPaul Traina /* Find the hash node that corresponds the ether address 'ep' */
3424edb46e9SPaul Traina 
3434edb46e9SPaul Traina static inline struct enamemem *
3444edb46e9SPaul Traina lookup_emem(const u_char *ep)
3454edb46e9SPaul Traina {
3464edb46e9SPaul Traina 	register u_int i, j, k;
3474edb46e9SPaul Traina 	struct enamemem *tp;
3484edb46e9SPaul Traina 
3494edb46e9SPaul Traina 	k = (ep[0] << 8) | ep[1];
3504edb46e9SPaul Traina 	j = (ep[2] << 8) | ep[3];
3514edb46e9SPaul Traina 	i = (ep[4] << 8) | ep[5];
3524edb46e9SPaul Traina 
3534edb46e9SPaul Traina 	tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
3544edb46e9SPaul Traina 	while (tp->e_nxt)
3554edb46e9SPaul Traina 		if (tp->e_addr0 == i &&
3564edb46e9SPaul Traina 		    tp->e_addr1 == j &&
3574edb46e9SPaul Traina 		    tp->e_addr2 == k)
3584edb46e9SPaul Traina 			return tp;
3594edb46e9SPaul Traina 		else
3604edb46e9SPaul Traina 			tp = tp->e_nxt;
3614edb46e9SPaul Traina 	tp->e_addr0 = i;
3624edb46e9SPaul Traina 	tp->e_addr1 = j;
3634edb46e9SPaul Traina 	tp->e_addr2 = k;
3644edb46e9SPaul Traina 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
3654edb46e9SPaul Traina 	if (tp->e_nxt == NULL)
3664edb46e9SPaul Traina 		error("lookup_emem: calloc");
3674edb46e9SPaul Traina 
3684edb46e9SPaul Traina 	return tp;
3694edb46e9SPaul Traina }
3704edb46e9SPaul Traina 
371a1c2090eSBill Fenner /*
372a1c2090eSBill Fenner  * Find the hash node that corresponds to the bytestring 'bs'
373a1c2090eSBill Fenner  * with length 'nlen'
374a1c2090eSBill Fenner  */
375a1c2090eSBill Fenner 
376a1c2090eSBill Fenner static inline struct enamemem *
377a1c2090eSBill Fenner lookup_bytestring(register const u_char *bs, const unsigned int nlen)
378a1c2090eSBill Fenner {
379a1c2090eSBill Fenner 	struct enamemem *tp;
380a1c2090eSBill Fenner 	register u_int i, j, k;
381a1c2090eSBill Fenner 
382a1c2090eSBill Fenner 	if (nlen >= 6) {
383a1c2090eSBill Fenner 		k = (bs[0] << 8) | bs[1];
384a1c2090eSBill Fenner 		j = (bs[2] << 8) | bs[3];
385a1c2090eSBill Fenner 		i = (bs[4] << 8) | bs[5];
386a1c2090eSBill Fenner 	} else if (nlen >= 4) {
387a1c2090eSBill Fenner 		k = (bs[0] << 8) | bs[1];
388a1c2090eSBill Fenner 		j = (bs[2] << 8) | bs[3];
389a1c2090eSBill Fenner 		i = 0;
390a1c2090eSBill Fenner 	} else
391a1c2090eSBill Fenner 		i = j = k = 0;
392a1c2090eSBill Fenner 
393a1c2090eSBill Fenner 	tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
394a1c2090eSBill Fenner 	while (tp->e_nxt)
395a1c2090eSBill Fenner 		if (tp->e_addr0 == i &&
396a1c2090eSBill Fenner 		    tp->e_addr1 == j &&
397a1c2090eSBill Fenner 		    tp->e_addr2 == k &&
398a1c2090eSBill Fenner 		    memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
399a1c2090eSBill Fenner 			return tp;
400a1c2090eSBill Fenner 		else
401a1c2090eSBill Fenner 			tp = tp->e_nxt;
402a1c2090eSBill Fenner 
403a1c2090eSBill Fenner 	tp->e_addr0 = i;
404a1c2090eSBill Fenner 	tp->e_addr1 = j;
405a1c2090eSBill Fenner 	tp->e_addr2 = k;
406a1c2090eSBill Fenner 
407a1c2090eSBill Fenner 	tp->e_bs = (u_char *) calloc(1, nlen + 1);
408d03c0883SXin LI 	if (tp->e_bs == NULL)
409d03c0883SXin LI 		error("lookup_bytestring: calloc");
410d03c0883SXin LI 
411a1c2090eSBill Fenner 	memcpy(tp->e_bs, bs, nlen);
412a1c2090eSBill Fenner 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
413a1c2090eSBill Fenner 	if (tp->e_nxt == NULL)
414a1c2090eSBill Fenner 		error("lookup_bytestring: calloc");
415a1c2090eSBill Fenner 
416a1c2090eSBill Fenner 	return tp;
417a1c2090eSBill Fenner }
418a1c2090eSBill Fenner 
4194edb46e9SPaul Traina /* Find the hash node that corresponds the NSAP 'nsap' */
4204edb46e9SPaul Traina 
4214edb46e9SPaul Traina static inline struct enamemem *
4224edb46e9SPaul Traina lookup_nsap(register const u_char *nsap)
4234edb46e9SPaul Traina {
4244edb46e9SPaul Traina 	register u_int i, j, k;
425a1c2090eSBill Fenner 	unsigned int nlen = *nsap;
4264edb46e9SPaul Traina 	struct enamemem *tp;
4274edb46e9SPaul Traina 	const u_char *ensap = nsap + nlen - 6;
4284edb46e9SPaul Traina 
4294edb46e9SPaul Traina 	if (nlen > 6) {
4304edb46e9SPaul Traina 		k = (ensap[0] << 8) | ensap[1];
4314edb46e9SPaul Traina 		j = (ensap[2] << 8) | ensap[3];
4324edb46e9SPaul Traina 		i = (ensap[4] << 8) | ensap[5];
4334edb46e9SPaul Traina 	}
4344edb46e9SPaul Traina 	else
4354edb46e9SPaul Traina 		i = j = k = 0;
4364edb46e9SPaul Traina 
4374edb46e9SPaul Traina 	tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
4384edb46e9SPaul Traina 	while (tp->e_nxt)
4394edb46e9SPaul Traina 		if (tp->e_addr0 == i &&
4404edb46e9SPaul Traina 		    tp->e_addr1 == j &&
4414edb46e9SPaul Traina 		    tp->e_addr2 == k &&
4424edb46e9SPaul Traina 		    tp->e_nsap[0] == nlen &&
443a1c2090eSBill Fenner 		    memcmp((const char *)&(nsap[1]),
4444edb46e9SPaul Traina 			(char *)&(tp->e_nsap[1]), nlen) == 0)
4454edb46e9SPaul Traina 			return tp;
4464edb46e9SPaul Traina 		else
4474edb46e9SPaul Traina 			tp = tp->e_nxt;
4484edb46e9SPaul Traina 	tp->e_addr0 = i;
4494edb46e9SPaul Traina 	tp->e_addr1 = j;
4504edb46e9SPaul Traina 	tp->e_addr2 = k;
4514edb46e9SPaul Traina 	tp->e_nsap = (u_char *)malloc(nlen + 1);
4524edb46e9SPaul Traina 	if (tp->e_nsap == NULL)
4534edb46e9SPaul Traina 		error("lookup_nsap: malloc");
454a1c2090eSBill Fenner 	memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
4554edb46e9SPaul Traina 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
4564edb46e9SPaul Traina 	if (tp->e_nxt == NULL)
4574edb46e9SPaul Traina 		error("lookup_nsap: calloc");
4584edb46e9SPaul Traina 
4594edb46e9SPaul Traina 	return tp;
4604edb46e9SPaul Traina }
4614edb46e9SPaul Traina 
4624edb46e9SPaul Traina /* Find the hash node that corresponds the protoid 'pi'. */
4634edb46e9SPaul Traina 
4644edb46e9SPaul Traina static inline struct protoidmem *
4654edb46e9SPaul Traina lookup_protoid(const u_char *pi)
4664edb46e9SPaul Traina {
4674edb46e9SPaul Traina 	register u_int i, j;
4684edb46e9SPaul Traina 	struct protoidmem *tp;
4694edb46e9SPaul Traina 
4704edb46e9SPaul Traina 	/* 5 octets won't be aligned */
4714edb46e9SPaul Traina 	i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
4724edb46e9SPaul Traina 	j =   (pi[3] << 8) + pi[4];
4734edb46e9SPaul Traina 	/* XXX should be endian-insensitive, but do big-endian testing  XXX */
4744edb46e9SPaul Traina 
4754edb46e9SPaul Traina 	tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
4764edb46e9SPaul Traina 	while (tp->p_nxt)
4774edb46e9SPaul Traina 		if (tp->p_oui == i && tp->p_proto == j)
4784edb46e9SPaul Traina 			return tp;
4794edb46e9SPaul Traina 		else
4804edb46e9SPaul Traina 			tp = tp->p_nxt;
4814edb46e9SPaul Traina 	tp->p_oui = i;
4824edb46e9SPaul Traina 	tp->p_proto = j;
4834edb46e9SPaul Traina 	tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
4844edb46e9SPaul Traina 	if (tp->p_nxt == NULL)
4854edb46e9SPaul Traina 		error("lookup_protoid: calloc");
4864edb46e9SPaul Traina 
4874edb46e9SPaul Traina 	return tp;
4884edb46e9SPaul Traina }
4894edb46e9SPaul Traina 
490a1c2090eSBill Fenner const char *
491*3c602fabSXin LI etheraddr_string(netdissect_options *ndo, register const u_char *ep)
4924edb46e9SPaul Traina {
49317cb103cSSam Leffler 	register int i;
4944edb46e9SPaul Traina 	register char *cp;
4954edb46e9SPaul Traina 	register struct enamemem *tp;
49617cb103cSSam Leffler 	int oui;
49729292c17SSam Leffler 	char buf[BUFSIZE];
4984edb46e9SPaul Traina 
4994edb46e9SPaul Traina 	tp = lookup_emem(ep);
5004edb46e9SPaul Traina 	if (tp->e_name)
5014edb46e9SPaul Traina 		return (tp->e_name);
502a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
503*3c602fabSXin LI 	if (!ndo->ndo_nflag) {
50429292c17SSam Leffler 		char buf2[BUFSIZE];
505c1ad1296SSam Leffler 
506c1ad1296SSam Leffler 		/*
507c1ad1296SSam Leffler 		 * We don't cast it to "const struct ether_addr *"
50817cb103cSSam Leffler 		 * because some systems fail to declare the second
50917cb103cSSam Leffler 		 * argument as a "const" pointer, even though they
51017cb103cSSam Leffler 		 * don't modify what it points to.
511c1ad1296SSam Leffler 		 */
512c1ad1296SSam Leffler 		if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
513cc391cceSBruce M Simpson 			tp->e_name = strdup(buf2);
5144edb46e9SPaul Traina 			return (tp->e_name);
5154edb46e9SPaul Traina 		}
5164edb46e9SPaul Traina 	}
5174edb46e9SPaul Traina #endif
5184edb46e9SPaul Traina 	cp = buf;
51929292c17SSam Leffler 	oui = EXTRACT_24BITS(ep);
520cc391cceSBruce M Simpson 	*cp++ = hex[*ep >> 4 ];
5214edb46e9SPaul Traina 	*cp++ = hex[*ep++ & 0xf];
52217cb103cSSam Leffler 	for (i = 5; --i >= 0;) {
5234edb46e9SPaul Traina 		*cp++ = ':';
524cc391cceSBruce M Simpson 		*cp++ = hex[*ep >> 4 ];
5254edb46e9SPaul Traina 		*cp++ = hex[*ep++ & 0xf];
5264edb46e9SPaul Traina 	}
52729292c17SSam Leffler 
528*3c602fabSXin LI 	if (!ndo->ndo_nflag) {
52917cb103cSSam Leffler 		snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
53029292c17SSam Leffler 		    tok2str(oui_values, "Unknown", oui));
53129292c17SSam Leffler 	} else
5324edb46e9SPaul Traina 		*cp = '\0';
533a1c2090eSBill Fenner 	tp->e_name = strdup(buf);
5344edb46e9SPaul Traina 	return (tp->e_name);
5354edb46e9SPaul Traina }
5364edb46e9SPaul Traina 
537a1c2090eSBill Fenner const char *
538cac3dcd5SXin LI le64addr_string(const u_char *ep)
539cac3dcd5SXin LI {
540cac3dcd5SXin LI 	const unsigned int len = 8;
541cac3dcd5SXin LI 	register u_int i;
542cac3dcd5SXin LI 	register char *cp;
543cac3dcd5SXin LI 	register struct enamemem *tp;
544cac3dcd5SXin LI 	char buf[BUFSIZE];
545cac3dcd5SXin LI 
546cac3dcd5SXin LI 	tp = lookup_bytestring(ep, len);
547cac3dcd5SXin LI 	if (tp->e_name)
548cac3dcd5SXin LI 		return (tp->e_name);
549cac3dcd5SXin LI 
550cac3dcd5SXin LI 	cp = buf;
551cac3dcd5SXin LI 	for (i = len; i > 0 ; --i) {
552cac3dcd5SXin LI 		*cp++ = hex[*(ep + i - 1) >> 4];
553cac3dcd5SXin LI 		*cp++ = hex[*(ep + i - 1) & 0xf];
554cac3dcd5SXin LI 		*cp++ = ':';
555cac3dcd5SXin LI 	}
556cac3dcd5SXin LI 	cp --;
557cac3dcd5SXin LI 
558cac3dcd5SXin LI 	*cp = '\0';
559cac3dcd5SXin LI 
560cac3dcd5SXin LI 	tp->e_name = strdup(buf);
561cac3dcd5SXin LI 
562cac3dcd5SXin LI 	return (tp->e_name);
563cac3dcd5SXin LI }
564cac3dcd5SXin LI 
565cac3dcd5SXin LI const char *
566*3c602fabSXin LI linkaddr_string(netdissect_options *ndo, const u_char *ep, const unsigned int type, const unsigned int len)
567a1c2090eSBill Fenner {
568c1ad1296SSam Leffler 	register u_int i;
569a1c2090eSBill Fenner 	register char *cp;
570a1c2090eSBill Fenner 	register struct enamemem *tp;
571a1c2090eSBill Fenner 
57227df3f5dSRui Paulo 	if (len == 0)
57327df3f5dSRui Paulo 		return ("<empty>");
574a5779b6eSRui Paulo 
57527df3f5dSRui Paulo 	if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
576*3c602fabSXin LI 		return (etheraddr_string(ndo, ep));
57727df3f5dSRui Paulo 
57827df3f5dSRui Paulo 	if (type == LINKADDR_FRELAY)
57927df3f5dSRui Paulo 		return (q922_string(ep));
580a1c2090eSBill Fenner 
581a1c2090eSBill Fenner 	tp = lookup_bytestring(ep, len);
582a1c2090eSBill Fenner 	if (tp->e_name)
583a1c2090eSBill Fenner 		return (tp->e_name);
584a1c2090eSBill Fenner 
585a1c2090eSBill Fenner 	tp->e_name = cp = (char *)malloc(len*3);
586a1c2090eSBill Fenner 	if (tp->e_name == NULL)
587a1c2090eSBill Fenner 		error("linkaddr_string: malloc");
588c1ad1296SSam Leffler 	*cp++ = hex[*ep >> 4];
589a1c2090eSBill Fenner 	*cp++ = hex[*ep++ & 0xf];
590a1c2090eSBill Fenner 	for (i = len-1; i > 0 ; --i) {
591a1c2090eSBill Fenner 		*cp++ = ':';
592c1ad1296SSam Leffler 		*cp++ = hex[*ep >> 4];
593a1c2090eSBill Fenner 		*cp++ = hex[*ep++ & 0xf];
594a1c2090eSBill Fenner 	}
595a1c2090eSBill Fenner 	*cp = '\0';
596a1c2090eSBill Fenner 	return (tp->e_name);
597a1c2090eSBill Fenner }
598a1c2090eSBill Fenner 
599a1c2090eSBill Fenner const char *
6004edb46e9SPaul Traina etherproto_string(u_short port)
6014edb46e9SPaul Traina {
6024edb46e9SPaul Traina 	register char *cp;
6034edb46e9SPaul Traina 	register struct hnamemem *tp;
604*3c602fabSXin LI 	register uint32_t i = port;
6054edb46e9SPaul Traina 	char buf[sizeof("0000")];
6064edb46e9SPaul Traina 
6074edb46e9SPaul Traina 	for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
6084edb46e9SPaul Traina 		if (tp->addr == i)
6094edb46e9SPaul Traina 			return (tp->name);
6104edb46e9SPaul Traina 
6114edb46e9SPaul Traina 	tp->addr = i;
6124edb46e9SPaul Traina 	tp->nxt = newhnamemem();
6134edb46e9SPaul Traina 
6144edb46e9SPaul Traina 	cp = buf;
6154edb46e9SPaul Traina 	NTOHS(port);
6164edb46e9SPaul Traina 	*cp++ = hex[port >> 12 & 0xf];
6174edb46e9SPaul Traina 	*cp++ = hex[port >> 8 & 0xf];
6184edb46e9SPaul Traina 	*cp++ = hex[port >> 4 & 0xf];
6194edb46e9SPaul Traina 	*cp++ = hex[port & 0xf];
6204edb46e9SPaul Traina 	*cp++ = '\0';
621a1c2090eSBill Fenner 	tp->name = strdup(buf);
6224edb46e9SPaul Traina 	return (tp->name);
6234edb46e9SPaul Traina }
6244edb46e9SPaul Traina 
625a1c2090eSBill Fenner const char *
6264edb46e9SPaul Traina protoid_string(register const u_char *pi)
6274edb46e9SPaul Traina {
6284edb46e9SPaul Traina 	register u_int i, j;
6294edb46e9SPaul Traina 	register char *cp;
6304edb46e9SPaul Traina 	register struct protoidmem *tp;
6314edb46e9SPaul Traina 	char buf[sizeof("00:00:00:00:00")];
6324edb46e9SPaul Traina 
6334edb46e9SPaul Traina 	tp = lookup_protoid(pi);
6344edb46e9SPaul Traina 	if (tp->p_name)
6354edb46e9SPaul Traina 		return tp->p_name;
6364edb46e9SPaul Traina 
6374edb46e9SPaul Traina 	cp = buf;
6384edb46e9SPaul Traina 	if ((j = *pi >> 4) != 0)
6394edb46e9SPaul Traina 		*cp++ = hex[j];
6404edb46e9SPaul Traina 	*cp++ = hex[*pi++ & 0xf];
6414edb46e9SPaul Traina 	for (i = 4; (int)--i >= 0;) {
6424edb46e9SPaul Traina 		*cp++ = ':';
6434edb46e9SPaul Traina 		if ((j = *pi >> 4) != 0)
6444edb46e9SPaul Traina 			*cp++ = hex[j];
6454edb46e9SPaul Traina 		*cp++ = hex[*pi++ & 0xf];
6464edb46e9SPaul Traina 	}
6474edb46e9SPaul Traina 	*cp = '\0';
648a1c2090eSBill Fenner 	tp->p_name = strdup(buf);
6494edb46e9SPaul Traina 	return (tp->p_name);
6504edb46e9SPaul Traina }
6514edb46e9SPaul Traina 
652c1ad1296SSam Leffler #define ISONSAP_MAX_LENGTH 20
653a1c2090eSBill Fenner const char *
654c1ad1296SSam Leffler isonsap_string(const u_char *nsap, register u_int nsap_length)
6554edb46e9SPaul Traina {
656c1ad1296SSam Leffler 	register u_int nsap_idx;
6574edb46e9SPaul Traina 	register char *cp;
6584edb46e9SPaul Traina 	register struct enamemem *tp;
6594edb46e9SPaul Traina 
660c1ad1296SSam Leffler 	if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
66129292c17SSam Leffler 		return ("isonsap_string: illegal length");
662c1ad1296SSam Leffler 
6634edb46e9SPaul Traina 	tp = lookup_nsap(nsap);
6644edb46e9SPaul Traina 	if (tp->e_name)
6654edb46e9SPaul Traina 		return tp->e_name;
6664edb46e9SPaul Traina 
667c1ad1296SSam Leffler 	tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
6684edb46e9SPaul Traina 	if (cp == NULL)
6694edb46e9SPaul Traina 		error("isonsap_string: malloc");
6704edb46e9SPaul Traina 
671c1ad1296SSam Leffler 	for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
6724edb46e9SPaul Traina 		*cp++ = hex[*nsap >> 4];
6734edb46e9SPaul Traina 		*cp++ = hex[*nsap++ & 0xf];
674c1ad1296SSam Leffler 		if (((nsap_idx & 1) == 0) &&
675c1ad1296SSam Leffler 		     (nsap_idx + 1 < nsap_length)) {
67601bd0dbcSPaul Traina 		     	*cp++ = '.';
6774edb46e9SPaul Traina 		}
678c1ad1296SSam Leffler 	}
6794edb46e9SPaul Traina 	*cp = '\0';
6804edb46e9SPaul Traina 	return (tp->e_name);
6814edb46e9SPaul Traina }
6824edb46e9SPaul Traina 
683a1c2090eSBill Fenner const char *
6844edb46e9SPaul Traina tcpport_string(u_short port)
6854edb46e9SPaul Traina {
6864edb46e9SPaul Traina 	register struct hnamemem *tp;
687*3c602fabSXin LI 	register uint32_t i = port;
6884edb46e9SPaul Traina 	char buf[sizeof("00000")];
6894edb46e9SPaul Traina 
6904edb46e9SPaul Traina 	for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
6914edb46e9SPaul Traina 		if (tp->addr == i)
6924edb46e9SPaul Traina 			return (tp->name);
6934edb46e9SPaul Traina 
6944edb46e9SPaul Traina 	tp->addr = i;
6954edb46e9SPaul Traina 	tp->nxt = newhnamemem();
6964edb46e9SPaul Traina 
6977524a079SKris Kennaway 	(void)snprintf(buf, sizeof(buf), "%u", i);
698a1c2090eSBill Fenner 	tp->name = strdup(buf);
6994edb46e9SPaul Traina 	return (tp->name);
7004edb46e9SPaul Traina }
7014edb46e9SPaul Traina 
702a1c2090eSBill Fenner const char *
7034edb46e9SPaul Traina udpport_string(register u_short port)
7044edb46e9SPaul Traina {
7054edb46e9SPaul Traina 	register struct hnamemem *tp;
706*3c602fabSXin LI 	register uint32_t i = port;
7074edb46e9SPaul Traina 	char buf[sizeof("00000")];
7084edb46e9SPaul Traina 
7094edb46e9SPaul Traina 	for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
7104edb46e9SPaul Traina 		if (tp->addr == i)
7114edb46e9SPaul Traina 			return (tp->name);
7124edb46e9SPaul Traina 
7134edb46e9SPaul Traina 	tp->addr = i;
7144edb46e9SPaul Traina 	tp->nxt = newhnamemem();
7154edb46e9SPaul Traina 
7167524a079SKris Kennaway 	(void)snprintf(buf, sizeof(buf), "%u", i);
717a1c2090eSBill Fenner 	tp->name = strdup(buf);
7184edb46e9SPaul Traina 	return (tp->name);
7194edb46e9SPaul Traina }
7204edb46e9SPaul Traina 
721cc391cceSBruce M Simpson const char *
722cc391cceSBruce M Simpson ipxsap_string(u_short port)
723cc391cceSBruce M Simpson {
724cc391cceSBruce M Simpson 	register char *cp;
725cc391cceSBruce M Simpson 	register struct hnamemem *tp;
726*3c602fabSXin LI 	register uint32_t i = port;
727cc391cceSBruce M Simpson 	char buf[sizeof("0000")];
728cc391cceSBruce M Simpson 
729cc391cceSBruce M Simpson 	for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
730cc391cceSBruce M Simpson 		if (tp->addr == i)
731cc391cceSBruce M Simpson 			return (tp->name);
732cc391cceSBruce M Simpson 
733cc391cceSBruce M Simpson 	tp->addr = i;
734cc391cceSBruce M Simpson 	tp->nxt = newhnamemem();
735cc391cceSBruce M Simpson 
736cc391cceSBruce M Simpson 	cp = buf;
737cc391cceSBruce M Simpson 	NTOHS(port);
738cc391cceSBruce M Simpson 	*cp++ = hex[port >> 12 & 0xf];
739cc391cceSBruce M Simpson 	*cp++ = hex[port >> 8 & 0xf];
740cc391cceSBruce M Simpson 	*cp++ = hex[port >> 4 & 0xf];
741cc391cceSBruce M Simpson 	*cp++ = hex[port & 0xf];
742cc391cceSBruce M Simpson 	*cp++ = '\0';
743cc391cceSBruce M Simpson 	tp->name = strdup(buf);
744cc391cceSBruce M Simpson 	return (tp->name);
745cc391cceSBruce M Simpson }
746cc391cceSBruce M Simpson 
7474edb46e9SPaul Traina static void
748*3c602fabSXin LI init_servarray(netdissect_options *ndo)
7494edb46e9SPaul Traina {
7504edb46e9SPaul Traina 	struct servent *sv;
7514edb46e9SPaul Traina 	register struct hnamemem *table;
7524edb46e9SPaul Traina 	register int i;
7534edb46e9SPaul Traina 	char buf[sizeof("0000000000")];
7544edb46e9SPaul Traina 
7554edb46e9SPaul Traina 	while ((sv = getservent()) != NULL) {
7564edb46e9SPaul Traina 		int port = ntohs(sv->s_port);
7574edb46e9SPaul Traina 		i = port & (HASHNAMESIZE-1);
7584edb46e9SPaul Traina 		if (strcmp(sv->s_proto, "tcp") == 0)
7594edb46e9SPaul Traina 			table = &tporttable[i];
7604edb46e9SPaul Traina 		else if (strcmp(sv->s_proto, "udp") == 0)
7614edb46e9SPaul Traina 			table = &uporttable[i];
7624edb46e9SPaul Traina 		else
7634edb46e9SPaul Traina 			continue;
7644edb46e9SPaul Traina 
7654edb46e9SPaul Traina 		while (table->name)
7664edb46e9SPaul Traina 			table = table->nxt;
767*3c602fabSXin LI 		if (ndo->ndo_nflag) {
7687524a079SKris Kennaway 			(void)snprintf(buf, sizeof(buf), "%d", port);
769a1c2090eSBill Fenner 			table->name = strdup(buf);
7704edb46e9SPaul Traina 		} else
771a1c2090eSBill Fenner 			table->name = strdup(sv->s_name);
7724edb46e9SPaul Traina 		table->addr = port;
7734edb46e9SPaul Traina 		table->nxt = newhnamemem();
7744edb46e9SPaul Traina 	}
7754edb46e9SPaul Traina 	endservent();
7764edb46e9SPaul Traina }
7774edb46e9SPaul Traina 
778c1ad1296SSam Leffler /* in libpcap.a (nametoaddr.c) */
779c1ad1296SSam Leffler #if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
780*3c602fabSXin LI extern __declspec(dllimport)
781cc391cceSBruce M Simpson #else
782c1ad1296SSam Leffler extern
783cc391cceSBruce M Simpson #endif
784c1ad1296SSam Leffler const struct eproto {
785c1ad1296SSam Leffler 	const char *s;
7864edb46e9SPaul Traina 	u_short p;
7874edb46e9SPaul Traina } eproto_db[];
7884edb46e9SPaul Traina 
7894edb46e9SPaul Traina static void
7904edb46e9SPaul Traina init_eprotoarray(void)
7914edb46e9SPaul Traina {
7924edb46e9SPaul Traina 	register int i;
7934edb46e9SPaul Traina 	register struct hnamemem *table;
7944edb46e9SPaul Traina 
7954edb46e9SPaul Traina 	for (i = 0; eproto_db[i].s; i++) {
796cc391cceSBruce M Simpson 		int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
7974edb46e9SPaul Traina 		table = &eprototable[j];
7984edb46e9SPaul Traina 		while (table->name)
7994edb46e9SPaul Traina 			table = table->nxt;
8004edb46e9SPaul Traina 		table->name = eproto_db[i].s;
801cc391cceSBruce M Simpson 		table->addr = htons(eproto_db[i].p);
8024edb46e9SPaul Traina 		table->nxt = newhnamemem();
8034edb46e9SPaul Traina 	}
8044edb46e9SPaul Traina }
8054edb46e9SPaul Traina 
80627df3f5dSRui Paulo static const struct protoidlist {
807a1c2090eSBill Fenner 	const u_char protoid[5];
808a1c2090eSBill Fenner 	const char *name;
809a1c2090eSBill Fenner } protoidlist[] = {
810a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
811a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
812a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
813a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
814a1c2090eSBill Fenner 	{{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
815a1c2090eSBill Fenner 	{{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
816a1c2090eSBill Fenner };
817a1c2090eSBill Fenner 
8184edb46e9SPaul Traina /*
8194edb46e9SPaul Traina  * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
8204edb46e9SPaul Traina  * types.
8214edb46e9SPaul Traina  */
8224edb46e9SPaul Traina static void
8234edb46e9SPaul Traina init_protoidarray(void)
8244edb46e9SPaul Traina {
8254edb46e9SPaul Traina 	register int i;
8264edb46e9SPaul Traina 	register struct protoidmem *tp;
82727df3f5dSRui Paulo 	const struct protoidlist *pl;
8284edb46e9SPaul Traina 	u_char protoid[5];
8294edb46e9SPaul Traina 
8304edb46e9SPaul Traina 	protoid[0] = 0;
8314edb46e9SPaul Traina 	protoid[1] = 0;
8324edb46e9SPaul Traina 	protoid[2] = 0;
8334edb46e9SPaul Traina 	for (i = 0; eproto_db[i].s; i++) {
8344edb46e9SPaul Traina 		u_short etype = htons(eproto_db[i].p);
8354edb46e9SPaul Traina 
8364edb46e9SPaul Traina 		memcpy((char *)&protoid[3], (char *)&etype, 2);
8374edb46e9SPaul Traina 		tp = lookup_protoid(protoid);
838a1c2090eSBill Fenner 		tp->p_name = strdup(eproto_db[i].s);
839a1c2090eSBill Fenner 	}
840a1c2090eSBill Fenner 	/* Hardwire some SNAP proto ID names */
841a1c2090eSBill Fenner 	for (pl = protoidlist; pl->name != NULL; ++pl) {
842a1c2090eSBill Fenner 		tp = lookup_protoid(pl->protoid);
843a1c2090eSBill Fenner 		/* Don't override existing name */
844a1c2090eSBill Fenner 		if (tp->p_name != NULL)
845a1c2090eSBill Fenner 			continue;
846a1c2090eSBill Fenner 
847a1c2090eSBill Fenner 		tp->p_name = pl->name;
8484edb46e9SPaul Traina 	}
8494edb46e9SPaul Traina }
8504edb46e9SPaul Traina 
85127df3f5dSRui Paulo static const struct etherlist {
852a1c2090eSBill Fenner 	const u_char addr[6];
853a1c2090eSBill Fenner 	const char *name;
8544edb46e9SPaul Traina } etherlist[] = {
8554edb46e9SPaul Traina 	{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
8564edb46e9SPaul Traina 	{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
8574edb46e9SPaul Traina };
8584edb46e9SPaul Traina 
8594edb46e9SPaul Traina /*
8604edb46e9SPaul Traina  * Initialize the ethers hash table.  We take two different approaches
8614edb46e9SPaul Traina  * depending on whether or not the system provides the ethers name
8624edb46e9SPaul Traina  * service.  If it does, we just wire in a few names at startup,
8634edb46e9SPaul Traina  * and etheraddr_string() fills in the table on demand.  If it doesn't,
8644edb46e9SPaul Traina  * then we suck in the entire /etc/ethers file at startup.  The idea
8654edb46e9SPaul Traina  * is that parsing the local file will be fast, but spinning through
8664edb46e9SPaul Traina  * all the ethers entries via NIS & next_etherent might be very slow.
8674edb46e9SPaul Traina  *
8684edb46e9SPaul Traina  * XXX pcap_next_etherent doesn't belong in the pcap interface, but
8694edb46e9SPaul Traina  * since the pcap module already does name-to-address translation,
8704edb46e9SPaul Traina  * it's already does most of the work for the ethernet address-to-name
8714edb46e9SPaul Traina  * translation, so we just pcap_next_etherent as a convenience.
8724edb46e9SPaul Traina  */
8734edb46e9SPaul Traina static void
8744edb46e9SPaul Traina init_etherarray(void)
8754edb46e9SPaul Traina {
87627df3f5dSRui Paulo 	register const struct etherlist *el;
8774edb46e9SPaul Traina 	register struct enamemem *tp;
878a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
8794edb46e9SPaul Traina 	char name[256];
8804edb46e9SPaul Traina #else
8814edb46e9SPaul Traina 	register struct pcap_etherent *ep;
8824edb46e9SPaul Traina 	register FILE *fp;
8834edb46e9SPaul Traina 
8844edb46e9SPaul Traina 	/* Suck in entire ethers file */
8854edb46e9SPaul Traina 	fp = fopen(PCAP_ETHERS_FILE, "r");
8864edb46e9SPaul Traina 	if (fp != NULL) {
8874edb46e9SPaul Traina 		while ((ep = pcap_next_etherent(fp)) != NULL) {
8884edb46e9SPaul Traina 			tp = lookup_emem(ep->addr);
889a1c2090eSBill Fenner 			tp->e_name = strdup(ep->name);
8904edb46e9SPaul Traina 		}
8914edb46e9SPaul Traina 		(void)fclose(fp);
8924edb46e9SPaul Traina 	}
8934edb46e9SPaul Traina #endif
8944edb46e9SPaul Traina 
8954edb46e9SPaul Traina 	/* Hardwire some ethernet names */
8964edb46e9SPaul Traina 	for (el = etherlist; el->name != NULL; ++el) {
8974edb46e9SPaul Traina 		tp = lookup_emem(el->addr);
8984edb46e9SPaul Traina 		/* Don't override existing name */
8994edb46e9SPaul Traina 		if (tp->e_name != NULL)
9004edb46e9SPaul Traina 			continue;
9014edb46e9SPaul Traina 
902a1c2090eSBill Fenner #ifdef USE_ETHER_NTOHOST
903c1ad1296SSam Leffler 		/*
904c1ad1296SSam Leffler 		 * Use YP/NIS version of name if available.
905c1ad1296SSam Leffler 		 *
906c1ad1296SSam Leffler 		 * We don't cast it to "const struct ether_addr *"
907c1ad1296SSam Leffler 		 * because some systems don't modify the Ethernet
908c1ad1296SSam Leffler 		 * address but fail to declare the second argument
909c1ad1296SSam Leffler 		 * as a "const" pointer.
910c1ad1296SSam Leffler 		 */
911c1ad1296SSam Leffler 		if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
912a1c2090eSBill Fenner 			tp->e_name = strdup(name);
9134edb46e9SPaul Traina 			continue;
9144edb46e9SPaul Traina 		}
9154edb46e9SPaul Traina #endif
9164edb46e9SPaul Traina 		tp->e_name = el->name;
9174edb46e9SPaul Traina 	}
9184edb46e9SPaul Traina }
9194edb46e9SPaul Traina 
92027df3f5dSRui Paulo static const struct tok ipxsap_db[] = {
921cc391cceSBruce M Simpson 	{ 0x0000, "Unknown" },
922cc391cceSBruce M Simpson 	{ 0x0001, "User" },
923cc391cceSBruce M Simpson 	{ 0x0002, "User Group" },
924cc391cceSBruce M Simpson 	{ 0x0003, "PrintQueue" },
925cc391cceSBruce M Simpson 	{ 0x0004, "FileServer" },
926cc391cceSBruce M Simpson 	{ 0x0005, "JobServer" },
927cc391cceSBruce M Simpson 	{ 0x0006, "Gateway" },
928cc391cceSBruce M Simpson 	{ 0x0007, "PrintServer" },
929cc391cceSBruce M Simpson 	{ 0x0008, "ArchiveQueue" },
930cc391cceSBruce M Simpson 	{ 0x0009, "ArchiveServer" },
931cc391cceSBruce M Simpson 	{ 0x000a, "JobQueue" },
932cc391cceSBruce M Simpson 	{ 0x000b, "Administration" },
933cc391cceSBruce M Simpson 	{ 0x000F, "Novell TI-RPC" },
934cc391cceSBruce M Simpson 	{ 0x0017, "Diagnostics" },
935cc391cceSBruce M Simpson 	{ 0x0020, "NetBIOS" },
936cc391cceSBruce M Simpson 	{ 0x0021, "NAS SNA Gateway" },
937cc391cceSBruce M Simpson 	{ 0x0023, "NACS AsyncGateway" },
938cc391cceSBruce M Simpson 	{ 0x0024, "RemoteBridge/RoutingService" },
939cc391cceSBruce M Simpson 	{ 0x0026, "BridgeServer" },
940cc391cceSBruce M Simpson 	{ 0x0027, "TCP/IP Gateway" },
941cc391cceSBruce M Simpson 	{ 0x0028, "Point-to-point X.25 BridgeServer" },
942cc391cceSBruce M Simpson 	{ 0x0029, "3270 Gateway" },
943cc391cceSBruce M Simpson 	{ 0x002a, "CHI Corp" },
944cc391cceSBruce M Simpson 	{ 0x002c, "PC Chalkboard" },
945cc391cceSBruce M Simpson 	{ 0x002d, "TimeSynchServer" },
946cc391cceSBruce M Simpson 	{ 0x002e, "ARCserve5.0/PalindromeBackup" },
947cc391cceSBruce M Simpson 	{ 0x0045, "DI3270 Gateway" },
948cc391cceSBruce M Simpson 	{ 0x0047, "AdvertisingPrintServer" },
949cc391cceSBruce M Simpson 	{ 0x004a, "NetBlazerModems" },
950cc391cceSBruce M Simpson 	{ 0x004b, "BtrieveVAP" },
951cc391cceSBruce M Simpson 	{ 0x004c, "NetwareSQL" },
952cc391cceSBruce M Simpson 	{ 0x004d, "XtreeNetwork" },
953cc391cceSBruce M Simpson 	{ 0x0050, "BtrieveVAP4.11" },
954cc391cceSBruce M Simpson 	{ 0x0052, "QuickLink" },
955cc391cceSBruce M Simpson 	{ 0x0053, "PrintQueueUser" },
956cc391cceSBruce M Simpson 	{ 0x0058, "Multipoint X.25 Router" },
957cc391cceSBruce M Simpson 	{ 0x0060, "STLB/NLM" },
958cc391cceSBruce M Simpson 	{ 0x0064, "ARCserve" },
959cc391cceSBruce M Simpson 	{ 0x0066, "ARCserve3.0" },
960cc391cceSBruce M Simpson 	{ 0x0072, "WAN CopyUtility" },
961cc391cceSBruce M Simpson 	{ 0x007a, "TES-NetwareVMS" },
962cc391cceSBruce M Simpson 	{ 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
963cc391cceSBruce M Simpson 	{ 0x0095, "DDA OBGYN" },
964cc391cceSBruce M Simpson 	{ 0x0098, "NetwareAccessServer" },
965cc391cceSBruce M Simpson 	{ 0x009a, "Netware for VMS II/NamedPipeServer" },
966cc391cceSBruce M Simpson 	{ 0x009b, "NetwareAccessServer" },
967cc391cceSBruce M Simpson 	{ 0x009e, "PortableNetwareServer/SunLinkNVT" },
968cc391cceSBruce M Simpson 	{ 0x00a1, "PowerchuteAPC UPS" },
969cc391cceSBruce M Simpson 	{ 0x00aa, "LAWserve" },
970cc391cceSBruce M Simpson 	{ 0x00ac, "CompaqIDA StatusMonitor" },
971cc391cceSBruce M Simpson 	{ 0x0100, "PIPE STAIL" },
972cc391cceSBruce M Simpson 	{ 0x0102, "LAN ProtectBindery" },
973cc391cceSBruce M Simpson 	{ 0x0103, "OracleDataBaseServer" },
974cc391cceSBruce M Simpson 	{ 0x0107, "Netware386/RSPX RemoteConsole" },
975cc391cceSBruce M Simpson 	{ 0x010f, "NovellSNA Gateway" },
976cc391cceSBruce M Simpson 	{ 0x0111, "TestServer" },
977cc391cceSBruce M Simpson 	{ 0x0112, "HP PrintServer" },
978cc391cceSBruce M Simpson 	{ 0x0114, "CSA MUX" },
979cc391cceSBruce M Simpson 	{ 0x0115, "CSA LCA" },
980cc391cceSBruce M Simpson 	{ 0x0116, "CSA CM" },
981cc391cceSBruce M Simpson 	{ 0x0117, "CSA SMA" },
982cc391cceSBruce M Simpson 	{ 0x0118, "CSA DBA" },
983cc391cceSBruce M Simpson 	{ 0x0119, "CSA NMA" },
984cc391cceSBruce M Simpson 	{ 0x011a, "CSA SSA" },
985cc391cceSBruce M Simpson 	{ 0x011b, "CSA STATUS" },
986cc391cceSBruce M Simpson 	{ 0x011e, "CSA APPC" },
987cc391cceSBruce M Simpson 	{ 0x0126, "SNA TEST SSA Profile" },
988cc391cceSBruce M Simpson 	{ 0x012a, "CSA TRACE" },
989cc391cceSBruce M Simpson 	{ 0x012b, "NetwareSAA" },
990cc391cceSBruce M Simpson 	{ 0x012e, "IKARUS VirusScan" },
991cc391cceSBruce M Simpson 	{ 0x0130, "CommunicationsExecutive" },
992cc391cceSBruce M Simpson 	{ 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
993cc391cceSBruce M Simpson 	{ 0x0135, "NetwareNamingServicesProfile" },
994cc391cceSBruce M Simpson 	{ 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
995cc391cceSBruce M Simpson 	{ 0x0141, "LAN SpoolServer" },
996cc391cceSBruce M Simpson 	{ 0x0152, "IRMALAN Gateway" },
997cc391cceSBruce M Simpson 	{ 0x0154, "NamedPipeServer" },
998cc391cceSBruce M Simpson 	{ 0x0166, "NetWareManagement" },
999cc391cceSBruce M Simpson 	{ 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1000cc391cceSBruce M Simpson 	{ 0x0173, "Compaq" },
1001cc391cceSBruce M Simpson 	{ 0x0174, "Compaq SNMP Agent" },
1002cc391cceSBruce M Simpson 	{ 0x0175, "Compaq" },
1003cc391cceSBruce M Simpson 	{ 0x0180, "XTreeServer/XTreeTools" },
1004cc391cceSBruce M Simpson 	{ 0x018A, "NASI ServicesBroadcastServer" },
1005cc391cceSBruce M Simpson 	{ 0x01b0, "GARP Gateway" },
1006cc391cceSBruce M Simpson 	{ 0x01b1, "Binfview" },
1007cc391cceSBruce M Simpson 	{ 0x01bf, "IntelLanDeskManager" },
1008cc391cceSBruce M Simpson 	{ 0x01ca, "AXTEC" },
1009cc391cceSBruce M Simpson 	{ 0x01cb, "ShivaNetModem/E" },
1010cc391cceSBruce M Simpson 	{ 0x01cc, "ShivaLanRover/E" },
1011cc391cceSBruce M Simpson 	{ 0x01cd, "ShivaLanRover/T" },
1012cc391cceSBruce M Simpson 	{ 0x01ce, "ShivaUniversal" },
1013cc391cceSBruce M Simpson 	{ 0x01d8, "CastelleFAXPressServer" },
1014cc391cceSBruce M Simpson 	{ 0x01da, "CastelleLANPressPrintServer" },
1015cc391cceSBruce M Simpson 	{ 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1016cc391cceSBruce M Simpson 	{ 0x01f0, "LEGATO" },
1017cc391cceSBruce M Simpson 	{ 0x01f5, "LEGATO" },
1018cc391cceSBruce M Simpson 	{ 0x0233, "NMS Agent/NetwareManagementAgent" },
1019cc391cceSBruce M Simpson 	{ 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1020cc391cceSBruce M Simpson 	{ 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1021cc391cceSBruce M Simpson 	{ 0x023a, "LANtern" },
1022cc391cceSBruce M Simpson 	{ 0x023c, "MAVERICK" },
1023cc391cceSBruce M Simpson 	{ 0x023f, "NovellSMDR" },
1024cc391cceSBruce M Simpson 	{ 0x024e, "NetwareConnect" },
1025cc391cceSBruce M Simpson 	{ 0x024f, "NASI ServerBroadcast Cisco" },
1026cc391cceSBruce M Simpson 	{ 0x026a, "NMS ServiceConsole" },
1027cc391cceSBruce M Simpson 	{ 0x026b, "TimeSynchronizationServer Netware 4.x" },
1028cc391cceSBruce M Simpson 	{ 0x0278, "DirectoryServer Netware 4.x" },
1029cc391cceSBruce M Simpson 	{ 0x027b, "NetwareManagementAgent" },
1030cc391cceSBruce M Simpson 	{ 0x0280, "Novell File and Printer Sharing Service for PC" },
1031cc391cceSBruce M Simpson 	{ 0x0304, "NovellSAA Gateway" },
1032cc391cceSBruce M Simpson 	{ 0x0308, "COM/VERMED" },
1033cc391cceSBruce M Simpson 	{ 0x030a, "GalacticommWorldgroupServer" },
1034cc391cceSBruce M Simpson 	{ 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1035cc391cceSBruce M Simpson 	{ 0x0320, "AttachmateGateway" },
1036cc391cceSBruce M Simpson 	{ 0x0327, "MicrosoftDiagnostiocs" },
1037cc391cceSBruce M Simpson 	{ 0x0328, "WATCOM SQL Server" },
1038cc391cceSBruce M Simpson 	{ 0x0335, "MultiTechSystems MultisynchCommServer" },
1039cc391cceSBruce M Simpson 	{ 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1040cc391cceSBruce M Simpson 	{ 0x0355, "ArcadaBackupExec" },
1041cc391cceSBruce M Simpson 	{ 0x0358, "MSLCD1" },
1042cc391cceSBruce M Simpson 	{ 0x0361, "NETINELO" },
1043cc391cceSBruce M Simpson 	{ 0x037e, "Powerchute UPS Monitoring" },
1044cc391cceSBruce M Simpson 	{ 0x037f, "ViruSafeNotify" },
1045cc391cceSBruce M Simpson 	{ 0x0386, "HP Bridge" },
1046cc391cceSBruce M Simpson 	{ 0x0387, "HP Hub" },
1047cc391cceSBruce M Simpson 	{ 0x0394, "NetWare SAA Gateway" },
1048cc391cceSBruce M Simpson 	{ 0x039b, "LotusNotes" },
1049cc391cceSBruce M Simpson 	{ 0x03b7, "CertusAntiVirus" },
1050cc391cceSBruce M Simpson 	{ 0x03c4, "ARCserve4.0" },
1051cc391cceSBruce M Simpson 	{ 0x03c7, "LANspool3.5" },
1052cc391cceSBruce M Simpson 	{ 0x03d7, "LexmarkPrinterServer" },
1053cc391cceSBruce M Simpson 	{ 0x03d8, "LexmarkXLE PrinterServer" },
1054cc391cceSBruce M Simpson 	{ 0x03dd, "BanyanENS NetwareClient" },
1055cc391cceSBruce M Simpson 	{ 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1056cc391cceSBruce M Simpson 	{ 0x03e1, "UnivelUnixware" },
1057cc391cceSBruce M Simpson 	{ 0x03e4, "UnivelUnixware" },
1058cc391cceSBruce M Simpson 	{ 0x03fc, "IntelNetport" },
1059cc391cceSBruce M Simpson 	{ 0x03fd, "PrintServerQueue" },
1060cc391cceSBruce M Simpson 	{ 0x040A, "ipnServer" },
1061cc391cceSBruce M Simpson 	{ 0x040D, "LVERRMAN" },
1062cc391cceSBruce M Simpson 	{ 0x040E, "LVLIC" },
1063cc391cceSBruce M Simpson 	{ 0x0414, "NET Silicon (DPI)/Kyocera" },
1064cc391cceSBruce M Simpson 	{ 0x0429, "SiteLockVirus" },
1065cc391cceSBruce M Simpson 	{ 0x0432, "UFHELPR???" },
1066cc391cceSBruce M Simpson 	{ 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1067cc391cceSBruce M Simpson 	{ 0x0444, "MicrosoftNT SNA Server" },
1068cc391cceSBruce M Simpson 	{ 0x0448, "Oracle" },
1069cc391cceSBruce M Simpson 	{ 0x044c, "ARCserve5.01" },
1070cc391cceSBruce M Simpson 	{ 0x0457, "CanonGP55" },
1071cc391cceSBruce M Simpson 	{ 0x045a, "QMS Printers" },
1072cc391cceSBruce M Simpson 	{ 0x045b, "DellSCSI Array" },
1073cc391cceSBruce M Simpson 	{ 0x0491, "NetBlazerModems" },
1074cc391cceSBruce M Simpson 	{ 0x04ac, "OnTimeScheduler" },
1075cc391cceSBruce M Simpson 	{ 0x04b0, "CD-Net" },
1076cc391cceSBruce M Simpson 	{ 0x0513, "EmulexNQA" },
1077cc391cceSBruce M Simpson 	{ 0x0520, "SiteLockChecks" },
1078cc391cceSBruce M Simpson 	{ 0x0529, "SiteLockChecks" },
1079cc391cceSBruce M Simpson 	{ 0x052d, "CitrixOS2 AppServer" },
1080cc391cceSBruce M Simpson 	{ 0x0535, "Tektronix" },
1081cc391cceSBruce M Simpson 	{ 0x0536, "Milan" },
1082cc391cceSBruce M Simpson 	{ 0x055d, "Attachmate SNA gateway" },
1083cc391cceSBruce M Simpson 	{ 0x056b, "IBM8235 ModemServer" },
1084cc391cceSBruce M Simpson 	{ 0x056c, "ShivaLanRover/E PLUS" },
1085cc391cceSBruce M Simpson 	{ 0x056d, "ShivaLanRover/T PLUS" },
1086cc391cceSBruce M Simpson 	{ 0x0580, "McAfeeNetShield" },
1087cc391cceSBruce M Simpson 	{ 0x05B8, "NLM to workstation communication (Revelation Software)" },
1088cc391cceSBruce M Simpson 	{ 0x05BA, "CompatibleSystemsRouters" },
1089cc391cceSBruce M Simpson 	{ 0x05BE, "CheyenneHierarchicalStorageManager" },
1090cc391cceSBruce M Simpson 	{ 0x0606, "JCWatermarkImaging" },
1091cc391cceSBruce M Simpson 	{ 0x060c, "AXISNetworkPrinter" },
1092cc391cceSBruce M Simpson 	{ 0x0610, "AdaptecSCSIManagement" },
1093cc391cceSBruce M Simpson 	{ 0x0621, "IBM AntiVirus" },
1094cc391cceSBruce M Simpson 	{ 0x0640, "Windows95 RemoteRegistryService" },
1095cc391cceSBruce M Simpson 	{ 0x064e, "MicrosoftIIS" },
1096cc391cceSBruce M Simpson 	{ 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1097cc391cceSBruce M Simpson 	{ 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1098cc391cceSBruce M Simpson 	{ 0x076C, "Xerox" },
1099cc391cceSBruce M Simpson 	{ 0x079b, "ShivaLanRover/E 115" },
1100cc391cceSBruce M Simpson 	{ 0x079c, "ShivaLanRover/T 115" },
1101cc391cceSBruce M Simpson 	{ 0x07B4, "CubixWorldDesk" },
1102cc391cceSBruce M Simpson 	{ 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1103cc391cceSBruce M Simpson 	{ 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1104cc391cceSBruce M Simpson 	{ 0x0810, "ELAN License Server Demo" },
1105cc391cceSBruce M Simpson 	{ 0x0824, "ShivaLanRoverAccessSwitch/E" },
1106cc391cceSBruce M Simpson 	{ 0x086a, "ISSC Collector" },
1107cc391cceSBruce M Simpson 	{ 0x087f, "ISSC DAS AgentAIX" },
1108cc391cceSBruce M Simpson 	{ 0x0880, "Intel Netport PRO" },
1109cc391cceSBruce M Simpson 	{ 0x0881, "Intel Netport PRO" },
1110cc391cceSBruce M Simpson 	{ 0x0b29, "SiteLock" },
1111cc391cceSBruce M Simpson 	{ 0x0c29, "SiteLockApplications" },
1112cc391cceSBruce M Simpson 	{ 0x0c2c, "LicensingServer" },
1113cc391cceSBruce M Simpson 	{ 0x2101, "PerformanceTechnologyInstantInternet" },
1114cc391cceSBruce M Simpson 	{ 0x2380, "LAI SiteLock" },
1115cc391cceSBruce M Simpson 	{ 0x238c, "MeetingMaker" },
1116cc391cceSBruce M Simpson 	{ 0x4808, "SiteLockServer/SiteLockMetering" },
1117cc391cceSBruce M Simpson 	{ 0x5555, "SiteLockUser" },
1118cc391cceSBruce M Simpson 	{ 0x6312, "Tapeware" },
1119cc391cceSBruce M Simpson 	{ 0x6f00, "RabbitGateway" },
1120cc391cceSBruce M Simpson 	{ 0x7703, "MODEM" },
1121cc391cceSBruce M Simpson 	{ 0x8002, "NetPortPrinters" },
1122cc391cceSBruce M Simpson 	{ 0x8008, "WordPerfectNetworkVersion" },
1123cc391cceSBruce M Simpson 	{ 0x85BE, "Cisco EIGRP" },
1124cc391cceSBruce M Simpson 	{ 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1125cc391cceSBruce M Simpson 	{ 0x9000, "McAfeeNetShield" },
1126cc391cceSBruce M Simpson 	{ 0x9604, "CSA-NT_MON" },
1127cc391cceSBruce M Simpson 	{ 0xb6a8, "OceanIsleReachoutRemoteControl" },
1128cc391cceSBruce M Simpson 	{ 0xf11f, "SiteLockMetering" },
1129cc391cceSBruce M Simpson 	{ 0xf1ff, "SiteLock" },
1130cc391cceSBruce M Simpson 	{ 0xf503, "Microsoft SQL Server" },
1131cc391cceSBruce M Simpson 	{ 0xF905, "IBM TimeAndPlace" },
1132cc391cceSBruce M Simpson 	{ 0xfbfb, "TopCallIII FaxServer" },
1133cc391cceSBruce M Simpson 	{ 0xffff, "AnyService/Wildcard" },
1134cc391cceSBruce M Simpson 	{ 0, (char *)0 }
1135cc391cceSBruce M Simpson };
1136cc391cceSBruce M Simpson 
1137cc391cceSBruce M Simpson static void
1138cc391cceSBruce M Simpson init_ipxsaparray(void)
1139cc391cceSBruce M Simpson {
1140cc391cceSBruce M Simpson 	register int i;
1141cc391cceSBruce M Simpson 	register struct hnamemem *table;
1142cc391cceSBruce M Simpson 
1143cc391cceSBruce M Simpson 	for (i = 0; ipxsap_db[i].s != NULL; i++) {
1144cc391cceSBruce M Simpson 		int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1145cc391cceSBruce M Simpson 		table = &ipxsaptable[j];
1146cc391cceSBruce M Simpson 		while (table->name)
1147cc391cceSBruce M Simpson 			table = table->nxt;
1148cc391cceSBruce M Simpson 		table->name = ipxsap_db[i].s;
1149cc391cceSBruce M Simpson 		table->addr = htons(ipxsap_db[i].v);
1150cc391cceSBruce M Simpson 		table->nxt = newhnamemem();
1151cc391cceSBruce M Simpson 	}
1152cc391cceSBruce M Simpson }
1153cc391cceSBruce M Simpson 
11544edb46e9SPaul Traina /*
11554edb46e9SPaul Traina  * Initialize the address to name translation machinery.  We map all
1156*3c602fabSXin LI  * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1157*3c602fabSXin LI  * (i.e., to prevent blocking on the nameserver).  localnet is the IP address
11584edb46e9SPaul Traina  * of the local network.  mask is its subnet mask.
11594edb46e9SPaul Traina  */
11604edb46e9SPaul Traina void
1161*3c602fabSXin LI init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
11624edb46e9SPaul Traina {
1163*3c602fabSXin LI 	if (ndo->ndo_fflag) {
11644edb46e9SPaul Traina 		f_localnet = localnet;
11654edb46e9SPaul Traina 		f_netmask = mask;
11664edb46e9SPaul Traina 	}
1167*3c602fabSXin LI 	if (ndo->ndo_nflag)
11684edb46e9SPaul Traina 		/*
11694edb46e9SPaul Traina 		 * Simplest way to suppress names.
11704edb46e9SPaul Traina 		 */
11714edb46e9SPaul Traina 		return;
11724edb46e9SPaul Traina 
11734edb46e9SPaul Traina 	init_etherarray();
1174*3c602fabSXin LI 	init_servarray(ndo);
11754edb46e9SPaul Traina 	init_eprotoarray();
11764edb46e9SPaul Traina 	init_protoidarray();
1177cc391cceSBruce M Simpson 	init_ipxsaparray();
11784edb46e9SPaul Traina }
11794edb46e9SPaul Traina 
1180a1c2090eSBill Fenner const char *
1181*3c602fabSXin LI dnaddr_string(netdissect_options *ndo, u_short dnaddr)
11824edb46e9SPaul Traina {
11834edb46e9SPaul Traina 	register struct hnamemem *tp;
11844edb46e9SPaul Traina 
11854edb46e9SPaul Traina 	for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
11864edb46e9SPaul Traina 	     tp = tp->nxt)
11874edb46e9SPaul Traina 		if (tp->addr == dnaddr)
11884edb46e9SPaul Traina 			return (tp->name);
11894edb46e9SPaul Traina 
11904edb46e9SPaul Traina 	tp->addr = dnaddr;
11914edb46e9SPaul Traina 	tp->nxt = newhnamemem();
1192*3c602fabSXin LI 	if (ndo->ndo_nflag)
11934edb46e9SPaul Traina 		tp->name = dnnum_string(dnaddr);
11944edb46e9SPaul Traina 	else
11954edb46e9SPaul Traina 		tp->name = dnname_string(dnaddr);
11964edb46e9SPaul Traina 
11974edb46e9SPaul Traina 	return(tp->name);
11984edb46e9SPaul Traina }
11994edb46e9SPaul Traina 
12004edb46e9SPaul Traina /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
12014edb46e9SPaul Traina struct hnamemem *
12022ebf6c05SBill Fenner newhnamemem(void)
12034edb46e9SPaul Traina {
12044edb46e9SPaul Traina 	register struct hnamemem *p;
12054edb46e9SPaul Traina 	static struct hnamemem *ptr = NULL;
12064edb46e9SPaul Traina 	static u_int num = 0;
12074edb46e9SPaul Traina 
12084edb46e9SPaul Traina 	if (num  <= 0) {
12094edb46e9SPaul Traina 		num = 64;
12104edb46e9SPaul Traina 		ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
12114edb46e9SPaul Traina 		if (ptr == NULL)
12124edb46e9SPaul Traina 			error("newhnamemem: calloc");
12134edb46e9SPaul Traina 	}
12144edb46e9SPaul Traina 	--num;
12154edb46e9SPaul Traina 	p = ptr++;
12164edb46e9SPaul Traina 	return (p);
12174edb46e9SPaul Traina }
1218a88113a8SBill Fenner 
1219a88113a8SBill Fenner #ifdef INET6
1220a88113a8SBill Fenner /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1221a88113a8SBill Fenner struct h6namemem *
1222a88113a8SBill Fenner newh6namemem(void)
1223a88113a8SBill Fenner {
1224a88113a8SBill Fenner 	register struct h6namemem *p;
1225a88113a8SBill Fenner 	static struct h6namemem *ptr = NULL;
1226a88113a8SBill Fenner 	static u_int num = 0;
1227a88113a8SBill Fenner 
1228a88113a8SBill Fenner 	if (num  <= 0) {
1229a88113a8SBill Fenner 		num = 64;
1230a88113a8SBill Fenner 		ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1231a88113a8SBill Fenner 		if (ptr == NULL)
1232a88113a8SBill Fenner 			error("newh6namemem: calloc");
1233a88113a8SBill Fenner 	}
1234a88113a8SBill Fenner 	--num;
1235a88113a8SBill Fenner 	p = ptr++;
1236a88113a8SBill Fenner 	return (p);
1237a88113a8SBill Fenner }
1238a88113a8SBill Fenner #endif /* INET6 */
1239