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