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