xref: /freebsd/contrib/tcpdump/addrtoname.c (revision 9286d46a794f25482880d29864a8901ef6666fae)
1 /*
2  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  *  Internet, ethernet, port, and protocol string to address
22  *  and address to string conversion routines
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #ifdef HAVE_CASPER
30 #include <libcasper.h>
31 #include <casper/cap_dns.h>
32 #endif /* HAVE_CASPER */
33 
34 #include "netdissect-stdinc.h"
35 
36 #ifdef USE_ETHER_NTOHOST
37   #if defined(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
38     /*
39      * OK, just include <net/ethernet.h>.
40      */
41     #include <net/ethernet.h>
42   #elif defined(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
43     /*
44      * OK, just include <netinet/ether.h>
45      */
46     #include <netinet/ether.h>
47   #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
48     /*
49      * OK, just include <sys/ethernet.h>
50      */
51     #include <sys/ethernet.h>
52   #elif defined(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
53     /*
54      * OK, just include <arpa/inet.h>
55      */
56     #include <arpa/inet.h>
57   #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
58     /*
59      * OK, include <netinet/if_ether.h>, after all the other stuff we
60      * need to include or define for its benefit.
61      */
62     #define NEED_NETINET_IF_ETHER_H
63   #else
64     /*
65      * We'll have to declare it ourselves.
66      * If <netinet/if_ether.h> defines struct ether_addr, include
67      * it.  Otherwise, define it ourselves.
68      */
69     #ifdef HAVE_STRUCT_ETHER_ADDR
70       #define NEED_NETINET_IF_ETHER_H
71     #else /* HAVE_STRUCT_ETHER_ADDR */
72 	struct ether_addr {
73 		/* Beware FreeBSD calls this "octet". */
74 		unsigned char ether_addr_octet[MAC_ADDR_LEN];
75 	};
76     #endif /* HAVE_STRUCT_ETHER_ADDR */
77   #endif /* what declares ether_ntohost() */
78 
79   #ifdef NEED_NETINET_IF_ETHER_H
80     /*
81      * Include diag-control.h before <net/if.h>, which too defines a macro
82      * named ND_UNREACHABLE.
83      */
84     #include "diag-control.h"
85     #include <net/if.h>		/* Needed on some platforms */
86     #include <netinet/in.h>	/* Needed on some platforms */
87     #include <netinet/if_ether.h>
88   #endif /* NEED_NETINET_IF_ETHER_H */
89 
90   #ifndef HAVE_DECL_ETHER_NTOHOST
91     /*
92      * No header declares it, so declare it ourselves.
93      */
94     extern int ether_ntohost(char *, const struct ether_addr *);
95   #endif /* !defined(HAVE_DECL_ETHER_NTOHOST) */
96 #endif /* USE_ETHER_NTOHOST */
97 
98 #include <pcap.h>
99 #include <pcap-namedb.h>
100 #ifndef HAVE_GETSERVENT
101 #include <getservent.h>
102 #endif
103 #include <signal.h>
104 #include <stdio.h>
105 #include <string.h>
106 #include <stdlib.h>
107 
108 #include "netdissect.h"
109 #include "addrtoname.h"
110 #include "addrtostr.h"
111 #include "ethertype.h"
112 #include "llc.h"
113 #include "extract.h"
114 #include "oui.h"
115 
116 /*
117  * hash tables for whatever-to-name translations
118  *
119  * ndo_error() called on strdup(3) failure with S_ERR_ND_MEM_ALLOC status
120  */
121 
122 #define HASHNAMESIZE 4096
123 
124 struct hnamemem {
125 	uint32_t addr;
126 	const char *name;
127 	struct hnamemem *nxt;
128 };
129 
130 static struct hnamemem hnametable[HASHNAMESIZE];
131 static struct hnamemem tporttable[HASHNAMESIZE];
132 static struct hnamemem uporttable[HASHNAMESIZE];
133 static struct hnamemem eprototable[HASHNAMESIZE];
134 static struct hnamemem dnaddrtable[HASHNAMESIZE];
135 static struct hnamemem ipxsaptable[HASHNAMESIZE];
136 
137 #ifdef _WIN32
138 /*
139  * fake gethostbyaddr for Win2k/XP
140  * gethostbyaddr() returns incorrect value when AF_INET6 is passed
141  * to 3rd argument.
142  *
143  * h_name in struct hostent is only valid.
144  */
145 static struct hostent *
146 win32_gethostbyaddr(const char *addr, int len, int type)
147 {
148 	static struct hostent host;
149 	static char hostbuf[NI_MAXHOST];
150 	char hname[NI_MAXHOST];
151 	struct sockaddr_in6 addr6;
152 
153 	host.h_name = hostbuf;
154 	switch (type) {
155 	case AF_INET:
156 		return gethostbyaddr(addr, len, type);
157 		break;
158 	case AF_INET6:
159 		memset(&addr6, 0, sizeof(addr6));
160 		addr6.sin6_family = AF_INET6;
161 		memcpy(&addr6.sin6_addr, addr, len);
162 		if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
163 		    hname, sizeof(hname), NULL, 0, 0)) {
164 			return NULL;
165 		} else {
166 			strlcpy(host.h_name, hname, NI_MAXHOST);
167 			return &host;
168 		}
169 		break;
170 	default:
171 		return NULL;
172 	}
173 }
174 #define gethostbyaddr win32_gethostbyaddr
175 #endif /* _WIN32 */
176 
177 struct h6namemem {
178 	nd_ipv6 addr;
179 	char *name;
180 	struct h6namemem *nxt;
181 };
182 
183 static struct h6namemem h6nametable[HASHNAMESIZE];
184 
185 struct enamemem {
186 	u_short e_addr0;
187 	u_short e_addr1;
188 	u_short e_addr2;
189 	const char *e_name;
190 	u_char *e_nsap;			/* used only for nsaptable[] */
191 	struct enamemem *e_nxt;
192 };
193 
194 static struct enamemem enametable[HASHNAMESIZE];
195 static struct enamemem nsaptable[HASHNAMESIZE];
196 
197 struct bsnamemem {
198 	u_short bs_addr0;
199 	u_short bs_addr1;
200 	u_short bs_addr2;
201 	const char *bs_name;
202 	u_char *bs_bytes;
203 	unsigned int bs_nbytes;
204 	struct bsnamemem *bs_nxt;
205 };
206 
207 static struct bsnamemem bytestringtable[HASHNAMESIZE];
208 
209 struct protoidmem {
210 	uint32_t p_oui;
211 	u_short p_proto;
212 	const char *p_name;
213 	struct protoidmem *p_nxt;
214 };
215 
216 static struct protoidmem protoidtable[HASHNAMESIZE];
217 
218 /*
219  * A faster replacement for inet_ntoa().
220  */
221 const char *
222 intoa(uint32_t addr)
223 {
224 	char *cp;
225 	u_int byte;
226 	int n;
227 	static char buf[sizeof(".xxx.xxx.xxx.xxx")];
228 
229 	addr = ntohl(addr);
230 	cp = buf + sizeof(buf);
231 	*--cp = '\0';
232 
233 	n = 4;
234 	do {
235 		byte = addr & 0xff;
236 		*--cp = (char)(byte % 10) + '0';
237 		byte /= 10;
238 		if (byte > 0) {
239 			*--cp = (char)(byte % 10) + '0';
240 			byte /= 10;
241 			if (byte > 0)
242 				*--cp = (char)byte + '0';
243 		}
244 		*--cp = '.';
245 		addr >>= 8;
246 	} while (--n > 0);
247 
248 	return cp + 1;
249 }
250 
251 static uint32_t f_netmask;
252 static uint32_t f_localnet;
253 #ifdef HAVE_CASPER
254 cap_channel_t *capdns;
255 #endif
256 
257 /*
258  * Return a name for the IP address pointed to by ap.  This address
259  * is assumed to be in network byte order.
260  *
261  * NOTE: ap is *NOT* necessarily part of the packet data, so you
262  * *CANNOT* use the ND_TCHECK_* or ND_TTEST_* macros on it.  Furthermore,
263  * even in cases where it *is* part of the packet data, the caller
264  * would still have to check for a null return value, even if it's
265  * just printing the return value with "%s" - not all versions of
266  * printf print "(null)" with "%s" and a null pointer, some of them
267  * don't check for a null pointer and crash in that case.
268  *
269  * The callers of this routine should, before handing this routine
270  * a pointer to packet data, be sure that the data is present in
271  * the packet buffer.  They should probably do those checks anyway,
272  * as other data at that layer might not be IP addresses, and it
273  * also needs to check whether they're present in the packet buffer.
274  */
275 const char *
276 ipaddr_string(netdissect_options *ndo, const u_char *ap)
277 {
278 	struct hostent *hp;
279 	uint32_t addr;
280 	struct hnamemem *p;
281 
282 	memcpy(&addr, ap, sizeof(addr));
283 	p = &hnametable[addr & (HASHNAMESIZE-1)];
284 	for (; p->nxt; p = p->nxt) {
285 		if (p->addr == addr)
286 			return (p->name);
287 	}
288 	p->addr = addr;
289 	p->nxt = newhnamemem(ndo);
290 
291 	/*
292 	 * Print names unless:
293 	 *	(1) -n was given.
294 	 *      (2) Address is foreign and -f was given. (If -f was not
295 	 *	    given, f_netmask and f_localnet are 0 and the test
296 	 *	    evaluates to true)
297 	 */
298 	if (!ndo->ndo_nflag &&
299 	    (addr & f_netmask) == f_localnet) {
300 #ifdef HAVE_CASPER
301 		if (capdns != NULL) {
302 			hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
303 			    AF_INET);
304 		} else
305 #endif
306 			hp = gethostbyaddr((char *)&addr, 4, AF_INET);
307 		if (hp) {
308 			char *dotp;
309 
310 			p->name = strdup(hp->h_name);
311 			if (p->name == NULL)
312 				(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
313 					"%s: strdup(hp->h_name)", __func__);
314 			if (ndo->ndo_Nflag) {
315 				/* Remove domain qualifications */
316 				dotp = strchr(p->name, '.');
317 				if (dotp)
318 					*dotp = '\0';
319 			}
320 			return (p->name);
321 		}
322 	}
323 	p->name = strdup(intoa(addr));
324 	if (p->name == NULL)
325 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
326 				  "%s: strdup(intoa(addr))", __func__);
327 	return (p->name);
328 }
329 
330 /*
331  * Return a name for the IP6 address pointed to by ap.  This address
332  * is assumed to be in network byte order.
333  */
334 const char *
335 ip6addr_string(netdissect_options *ndo, const u_char *ap)
336 {
337 	struct hostent *hp;
338 	union {
339 		nd_ipv6 addr;
340 		struct for_hash_addr {
341 			char fill[14];
342 			uint16_t d;
343 		} addra;
344 	} addr;
345 	struct h6namemem *p;
346 	const char *cp;
347 	char ntop_buf[INET6_ADDRSTRLEN];
348 
349 	memcpy(&addr, ap, sizeof(addr));
350 	p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
351 	for (; p->nxt; p = p->nxt) {
352 		if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
353 			return (p->name);
354 	}
355 	memcpy(p->addr, addr.addr, sizeof(nd_ipv6));
356 	p->nxt = newh6namemem(ndo);
357 
358 	/*
359 	 * Do not print names if -n was given.
360 	 */
361 	if (!ndo->ndo_nflag) {
362 #ifdef HAVE_CASPER
363 		if (capdns != NULL) {
364 			hp = cap_gethostbyaddr(capdns, (char *)&addr,
365 			    sizeof(addr), AF_INET6);
366 		} else
367 #endif
368 			hp = gethostbyaddr((char *)&addr, sizeof(addr),
369 			    AF_INET6);
370 		if (hp) {
371 			char *dotp;
372 
373 			p->name = strdup(hp->h_name);
374 			if (p->name == NULL)
375 				(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
376 					"%s: strdup(hp->h_name)", __func__);
377 			if (ndo->ndo_Nflag) {
378 				/* Remove domain qualifications */
379 				dotp = strchr(p->name, '.');
380 				if (dotp)
381 					*dotp = '\0';
382 			}
383 			return (p->name);
384 		}
385 	}
386 	cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
387 	p->name = strdup(cp);
388 	if (p->name == NULL)
389 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
390 				  "%s: strdup(cp)", __func__);
391 	return (p->name);
392 }
393 
394 static const char hex[16] = {
395 	'0', '1', '2', '3', '4', '5', '6', '7',
396 	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
397 };
398 
399 /*
400  * Convert an octet to two hex digits.
401  *
402  * Coverity appears either:
403  *
404  *    not to believe the C standard when it asserts that a uint8_t is
405  *    exactly 8 bits in size;
406  *
407  *    not to believe that an unsigned type of exactly 8 bits has a value
408  *    in the range of 0 to 255;
409  *
410  *    not to believe that, for a range of unsigned values, if you shift
411  *    one of those values right by 4 bits, the maximum result value is
412  *    the maximum value shifted right by 4 bits, with no stray 1's shifted
413  *    in;
414  *
415  *    not to believe that 255 >> 4 is 15;
416  *
417  * so it gets upset that we're taking a "tainted" unsigned value, shifting
418  * it right 4 bits, and using it as an index into a 16-element array.
419  *
420  * So we do a stupid pointless masking of the result of the shift with
421  * 0xf, to hammer the point home to Coverity.
422  */
423 static inline char *
424 octet_to_hex(char *cp, uint8_t octet)
425 {
426 	*cp++ = hex[(octet >> 4) & 0xf];
427 	*cp++ = hex[(octet >> 0) & 0xf];
428 	return (cp);
429 }
430 
431 /* Find the hash node that corresponds the ether address 'ep' */
432 
433 static struct enamemem *
434 lookup_emem(netdissect_options *ndo, const u_char *ep)
435 {
436 	u_int i, j, k;
437 	struct enamemem *tp;
438 
439 	k = (ep[0] << 8) | ep[1];
440 	j = (ep[2] << 8) | ep[3];
441 	i = (ep[4] << 8) | ep[5];
442 
443 	tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
444 	while (tp->e_nxt)
445 		if (tp->e_addr0 == i &&
446 		    tp->e_addr1 == j &&
447 		    tp->e_addr2 == k)
448 			return tp;
449 		else
450 			tp = tp->e_nxt;
451 	tp->e_addr0 = (u_short)i;
452 	tp->e_addr1 = (u_short)j;
453 	tp->e_addr2 = (u_short)k;
454 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
455 	if (tp->e_nxt == NULL)
456 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
457 
458 	return tp;
459 }
460 
461 /*
462  * Find the hash node that corresponds to the bytestring 'bs'
463  * with length 'nlen'
464  */
465 
466 static struct bsnamemem *
467 lookup_bytestring(netdissect_options *ndo, const u_char *bs,
468 		  const unsigned int nlen)
469 {
470 	struct bsnamemem *tp;
471 	u_int i, j, k;
472 
473 	if (nlen >= 6) {
474 		k = (bs[0] << 8) | bs[1];
475 		j = (bs[2] << 8) | bs[3];
476 		i = (bs[4] << 8) | bs[5];
477 	} else if (nlen >= 4) {
478 		k = (bs[0] << 8) | bs[1];
479 		j = (bs[2] << 8) | bs[3];
480 		i = 0;
481 	} else
482 		i = j = k = 0;
483 
484 	tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
485 	while (tp->bs_nxt)
486 		if (nlen == tp->bs_nbytes &&
487 		    tp->bs_addr0 == i &&
488 		    tp->bs_addr1 == j &&
489 		    tp->bs_addr2 == k &&
490 		    memcmp((const char *)bs, (const char *)(tp->bs_bytes), nlen) == 0)
491 			return tp;
492 		else
493 			tp = tp->bs_nxt;
494 
495 	tp->bs_addr0 = (u_short)i;
496 	tp->bs_addr1 = (u_short)j;
497 	tp->bs_addr2 = (u_short)k;
498 
499 	tp->bs_bytes = (u_char *) calloc(1, nlen);
500 	if (tp->bs_bytes == NULL)
501 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
502 				  "%s: calloc", __func__);
503 
504 	memcpy(tp->bs_bytes, bs, nlen);
505 	tp->bs_nbytes = nlen;
506 	tp->bs_nxt = (struct bsnamemem *)calloc(1, sizeof(*tp));
507 	if (tp->bs_nxt == NULL)
508 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
509 				  "%s: calloc", __func__);
510 
511 	return tp;
512 }
513 
514 /* Find the hash node that corresponds the NSAP 'nsap' */
515 
516 static struct enamemem *
517 lookup_nsap(netdissect_options *ndo, const u_char *nsap,
518 	    u_int nsap_length)
519 {
520 	u_int i, j, k;
521 	struct enamemem *tp;
522 	const u_char *ensap;
523 
524 	if (nsap_length > 6) {
525 		ensap = nsap + nsap_length - 6;
526 		k = (ensap[0] << 8) | ensap[1];
527 		j = (ensap[2] << 8) | ensap[3];
528 		i = (ensap[4] << 8) | ensap[5];
529 	}
530 	else
531 		i = j = k = 0;
532 
533 	tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
534 	while (tp->e_nxt)
535 		if (nsap_length == tp->e_nsap[0] &&
536 		    tp->e_addr0 == i &&
537 		    tp->e_addr1 == j &&
538 		    tp->e_addr2 == k &&
539 		    memcmp((const char *)nsap,
540 			(char *)&(tp->e_nsap[1]), nsap_length) == 0)
541 			return tp;
542 		else
543 			tp = tp->e_nxt;
544 	tp->e_addr0 = (u_short)i;
545 	tp->e_addr1 = (u_short)j;
546 	tp->e_addr2 = (u_short)k;
547 	tp->e_nsap = (u_char *)malloc(nsap_length + 1);
548 	if (tp->e_nsap == NULL)
549 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: malloc", __func__);
550 	tp->e_nsap[0] = (u_char)nsap_length;	/* guaranteed < ISONSAP_MAX_LENGTH */
551 	memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
552 	tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
553 	if (tp->e_nxt == NULL)
554 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
555 
556 	return tp;
557 }
558 
559 /* Find the hash node that corresponds the protoid 'pi'. */
560 
561 static struct protoidmem *
562 lookup_protoid(netdissect_options *ndo, const u_char *pi)
563 {
564 	u_int i, j;
565 	struct protoidmem *tp;
566 
567 	/* 5 octets won't be aligned */
568 	i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
569 	j =   (pi[3] << 8) + pi[4];
570 	/* XXX should be endian-insensitive, but do big-endian testing  XXX */
571 
572 	tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
573 	while (tp->p_nxt)
574 		if (tp->p_oui == i && tp->p_proto == j)
575 			return tp;
576 		else
577 			tp = tp->p_nxt;
578 	tp->p_oui = i;
579 	tp->p_proto = (u_short)j;
580 	tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
581 	if (tp->p_nxt == NULL)
582 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
583 
584 	return tp;
585 }
586 
587 const char *
588 etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
589 {
590 	int i;
591 	char *cp;
592 	struct enamemem *tp;
593 	int oui;
594 	char buf[BUFSIZE];
595 
596 	tp = lookup_emem(ndo, ep);
597 	if (tp->e_name)
598 		return (tp->e_name);
599 #ifdef USE_ETHER_NTOHOST
600 	if (!ndo->ndo_nflag) {
601 		char buf2[BUFSIZE];
602 		/*
603 		 * This is a non-const copy of ep for ether_ntohost(), which
604 		 * has its second argument non-const in OpenBSD. Also saves a
605 		 * type cast.
606 		 */
607 		struct ether_addr ea;
608 
609 		memcpy (&ea, ep, MAC_ADDR_LEN);
610 		if (ether_ntohost(buf2, &ea) == 0) {
611 			tp->e_name = strdup(buf2);
612 			if (tp->e_name == NULL)
613 				(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
614 					"%s: strdup(buf2)", __func__);
615 			return (tp->e_name);
616 		}
617 	}
618 #endif
619 	cp = buf;
620 	oui = EXTRACT_BE_U_3(ep);
621 	cp = octet_to_hex(cp, *ep++);
622 	for (i = 5; --i >= 0;) {
623 		*cp++ = ':';
624 		cp = octet_to_hex(cp, *ep++);
625 	}
626 
627 	if (!ndo->ndo_nflag) {
628 		snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
629 		    tok2str(oui_values, "Unknown", oui));
630 	} else
631 		*cp = '\0';
632 	tp->e_name = strdup(buf);
633 	if (tp->e_name == NULL)
634 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
635 				  "%s: strdup(buf)", __func__);
636 	return (tp->e_name);
637 }
638 
639 const char *
640 le64addr_string(netdissect_options *ndo, const uint8_t *ep)
641 {
642 	const unsigned int len = 8;
643 	u_int i;
644 	char *cp;
645 	struct bsnamemem *tp;
646 	char buf[BUFSIZE];
647 
648 	tp = lookup_bytestring(ndo, ep, len);
649 	if (tp->bs_name)
650 		return (tp->bs_name);
651 
652 	cp = buf;
653 	for (i = len; i > 0 ; --i) {
654 		cp = octet_to_hex(cp, *(ep + i - 1));
655 		*cp++ = ':';
656 	}
657 	cp --;
658 
659 	*cp = '\0';
660 
661 	tp->bs_name = strdup(buf);
662 	if (tp->bs_name == NULL)
663 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
664 				  "%s: strdup(buf)", __func__);
665 
666 	return (tp->bs_name);
667 }
668 
669 const char *
670 linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
671 		const unsigned int type, const unsigned int len)
672 {
673 	u_int i;
674 	char *cp;
675 	struct bsnamemem *tp;
676 
677 	if (len == 0)
678 		return ("<empty>");
679 
680 	if (type == LINKADDR_ETHER && len == MAC_ADDR_LEN)
681 		return (etheraddr_string(ndo, ep));
682 
683 	if (type == LINKADDR_FRELAY)
684 		return (q922_string(ndo, ep, len));
685 
686 	tp = lookup_bytestring(ndo, ep, len);
687 	if (tp->bs_name)
688 		return (tp->bs_name);
689 
690 	tp->bs_name = cp = (char *)malloc(len*3);
691 	if (tp->bs_name == NULL)
692 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
693 				  "%s: malloc", __func__);
694 	cp = octet_to_hex(cp, *ep++);
695 	for (i = len-1; i > 0 ; --i) {
696 		*cp++ = ':';
697 		cp = octet_to_hex(cp, *ep++);
698 	}
699 	*cp = '\0';
700 	return (tp->bs_name);
701 }
702 
703 #define ISONSAP_MAX_LENGTH 20
704 const char *
705 isonsap_string(netdissect_options *ndo, const uint8_t *nsap,
706 	       u_int nsap_length)
707 {
708 	u_int nsap_idx;
709 	char *cp;
710 	struct enamemem *tp;
711 
712 	if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
713 		return ("isonsap_string: illegal length");
714 
715 	tp = lookup_nsap(ndo, nsap, nsap_length);
716 	if (tp->e_name)
717 		return tp->e_name;
718 
719 	tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
720 	if (cp == NULL)
721 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
722 				  "%s: malloc", __func__);
723 
724 	for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
725 		cp = octet_to_hex(cp, *nsap++);
726 		if (((nsap_idx & 1) == 0) &&
727 		     (nsap_idx + 1 < nsap_length)) {
728 			*cp++ = '.';
729 		}
730 	}
731 	*cp = '\0';
732 	return (tp->e_name);
733 }
734 
735 const char *
736 tcpport_string(netdissect_options *ndo, u_short port)
737 {
738 	struct hnamemem *tp;
739 	uint32_t i = port;
740 	char buf[sizeof("00000")];
741 
742 	for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
743 		if (tp->addr == i)
744 			return (tp->name);
745 
746 	tp->addr = i;
747 	tp->nxt = newhnamemem(ndo);
748 
749 	(void)snprintf(buf, sizeof(buf), "%u", i);
750 	tp->name = strdup(buf);
751 	if (tp->name == NULL)
752 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
753 				  "%s: strdup(buf)", __func__);
754 	return (tp->name);
755 }
756 
757 const char *
758 udpport_string(netdissect_options *ndo, u_short port)
759 {
760 	struct hnamemem *tp;
761 	uint32_t i = port;
762 	char buf[sizeof("00000")];
763 
764 	for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
765 		if (tp->addr == i)
766 			return (tp->name);
767 
768 	tp->addr = i;
769 	tp->nxt = newhnamemem(ndo);
770 
771 	(void)snprintf(buf, sizeof(buf), "%u", i);
772 	tp->name = strdup(buf);
773 	if (tp->name == NULL)
774 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
775 				  "%s: strdup(buf)", __func__);
776 	return (tp->name);
777 }
778 
779 const char *
780 ipxsap_string(netdissect_options *ndo, u_short port)
781 {
782 	char *cp;
783 	struct hnamemem *tp;
784 	uint32_t i = port;
785 	char buf[sizeof("0000")];
786 
787 	for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
788 		if (tp->addr == i)
789 			return (tp->name);
790 
791 	tp->addr = i;
792 	tp->nxt = newhnamemem(ndo);
793 
794 	cp = buf;
795 	port = ntohs(port);
796 	*cp++ = hex[port >> 12 & 0xf];
797 	*cp++ = hex[port >> 8 & 0xf];
798 	*cp++ = hex[port >> 4 & 0xf];
799 	*cp++ = hex[port & 0xf];
800 	*cp++ = '\0';
801 	tp->name = strdup(buf);
802 	if (tp->name == NULL)
803 		(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
804 				  "%s: strdup(buf)", __func__);
805 	return (tp->name);
806 }
807 
808 static void
809 init_servarray(netdissect_options *ndo)
810 {
811 	struct servent *sv;
812 	struct hnamemem *table;
813 	int i;
814 	char buf[sizeof("0000000000")];
815 
816 	while ((sv = getservent()) != NULL) {
817 		int port = ntohs(sv->s_port);
818 		i = port & (HASHNAMESIZE-1);
819 		if (strcmp(sv->s_proto, "tcp") == 0)
820 			table = &tporttable[i];
821 		else if (strcmp(sv->s_proto, "udp") == 0)
822 			table = &uporttable[i];
823 		else
824 			continue;
825 
826 		while (table->name)
827 			table = table->nxt;
828 		if (ndo->ndo_nflag) {
829 			(void)snprintf(buf, sizeof(buf), "%d", port);
830 			table->name = strdup(buf);
831 		} else
832 			table->name = strdup(sv->s_name);
833 		if (table->name == NULL)
834 			(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
835 					  "%s: strdup", __func__);
836 
837 		table->addr = port;
838 		table->nxt = newhnamemem(ndo);
839 	}
840 	endservent();
841 }
842 
843 static const struct eproto {
844 	const char *s;
845 	u_short p;
846 } eproto_db[] = {
847 	{ "aarp", ETHERTYPE_AARP },
848 	{ "arp", ETHERTYPE_ARP },
849 	{ "atalk", ETHERTYPE_ATALK },
850 	{ "decnet", ETHERTYPE_DN },
851 	{ "ip", ETHERTYPE_IP },
852 	{ "ip6", ETHERTYPE_IPV6 },
853 	{ "lat", ETHERTYPE_LAT },
854 	{ "loopback", ETHERTYPE_LOOPBACK },
855 	{ "mopdl", ETHERTYPE_MOPDL },
856 	{ "moprc", ETHERTYPE_MOPRC },
857 	{ "rarp", ETHERTYPE_REVARP },
858 	{ "sca", ETHERTYPE_SCA },
859 	{ (char *)0, 0 }
860 };
861 
862 static void
863 init_eprotoarray(netdissect_options *ndo)
864 {
865 	int i;
866 	struct hnamemem *table;
867 
868 	for (i = 0; eproto_db[i].s; i++) {
869 		int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
870 		table = &eprototable[j];
871 		while (table->name)
872 			table = table->nxt;
873 		table->name = eproto_db[i].s;
874 		table->addr = htons(eproto_db[i].p);
875 		table->nxt = newhnamemem(ndo);
876 	}
877 }
878 
879 static const struct protoidlist {
880 	const u_char protoid[5];
881 	const char *name;
882 } protoidlist[] = {
883 	{{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
884 	{{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
885 	{{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
886 	{{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
887 	{{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
888 	{{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
889 };
890 
891 /*
892  * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
893  * types.
894  */
895 static void
896 init_protoidarray(netdissect_options *ndo)
897 {
898 	int i;
899 	struct protoidmem *tp;
900 	const struct protoidlist *pl;
901 	u_char protoid[5];
902 
903 	protoid[0] = 0;
904 	protoid[1] = 0;
905 	protoid[2] = 0;
906 	for (i = 0; eproto_db[i].s; i++) {
907 		u_short etype = htons(eproto_db[i].p);
908 
909 		memcpy((char *)&protoid[3], (char *)&etype, 2);
910 		tp = lookup_protoid(ndo, protoid);
911 		tp->p_name = strdup(eproto_db[i].s);
912 		if (tp->p_name == NULL)
913 			(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
914 				"%s: strdup(eproto_db[i].s)", __func__);
915 	}
916 	/* Hardwire some SNAP proto ID names */
917 	for (pl = protoidlist; pl->name != NULL; ++pl) {
918 		tp = lookup_protoid(ndo, pl->protoid);
919 		/* Don't override existing name */
920 		if (tp->p_name != NULL)
921 			continue;
922 
923 		tp->p_name = pl->name;
924 	}
925 }
926 
927 static const struct etherlist {
928 	const nd_mac_addr addr;
929 	const char *name;
930 } etherlist[] = {
931 	{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
932 	{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
933 };
934 
935 /*
936  * Initialize the ethers hash table.  We take two different approaches
937  * depending on whether or not the system provides the ethers name
938  * service.  If it does, we just wire in a few names at startup,
939  * and etheraddr_string() fills in the table on demand.  If it doesn't,
940  * then we suck in the entire /etc/ethers file at startup.  The idea
941  * is that parsing the local file will be fast, but spinning through
942  * all the ethers entries via NIS & next_etherent might be very slow.
943  *
944  * XXX pcap_next_etherent doesn't belong in the pcap interface, but
945  * since the pcap module already does name-to-address translation,
946  * it's already does most of the work for the ethernet address-to-name
947  * translation, so we just pcap_next_etherent as a convenience.
948  */
949 static void
950 init_etherarray(netdissect_options *ndo)
951 {
952 	const struct etherlist *el;
953 	struct enamemem *tp;
954 #ifdef USE_ETHER_NTOHOST
955 	char name[256];
956 #else
957 	struct pcap_etherent *ep;
958 	FILE *fp;
959 
960 	/* Suck in entire ethers file */
961 	fp = fopen(PCAP_ETHERS_FILE, "r");
962 	if (fp != NULL) {
963 		while ((ep = pcap_next_etherent(fp)) != NULL) {
964 			tp = lookup_emem(ndo, ep->addr);
965 			tp->e_name = strdup(ep->name);
966 			if (tp->e_name == NULL)
967 				(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
968 					"%s: strdup(ep->addr)", __func__);
969 		}
970 		(void)fclose(fp);
971 	}
972 #endif
973 
974 	/* Hardwire some ethernet names */
975 	for (el = etherlist; el->name != NULL; ++el) {
976 		tp = lookup_emem(ndo, el->addr);
977 		/* Don't override existing name */
978 		if (tp->e_name != NULL)
979 			continue;
980 
981 #ifdef USE_ETHER_NTOHOST
982 		/*
983 		 * Use YP/NIS version of name if available.
984 		 */
985 		/* Same workaround as in etheraddr_string(). */
986 		struct ether_addr ea;
987 		memcpy (&ea, el->addr, MAC_ADDR_LEN);
988 		if (ether_ntohost(name, &ea) == 0) {
989 			tp->e_name = strdup(name);
990 			if (tp->e_name == NULL)
991 				(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
992 					"%s: strdup(name)", __func__);
993 			continue;
994 		}
995 #endif
996 		tp->e_name = el->name;
997 	}
998 }
999 
1000 static const struct ipxsap_ent {
1001 	uint16_t	v;
1002 	const char	*s;
1003 } ipxsap_db[] = {
1004 	{ 0x0000, "Unknown" },
1005 	{ 0x0001, "User" },
1006 	{ 0x0002, "User Group" },
1007 	{ 0x0003, "PrintQueue" },
1008 	{ 0x0004, "FileServer" },
1009 	{ 0x0005, "JobServer" },
1010 	{ 0x0006, "Gateway" },
1011 	{ 0x0007, "PrintServer" },
1012 	{ 0x0008, "ArchiveQueue" },
1013 	{ 0x0009, "ArchiveServer" },
1014 	{ 0x000a, "JobQueue" },
1015 	{ 0x000b, "Administration" },
1016 	{ 0x000F, "Novell TI-RPC" },
1017 	{ 0x0017, "Diagnostics" },
1018 	{ 0x0020, "NetBIOS" },
1019 	{ 0x0021, "NAS SNA Gateway" },
1020 	{ 0x0023, "NACS AsyncGateway" },
1021 	{ 0x0024, "RemoteBridge/RoutingService" },
1022 	{ 0x0026, "BridgeServer" },
1023 	{ 0x0027, "TCP/IP Gateway" },
1024 	{ 0x0028, "Point-to-point X.25 BridgeServer" },
1025 	{ 0x0029, "3270 Gateway" },
1026 	{ 0x002a, "CHI Corp" },
1027 	{ 0x002c, "PC Chalkboard" },
1028 	{ 0x002d, "TimeSynchServer" },
1029 	{ 0x002e, "ARCserve5.0/PalindromeBackup" },
1030 	{ 0x0045, "DI3270 Gateway" },
1031 	{ 0x0047, "AdvertisingPrintServer" },
1032 	{ 0x004a, "NetBlazerModems" },
1033 	{ 0x004b, "BtrieveVAP" },
1034 	{ 0x004c, "NetwareSQL" },
1035 	{ 0x004d, "XtreeNetwork" },
1036 	{ 0x0050, "BtrieveVAP4.11" },
1037 	{ 0x0052, "QuickLink" },
1038 	{ 0x0053, "PrintQueueUser" },
1039 	{ 0x0058, "Multipoint X.25 Router" },
1040 	{ 0x0060, "STLB/NLM" },
1041 	{ 0x0064, "ARCserve" },
1042 	{ 0x0066, "ARCserve3.0" },
1043 	{ 0x0072, "WAN CopyUtility" },
1044 	{ 0x007a, "TES-NetwareVMS" },
1045 	{ 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
1046 	{ 0x0095, "DDA OBGYN" },
1047 	{ 0x0098, "NetwareAccessServer" },
1048 	{ 0x009a, "Netware for VMS II/NamedPipeServer" },
1049 	{ 0x009b, "NetwareAccessServer" },
1050 	{ 0x009e, "PortableNetwareServer/SunLinkNVT" },
1051 	{ 0x00a1, "PowerchuteAPC UPS" },
1052 	{ 0x00aa, "LAWserve" },
1053 	{ 0x00ac, "CompaqIDA StatusMonitor" },
1054 	{ 0x0100, "PIPE STAIL" },
1055 	{ 0x0102, "LAN ProtectBindery" },
1056 	{ 0x0103, "OracleDataBaseServer" },
1057 	{ 0x0107, "Netware386/RSPX RemoteConsole" },
1058 	{ 0x010f, "NovellSNA Gateway" },
1059 	{ 0x0111, "TestServer" },
1060 	{ 0x0112, "HP PrintServer" },
1061 	{ 0x0114, "CSA MUX" },
1062 	{ 0x0115, "CSA LCA" },
1063 	{ 0x0116, "CSA CM" },
1064 	{ 0x0117, "CSA SMA" },
1065 	{ 0x0118, "CSA DBA" },
1066 	{ 0x0119, "CSA NMA" },
1067 	{ 0x011a, "CSA SSA" },
1068 	{ 0x011b, "CSA STATUS" },
1069 	{ 0x011e, "CSA APPC" },
1070 	{ 0x0126, "SNA TEST SSA Profile" },
1071 	{ 0x012a, "CSA TRACE" },
1072 	{ 0x012b, "NetwareSAA" },
1073 	{ 0x012e, "IKARUS VirusScan" },
1074 	{ 0x0130, "CommunicationsExecutive" },
1075 	{ 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1076 	{ 0x0135, "NetwareNamingServicesProfile" },
1077 	{ 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1078 	{ 0x0141, "LAN SpoolServer" },
1079 	{ 0x0152, "IRMALAN Gateway" },
1080 	{ 0x0154, "NamedPipeServer" },
1081 	{ 0x0166, "NetWareManagement" },
1082 	{ 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1083 	{ 0x0173, "Compaq" },
1084 	{ 0x0174, "Compaq SNMP Agent" },
1085 	{ 0x0175, "Compaq" },
1086 	{ 0x0180, "XTreeServer/XTreeTools" },
1087 	{ 0x018A, "NASI ServicesBroadcastServer" },
1088 	{ 0x01b0, "GARP Gateway" },
1089 	{ 0x01b1, "Binfview" },
1090 	{ 0x01bf, "IntelLanDeskManager" },
1091 	{ 0x01ca, "AXTEC" },
1092 	{ 0x01cb, "ShivaNetModem/E" },
1093 	{ 0x01cc, "ShivaLanRover/E" },
1094 	{ 0x01cd, "ShivaLanRover/T" },
1095 	{ 0x01ce, "ShivaUniversal" },
1096 	{ 0x01d8, "CastelleFAXPressServer" },
1097 	{ 0x01da, "CastelleLANPressPrintServer" },
1098 	{ 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1099 	{ 0x01f0, "LEGATO" },
1100 	{ 0x01f5, "LEGATO" },
1101 	{ 0x0233, "NMS Agent/NetwareManagementAgent" },
1102 	{ 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1103 	{ 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1104 	{ 0x023a, "LANtern" },
1105 	{ 0x023c, "MAVERICK" },
1106 	{ 0x023f, "NovellSMDR" },
1107 	{ 0x024e, "NetwareConnect" },
1108 	{ 0x024f, "NASI ServerBroadcast Cisco" },
1109 	{ 0x026a, "NMS ServiceConsole" },
1110 	{ 0x026b, "TimeSynchronizationServer Netware 4.x" },
1111 	{ 0x0278, "DirectoryServer Netware 4.x" },
1112 	{ 0x027b, "NetwareManagementAgent" },
1113 	{ 0x0280, "Novell File and Printer Sharing Service for PC" },
1114 	{ 0x0304, "NovellSAA Gateway" },
1115 	{ 0x0308, "COM/VERMED" },
1116 	{ 0x030a, "GalacticommWorldgroupServer" },
1117 	{ 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1118 	{ 0x0320, "AttachmateGateway" },
1119 	{ 0x0327, "MicrosoftDiagnostiocs" },
1120 	{ 0x0328, "WATCOM SQL Server" },
1121 	{ 0x0335, "MultiTechSystems MultisynchCommServer" },
1122 	{ 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1123 	{ 0x0355, "ArcadaBackupExec" },
1124 	{ 0x0358, "MSLCD1" },
1125 	{ 0x0361, "NETINELO" },
1126 	{ 0x037e, "Powerchute UPS Monitoring" },
1127 	{ 0x037f, "ViruSafeNotify" },
1128 	{ 0x0386, "HP Bridge" },
1129 	{ 0x0387, "HP Hub" },
1130 	{ 0x0394, "NetWare SAA Gateway" },
1131 	{ 0x039b, "LotusNotes" },
1132 	{ 0x03b7, "CertusAntiVirus" },
1133 	{ 0x03c4, "ARCserve4.0" },
1134 	{ 0x03c7, "LANspool3.5" },
1135 	{ 0x03d7, "LexmarkPrinterServer" },
1136 	{ 0x03d8, "LexmarkXLE PrinterServer" },
1137 	{ 0x03dd, "BanyanENS NetwareClient" },
1138 	{ 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1139 	{ 0x03e1, "UnivelUnixware" },
1140 	{ 0x03e4, "UnivelUnixware" },
1141 	{ 0x03fc, "IntelNetport" },
1142 	{ 0x03fd, "PrintServerQueue" },
1143 	{ 0x040A, "ipnServer" },
1144 	{ 0x040D, "LVERRMAN" },
1145 	{ 0x040E, "LVLIC" },
1146 	{ 0x0414, "NET Silicon (DPI)/Kyocera" },
1147 	{ 0x0429, "SiteLockVirus" },
1148 	{ 0x0432, "UFHELPR???" },
1149 	{ 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1150 	{ 0x0444, "MicrosoftNT SNA Server" },
1151 	{ 0x0448, "Oracle" },
1152 	{ 0x044c, "ARCserve5.01" },
1153 	{ 0x0457, "CanonGP55" },
1154 	{ 0x045a, "QMS Printers" },
1155 	{ 0x045b, "DellSCSI Array" },
1156 	{ 0x0491, "NetBlazerModems" },
1157 	{ 0x04ac, "OnTimeScheduler" },
1158 	{ 0x04b0, "CD-Net" },
1159 	{ 0x0513, "EmulexNQA" },
1160 	{ 0x0520, "SiteLockChecks" },
1161 	{ 0x0529, "SiteLockChecks" },
1162 	{ 0x052d, "CitrixOS2 AppServer" },
1163 	{ 0x0535, "Tektronix" },
1164 	{ 0x0536, "Milan" },
1165 	{ 0x055d, "Attachmate SNA gateway" },
1166 	{ 0x056b, "IBM8235 ModemServer" },
1167 	{ 0x056c, "ShivaLanRover/E PLUS" },
1168 	{ 0x056d, "ShivaLanRover/T PLUS" },
1169 	{ 0x0580, "McAfeeNetShield" },
1170 	{ 0x05B8, "NLM to workstation communication (Revelation Software)" },
1171 	{ 0x05BA, "CompatibleSystemsRouters" },
1172 	{ 0x05BE, "CheyenneHierarchicalStorageManager" },
1173 	{ 0x0606, "JCWatermarkImaging" },
1174 	{ 0x060c, "AXISNetworkPrinter" },
1175 	{ 0x0610, "AdaptecSCSIManagement" },
1176 	{ 0x0621, "IBM AntiVirus" },
1177 	{ 0x0640, "Windows95 RemoteRegistryService" },
1178 	{ 0x064e, "MicrosoftIIS" },
1179 	{ 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1180 	{ 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1181 	{ 0x076C, "Xerox" },
1182 	{ 0x079b, "ShivaLanRover/E 115" },
1183 	{ 0x079c, "ShivaLanRover/T 115" },
1184 	{ 0x07B4, "CubixWorldDesk" },
1185 	{ 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1186 	{ 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1187 	{ 0x0810, "ELAN License Server Demo" },
1188 	{ 0x0824, "ShivaLanRoverAccessSwitch/E" },
1189 	{ 0x086a, "ISSC Collector" },
1190 	{ 0x087f, "ISSC DAS AgentAIX" },
1191 	{ 0x0880, "Intel Netport PRO" },
1192 	{ 0x0881, "Intel Netport PRO" },
1193 	{ 0x0b29, "SiteLock" },
1194 	{ 0x0c29, "SiteLockApplications" },
1195 	{ 0x0c2c, "LicensingServer" },
1196 	{ 0x2101, "PerformanceTechnologyInstantInternet" },
1197 	{ 0x2380, "LAI SiteLock" },
1198 	{ 0x238c, "MeetingMaker" },
1199 	{ 0x4808, "SiteLockServer/SiteLockMetering" },
1200 	{ 0x5555, "SiteLockUser" },
1201 	{ 0x6312, "Tapeware" },
1202 	{ 0x6f00, "RabbitGateway" },
1203 	{ 0x7703, "MODEM" },
1204 	{ 0x8002, "NetPortPrinters" },
1205 	{ 0x8008, "WordPerfectNetworkVersion" },
1206 	{ 0x85BE, "Cisco EIGRP" },
1207 	{ 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1208 	{ 0x9000, "McAfeeNetShield" },
1209 	{ 0x9604, "CSA-NT_MON" },
1210 	{ 0xb6a8, "OceanIsleReachoutRemoteControl" },
1211 	{ 0xf11f, "SiteLockMetering" },
1212 	{ 0xf1ff, "SiteLock" },
1213 	{ 0xf503, "Microsoft SQL Server" },
1214 	{ 0xF905, "IBM TimeAndPlace" },
1215 	{ 0xfbfb, "TopCallIII FaxServer" },
1216 	{ 0xffff, "AnyService/Wildcard" },
1217 	{ 0, (char *)0 }
1218 };
1219 
1220 static void
1221 init_ipxsaparray(netdissect_options *ndo)
1222 {
1223 	int i;
1224 	struct hnamemem *table;
1225 
1226 	for (i = 0; ipxsap_db[i].s != NULL; i++) {
1227 		u_int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1228 		table = &ipxsaptable[j];
1229 		while (table->name)
1230 			table = table->nxt;
1231 		table->name = ipxsap_db[i].s;
1232 		table->addr = htons(ipxsap_db[i].v);
1233 		table->nxt = newhnamemem(ndo);
1234 	}
1235 }
1236 
1237 /*
1238  * Initialize the address to name translation machinery.  We map all
1239  * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1240  * (i.e., to prevent blocking on the nameserver).  localnet is the IP address
1241  * of the local network.  mask is its subnet mask.
1242  */
1243 void
1244 init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
1245 {
1246 	if (ndo->ndo_fflag) {
1247 		f_localnet = localnet;
1248 		f_netmask = mask;
1249 	}
1250 	if (ndo->ndo_nflag)
1251 		/*
1252 		 * Simplest way to suppress names.
1253 		 */
1254 		return;
1255 
1256 	init_etherarray(ndo);
1257 	init_servarray(ndo);
1258 	init_eprotoarray(ndo);
1259 	init_protoidarray(ndo);
1260 	init_ipxsaparray(ndo);
1261 }
1262 
1263 const char *
1264 dnaddr_string(netdissect_options *ndo, u_short dnaddr)
1265 {
1266 	struct hnamemem *tp;
1267 
1268 	for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
1269 	     tp = tp->nxt)
1270 		if (tp->addr == dnaddr)
1271 			return (tp->name);
1272 
1273 	tp->addr = dnaddr;
1274 	tp->nxt = newhnamemem(ndo);
1275 	tp->name = dnnum_string(ndo, dnaddr);
1276 
1277 	return(tp->name);
1278 }
1279 
1280 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1281 struct hnamemem *
1282 newhnamemem(netdissect_options *ndo)
1283 {
1284 	struct hnamemem *p;
1285 	static struct hnamemem *ptr = NULL;
1286 	static u_int num = 0;
1287 
1288 	if (num  == 0) {
1289 		num = 64;
1290 		ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1291 		if (ptr == NULL)
1292 			(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1293 					  "%s: calloc", __func__);
1294 	}
1295 	--num;
1296 	p = ptr++;
1297 	return (p);
1298 }
1299 
1300 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1301 struct h6namemem *
1302 newh6namemem(netdissect_options *ndo)
1303 {
1304 	struct h6namemem *p;
1305 	static struct h6namemem *ptr = NULL;
1306 	static u_int num = 0;
1307 
1308 	if (num  == 0) {
1309 		num = 64;
1310 		ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1311 		if (ptr == NULL)
1312 			(*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1313 					  "%s: calloc", __func__);
1314 	}
1315 	--num;
1316 	p = ptr++;
1317 	return (p);
1318 }
1319 
1320 /* Represent TCI part of the 802.1Q 4-octet tag as text. */
1321 const char *
1322 ieee8021q_tci_string(const uint16_t tci)
1323 {
1324 	static char buf[128];
1325 	snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1326 	         tci & 0xfff,
1327 	         tci >> 13,
1328 	         (tci & 0x1000) ? ", DEI" : "");
1329 	return buf;
1330 }
1331