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