OSDN Git Service

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