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