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