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