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