Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / addrtoname.c
blob475a9b7bb1a5f6f64fecd72beac0179840dea746
1 /* $NetBSD: addrtoname.c,v 1.8 2007/07/24 11:53:36 drochner Exp $ */
3 /*
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Internet, ethernet, port, and protocol string to address
24 * and address to string conversion routines
26 #include <sys/cdefs.h>
27 #ifndef lint
28 #if 0
29 static const char rcsid[] _U_ =
30 "@(#) Header: /tcpdump/master/tcpdump/addrtoname.c,v 1.108.2.8 2006/02/27 07:27:16 hannes Exp (LBL)";
31 #else
32 __RCSID("$NetBSD: addrtoname.c,v 1.8 2007/07/24 11:53:36 drochner Exp $");
33 #endif
34 #endif
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
40 #include <tcpdump-stdinc.h>
42 #ifdef USE_ETHER_NTOHOST
43 #ifdef HAVE_NETINET_IF_ETHER_H
44 struct mbuf; /* Squelch compiler warnings on some platforms for */
45 struct rtentry; /* declarations in <net/if.h> */
46 #include <net/if.h> /* for "struct ifnet" in "struct arpcom" on Solaris */
47 #include <netinet/if_ether.h>
48 #endif /* HAVE_NETINET_IF_ETHER_H */
49 #ifdef NETINET_ETHER_H_DECLARES_ETHER_NTOHOST
50 #include <netinet/ether.h>
51 #endif /* NETINET_ETHER_H_DECLARES_ETHER_NTOHOST */
53 #if !defined(HAVE_DECL_ETHER_NTOHOST) || !HAVE_DECL_ETHER_NTOHOST
54 #ifndef HAVE_STRUCT_ETHER_ADDR
55 struct ether_addr {
56 unsigned char ether_addr_octet[6];
58 #endif
59 extern int ether_ntohost(char *, const struct ether_addr *);
60 #endif
62 #endif /* USE_ETHER_NTOHOST */
64 #include <pcap.h>
65 #include <pcap-namedb.h>
66 #include <signal.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <stdlib.h>
71 #include "interface.h"
72 #include "addrtoname.h"
73 #include "llc.h"
74 #include "setsignal.h"
75 #include "extract.h"
76 #include "oui.h"
78 #ifndef ETHER_ADDR_LEN
79 #include "ether.h"
80 #endif
83 * hash tables for whatever-to-name translations
85 * XXX there has to be error checks against strdup(3) failure
88 #define HASHNAMESIZE 4096
89 #define BUFSIZE 128
91 struct hnamemem {
92 u_int32_t addr;
93 const char *name;
94 struct hnamemem *nxt;
97 struct hnamemem hnametable[HASHNAMESIZE];
98 struct hnamemem tporttable[HASHNAMESIZE];
99 struct hnamemem uporttable[HASHNAMESIZE];
100 struct hnamemem eprototable[HASHNAMESIZE];
101 struct hnamemem dnaddrtable[HASHNAMESIZE];
102 struct hnamemem ipxsaptable[HASHNAMESIZE];
104 #if defined(INET6) && defined(WIN32)
106 * fake gethostbyaddr for Win2k/XP
107 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
108 * to 3rd argument.
110 * h_name in struct hostent is only valid.
112 static struct hostent *
113 win32_gethostbyaddr(const char *addr, int len, int type)
115 static struct hostent host;
116 static char hostbuf[NI_MAXHOST];
117 char hname[NI_MAXHOST];
118 struct sockaddr_in6 addr6;
120 host.h_name = hostbuf;
121 switch (type) {
122 case AF_INET:
123 return gethostbyaddr(addr, len, type);
124 break;
125 case AF_INET6:
126 memset(&addr6, 0, sizeof(addr6));
127 addr6.sin6_family = AF_INET6;
128 memcpy(&addr6.sin6_addr, addr, len);
129 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
130 hname, sizeof(hname), NULL, 0, 0)) {
131 return NULL;
132 } else {
133 strcpy(host.h_name, hname);
134 return &host;
136 break;
137 default:
138 return NULL;
141 #define gethostbyaddr win32_gethostbyaddr
142 #endif /* INET6 & WIN32 */
144 #ifdef INET6
145 struct h6namemem {
146 struct in6_addr addr;
147 char *name;
148 struct h6namemem *nxt;
151 struct h6namemem h6nametable[HASHNAMESIZE];
152 #endif /* INET6 */
154 struct enamemem {
155 u_short e_addr0;
156 u_short e_addr1;
157 u_short e_addr2;
158 const char *e_name;
159 u_char *e_nsap; /* used only for nsaptable[] */
160 #define e_bs e_nsap /* for bytestringtable */
161 struct enamemem *e_nxt;
164 struct enamemem enametable[HASHNAMESIZE];
165 struct enamemem nsaptable[HASHNAMESIZE];
166 struct enamemem bytestringtable[HASHNAMESIZE];
168 struct protoidmem {
169 u_int32_t p_oui;
170 u_short p_proto;
171 const char *p_name;
172 struct protoidmem *p_nxt;
175 struct protoidmem protoidtable[HASHNAMESIZE];
178 * A faster replacement for inet_ntoa().
180 const char *
181 intoa(u_int32_t addr)
183 register char *cp;
184 register u_int byte;
185 register int n;
186 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
188 NTOHL(addr);
189 cp = buf + sizeof(buf);
190 *--cp = '\0';
192 n = 4;
193 do {
194 byte = addr & 0xff;
195 *--cp = byte % 10 + '0';
196 byte /= 10;
197 if (byte > 0) {
198 *--cp = byte % 10 + '0';
199 byte /= 10;
200 if (byte > 0)
201 *--cp = byte + '0';
203 *--cp = '.';
204 addr >>= 8;
205 } while (--n > 0);
207 return cp + 1;
210 static u_int32_t f_netmask;
211 static u_int32_t f_localnet;
214 * Return a name for the IP address pointed to by ap. This address
215 * is assumed to be in network byte order.
217 * NOTE: ap is *NOT* necessarily part of the packet data (not even if
218 * this is being called with the "ipaddr_string()" macro), so you
219 * *CANNOT* use the TCHECK{2}/TTEST{2} macros on it. Furthermore,
220 * even in cases where it *is* part of the packet data, the caller
221 * would still have to check for a null return value, even if it's
222 * just printing the return value with "%s" - not all versions of
223 * printf print "(null)" with "%s" and a null pointer, some of them
224 * don't check for a null pointer and crash in that case.
226 * The callers of this routine should, before handing this routine
227 * a pointer to packet data, be sure that the data is present in
228 * the packet buffer. They should probably do those checks anyway,
229 * as other data at that layer might not be IP addresses, and it
230 * also needs to check whether they're present in the packet buffer.
232 const char *
233 getname(const u_char *ap)
235 register struct hostent *hp;
236 u_int32_t addr;
237 static struct hnamemem *p; /* static for longjmp() */
239 memcpy(&addr, ap, sizeof(addr));
240 p = &hnametable[addr & (HASHNAMESIZE-1)];
241 for (; p->nxt; p = p->nxt) {
242 if (p->addr == addr)
243 return (p->name);
245 p->addr = addr;
246 p->nxt = newhnamemem();
249 * Print names unless:
250 * (1) -n was given.
251 * (2) Address is foreign and -f was given. (If -f was not
252 * given, f_netmask and f_localnet are 0 and the test
253 * evaluates to true)
255 if (!nflag &&
256 (addr & f_netmask) == f_localnet) {
257 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
258 if (hp) {
259 char *dotp;
261 p->name = strdup(hp->h_name);
262 if (Nflag) {
263 /* Remove domain qualifications */
264 dotp = strchr(p->name, '.');
265 if (dotp)
266 *dotp = '\0';
268 return (p->name);
271 p->name = strdup(intoa(addr));
272 return (p->name);
275 #ifdef INET6
277 * Return a name for the IP6 address pointed to by ap. This address
278 * is assumed to be in network byte order.
280 const char *
281 getname6(const u_char *ap)
283 register struct hostent *hp;
284 struct in6_addr addr;
285 static struct h6namemem *p; /* static for longjmp() */
286 register const char *cp;
287 char ntop_buf[INET6_ADDRSTRLEN];
289 memcpy(&addr, ap, sizeof(addr));
290 p = &h6nametable[*(u_int16_t *)&addr.s6_addr[14] & (HASHNAMESIZE-1)];
291 for (; p->nxt; p = p->nxt) {
292 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
293 return (p->name);
295 p->addr = addr;
296 p->nxt = newh6namemem();
299 * Do not print names if -n was given.
301 if (!nflag) {
302 hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET6);
303 if (hp) {
304 char *dotp;
306 p->name = strdup(hp->h_name);
307 if (Nflag) {
308 /* Remove domain qualifications */
309 dotp = strchr(p->name, '.');
310 if (dotp)
311 *dotp = '\0';
313 return (p->name);
316 cp = inet_ntop(AF_INET6, &addr, ntop_buf, sizeof(ntop_buf));
317 p->name = strdup(cp);
318 return (p->name);
320 #endif /* INET6 */
322 static char hex[] = "0123456789abcdef";
325 /* Find the hash node that corresponds the ether address 'ep' */
327 static inline struct enamemem *
328 lookup_emem(const u_char *ep)
330 register u_int i, j, k;
331 struct enamemem *tp;
333 k = (ep[0] << 8) | ep[1];
334 j = (ep[2] << 8) | ep[3];
335 i = (ep[4] << 8) | ep[5];
337 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
338 while (tp->e_nxt)
339 if (tp->e_addr0 == i &&
340 tp->e_addr1 == j &&
341 tp->e_addr2 == k)
342 return tp;
343 else
344 tp = tp->e_nxt;
345 tp->e_addr0 = i;
346 tp->e_addr1 = j;
347 tp->e_addr2 = k;
348 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
349 if (tp->e_nxt == NULL)
350 error("lookup_emem: calloc");
352 return tp;
356 * Find the hash node that corresponds to the bytestring 'bs'
357 * with length 'nlen'
360 static inline struct enamemem *
361 lookup_bytestring(register const u_char *bs, const unsigned int nlen)
363 struct enamemem *tp;
364 register u_int i, j, k;
366 if (nlen >= 6) {
367 k = (bs[0] << 8) | bs[1];
368 j = (bs[2] << 8) | bs[3];
369 i = (bs[4] << 8) | bs[5];
370 } else if (nlen >= 4) {
371 k = (bs[0] << 8) | bs[1];
372 j = (bs[2] << 8) | bs[3];
373 i = 0;
374 } else
375 i = j = k = 0;
377 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
378 while (tp->e_nxt)
379 if (tp->e_addr0 == i &&
380 tp->e_addr1 == j &&
381 tp->e_addr2 == k &&
382 memcmp((const char *)bs, (const char *)(tp->e_bs), nlen) == 0)
383 return tp;
384 else
385 tp = tp->e_nxt;
387 tp->e_addr0 = i;
388 tp->e_addr1 = j;
389 tp->e_addr2 = k;
391 tp->e_bs = (u_char *) calloc(1, nlen + 1);
392 memcpy(tp->e_bs, bs, nlen);
393 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
394 if (tp->e_nxt == NULL)
395 error("lookup_bytestring: calloc");
397 return tp;
400 /* Find the hash node that corresponds the NSAP 'nsap' */
402 static inline struct enamemem *
403 lookup_nsap(register const u_char *nsap)
405 register u_int i, j, k;
406 unsigned int nlen = *nsap;
407 struct enamemem *tp;
408 const u_char *ensap = nsap + nlen - 6;
410 if (nlen > 6) {
411 k = (ensap[0] << 8) | ensap[1];
412 j = (ensap[2] << 8) | ensap[3];
413 i = (ensap[4] << 8) | ensap[5];
415 else
416 i = j = k = 0;
418 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
419 while (tp->e_nxt)
420 if (tp->e_addr0 == i &&
421 tp->e_addr1 == j &&
422 tp->e_addr2 == k &&
423 tp->e_nsap[0] == nlen &&
424 memcmp((const char *)&(nsap[1]),
425 (char *)&(tp->e_nsap[1]), nlen) == 0)
426 return tp;
427 else
428 tp = tp->e_nxt;
429 tp->e_addr0 = i;
430 tp->e_addr1 = j;
431 tp->e_addr2 = k;
432 tp->e_nsap = (u_char *)malloc(nlen + 1);
433 if (tp->e_nsap == NULL)
434 error("lookup_nsap: malloc");
435 memcpy((char *)tp->e_nsap, (const char *)nsap, nlen + 1);
436 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
437 if (tp->e_nxt == NULL)
438 error("lookup_nsap: calloc");
440 return tp;
443 /* Find the hash node that corresponds the protoid 'pi'. */
445 static inline struct protoidmem *
446 lookup_protoid(const u_char *pi)
448 register u_int i, j;
449 struct protoidmem *tp;
451 /* 5 octets won't be aligned */
452 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
453 j = (pi[3] << 8) + pi[4];
454 /* XXX should be endian-insensitive, but do big-endian testing XXX */
456 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
457 while (tp->p_nxt)
458 if (tp->p_oui == i && tp->p_proto == j)
459 return tp;
460 else
461 tp = tp->p_nxt;
462 tp->p_oui = i;
463 tp->p_proto = j;
464 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
465 if (tp->p_nxt == NULL)
466 error("lookup_protoid: calloc");
468 return tp;
471 const char *
472 etheraddr_string(register const u_char *ep)
474 register int i;
475 register char *cp;
476 register struct enamemem *tp;
477 int oui;
478 char buf[BUFSIZE];
480 tp = lookup_emem(ep);
481 if (tp->e_name)
482 return (tp->e_name);
483 #ifdef USE_ETHER_NTOHOST
484 if (!nflag) {
485 char buf2[BUFSIZE];
488 * We don't cast it to "const struct ether_addr *"
489 * because some systems fail to declare the second
490 * argument as a "const" pointer, even though they
491 * don't modify what it points to.
493 if (ether_ntohost(buf2, (struct ether_addr *)ep) == 0) {
494 tp->e_name = strdup(buf2);
495 return (tp->e_name);
498 #endif
499 cp = buf;
500 oui = EXTRACT_24BITS(ep);
501 *cp++ = hex[*ep >> 4 ];
502 *cp++ = hex[*ep++ & 0xf];
503 for (i = 5; --i >= 0;) {
504 *cp++ = ':';
505 *cp++ = hex[*ep >> 4 ];
506 *cp++ = hex[*ep++ & 0xf];
509 if (!nflag) {
510 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
511 tok2str(oui_values, "Unknown", oui));
512 } else
513 *cp = '\0';
514 tp->e_name = strdup(buf);
515 return (tp->e_name);
518 const char *
519 linkaddr_string(const u_char *ep, const unsigned int len)
521 register u_int i;
522 register char *cp;
523 register struct enamemem *tp;
525 if (len == 6) /* XXX not totally correct... */
526 return etheraddr_string(ep);
528 tp = lookup_bytestring(ep, len);
529 if (tp->e_name)
530 return (tp->e_name);
532 tp->e_name = cp = (char *)malloc(len*3);
533 if (tp->e_name == NULL)
534 error("linkaddr_string: malloc");
535 *cp++ = hex[*ep >> 4];
536 *cp++ = hex[*ep++ & 0xf];
537 for (i = len-1; i > 0 ; --i) {
538 *cp++ = ':';
539 *cp++ = hex[*ep >> 4];
540 *cp++ = hex[*ep++ & 0xf];
542 *cp = '\0';
543 return (tp->e_name);
546 const char *
547 etherproto_string(u_short port)
549 register char *cp;
550 register struct hnamemem *tp;
551 register u_int32_t i = port;
552 char buf[sizeof("0000")];
554 for (tp = &eprototable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
555 if (tp->addr == i)
556 return (tp->name);
558 tp->addr = i;
559 tp->nxt = newhnamemem();
561 cp = buf;
562 NTOHS(port);
563 *cp++ = hex[port >> 12 & 0xf];
564 *cp++ = hex[port >> 8 & 0xf];
565 *cp++ = hex[port >> 4 & 0xf];
566 *cp++ = hex[port & 0xf];
567 *cp++ = '\0';
568 tp->name = strdup(buf);
569 return (tp->name);
572 const char *
573 protoid_string(register const u_char *pi)
575 register u_int i, j;
576 register char *cp;
577 register struct protoidmem *tp;
578 char buf[sizeof("00:00:00:00:00")];
580 tp = lookup_protoid(pi);
581 if (tp->p_name)
582 return tp->p_name;
584 cp = buf;
585 if ((j = *pi >> 4) != 0)
586 *cp++ = hex[j];
587 *cp++ = hex[*pi++ & 0xf];
588 for (i = 4; (int)--i >= 0;) {
589 *cp++ = ':';
590 if ((j = *pi >> 4) != 0)
591 *cp++ = hex[j];
592 *cp++ = hex[*pi++ & 0xf];
594 *cp = '\0';
595 tp->p_name = strdup(buf);
596 return (tp->p_name);
599 #define ISONSAP_MAX_LENGTH 20
600 const char *
601 isonsap_string(const u_char *nsap, register u_int nsap_length)
603 register u_int nsap_idx;
604 register char *cp;
605 register struct enamemem *tp;
607 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
608 return ("isonsap_string: illegal length");
610 tp = lookup_nsap(nsap);
611 if (tp->e_name)
612 return tp->e_name;
614 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
615 if (cp == NULL)
616 error("isonsap_string: malloc");
618 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
619 *cp++ = hex[*nsap >> 4];
620 *cp++ = hex[*nsap++ & 0xf];
621 if (((nsap_idx & 1) == 0) &&
622 (nsap_idx + 1 < nsap_length)) {
623 *cp++ = '.';
626 *cp = '\0';
627 return (tp->e_name);
630 const char *
631 tcpport_string(u_short port)
633 register struct hnamemem *tp;
634 register u_int32_t i = port;
635 char buf[sizeof("00000")];
637 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
638 if (tp->addr == i)
639 return (tp->name);
641 tp->addr = i;
642 tp->nxt = newhnamemem();
644 (void)snprintf(buf, sizeof(buf), "%u", i);
645 tp->name = strdup(buf);
646 return (tp->name);
649 const char *
650 udpport_string(register u_short port)
652 register struct hnamemem *tp;
653 register u_int32_t i = port;
654 char buf[sizeof("00000")];
656 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
657 if (tp->addr == i)
658 return (tp->name);
660 tp->addr = i;
661 tp->nxt = newhnamemem();
663 (void)snprintf(buf, sizeof(buf), "%u", i);
664 tp->name = strdup(buf);
665 return (tp->name);
668 const char *
669 ipxsap_string(u_short port)
671 register char *cp;
672 register struct hnamemem *tp;
673 register u_int32_t i = port;
674 char buf[sizeof("0000")];
676 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
677 if (tp->addr == i)
678 return (tp->name);
680 tp->addr = i;
681 tp->nxt = newhnamemem();
683 cp = buf;
684 NTOHS(port);
685 *cp++ = hex[port >> 12 & 0xf];
686 *cp++ = hex[port >> 8 & 0xf];
687 *cp++ = hex[port >> 4 & 0xf];
688 *cp++ = hex[port & 0xf];
689 *cp++ = '\0';
690 tp->name = strdup(buf);
691 return (tp->name);
694 static void
695 init_servarray(void)
697 struct servent *sv;
698 register struct hnamemem *table;
699 register int i;
700 char buf[sizeof("0000000000")];
702 while ((sv = getservent()) != NULL) {
703 int port = ntohs(sv->s_port);
704 i = port & (HASHNAMESIZE-1);
705 if (strcmp(sv->s_proto, "tcp") == 0)
706 table = &tporttable[i];
707 else if (strcmp(sv->s_proto, "udp") == 0)
708 table = &uporttable[i];
709 else
710 continue;
712 while (table->name)
713 table = table->nxt;
714 if (nflag) {
715 (void)snprintf(buf, sizeof(buf), "%d", port);
716 table->name = strdup(buf);
717 } else
718 table->name = strdup(sv->s_name);
719 table->addr = port;
720 table->nxt = newhnamemem();
722 endservent();
725 /* in libpcap.a (nametoaddr.c) */
726 #if defined(WIN32) && !defined(USE_STATIC_LIBPCAP)
727 __declspec(dllimport)
728 #else
729 extern
730 #endif
731 const struct eproto {
732 const char *s;
733 u_short p;
734 } eproto_db[];
736 static void
737 init_eprotoarray(void)
739 register int i;
740 register struct hnamemem *table;
742 for (i = 0; eproto_db[i].s; i++) {
743 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
744 table = &eprototable[j];
745 while (table->name)
746 table = table->nxt;
747 table->name = eproto_db[i].s;
748 table->addr = htons(eproto_db[i].p);
749 table->nxt = newhnamemem();
753 static struct protoidlist {
754 const u_char protoid[5];
755 const char *name;
756 } protoidlist[] = {
757 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
758 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
759 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
760 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
761 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
762 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
766 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
767 * types.
769 static void
770 init_protoidarray(void)
772 register int i;
773 register struct protoidmem *tp;
774 struct protoidlist *pl;
775 u_char protoid[5];
777 protoid[0] = 0;
778 protoid[1] = 0;
779 protoid[2] = 0;
780 for (i = 0; eproto_db[i].s; i++) {
781 u_short etype = htons(eproto_db[i].p);
783 memcpy((char *)&protoid[3], (char *)&etype, 2);
784 tp = lookup_protoid(protoid);
785 tp->p_name = strdup(eproto_db[i].s);
787 /* Hardwire some SNAP proto ID names */
788 for (pl = protoidlist; pl->name != NULL; ++pl) {
789 tp = lookup_protoid(pl->protoid);
790 /* Don't override existing name */
791 if (tp->p_name != NULL)
792 continue;
794 tp->p_name = pl->name;
798 static struct etherlist {
799 const u_char addr[6];
800 const char *name;
801 } etherlist[] = {
802 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
803 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
807 * Initialize the ethers hash table. We take two different approaches
808 * depending on whether or not the system provides the ethers name
809 * service. If it does, we just wire in a few names at startup,
810 * and etheraddr_string() fills in the table on demand. If it doesn't,
811 * then we suck in the entire /etc/ethers file at startup. The idea
812 * is that parsing the local file will be fast, but spinning through
813 * all the ethers entries via NIS & next_etherent might be very slow.
815 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
816 * since the pcap module already does name-to-address translation,
817 * it's already does most of the work for the ethernet address-to-name
818 * translation, so we just pcap_next_etherent as a convenience.
820 static void
821 init_etherarray(void)
823 register struct etherlist *el;
824 register struct enamemem *tp;
825 #ifdef USE_ETHER_NTOHOST
826 char name[256];
827 #else
828 register struct pcap_etherent *ep;
829 register FILE *fp;
831 /* Suck in entire ethers file */
832 fp = fopen(PCAP_ETHERS_FILE, "r");
833 if (fp != NULL) {
834 while ((ep = pcap_next_etherent(fp)) != NULL) {
835 tp = lookup_emem(ep->addr);
836 tp->e_name = strdup(ep->name);
838 (void)fclose(fp);
840 #endif
842 /* Hardwire some ethernet names */
843 for (el = etherlist; el->name != NULL; ++el) {
844 tp = lookup_emem(el->addr);
845 /* Don't override existing name */
846 if (tp->e_name != NULL)
847 continue;
849 #ifdef USE_ETHER_NTOHOST
851 * Use YP/NIS version of name if available.
853 * We don't cast it to "const struct ether_addr *"
854 * because some systems don't modify the Ethernet
855 * address but fail to declare the second argument
856 * as a "const" pointer.
858 if (ether_ntohost(name, (struct ether_addr *)el->addr) == 0) {
859 tp->e_name = strdup(name);
860 continue;
862 #endif
863 tp->e_name = el->name;
867 static struct tok ipxsap_db[] = {
868 { 0x0000, "Unknown" },
869 { 0x0001, "User" },
870 { 0x0002, "User Group" },
871 { 0x0003, "PrintQueue" },
872 { 0x0004, "FileServer" },
873 { 0x0005, "JobServer" },
874 { 0x0006, "Gateway" },
875 { 0x0007, "PrintServer" },
876 { 0x0008, "ArchiveQueue" },
877 { 0x0009, "ArchiveServer" },
878 { 0x000a, "JobQueue" },
879 { 0x000b, "Administration" },
880 { 0x000F, "Novell TI-RPC" },
881 { 0x0017, "Diagnostics" },
882 { 0x0020, "NetBIOS" },
883 { 0x0021, "NAS SNA Gateway" },
884 { 0x0023, "NACS AsyncGateway" },
885 { 0x0024, "RemoteBridge/RoutingService" },
886 { 0x0026, "BridgeServer" },
887 { 0x0027, "TCP/IP Gateway" },
888 { 0x0028, "Point-to-point X.25 BridgeServer" },
889 { 0x0029, "3270 Gateway" },
890 { 0x002a, "CHI Corp" },
891 { 0x002c, "PC Chalkboard" },
892 { 0x002d, "TimeSynchServer" },
893 { 0x002e, "ARCserve5.0/PalindromeBackup" },
894 { 0x0045, "DI3270 Gateway" },
895 { 0x0047, "AdvertisingPrintServer" },
896 { 0x004a, "NetBlazerModems" },
897 { 0x004b, "BtrieveVAP" },
898 { 0x004c, "NetwareSQL" },
899 { 0x004d, "XtreeNetwork" },
900 { 0x0050, "BtrieveVAP4.11" },
901 { 0x0052, "QuickLink" },
902 { 0x0053, "PrintQueueUser" },
903 { 0x0058, "Multipoint X.25 Router" },
904 { 0x0060, "STLB/NLM" },
905 { 0x0064, "ARCserve" },
906 { 0x0066, "ARCserve3.0" },
907 { 0x0072, "WAN CopyUtility" },
908 { 0x007a, "TES-NetwareVMS" },
909 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
910 { 0x0095, "DDA OBGYN" },
911 { 0x0098, "NetwareAccessServer" },
912 { 0x009a, "Netware for VMS II/NamedPipeServer" },
913 { 0x009b, "NetwareAccessServer" },
914 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
915 { 0x00a1, "PowerchuteAPC UPS" },
916 { 0x00aa, "LAWserve" },
917 { 0x00ac, "CompaqIDA StatusMonitor" },
918 { 0x0100, "PIPE STAIL" },
919 { 0x0102, "LAN ProtectBindery" },
920 { 0x0103, "OracleDataBaseServer" },
921 { 0x0107, "Netware386/RSPX RemoteConsole" },
922 { 0x010f, "NovellSNA Gateway" },
923 { 0x0111, "TestServer" },
924 { 0x0112, "HP PrintServer" },
925 { 0x0114, "CSA MUX" },
926 { 0x0115, "CSA LCA" },
927 { 0x0116, "CSA CM" },
928 { 0x0117, "CSA SMA" },
929 { 0x0118, "CSA DBA" },
930 { 0x0119, "CSA NMA" },
931 { 0x011a, "CSA SSA" },
932 { 0x011b, "CSA STATUS" },
933 { 0x011e, "CSA APPC" },
934 { 0x0126, "SNA TEST SSA Profile" },
935 { 0x012a, "CSA TRACE" },
936 { 0x012b, "NetwareSAA" },
937 { 0x012e, "IKARUS VirusScan" },
938 { 0x0130, "CommunicationsExecutive" },
939 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
940 { 0x0135, "NetwareNamingServicesProfile" },
941 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
942 { 0x0141, "LAN SpoolServer" },
943 { 0x0152, "IRMALAN Gateway" },
944 { 0x0154, "NamedPipeServer" },
945 { 0x0166, "NetWareManagement" },
946 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
947 { 0x0173, "Compaq" },
948 { 0x0174, "Compaq SNMP Agent" },
949 { 0x0175, "Compaq" },
950 { 0x0180, "XTreeServer/XTreeTools" },
951 { 0x018A, "NASI ServicesBroadcastServer" },
952 { 0x01b0, "GARP Gateway" },
953 { 0x01b1, "Binfview" },
954 { 0x01bf, "IntelLanDeskManager" },
955 { 0x01ca, "AXTEC" },
956 { 0x01cb, "ShivaNetModem/E" },
957 { 0x01cc, "ShivaLanRover/E" },
958 { 0x01cd, "ShivaLanRover/T" },
959 { 0x01ce, "ShivaUniversal" },
960 { 0x01d8, "CastelleFAXPressServer" },
961 { 0x01da, "CastelleLANPressPrintServer" },
962 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
963 { 0x01f0, "LEGATO" },
964 { 0x01f5, "LEGATO" },
965 { 0x0233, "NMS Agent/NetwareManagementAgent" },
966 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
967 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
968 { 0x023a, "LANtern" },
969 { 0x023c, "MAVERICK" },
970 { 0x023f, "NovellSMDR" },
971 { 0x024e, "NetwareConnect" },
972 { 0x024f, "NASI ServerBroadcast Cisco" },
973 { 0x026a, "NMS ServiceConsole" },
974 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
975 { 0x0278, "DirectoryServer Netware 4.x" },
976 { 0x027b, "NetwareManagementAgent" },
977 { 0x0280, "Novell File and Printer Sharing Service for PC" },
978 { 0x0304, "NovellSAA Gateway" },
979 { 0x0308, "COM/VERMED" },
980 { 0x030a, "GalacticommWorldgroupServer" },
981 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
982 { 0x0320, "AttachmateGateway" },
983 { 0x0327, "MicrosoftDiagnostiocs" },
984 { 0x0328, "WATCOM SQL Server" },
985 { 0x0335, "MultiTechSystems MultisynchCommServer" },
986 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
987 { 0x0355, "ArcadaBackupExec" },
988 { 0x0358, "MSLCD1" },
989 { 0x0361, "NETINELO" },
990 { 0x037e, "Powerchute UPS Monitoring" },
991 { 0x037f, "ViruSafeNotify" },
992 { 0x0386, "HP Bridge" },
993 { 0x0387, "HP Hub" },
994 { 0x0394, "NetWare SAA Gateway" },
995 { 0x039b, "LotusNotes" },
996 { 0x03b7, "CertusAntiVirus" },
997 { 0x03c4, "ARCserve4.0" },
998 { 0x03c7, "LANspool3.5" },
999 { 0x03d7, "LexmarkPrinterServer" },
1000 { 0x03d8, "LexmarkXLE PrinterServer" },
1001 { 0x03dd, "BanyanENS NetwareClient" },
1002 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1003 { 0x03e1, "UnivelUnixware" },
1004 { 0x03e4, "UnivelUnixware" },
1005 { 0x03fc, "IntelNetport" },
1006 { 0x03fd, "PrintServerQueue" },
1007 { 0x040A, "ipnServer" },
1008 { 0x040D, "LVERRMAN" },
1009 { 0x040E, "LVLIC" },
1010 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1011 { 0x0429, "SiteLockVirus" },
1012 { 0x0432, "UFHELPR???" },
1013 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1014 { 0x0444, "MicrosoftNT SNA Server" },
1015 { 0x0448, "Oracle" },
1016 { 0x044c, "ARCserve5.01" },
1017 { 0x0457, "CanonGP55" },
1018 { 0x045a, "QMS Printers" },
1019 { 0x045b, "DellSCSI Array" },
1020 { 0x0491, "NetBlazerModems" },
1021 { 0x04ac, "OnTimeScheduler" },
1022 { 0x04b0, "CD-Net" },
1023 { 0x0513, "EmulexNQA" },
1024 { 0x0520, "SiteLockChecks" },
1025 { 0x0529, "SiteLockChecks" },
1026 { 0x052d, "CitrixOS2 AppServer" },
1027 { 0x0535, "Tektronix" },
1028 { 0x0536, "Milan" },
1029 { 0x055d, "Attachmate SNA gateway" },
1030 { 0x056b, "IBM8235 ModemServer" },
1031 { 0x056c, "ShivaLanRover/E PLUS" },
1032 { 0x056d, "ShivaLanRover/T PLUS" },
1033 { 0x0580, "McAfeeNetShield" },
1034 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1035 { 0x05BA, "CompatibleSystemsRouters" },
1036 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1037 { 0x0606, "JCWatermarkImaging" },
1038 { 0x060c, "AXISNetworkPrinter" },
1039 { 0x0610, "AdaptecSCSIManagement" },
1040 { 0x0621, "IBM AntiVirus" },
1041 { 0x0640, "Windows95 RemoteRegistryService" },
1042 { 0x064e, "MicrosoftIIS" },
1043 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1044 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1045 { 0x076C, "Xerox" },
1046 { 0x079b, "ShivaLanRover/E 115" },
1047 { 0x079c, "ShivaLanRover/T 115" },
1048 { 0x07B4, "CubixWorldDesk" },
1049 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1050 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1051 { 0x0810, "ELAN License Server Demo" },
1052 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1053 { 0x086a, "ISSC Collector" },
1054 { 0x087f, "ISSC DAS AgentAIX" },
1055 { 0x0880, "Intel Netport PRO" },
1056 { 0x0881, "Intel Netport PRO" },
1057 { 0x0b29, "SiteLock" },
1058 { 0x0c29, "SiteLockApplications" },
1059 { 0x0c2c, "LicensingServer" },
1060 { 0x2101, "PerformanceTechnologyInstantInternet" },
1061 { 0x2380, "LAI SiteLock" },
1062 { 0x238c, "MeetingMaker" },
1063 { 0x4808, "SiteLockServer/SiteLockMetering" },
1064 { 0x5555, "SiteLockUser" },
1065 { 0x6312, "Tapeware" },
1066 { 0x6f00, "RabbitGateway" },
1067 { 0x7703, "MODEM" },
1068 { 0x8002, "NetPortPrinters" },
1069 { 0x8008, "WordPerfectNetworkVersion" },
1070 { 0x85BE, "Cisco EIGRP" },
1071 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1072 { 0x9000, "McAfeeNetShield" },
1073 { 0x9604, "CSA-NT_MON" },
1074 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1075 { 0xf11f, "SiteLockMetering" },
1076 { 0xf1ff, "SiteLock" },
1077 { 0xf503, "Microsoft SQL Server" },
1078 { 0xF905, "IBM TimeAndPlace" },
1079 { 0xfbfb, "TopCallIII FaxServer" },
1080 { 0xffff, "AnyService/Wildcard" },
1081 { 0, (char *)0 }
1084 static void
1085 init_ipxsaparray(void)
1087 register int i;
1088 register struct hnamemem *table;
1090 for (i = 0; ipxsap_db[i].s != NULL; i++) {
1091 int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
1092 table = &ipxsaptable[j];
1093 while (table->name)
1094 table = table->nxt;
1095 table->name = ipxsap_db[i].s;
1096 table->addr = htons(ipxsap_db[i].v);
1097 table->nxt = newhnamemem();
1102 * Initialize the address to name translation machinery. We map all
1103 * non-local IP addresses to numeric addresses if fflag is true (i.e.,
1104 * to prevent blocking on the nameserver). localnet is the IP address
1105 * of the local network. mask is its subnet mask.
1107 void
1108 init_addrtoname(u_int32_t localnet, u_int32_t mask)
1110 if (fflag) {
1111 f_localnet = localnet;
1112 f_netmask = mask;
1114 if (nflag)
1116 * Simplest way to suppress names.
1118 return;
1120 init_etherarray();
1121 init_servarray();
1122 init_eprotoarray();
1123 init_protoidarray();
1124 init_ipxsaparray();
1127 const char *
1128 dnaddr_string(u_short dnaddr)
1130 register struct hnamemem *tp;
1132 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != 0;
1133 tp = tp->nxt)
1134 if (tp->addr == dnaddr)
1135 return (tp->name);
1137 tp->addr = dnaddr;
1138 tp->nxt = newhnamemem();
1139 if (nflag)
1140 tp->name = dnnum_string(dnaddr);
1141 else
1142 tp->name = dnname_string(dnaddr);
1144 return(tp->name);
1147 /* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1148 struct hnamemem *
1149 newhnamemem(void)
1151 register struct hnamemem *p;
1152 static struct hnamemem *ptr = NULL;
1153 static u_int num = 0;
1155 if (num <= 0) {
1156 num = 64;
1157 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1158 if (ptr == NULL)
1159 error("newhnamemem: calloc");
1161 --num;
1162 p = ptr++;
1163 return (p);
1166 #ifdef INET6
1167 /* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1168 struct h6namemem *
1169 newh6namemem(void)
1171 register struct h6namemem *p;
1172 static struct h6namemem *ptr = NULL;
1173 static u_int num = 0;
1175 if (num <= 0) {
1176 num = 64;
1177 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1178 if (ptr == NULL)
1179 error("newh6namemem: calloc");
1181 --num;
1182 p = ptr++;
1183 return (p);
1185 #endif /* INET6 */