kmalloc: Avoid code duplication.
[dragonfly.git] / contrib / tcpdump / addrtoname.c
blobd77f27b0ac4fd999e29cf48fa8f49df82270582a
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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.
21 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
24 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.119 2007-08-08 14:06:34 hannes Exp $ (LBL)";
27 #endif
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <tcpdump-stdinc.h>
35 #ifdef USE_ETHER_NTOHOST
36 #ifdef HAVE_NETINET_IF_ETHER_H
37 struct mbuf; /* Squelch compiler warnings on some platforms for */
38 struct rtentry; /* declarations in <net/if.h> */
39 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
40 #include <netinet/if_ether.h>
41 #endif /* HAVE_NETINET_IF_ETHER_H */
42 #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
43 #include <netinet/ether.h>
44 #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
46 #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
47 #ifndef HAVE_STRUCT_ETHER_ADDR
48 struct ether_addr {
49 unsigned char ether_addr_octet[6];
51 #endif
52 extern int ether_ntohost(char *, const struct ether_addr *);
53 #endif
55 #endif /* USE_ETHER_NTOHOST */
57 #include <pcap.h>
58 #include <pcap-namedb.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <stdlib.h>
64 #include "interface.h"
65 #include "addrtoname.h"
66 #include "llc.h"
67 #include "setsignal.h"
68 #include "extract.h"
69 #include "oui.h"
71 #ifndef ETHER_ADDR_LEN
72 #define ETHER_ADDR_LEN 6
73 #endif
76 * hash tables for whatever-to-name translations
78 * XXX there has to be error checks against strdup(3) failure
81 #define HASHNAMESIZE 4096
83 struct hnamemem {
84 u_int32_t addr;
85 const char *name;
86 struct hnamemem *nxt;
89 static struct hnamemem hnametable[HASHNAMESIZE];
90 static struct hnamemem tporttable[HASHNAMESIZE];
91 static struct hnamemem uporttable[HASHNAMESIZE];
92 static struct hnamemem eprototable[HASHNAMESIZE];
93 static struct hnamemem dnaddrtable[HASHNAMESIZE];
94 static struct hnamemem ipxsaptable[HASHNAMESIZE];
96 #if defined(INET6) && defined(WIN32)
98 * fake gethostbyaddr for Win2k/XP
99 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
100 * to 3rd argument.
102 * h_name in struct hostent is only valid.
104 static struct hostent *
105 win32_gethostbyaddr(const char *addr, int len, int type)
107 static struct hostent host;
108 static char hostbuf[NI_MAXHOST];
109 char hname[NI_MAXHOST];
110 struct sockaddr_in6 addr6;
112 host.h_name = hostbuf;
113 switch (type) {
114 case AF_INET:
115 return gethostbyaddr(addr, len, type);
116 break;
117 case AF_INET6:
118 memset(&addr6, 0, sizeof(addr6));
119 addr6.sin6_family = AF_INET6;
120 memcpy(&addr6.sin6_addr, addr, len);
121 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
122 hname, sizeof(hname), NULL, 0, 0)) {
123 return NULL;
124 } else {
125 strcpy(host.h_name, hname);
126 return &host;
128 break;
129 default:
130 return NULL;
133 #define gethostbyaddr win32_gethostbyaddr
134 #endif /* INET6 & WIN32 */
136 #ifdef INET6
137 struct h6namemem {
138 struct in6_addr addr;
139 char *name;
140 struct h6namemem *nxt;
143 static struct h6namemem h6nametable[HASHNAMESIZE];
144 #endif /* INET6 */
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 #define e_bs e_nsap /* for bytestringtable */
153 struct enamemem *e_nxt;
156 static struct enamemem enametable[HASHNAMESIZE];
157 static struct enamemem nsaptable[HASHNAMESIZE];
158 static struct enamemem bytestringtable[HASHNAMESIZE];
160 struct protoidmem {
161 u_int32_t p_oui;
162 u_short p_proto;
163 const char *p_name;
164 struct protoidmem *p_nxt;
167 static struct protoidmem protoidtable[HASHNAMESIZE];
170 * A faster replacement for inet_ntoa().
172 const char *
173 intoa(u_int32_t addr)
175 register char *cp;
176 register u_int byte;
177 register int n;
178 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
180 NTOHL(addr);
181 cp = buf + sizeof(buf);
182 *--cp = '\0';
184 n = 4;
185 do {
186 byte = addr & 0xff;
187 *--cp = byte % 10 + '0';
188 byte /= 10;
189 if (byte > 0) {
190 *--cp = byte % 10 + '0';
191 byte /= 10;
192 if (byte > 0)
193 *--cp = byte + '0';
195 *--cp = '.';
196 addr >>= 8;
197 } while (--n > 0);
199 return cp + 1;
202 static u_int32_t f_netmask;
203 static u_int32_t f_localnet;
206 * Return a name for the IP address pointed to by ap. This address
207 * is assumed to be in network byte order.
209 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
210 * this is being called with the "ipaddr_string()" macro), so you
211 * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it. Furthermore,
212 * even in cases where it *is* part of the packet data, the caller
213 * would still have to check for a null return value, even if it's
214 * just printing the return value with "%s" - not all versions of
215 * printf print "(null)" with "%s" and a null pointer, some of them
216 * don't check for a null pointer and crash in that case.
218 * The callers of this routine should, before handing this routine
219 * a pointer to packet data, be sure that the data is present in
220 * the packet buffer. They should probably do those checks anyway,
221 * as other data at that layer might not be IP addresses, and it
222 * also needs to check whether they're present in the packet buffer.
224 const char *
225 getname(const u_char *ap)
227 register struct hostent *hp;
228 u_int32_t addr;
229 static struct hnamemem *p; /* static for longjmp() */
231 memcpy(&addr, ap, sizeof(addr));
232 p = &hnametable[addr & (HASHNAMESIZE-1)];
233 for (; p->nxt; p = p->nxt) {
234 if (p->addr == addr)
235 return (p->name);
237 p->addr = addr;
238 p->nxt = newhnamemem();
241 * Print names unless:
242 * (1) -n was given.
243 * (2) Address is foreign and -f was given. (If -f was not
244 * given, f_netmask and f_localnet are 0 and the test
245 * evaluates to true)
247 if (!nflag &&
248 (addr & f_netmask) == f_localnet) {
249 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
250 if (hp) {
251 char *dotp;
253 p->name = strdup(hp->h_name);
254 if (Nflag) {
255 /* Remove domain qualifications */
256 dotp = strchr(p->name, '.');
257 if (dotp)
258 *dotp = '\0';
260 return (p->name);
263 p->name = strdup(intoa(addr));
264 return (p->name);
267 #ifdef INET6
269 * Return a name for the IP6 address pointed to by ap. This address
270 * is assumed to be in network byte order.
272 const char *
273 getname6(const u_char *ap)
275 register struct hostent *hp;
276 struct in6_addr addr;
277 static struct h6namemem *p; /* static for longjmp() */
278 register const char *cp;
279 char ntop_buf[INET6_ADDRSTRLEN];
281 memcpy(&addr, ap, sizeof(addr));
282 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
283 for (; p->nxt; p = p->nxt) {
284 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
285 return (p->name);
287 p->addr = addr;
288 p->nxt = newh6namemem();
291 * Do not print names if -n was given.
293 if (!nflag) {
294 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
295 if (hp) {
296 char *dotp;
298 p->name = strdup(hp->h_name);
299 if (Nflag) {
300 /* Remove domain qualifications */
301 dotp = strchr(p->name, '.');
302 if (dotp)
303 *dotp = '\0';
305 return (p->name);
308 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
309 p->name = strdup(cp);
310 return (p->name);
312 #endif /* INET6 */
314 static const char hex[] = "0123456789abcdef";
317 /* Find the hash node that corresponds the ether address 'ep' */
319 static inline struct enamemem *
320 lookup_emem(const u_char *ep)
322 register u_int i, j, k;
323 struct enamemem *tp;
325 k = (ep[0] << 8) | ep[1];
326 j = (ep[2] << 8) | ep[3];
327 i = (ep[4] << 8) | ep[5];
329 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
330 while (tp->e_nxt)
331 if (tp->e_addr0 == i &&
332 tp->e_addr1 == j &&
333 tp->e_addr2 == k)
334 return tp;
335 else
336 tp = tp->e_nxt;
337 tp->e_addr0 = i;
338 tp->e_addr1 = j;
339 tp->e_addr2 = k;
340 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
341 if (tp->e_nxt == NULL)
342 error("lookup_emem: calloc");
344 return tp;
348 * Find the hash node that corresponds to the bytestring 'bs'
349 * with length 'nlen'
352 static inline struct enamemem *
353 lookup_bytestring(register const u_char *bs, const unsigned int nlen)
355 struct enamemem *tp;
356 register u_int i, j, k;
358 if (nlen >= 6) {
359 k = (bs[0] << 8) | bs[1];
360 j = (bs[2] << 8) | bs[3];
361 i = (bs[4] << 8) | bs[5];
362 } else if (nlen >= 4) {
363 k = (bs[0] << 8) | bs[1];
364 j = (bs[2] << 8) | bs[3];
365 i = 0;
366 } else
367 i = j = k = 0;
369 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
370 while (tp->e_nxt)
371 if (tp->e_addr0 == i &&
372 tp->e_addr1 == j &&
373 tp->e_addr2 == k &&
374 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
375 return tp;
376 else
377 tp = tp->e_nxt;
379 tp->e_addr0 = i;
380 tp->e_addr1 = j;
381 tp->e_addr2 = k;
383 tp->e_bs = (u_char *) calloc(1, nlen + 1);
384 memcpy(tp->e_bs, bs, nlen);
385 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
386 if (tp->e_nxt == NULL)
387 error("lookup_bytestring: calloc");
389 return tp;
392 /* Find the hash node that corresponds the NSAP 'nsap' */
394 static inline struct enamemem *
395 lookup_nsap(register const u_char *nsap)
397 register u_int i, j, k;
398 unsigned int nlen = *nsap;
399 struct enamemem *tp;
400 const u_char *ensap = nsap + nlen - 6;
402 if (nlen > 6) {
403 k = (ensap[0] << 8) | ensap[1];
404 j = (ensap[2] << 8) | ensap[3];
405 i = (ensap[4] << 8) | ensap[5];
407 else
408 i = j = k = 0;
410 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
411 while (tp->e_nxt)
412 if (tp->e_addr0 == i &&
413 tp->e_addr1 == j &&
414 tp->e_addr2 == k &&
415 tp->e_nsap[0] == nlen &&
416 memcmp((const char *)&(nsap[1]),
417 (char *)&(tp->e_nsap[1]), nlen) == 0)
418 return tp;
419 else
420 tp = tp->e_nxt;
421 tp->e_addr0 = i;
422 tp->e_addr1 = j;
423 tp->e_addr2 = k;
424 tp->e_nsap = (u_char *)malloc(nlen + 1);
425 if (tp->e_nsap == NULL)
426 error("lookup_nsap: malloc");
427 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
428 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
429 if (tp->e_nxt == NULL)
430 error("lookup_nsap: calloc");
432 return tp;
435 /* Find the hash node that corresponds the protoid 'pi'. */
437 static inline struct protoidmem *
438 lookup_protoid(const u_char *pi)
440 register u_int i, j;
441 struct protoidmem *tp;
443 /* 5 octets won't be aligned */
444 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
445 j = (pi[3] << 8) + pi[4];
446 /* XXX should be endian-insensitive, but do big-endian testing XXX */
448 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
449 while (tp->p_nxt)
450 if (tp->p_oui == i && tp->p_proto == j)
451 return tp;
452 else
453 tp = tp->p_nxt;
454 tp->p_oui = i;
455 tp->p_proto = j;
456 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
457 if (tp->p_nxt == NULL)
458 error("lookup_protoid: calloc");
460 return tp;
463 const char *
464 etheraddr_string(register const u_char *ep)
466 register int i;
467 register char *cp;
468 register struct enamemem *tp;
469 int oui;
470 char buf[BUFSIZE];
472 tp = lookup_emem(ep);
473 if (tp->e_name)
474 return (tp->e_name);
475 #ifdef USE_ETHER_NTOHOST
476 if (!nflag) {
477 char buf2[BUFSIZE];
480 * We don't cast it to "const struct ether_addr *"
481 * because some systems fail to declare the second
482 * argument as a "const" pointer, even though they
483 * don't modify what it points to.
485 if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
486 tp->e_name = strdup(buf2);
487 return (tp->e_name);
490 #endif
491 cp = buf;
492 oui = EXTRACT_24BITS(ep);
493 *cp++ = hex[*ep >> 4 ];
494 *cp++ = hex[*ep++ & 0xf];
495 for (i = 5; --i >= 0;) {
496 *cp++ = ':';
497 *cp++ = hex[*ep >> 4 ];
498 *cp++ = hex[*ep++ & 0xf];
501 if (!nflag) {
502 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
503 tok2str(oui_values, "Unknown", oui));
504 } else
505 *cp = '\0';
506 tp->e_name = strdup(buf);
507 return (tp->e_name);
510 const char *
511 le64addr_string(const u_char *ep)
513 const unsigned int len = 8;
514 register u_int i;
515 register char *cp;
516 register struct enamemem *tp;
517 char buf[BUFSIZE];
519 tp = lookup_bytestring(ep, len);
520 if (tp->e_name)
521 return (tp->e_name);
523 cp = buf;
524 for (i = len; i > 0 ; --i) {
525 *cp++ = hex[*(ep + i - 1) >> 4];
526 *cp++ = hex[*(ep + i - 1) & 0xf];
527 *cp++ = ':';
529 cp --;
531 *cp = '\0';
533 tp->e_name = strdup(buf);
535 return (tp->e_name);
538 const char *
539 linkaddr_string(const u_char *ep, const unsigned int type, const unsigned int len)
541 register u_int i;
542 register char *cp;
543 register struct enamemem *tp;
545 if (len == 0)
546 return ("<empty>");
548 if (type == LINKADDR_ETHER && len == ETHER_ADDR_LEN)
549 return (etheraddr_string(ep));
551 if (type == LINKADDR_FRELAY)
552 return (q922_string(ep));
554 tp = lookup_bytestring(ep, len);
555 if (tp->e_name)
556 return (tp->e_name);
558 tp->e_name = cp = (char *)malloc(len*3);
559 if (tp->e_name == NULL)
560 error("linkaddr_string: malloc");
561 *cp++ = hex[*ep >> 4];
562 *cp++ = hex[*ep++ & 0xf];
563 for (i = len-1; i > 0 ; --i) {
564 *cp++ = ':';
565 *cp++ = hex[*ep >> 4];
566 *cp++ = hex[*ep++ & 0xf];
568 *cp = '\0';
569 return (tp->e_name);
572 const char *
573 etherproto_string(u_short port)
575 register char *cp;
576 register struct hnamemem *tp;
577 register u_int32_t i = port;
578 char buf[sizeof("0000")];
580 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
581 if (tp->addr == i)
582 return (tp->name);
584 tp->addr = i;
585 tp->nxt = newhnamemem();
587 cp = buf;
588 NTOHS(port);
589 *cp++ = hex[port >> 12 & 0xf];
590 *cp++ = hex[port >> 8 & 0xf];
591 *cp++ = hex[port >> 4 & 0xf];
592 *cp++ = hex[port & 0xf];
593 *cp++ = '\0';
594 tp->name = strdup(buf);
595 return (tp->name);
598 const char *
599 protoid_string(register const u_char *pi)
601 register u_int i, j;
602 register char *cp;
603 register struct protoidmem *tp;
604 char buf[sizeof("00:00:00:00:00")];
606 tp = lookup_protoid(pi);
607 if (tp->p_name)
608 return tp->p_name;
610 cp = buf;
611 if ((j = *pi >> 4) != 0)
612 *cp++ = hex[j];
613 *cp++ = hex[*pi++ & 0xf];
614 for (i = 4; (int)--i >= 0;) {
615 *cp++ = ':';
616 if ((j = *pi >> 4) != 0)
617 *cp++ = hex[j];
618 *cp++ = hex[*pi++ & 0xf];
620 *cp = '\0';
621 tp->p_name = strdup(buf);
622 return (tp->p_name);
625 #define ISONSAP_MAX_LENGTH 20
626 const char *
627 isonsap_string(const u_char *nsap, register u_int nsap_length)
629 register u_int nsap_idx;
630 register char *cp;
631 register struct enamemem *tp;
633 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
634 return ("isonsap_string: illegal length");
636 tp = lookup_nsap(nsap);
637 if (tp->e_name)
638 return tp->e_name;
640 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
641 if (cp == NULL)
642 error("isonsap_string: malloc");
644 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
645 *cp++ = hex[*nsap >> 4];
646 *cp++ = hex[*nsap++ & 0xf];
647 if (((nsap_idx & 1) == 0) &&
648 (nsap_idx + 1 < nsap_length)) {
649 *cp++ = '.';
652 *cp = '\0';
653 return (tp->e_name);
656 const char *
657 tcpport_string(u_short port)
659 register struct hnamemem *tp;
660 register u_int32_t i = port;
661 char buf[sizeof("00000")];
663 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
664 if (tp->addr == i)
665 return (tp->name);
667 tp->addr = i;
668 tp->nxt = newhnamemem();
670 (void)snprintf(buf, sizeof(buf), "%u", i);
671 tp->name = strdup(buf);
672 return (tp->name);
675 const char *
676 udpport_string(register u_short port)
678 register struct hnamemem *tp;
679 register u_int32_t i = port;
680 char buf[sizeof("00000")];
682 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
683 if (tp->addr == i)
684 return (tp->name);
686 tp->addr = i;
687 tp->nxt = newhnamemem();
689 (void)snprintf(buf, sizeof(buf), "%u", i);
690 tp->name = strdup(buf);
691 return (tp->name);
694 const char *
695 ipxsap_string(u_short port)
697 register char *cp;
698 register struct hnamemem *tp;
699 register u_int32_t i = port;
700 char buf[sizeof("0000")];
702 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
703 if (tp->addr == i)
704 return (tp->name);
706 tp->addr = i;
707 tp->nxt = newhnamemem();
709 cp = buf;
710 NTOHS(port);
711 *cp++ = hex[port >> 12 & 0xf];
712 *cp++ = hex[port >> 8 & 0xf];
713 *cp++ = hex[port >> 4 & 0xf];
714 *cp++ = hex[port & 0xf];
715 *cp++ = '\0';
716 tp->name = strdup(buf);
717 return (tp->name);
720 static void
721 init_servarray(void)
723 struct servent *sv;
724 register struct hnamemem *table;
725 register int i;
726 char buf[sizeof("0000000000")];
728 while ((sv = getservent()) != NULL) {
729 int port = ntohs(sv->s_port);
730 i = port & (HASHNAMESIZE-1);
731 if (strcmp(sv->s_proto, "tcp") == 0)
732 table = &tporttable[i];
733 else if (strcmp(sv->s_proto, "udp") == 0)
734 table = &uporttable[i];
735 else
736 continue;
738 while (table->name)
739 table = table->nxt;
740 if (nflag) {
741 (void)snprintf(buf, sizeof(buf), "%d", port);
742 table->name = strdup(buf);
743 } else
744 table->name = strdup(sv->s_name);
745 table->addr = port;
746 table->nxt = newhnamemem();
748 endservent();
751 /* in libpcap.a (nametoaddr.c) */
752 #if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
753 __declspec(dllimport)
754 #else
755 extern
756 #endif
757 const struct eproto {
758 const char *s;
759 u_short p;
760 } eproto_db[];
762 static void
763 init_eprotoarray(void)
765 register int i;
766 register struct hnamemem *table;
768 for (i = 0; eproto_db[i].s; i++) {
769 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
770 table = &eprototable[j];
771 while (table->name)
772 table = table->nxt;
773 table->name = eproto_db[i].s;
774 table->addr = htons(eproto_db[i].p);
775 table->nxt = newhnamemem();
779 static const struct protoidlist {
780 const u_char protoid[5];
781 const char *name;
782 } protoidlist[] = {
783 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
784 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
785 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
786 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
787 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
788 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
792 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
793 * types.
795 static void
796 init_protoidarray(void)
798 register int i;
799 register struct protoidmem *tp;
800 const struct protoidlist *pl;
801 u_char protoid[5];
803 protoid[0] = 0;
804 protoid[1] = 0;
805 protoid[2] = 0;
806 for (i = 0; eproto_db[i].s; i++) {
807 u_short etype = htons(eproto_db[i].p);
809 memcpy((char *)&protoid[3], (char *)&etype, 2);
810 tp = lookup_protoid(protoid);
811 tp->p_name = strdup(eproto_db[i].s);
813 /* Hardwire some SNAP proto ID names */
814 for (pl = protoidlist; pl->name != NULL; ++pl) {
815 tp = lookup_protoid(pl->protoid);
816 /* Don't override existing name */
817 if (tp->p_name != NULL)
818 continue;
820 tp->p_name = pl->name;
824 static const struct etherlist {
825 const u_char addr[6];
826 const char *name;
827 } etherlist[] = {
828 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
829 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
833 * Initialize the ethers hash table. We take two different approaches
834 * depending on whether or not the system provides the ethers name
835 * service. If it does, we just wire in a few names at startup,
836 * and etheraddr_string() fills in the table on demand. If it doesn't,
837 * then we suck in the entire /etc/ethers file at startup. The idea
838 * is that parsing the local file will be fast, but spinning through
839 * all the ethers entries via NIS & next_etherent might be very slow.
841 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
842 * since the pcap module already does name-to-address translation,
843 * it's already does most of the work for the ethernet address-to-name
844 * translation, so we just pcap_next_etherent as a convenience.
846 static void
847 init_etherarray(void)
849 register const struct etherlist *el;
850 register struct enamemem *tp;
851 #ifdef USE_ETHER_NTOHOST
852 char name[256];
853 #else
854 register struct pcap_etherent *ep;
855 register FILE *fp;
857 /* Suck in entire ethers file */
858 fp = fopen(PCAP_ETHERS_FILE, "r");
859 if (fp != NULL) {
860 while ((ep = pcap_next_etherent(fp)) != NULL) {
861 tp = lookup_emem(ep->addr);
862 tp->e_name = strdup(ep->name);
864 (void)fclose(fp);
866 #endif
868 /* Hardwire some ethernet names */
869 for (el = etherlist; el->name != NULL; ++el) {
870 tp = lookup_emem(el->addr);
871 /* Don't override existing name */
872 if (tp->e_name != NULL)
873 continue;
875 #ifdef USE_ETHER_NTOHOST
877 * Use YP/NIS version of name if available.
879 * We don't cast it to "const struct ether_addr *"
880 * because some systems don't modify the Ethernet
881 * address but fail to declare the second argument
882 * as a "const" pointer.
884 if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
885 tp->e_name = strdup(name);
886 continue;
888 #endif
889 tp->e_name = el->name;
893 static const struct tok ipxsap_db[] = {
894 { 0x0000, "Unknown" },
895 { 0x0001, "User" },
896 { 0x0002, "User Group" },
897 { 0x0003, "PrintQueue" },
898 { 0x0004, "FileServer" },
899 { 0x0005, "JobServer" },
900 { 0x0006, "Gateway" },
901 { 0x0007, "PrintServer" },
902 { 0x0008, "ArchiveQueue" },
903 { 0x0009, "ArchiveServer" },
904 { 0x000a, "JobQueue" },
905 { 0x000b, "Administration" },
906 { 0x000F, "Novell TI-RPC" },
907 { 0x0017, "Diagnostics" },
908 { 0x0020, "NetBIOS" },
909 { 0x0021, "NAS SNA Gateway" },
910 { 0x0023, "NACS AsyncGateway" },
911 { 0x0024, "RemoteBridge/RoutingService" },
912 { 0x0026, "BridgeServer" },
913 { 0x0027, "TCP/IP Gateway" },
914 { 0x0028, "Point-to-point X.25 BridgeServer" },
915 { 0x0029, "3270 Gateway" },
916 { 0x002a, "CHI Corp" },
917 { 0x002c, "PC Chalkboard" },
918 { 0x002d, "TimeSynchServer" },
919 { 0x002e, "ARCserve5.0/PalindromeBackup" },
920 { 0x0045, "DI3270 Gateway" },
921 { 0x0047, "AdvertisingPrintServer" },
922 { 0x004a, "NetBlazerModems" },
923 { 0x004b, "BtrieveVAP" },
924 { 0x004c, "NetwareSQL" },
925 { 0x004d, "XtreeNetwork" },
926 { 0x0050, "BtrieveVAP4.11" },
927 { 0x0052, "QuickLink" },
928 { 0x0053, "PrintQueueUser" },
929 { 0x0058, "Multipoint X.25 Router" },
930 { 0x0060, "STLB/NLM" },
931 { 0x0064, "ARCserve" },
932 { 0x0066, "ARCserve3.0" },
933 { 0x0072, "WAN CopyUtility" },
934 { 0x007a, "TES-NetwareVMS" },
935 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
936 { 0x0095, "DDA OBGYN" },
937 { 0x0098, "NetwareAccessServer" },
938 { 0x009a, "Netware for VMS II/NamedPipeServer" },
939 { 0x009b, "NetwareAccessServer" },
940 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
941 { 0x00a1, "PowerchuteAPC UPS" },
942 { 0x00aa, "LAWserve" },
943 { 0x00ac, "CompaqIDA StatusMonitor" },
944 { 0x0100, "PIPE STAIL" },
945 { 0x0102, "LAN ProtectBindery" },
946 { 0x0103, "OracleDataBaseServer" },
947 { 0x0107, "Netware386/RSPX RemoteConsole" },
948 { 0x010f, "NovellSNA Gateway" },
949 { 0x0111, "TestServer" },
950 { 0x0112, "HP PrintServer" },
951 { 0x0114, "CSA MUX" },
952 { 0x0115, "CSA LCA" },
953 { 0x0116, "CSA CM" },
954 { 0x0117, "CSA SMA" },
955 { 0x0118, "CSA DBA" },
956 { 0x0119, "CSA NMA" },
957 { 0x011a, "CSA SSA" },
958 { 0x011b, "CSA STATUS" },
959 { 0x011e, "CSA APPC" },
960 { 0x0126, "SNA TEST SSA Profile" },
961 { 0x012a, "CSA TRACE" },
962 { 0x012b, "NetwareSAA" },
963 { 0x012e, "IKARUS VirusScan" },
964 { 0x0130, "CommunicationsExecutive" },
965 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
966 { 0x0135, "NetwareNamingServicesProfile" },
967 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
968 { 0x0141, "LAN SpoolServer" },
969 { 0x0152, "IRMALAN Gateway" },
970 { 0x0154, "NamedPipeServer" },
971 { 0x0166, "NetWareManagement" },
972 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
973 { 0x0173, "Compaq" },
974 { 0x0174, "Compaq SNMP Agent" },
975 { 0x0175, "Compaq" },
976 { 0x0180, "XTreeServer/XTreeTools" },
977 { 0x018A, "NASI ServicesBroadcastServer" },
978 { 0x01b0, "GARP Gateway" },
979 { 0x01b1, "Binfview" },
980 { 0x01bf, "IntelLanDeskManager" },
981 { 0x01ca, "AXTEC" },
982 { 0x01cb, "ShivaNetModem/E" },
983 { 0x01cc, "ShivaLanRover/E" },
984 { 0x01cd, "ShivaLanRover/T" },
985 { 0x01ce, "ShivaUniversal" },
986 { 0x01d8, "CastelleFAXPressServer" },
987 { 0x01da, "CastelleLANPressPrintServer" },
988 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
989 { 0x01f0, "LEGATO" },
990 { 0x01f5, "LEGATO" },
991 { 0x0233, "NMS Agent/NetwareManagementAgent" },
992 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
993 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
994 { 0x023a, "LANtern" },
995 { 0x023c, "MAVERICK" },
996 { 0x023f, "NovellSMDR" },
997 { 0x024e, "NetwareConnect" },
998 { 0x024f, "NASI ServerBroadcast Cisco" },
999 { 0x026a, "NMS ServiceConsole" },
1000 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
1001 { 0x0278, "DirectoryServer Netware 4.x" },
1002 { 0x027b, "NetwareManagementAgent" },
1003 { 0x0280, "Novell File and Printer Sharing Service for PC" },
1004 { 0x0304, "NovellSAA Gateway" },
1005 { 0x0308, "COM/VERMED" },
1006 { 0x030a, "GalacticommWorldgroupServer" },
1007 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1008 { 0x0320, "AttachmateGateway" },
1009 { 0x0327, "MicrosoftDiagnostiocs" },
1010 { 0x0328, "WATCOM SQL Server" },
1011 { 0x0335, "MultiTechSystems MultisynchCommServer" },
1012 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1013 { 0x0355, "ArcadaBackupExec" },
1014 { 0x0358, "MSLCD1" },
1015 { 0x0361, "NETINELO" },
1016 { 0x037e, "Powerchute UPS Monitoring" },
1017 { 0x037f, "ViruSafeNotify" },
1018 { 0x0386, "HP Bridge" },
1019 { 0x0387, "HP Hub" },
1020 { 0x0394, "NetWare SAA Gateway" },
1021 { 0x039b, "LotusNotes" },
1022 { 0x03b7, "CertusAntiVirus" },
1023 { 0x03c4, "ARCserve4.0" },
1024 { 0x03c7, "LANspool3.5" },
1025 { 0x03d7, "LexmarkPrinterServer" },
1026 { 0x03d8, "LexmarkXLE PrinterServer" },
1027 { 0x03dd, "BanyanENS NetwareClient" },
1028 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1029 { 0x03e1, "UnivelUnixware" },
1030 { 0x03e4, "UnivelUnixware" },
1031 { 0x03fc, "IntelNetport" },
1032 { 0x03fd, "PrintServerQueue" },
1033 { 0x040A, "ipnServer" },
1034 { 0x040D, "LVERRMAN" },
1035 { 0x040E, "LVLIC" },
1036 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1037 { 0x0429, "SiteLockVirus" },
1038 { 0x0432, "UFHELPR???" },
1039 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1040 { 0x0444, "MicrosoftNT SNA Server" },
1041 { 0x0448, "Oracle" },
1042 { 0x044c, "ARCserve5.01" },
1043 { 0x0457, "CanonGP55" },
1044 { 0x045a, "QMS Printers" },
1045 { 0x045b, "DellSCSI Array" },
1046 { 0x0491, "NetBlazerModems" },
1047 { 0x04ac, "OnTimeScheduler" },
1048 { 0x04b0, "CD-Net" },
1049 { 0x0513, "EmulexNQA" },
1050 { 0x0520, "SiteLockChecks" },
1051 { 0x0529, "SiteLockChecks" },
1052 { 0x052d, "CitrixOS2 AppServer" },
1053 { 0x0535, "Tektronix" },
1054 { 0x0536, "Milan" },
1055 { 0x055d, "Attachmate SNA gateway" },
1056 { 0x056b, "IBM8235 ModemServer" },
1057 { 0x056c, "ShivaLanRover/E PLUS" },
1058 { 0x056d, "ShivaLanRover/T PLUS" },
1059 { 0x0580, "McAfeeNetShield" },
1060 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1061 { 0x05BA, "CompatibleSystemsRouters" },
1062 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1063 { 0x0606, "JCWatermarkImaging" },
1064 { 0x060c, "AXISNetworkPrinter" },
1065 { 0x0610, "AdaptecSCSIManagement" },
1066 { 0x0621, "IBM AntiVirus" },
1067 { 0x0640, "Windows95 RemoteRegistryService" },
1068 { 0x064e, "MicrosoftIIS" },
1069 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1070 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1071 { 0x076C, "Xerox" },
1072 { 0x079b, "ShivaLanRover/E 115" },
1073 { 0x079c, "ShivaLanRover/T 115" },
1074 { 0x07B4, "CubixWorldDesk" },
1075 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1076 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1077 { 0x0810, "ELAN License Server Demo" },
1078 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1079 { 0x086a, "ISSC Collector" },
1080 { 0x087f, "ISSC DAS AgentAIX" },
1081 { 0x0880, "Intel Netport PRO" },
1082 { 0x0881, "Intel Netport PRO" },
1083 { 0x0b29, "SiteLock" },
1084 { 0x0c29, "SiteLockApplications" },
1085 { 0x0c2c, "LicensingServer" },
1086 { 0x2101, "PerformanceTechnologyInstantInternet" },
1087 { 0x2380, "LAI SiteLock" },
1088 { 0x238c, "MeetingMaker" },
1089 { 0x4808, "SiteLockServer/SiteLockMetering" },
1090 { 0x5555, "SiteLockUser" },
1091 { 0x6312, "Tapeware" },
1092 { 0x6f00, "RabbitGateway" },
1093 { 0x7703, "MODEM" },
1094 { 0x8002, "NetPortPrinters" },
1095 { 0x8008, "WordPerfectNetworkVersion" },
1096 { 0x85BE, "Cisco EIGRP" },
1097 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1098 { 0x9000, "McAfeeNetShield" },
1099 { 0x9604, "CSA-NT_MON" },
1100 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1101 { 0xf11f, "SiteLockMetering" },
1102 { 0xf1ff, "SiteLock" },
1103 { 0xf503, "Microsoft SQL Server" },
1104 { 0xF905, "IBM TimeAndPlace" },
1105 { 0xfbfb, "TopCallIII FaxServer" },
1106 { 0xffff, "AnyService/Wildcard" },
1107 { 0, (char *)0 }
1110 static void
1111 init_ipxsaparray(void)
1113 register int i;
1114 register struct hnamemem *table;
1116 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1117 int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1118 table = &ipxsaptable[j];
1119 while (table->name)
1120 table = table->nxt;
1121 table->name = ipxsap_db[i].s;
1122 table->addr = htons(ipxsap_db[i].v);
1123 table->nxt = newhnamemem();
1128 * Initialize the address to name translation machinery. We map all
1129 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
1130 * to prevent blocking on the nameserver). localnet is the IP address
1131 * of the local network. mask is its subnet mask.
1133 void
1134 init_addrtoname(u_int32_t localnet, u_int32_t mask)
1136 if (fflag) {
1137 f_localnet = localnet;
1138 f_netmask = mask;
1140 if (nflag)
1142 * Simplest way to suppress names.
1144 return;
1146 init_etherarray();
1147 init_servarray();
1148 init_eprotoarray();
1149 init_protoidarray();
1150 init_ipxsaparray();
1153 const char *
1154 dnaddr_string(u_short dnaddr)
1156 register struct hnamemem *tp;
1158 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1159 tp = tp->nxt)
1160 if (tp->addr == dnaddr)
1161 return (tp->name);
1163 tp->addr = dnaddr;
1164 tp->nxt = newhnamemem();
1165 if (nflag)
1166 tp->name = dnnum_string(dnaddr);
1167 else
1168 tp->name = dnname_string(dnaddr);
1170 return(tp->name);
1173 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1174 struct hnamemem *
1175 newhnamemem(void)
1177 register struct hnamemem *p;
1178 static struct hnamemem *ptr = NULL;
1179 static u_int num = 0;
1181 if (num <= 0) {
1182 num = 64;
1183 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1184 if (ptr == NULL)
1185 error("newhnamemem: calloc");
1187 --num;
1188 p = ptr++;
1189 return (p);
1192 #ifdef INET6
1193 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1194 struct h6namemem *
1195 newh6namemem(void)
1197 register struct h6namemem *p;
1198 static struct h6namemem *ptr = NULL;
1199 static u_int num = 0;
1201 if (num <= 0) {
1202 num = 64;
1203 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1204 if (ptr == NULL)
1205 error("newh6namemem: calloc");
1207 --num;
1208 p = ptr++;
1209 return (p);
1211 #endif /* INET6 */