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