1fc7c45fc6cb0210d4245579343c4cfaed9abeeb
[obnox/wireshark/wip.git] / epan / addr_resolv.c
1 /* addr_resolv.c
2  * Routines for network object lookup
3  *
4  * $Id$
5  *
6  * Laurent Deniel <laurent.deniel@free.fr>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
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 <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36
37 /*
38  * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
39  * are most likely to take a long time, given the way address-to-name
40  * lookups are done over NBNS).
41  *
42  * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
43  * call in a signal handler, you might crash, because the state of the
44  * resolution code that sends messages to lookupd might be inconsistent
45  * if you jump out of it in middle of a call.
46  *
47  * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
48  * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
49  * call (and possibly in other gethostbyaddr() calls), because there's
50  * a mutex lock held in gethostbyaddr() and it doesn't get released
51  * if we longjmp out of it.
52  *
53  * There's no guarantee that longjmp()ing out of name resolution calls
54  * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
55  * code in tcpdump, to avoid those sorts of problems, and that was
56  * picked up by tcpdump.org tcpdump.
57  *
58  * So, for now, we do not define AVOID_DNS_TIMEOUT.  If we get a
59  * significantly more complaints about lookups taking a long time,
60  * we can reconsider that decision.  (Note that tcpdump originally
61  * added that for the benefit of systems using NIS to look up host
62  * names; that might now be fixed in NIS implementations, for those
63  * sites still using NIS rather than DNS for that....)
64  */
65
66 #ifdef HAVE_GNU_ADNS
67 # include <errno.h>
68 # include <adns.h>
69 # ifdef inet_aton
70 #  undef inet_aton
71 # endif
72 #endif
73
74 #ifdef HAVE_UNISTD_H
75 #include <unistd.h>
76 #endif
77
78 #ifdef HAVE_NETINET_IN_H
79 # include <netinet/in.h>
80 #endif
81
82 #ifdef HAVE_NETDB_H
83 #include <netdb.h>
84 #endif
85
86 #ifdef HAVE_ARPA_INET_H
87 #include <arpa/inet.h>
88 #endif
89
90 #include <signal.h>
91
92 #ifdef HAVE_SYS_SOCKET_H
93 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
94 #endif
95
96 #ifdef HAVE_WINSOCK2_H
97 #include <winsock2.h>           /* needed to define AF_ values on Windows */
98 #endif
99
100 #ifdef AVOID_DNS_TIMEOUT
101 # include <setjmp.h>
102 #endif
103
104 #ifdef NEED_INET_ATON_H
105 # include "inet_aton.h"
106 #endif
107
108 #ifdef NEED_INET_V6DEFS_H
109 # include "inet_v6defs.h"
110 #endif
111
112 #if defined(_WIN32) && defined(INET6)
113 # include <ws2tcpip.h>
114 #endif
115
116 #include <glib.h>
117
118 #include "report_err.h"
119 #include "packet.h"
120 #include "ipv6-utils.h"
121 #include "addr_resolv.h"
122 #include "filesystem.h"
123
124 #include <epan/strutil.h>
125 #include <wiretap/file_util.h>
126 #include <epan/prefs.h>
127 #include <epan/emem.h>
128
129 #define ENAME_HOSTS             "hosts"
130 #define ENAME_ETHERS            "ethers"
131 #define ENAME_IPXNETS           "ipxnets"
132 #define ENAME_MANUF             "manuf"
133 #define ENAME_SERVICES  "services"
134
135 #define MAXMANUFLEN     9       /* max vendor name length with ending '\0' */
136 #define HASHETHSIZE     1024
137 #define HASHHOSTSIZE    1024
138 #define HASHIPXNETSIZE  256
139 #define HASHMANUFSIZE   256
140 #define HASHPORTSIZE    256
141
142 /* hash table used for IPv4 lookup */
143
144 #define HASH_IPV4_ADDRESS(addr) ((addr) & (HASHHOSTSIZE - 1))
145
146 typedef struct hashipv4 {
147   guint                 addr;
148   gboolean              is_dummy_entry; /* name is IPv4 address in dot format */
149   struct hashipv4       *next;
150   gchar                 name[MAXNAMELEN];
151 } hashipv4_t;
152
153 /* hash table used for IPv6 lookup */
154
155 #define HASH_IPV6_ADDRESS(addr) \
156         ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
157
158 typedef struct hashipv6 {
159   struct e_in6_addr     addr;
160   gchar                 name[MAXNAMELEN];
161   gboolean              is_dummy_entry; /* name is IPv6 address in colon format */
162   struct hashipv6       *next;
163 } hashipv6_t;
164
165 /* hash table used for TCP/UDP/SCTP port lookup */
166
167 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
168
169 typedef struct hashport {
170   guint16               port;
171   gchar                 name[MAXNAMELEN];
172   struct hashport       *next;
173 } hashport_t;
174
175 /* hash table used for IPX network lookup */
176
177 /* XXX - check goodness of hash function */
178
179 #define HASH_IPX_NET(net)       ((net) & (HASHIPXNETSIZE - 1))
180
181 typedef struct hashipxnet {
182   guint                 addr;
183   gchar                 name[MAXNAMELEN];
184   struct hashipxnet     *next;
185 } hashipxnet_t;
186
187 /* hash tables used for ethernet and manufacturer lookup */
188
189 #define HASH_ETH_ADDRESS(addr) \
190         (((((addr)[2] << 8) | (addr)[3]) ^ (((addr)[4] << 8) | (addr)[5])) & \
191          (HASHETHSIZE - 1))
192
193 #define HASH_ETH_MANUF(addr) (((int)(addr)[2]) & (HASHMANUFSIZE - 1))
194
195 typedef struct hashmanuf {
196   guint8                addr[3];
197   char                  name[MAXMANUFLEN];
198   struct hashmanuf      *next;
199 } hashmanuf_t;
200
201 typedef struct hashether {
202   guint8                addr[6];
203   char                  name[MAXNAMELEN];
204   gboolean              is_dummy_entry;         /* not a complete entry */
205   struct hashether      *next;
206 } hashether_t;
207
208 /* internal ethernet type */
209
210 typedef struct _ether
211 {
212   guint8                addr[6];
213   char                  name[MAXNAMELEN];
214 } ether_t;
215
216 /* internal ipxnet type */
217
218 typedef struct _ipxnet
219 {
220   guint                 addr;
221   char                  name[MAXNAMELEN];
222 } ipxnet_t;
223
224 static hashipv4_t       *ipv4_table[HASHHOSTSIZE];
225 static hashipv6_t       *ipv6_table[HASHHOSTSIZE];
226 static hashport_t       *udp_port_table[HASHPORTSIZE];
227 static hashport_t       *tcp_port_table[HASHPORTSIZE];
228 static hashport_t       *sctp_port_table[HASHPORTSIZE];
229 static hashport_t       *dccp_port_table[HASHPORTSIZE];
230 static hashether_t      *eth_table[HASHETHSIZE];
231 static hashmanuf_t      *manuf_table[HASHMANUFSIZE];
232 static hashether_t      *(*wka_table[48])[HASHETHSIZE];
233 static hashipxnet_t     *ipxnet_table[HASHIPXNETSIZE];
234
235 static int              eth_resolution_initialized = 0;
236 static int              ipxnet_resolution_initialized = 0;
237 static int              service_resolution_initialized = 0;
238
239 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
240
241 /*
242  * Flag controlling what names to resolve.
243  */
244 guint32 g_resolv_flags;
245
246 /*
247  *  Global variables (can be changed in GUI sections)
248  *  XXX - they could be changed in GUI code, but there's currently no
249  *  GUI code to change them.
250  */
251
252 gchar *g_ethers_path  = NULL;           /* global ethers file    */
253 gchar *g_pethers_path = NULL;           /* personal ethers file  */
254 gchar *g_ipxnets_path  = NULL;          /* global ipxnets file   */
255 gchar *g_pipxnets_path = NULL;          /* personal ipxnets file */
256 gchar *g_services_path  = NULL;         /* global services file   */
257 gchar *g_pservices_path = NULL;         /* personal services file */
258                                         /* first resolving call  */
259
260 /* GNU ADNS */
261
262 #ifdef HAVE_GNU_ADNS
263
264 static gboolean gnu_adns_initialized = FALSE;
265
266 adns_state ads;
267
268 int adns_currently_queued = 0;
269
270 typedef struct _adns_queue_msg
271 {
272   gboolean          submitted;
273   guint32           ip4_addr;
274   struct e_in6_addr ip6_addr;
275   int               type;
276   adns_query        query;
277 } adns_queue_msg_t;
278
279 GList *adns_queue_head = NULL;
280
281 #endif /* HAVE_GNU_ADNS */
282
283
284 /*
285  *  Miscellaneous functions
286  */
287
288 static int fgetline(char **buf, int *size, FILE *fp)
289 {
290   int len;
291   int c;
292
293   if (fp == NULL)
294     return -1;
295
296   if (*buf == NULL) {
297     if (*size == 0)
298       *size = BUFSIZ;
299
300     if ((*buf = g_malloc(*size)) == NULL)
301       return -1;
302   }
303
304   if (feof(fp))
305     return -1;
306
307   len = 0;
308   while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
309     if (len+1 >= *size) {
310       if ((*buf = g_realloc(*buf, *size += BUFSIZ)) == NULL)
311         return -1;
312     }
313     (*buf)[len++] = c;
314   }
315
316   if (len == 0 && c == EOF)
317     return -1;
318
319   (*buf)[len] = '\0';
320
321   return len;
322
323 } /* fgetline */
324
325
326 /*
327  *  Local function definitions
328  */
329
330
331 static void add_service_name(hashport_t **proto_table, guint port, const char *service_name)
332 {
333   int hash_idx;
334   hashport_t *tp;
335   
336
337   hash_idx = HASH_PORT(port);
338   tp = proto_table[hash_idx];
339
340   if( tp == NULL ) {
341     tp = proto_table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
342   } else {
343     while(1) {
344       if( tp->port == port ) {
345         return;
346       }
347       if (tp->next == NULL) {
348         tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
349         tp = tp->next;
350         break;
351       }
352       tp = tp->next;
353     }
354   }
355
356   /* fill in a new entry */
357   tp->port = port;
358   tp->next = NULL;
359
360   g_strlcpy(tp->name, service_name, MAXNAMELEN);
361 }
362
363
364 static void parse_service_line (char *line)
365 {
366   /*
367    *  See the services(4) or services(5) man page for services file format
368    *  (not available on all systems).
369    */
370
371   gchar *cp;
372   gchar *service;
373   gchar *port;
374
375
376   if ((cp = strchr(line, '#')))
377     *cp = '\0';
378
379   if ((cp = strtok(line, " \t")) == NULL)
380     return;
381
382   service = cp;
383
384   if ((cp = strtok(NULL, " \t")) == NULL)
385     return;
386
387   port = cp;
388
389   if ((cp = strtok(cp, "/")) == NULL)
390     return;
391
392   if ((cp = strtok(NULL, "/")) == NULL)
393     return;
394
395   /* seems we got all interesting things from the file */
396   if(strcmp(cp, "tcp") == 0) {
397     add_service_name(tcp_port_table, atoi(port), service);
398     return;
399   }
400
401   if(strcmp(cp, "udp") == 0) {
402     add_service_name(udp_port_table, atoi(port), service);
403     return;
404   }
405
406   if(strcmp(cp, "sctp") == 0) {
407     add_service_name(sctp_port_table, atoi(port), service);
408     return;
409   }
410
411   if(strcmp(cp, "dcp") == 0) {
412     add_service_name(dccp_port_table, atoi(port), service);
413     return;
414   }
415
416 } /* parse_service_line */
417
418
419
420 static void parse_services_file(const char * path)
421 {
422   FILE *serv_p;
423   static int     size = 0;
424   static char   *buf = NULL;
425
426   /* services hash table initialization */
427   serv_p = eth_fopen(path, "r");
428
429   if (serv_p == NULL)
430     return;
431
432   while (fgetline(&buf, &size, serv_p) >= 0) {
433     parse_service_line (buf);
434   }
435
436   fclose(serv_p);
437 }
438
439
440 static void initialize_services(void)
441 {
442
443   /* the hash table won't ignore duplicates, so use the personal path first */
444
445   /* set personal services path */
446   if (g_pservices_path == NULL)
447     g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
448
449   parse_services_file(g_pservices_path);
450
451   /* Compute the pathname of the services file. */
452   if (g_services_path == NULL) {
453     g_services_path = get_datafile_path(ENAME_SERVICES);
454   }
455
456   parse_services_file(g_services_path);
457
458 } /* initialize_services */
459
460
461
462 static gchar *serv_name_lookup(guint port, port_type proto)
463 {
464   int hash_idx;
465   hashport_t *tp;
466   hashport_t **table;
467   const char *serv_proto = NULL;
468   struct servent *servp;
469
470
471   if (!service_resolution_initialized) {
472     initialize_services();
473     service_resolution_initialized = 1;
474   }
475
476   switch(proto) {
477   case PT_UDP:
478     table = udp_port_table;
479     serv_proto = "udp";
480     break;
481   case PT_TCP:
482     table = tcp_port_table;
483     serv_proto = "tcp";
484     break;
485   case PT_SCTP:
486     table = sctp_port_table;
487     serv_proto = "sctp";
488     break;
489   case PT_DCCP:
490     table = dccp_port_table;
491     serv_proto = "dcp";
492     break;
493   default:
494     /* not yet implemented */
495     return NULL;
496     /*NOTREACHED*/
497   } /* proto */
498
499   hash_idx = HASH_PORT(port);
500   tp = table[hash_idx];
501
502   if( tp == NULL ) {
503     tp = table[hash_idx] = (hashport_t *)g_malloc(sizeof(hashport_t));
504   } else {
505     while(1) {
506       if( tp->port == port ) {
507         return tp->name;
508       }
509       if (tp->next == NULL) {
510         tp->next = (hashport_t *)g_malloc(sizeof(hashport_t));
511         tp = tp->next;
512         break;
513       }
514       tp = tp->next;
515     }
516   }
517
518   /* fill in a new entry */
519   tp->port = port;
520   tp->next = NULL;
521
522   if (!(g_resolv_flags & RESOLV_TRANSPORT) ||
523       (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
524     /* unknown port */
525     g_snprintf(tp->name, MAXNAMELEN, "%d", port);
526   } else {
527     g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
528   }
529
530   return (tp->name);
531
532 } /* serv_name_lookup */
533
534
535 #ifdef AVOID_DNS_TIMEOUT
536
537 #define DNS_TIMEOUT     2       /* max sec per call */
538
539 jmp_buf hostname_env;
540
541 static void abort_network_query(int sig _U_)
542 {
543   longjmp(hostname_env, 1);
544 }
545 #endif /* AVOID_DNS_TIMEOUT */
546
547 static gchar *host_name_lookup(guint addr, gboolean *found)
548 {
549   int hash_idx;
550   hashipv4_t * volatile tp;
551   struct hostent *hostp;
552 #ifdef HAVE_GNU_ADNS
553   adns_queue_msg_t *qmsg;
554 #endif
555
556   *found = TRUE;
557
558   hash_idx = HASH_IPV4_ADDRESS(addr);
559
560   tp = ipv4_table[hash_idx];
561
562   if( tp == NULL ) {
563     tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
564   } else {
565     while(1) {
566       if( tp->addr == addr ) {
567         if (tp->is_dummy_entry)
568           *found = FALSE;
569         return tp->name;
570       }
571       if (tp->next == NULL) {
572         tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
573         tp = tp->next;
574         break;
575       }
576       tp = tp->next;
577     }
578   }
579
580   /* fill in a new entry */
581   tp->addr = addr;
582   tp->next = NULL;
583
584 #ifdef HAVE_GNU_ADNS
585   if ((g_resolv_flags & RESOLV_CONCURRENT) &&
586       prefs.name_resolve_concurrency > 0 &&
587       gnu_adns_initialized) {
588     qmsg = g_malloc(sizeof(adns_queue_msg_t));
589     qmsg->type = AF_INET;
590     qmsg->ip4_addr = addr;
591     qmsg->submitted = FALSE;
592     adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
593
594     tp->is_dummy_entry = TRUE;
595     ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
596     return tp->name;
597   }
598 #endif /* HAVE_GNU_ADNS */
599
600   /*
601    * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
602    * the name of the host on which it's running; to work around that
603    * botch, we don't try to translate an all-zero IP address to a host
604    * name.
605    */
606   if (addr != 0 && (g_resolv_flags & RESOLV_NETWORK)) {
607   /* Use async DNS if possible, else fall back to timeouts,
608    * else call gethostbyaddr and hope for the best
609    */
610
611 # ifdef AVOID_DNS_TIMEOUT
612
613     /* Quick hack to avoid DNS/YP timeout */
614
615     if (!setjmp(hostname_env)) {
616       signal(SIGALRM, abort_network_query);
617       alarm(DNS_TIMEOUT);
618 # endif /* AVOID_DNS_TIMEOUT */
619
620       hostp = gethostbyaddr((char *)&addr, 4, AF_INET);
621
622 # ifdef AVOID_DNS_TIMEOUT
623       alarm(0);
624 # endif /* AVOID_DNS_TIMEOUT */
625
626       if (hostp != NULL) {
627         g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
628         tp->is_dummy_entry = FALSE;
629         return tp->name;
630       }
631
632 # ifdef AVOID_DNS_TIMEOUT
633     }
634 # endif /* AVOID_DNS_TIMEOUT */
635
636   }
637
638   /* unknown host or DNS timeout */
639
640   ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
641   tp->is_dummy_entry = TRUE;
642   *found = FALSE;
643
644   return (tp->name);
645
646 } /* host_name_lookup */
647
648 static gchar *host_name_lookup6(struct e_in6_addr *addr, gboolean *found)
649 {
650   int hash_idx;
651   hashipv6_t * volatile tp;
652 #ifdef INET6
653   struct hostent *hostp;
654 #endif
655
656   *found = TRUE;
657
658   hash_idx = HASH_IPV6_ADDRESS(*addr);
659
660   tp = ipv6_table[hash_idx];
661
662   if( tp == NULL ) {
663     tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
664   } else {
665     while(1) {
666       if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
667         if (tp->is_dummy_entry)
668           *found = FALSE;
669         return tp->name;
670       }
671       if (tp->next == NULL) {
672         tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
673         tp = tp->next;
674         break;
675       }
676       tp = tp->next;
677     }
678   }
679
680   /* fill in a new entry */
681   tp->addr = *addr;
682   tp->next = NULL;
683
684 #ifdef INET6
685   if (g_resolv_flags & RESOLV_NETWORK) {
686 #ifdef AVOID_DNS_TIMEOUT
687
688     /* Quick hack to avoid DNS/YP timeout */
689
690     if (!setjmp(hostname_env)) {
691       signal(SIGALRM, abort_network_query);
692       alarm(DNS_TIMEOUT);
693 #endif /* AVOID_DNS_TIMEOUT */
694       hostp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET6);
695 #ifdef AVOID_DNS_TIMEOUT
696       alarm(0);
697 # endif /* AVOID_DNS_TIMEOUT */
698
699       if (hostp != NULL) {
700         g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
701         tp->is_dummy_entry = FALSE;
702         return tp->name;
703       }
704
705 #ifdef AVOID_DNS_TIMEOUT
706     }
707 # endif /* AVOID_DNS_TIMEOUT */
708
709   }
710
711   /* unknown host or DNS timeout */
712 #endif /* INET6 */
713   ip6_to_str_buf(addr, tp->name);
714   tp->is_dummy_entry = TRUE;
715   *found = FALSE;
716   return (tp->name);
717
718 } /* host_name_lookup6 */
719
720 static const gchar *solve_address_to_name(address *addr)
721 {
722   guint32 ipv4_addr;
723   struct e_in6_addr ipv6_addr;
724
725   switch (addr->type) {
726
727   case AT_ETHER:
728     return get_ether_name(addr->data);
729
730   case AT_IPv4:
731     memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
732     return get_hostname(ipv4_addr);
733
734   case AT_IPv6:
735     memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
736     return get_hostname6(&ipv6_addr);
737
738   case AT_STRINGZ:
739     return addr->data;
740
741   default:
742     return NULL;
743   }
744 } /* solve_address_to_name */
745
746
747 /*
748  * Ethernet / manufacturer resolution
749  *
750  * The following functions implement ethernet address resolution and
751  * ethers files parsing (see ethers(4)).
752  *
753  * The manuf file has the same format as ethers(4) except that names are
754  * truncated to MAXMANUFLEN-1 characters and that an address contains
755  * only 3 bytes (instead of 6).
756  *
757  * Notes:
758  *
759  * I decide to not use the existing functions (see ethers(3) on some
760  * operating systems) for the following reasons:
761  * - performance gains (use of hash tables and some other enhancements),
762  * - use of two ethers files (system-wide and per user),
763  * - avoid the use of NIS maps,
764  * - lack of these functions on some systems.
765  *
766  * So the following functions do _not_ behave as the standard ones.
767  *
768  * -- Laurent.
769  */
770
771
772 /*
773  * If "manuf_file" is FALSE, parse a 6-byte MAC address.
774  * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
775  * mask.
776  */
777 static gboolean
778 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
779                     gboolean manuf_file)
780 {
781   int i;
782   unsigned long num;
783   char *p;
784   char sep = '\0';
785
786   for (i = 0; i < 6; i++) {
787     /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
788     if (!isxdigit((unsigned char)*cp))
789       return FALSE;
790     num = strtoul(cp, &p, 16);
791     if (p == cp)
792       return FALSE;     /* failed */
793     if (num > 0xFF)
794       return FALSE;     /* not a valid octet */
795     eth->addr[i] = (guint8) num;
796     cp = p;             /* skip past the number */
797
798     /* OK, what character terminated the octet? */
799     if (*cp == '/') {
800       /* "/" - this has a mask. */
801       if (!manuf_file) {
802         /* Entries with masks are allowed only in the "manuf" files. */
803         return FALSE;
804       }
805       cp++;     /* skip past the '/' to get to the mask */
806       if (!isdigit((unsigned char)*cp))
807         return FALSE;   /* no sign allowed */
808       num = strtoul(cp, &p, 10);
809       if (p == cp)
810         return FALSE;   /* failed */
811       cp = p;   /* skip past the number */
812       if (*cp != '\0' && !isspace((unsigned char)*cp))
813         return FALSE;   /* bogus terminator */
814       if (num == 0 || num >= 48)
815         return FALSE;   /* bogus mask */
816       /* Mask out the bits not covered by the mask */
817       *mask = num;
818       for (i = 0; num >= 8; i++, num -= 8)
819         ;       /* skip octets entirely covered by the mask */
820       /* Mask out the first masked octet */
821       eth->addr[i] &= (0xFF << (8 - num));
822       i++;
823       /* Mask out completely-masked-out octets */
824       for (; i < 6; i++)
825         eth->addr[i] = 0;
826       return TRUE;
827     }
828     if (*cp == '\0') {
829       /* We're at the end of the address, and there's no mask. */
830       if (i == 2) {
831         /* We got 3 bytes, so this is a manufacturer ID. */
832         if (!manuf_file) {
833           /* Manufacturer IDs are only allowed in the "manuf"
834              files. */
835           return FALSE;
836         }
837         /* Indicate that this is a manufacturer ID (0 is not allowed
838            as a mask). */
839         *mask = 0;
840         return TRUE;
841       }
842
843       if (i == 5) {
844         /* We got 6 bytes, so this is a MAC address.
845            If we're reading one of the "manuf" files, indicate that
846            this is a MAC address (48 is not allowed as a mask). */
847         if (manuf_file)
848           *mask = 48;
849         return TRUE;
850       }
851
852       /* We didn't get 3 or 6 bytes, and there's no mask; this is
853          illegal. */
854       return FALSE;
855     } else {
856       if (sep == '\0') {
857         /* We don't know the separator used in this number; it can either
858            be ':', '-', or '.'. */
859         if (*cp != ':' && *cp != '-' && *cp != '.')
860           return FALSE;
861         sep = *cp;      /* subsequent separators must be the same */
862       } else {
863           /* It has to be the same as the first separator */
864           if (*cp != sep)
865             return FALSE;
866       }
867     }
868     cp++;
869   }
870
871   return TRUE;
872 }
873
874 static int parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
875                             gboolean manuf_file)
876 {
877   /*
878    *  See the ethers(4) or ethers(5) man page for ethers file format
879    *  (not available on all systems).
880    *  We allow both ethernet address separators (':' and '-'),
881    *  as well as Wireshark's '.' separator.
882    */
883
884   gchar *cp;
885
886   if ((cp = strchr(line, '#')))
887     *cp = '\0';
888
889   if ((cp = strtok(line, " \t")) == NULL)
890     return -1;
891
892   if (!parse_ether_address(cp, eth, mask, manuf_file))
893     return -1;
894
895   if ((cp = strtok(NULL, " \t")) == NULL)
896     return -1;
897
898   g_strlcpy(eth->name, cp, MAXNAMELEN);
899
900   return 0;
901
902 } /* parse_ether_line */
903
904 static FILE *eth_p = NULL;
905
906 static void set_ethent(char *path)
907 {
908   if (eth_p)
909     rewind(eth_p);
910   else
911     eth_p = eth_fopen(path, "r");
912 }
913
914 static void end_ethent(void)
915 {
916   if (eth_p) {
917     fclose(eth_p);
918     eth_p = NULL;
919   }
920 }
921
922 static ether_t *get_ethent(unsigned int *mask, gboolean manuf_file)
923 {
924
925   static ether_t eth;
926   static int     size = 0;
927   static char   *buf = NULL;
928
929   if (eth_p == NULL)
930     return NULL;
931
932   while (fgetline(&buf, &size, eth_p) >= 0) {
933     if (parse_ether_line(buf, &eth, mask, manuf_file) == 0) {
934       return &eth;
935     }
936   }
937
938   return NULL;
939
940 } /* get_ethent */
941
942 static ether_t *get_ethbyname(const gchar *name)
943 {
944   ether_t *eth;
945
946   set_ethent(g_pethers_path);
947
948   while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
949     ;
950
951   if (eth == NULL) {
952     end_ethent();
953
954     set_ethent(g_ethers_path);
955
956     while ((eth = get_ethent(NULL, FALSE)) && strncmp(name, eth->name, MAXNAMELEN) != 0)
957       ;
958
959     end_ethent();
960   }
961
962   return eth;
963
964 } /* get_ethbyname */
965
966 static ether_t *get_ethbyaddr(const guint8 *addr)
967 {
968
969   ether_t *eth;
970
971   set_ethent(g_pethers_path);
972
973   while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
974     ;
975
976   if (eth == NULL) {
977     end_ethent();
978
979     set_ethent(g_ethers_path);
980
981     while ((eth = get_ethent(NULL, FALSE)) && memcmp(addr, eth->addr, 6) != 0)
982       ;
983
984     end_ethent();
985   }
986
987   return eth;
988
989 } /* get_ethbyaddr */
990
991 static int hash_eth_wka(const guint8 *addr, unsigned int mask)
992 {
993   if (mask <= 8) {
994     /* All but the topmost byte is masked out */
995     return (addr[0] & (0xFF << (8 - mask))) & (HASHETHSIZE - 1);
996   }
997   mask -= 8;
998   if (mask <= 8) {
999     /* All but the topmost 2 bytes are masked out */
1000     return ((addr[0] << 8) | (addr[1] & (0xFF << (8 - mask)))) &
1001             (HASHETHSIZE - 1);
1002   }
1003   mask -= 8;
1004   if (mask <= 8) {
1005     /* All but the topmost 3 bytes are masked out */
1006     return ((addr[0] << 16) | (addr[1] << 8) | (addr[2] & (0xFF << (8 - mask))))
1007      & (HASHETHSIZE - 1);
1008   }
1009   mask -= 8;
1010   if (mask <= 8) {
1011     /* All but the topmost 4 bytes are masked out */
1012     return ((((addr[0] << 8) | addr[1]) ^
1013              ((addr[2] << 8) | (addr[3] & (0xFF << (8 - mask)))))) &
1014          (HASHETHSIZE - 1);
1015   }
1016   mask -= 8;
1017   if (mask <= 8) {
1018     /* All but the topmost 5 bytes are masked out */
1019     return ((((addr[1] << 8) | addr[2]) ^
1020              ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1021          (HASHETHSIZE - 1);
1022   }
1023   mask -= 8;
1024   /* No bytes are fully masked out */
1025   return ((((addr[1] << 8) | addr[2]) ^
1026            ((addr[3] << 8) | (addr[4] & (0xFF << (8 - mask)))))) &
1027             (HASHETHSIZE - 1);
1028 }
1029
1030 static void add_manuf_name(guint8 *addr, unsigned int mask, gchar *name)
1031 {
1032   int hash_idx;
1033   hashmanuf_t *tp;
1034   hashether_t *(*wka_tp)[HASHETHSIZE], *etp;
1035
1036   if (mask == 48) {
1037     /* This is a well-known MAC address; just add this to the Ethernet
1038        hash table */
1039     add_eth_name(addr, name);
1040     return;
1041   }
1042
1043   if (mask == 0) {
1044     /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1045
1046     hash_idx = HASH_ETH_MANUF(addr);
1047
1048     tp = manuf_table[hash_idx];
1049
1050     if( tp == NULL ) {
1051       tp = manuf_table[hash_idx] = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1052     } else {
1053       while(1) {
1054         if (tp->next == NULL) {
1055           tp->next = (hashmanuf_t *)g_malloc(sizeof(hashmanuf_t));
1056           tp = tp->next;
1057           break;
1058         }
1059         tp = tp->next;
1060       }
1061     }
1062
1063     memcpy(tp->addr, addr, sizeof(tp->addr));
1064     g_strlcpy(tp->name, name, MAXMANUFLEN);
1065     tp->next = NULL;
1066     return;
1067   }
1068
1069   /* This is a range of well-known addresses; add it to the appropriate
1070      well-known-address table, creating that table if necessary. */
1071   wka_tp = wka_table[mask];
1072   if (wka_tp == NULL)
1073     wka_tp = wka_table[mask] = g_malloc0(sizeof *wka_table[mask]);
1074
1075   hash_idx = hash_eth_wka(addr, mask);
1076
1077   etp = (*wka_tp)[hash_idx];
1078
1079   if( etp == NULL ) {
1080     etp = (*wka_tp)[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1081   } else {
1082     while(1) {
1083       if (memcmp(etp->addr, addr, sizeof(etp->addr)) == 0) {
1084         /* address already known */
1085         return;
1086       }
1087       if (etp->next == NULL) {
1088         etp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1089         etp = etp->next;
1090         break;
1091       }
1092       etp = etp->next;
1093     }
1094   }
1095
1096   memcpy(etp->addr, addr, sizeof(etp->addr));
1097   g_strlcpy(etp->name, name, MAXNAMELEN);
1098   etp->next = NULL;
1099   etp->is_dummy_entry = FALSE;
1100
1101 } /* add_manuf_name */
1102
1103 static hashmanuf_t *manuf_name_lookup(const guint8 *addr)
1104 {
1105   int hash_idx;
1106   hashmanuf_t *tp;
1107   guint8 stripped_addr[3];
1108
1109   hash_idx = HASH_ETH_MANUF(addr);
1110
1111   /* first try to find a "perfect match" */
1112   tp = manuf_table[hash_idx];
1113   while(tp != NULL) {
1114     if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1115       return tp;
1116     }
1117     tp = tp->next;
1118   }
1119
1120   /* Mask out the broadcast/multicast flag but not the locally
1121    * administered flag as localy administered means: not assigend
1122    * by the IEEE but the local administrator instead.
1123    * 0x01 multicast / broadcast bit
1124    * 0x02 locally administered bit */
1125   memcpy(stripped_addr, addr, 3);
1126   stripped_addr[0] &= 0xFE;
1127
1128   tp = manuf_table[hash_idx];
1129   while(tp != NULL) {
1130     if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
1131       return tp;
1132     }
1133     tp = tp->next;
1134   }
1135
1136   return NULL;
1137
1138 } /* manuf_name_lookup */
1139
1140 static hashether_t *wka_name_lookup(const guint8 *addr, unsigned int mask)
1141 {
1142   int hash_idx;
1143   hashether_t *(*wka_tp)[HASHETHSIZE];
1144   hashether_t *tp;
1145   guint8 masked_addr[6];
1146   unsigned int num;
1147   int i;
1148
1149   wka_tp = wka_table[mask];
1150   if (wka_tp == NULL) {
1151     /* There are no entries in the table for that mask value, as there is
1152        no table for that mask value. */
1153     return NULL;
1154   }
1155
1156   /* Get the part of the address covered by the mask. */
1157   for (i = 0, num = mask; num >= 8; i++, num -= 8)
1158     masked_addr[i] = addr[i];   /* copy octets entirely covered by the mask */
1159   /* Mask out the first masked octet */
1160   masked_addr[i] = addr[i] & (0xFF << (8 - num));
1161   i++;
1162   /* Zero out completely-masked-out octets */
1163   for (; i < 6; i++)
1164     masked_addr[i] = 0;
1165
1166   hash_idx = hash_eth_wka(masked_addr, mask);
1167
1168   tp = (*wka_tp)[hash_idx];
1169
1170   while(tp != NULL) {
1171     if (memcmp(tp->addr, masked_addr, sizeof(tp->addr)) == 0) {
1172       return tp;
1173     }
1174     tp = tp->next;
1175   }
1176
1177   return NULL;
1178
1179 } /* wka_name_lookup */
1180
1181 static void initialize_ethers(void)
1182 {
1183   ether_t *eth;
1184   char *manuf_path;
1185   unsigned int mask;
1186
1187   /* Compute the pathname of the ethers file. */
1188   if (g_ethers_path == NULL) {
1189     g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1190             get_systemfile_dir(), ENAME_ETHERS);
1191   }
1192
1193   /* Set g_pethers_path here, but don't actually do anything
1194    * with it. It's used in get_ethbyname() and get_ethbyaddr()
1195    */
1196   if (g_pethers_path == NULL)
1197     g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
1198
1199   /* manuf hash table initialization */
1200
1201   /* Compute the pathname of the manuf file */
1202   manuf_path = get_datafile_path(ENAME_MANUF);
1203
1204   /* Read it and initialize the hash table */
1205   set_ethent(manuf_path);
1206
1207   while ((eth = get_ethent(&mask, TRUE))) {
1208     add_manuf_name(eth->addr, mask, eth->name);
1209   }
1210
1211   end_ethent();
1212
1213   g_free(manuf_path);
1214
1215 } /* initialize_ethers */
1216
1217 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name)
1218 {
1219   int hash_idx;
1220   hashether_t *tp;
1221   int new_one = TRUE;
1222
1223   hash_idx = HASH_ETH_ADDRESS(addr);
1224
1225   tp = eth_table[hash_idx];
1226
1227   if( tp == NULL ) {
1228     tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1229   } else {
1230     while(1) {
1231       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1232         /* address already known */
1233         if (!tp->is_dummy_entry) {
1234           return tp;
1235         } else {
1236           /* replace this dummy (manuf) entry with a real name */
1237           new_one = FALSE;
1238           break;
1239         }
1240       }
1241       if (tp->next == NULL) {
1242         tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1243         tp = tp->next;
1244         break;
1245       }
1246       tp = tp->next;
1247     }
1248   }
1249
1250   g_strlcpy(tp->name, name, MAXNAMELEN);
1251   if (new_one) {
1252       memcpy(tp->addr, addr, sizeof(tp->addr));
1253       tp->next = NULL;
1254   }
1255   tp->is_dummy_entry = FALSE;
1256
1257   return tp;
1258
1259 } /* add_eth_name */
1260
1261 static gchar *eth_name_lookup(const guint8 *addr)
1262 {
1263   int hash_idx;
1264   hashmanuf_t *manufp;
1265   hashether_t *tp;
1266   ether_t *eth;
1267   hashether_t *etp;
1268   unsigned int mask;
1269
1270   hash_idx = HASH_ETH_ADDRESS(addr);
1271
1272   tp = eth_table[hash_idx];
1273
1274   if( tp == NULL ) {
1275     tp = eth_table[hash_idx] = (hashether_t *)g_malloc(sizeof(hashether_t));
1276   } else {
1277     while(1) {
1278       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
1279         return tp->name;
1280       }
1281       if (tp->next == NULL) {
1282         tp->next = (hashether_t *)g_malloc(sizeof(hashether_t));
1283         tp = tp->next;
1284         break;
1285       }
1286       tp = tp->next;
1287     }
1288   }
1289
1290   /* fill in a new entry */
1291
1292   memcpy(tp->addr, addr, sizeof(tp->addr));
1293   tp->next = NULL;
1294
1295   if ( (eth = get_ethbyaddr(addr)) == NULL) {
1296     /* Unknown name.  Try looking for it in the well-known-address
1297        tables for well-known address ranges smaller than 2^24. */
1298     mask = 7;
1299     for (;;) {
1300       /* Only the topmost 5 bytes participate fully */
1301       if ((etp = wka_name_lookup(addr, mask+40)) != NULL) {
1302         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x",
1303               etp->name, addr[5] & (0xFF >> mask));
1304         tp->is_dummy_entry = TRUE;
1305         return (tp->name);
1306       }
1307       if (mask == 0)
1308         break;
1309       mask--;
1310     }
1311
1312     mask = 7;
1313     for (;;) {
1314       /* Only the topmost 4 bytes participate fully */
1315       if ((etp = wka_name_lookup(addr, mask+32)) != NULL) {
1316         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x",
1317               etp->name, addr[4] & (0xFF >> mask), addr[5]);
1318         tp->is_dummy_entry = TRUE;
1319         return (tp->name);
1320       }
1321       if (mask == 0)
1322         break;
1323       mask--;
1324     }
1325
1326     mask = 7;
1327     for (;;) {
1328       /* Only the topmost 3 bytes participate fully */
1329       if ((etp = wka_name_lookup(addr, mask+24)) != NULL) {
1330         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1331               etp->name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1332         tp->is_dummy_entry = TRUE;
1333         return (tp->name);
1334       }
1335       if (mask == 0)
1336         break;
1337       mask--;
1338     }
1339
1340     /* Now try looking in the manufacturer table. */
1341     if ((manufp = manuf_name_lookup(addr)) != NULL) {
1342       g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1343               manufp->name, addr[3], addr[4], addr[5]);
1344       tp->is_dummy_entry = TRUE;
1345       return (tp->name);
1346     }
1347
1348     /* Now try looking for it in the well-known-address
1349        tables for well-known address ranges larger than 2^24. */
1350     mask = 7;
1351     for (;;) {
1352       /* Only the topmost 2 bytes participate fully */
1353       if ((etp = wka_name_lookup(addr, mask+16)) != NULL) {
1354         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1355               etp->name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1356               addr[5]);
1357         tp->is_dummy_entry = TRUE;
1358         return (tp->name);
1359       }
1360       if (mask == 0)
1361         break;
1362       mask--;
1363     }
1364
1365     mask = 7;
1366     for (;;) {
1367       /* Only the topmost byte participates fully */
1368       if ((etp = wka_name_lookup(addr, mask+8)) != NULL) {
1369         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1370               etp->name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1371               addr[4], addr[5]);
1372         tp->is_dummy_entry = TRUE;
1373         return (tp->name);
1374       }
1375       if (mask == 0)
1376         break;
1377       mask--;
1378     }
1379
1380     for (mask = 7; mask > 0; mask--) {
1381       /* Not even the topmost byte participates fully */
1382       if ((etp = wka_name_lookup(addr, mask)) != NULL) {
1383         g_snprintf(tp->name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1384               etp->name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1385               addr[3], addr[4], addr[5]);
1386         tp->is_dummy_entry = TRUE;
1387         return (tp->name);
1388       }
1389     }
1390
1391     /* No match whatsoever. */
1392     g_snprintf(tp->name, MAXNAMELEN, "%s", ether_to_str(addr));
1393     tp->is_dummy_entry = TRUE;
1394
1395   } else {
1396     g_strlcpy(tp->name, eth->name, MAXNAMELEN);
1397     tp->is_dummy_entry = FALSE;
1398   }
1399
1400   return (tp->name);
1401
1402 } /* eth_name_lookup */
1403
1404 static guint8 *eth_addr_lookup(const gchar *name)
1405 {
1406   ether_t *eth;
1407   hashether_t *tp;
1408   hashether_t **table = eth_table;
1409   int i;
1410
1411   /* to be optimized (hash table from name to addr) */
1412   for (i = 0; i < HASHETHSIZE; i++) {
1413     tp = table[i];
1414     while (tp) {
1415       if (strcmp(tp->name, name) == 0)
1416         return tp->addr;
1417       tp = tp->next;
1418     }
1419   }
1420
1421   /* not in hash table : performs a file lookup */
1422
1423   if ((eth = get_ethbyname(name)) == NULL)
1424     return NULL;
1425
1426   /* add new entry in hash table */
1427
1428   tp = add_eth_name(eth->addr, name);
1429
1430   return tp->addr;
1431
1432 } /* eth_addr_lookup */
1433
1434
1435 /* IPXNETS */
1436 static int parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1437 {
1438   /*
1439    *  We allow three address separators (':', '-', and '.'),
1440    *  as well as no separators
1441    */
1442
1443   gchar         *cp;
1444   guint32       a, a0, a1, a2, a3;
1445   gboolean      found_single_number = FALSE;
1446
1447   if ((cp = strchr(line, '#')))
1448     *cp = '\0';
1449
1450   if ((cp = strtok(line, " \t\n")) == NULL)
1451     return -1;
1452
1453   /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1454    * fill a and found_single_number is TRUE,
1455    * or return -1
1456    */
1457   if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1458     if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1459       if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1460         if (sscanf(cp, "%x", &a) == 1) {
1461           found_single_number = TRUE;
1462         }
1463         else {
1464           return -1;
1465         }
1466       }
1467     }
1468   }
1469
1470   if ((cp = strtok(NULL, " \t\n")) == NULL)
1471     return -1;
1472
1473   if (found_single_number) {
1474         ipxnet->addr = a;
1475   }
1476   else {
1477         ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1478   }
1479
1480   g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1481
1482   return 0;
1483
1484 } /* parse_ipxnets_line */
1485
1486 static FILE *ipxnet_p = NULL;
1487
1488 static void set_ipxnetent(char *path)
1489 {
1490   if (ipxnet_p)
1491     rewind(ipxnet_p);
1492   else
1493     ipxnet_p = eth_fopen(path, "r");
1494 }
1495
1496 static void end_ipxnetent(void)
1497 {
1498   if (ipxnet_p) {
1499     fclose(ipxnet_p);
1500     ipxnet_p = NULL;
1501   }
1502 }
1503
1504 static ipxnet_t *get_ipxnetent(void)
1505 {
1506
1507   static ipxnet_t ipxnet;
1508   static int     size = 0;
1509   static char   *buf = NULL;
1510
1511   if (ipxnet_p == NULL)
1512     return NULL;
1513
1514   while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1515     if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1516       return &ipxnet;
1517     }
1518   }
1519
1520   return NULL;
1521
1522 } /* get_ipxnetent */
1523
1524 static ipxnet_t *get_ipxnetbyname(const gchar *name)
1525 {
1526   ipxnet_t *ipxnet;
1527
1528   set_ipxnetent(g_ipxnets_path);
1529
1530   while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1531     ;
1532
1533   if (ipxnet == NULL) {
1534     end_ipxnetent();
1535
1536     set_ipxnetent(g_pipxnets_path);
1537
1538     while ((ipxnet = get_ipxnetent()) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1539       ;
1540
1541     end_ipxnetent();
1542   }
1543
1544   return ipxnet;
1545
1546 } /* get_ipxnetbyname */
1547
1548 static ipxnet_t *get_ipxnetbyaddr(guint32 addr)
1549 {
1550
1551   ipxnet_t *ipxnet;
1552
1553   set_ipxnetent(g_ipxnets_path);
1554
1555   while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) ) ;
1556
1557   if (ipxnet == NULL) {
1558     end_ipxnetent();
1559
1560     set_ipxnetent(g_pipxnets_path);
1561
1562     while ((ipxnet = get_ipxnetent()) && (addr != ipxnet->addr) )
1563       ;
1564
1565     end_ipxnetent();
1566   }
1567
1568   return ipxnet;
1569
1570 } /* get_ipxnetbyaddr */
1571
1572 static void initialize_ipxnets(void)
1573 {
1574   /* Compute the pathname of the ipxnets file.
1575    *
1576    * XXX - is there a notion of an "ipxnets file" in any flavor of
1577    * UNIX, or with any add-on Netware package for UNIX?  If not,
1578    * should the UNIX version of the ipxnets file be in the datafile
1579    * directory as well?
1580    */
1581   if (g_ipxnets_path == NULL) {
1582         g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1583             get_systemfile_dir(), ENAME_IPXNETS);
1584   }
1585
1586   /* Set g_pipxnets_path here, but don't actually do anything
1587    * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1588    */
1589   if (g_pipxnets_path == NULL)
1590     g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
1591
1592 } /* initialize_ipxnets */
1593
1594 static hashipxnet_t *add_ipxnet_name(guint addr, const gchar *name)
1595 {
1596   int hash_idx;
1597   hashipxnet_t *tp;
1598
1599   hash_idx = HASH_IPX_NET(addr);
1600
1601   tp = ipxnet_table[hash_idx];
1602
1603   if( tp == NULL ) {
1604     tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1605   } else {
1606     while(1) {
1607       if (tp->next == NULL) {
1608         tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1609         tp = tp->next;
1610         break;
1611       }
1612       tp = tp->next;
1613     }
1614   }
1615
1616   tp->addr = addr;
1617   g_strlcpy(tp->name, name, MAXNAMELEN);
1618   tp->next = NULL;
1619
1620   return tp;
1621
1622 } /* add_ipxnet_name */
1623
1624 static gchar *ipxnet_name_lookup(const guint addr)
1625 {
1626   int hash_idx;
1627   hashipxnet_t *tp;
1628   ipxnet_t *ipxnet;
1629
1630   hash_idx = HASH_IPX_NET(addr);
1631
1632   tp = ipxnet_table[hash_idx];
1633
1634   if( tp == NULL ) {
1635     tp = ipxnet_table[hash_idx] = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1636   } else {
1637     while(1) {
1638       if (tp->addr == addr) {
1639         return tp->name;
1640       }
1641       if (tp->next == NULL) {
1642         tp->next = (hashipxnet_t *)g_malloc(sizeof(hashipxnet_t));
1643         tp = tp->next;
1644         break;
1645       }
1646       tp = tp->next;
1647     }
1648   }
1649
1650   /* fill in a new entry */
1651
1652   tp->addr = addr;
1653   tp->next = NULL;
1654
1655   if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1656     /* unknown name */
1657       g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1658
1659   } else {
1660     g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1661   }
1662
1663   return (tp->name);
1664
1665 } /* ipxnet_name_lookup */
1666
1667 static guint ipxnet_addr_lookup(const gchar *name, gboolean *success)
1668 {
1669   ipxnet_t *ipxnet;
1670   hashipxnet_t *tp;
1671   hashipxnet_t **table = ipxnet_table;
1672   int i;
1673
1674   /* to be optimized (hash table from name to addr) */
1675   for (i = 0; i < HASHIPXNETSIZE; i++) {
1676     tp = table[i];
1677     while (tp) {
1678       if (strcmp(tp->name, name) == 0) {
1679         *success = TRUE;
1680         return tp->addr;
1681       }
1682       tp = tp->next;
1683     }
1684   }
1685
1686   /* not in hash table : performs a file lookup */
1687
1688   if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1689     *success = FALSE;
1690     return 0;
1691   }
1692
1693   /* add new entry in hash table */
1694
1695   tp = add_ipxnet_name(ipxnet->addr, name);
1696
1697   *success = TRUE;
1698   return tp->addr;
1699
1700 } /* ipxnet_addr_lookup */
1701
1702 static gboolean
1703 read_hosts_file (const char *hostspath)
1704 {
1705   FILE *hf;
1706   char *line = NULL;
1707   int size = 0;
1708   gchar *cp;
1709   guint32 host_addr[4]; /* IPv4 or IPv6 */
1710   struct e_in6_addr ipv6_addr;
1711   gboolean is_ipv6;
1712   int ret;
1713
1714   /*
1715    *  See the hosts(4) or hosts(5) man page for hosts file format
1716    *  (not available on all systems).
1717    */
1718   if ((hf = eth_fopen(hostspath, "r")) == NULL)
1719     return FALSE;
1720
1721   while (fgetline(&line, &size, hf) >= 0) {
1722     if ((cp = strchr(line, '#')))
1723       *cp = '\0';
1724
1725     if ((cp = strtok(line, " \t")) == NULL)
1726       continue; /* no tokens in the line */
1727
1728     ret = inet_pton(AF_INET6, cp, &host_addr);
1729     if (ret == -1)
1730       continue; /* error parsing */
1731     if (ret == 1) {
1732       /* Valid IPv6 */
1733       is_ipv6 = TRUE;
1734     } else {
1735       /* Not valid IPv6 - valid IPv4? */
1736       if (inet_pton(AF_INET, cp, &host_addr) != 1)
1737         continue; /* no */
1738       is_ipv6 = FALSE;
1739     }
1740
1741     if ((cp = strtok(NULL, " \t")) == NULL)
1742       continue; /* no host name */
1743
1744     if (is_ipv6) {
1745       memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1746       add_ipv6_name(&ipv6_addr, cp);
1747     } else
1748       add_ipv4_name(host_addr[0], cp);
1749
1750     /*
1751      * Add the aliases, too, if there are any.
1752      */
1753     while ((cp = strtok(NULL, " \t")) != NULL) {
1754       if (is_ipv6) {
1755         memcpy(&ipv6_addr, host_addr, sizeof ipv6_addr);
1756         add_ipv6_name(&ipv6_addr, cp);
1757       } else
1758         add_ipv4_name(host_addr[0], cp);
1759     }
1760   }
1761   if (line != NULL)
1762     g_free(line);
1763
1764   fclose(hf);
1765   return TRUE;
1766 } /* read_hosts_file */
1767
1768 /*
1769  *  External Functions
1770  */
1771
1772 void
1773 host_name_lookup_init(void) {
1774   char *hostspath;
1775
1776 #ifdef HAVE_GNU_ADNS
1777 #ifdef _WIN32
1778   char *sysroot;
1779   static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
1780   static char rootpath_ot[] = "\\hosts";
1781 #endif /* _WIN32 */
1782 #endif /*GNU_ADNS */
1783
1784   /*
1785    * Load the user's hosts file, if they have one.
1786    */
1787   hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
1788   if (!read_hosts_file(hostspath) && errno != ENOENT) {
1789     report_open_failure(hostspath, errno, FALSE);
1790   }
1791   g_free(hostspath);
1792
1793   /*
1794    * Load the global hosts file, if we have one.
1795    */
1796   hostspath = get_datafile_path(ENAME_HOSTS);
1797   if (!read_hosts_file(hostspath) && errno != ENOENT) {
1798     report_open_failure(hostspath, errno, FALSE);
1799   }
1800   g_free(hostspath);
1801
1802 #ifdef HAVE_GNU_ADNS
1803   /*
1804    * We're using GNU ADNS, which doesn't check the system hosts file;
1805    * we load that file ourselves.
1806    */
1807 #ifdef _WIN32
1808
1809   sysroot = getenv_utf8("WINDIR");
1810   if (sysroot != NULL) {
1811     /*
1812      * The file should be under WINDIR.
1813      * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
1814      * %WINDIR%\system32\drivers\etc\hosts.
1815      * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
1816      * Try both.
1817      * XXX - should we base it on the dwPlatformId value from
1818      * GetVersionEx()?
1819      */
1820     hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
1821     if (!read_hosts_file(hostspath)) {
1822       g_free(hostspath);
1823       hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
1824       read_hosts_file(hostspath);
1825     }
1826     g_free(hostspath);
1827   }
1828 #else /* _WIN32 */
1829   read_hosts_file("/etc/hosts");
1830 #endif /* _WIN32 */
1831
1832   /* XXX - Any flags we should be using? */
1833   /* XXX - We could provide config settings for DNS servers, and
1834            pass them to ADNS with adns_init_strcfg */
1835   if (adns_init(&ads, 0, 0 /*0=>stderr*/) != 0) {
1836     /*
1837      * XXX - should we report the error?  I'm assuming that some crashes
1838      * reported on a Windows machine with TCP/IP not configured are due
1839      * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
1840      * ADNS in a state where it crashes due to that.  We'll still try
1841      * doing name resolution anyway.
1842      */
1843     return;
1844   }
1845   gnu_adns_initialized = TRUE;
1846   adns_currently_queued = 0;
1847 #endif /* HAVE_GNU_ADNS */
1848 }
1849
1850 #ifdef HAVE_GNU_ADNS
1851
1852 /* XXX - The ADNS "documentation" isn't very clear:
1853  * - Do we need to keep our query structures around?
1854  */
1855 gint
1856 host_name_lookup_process(gpointer data _U_) {
1857   adns_queue_msg_t *almsg;
1858   GList *cur;
1859   char addr_str[] = "111.222.333.444.in-addr.arpa.";
1860   guint8 *addr_bytes;
1861   adns_answer *ans;
1862   int ret;
1863   gboolean dequeue;
1864
1865   adns_queue_head = g_list_first(adns_queue_head);
1866
1867   cur = adns_queue_head;
1868   while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
1869     almsg = (adns_queue_msg_t *) cur->data;
1870     if (! almsg->submitted && almsg->type == AF_INET) {
1871       addr_bytes = (guint8 *) &almsg->ip4_addr;
1872       g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
1873           addr_bytes[2], addr_bytes[1], addr_bytes[0]);
1874       /* XXX - what if it fails? */
1875       adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
1876       almsg->submitted = TRUE;
1877       adns_currently_queued++;
1878     }
1879     cur = cur->next;
1880   }
1881
1882   cur = adns_queue_head;
1883   while (cur) {
1884     dequeue = FALSE;
1885     almsg = (adns_queue_msg_t *) cur->data;
1886     if (almsg->submitted) {
1887       ret = adns_check(ads, &almsg->query, &ans, NULL);
1888       if (ret == 0) {
1889         if (ans->status == adns_s_ok) {
1890           add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
1891         }
1892         dequeue = TRUE;
1893       }
1894     }
1895     cur = cur->next;
1896     if (dequeue) {
1897       adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
1898       g_free(almsg);
1899       adns_currently_queued--;
1900     }
1901   }
1902
1903   /* Keep the timeout in place */
1904   return 1;
1905 }
1906
1907 void
1908 host_name_lookup_cleanup(void) {
1909   void *qdata;
1910
1911   adns_queue_head = g_list_first(adns_queue_head);
1912   while (adns_queue_head) {
1913     qdata = adns_queue_head->data;
1914     adns_queue_head = g_list_remove(adns_queue_head, qdata);
1915     g_free(qdata);
1916   }
1917
1918   if (gnu_adns_initialized)
1919     adns_finish(ads);
1920 }
1921
1922 #else
1923
1924 gint
1925 host_name_lookup_process(gpointer data _U_) {
1926   /* Kill the timeout, as there's nothing for it to do */
1927   return 0;
1928 }
1929
1930 void
1931 host_name_lookup_cleanup(void) {
1932 }
1933
1934 #endif /* HAVE_GNU_ADNS */
1935
1936 extern gchar *get_hostname(guint addr)
1937 {
1938   gboolean found;
1939
1940   if (!(g_resolv_flags & RESOLV_NETWORK))
1941     return ip_to_str((guint8 *)&addr);
1942
1943   return host_name_lookup(addr, &found);
1944 }
1945
1946 extern const gchar *get_hostname6(struct e_in6_addr *addr)
1947 {
1948   gboolean found;
1949
1950   if (!(g_resolv_flags & RESOLV_NETWORK))
1951     return ip6_to_str(addr);
1952   if (E_IN6_IS_ADDR_LINKLOCAL(addr) || E_IN6_IS_ADDR_MULTICAST(addr))
1953     return ip6_to_str(addr);
1954   return host_name_lookup6(addr, &found);
1955 }
1956
1957 extern void add_ipv4_name(guint addr, const gchar *name)
1958 {
1959   int hash_idx;
1960   hashipv4_t *tp;
1961   int new_one = TRUE;
1962
1963   hash_idx = HASH_IPV4_ADDRESS(addr);
1964
1965   tp = ipv4_table[hash_idx];
1966
1967   if( tp == NULL ) {
1968     tp = ipv4_table[hash_idx] = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1969   } else {
1970     while(1) {
1971       if (tp->addr == addr) {
1972         /* address already known */
1973         if (!tp->is_dummy_entry) {
1974           return;
1975         } else {
1976           /* replace this dummy entry with the new one */
1977           new_one = FALSE;
1978           break;
1979         }
1980       }
1981       if (tp->next == NULL) {
1982         tp->next = (hashipv4_t *)g_malloc(sizeof(hashipv4_t));
1983         tp = tp->next;
1984         break;
1985       }
1986       tp = tp->next;
1987     }
1988   }
1989
1990   g_strlcpy(tp->name, name, MAXNAMELEN);
1991   if (new_one) {
1992       tp->addr = addr;
1993       tp->next = NULL;
1994   }
1995   tp->is_dummy_entry = FALSE;
1996
1997 } /* add_ipv4_name */
1998
1999 extern void add_ipv6_name(struct e_in6_addr *addrp, const gchar *name)
2000 {
2001   int hash_idx;
2002   hashipv6_t *tp;
2003   int new_one = TRUE;
2004
2005   hash_idx = HASH_IPV6_ADDRESS(*addrp);
2006
2007   tp = ipv6_table[hash_idx];
2008
2009   if( tp == NULL ) {
2010     tp = ipv6_table[hash_idx] = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2011   } else {
2012     while(1) {
2013       if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2014         /* address already known */
2015         if (!tp->is_dummy_entry) {
2016           return;
2017         } else {
2018           /* replace this dummy entry with the new one */
2019           new_one = FALSE;
2020           break;
2021         }
2022       }
2023       if (tp->next == NULL) {
2024         tp->next = (hashipv6_t *)g_malloc(sizeof(hashipv6_t));
2025         tp = tp->next;
2026         break;
2027       }
2028       tp = tp->next;
2029     }
2030   }
2031
2032   g_strlcpy(tp->name, name, MAXNAMELEN);
2033   if (new_one) {
2034       tp->addr = *addrp;
2035       tp->next = NULL;
2036   }
2037   tp->is_dummy_entry = FALSE;
2038
2039 } /* add_ipv6_name */
2040
2041 /* -----------------
2042  * unsigned integer to ascii
2043 */
2044 static gchar *ep_utoa(guint port)
2045 {
2046   gchar *bp = ep_alloc(MAXNAMELEN);
2047   
2048   bp = &bp[MAXNAMELEN -1];
2049   
2050   *bp = 0;
2051   do {
2052       *--bp = (port % 10) +'0';
2053   } while ((port /= 10) != 0);
2054   return bp;
2055 }
2056
2057
2058 extern gchar *get_udp_port(guint port)
2059 {
2060
2061   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2062     return ep_utoa(port);
2063   }
2064
2065   return serv_name_lookup(port, PT_UDP);
2066
2067 } /* get_udp_port */
2068
2069 extern gchar *get_dccp_port(guint port)
2070 {
2071
2072   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2073     return ep_utoa(port);
2074   }
2075
2076   return serv_name_lookup(port, PT_DCCP);
2077
2078 } /* get_dccp_port */
2079
2080
2081 extern gchar *get_tcp_port(guint port)
2082 {
2083
2084   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2085     return ep_utoa(port);
2086   }
2087
2088   return serv_name_lookup(port, PT_TCP);
2089
2090 } /* get_tcp_port */
2091
2092 extern gchar *get_sctp_port(guint port)
2093 {
2094
2095   if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
2096     return ep_utoa(port);
2097   }
2098
2099   return serv_name_lookup(port, PT_SCTP);
2100
2101 } /* get_sctp_port */
2102
2103
2104 const gchar *get_addr_name(address *addr)
2105 {
2106   const gchar *result;
2107
2108   result = solve_address_to_name(addr);
2109
2110   if (result!=NULL){
2111           return result;
2112   }
2113
2114   /* if it gets here, either it is of type AT_NONE, */
2115   /* or it should be solvable in address_to_str -unless addr->type is wrongly defined- */
2116
2117   if (addr->type == AT_NONE){
2118           return "NONE";
2119   }
2120
2121   return(address_to_str(addr));
2122 } /* get_addr_name */
2123
2124
2125 void get_addr_name_buf(address *addr, gchar *buf, guint size)
2126 {
2127   const gchar *result = get_addr_name(addr);
2128
2129   g_snprintf(buf, size, "%s", result);
2130 } /* get_addr_name_buf */
2131
2132
2133 extern gchar *get_ether_name(const guint8 *addr)
2134 {
2135   if (!(g_resolv_flags & RESOLV_MAC))
2136     return ether_to_str(addr);
2137
2138   if (!eth_resolution_initialized) {
2139     initialize_ethers();
2140     eth_resolution_initialized = 1;
2141   }
2142
2143   return eth_name_lookup(addr);
2144
2145 } /* get_ether_name */
2146
2147 /* Look for an ether name in the hash, and return it if found.
2148  * If it's not found, simply return NULL. We DO NOT make a new
2149  * hash entry for it with the hex digits turned into a string.
2150  */
2151 gchar *get_ether_name_if_known(const guint8 *addr)
2152 {
2153   int hash_idx;
2154   hashether_t *tp;
2155
2156   /* Initialize ether structs if we're the first
2157    * ether-related function called */
2158   if (!(g_resolv_flags & RESOLV_MAC))
2159     return NULL;
2160
2161   if (!eth_resolution_initialized) {
2162     initialize_ethers();
2163     eth_resolution_initialized = 1;
2164   }
2165
2166   hash_idx = HASH_ETH_ADDRESS(addr);
2167
2168   tp = eth_table[hash_idx];
2169
2170   if( tp == NULL ) {
2171           /* Hash key not found in table.
2172            * Force a lookup (and a hash entry) for addr, then call
2173            * myself. I plan on not getting into an infinite loop because
2174            * eth_name_lookup() is guaranteed to make a hashtable entry,
2175            * so when I call myself again, I can never get into this
2176            * block of code again. Knock on wood...
2177            */
2178           (void) eth_name_lookup(addr);
2179           return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2180   }
2181   else {
2182     while(1) {
2183       if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
2184               if (!tp->is_dummy_entry) {
2185                 /* A name was found, and its origin is an ethers file */
2186                 return tp->name;
2187               }
2188               else {
2189                 /* A name was found, but it was created, not found in a file */
2190                 return NULL;
2191               }
2192       }
2193       if (tp->next == NULL) {
2194           /* Read my reason above for why I'm sure I can't get into an infinite loop */
2195           (void) eth_name_lookup(addr);
2196           return get_ether_name_if_known(addr); /* a well-placed goto would suffice */
2197       }
2198       tp = tp->next;
2199     }
2200   }
2201   g_assert_not_reached();
2202   return NULL;
2203 }
2204
2205
2206 extern guint8 *get_ether_addr(const gchar *name)
2207 {
2208
2209   /* force resolution (do not check g_resolv_flags) */
2210
2211   if (!eth_resolution_initialized) {
2212     initialize_ethers();
2213     eth_resolution_initialized = 1;
2214   }
2215
2216   return eth_addr_lookup(name);
2217
2218 } /* get_ether_addr */
2219
2220 extern void add_ether_byip(guint ip, const guint8 *eth)
2221 {
2222
2223   gchar *host;
2224   gboolean found;
2225
2226   /* first check that IP address can be resolved */
2227
2228   if (!(g_resolv_flags & RESOLV_NETWORK) || ((host = host_name_lookup(ip, &found)) == NULL))
2229     return;
2230
2231   /* ok, we can add this entry in the ethers hashtable */
2232
2233   if (found)
2234     add_eth_name(eth, host);
2235
2236 } /* add_ether_byip */
2237
2238 extern const gchar *get_ipxnet_name(const guint32 addr)
2239 {
2240
2241   if (!(g_resolv_flags & RESOLV_NETWORK)) {
2242           return ipxnet_to_str_punct(addr, '\0');
2243   }
2244
2245   if (!ipxnet_resolution_initialized) {
2246     initialize_ipxnets();
2247     ipxnet_resolution_initialized = 1;
2248   }
2249
2250   return ipxnet_name_lookup(addr);
2251
2252 } /* get_ipxnet_name */
2253
2254 extern guint32 get_ipxnet_addr(const gchar *name, gboolean *known)
2255 {
2256   guint32 addr;
2257   gboolean success;
2258
2259   /* force resolution (do not check g_resolv_flags) */
2260
2261   if (!ipxnet_resolution_initialized) {
2262     initialize_ipxnets();
2263     ipxnet_resolution_initialized = 1;
2264   }
2265
2266   addr =  ipxnet_addr_lookup(name, &success);
2267
2268   *known = success;
2269   return addr;
2270
2271 } /* get_ipxnet_addr */
2272
2273 extern const gchar *get_manuf_name(const guint8 *addr)
2274 {
2275   gchar *cur;
2276   hashmanuf_t  *manufp;
2277
2278   if ((g_resolv_flags & RESOLV_MAC) && !eth_resolution_initialized) {
2279     initialize_ethers();
2280     eth_resolution_initialized = 1;
2281   }
2282
2283   if (!(g_resolv_flags & RESOLV_MAC) || ((manufp = manuf_name_lookup(addr)) == NULL)) {
2284     cur=ep_alloc(MAXMANUFLEN);
2285     g_snprintf(cur, MAXMANUFLEN, "%02x:%02x:%02x", addr[0], addr[1], addr[2]);
2286     return cur;
2287   }
2288
2289   return manufp->name;
2290
2291 } /* get_manuf_name */
2292
2293
2294 const gchar *get_manuf_name_if_known(const guint8 *addr)
2295 {
2296   hashmanuf_t  *manufp;
2297
2298   if (!eth_resolution_initialized) {
2299     initialize_ethers();
2300     eth_resolution_initialized = 1;
2301   }
2302
2303   if ((manufp = manuf_name_lookup(addr)) == NULL) {
2304     return NULL;
2305   }
2306
2307   return manufp->name;
2308
2309 } /* get_manuf_name_if_known */
2310
2311
2312 /* Translate a string, assumed either to be a dotted-quad IP address or
2313  * a host name, to a numeric IP address.  Return TRUE if we succeed and
2314  * set "*addrp" to that numeric IP address; return FALSE if we fail.
2315  * Used more in the dfilter parser rather than in packet dissectors */
2316 gboolean get_host_ipaddr(const char *host, guint32 *addrp)
2317 {
2318         struct in_addr          ipaddr;
2319         struct hostent          *hp;
2320
2321         /*
2322          * don't change it to inet_pton(AF_INET), they are not 100% compatible.
2323          * inet_pton(AF_INET) does not support hexadecimal notation nor
2324          * less-than-4 octet notation.
2325          */
2326         if (!inet_aton(host, &ipaddr)) {
2327                 /* It's not a valid dotted-quad IP address; is it a valid
2328                  * host name? */
2329                 hp = gethostbyname(host);
2330                 if (hp == NULL) {
2331                         /* No. */
2332                         return FALSE;
2333                         /* Apparently, some versions of gethostbyaddr can
2334                          * return IPv6 addresses. */
2335                 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
2336                         memcpy(&ipaddr, hp->h_addr, hp->h_length);
2337                 } else {
2338                         return FALSE;
2339                 }
2340         } else {
2341                 /* Does the string really contain dotted-quad IP?
2342                  * Check against inet_atons that accept strings such as
2343                  * "130.230" as valid addresses and try to convert them
2344                  * to some form of a classful (host.net) notation.
2345                  */
2346                 unsigned int a0, a1, a2, a3;
2347                 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
2348                         return FALSE;
2349         }
2350
2351         *addrp = g_ntohl(ipaddr.s_addr);
2352         return TRUE;
2353 }
2354
2355 /*
2356  * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
2357  * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
2358  * return FALSE if we fail.
2359  */
2360 gboolean get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
2361 {
2362         struct hostent *hp;
2363
2364         if (inet_pton(AF_INET6, host, addrp) == 1)
2365                 return TRUE;
2366
2367         /* try FQDN */
2368 #ifdef HAVE_GETHOSTBYNAME2
2369         hp = gethostbyname2(host, AF_INET6);
2370 #else
2371         hp = NULL;
2372 #endif
2373         if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
2374                 memcpy(addrp, hp->h_addr, hp->h_length);
2375                 return TRUE;
2376         }
2377
2378         return FALSE;
2379 }
2380
2381 /*
2382  * Find out whether a hostname resolves to an ip or ipv6 address
2383  * Return "ip6" if it is IPv6, "ip" otherwise (including the case
2384  * that we don't know)
2385  */
2386 const char* host_ip_af(const char *host
2387 #ifndef HAVE_GETHOSTBYNAME2
2388 _U_
2389 #endif
2390 )
2391 {
2392 #ifdef HAVE_GETHOSTBYNAME2
2393         struct hostent *h;
2394         return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
2395 #else
2396         return "ip";
2397 #endif
2398 }