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