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