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