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