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