7c8128918d6665262f4fa7c77ca95d8aeb5b36f1
[metze/wireshark/wip.git] / epan / resolv.c
1 /* resolv.c
2  * Routines for network object lookup
3  *
4  * $Id: resolv.c,v 1.24 2002/08/02 21:29:40 jmayer Exp $
5  *
6  * Laurent Deniel <deniel@worldnet.fr>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #ifndef AVOID_DNS_TIMEOUT
37 #define AVOID_DNS_TIMEOUT
38 #endif
39 #endif
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #ifdef HAVE_NETINET_IN_H
46 # include <netinet/in.h>
47 #endif
48
49 #ifdef HAVE_NETDB_H
50 #include <netdb.h>
51 #endif
52
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
55 #endif
56
57 #include <signal.h>
58
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
61 #endif
62
63 #ifdef AVOID_DNS_TIMEOUT
64 # include <setjmp.h>
65 #endif
66
67 #ifdef NEED_INET_ATON_H
68 # include "inet_aton.h"
69 #endif
70
71 #ifdef NEED_INET_V6DEFS_H
72 # include "inet_v6defs.h"
73 #endif
74
75 #include "packet.h"
76 #include "ipv6-utils.h"
77 #include "resolv.h"
78 #include "filesystem.h"
79
80 #define ENAME_ETHERS            "ethers"
81 #define ENAME_IPXNETS           "ipxnets"
82 #define ENAME_MANUF             "manuf"
83
84 #define MAXMANUFLEN     9       /* max vendor name length with ending '\0' */
85 #define HASHETHSIZE     1024
86 #define HASHHOSTSIZE    1024
87 #define HASHIPXNETSIZE  256
88 #define HASHMANUFSIZE   256
89 #define HASHPORTSIZE    256
90
91 /* hash table used for host and port lookup */
92
93 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
94
95 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
96
97 typedef struct hashname {
98   guint                 addr;
99   guchar                name[MAXNAMELEN];
100   gboolean              is_dummy_entry; /* name is IP address in dot format */
101   struct hashname       *next;
102 } hashname_t;
103
104 /* hash table used for IPX network lookup */
105
106 /* XXX - check goodness of hash function */
107
108 #define HASH_IPX_NET(net)       ((net) & (HASHIPXNETSIZE - 1))
109
110 typedef struct hashname hashipxnet_t;
111
112 /* hash tables used for ethernet and manufacturer lookup */
113
114 #define HASH_ETH_ADDRESS(addr) \
115         (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
116          (HASHETHSIZE - 1))
117
118 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
119
120 typedef struct hashmanuf {
121   guint8                addr[3];
122   char                  name[MAXMANUFLEN];
123   struct hashmanuf      *next;
124 } hashmanuf_t;
125
126 typedef struct hashether {
127   guint8                addr[6];
128   char                  name[MAXNAMELEN];
129   gboolean              is_dummy_entry;         /* not a complete entry */
130   struct hashether      *next;
131 } hashether_t;
132
133 /* internal ethernet type */
134
135 typedef struct _ether
136 {
137   guint8                addr[6];
138   char                  name[MAXNAMELEN];
139 } ether_t;
140
141 /* internal ipxnet type */
142
143 typedef struct _ipxnet
144 {
145   guint                 addr;
146   char                  name[MAXNAMELEN];
147 } ipxnet_t;
148
149 static hashname_t       *host_table[HASHHOSTSIZE];
150 static hashname_t       *udp_port_table[HASHPORTSIZE];
151 static hashname_t       *tcp_port_table[HASHPORTSIZE];
152 static hashname_t       *sctp_port_table[HASHPORTSIZE];
153 static hashether_t      *eth_table[HASHETHSIZE];
154 static hashmanuf_t      *manuf_table[HASHMANUFSIZE];
155 static hashipxnet_t     *ipxnet_table[HASHIPXNETSIZE];
156
157 static int              eth_resolution_initialized = 0;
158 static int              ipxnet_resolution_initialized = 0;
159
160 /*
161  * Flag controlling what names to resolve.
162  */
163 guint32 g_resolv_flags;
164
165 /*
166  *  Global variables (can be changed in GUI sections)
167  *  XXX - they could be changed in GUI code, but there's currently no
168  *  GUI code to change them.
169  */
170
171 gchar *g_ethers_path  = NULL;           /* global ethers file    */
172 gchar *g_pethers_path = NULL;           /* personal ethers file  */
173 gchar *g_ipxnets_path  = NULL;          /* global ipxnets file   */
174 gchar *g_pipxnets_path = NULL;          /* personal ipxnets file */
175                                         /* first resolving call  */
176
177 /*
178  *  Local function definitions 
179  */
180
181 static guchar *serv_name_lookup(guint port, port_type proto)
182 {
183   int hash_idx;
184   hashname_t *tp;
185   hashname_t **table;
186   char *serv_proto = NULL;
187   struct servent *servp;
188
189   switch(proto) {
190   case PT_UDP:
191     table = udp_port_table;
192     serv_proto = "udp";
193     break;
194   case PT_TCP:
195     table = tcp_port_table;
196     serv_proto = "tcp";
197     break;
198   case PT_SCTP:
199     table = sctp_port_table;
200     serv_proto = "sctp";
201     break;
202   default:
203     /* not yet implemented */
204     return NULL;
205     /*NOTREACHED*/
206     break;
207   } /* proto */
208   
209   hash_idx = HASH_PORT(port);
210   tp = table[hash_idx];
211
212   if( tp == NULL ) {
213     tp = table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
214   } else {  
215     while(1) {
216       if( tp->addr == port ) {
217         return tp->name;
218       }
219       if (tp->next == NULL) {
220         tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
221         tp = tp->next;
222         break;
223       }
224       tp = tp->next;
225     }
226   }
227   
228   /* fill in a new entry */
229   tp->addr = port;
230   tp->next = NULL;
231
232   if (!(g_resolv_flags & RESOLV_TRANSPORT) || 
233       (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
234     /* unknown port */
235     sprintf(tp->name, "%d", port);
236   } else {
237     strncpy(tp->name, servp->s_name, MAXNAMELEN);
238     tp->name[MAXNAMELEN-1] = '\0';
239   }
240
241   return (tp->name);
242
243 } /* serv_name_lookup */
244
245 #ifdef AVOID_DNS_TIMEOUT
246
247 #define DNS_TIMEOUT     2       /* max sec per call */
248
249 jmp_buf hostname_env;
250
251 static void abort_network_query(int sig _U_)
252 {
253   longjmp(hostname_env, 1);
254 }
255 #endif /* AVOID_DNS_TIMEOUT */
256
257 static guchar *host_name_lookup(guint addr, gboolean *found)
258 {
259   int hash_idx;
260   hashname_t * volatile tp;
261   struct hostent *hostp;
262
263   *found = TRUE;
264
265   hash_idx = HASH_IPV4_ADDRESS(addr);
266
267   tp = host_table[hash_idx];
268
269   if( tp == NULL ) {
270     tp = host_table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
271   } else {  
272     while(1) {
273       if( tp->addr == addr ) {
274         if (tp->is_dummy_entry)
275           *found = FALSE;
276         return tp->name;
277       }
278       if (tp->next == NULL) {
279         tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
280         tp = tp->next;
281         break;
282       }
283       tp = tp->next;
284     }
285   }
286   
287   /* fill in a new entry */
288   tp->addr = addr;
289   tp->next = NULL;
290
291   /*
292    * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
293    * the name of the host on which it's running; to work around that
294    * botch, we don't try to translate an all-zero IP address to a host
295    * name.
296    */
297   if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
298 #ifdef AVOID_DNS_TIMEOUT
299     
300     /* Quick hack to avoid DNS/YP timeout */
301   
302     if (!setjmp(hostname_env)) {
303       signal(SIGALRM, abort_network_query);
304       alarm(DNS_TIMEOUT);
305 #endif
306       hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
307 #ifdef AVOID_DNS_TIMEOUT
308       alarm(0);
309 #endif
310       if (hostp != NULL) {
311         strncpy(tp->name, hostp->h_name, MAXNAMELEN);
312         tp->name[MAXNAMELEN-1] = '\0';
313         tp->is_dummy_entry = FALSE;
314         return tp->name;
315       }
316 #ifdef AVOID_DNS_TIMEOUT
317     }
318 #endif
319   }
320
321   /* unknown host or DNS timeout */
322
323   ip_to_str_buf((guint8 *)&addr, tp->name);
324   tp->is_dummy_entry = TRUE;
325   *found = FALSE;
326
327   return (tp->name);
328
329 } /* host_name_lookup */
330
331 static guchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
332 {
333   static guchar name[MAXNAMELEN];
334 #ifdef INET6
335   struct hostent *hostp;
336
337   if (g_resolv_flags & RESOLV_NETWORK) {
338 #ifdef AVOID_DNS_TIMEOUT
339     
340     /* Quick hack to avoid DNS/YP timeout */
341     
342     if (!setjmp(hostname_env)) {
343       signal(SIGALRM, abort_network_query);
344       alarm(DNS_TIMEOUT);
345 #endif /* AVOID_DNS_TIMEOUT */
346       hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
347 #ifdef AVOID_DNS_TIMEOUT
348       alarm(0);
349 #endif
350       if (hostp != NULL) {
351         strncpy(name, hostp->h_name, MAXNAMELEN);
352         name[MAXNAMELEN-1] = '\0';
353         *found = TRUE;
354         return name;
355       }
356 #ifdef AVOID_DNS_TIMEOUT
357     }
358 #endif
359   }
360
361   /* unknown host or DNS timeout */
362 #endif /* INET6 */
363   *found = FALSE;
364   sprintf(name, "%s", ip6_to_str(addr));  
365   return (name);
366 }
367
368 /*
369  *  Miscellaneous functions
370  */
371
372 static int fgetline(char **buf, int *size, FILE *fp)
373 {
374   int len;
375   int c;
376
377   if (fp == NULL)
378     return -1;
379
380   if (*buf == NULL) {
381     if (*size == 0) 
382       *size = BUFSIZ;
383     
384     if ((*buf = g_malloc(*size)) == NULL)
385       return -1;
386   }
387
388   if (feof(fp))
389     return -1;
390     
391   len = 0;
392   while ((c = getc(fp)) != EOF && c != '\n') {
393     if (len+1 >= *size) {
394       if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
395         return -1;
396     }
397     (*buf)[len++] = c;
398   }
399
400   if (len == 0 && c == EOF)
401     return -1;
402     
403   (*buf)[len] = '\0';
404     
405   return len;
406
407 } /* fgetline */
408
409
410 /*
411  * Ethernet / manufacturer resolution
412  *
413  * The following functions implement ethernet address resolution and
414  * ethers files parsing (see ethers(4)). 
415  *
416  * The manuf file has the same format as ethers(4) except that names are 
417  * truncated to MAXMANUFLEN-1 characters and that an address contains 
418  * only 3 bytes (instead of 6).
419  *
420  * Notes:
421  *
422  * I decide to not use the existing functions (see ethers(3) on some 
423  * operating systems) for the following reasons:
424  * - performance gains (use of hash tables and some other enhancements),
425  * - use of two ethers files (system-wide and per user),
426  * - avoid the use of NIS maps,
427  * - lack of these functions on some systems.
428  *
429  * So the following functions do _not_ behave as the standard ones.
430  *
431  * -- Laurent.
432  */
433
434
435 static int parse_ether_line(char *line, ether_t *eth, int six_bytes)
436 {
437   /*
438    *  See man ethers(4) for ethers file format
439    *  (not available on all systems).
440    *  We allow both ethernet address separators (':' and '-'),
441    *  as well as Ethereal's '.' separator.
442    */
443
444   gchar *cp;
445   int a0, a1, a2, a3, a4, a5;
446     
447   if ((cp = strchr(line, '#')))
448     *cp = '\0';
449   
450   if ((cp = strtok(line, " \t\n")) == NULL)
451     return -1;
452
453   if (six_bytes) {
454     if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6) {
455       if (sscanf(cp, "%x-%x-%x-%x-%x-%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6) {
456         if (sscanf(cp, "%x.%x.%x.%x.%x.%x", &a0, &a1, &a2, &a3, &a4, &a5) != 6)
457           return -1;
458       }
459     }
460   } else {
461     if (sscanf(cp, "%x:%x:%x", &a0, &a1, &a2) != 3) {
462       if (sscanf(cp, "%x-%x-%x", &a0, &a1, &a2) != 3) {
463         if (sscanf(cp, "%x.%x.%x", &a0, &a1, &a2) != 3)
464         return -1;
465       }
466     }
467   }
468
469   if ((cp = strtok(NULL, " \t\n")) == NULL)
470     return -1;
471
472   eth->addr[0] = a0;
473   eth->addr[1] = a1;
474   eth->addr[2] = a2;
475   if (six_bytes) {
476     eth->addr[3] = a3;
477     eth->addr[4] = a4;
478     eth->addr[5] = a5;
479   } else {
480     eth->addr[3] = 0;
481     eth->addr[4] = 0;
482     eth->addr[5] = 0;
483   }
484
485   strncpy(eth->name, cp, MAXNAMELEN);
486   eth->name[MAXNAMELEN-1] = '\0';
487
488   return 0;
489
490 } /* parse_ether_line */
491
492 static FILE *eth_p = NULL;
493
494 static void set_ethent(char *path)
495 {
496   if (eth_p)
497     rewind(eth_p);
498   else
499     eth_p = fopen(path, "r");
500 }
501
502 static void end_ethent(void)
503 {
504   if (eth_p) {
505     fclose(eth_p);
506     eth_p = NULL;
507   }
508 }
509
510 static ether_t *get_ethent(int six_bytes)
511 {
512  
513   static ether_t eth;
514   static int     size = 0;
515   static char   *buf = NULL;
516   
517   if (eth_p == NULL) 
518     return NULL;
519
520   while (fgetline(&buf, &size, eth_p) >= 0) {
521     if (parse_ether_line(buf, &eth, six_bytes) == 0) {
522       return &eth;
523     }
524   }
525     
526   return NULL;
527
528 } /* get_ethent */
529
530 static ether_t *get_ethbyname(const guchar *name)
531 {
532   ether_t *eth;
533   
534   set_ethent(g_ethers_path);
535
536   while ((eth = get_ethent(1)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
537     ;
538
539   if (eth == NULL) {
540     end_ethent();
541     
542     set_ethent(g_pethers_path);
543
544     while ((eth = get_ethent(1)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
545       ;
546
547     end_ethent();
548   }
549
550   return eth;
551
552 } /* get_ethbyname */
553
554 static ether_t *get_ethbyaddr(const guint8 *addr)
555 {
556
557   ether_t *eth;
558   
559   set_ethent(g_ethers_path);
560
561   while ((eth = get_ethent(1)) && memcmp(addr, eth->addr, 6) != 0)
562     ;
563
564   if (eth == NULL) {
565     end_ethent();
566     
567     set_ethent(g_pethers_path);
568     
569     while ((eth = get_ethent(1)) && memcmp(addr, eth->addr, 6) != 0)
570       ;
571     
572     end_ethent();
573   }
574
575   return eth;
576
577 } /* get_ethbyaddr */
578
579 static void add_manuf_name(guint8 *addr, guchar *name)
580 {
581   int hash_idx;
582   hashmanuf_t *tp;
583
584   hash_idx = HASH_ETH_MANUF(addr);
585
586   tp = manuf_table[hash_idx];
587
588   if( tp == NULL ) {
589     tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
590   } else {  
591     while(1) {
592       if (tp->next == NULL) {
593         tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
594         tp = tp->next;
595         break;
596       }
597       tp = tp->next;
598     }
599   }
600   
601   memcpy(tp->addr, addr, sizeof(tp->addr));
602   strncpy(tp->name, name, MAXMANUFLEN);
603   tp->name[MAXMANUFLEN-1] = '\0';
604   tp->next = NULL;
605
606 } /* add_manuf_name */
607
608 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
609 {
610   int hash_idx;
611   hashmanuf_t *tp;
612
613   hash_idx = HASH_ETH_MANUF(addr);
614
615   tp = manuf_table[hash_idx];
616   
617   while(tp != NULL) {
618     if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
619       return tp;
620     }
621     tp = tp->next;
622   }
623   
624   return NULL;
625
626 } /* manuf_name_lookup */
627
628 static void initialize_ethers(void)
629 {
630   ether_t *eth;
631   char *manuf_path;
632
633   /* Compute the pathname of the ethers file. */
634   if (g_ethers_path == NULL) {
635     g_ethers_path = g_malloc(strlen(get_systemfile_dir()) +
636                              strlen(ENAME_ETHERS) + 2);
637     sprintf(g_ethers_path, "%s" G_DIR_SEPARATOR_S "%s",
638             get_systemfile_dir(), ENAME_ETHERS);
639   }
640
641   /* Set g_pethers_path here, but don't actually do anything
642    * with it. It's used in get_ethbyname() and get_ethbyaddr()
643    */
644   if (g_pethers_path == NULL)
645     g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
646
647   /* manuf hash table initialization */
648
649   /* Compute the pathname of the manuf file */
650   manuf_path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
651     strlen(ENAME_MANUF) + 2);
652   sprintf(manuf_path, "%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
653     ENAME_MANUF);
654   
655   /* Read it and initialize the hash table */
656   set_ethent(manuf_path);
657
658   while ((eth = get_ethent(0))) {
659     add_manuf_name(eth->addr, eth->name);
660   }
661
662   end_ethent();
663
664   g_free(manuf_path);
665
666 } /* initialize_ethers */
667
668 static hashether_t *add_eth_name(const guint8 *addr, const guchar *name)
669 {
670   int hash_idx;
671   hashether_t *tp;
672
673   hash_idx = HASH_ETH_ADDRESS(addr);
674
675   tp = eth_table[hash_idx];
676
677   if( tp == NULL ) {
678     tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
679   } else {  
680     while(1) {
681       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
682         /* address already known */
683         if (!tp->is_dummy_entry) {
684           return tp;
685         } else {
686           /* replace this dummy (manuf) entry with a real name */
687           break;
688         }
689       }
690       if (tp->next == NULL) {
691         tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
692         tp = tp->next;
693         break;
694       }
695       tp = tp->next;
696     }
697   }
698   
699   memcpy(tp->addr, addr, sizeof(tp->addr));
700   strncpy(tp->name, name, MAXNAMELEN);
701   tp->name[MAXNAMELEN-1] = '\0';
702   tp->next = NULL;
703   tp->is_dummy_entry = FALSE;
704
705   return tp;
706
707 } /* add_eth_name */
708
709 static guchar *eth_name_lookup(const guint8 *addr)
710 {
711   int hash_idx;
712   hashmanuf_t *manufp;
713   hashether_t *tp;
714   ether_t *eth;
715
716   hash_idx = HASH_ETH_ADDRESS(addr);
717
718   tp = eth_table[hash_idx];
719
720   if( tp == NULL ) {
721     tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
722   } else {  
723     while(1) {
724       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
725         return tp->name;
726       }
727       if (tp->next == NULL) {
728         tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
729         tp = tp->next;
730         break;
731       }
732       tp = tp->next;
733     }
734   }
735   
736   /* fill in a new entry */
737
738   memcpy(tp->addr, addr, sizeof(tp->addr));
739   tp->next = NULL;
740
741   if ( (eth = get_ethbyaddr(addr)) == NULL) {
742     /* unknown name */
743
744     if ((manufp = manuf_name_lookup(addr)) == NULL)
745       sprintf(tp->name, "%s", ether_to_str((guint8 *)addr));
746     else
747       sprintf(tp->name, "%s_%02x:%02x:%02x", 
748               manufp->name, addr[3], addr[4], addr[5]);
749
750     tp->is_dummy_entry = TRUE;
751
752   } else {
753     strncpy(tp->name, eth->name, MAXNAMELEN);
754     tp->name[MAXNAMELEN-1] = '\0';
755     tp->is_dummy_entry = FALSE;
756   }
757
758   return (tp->name);
759
760 } /* eth_name_lookup */
761
762 static guint8 *eth_addr_lookup(const guchar *name)
763 {
764   ether_t *eth;
765   hashether_t *tp;
766   hashether_t **table = eth_table;
767   int i;
768
769   /* to be optimized (hash table from name to addr) */
770   for (i = 0; i < HASHETHSIZE; i++) {
771     tp = table[i];
772     while (tp) {
773       if (strcmp(tp->name, name) == 0)
774         return tp->addr;
775       tp = tp->next;
776     }
777   }
778
779   /* not in hash table : performs a file lookup */
780
781   if ((eth = get_ethbyname(name)) == NULL)
782     return NULL;
783
784   /* add new entry in hash table */
785
786   tp = add_eth_name(eth->addr, name);
787
788   return tp->addr;
789
790 } /* eth_addr_lookup */
791
792
793 /* IPXNETS */
794 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
795 {
796   /*
797    *  We allow three address separators (':', '-', and '.'),
798    *  as well as no separators
799    */
800
801   gchar         *cp;
802   guint32       a, a0, a1, a2, a3;
803   gboolean      found_single_number = FALSE;
804     
805   if ((cp = strchr(line, '#')))
806     *cp = '\0';
807   
808   if ((cp = strtok(line, " \t\n")) == NULL)
809     return -1;
810
811   /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
812    * fill a and found_single_number is TRUE,
813    * or return -1
814    */
815   if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
816     if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
817       if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
818         if (sscanf(cp, "%x", &a) == 1) {
819           found_single_number = TRUE;
820         }
821         else {
822           return -1;
823         }
824       }
825     }
826   }
827
828   if ((cp = strtok(NULL, " \t\n")) == NULL)
829     return -1;
830
831   if (found_single_number) {
832         ipxnet->addr = a;
833   }
834   else {
835         ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
836   }
837
838   strncpy(ipxnet->name, cp, MAXNAMELEN);
839   ipxnet->name[MAXNAMELEN-1] = '\0';
840
841   return 0;
842
843 } /* parse_ipxnets_line */
844
845 static FILE *ipxnet_p = NULL;
846
847 static void set_ipxnetent(char *path)
848 {
849   if (ipxnet_p)
850     rewind(ipxnet_p);
851   else
852     ipxnet_p = fopen(path, "r");
853 }
854
855 static void end_ipxnetent(void)
856 {
857   if (ipxnet_p) {
858     fclose(ipxnet_p);
859     ipxnet_p = NULL;
860   }
861 }
862
863 static ipxnet_t *get_ipxnetent(void)
864 {
865  
866   static ipxnet_t ipxnet;
867   static int     size = 0;
868   static char   *buf = NULL;
869   
870   if (ipxnet_p == NULL) 
871     return NULL;
872
873   while (fgetline(&buf, &size, ipxnet_p) >= 0) {
874     if (parse_ipxnets_line(buf, &ipxnet) == 0) {
875       return &ipxnet;
876     }
877   }
878     
879   return NULL;
880
881 } /* get_ipxnetent */
882
883 static ipxnet_t *get_ipxnetbyname(const guchar *name)
884 {
885   ipxnet_t *ipxnet;
886   
887   set_ipxnetent(g_ipxnets_path);
888
889   while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
890     ;
891
892   if (ipxnet == NULL) {
893     end_ipxnetent();
894     
895     set_ipxnetent(g_pipxnets_path);
896
897     while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
898       ;
899
900     end_ipxnetent();
901   }
902
903   return ipxnet;
904
905 } /* get_ipxnetbyname */
906
907 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
908 {
909
910   ipxnet_t *ipxnet;
911   
912   set_ipxnetent(g_ipxnets_path);
913
914   while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
915
916   if (ipxnet == NULL) {
917     end_ipxnetent();
918     
919     set_ipxnetent(g_pipxnets_path);
920     
921     while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
922       ;
923     
924     end_ipxnetent();
925   }
926
927   return ipxnet;
928
929 } /* get_ipxnetbyaddr */
930
931 static void initialize_ipxnets(void)
932 {
933   /* Compute the pathname of the ipxnets file.
934    *
935    * XXX - is there a notion of an "ipxnets file" in any flavor of
936    * UNIX, or with any add-on Netware package for UNIX?  If not,
937    * should the UNIX version of the ipxnets file be in the datafile
938    * directory as well?
939    */
940   if (g_ipxnets_path == NULL) {
941     g_ipxnets_path = g_malloc(strlen(get_systemfile_dir()) +
942                               strlen(ENAME_IPXNETS) + 2);
943     sprintf(g_ipxnets_path, "%s" G_DIR_SEPARATOR_S "%s",
944             get_systemfile_dir(), ENAME_IPXNETS);
945   }
946
947   /* Set g_pipxnets_path here, but don't actually do anything
948    * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
949    */
950   if (g_pipxnets_path == NULL)
951     g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
952
953 } /* initialize_ipxnets */
954
955 static hashipxnet_t *add_ipxnet_name(guint addr, const guchar *name)
956 {
957   int hash_idx;
958   hashipxnet_t *tp;
959
960   hash_idx = HASH_IPX_NET(addr);
961
962   tp = ipxnet_table[hash_idx];
963
964   if( tp == NULL ) {
965     tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
966   } else {  
967     while(1) {
968       if (tp->next == NULL) {
969         tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
970         tp = tp->next;
971         break;
972       }
973       tp = tp->next;
974     }
975   }
976   
977   tp->addr = addr;
978   strncpy(tp->name, name, MAXNAMELEN);
979   tp->name[MAXNAMELEN-1] = '\0';
980   tp->next = NULL;
981
982   return tp;
983
984 } /* add_ipxnet_name */
985
986 static guchar *ipxnet_name_lookup(const guint addr)
987 {
988   int hash_idx;
989   hashipxnet_t *tp;
990   ipxnet_t *ipxnet;
991
992   hash_idx = HASH_IPX_NET(addr);
993
994   tp = ipxnet_table[hash_idx];
995
996   if( tp == NULL ) {
997     tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
998   } else {  
999     while(1) {
1000       if (tp->addr == addr) {
1001         return tp->name;
1002       }
1003       if (tp->next == NULL) {
1004         tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1005         tp = tp->next;
1006         break;
1007       }
1008       tp = tp->next;
1009     }
1010   }
1011   
1012   /* fill in a new entry */
1013
1014   tp->addr = addr;
1015   tp->next = NULL;
1016
1017   if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1018     /* unknown name */
1019       sprintf(tp->name, "%X", addr);
1020
1021   } else {
1022     strncpy(tp->name, ipxnet->name, MAXNAMELEN);
1023     tp->name[MAXNAMELEN-1] = '\0';
1024   }
1025
1026   return (tp->name);
1027
1028 } /* ipxnet_name_lookup */
1029
1030 static guint ipxnet_addr_lookup(const guchar *name, gboolean *success)
1031 {
1032   ipxnet_t *ipxnet;
1033   hashipxnet_t *tp;
1034   hashipxnet_t **table = ipxnet_table;
1035   int i;
1036
1037   /* to be optimized (hash table from name to addr) */
1038   for (i = 0; i < HASHIPXNETSIZE; i++) {
1039     tp = table[i];
1040     while (tp) {
1041       if (strcmp(tp->name, name) == 0)
1042         return tp->addr;
1043       tp = tp->next;
1044     }
1045   }
1046
1047   /* not in hash table : performs a file lookup */
1048
1049   if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1050           *success = FALSE;
1051           return 0;
1052   }
1053
1054   /* add new entry in hash table */
1055
1056   tp = add_ipxnet_name(ipxnet->addr, name);
1057
1058   *success = TRUE;
1059   return tp->addr;
1060
1061 } /* ipxnet_addr_lookup */
1062
1063
1064 /* 
1065  *  External Functions
1066  */
1067
1068 extern guchar *get_hostname(guint addr) 
1069 {
1070   gboolean found;
1071
1072   if (!(g_resolv_flags & RESOLV_NETWORK))
1073     return ip_to_str((guint8 *)&addr);
1074
1075   return host_name_lookup(addr, &found);
1076 }
1077
1078 extern const guchar *get_hostname6(struct e_in6_addr *addr)
1079 {
1080   gboolean found;
1081
1082 #ifdef INET6
1083   if (!(g_resolv_flags & RESOLV_NETWORK))
1084     return ip6_to_str(addr);
1085 #ifdef SOLARIS8_INET6
1086   if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr*)addr) || IN6_IS_ADDR_MULTICAST((struct in6_addr*)addr))
1087 #else
1088   if (IN6_IS_ADDR_LINKLOCAL(addr) || IN6_IS_ADDR_MULTICAST(addr))
1089 #endif
1090     return ip6_to_str(addr);
1091 #endif
1092
1093   return host_name_lookup6(addr, &found);
1094 }
1095
1096 extern void add_host_name(guint addr, const guchar *name)
1097 {
1098   int hash_idx;
1099   hashname_t *tp;
1100
1101   hash_idx = HASH_IPV4_ADDRESS(addr);
1102
1103   tp = host_table[hash_idx];
1104
1105   if( tp == NULL ) {
1106     tp = host_table[hash_idx] = (hashname_t *)g_malloc(sizeof(hashname_t));
1107   } else {  
1108     while(1) {
1109       if (tp->addr == addr) {
1110         /* address already known */
1111         if (!tp->is_dummy_entry) {
1112           return;
1113         } else {
1114           /* replace this dummy entry with the new one */
1115           break;
1116         }
1117       }
1118       if (tp->next == NULL) {
1119         tp->next = (hashname_t *)g_malloc(sizeof(hashname_t));
1120         tp = tp->next;
1121         break;
1122       }
1123       tp = tp->next;
1124     }
1125   }
1126   
1127   strncpy(tp->name, name, MAXNAMELEN);
1128   tp->name[MAXNAMELEN-1] = '\0';
1129   tp->addr = addr;
1130   tp->next = NULL;
1131   tp->is_dummy_entry = FALSE;
1132
1133 } /* add_host_name */
1134
1135 extern guchar *get_udp_port(guint port)
1136 {
1137   static gchar  str[3][MAXNAMELEN];
1138   static gchar *cur;
1139
1140   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1141     if (cur == &str[0][0]) {
1142       cur = &str[1][0];
1143     } else if (cur == &str[1][0]) {  
1144       cur = &str[2][0];
1145     } else {  
1146       cur = &str[0][0];
1147     }
1148     sprintf(cur, "%u", port);
1149     return cur;
1150   }
1151
1152   return serv_name_lookup(port, PT_UDP);
1153
1154 } /* get_udp_port */
1155
1156 extern guchar *get_tcp_port(guint port) 
1157 {
1158   static gchar  str[3][MAXNAMELEN];
1159   static gchar *cur;
1160
1161   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1162     if (cur == &str[0][0]) {
1163       cur = &str[1][0];
1164     } else if (cur == &str[1][0]) {  
1165       cur = &str[2][0];
1166     } else {  
1167       cur = &str[0][0];
1168     }
1169     sprintf(cur, "%u", port);
1170     return cur;
1171   }
1172
1173   return serv_name_lookup(port, PT_TCP);
1174
1175 } /* get_tcp_port */
1176
1177 extern guchar *get_sctp_port(guint port) 
1178 {
1179   static gchar  str[3][MAXNAMELEN];
1180   static gchar *cur;
1181
1182   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
1183     if (cur == &str[0][0]) {
1184       cur = &str[1][0];
1185     } else if (cur == &str[1][0]) {  
1186       cur = &str[2][0];
1187     } else {  
1188       cur = &str[0][0];
1189     }
1190     sprintf(cur, "%u", port);
1191     return cur;
1192   }
1193
1194   return serv_name_lookup(port, PT_SCTP);
1195
1196 } /* get_sctp_port */
1197
1198 extern guchar *get_ether_name(const guint8 *addr)
1199 {
1200   if (!(g_resolv_flags & RESOLV_MAC))
1201     return ether_to_str((guint8 *)addr);
1202
1203   if (!eth_resolution_initialized) {
1204     initialize_ethers();
1205     eth_resolution_initialized = 1;
1206   }
1207
1208   return eth_name_lookup(addr);
1209
1210 } /* get_ether_name */
1211
1212 /* Look for an ether name in the hash, and return it if found.
1213  * If it's not found, simply return NULL. We DO NOT make a new
1214  * hash entry for it with the hex digits turned into a string.
1215  */
1216 guchar *get_ether_name_if_known(const guint8 *addr)
1217 {
1218   int hash_idx;
1219   hashether_t *tp;
1220
1221   /* Initialize ether structs if we're the first
1222    * ether-related function called */
1223   if (!(g_resolv_flags & RESOLV_MAC))
1224     return NULL;
1225   
1226   if (!eth_resolution_initialized) {
1227     initialize_ethers();
1228     eth_resolution_initialized = 1;
1229   }
1230
1231   hash_idx = HASH_ETH_ADDRESS(addr);
1232
1233   tp = eth_table[hash_idx];
1234
1235   if( tp == NULL ) {
1236           /* Hash key not found in table.
1237            * Force a lookup (and a hash entry) for addr, then call
1238            * myself. I plan on not getting into an infinite loop because
1239            * eth_name_lookup() is guaranteed to make a hashtable entry,
1240            * so when I call myself again, I can never get into this
1241            * block of code again. Knock on wood...
1242            */
1243           (void) eth_name_lookup(addr);
1244           return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1245   }
1246   else { 
1247     while(1) {
1248       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1249               if (!tp->is_dummy_entry) {
1250                 /* A name was found, and its origin is an ethers file */
1251                 return tp->name;
1252               }
1253               else {
1254                 /* A name was found, but it was created, not found in a file */
1255                 return NULL;
1256               }
1257       }
1258       if (tp->next == NULL) {
1259           /* Read my reason above for why I'm sure I can't get into an infinite loop */
1260           (void) eth_name_lookup(addr);
1261           return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
1262       }
1263       tp = tp->next;
1264     }
1265   }
1266   g_assert_not_reached();
1267   return NULL;
1268 }
1269
1270
1271 extern guint8 *get_ether_addr(const guchar *name)
1272 {
1273
1274   /* force resolution (do not check g_resolv_flags) */
1275
1276   if (!eth_resolution_initialized) {
1277     initialize_ethers();
1278     eth_resolution_initialized = 1;
1279   }
1280
1281   return eth_addr_lookup(name);
1282
1283 } /* get_ether_addr */
1284
1285 extern void add_ether_byip(guint ip, const guint8 *eth)
1286 {
1287
1288   guchar *host;
1289   gboolean found;
1290
1291   /* first check that IP address can be resolved */
1292
1293   if ((host = host_name_lookup(ip, &found)) == NULL)
1294     return;
1295   
1296   /* ok, we can add this entry in the ethers hashtable */
1297
1298   if (found)
1299     add_eth_name(eth, host);
1300
1301 } /* add_ether_byip */
1302
1303 extern const guchar *get_ipxnet_name(const guint32 addr)
1304 {
1305
1306   if (!(g_resolv_flags & RESOLV_NETWORK)) {
1307           return ipxnet_to_str_punct(addr, '\0');
1308   }
1309
1310   if (!ipxnet_resolution_initialized) {
1311     initialize_ipxnets();
1312     ipxnet_resolution_initialized = 1;
1313   }
1314
1315   return ipxnet_name_lookup(addr);
1316
1317 } /* get_ipxnet_name */
1318
1319 extern guint32 get_ipxnet_addr(const guchar *name, gboolean *known)
1320 {
1321   guint32 addr;
1322   gboolean success;
1323
1324   /* force resolution (do not check g_resolv_flags) */
1325
1326   if (!ipxnet_resolution_initialized) {
1327     initialize_ipxnets();
1328     ipxnet_resolution_initialized = 1;
1329   }
1330
1331   addr =  ipxnet_addr_lookup(name, &success);
1332
1333   *known = success;
1334   return addr;
1335
1336 } /* get_ipxnet_addr */
1337
1338 extern const guchar *get_manuf_name(const guint8 *addr)
1339 {
1340   static gchar  str[3][MAXMANUFLEN];
1341   static gchar *cur;
1342   hashmanuf_t  *manufp;
1343
1344   if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
1345     initialize_ethers();
1346     eth_resolution_initialized = 1;
1347   }
1348
1349   if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
1350     if (cur == &str[0][0]) {
1351       cur = &str[1][0];
1352     } else if (cur == &str[1][0]) {  
1353       cur = &str[2][0];
1354     } else {  
1355       cur = &str[0][0];
1356     }
1357     sprintf(cur, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
1358     return cur;
1359   }
1360   
1361   return manufp->name;
1362
1363 } /* get_manuf_name */
1364
1365
1366
1367 /* Translate a string, assumed either to be a dotted-quad IP address or
1368  * a host name, to a numeric IP address.  Return TRUE if we succeed and
1369  * set "*addrp" to that numeric IP address; return FALSE if we fail.
1370  * Used more in the dfilter parser rather than in packet dissectors */
1371 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
1372 {
1373         struct in_addr          ipaddr;
1374         struct hostent          *hp;
1375
1376         /*
1377          * don't change it to inet_pton(AF_INET), they are not 100% compatible.
1378          * inet_pton(AF_INET) does not support hexadecimal notation nor
1379          * less-than-4 octet notation.
1380          */
1381         if (!inet_aton(host, &ipaddr)) {
1382                 /* It's not a valid dotted-quad IP address; is it a valid
1383                  * host name? */
1384                 hp = gethostbyname(host);
1385                 if (hp == NULL) {
1386                         /* No. */
1387                         return FALSE;
1388                         /* Apparently, some versions of gethostbyaddr can
1389                          * return IPv6 addresses. */
1390                 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
1391                         memcpy(&ipaddr, hp->h_addr, hp->h_length);
1392                 } else {
1393                         return FALSE;
1394                 }
1395         } else {
1396                 /* Does the string really contain dotted-quad IP?
1397                  * Check against inet_atons that accept strings such as
1398                  * "130.230" as valid addresses and try to convert them
1399                  * to some form of a classful (host.net) notation.
1400                  */
1401                 unsigned int a0, a1, a2, a3;
1402                 if (sscanf(host, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1403                         return FALSE;
1404         }
1405
1406         *addrp = g_ntohl(ipaddr.s_addr);
1407         return TRUE;
1408 }
1409
1410 /*
1411  * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
1412  * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
1413  * return FALSE if we fail.
1414  */
1415 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
1416 {
1417         struct hostent *hp;
1418
1419         if (inet_pton(AF_INET6, host, addrp) == 1)
1420                 return TRUE;
1421
1422         /* try FQDN */
1423 #ifdef HAVE_GETHOSTBYNAME2
1424         hp = gethostbyname2(host, AF_INET6);
1425 #else
1426         hp = NULL;
1427 #endif
1428         if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
1429                 memcpy(addrp, hp->h_addr, hp->h_length);
1430                 return TRUE;
1431         }
1432
1433         return FALSE;
1434 }