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