ISO14443: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / epan / addr_resolv.c
1 /* addr_resolv.c
2  * Routines for network object lookup
3  *
4  * Laurent Deniel <laurent.deniel@free.fr>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 /*
33  * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
34  * are most likely to take a long time, given the way address-to-name
35  * lookups are done over NBNS).
36  *
37  * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
38  * call in a signal handler, you might crash, because the state of the
39  * resolution code that sends messages to lookupd might be inconsistent
40  * if you jump out of it in middle of a call.
41  *
42  * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
43  * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
44  * call (and possibly in other gethostbyaddr() calls), because there's
45  * a mutex lock held in gethostbyaddr() and it doesn't get released
46  * if we longjmp out of it.
47  *
48  * There's no guarantee that longjmp()ing out of name resolution calls
49  * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
50  * code in tcpdump, to avoid those sorts of problems, and that was
51  * picked up by tcpdump.org tcpdump.
52  *
53  * So, for now, we do not use alarm() and SIGALRM to time out host name
54  * lookups.  If we get a lot of complaints about lookups taking a long time,
55  * we can reconsider that decision.  (Note that tcpdump originally added
56  * such a timeout mechanism that for the benefit of systems using NIS to
57  * look up host names; that might now be fixed in NIS implementations, for
58  * those sites still using NIS rather than DNS for that....  tcpdump no
59  * longer does that, for the same reasons that we don't.)
60  *
61  * If we're using an asynchronous DNS resolver, that shouldn't be an issue.
62  * If we're using a synchronous name lookup mechanism (which we'd do mainly
63  * to support resolving addresses and host names using more mechanisms than
64  * just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
65  * a separate thread, making it, in effect, asynchronous.
66  */
67
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
70 #endif
71
72 #ifdef HAVE_NETDB_H
73 #include <netdb.h>
74 #endif
75
76 #ifdef HAVE_ARPA_INET_H
77 #include <arpa/inet.h>
78 #endif
79
80 #ifdef HAVE_SYS_SOCKET_H
81 #include <sys/socket.h>     /* needed to define AF_ values on UNIX */
82 #endif
83
84 #ifdef HAVE_WINSOCK2_H
85 #include <winsock2.h>       /* needed to define AF_ values on Windows */
86 #endif
87
88 #ifndef HAVE_INET_ATON
89 # include "wsutil/inet_aton.h"
90 #endif
91
92 #ifdef NEED_INET_V6DEFS_H
93 # include "wsutil/inet_v6defs.h"
94 #endif
95
96 #if defined(_WIN32) && defined(INET6)
97 # include <ws2tcpip.h>
98 #endif
99
100 #ifdef HAVE_C_ARES
101 # if defined(_WIN32) && !defined(INET6)
102 #  define socklen_t unsigned int
103 # endif
104 # include <ares.h>
105 # include <ares_version.h>
106 #else
107 # ifdef HAVE_GNU_ADNS
108 #  include <errno.h>
109 #  include <adns.h>
110 #  if defined(inet_aton) && defined(_WIN32)
111 #   undef inet_aton
112 #  endif
113 # endif /* HAVE_GNU_ADNS */
114 #endif  /* HAVE_C_ARES */
115
116
117 #include <glib.h>
118
119 #include "packet.h"
120 #include "addr_and_mask.h"
121 #include "ipv6-utils.h"
122 #include "addr_resolv.h"
123 #include "wsutil/filesystem.h"
124
125 #include <wsutil/report_err.h>
126 #include <wsutil/file_util.h>
127 #include <wsutil/pint.h>
128
129 #include <epan/strutil.h>
130 #include <epan/to_str-int.h>
131 #include <epan/prefs.h>
132
133 #define ENAME_HOSTS     "hosts"
134 #define ENAME_SUBNETS   "subnets"
135 #define ENAME_ETHERS    "ethers"
136 #define ENAME_IPXNETS   "ipxnets"
137 #define ENAME_MANUF     "manuf"
138 #define ENAME_SERVICES  "services"
139
140 #define HASHETHSIZE      2048
141 #define HASHHOSTSIZE     2048
142 #define HASHIPXNETSIZE    256
143 #define SUBNETLENGTHSIZE   32  /*1-32 inc.*/
144
145 /* hash table used for IPv4 lookup */
146
147 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
148
149
150 typedef struct sub_net_hashipv4 {
151     guint             addr;
152     guint8            flags;          /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
153     struct sub_net_hashipv4   *next;
154     gchar             ip[16];
155     gchar             name[MAXNAMELEN];
156 } sub_net_hashipv4_t;
157
158 /* Array of entries of subnets of different lengths */
159 typedef struct {
160     gsize        mask_length;      /*1-32*/
161     guint32      mask;             /* e.g. 255.255.255.*/
162     sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
163 } subnet_length_entry_t;
164
165
166 /* hash table used for IPX network lookup */
167
168 /* XXX - check goodness of hash function */
169
170 #define HASH_IPX_NET(net)   ((net) & (HASHIPXNETSIZE - 1))
171
172 typedef struct hashipxnet {
173     guint               addr;
174     struct hashipxnet  *next;
175     gchar               name[MAXNAMELEN];
176 } hashipxnet_t;
177
178 /* hash tables used for ethernet and manufacturer lookup */
179 #define HASHETHER_STATUS_UNRESOLVED     1
180 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
181 #define HASHETHER_STATUS_RESOLVED_NAME  3
182
183 struct hashether {
184     guint             status;  /* (See above) */
185     guint8            addr[6];
186     char              hexaddr[6*3];
187     char              resolved_name[MAXNAMELEN];
188 };
189
190 struct hashmanuf {
191     guint             status;  /* (See above) */
192     guint8            addr[3];
193     char              hexaddr[3*3];
194     char              resolved_name[MAXNAMELEN];
195 };
196
197 /* internal ethernet type */
198 typedef struct _ether
199 {
200     guint8            addr[6];
201     char              name[MAXNAMELEN];
202 } ether_t;
203
204 /* internal ipxnet type */
205 typedef struct _ipxnet
206 {
207     guint             addr;
208     char              name[MAXNAMELEN];
209 } ipxnet_t;
210
211 static GHashTable   *ipxnet_hash_table = NULL;
212 static GHashTable   *ipv4_hash_table = NULL;
213 static GHashTable   *ipv6_hash_table = NULL;
214
215 static GSList *manually_resolved_ipv4_list = NULL;
216 static GSList *manually_resolved_ipv6_list = NULL;
217
218 typedef struct _resolved_ipv4
219 {
220     guint32          host_addr;
221     char             name[MAXNAMELEN];
222 } resolved_ipv4_t;
223
224 typedef struct _resolved_ipv6
225 {
226     struct e_in6_addr  ip6_addr;
227     char               name[MAXNAMELEN];
228 } resolved_ipv6_t;
229
230 static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
231
232 static gchar        *cb_service;
233 static port_type    cb_proto = PT_NONE;
234
235
236 static GHashTable *manuf_hashtable = NULL;
237 static GHashTable *wka_hashtable = NULL;
238 static GHashTable *eth_hashtable = NULL;
239 static GHashTable *serv_port_hashtable = NULL;
240
241 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
242 static gboolean have_subnet_entry = FALSE;
243
244 static gboolean new_resolved_objects = FALSE;
245
246 static GPtrArray* extra_hosts_files = NULL;
247
248 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
249 static void add_serv_port_cb(const guint32 port);
250
251
252 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
253  * One-at-a-Time hash
254  */
255 static guint32
256 ipv6_oat_hash(gconstpointer key)
257 {
258     int len = 16;
259     const unsigned char *p = (const unsigned char *)key;
260     guint32 h = 0;
261     int i;
262
263     for ( i = 0; i < len; i++ ) {
264         h += p[i];
265         h += ( h << 10 );
266         h ^= ( h >> 6 );
267     }
268
269     h += ( h << 3 );
270     h ^= ( h >> 11 );
271     h += ( h << 15 );
272
273     return h;
274 }
275
276 static gboolean
277 ipv6_equal(gconstpointer v1, gconstpointer v2)
278 {
279
280     if (memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0) {
281         return TRUE;
282     }
283
284     return FALSE;
285 }
286
287 /*
288  * Flag controlling what names to resolve.
289  */
290 e_addr_resolve gbl_resolv_flags = {
291     TRUE,   /* mac_name */
292     FALSE,  /* network_name */
293     FALSE,  /* transport_name */
294     TRUE,   /* concurrent_dns */
295     TRUE,   /* dns_pkt_addr_resolution */
296     TRUE,   /* use_external_net_name_resolver */
297     FALSE   /* load_hosts_file_from_profile_only */
298 };
299 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
300 static guint name_resolve_concurrency = 500;
301 #endif
302
303 /*
304  *  Global variables (can be changed in GUI sections)
305  *  XXX - they could be changed in GUI code, but there's currently no
306  *  GUI code to change them.
307  */
308
309 gchar *g_ethers_path    = NULL;     /* global ethers file     */
310 gchar *g_pethers_path   = NULL;     /* personal ethers file   */
311 gchar *g_ipxnets_path   = NULL;     /* global ipxnets file    */
312 gchar *g_pipxnets_path  = NULL;     /* personal ipxnets file  */
313 gchar *g_services_path  = NULL;     /* global services file   */
314 gchar *g_pservices_path = NULL;     /* personal services file */
315                                     /* first resolving call   */
316
317 /* c-ares */
318 #ifdef HAVE_C_ARES
319 /*
320  * Submitted queries trigger a callback (c_ares_ghba_cb()).
321  * Queries are added to c_ares_queue_head. During processing, queries are
322  * popped off the front of c_ares_queue_head and submitted using
323  * ares_gethostbyaddr().
324  * The callback processes the response, then frees the request.
325  */
326 #define ASYNC_DNS
327 typedef struct _async_dns_queue_msg
328 {
329     union {
330         guint32           ip4;
331         struct e_in6_addr ip6;
332     } addr;
333     int                 family;
334 } async_dns_queue_msg_t;
335
336 typedef struct _async_hostent {
337     int addr_size;
338     int   copied;
339     void *addrp;
340 } async_hostent_t;
341
342 #if ( ( ARES_VERSION_MAJOR < 1 )                                     \
343         || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
344 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
345 #else
346 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
347 #endif
348
349 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
350 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
351
352 #else
353 /* GNU ADNS */
354 #ifdef HAVE_GNU_ADNS
355 #define ASYNC_DNS
356 /*
357  * Submitted queries have to be checked individually using adns_check().
358  * Queries are added to adns_queue_head. During processing, the list is
359  * iterated twice: once to request queries up to the concurrency limit,
360  * and once to check the status of each query.
361  */
362
363 adns_state ads;
364
365 typedef struct _async_dns_queue_msg
366 {
367     gboolean    submitted;
368     guint32     ip4_addr;
369     int         type;
370     adns_query  query;
371 } async_dns_queue_msg_t;
372
373 #endif /* HAVE_GNU_ADNS */
374 #endif /* HAVE_C_ARES */
375 #ifdef ASYNC_DNS
376 static  gboolean  async_dns_initialized = FALSE;
377 static  guint       async_dns_in_flight = 0;
378 static  GList    *async_dns_queue_head = NULL;
379
380 /* push a dns request */
381 static void
382 add_async_dns_ipv4(int type, guint32 addr)
383 {
384     async_dns_queue_msg_t *msg;
385
386     msg = g_new(async_dns_queue_msg_t,1);
387 #ifdef HAVE_C_ARES
388     msg->family = type;
389     msg->addr.ip4 = addr;
390 #else
391     msg->type = type;
392     msg->ip4_addr = addr;
393     msg->submitted = FALSE;
394 #endif
395     async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
396 }
397
398 #endif
399
400 typedef struct {
401     guint32      mask;
402     gsize        mask_length;
403     const gchar* name; /* Shallow copy */
404 } subnet_entry_t;
405
406 /*
407  *  Miscellaneous functions
408  */
409
410 static int
411 fgetline(char **buf, int *size, FILE *fp)
412 {
413     int len;
414     int c;
415
416     if (fp == NULL || buf == NULL)
417         return -1;
418
419     if (*buf == NULL) {
420         if (*size == 0)
421             *size = BUFSIZ;
422
423         *buf = (char *)g_malloc(*size);
424     }
425
426     g_assert(*buf);
427     g_assert(*size > 0);
428
429     if (feof(fp))
430         return -1;
431
432     len = 0;
433     while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
434         if (len+1 >= *size) {
435             *buf = (char *)g_realloc(*buf, *size += BUFSIZ);
436         }
437         (*buf)[len++] = c;
438     }
439
440     if (len == 0 && c == EOF)
441         return -1;
442
443     (*buf)[len] = '\0';
444
445     return len;
446
447 } /* fgetline */
448
449
450 /*
451  *  Local function definitions
452  */
453 static subnet_entry_t subnet_lookup(const guint32 addr);
454 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
455
456
457 static void
458 add_service_name(port_type proto, const guint port, const char *service_name)
459 {
460     serv_port_t *serv_port_table;
461     int *key;
462
463     key = (int *)g_new(int, 1);
464     *key = port;
465
466     serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
467     if (serv_port_table == NULL) {
468         serv_port_table = g_new0(serv_port_t,1);
469         g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
470     }
471     else {
472         g_free(key);
473     }
474
475     switch(proto) {
476         case PT_TCP:
477             g_free(serv_port_table->tcp_name);
478             serv_port_table->tcp_name = g_strdup(service_name);
479             break;
480         case PT_UDP:
481             g_free(serv_port_table->udp_name);
482             serv_port_table->udp_name = g_strdup(service_name);
483             break;
484         case PT_SCTP:
485             g_free(serv_port_table->sctp_name);
486             serv_port_table->sctp_name = g_strdup(service_name);
487             break;
488         case PT_DCCP:
489             g_free(serv_port_table->dccp_name);
490             serv_port_table->dccp_name = g_strdup(service_name);
491             break;
492         default:
493             return;
494             /* Should not happen */
495     }
496
497     new_resolved_objects = TRUE;
498 }
499
500
501 static void
502 parse_service_line (char *line)
503 {
504     /*
505      *  See the services(4) or services(5) man page for services file format
506      *  (not available on all systems).
507      */
508
509     gchar *cp;
510     gchar *service;
511     gchar *port;
512     port_type proto;
513
514     range_t *port_rng = NULL;
515     guint32 max_port = MAX_UDP_PORT;
516
517     if ((cp = strchr(line, '#')))
518         *cp = '\0';
519
520     if ((cp = strtok(line, " \t")) == NULL)
521         return;
522
523     service = cp;
524
525     if ((cp = strtok(NULL, " \t")) == NULL)
526         return;
527
528     port = cp;
529
530     if (strtok(cp, "/") == NULL)
531         return;
532
533     if ((cp = strtok(NULL, "/")) == NULL)
534         return;
535
536     /* seems we got all interesting things from the file */
537     if (strcmp(cp, "tcp") == 0) {
538         max_port = MAX_TCP_PORT;
539         proto = PT_TCP;
540     }
541     else if (strcmp(cp, "udp") == 0) {
542         max_port = MAX_UDP_PORT;
543         proto = PT_UDP;
544     }
545     else if (strcmp(cp, "sctp") == 0) {
546         max_port = MAX_SCTP_PORT;
547         proto = PT_SCTP;
548     }
549     else if (strcmp(cp, "dccp") == 0) {
550         max_port = MAX_DCCP_PORT;
551         proto = PT_DCCP;
552     } else {
553         return;
554     }
555
556     if (CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port)) {
557         /* some assertion here? */
558         return;
559     }
560
561     cb_service = service;
562     cb_proto = proto;
563     range_foreach(port_rng, add_serv_port_cb);
564     g_free (port_rng);
565     cb_proto = PT_NONE;
566 } /* parse_service_line */
567
568
569 static void
570 add_serv_port_cb(const guint32 port)
571 {
572     if ( port ) {
573         add_service_name(cb_proto, port, cb_service);
574     }
575 }
576
577
578 static void
579 parse_services_file(const char * path)
580 {
581     FILE *serv_p;
582     static int     size = 0;
583     static char   *buf = NULL;
584
585     /* services hash table initialization */
586     serv_p = ws_fopen(path, "r");
587
588     if (serv_p == NULL)
589         return;
590
591     while (fgetline(&buf, &size, serv_p) >= 0) {
592         parse_service_line (buf);
593     }
594
595     fclose(serv_p);
596 }
597
598 /* -----------------
599  * unsigned integer to ascii
600  */
601 static gchar *
602 wmem_utoa(wmem_allocator_t *allocator, guint port)
603 {
604     gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
605
606     /* XXX, guint32_to_str() ? */
607     guint32_to_str_buf(port, bp, MAXNAMELEN);
608     return bp;
609 }
610
611 static const gchar *
612 _serv_name_lookup(port_type proto, guint port, serv_port_t **value_ret)
613 {
614     serv_port_t *serv_port_table;
615
616     serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
617
618     if (value_ret != NULL)
619         *value_ret = serv_port_table;
620
621     if (serv_port_table == NULL)
622         return NULL;
623
624     switch (proto) {
625         case PT_UDP:
626             return serv_port_table->udp_name;
627         case PT_TCP:
628             return serv_port_table->tcp_name;
629         case PT_SCTP:
630             return serv_port_table->sctp_name;
631         case PT_DCCP:
632             return serv_port_table->dccp_name;
633         default:
634             break;
635     }
636     return NULL;
637 }
638
639 const gchar *
640 try_serv_name_lookup(port_type proto, guint port)
641 {
642     return _serv_name_lookup(proto, port, NULL);
643 }
644
645 const gchar *
646 serv_name_lookup(port_type proto, guint port)
647 {
648     serv_port_t *serv_port_table = NULL;
649     const char *name;
650     guint *key;
651
652     name = _serv_name_lookup(proto, port, &serv_port_table);
653     if (name != NULL)
654         return name;
655
656     if (serv_port_table == NULL) {
657         key = (guint *)g_new(guint, 1);
658         *key = port;
659         serv_port_table = g_new0(serv_port_t, 1);
660         g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
661     }
662     if (serv_port_table->numeric == NULL) {
663         serv_port_table->numeric = g_strdup_printf("%u", port);
664     }
665
666     return serv_port_table->numeric;
667 }
668
669 static void
670 destroy_serv_port(gpointer data)
671 {
672     serv_port_t *table = (serv_port_t*)data;
673     g_free(table->udp_name);
674     g_free(table->tcp_name);
675     g_free(table->sctp_name);
676     g_free(table->dccp_name);
677     g_free(table->numeric);
678     g_free(table);
679 }
680
681 static void
682 initialize_services(void)
683 {
684     g_assert(serv_port_hashtable == NULL);
685     serv_port_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, destroy_serv_port);
686
687     /* Compute the pathname of the services file. */
688     if (g_services_path == NULL) {
689         g_services_path = get_datafile_path(ENAME_SERVICES);
690     }
691     parse_services_file(g_services_path);
692
693     /* Compute the pathname of the personal services file */
694     if (g_pservices_path == NULL) {
695         g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
696     }
697     parse_services_file(g_pservices_path);
698 }
699
700 static void
701 service_name_lookup_cleanup(void)
702 {
703     if (serv_port_hashtable) {
704         g_hash_table_destroy(serv_port_hashtable);
705         serv_port_hashtable = NULL;
706     }
707 }
708
709 /* Fill in an IP4 structure with info from subnets file or just with the
710  * string form of the address.
711  */
712 static void
713 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
714 {
715     subnet_entry_t subnet_entry;
716
717     if (tp->flags & DUMMY_ADDRESS_ENTRY)
718         return; /* already done */
719
720     tp->flags |= DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
721
722     /* Do we have a subnet for this address? */
723     subnet_entry = subnet_lookup(addr);
724     if (0 != subnet_entry.mask) {
725         /* Print name, then '.' then IP address after subnet mask */
726         guint32 host_addr;
727         gchar buffer[MAX_IP_STR_LEN];
728         gchar* paddr;
729         gsize i;
730
731         host_addr = addr & (~(guint32)subnet_entry.mask);
732         ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
733         paddr = buffer;
734
735         /* Skip to first octet that is not totally masked
736          * If length of mask is 32, we chomp the whole address.
737          * If the address string starts '.' (should not happen?),
738          * we skip that '.'.
739          */
740         i = subnet_entry.mask_length / 8;
741         while(*(paddr) != '\0' && i > 0) {
742             if (*(++paddr) == '.') {
743                 --i;
744             }
745         }
746
747         /* There are more efficient ways to do this, but this is safe if we
748          * trust g_snprintf and MAXNAMELEN
749          */
750         g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
751     } else {
752         ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
753     }
754 }
755
756 #ifdef HAVE_C_ARES
757
758 static void
759 c_ares_ghba_cb(
760         void *arg,
761         int status,
762 #if ( ( ARES_VERSION_MAJOR < 1 )                                     \
763         || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
764         struct hostent *he
765 #else
766         int timeouts _U_,
767         struct hostent *he
768 #endif
769         ) {
770
771     async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
772     char **p;
773
774     if (!caqm) return;
775     /* XXX, what to do if async_dns_in_flight == 0? */
776     async_dns_in_flight--;
777
778     if (status == ARES_SUCCESS) {
779         for (p = he->h_addr_list; *p != NULL; p++) {
780             switch(caqm->family) {
781                 case AF_INET:
782                     add_ipv4_name(caqm->addr.ip4, he->h_name);
783                     break;
784                 case AF_INET6:
785                     add_ipv6_name(&caqm->addr.ip6, he->h_name);
786                     break;
787                 default:
788                     /* Throw an exception? */
789                     break;
790             }
791         }
792     }
793     g_free(caqm);
794 }
795 #endif /* HAVE_C_ARES */
796
797 /* --------------- */
798 static hashipv4_t *
799 new_ipv4(const guint addr)
800 {
801     hashipv4_t *tp = g_new(hashipv4_t, 1);
802     tp->addr = addr;
803     tp->flags = 0;
804     tp->name[0] = '\0';
805     ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
806     return tp;
807 }
808
809 static hashipv4_t *
810 host_lookup(const guint addr, gboolean *found)
811 {
812     hashipv4_t * volatile tp;
813
814     *found = TRUE;
815
816     tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
817     if (tp == NULL) {
818         tp = new_ipv4(addr);
819         g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
820     } else {
821         if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY) {
822             goto try_resolv;
823         }
824         if (tp->flags & DUMMY_ADDRESS_ENTRY) {
825             *found = FALSE;
826         }
827         return tp;
828     }
829
830 try_resolv:
831     if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
832         tp->flags |= TRIED_RESOLVE_ADDRESS;
833
834 #ifdef ASYNC_DNS
835         if (gbl_resolv_flags.concurrent_dns &&
836                 name_resolve_concurrency > 0 &&
837                 async_dns_initialized) {
838             add_async_dns_ipv4(AF_INET, addr);
839             /* XXX found is set to TRUE, which seems a bit odd, but I'm not
840              * going to risk changing the semantics.
841              */
842             fill_dummy_ip4(addr, tp);
843             return tp;
844         }
845
846         /*
847          * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
848          * the name of the host on which it's running; to work around that
849          * botch, we don't try to translate an all-zero IP address to a host
850          * name.
851          *
852          * Presumably getaddrinfo() behaves the same way.  Anyway, we should
853          * never get to this code on Windows since those builds include c-ares.
854          */
855 #elif defined(HAVE_GETADDRINFO)
856         if (addr != 0) {
857             struct sockaddr_in sin;
858
859             memset(&sin, 0, sizeof(sin));
860             sin.sin_family      = AF_INET;
861             sin.sin_addr.s_addr = addr;
862             if (getnameinfo((struct sockaddr *)&sin, sizeof(sin),
863                             tp->name, sizeof(tp->name),
864                             NULL, 0, NI_NAMEREQD) == 0) {
865                 return tp;
866             }
867         }
868 #elif defined(HAVE_GETHOSTBYNAME)
869         if (addr != 0) {
870             struct hostent *hostp;
871
872             hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
873
874             if (hostp != NULL && hostp->h_name[0] != '\0') {
875                 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
876                 return tp;
877             }
878         }
879 #endif
880
881         /* unknown host or DNS timeout */
882     }
883
884     *found = FALSE;
885     fill_dummy_ip4(addr, tp);
886     return tp;
887
888 } /* host_lookup */
889
890 /* --------------- */
891 static hashipv6_t *
892 new_ipv6(const struct e_in6_addr *addr)
893 {
894     hashipv6_t *tp = g_new(hashipv6_t,1);
895     memcpy(tp->addr, addr->bytes, sizeof tp->addr);
896     tp->flags = 0;
897     tp->name[0] = '\0';
898     ip6_to_str_buf(addr, tp->ip6);
899     return tp;
900 }
901
902 /* ------------------------------------ */
903 static hashipv6_t *
904 host_lookup6(const struct e_in6_addr *addr, gboolean *found)
905 {
906     hashipv6_t * volatile tp;
907 #ifdef INET6
908 #ifdef HAVE_C_ARES
909     async_dns_queue_msg_t *caqm;
910 #elif defined(HAVE_GETADDRINFO)
911     struct sockaddr_in6 sin6;
912 #elif defined(HAVE_GETHOSTBYNAME)
913     struct hostent *hostp;
914 #endif
915 #endif /* INET6 */
916
917     *found = TRUE;
918
919     tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
920     if (tp == NULL) {
921         struct e_in6_addr *addr_key;
922
923         addr_key = g_new(struct e_in6_addr,1);
924         tp = new_ipv6(addr);
925         memcpy(addr_key, addr, 16);
926         g_hash_table_insert(ipv6_hash_table, addr_key, tp);
927     } else {
928         if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) ==  DUMMY_ADDRESS_ENTRY) {
929             goto try_resolv;
930         }
931         if (tp->flags & DUMMY_ADDRESS_ENTRY) {
932             *found = FALSE;
933         }
934         return tp;
935     }
936
937 try_resolv:
938     if (gbl_resolv_flags.network_name &&
939             gbl_resolv_flags.use_external_net_name_resolver) {
940         tp->flags |= TRIED_RESOLVE_ADDRESS;
941 #ifdef INET6
942 #ifdef HAVE_C_ARES
943         if ((gbl_resolv_flags.concurrent_dns) &&
944                 name_resolve_concurrency > 0 &&
945                 async_dns_initialized) {
946             caqm = g_new(async_dns_queue_msg_t,1);
947             caqm->family = AF_INET6;
948             memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
949             async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
950
951             /* XXX found is set to TRUE, which seems a bit odd, but I'm not
952              * going to risk changing the semantics.
953              */
954             if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
955                 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
956                 ip6_to_str_buf(addr, tp->name);
957                 tp->flags |= DUMMY_ADDRESS_ENTRY;
958             }
959             return tp;
960         }
961 #elif defined(HAVE_GETADDRINFO)
962         memset(&sin6, 0, sizeof(sin6));
963         sin6.sin6_family      = AF_INET6;
964         memcpy(sin6.sin6_addr.s6_addr, addr, sizeof(*addr));
965         if (getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
966                         tp->name, sizeof(tp->name),
967                         NULL, 0, NI_NAMEREQD) == 0) {
968             return tp;
969         }
970 #elif defined(HAVE_GETHOSTBYNAME)
971         /* Quick hack to avoid DNS/YP timeout */
972         hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
973
974         if (hostp != NULL && hostp->h_name[0] != '\0') {
975             g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
976             return tp;
977         }
978 #endif
979 #endif /* INET6 */
980     }
981
982     /* unknown host or DNS timeout */
983     if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
984         tp->flags |= DUMMY_ADDRESS_ENTRY;
985         g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
986     }
987     *found = FALSE;
988     return tp;
989
990 } /* host_lookup6 */
991
992 /*
993  * Ethernet / manufacturer resolution
994  *
995  * The following functions implement ethernet address resolution and
996  * ethers files parsing (see ethers(4)).
997  *
998  * The manuf file has the same format as ethers(4) except that names are
999  * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1000  * only 3 bytes (instead of 6).
1001  *
1002  * Notes:
1003  *
1004  * I decide to not use the existing functions (see ethers(3) on some
1005  * operating systems) for the following reasons:
1006  * - performance gains (use of hash tables and some other enhancements),
1007  * - use of two ethers files (system-wide and per user),
1008  * - avoid the use of NIS maps,
1009  * - lack of these functions on some systems.
1010  *
1011  * So the following functions do _not_ behave as the standard ones.
1012  *
1013  * -- Laurent.
1014  */
1015
1016
1017 /*
1018  * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1019  * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1020  * mask.
1021  */
1022 static gboolean
1023 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1024         const gboolean manuf_file)
1025 {
1026     int i;
1027     unsigned long num;
1028     char *p;
1029     char sep = '\0';
1030
1031     for (i = 0; i < 6; i++) {
1032         /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1033         if (!g_ascii_isxdigit(*cp))
1034             return FALSE;
1035         num = strtoul(cp, &p, 16);
1036         if (p == cp)
1037             return FALSE; /* failed */
1038         if (num > 0xFF)
1039             return FALSE; /* not a valid octet */
1040         eth->addr[i] = (guint8) num;
1041         cp = p;     /* skip past the number */
1042
1043         /* OK, what character terminated the octet? */
1044         if (*cp == '/') {
1045             /* "/" - this has a mask. */
1046             if (!manuf_file) {
1047                 /* Entries with masks are allowed only in the "manuf" files. */
1048                 return FALSE;
1049             }
1050             cp++; /* skip past the '/' to get to the mask */
1051             if (!g_ascii_isdigit(*cp))
1052                 return FALSE;   /* no sign allowed */
1053             num = strtoul(cp, &p, 10);
1054             if (p == cp)
1055                 return FALSE;   /* failed */
1056             cp = p;   /* skip past the number */
1057             if (*cp != '\0' && !g_ascii_isspace(*cp))
1058                 return FALSE;   /* bogus terminator */
1059             if (num == 0 || num >= 48)
1060                 return FALSE;   /* bogus mask */
1061             /* Mask out the bits not covered by the mask */
1062             *mask = (int)num;
1063             for (i = 0; num >= 8; i++, num -= 8)
1064                 ;   /* skip octets entirely covered by the mask */
1065             /* Mask out the first masked octet */
1066             eth->addr[i] &= (0xFF << (8 - num));
1067             i++;
1068             /* Mask out completely-masked-out octets */
1069             for (; i < 6; i++)
1070                 eth->addr[i] = 0;
1071             return TRUE;
1072         }
1073         if (*cp == '\0') {
1074             /* We're at the end of the address, and there's no mask. */
1075             if (i == 2) {
1076                 /* We got 3 bytes, so this is a manufacturer ID. */
1077                 if (!manuf_file) {
1078                     /* Manufacturer IDs are only allowed in the "manuf"
1079                        files. */
1080                     return FALSE;
1081                 }
1082                 /* Indicate that this is a manufacturer ID (0 is not allowed
1083                    as a mask). */
1084                 *mask = 0;
1085                 return TRUE;
1086             }
1087
1088             if (i == 5) {
1089                 /* We got 6 bytes, so this is a MAC address.
1090                    If we're reading one of the "manuf" files, indicate that
1091                    this is a MAC address (48 is not allowed as a mask). */
1092                 if (manuf_file)
1093                     *mask = 48;
1094                 return TRUE;
1095             }
1096
1097             /* We didn't get 3 or 6 bytes, and there's no mask; this is
1098                illegal. */
1099             return FALSE;
1100         } else {
1101             if (sep == '\0') {
1102                 /* We don't know the separator used in this number; it can either
1103                    be ':', '-', or '.'. */
1104                 if (*cp != ':' && *cp != '-' && *cp != '.')
1105                     return FALSE;
1106                 sep = *cp;  /* subsequent separators must be the same */
1107             } else {
1108                 /* It has to be the same as the first separator */
1109                 if (*cp != sep)
1110                     return FALSE;
1111             }
1112         }
1113         cp++;
1114     }
1115
1116     return TRUE;
1117 }
1118
1119 static int
1120 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1121         const gboolean manuf_file)
1122 {
1123     /*
1124      *  See the ethers(4) or ethers(5) man page for ethers file format
1125      *  (not available on all systems).
1126      *  We allow both ethernet address separators (':' and '-'),
1127      *  as well as Wireshark's '.' separator.
1128      */
1129
1130     gchar *cp;
1131
1132     if ((cp = strchr(line, '#')))
1133         *cp = '\0';
1134
1135     if ((cp = strtok(line, " \t")) == NULL)
1136         return -1;
1137
1138     if (!parse_ether_address(cp, eth, mask, manuf_file))
1139         return -1;
1140
1141     if ((cp = strtok(NULL, " \t")) == NULL)
1142         return -1;
1143
1144     g_strlcpy(eth->name, cp, MAXNAMELEN);
1145
1146     return 0;
1147
1148 } /* parse_ether_line */
1149
1150 static FILE *eth_p = NULL;
1151
1152 static void
1153 set_ethent(char *path)
1154 {
1155     if (eth_p)
1156         rewind(eth_p);
1157     else
1158         eth_p = ws_fopen(path, "r");
1159 }
1160
1161 static void
1162 end_ethent(void)
1163 {
1164     if (eth_p) {
1165         fclose(eth_p);
1166         eth_p = NULL;
1167     }
1168 }
1169
1170 static ether_t *
1171 get_ethent(unsigned int *mask, const gboolean manuf_file)
1172 {
1173
1174     static ether_t eth;
1175     static int     size = 0;
1176     static char   *buf = NULL;
1177
1178     if (eth_p == NULL)
1179         return NULL;
1180
1181     while (fgetline(&buf, &size, eth_p) >= 0) {
1182         if (parse_ether_line(buf, &eth, mask, manuf_file) == 0) {
1183             return &eth;
1184         }
1185     }
1186
1187     return NULL;
1188
1189 } /* get_ethent */
1190
1191 #if 0
1192 static ether_t *
1193 get_ethbyname(const gchar *name)
1194 {
1195     ether_t *eth;
1196
1197     set_ethent(g_pethers_path);
1198
1199     while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1200         ;
1201
1202     if (eth == NULL) {
1203         end_ethent();
1204
1205         set_ethent(g_ethers_path);
1206
1207         while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1208             ;
1209
1210         end_ethent();
1211     }
1212
1213     return eth;
1214
1215 } /* get_ethbyname */
1216 #endif
1217
1218 static ether_t *
1219 get_ethbyaddr(const guint8 *addr)
1220 {
1221
1222     ether_t *eth;
1223
1224     set_ethent(g_pethers_path);
1225
1226     while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1227         ;
1228
1229     if (eth == NULL) {
1230         end_ethent();
1231
1232         set_ethent(g_ethers_path);
1233
1234         while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1235             ;
1236
1237         end_ethent();
1238     }
1239
1240     return eth;
1241
1242 } /* get_ethbyaddr */
1243
1244 static hashmanuf_t *manuf_hash_new_entry(const guint8 *addr, char* name)
1245 {
1246     int    *manuf_key;
1247     hashmanuf_t *manuf_value;
1248     char *endp;
1249
1250     /* manuf needs only the 3 most significant octets of the ethernet address */
1251     manuf_key = (int *)g_new(int, 1);
1252     *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1253     manuf_value = g_new(hashmanuf_t, 1);
1254
1255     memcpy(manuf_value->addr, addr, 3);
1256     manuf_value->status = (name != NULL) ? HASHETHER_STATUS_RESOLVED_NAME : HASHETHER_STATUS_UNRESOLVED;
1257     if (name != NULL) {
1258         g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1259         manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1260     }
1261     else {
1262         manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1263         manuf_value->resolved_name[0] = '\0';
1264     }
1265     /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1266     endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1267     *endp = '\0';
1268
1269     g_hash_table_insert(manuf_hashtable, manuf_key, manuf_value);
1270     return manuf_value;
1271 }
1272
1273 static void
1274 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1275 {
1276     guint8 *wka_key;
1277
1278     /*
1279      * XXX - can we use Standard Annotation Language annotations to
1280      * note that mask, as returned by parse_ethe)r_address() (and thus
1281      * by the routines that call it, and thus passed to us) cannot be > 48,
1282      * or is SAL too weak to express that?
1283      */
1284     if (mask >= 48) {
1285         /* This is a well-known MAC address; just add this to the Ethernet
1286            hash table */
1287         add_eth_name(addr, name);
1288         return;
1289     }
1290
1291     if (mask == 0) {
1292         /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1293         manuf_hash_new_entry(addr, name);
1294         return;
1295     } /* mask == 0 */
1296
1297     /* This is a range of well-known addresses; add it to the appropriate
1298        well-known-address table, creating that table if necessary. */
1299
1300     wka_key = (guint8 *)g_malloc(6);
1301     memcpy(wka_key, addr, 6);
1302
1303     g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
1304
1305 } /* add_manuf_name */
1306
1307 static hashmanuf_t *
1308 manuf_name_lookup(const guint8 *addr)
1309 {
1310     gint32       manuf_key = 0;
1311     guint8       oct;
1312     hashmanuf_t  *manuf_value;
1313
1314     /* manuf needs only the 3 most significant octets of the ethernet address */
1315     manuf_key = addr[0];
1316     manuf_key = manuf_key<<8;
1317     oct = addr[1];
1318     manuf_key = manuf_key | oct;
1319     manuf_key = manuf_key<<8;
1320     oct = addr[2];
1321     manuf_key = manuf_key | oct;
1322
1323
1324     /* first try to find a "perfect match" */
1325     manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1326     if (manuf_value != NULL) {
1327         return manuf_value;
1328     }
1329
1330     /* Mask out the broadcast/multicast flag but not the locally
1331      * administered flag as localy administered means: not assigend
1332      * by the IEEE but the local administrator instead.
1333      * 0x01 multicast / broadcast bit
1334      * 0x02 locally administered bit */
1335     if ((manuf_key & 0x00010000) != 0) {
1336         manuf_key &= 0x00FEFFFF;
1337         manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1338         if (manuf_value != NULL) {
1339             return manuf_value;
1340         }
1341     }
1342
1343     /* Add the address as a hex string */
1344     return manuf_hash_new_entry(addr, NULL);
1345
1346 } /* manuf_name_lookup */
1347
1348 static gchar *
1349 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1350 {
1351     guint8     masked_addr[6];
1352     guint      num;
1353     gint       i;
1354     gchar     *name;
1355
1356     if (wka_hashtable == NULL) {
1357         return NULL;
1358     }
1359     /* Get the part of the address covered by the mask. */
1360     for (i = 0, num = mask; num >= 8; i++, num -= 8)
1361         masked_addr[i] = addr[i];   /* copy octets entirely covered by the mask */
1362     /* Mask out the first masked octet */
1363     masked_addr[i] = addr[i] & (0xFF << (8 - num));
1364     i++;
1365     /* Zero out completely-masked-out octets */
1366     for (; i < 6; i++)
1367         masked_addr[i] = 0;
1368
1369     name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
1370
1371     return name;
1372
1373 } /* wka_name_lookup */
1374
1375
1376 guint get_hash_ether_status(hashether_t* ether)
1377 {
1378     return ether->status;
1379 }
1380
1381 char* get_hash_ether_hexaddr(hashether_t* ether)
1382 {
1383     return ether->hexaddr;
1384 }
1385
1386 char* get_hash_ether_resolved_name(hashether_t* ether)
1387 {
1388     return ether->resolved_name;
1389 }
1390
1391 static guint
1392 eth_addr_hash(gconstpointer key)
1393 {
1394     return wmem_strong_hash((const guint8 *)key, 6);
1395 }
1396
1397 static gboolean
1398 eth_addr_cmp(gconstpointer a, gconstpointer b)
1399 {
1400     return (memcmp(a, b, 6) == 0);
1401 }
1402
1403 static void
1404 initialize_ethers(void)
1405 {
1406     ether_t *eth;
1407     char    *manuf_path;
1408     guint    mask = 0;
1409
1410     /* hash table initialization */
1411     wka_hashtable   = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, g_free, g_free);
1412     manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
1413     eth_hashtable   = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, NULL, g_free);
1414
1415     /* Compute the pathname of the ethers file. */
1416     if (g_ethers_path == NULL) {
1417         g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1418                 get_systemfile_dir(), ENAME_ETHERS);
1419     }
1420
1421     /* Set g_pethers_path here, but don't actually do anything
1422      * with it. It's used in get_ethbyname() and get_ethbyaddr()
1423      */
1424     if (g_pethers_path == NULL)
1425         g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1426
1427     /* Compute the pathname of the manuf file */
1428     manuf_path = get_datafile_path(ENAME_MANUF);
1429
1430     /* Read it and initialize the hash table */
1431     set_ethent(manuf_path);
1432
1433     while ((eth = get_ethent(&mask, TRUE))) {
1434         add_manuf_name(eth->addr, mask, eth->name);
1435     }
1436
1437     end_ethent();
1438
1439     g_free(manuf_path);
1440
1441 } /* initialize_ethers */
1442
1443 /* this is only needed when shuting down application (if at all) */
1444 static void
1445 eth_name_lookup_cleanup(void)
1446 {
1447
1448     if (manuf_hashtable) {
1449         g_hash_table_destroy(manuf_hashtable);
1450         manuf_hashtable = NULL;
1451     }
1452     if (wka_hashtable) {
1453         g_hash_table_destroy(wka_hashtable);
1454         wka_hashtable = NULL;
1455     }
1456
1457     if (eth_hashtable) {
1458         g_hash_table_destroy(eth_hashtable);
1459         eth_hashtable = NULL;
1460     }
1461
1462 }
1463
1464 /* Resolve ethernet address */
1465 static hashether_t *
1466 eth_addr_resolve(hashether_t *tp) {
1467     ether_t      *eth;
1468     hashmanuf_t *manuf_value;
1469     const guint8 *addr = tp->addr;
1470
1471     if ( (eth = get_ethbyaddr(addr)) != NULL) {
1472         g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1473         tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1474         return tp;
1475     } else {
1476         guint         mask;
1477         gchar        *name;
1478         address       ether_addr;
1479
1480         /* Unknown name.  Try looking for it in the well-known-address
1481            tables for well-known address ranges smaller than 2^24. */
1482         mask = 7;
1483         for (;;) {
1484             /* Only the topmost 5 bytes participate fully */
1485             if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1486                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1487                         name, addr[5] & (0xFF >> mask));
1488                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1489                 return tp;
1490             }
1491             if (mask == 0)
1492                 break;
1493             mask--;
1494         }
1495
1496         mask = 7;
1497         for (;;) {
1498             /* Only the topmost 4 bytes participate fully */
1499             if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1500                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1501                         name, addr[4] & (0xFF >> mask), addr[5]);
1502                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1503                 return tp;
1504             }
1505             if (mask == 0)
1506                 break;
1507             mask--;
1508         }
1509
1510         mask = 7;
1511         for (;;) {
1512             /* Only the topmost 3 bytes participate fully */
1513             if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1514                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1515                         name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1516                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1517                 return tp;
1518             }
1519             if (mask == 0)
1520                 break;
1521             mask--;
1522         }
1523
1524         /* Now try looking in the manufacturer table. */
1525         manuf_value = manuf_name_lookup(addr);
1526         if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1527             g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1528                     manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1529             tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1530             return tp;
1531         }
1532
1533         /* Now try looking for it in the well-known-address
1534            tables for well-known address ranges larger than 2^24. */
1535         mask = 7;
1536         for (;;) {
1537             /* Only the topmost 2 bytes participate fully */
1538             if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1539                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1540                         name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1541                         addr[5]);
1542                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1543                 return tp;
1544             }
1545             if (mask == 0)
1546                 break;
1547             mask--;
1548         }
1549
1550         mask = 7;
1551         for (;;) {
1552             /* Only the topmost byte participates fully */
1553             if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1554                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1555                         name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1556                         addr[4], addr[5]);
1557                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1558                 return tp;
1559             }
1560             if (mask == 0)
1561                 break;
1562             mask--;
1563         }
1564
1565         for (mask = 7; mask > 0; mask--) {
1566             /* Not even the topmost byte participates fully */
1567             if ((name = wka_name_lookup(addr, mask)) != NULL) {
1568                 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1569                         name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1570                         addr[3], addr[4], addr[5]);
1571                 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1572                 return tp;
1573             }
1574         }
1575
1576         /* No match whatsoever. */
1577         set_address(&ether_addr, AT_ETHER, 6, addr);
1578         address_to_str_buf(&ether_addr, tp->resolved_name, MAXNAMELEN);
1579         tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1580         return tp;
1581     }
1582     g_assert_not_reached();
1583 } /* eth_addr_resolve */
1584
1585 static hashether_t *
1586 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1587 {
1588     hashether_t *tp;
1589     char *endp;
1590
1591     tp = g_new(hashether_t, 1);
1592     memcpy(tp->addr, addr, sizeof(tp->addr));
1593     tp->status = HASHETHER_STATUS_UNRESOLVED;
1594     /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1595     endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1596     *endp = '\0';
1597     tp->resolved_name[0] = '\0';
1598
1599     if (resolve)
1600         eth_addr_resolve(tp);
1601
1602     g_hash_table_insert(eth_hashtable, tp->addr, tp);
1603
1604     return tp;
1605 } /* eth_hash_new_entry */
1606
1607 static hashether_t *
1608 add_eth_name(const guint8 *addr, const gchar *name)
1609 {
1610     hashether_t *tp;
1611
1612     tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1613
1614     if (tp == NULL) {
1615         tp = eth_hash_new_entry(addr, FALSE);
1616     }
1617
1618     g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1619     tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1620     new_resolved_objects = TRUE;
1621
1622     return tp;
1623 } /* add_eth_name */
1624
1625 static hashether_t *
1626 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1627 {
1628     hashether_t  *tp;
1629
1630     tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1631     if (tp == NULL) {
1632         tp = eth_hash_new_entry(addr, resolve);
1633     } else {
1634         if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1635             eth_addr_resolve(tp); /* Found but needs to be resolved */
1636         }
1637     }
1638
1639     return tp;
1640
1641 } /* eth_name_lookup */
1642
1643 static guint8 *
1644 eth_addr_lookup(const gchar *name _U_)
1645 {
1646 #if 0
1647     /* XXX Do we need reverse lookup??? */
1648     ether_t      *eth;
1649     hashether_t  *tp;
1650     hashether_t **table = eth_table;
1651     gint          i;
1652
1653     /* to be optimized (hash table from name to addr) */
1654     for (i = 0; i < HASHETHSIZE; i++) {
1655         tp = table[i];
1656         while (tp) {
1657             if (strcmp(tp->resolved_name, name) == 0)
1658                 return tp->addr;
1659             tp = tp->next;
1660         }
1661     }
1662
1663     /* not in hash table : performs a file lookup */
1664
1665     if ((eth = get_ethbyname(name)) == NULL)
1666         return NULL;
1667
1668     /* add new entry in hash table */
1669
1670     tp = add_eth_name(eth->addr, name);
1671
1672     return tp->addr;
1673 #endif
1674     return NULL;
1675
1676 } /* eth_addr_lookup */
1677
1678
1679 /* IPXNETS */
1680 static int
1681 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1682 {
1683     /*
1684      *  We allow three address separators (':', '-', and '.'),
1685      *  as well as no separators
1686      */
1687
1688     gchar     *cp;
1689     guint32   a, a0, a1, a2, a3;
1690     gboolean  found_single_number = FALSE;
1691
1692     if ((cp = strchr(line, '#')))
1693         *cp = '\0';
1694
1695     if ((cp = strtok(line, " \t\n")) == NULL)
1696         return -1;
1697
1698     /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1699      * fill a and found_single_number is TRUE,
1700      * or return -1
1701      */
1702     if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1703         if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1704             if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1705                 if (sscanf(cp, "%x", &a) == 1) {
1706                     found_single_number = TRUE;
1707                 }
1708                 else {
1709                     return -1;
1710                 }
1711             }
1712         }
1713     }
1714
1715     if ((cp = strtok(NULL, " \t\n")) == NULL)
1716         return -1;
1717
1718     if (found_single_number) {
1719         ipxnet->addr = a;
1720     }
1721     else {
1722         ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1723     }
1724
1725     g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1726
1727     return 0;
1728
1729 } /* parse_ipxnets_line */
1730
1731 static FILE *ipxnet_p = NULL;
1732
1733 static void
1734 set_ipxnetent(char *path)
1735 {
1736     if (ipxnet_p)
1737         rewind(ipxnet_p);
1738     else
1739         ipxnet_p = ws_fopen(path, "r");
1740 }
1741
1742 static void
1743 end_ipxnetent(void)
1744 {
1745     if (ipxnet_p) {
1746         fclose(ipxnet_p);
1747         ipxnet_p = NULL;
1748     }
1749 }
1750
1751 static ipxnet_t *
1752 get_ipxnetent(void)
1753 {
1754
1755     static ipxnet_t ipxnet;
1756     static int     size = 0;
1757     static char   *buf = NULL;
1758
1759     if (ipxnet_p == NULL)
1760         return NULL;
1761
1762     while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1763         if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1764             return &ipxnet;
1765         }
1766     }
1767
1768     return NULL;
1769
1770 } /* get_ipxnetent */
1771
1772 /* Unused ??? */
1773 #if 0
1774 static ipxnet_t *
1775 get_ipxnetbyname(const gchar *name)
1776 {
1777     ipxnet_t *ipxnet;
1778
1779     set_ipxnetent(g_ipxnets_path);
1780
1781     while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1782         ;
1783
1784     if (ipxnet == NULL) {
1785         end_ipxnetent();
1786
1787         set_ipxnetent(g_pipxnets_path);
1788
1789         while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1790             ;
1791
1792         end_ipxnetent();
1793     }
1794
1795     return ipxnet;
1796
1797 } /* get_ipxnetbyname */
1798 #endif
1799
1800 static ipxnet_t *
1801 get_ipxnetbyaddr(guint32 addr)
1802 {
1803     ipxnet_t *ipxnet;
1804
1805     set_ipxnetent(g_ipxnets_path);
1806
1807     while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1808
1809     if (ipxnet == NULL) {
1810         end_ipxnetent();
1811
1812         set_ipxnetent(g_pipxnets_path);
1813
1814         while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1815             ;
1816
1817         end_ipxnetent();
1818     }
1819
1820     return ipxnet;
1821
1822 } /* get_ipxnetbyaddr */
1823
1824 static void
1825 initialize_ipxnets(void)
1826 {
1827     /* Compute the pathname of the ipxnets file.
1828      *
1829      * XXX - is there a notion of an "ipxnets file" in any flavor of
1830      * UNIX, or with any add-on Netware package for UNIX?  If not,
1831      * should the UNIX version of the ipxnets file be in the datafile
1832      * directory as well?
1833      */
1834     if (g_ipxnets_path == NULL) {
1835         g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1836                 get_systemfile_dir(), ENAME_IPXNETS);
1837     }
1838
1839     /* Set g_pipxnets_path here, but don't actually do anything
1840      * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1841      */
1842     if (g_pipxnets_path == NULL)
1843         g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1844
1845 } /* initialize_ipxnets */
1846
1847 static void
1848 ipx_name_lookup_cleanup(void)
1849 {
1850     if (ipxnet_hash_table) {
1851         g_hash_table_destroy(ipxnet_hash_table);
1852         ipxnet_hash_table = NULL;
1853     }
1854
1855 }
1856
1857 #if 0
1858 static hashipxnet_t *
1859 add_ipxnet_name(guint addr, const gchar *name)
1860 {
1861     hashipxnet_t *tp;
1862
1863     tp = (hashipxnet_t   *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1864     if (tp) {
1865         g_strlcpy(tp->name, name, MAXNAMELEN);
1866     } else {
1867         int *key;
1868
1869         key = (int *)g_new(int, 1);
1870         *key = addr;
1871         tp = g_new(hashipxnet_t,1);
1872         g_strlcpy(tp->name, name, MAXNAMELEN);
1873         g_hash_table_insert(ipxnet_hash_table, key, tp);
1874     }
1875
1876     tp->addr = addr;
1877     g_strlcpy(tp->name, name, MAXNAMELEN);
1878     tp->next = NULL;
1879     new_resolved_objects = TRUE;
1880
1881     return tp;
1882
1883 } /* add_ipxnet_name */
1884 #endif
1885
1886 static gchar *
1887 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1888 {
1889     hashipxnet_t *tp;
1890     ipxnet_t *ipxnet;
1891
1892     tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1893     if (tp == NULL) {
1894         int *key;
1895
1896         key = (int *)g_new(int, 1);
1897         *key = addr;
1898         tp = g_new(hashipxnet_t, 1);
1899         g_hash_table_insert(ipxnet_hash_table, key, tp);
1900     } else {
1901         return wmem_strdup(allocator, tp->name);
1902     }
1903
1904     /* fill in a new entry */
1905
1906     tp->addr = addr;
1907
1908     if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1909         /* unknown name */
1910         g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1911
1912     } else {
1913         g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1914     }
1915
1916     return wmem_strdup(allocator, tp->name);
1917
1918 } /* ipxnet_name_lookup */
1919
1920 static guint
1921 ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
1922 {
1923     *success = FALSE;
1924     return 0;
1925 #if 0
1926     /* XXX Do we need reverse lookup??? */
1927     ipxnet_t *ipxnet;
1928     hashipxnet_t *tp;
1929     hashipxnet_t **table = ipxnet_table;
1930     int i;
1931
1932     /* to be optimized (hash table from name to addr) */
1933     for (i = 0; i < HASHIPXNETSIZE; i++) {
1934         tp = table[i];
1935         while (tp) {
1936             if (strcmp(tp->name, name) == 0) {
1937                 *success = TRUE;
1938                 return tp->addr;
1939             }
1940             tp = tp->next;
1941         }
1942     }
1943
1944     /* not in hash table : performs a file lookup */
1945
1946     if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1947         *success = FALSE;
1948         return 0;
1949     }
1950
1951     /* add new entry in hash table */
1952
1953     tp = add_ipxnet_name(ipxnet->addr, name);
1954
1955     *success = TRUE;
1956     return tp->addr;
1957 #endif
1958 } /* ipxnet_addr_lookup */
1959
1960 static gboolean
1961 read_hosts_file (const char *hostspath, gboolean store_entries)
1962 {
1963     FILE *hf;
1964     char *line = NULL;
1965     int size = 0;
1966     gchar *cp;
1967     guint32 host_addr[4]; /* IPv4 or IPv6 */
1968     struct e_in6_addr ip6_addr;
1969     gboolean is_ipv6, entry_found = FALSE;
1970     int ret;
1971
1972     /*
1973      *  See the hosts(4) or hosts(5) man page for hosts file format
1974      *  (not available on all systems).
1975      */
1976     if ((hf = ws_fopen(hostspath, "r")) == NULL)
1977         return FALSE;
1978
1979     while (fgetline(&line, &size, hf) >= 0) {
1980         if ((cp = strchr(line, '#')))
1981             *cp = '\0';
1982
1983         if ((cp = strtok(line, " \t")) == NULL)
1984             continue; /* no tokens in the line */
1985
1986         ret = inet_pton(AF_INET6, cp, &host_addr);
1987         if (ret < 0)
1988             continue; /* error parsing */
1989         if (ret > 0) {
1990             /* Valid IPv6 */
1991             is_ipv6 = TRUE;
1992         } else {
1993             /* Not valid IPv6 - valid IPv4? */
1994             if (!str_to_ip(cp, &host_addr))
1995                 continue; /* no */
1996             is_ipv6 = FALSE;
1997         }
1998
1999         if ((cp = strtok(NULL, " \t")) == NULL)
2000             continue; /* no host name */
2001
2002         entry_found = TRUE;
2003         if (store_entries) {
2004             if (is_ipv6) {
2005                 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2006                 add_ipv6_name(&ip6_addr, cp);
2007             } else
2008                 add_ipv4_name(host_addr[0], cp);
2009
2010 #if 0
2011             /*
2012              * Add the aliases, too, if there are any.
2013              * XXX - except we only store the last one added.  The name
2014              * resolver returns the first name in the hosts file, we should
2015              * too.
2016              */
2017             while ((cp = strtok(NULL, " \t")) != NULL) {
2018                 if (is_ipv6) {
2019                     memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2020                     add_ipv6_name(&ip6_addr, cp);
2021                 } else
2022                     add_ipv4_name(host_addr[0], cp);
2023             }
2024 #endif
2025         }
2026     }
2027     g_free(line);
2028
2029     fclose(hf);
2030     return entry_found ? TRUE : FALSE;
2031 } /* read_hosts_file */
2032
2033 gboolean
2034 add_hosts_file (const char *hosts_file)
2035 {
2036     gboolean found = FALSE;
2037     guint i;
2038
2039     if (!hosts_file)
2040         return FALSE;
2041
2042     if (!extra_hosts_files)
2043         extra_hosts_files = g_ptr_array_new();
2044
2045     for (i = 0; i < extra_hosts_files->len; i++) {
2046         if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2047             found = TRUE;
2048     }
2049
2050     if (!found) {
2051         g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
2052         return read_hosts_file (hosts_file, FALSE);
2053     }
2054     return TRUE;
2055 }
2056
2057 gboolean
2058 add_ip_name_from_string (const char *addr, const char *name)
2059 {
2060     guint32 host_addr[4]; /* IPv4 */
2061     struct e_in6_addr ip6_addr; /* IPv6 */
2062     gboolean is_ipv6;
2063     int ret;
2064     resolved_ipv4_t *resolved_ipv4_entry;
2065     resolved_ipv6_t *resolved_ipv6_entry;
2066
2067     ret = inet_pton(AF_INET6, addr, &ip6_addr);
2068     if (ret < 0)
2069         /* Error parsing address */
2070         return FALSE;
2071
2072     if (ret > 0) {
2073         /* Valid IPv6 */
2074         is_ipv6 = TRUE;
2075     } else {
2076         /* Not valid IPv6 - valid IPv4? */
2077         if (!str_to_ip(addr, &host_addr))
2078             return FALSE; /* no */
2079         is_ipv6 = FALSE;
2080     }
2081
2082     if (is_ipv6) {
2083         resolved_ipv6_entry = g_new(resolved_ipv6_t, 1);
2084         memcpy(&(resolved_ipv6_entry->ip6_addr), &ip6_addr, 16);
2085         g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2086         manually_resolved_ipv6_list = g_slist_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2087     } else {
2088         resolved_ipv4_entry = g_new(resolved_ipv4_t, 1);
2089         resolved_ipv4_entry->host_addr = host_addr[0];
2090         g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2091         manually_resolved_ipv4_list = g_slist_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2092     }
2093
2094     return TRUE;
2095 } /* add_ip_name_from_string */
2096
2097 /*
2098  * Add the resolved addresses that are in use to the list used to create the NRB
2099  */
2100 static void
2101 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2102 {
2103     addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2104     hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2105
2106     if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
2107         lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
2108     }
2109
2110 }
2111
2112 /*
2113  * Add the resolved addresses that are in use to the list used to create the NRB
2114  */
2115
2116 static void
2117 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2118 {
2119     addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2120     hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2121
2122     if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
2123         lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2124     }
2125
2126 }
2127
2128 addrinfo_lists_t *
2129 get_addrinfo_list(void) {
2130
2131     if (ipv4_hash_table) {
2132         g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2133     }
2134
2135     if (ipv6_hash_table) {
2136         g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2137     }
2138
2139     return &addrinfo_lists;
2140 }
2141
2142 /* Read in a list of subnet definition - name pairs.
2143  * <line> = <comment> | <entry> | <whitespace>
2144  * <comment> = <whitespace>#<any>
2145  * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2146  * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2147  * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2148  * <subnet_mask_length> is a decimal 1-31
2149  * <subnet_name> is a string containing no whitespace.
2150  * <whitespace> = (space | tab)+
2151  * Any malformed entries are ignored.
2152  * Any trailing data after the subnet_name is ignored.
2153  *
2154  * XXX Support IPv6
2155  */
2156 static gboolean
2157 read_subnets_file (const char *subnetspath)
2158 {
2159     FILE *hf;
2160     char *line = NULL;
2161     int size = 0;
2162     gchar *cp, *cp2;
2163     guint32 host_addr; /* IPv4 ONLY */
2164     int mask_length;
2165
2166     if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2167         return FALSE;
2168
2169     while (fgetline(&line, &size, hf) >= 0) {
2170         if ((cp = strchr(line, '#')))
2171             *cp = '\0';
2172
2173         if ((cp = strtok(line, " \t")) == NULL)
2174             continue; /* no tokens in the line */
2175
2176
2177         /* Expected format is <IP4 address>/<subnet length> */
2178         cp2 = strchr(cp, '/');
2179         if (NULL == cp2) {
2180             /* No length */
2181             continue;
2182         }
2183         *cp2 = '\0'; /* Cut token */
2184         ++cp2    ;
2185
2186         /* Check if this is a valid IPv4 address */
2187         if (!str_to_ip(cp, &host_addr)) {
2188             continue; /* no */
2189         }
2190
2191         mask_length = atoi(cp2);
2192         if (0 >= mask_length || mask_length > 32) {
2193             continue; /* invalid mask length */
2194         }
2195
2196         if ((cp = strtok(NULL, " \t")) == NULL)
2197             continue; /* no subnet name */
2198
2199         subnet_entry_set(host_addr, (guint32)mask_length, cp);
2200     }
2201     g_free(line);
2202
2203     fclose(hf);
2204     return TRUE;
2205 } /* read_subnets_file */
2206
2207 static subnet_entry_t
2208 subnet_lookup(const guint32 addr)
2209 {
2210     subnet_entry_t subnet_entry;
2211     guint32 i;
2212
2213     /* Search mask lengths linearly, longest first */
2214
2215     i = SUBNETLENGTHSIZE;
2216     while(have_subnet_entry && i > 0) {
2217         guint32 masked_addr;
2218         subnet_length_entry_t* length_entry;
2219
2220         /* Note that we run from 31 (length 32)  to 0 (length 1)  */
2221         --i;
2222         g_assert(i < SUBNETLENGTHSIZE);
2223
2224
2225         length_entry = &subnet_length_entries[i];
2226
2227         if (NULL != length_entry->subnet_addresses) {
2228             sub_net_hashipv4_t * tp;
2229             guint32 hash_idx;
2230
2231             masked_addr = addr & length_entry->mask;
2232             hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2233
2234             tp = length_entry->subnet_addresses[hash_idx];
2235             while(tp != NULL && tp->addr != masked_addr) {
2236                 tp = tp->next;
2237             }
2238
2239             if (NULL != tp) {
2240                 subnet_entry.mask = length_entry->mask;
2241                 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2242                 subnet_entry.name = tp->name;
2243                 return subnet_entry;
2244             }
2245         }
2246     }
2247
2248     subnet_entry.mask = 0;
2249     subnet_entry.mask_length = 0;
2250     subnet_entry.name = NULL;
2251
2252     return subnet_entry;
2253 }
2254
2255 /* Add a subnet-definition - name pair to the set.
2256  * The definition is taken by masking the address passed in with the mask of the
2257  * given length.
2258  */
2259 static void
2260 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2261 {
2262     subnet_length_entry_t* entry;
2263     sub_net_hashipv4_t * tp;
2264     gsize hash_idx;
2265
2266     g_assert(mask_length > 0 && mask_length <= 32);
2267
2268     entry = &subnet_length_entries[mask_length - 1];
2269
2270     subnet_addr &= entry->mask;
2271
2272     hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2273
2274     if (NULL == entry->subnet_addresses) {
2275         entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2276     }
2277
2278     if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2279         sub_net_hashipv4_t * new_tp;
2280
2281         while (tp->next) {
2282             if (tp->addr == subnet_addr) {
2283                 return; /* XXX provide warning that an address was repeated? */
2284             } else {
2285                 tp = tp->next;
2286             }
2287         }
2288
2289         new_tp = g_new(sub_net_hashipv4_t, 1);
2290         tp->next = new_tp;
2291         tp = new_tp;
2292     } else {
2293         tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
2294     }
2295
2296     tp->next = NULL;
2297     tp->addr = subnet_addr;
2298     /* Clear DUMMY_ADDRESS_ENTRY */
2299     tp->flags &= ~DUMMY_ADDRESS_ENTRY; /*Never used again...*/
2300     g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2301     have_subnet_entry = TRUE;
2302 }
2303
2304 static void
2305 subnet_name_lookup_init(void)
2306 {
2307     gchar* subnetspath;
2308     guint32 i;
2309
2310     for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2311         guint32 length = i + 1;
2312
2313         subnet_length_entries[i].subnet_addresses  = NULL;
2314         subnet_length_entries[i].mask_length  = length;
2315         subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2316     }
2317
2318     subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2319     if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2320         report_open_failure(subnetspath, errno, FALSE);
2321     }
2322     g_free(subnetspath);
2323
2324     /*
2325      * Load the global subnets file, if we have one.
2326      */
2327     subnetspath = get_datafile_path(ENAME_SUBNETS);
2328     if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2329         report_open_failure(subnetspath, errno, FALSE);
2330     }
2331     g_free(subnetspath);
2332 }
2333
2334 static void
2335 cleanup_subnet_entry(sub_net_hashipv4_t* entry)
2336 {
2337     if ((entry != NULL) && (entry->next != NULL)) {
2338         cleanup_subnet_entry(entry->next);
2339     }
2340
2341     g_free(entry);
2342 }
2343
2344 /*
2345  *  External Functions
2346  */
2347
2348 void
2349 addr_resolve_pref_init(module_t *nameres)
2350 {
2351     prefs_register_bool_preference(nameres, "mac_name",
2352             "Resolve MAC addresses",
2353             "Resolve Ethernet MAC address to manufacturer names",
2354             &gbl_resolv_flags.mac_name);
2355
2356     prefs_register_bool_preference(nameres, "transport_name",
2357             "Resolve transport names",
2358             "Resolve TCP/UDP ports into service names",
2359             &gbl_resolv_flags.transport_name);
2360
2361     prefs_register_bool_preference(nameres, "network_name",
2362             "Resolve network (IP) addresses",
2363             "Resolve IPv4, IPv6, and IPX addresses into host names."
2364             " The next set of check boxes determines how name resolution should be performed."
2365             " If no other options are checked name resolution is made from Wireshark's host file,"
2366             " capture file name resolution blocks and DNS packets in the capture.",
2367             &gbl_resolv_flags.network_name);
2368
2369     prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
2370             "Use captured DNS packet data for address resolution",
2371             "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
2372             &gbl_resolv_flags.dns_pkt_addr_resolution);
2373
2374     prefs_register_bool_preference(nameres, "use_external_name_resolver",
2375             "Use an external network name resolver",
2376             "Use your system's configured name resolver"
2377             " (usually DNS) to resolve network names."
2378             " Only applies when network name resolution"
2379             " is enabled.",
2380             &gbl_resolv_flags.use_external_net_name_resolver);
2381
2382 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
2383     prefs_register_bool_preference(nameres, "concurrent_dns",
2384             "Enable concurrent DNS name resolution",
2385             "Enable concurrent DNS name resolution. Only"
2386             " applies when network name resolution is"
2387             " enabled. You probably want to enable this.",
2388             &gbl_resolv_flags.concurrent_dns);
2389
2390     prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2391             "Maximum concurrent requests",
2392             "The maximum number of DNS requests that may"
2393             " be active at any time. A large value (many"
2394             " thousands) might overload the network or make"
2395             " your DNS server behave badly.",
2396             10,
2397             &name_resolve_concurrency);
2398 #else
2399     prefs_register_static_text_preference(nameres, "concurrent_dns",
2400             "Enable concurrent DNS name resolution: N/A",
2401             "Support for concurrent DNS name resolution was not"
2402             " compiled into this version of Wireshark");
2403 #endif
2404
2405     prefs_register_bool_preference(nameres, "hosts_file_handling",
2406             "Only use the profile \"hosts\" file",
2407             "By default \"hosts\" files will be loaded from multiple sources."
2408             " Checking this box only loads the \"hosts\" in the current profile.",
2409             &gbl_resolv_flags.load_hosts_file_from_profile_only);
2410
2411 }
2412
2413 void
2414 disable_name_resolution(void) {
2415     gbl_resolv_flags.mac_name                           = FALSE;
2416     gbl_resolv_flags.network_name                       = FALSE;
2417     gbl_resolv_flags.transport_name                     = FALSE;
2418     gbl_resolv_flags.concurrent_dns                     = FALSE;
2419     gbl_resolv_flags.dns_pkt_addr_resolution            = FALSE;
2420     gbl_resolv_flags.use_external_net_name_resolver     = FALSE;
2421 }
2422
2423 #ifdef HAVE_C_ARES
2424 gboolean
2425 host_name_lookup_process(void) {
2426     async_dns_queue_msg_t *caqm;
2427     struct timeval tv = { 0, 0 };
2428     int nfds;
2429     fd_set rfds, wfds;
2430     gboolean nro = new_resolved_objects;
2431
2432     new_resolved_objects = FALSE;
2433
2434     if (!async_dns_initialized)
2435         /* c-ares not initialized. Bail out and cancel timers. */
2436         return nro;
2437
2438     async_dns_queue_head = g_list_first(async_dns_queue_head);
2439
2440     while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2441         caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2442         async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2443         if (caqm->family == AF_INET) {
2444             ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2445                     c_ares_ghba_cb, caqm);
2446             async_dns_in_flight++;
2447         } else if (caqm->family == AF_INET6) {
2448             ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2449                     AF_INET6, c_ares_ghba_cb, caqm);
2450             async_dns_in_flight++;
2451         }
2452     }
2453
2454     FD_ZERO(&rfds);
2455     FD_ZERO(&wfds);
2456     nfds = ares_fds(ghba_chan, &rfds, &wfds);
2457     if (nfds > 0) {
2458         if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2459             fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2460             return nro;
2461         }
2462         ares_process(ghba_chan, &rfds, &wfds);
2463     }
2464
2465     /* Any new entries? */
2466     return nro;
2467 }
2468
2469 static void
2470 _host_name_lookup_cleanup(void) {
2471     GList *cur;
2472
2473     cur = g_list_first(async_dns_queue_head);
2474     while (cur) {
2475         g_free(cur->data);
2476         cur = g_list_next (cur);
2477     }
2478
2479     g_list_free(async_dns_queue_head);
2480     async_dns_queue_head = NULL;
2481
2482     if (async_dns_initialized) {
2483         ares_destroy(ghba_chan);
2484         ares_destroy(ghbn_chan);
2485     }
2486 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2487     ares_library_cleanup();
2488 #endif
2489     async_dns_initialized = FALSE;
2490 }
2491
2492 #elif defined(HAVE_GNU_ADNS)
2493
2494 /* XXX - The ADNS "documentation" isn't very clear:
2495  * - Do we need to keep our query structures around?
2496  */
2497 gboolean
2498 host_name_lookup_process(void) {
2499     async_dns_queue_msg_t *almsg;
2500     GList *cur;
2501     char addr_str[] = "111.222.333.444.in-addr.arpa.";
2502     guint8 *addr_bytes;
2503     adns_answer *ans;
2504     int ret;
2505     gboolean dequeue;
2506     gboolean nro = new_resolved_objects;
2507
2508     new_resolved_objects = FALSE;
2509     async_dns_queue_head = g_list_first(async_dns_queue_head);
2510
2511     cur = async_dns_queue_head;
2512     while (cur &&  async_dns_in_flight <= name_resolve_concurrency) {
2513         almsg = (async_dns_queue_msg_t *) cur->data;
2514         if (! almsg->submitted && almsg->type == AF_INET) {
2515             addr_bytes = (guint8 *) &almsg->ip4_addr;
2516             g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2517                     addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2518             /* XXX - what if it fails? */
2519             adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
2520             almsg->submitted = TRUE;
2521             async_dns_in_flight++;
2522         }
2523         cur = cur->next;
2524     }
2525
2526     cur = async_dns_queue_head;
2527     while (cur) {
2528         dequeue = FALSE;
2529         almsg = (async_dns_queue_msg_t *) cur->data;
2530         if (almsg->submitted) {
2531             ret = adns_check(ads, &almsg->query, &ans, NULL);
2532             if (ret == 0) {
2533                 if (ans->status == adns_s_ok) {
2534                     add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2535                 }
2536                 dequeue = TRUE;
2537             }
2538         }
2539         cur = cur->next;
2540         if (dequeue) {
2541             async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2542             g_free(almsg);
2543             /* XXX, what to do if async_dns_in_flight == 0? */
2544             async_dns_in_flight--;
2545         }
2546     }
2547
2548     /* Keep the timeout in place */
2549     return nro;
2550 }
2551
2552 static void
2553 _host_name_lookup_cleanup(void) {
2554     void *qdata;
2555
2556     async_dns_queue_head = g_list_first(async_dns_queue_head);
2557     while (async_dns_queue_head) {
2558         qdata = async_dns_queue_head->data;
2559         async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2560         g_free(qdata);
2561     }
2562
2563     if (async_dns_initialized)
2564         adns_finish(ads);
2565     async_dns_initialized = FALSE;
2566 }
2567
2568 #else /* HAVE_GNU_ADNS */
2569
2570 gboolean
2571 host_name_lookup_process(void) {
2572     gboolean nro = new_resolved_objects;
2573
2574     new_resolved_objects = FALSE;
2575
2576     return nro;
2577 }
2578
2579 static void
2580 _host_name_lookup_cleanup(void) {
2581 }
2582
2583 #endif /* HAVE_C_ARES */
2584
2585 const gchar *
2586 get_hostname(const guint addr)
2587 {
2588     gboolean found;
2589
2590     /* XXX why do we call this if we're not resolving? To create hash entries?
2591      * Why?
2592      */
2593     hashipv4_t *tp = host_lookup(addr, &found);
2594
2595     if (!gbl_resolv_flags.network_name)
2596         return tp->ip;
2597
2598     tp->flags |= RESOLVED_ADDRESS_USED;
2599
2600     return tp->name;
2601 }
2602
2603 /* -------------------------- */
2604
2605 const gchar *
2606 get_hostname6(const struct e_in6_addr *addr)
2607 {
2608     gboolean found;
2609
2610     /* XXX why do we call this if we're not resolving? To create hash entries?
2611      * Why?
2612      */
2613     hashipv6_t *tp = host_lookup6(addr, &found);
2614
2615     if (!gbl_resolv_flags.network_name)
2616         return tp->ip6;
2617
2618     tp->flags |= RESOLVED_ADDRESS_USED;
2619
2620     return tp->name;
2621 }
2622
2623 /* -------------------------- */
2624 void
2625 add_ipv4_name(const guint addr, const gchar *name)
2626 {
2627     hashipv4_t *tp;
2628
2629     /*
2630      * Don't add zero-length names; apparently, some resolvers will return
2631      * them if they get them from DNS.
2632      */
2633     if (!name || name[0] == '\0')
2634         return;
2635
2636
2637     tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2638     if (!tp) {
2639         tp = new_ipv4(addr);
2640         g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2641     }
2642
2643     if (g_ascii_strcasecmp(tp->name, name)) {
2644         g_strlcpy(tp->name, name, MAXNAMELEN);
2645         new_resolved_objects = TRUE;
2646     }
2647     tp->flags |= TRIED_RESOLVE_ADDRESS;
2648
2649 } /* add_ipv4_name */
2650
2651 /* -------------------------- */
2652 void
2653 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2654 {
2655     hashipv6_t *tp;
2656
2657     /*
2658      * Don't add zero-length names; apparently, some resolvers will return
2659      * them if they get them from DNS.
2660      */
2661     if (!name || name[0] == '\0')
2662         return;
2663
2664     tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
2665     if (!tp) {
2666         struct e_in6_addr *addr_key;
2667
2668         addr_key = g_new(struct e_in6_addr,1);
2669         tp = new_ipv6(addrp);
2670         memcpy(addr_key, addrp, 16);
2671         g_hash_table_insert(ipv6_hash_table, addr_key, tp);
2672     }
2673
2674     if (g_ascii_strcasecmp(tp->name, name)) {
2675         g_strlcpy(tp->name, name, MAXNAMELEN);
2676         new_resolved_objects = TRUE;
2677     }
2678     tp->flags |= TRIED_RESOLVE_ADDRESS;
2679
2680 } /* add_ipv6_name */
2681
2682 static void
2683 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2684 {
2685     resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2686
2687     add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2688 }
2689
2690 static void
2691 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2692 {
2693     resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2694
2695     add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2696 }
2697
2698 static void
2699 add_manually_resolved(void)
2700 {
2701     if (manually_resolved_ipv4_list) {
2702         g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2703     }
2704
2705     if (manually_resolved_ipv6_list) {
2706         g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2707     }
2708 }
2709
2710 void
2711 host_name_lookup_init(void)
2712 {
2713     char *hostspath;
2714     guint i;
2715
2716 #ifdef HAVE_GNU_ADNS
2717 #ifdef _WIN32
2718     char *sysroot;
2719     static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2720     static char rootpath_ot[] = "\\hosts";
2721 #endif /* _WIN32 */
2722 #endif /*GNU_ADNS */
2723
2724     g_assert(ipxnet_hash_table == NULL);
2725     ipxnet_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
2726
2727     g_assert(ipv4_hash_table == NULL);
2728     ipv4_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
2729
2730     g_assert(ipv6_hash_table == NULL);
2731     ipv6_hash_table = g_hash_table_new_full(ipv6_oat_hash, ipv6_equal, g_free, g_free);
2732
2733     /*
2734      * Load the global hosts file, if we have one.
2735      */
2736     if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2737         hostspath = get_datafile_path(ENAME_HOSTS);
2738         if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2739             report_open_failure(hostspath, errno, FALSE);
2740         }
2741         g_free(hostspath);
2742     }
2743     /*
2744      * Load the user's hosts file no matter what, if they have one.
2745      */
2746     hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2747     if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2748         report_open_failure(hostspath, errno, FALSE);
2749     }
2750     g_free(hostspath);
2751 #ifdef HAVE_C_ARES
2752     if (gbl_resolv_flags.concurrent_dns) {
2753 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2754         if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2755 #endif
2756             if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2757                 async_dns_initialized = TRUE;
2758             }
2759 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2760         }
2761 #endif
2762     }
2763 #else
2764 #ifdef HAVE_GNU_ADNS
2765     /*
2766      * We're using GNU ADNS, which doesn't check the system hosts file;
2767      * we load that file ourselves.
2768      */
2769 #ifdef _WIN32
2770
2771     sysroot = getenv_utf8("WINDIR");
2772     if (sysroot != NULL) {
2773         /*
2774          * The file should be under WINDIR.
2775          * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2776          * %WINDIR%\system32\drivers\etc\hosts.
2777          * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2778          * Try both.
2779          * XXX - should we base it on the dwPlatformId value from
2780          * GetVersionEx()?
2781          */
2782         if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2783             hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2784             if (!read_hosts_file(hostspath, TRUE)) {
2785                 g_free(hostspath);
2786                 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2787                 read_hosts_file(hostspath, TRUE);
2788             }
2789             g_free(hostspath);
2790         }
2791     }
2792 #else /* _WIN32 */
2793     if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2794         read_hosts_file("/etc/hosts", TRUE);
2795     }
2796 #endif /* _WIN32 */
2797
2798     if (gbl_resolv_flags.concurrent_dns) {
2799         /* XXX - Any flags we should be using? */
2800         /* XXX - We could provide config settings for DNS servers, and
2801            pass them to ADNS with adns_init_strcfg */
2802         if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
2803             /*
2804              * XXX - should we report the error?  I'm assuming that some crashes
2805              * reported on a Windows machine with TCP/IP not configured are due
2806              * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2807              * ADNS in a state where it crashes due to that.  We'll still try
2808              * doing name resolution anyway.
2809              */
2810             return;
2811         }
2812         async_dns_initialized = TRUE;
2813         async_dns_in_flight = 0;
2814     }
2815 #endif /* HAVE_GNU_ADNS */
2816 #endif /* HAVE_C_ARES */
2817
2818     if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
2819         for (i = 0; i < extra_hosts_files->len; i++) {
2820             read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
2821         }
2822     }
2823
2824     subnet_name_lookup_init();
2825
2826     add_manually_resolved();
2827 }
2828
2829 void
2830 host_name_lookup_cleanup(void)
2831 {
2832     guint32 i, j;
2833     _host_name_lookup_cleanup();
2834
2835     if (ipxnet_hash_table) {
2836         g_hash_table_destroy(ipxnet_hash_table);
2837         ipxnet_hash_table = NULL;
2838     }
2839
2840     if (ipv4_hash_table) {
2841         g_hash_table_destroy(ipv4_hash_table);
2842         ipv4_hash_table = NULL;
2843     }
2844
2845     if (ipv6_hash_table) {
2846         g_hash_table_destroy(ipv6_hash_table);
2847         ipv6_hash_table = NULL;
2848     }
2849
2850     for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2851         if (subnet_length_entries[i].subnet_addresses != NULL) {
2852             for (j = 0; j < HASHHOSTSIZE; j++) {
2853                 if (subnet_length_entries[i].subnet_addresses[j] != NULL)
2854                 {
2855                     cleanup_subnet_entry(subnet_length_entries[i].subnet_addresses[j]);
2856                 }
2857             }
2858             g_free(subnet_length_entries[i].subnet_addresses);
2859             subnet_length_entries[i].subnet_addresses = NULL;
2860         }
2861     }
2862
2863     have_subnet_entry = FALSE;
2864     new_resolved_objects = FALSE;
2865 }
2866
2867 static void
2868 free_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2869 {
2870     resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2871
2872     g_free(resolved_ipv4_entry);
2873 }
2874
2875 static void
2876 free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2877 {
2878     resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2879
2880     g_free(resolved_ipv6_entry);
2881 }
2882
2883 void
2884 manually_resolve_cleanup(void)
2885 {
2886     if (manually_resolved_ipv4_list) {
2887         g_slist_foreach(manually_resolved_ipv4_list, free_manually_resolved_ipv4, NULL);
2888         g_slist_free(manually_resolved_ipv4_list);
2889         manually_resolved_ipv4_list = NULL;
2890     }
2891
2892     if (manually_resolved_ipv6_list) {
2893         g_slist_foreach(manually_resolved_ipv6_list, free_manually_resolved_ipv6, NULL);
2894         g_slist_free(manually_resolved_ipv6_list);
2895         manually_resolved_ipv6_list = NULL;
2896     }
2897
2898 }
2899
2900 gchar *
2901 udp_port_to_display(wmem_allocator_t *allocator, guint port)
2902 {
2903
2904     if (!gbl_resolv_flags.transport_name) {
2905         return wmem_utoa(allocator, port);
2906     }
2907
2908     return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
2909
2910 } /* udp_port_to_display */
2911
2912 gchar *
2913 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
2914 {
2915
2916     if (!gbl_resolv_flags.transport_name) {
2917         return wmem_utoa(allocator, port);
2918     }
2919
2920     return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
2921
2922 } /* dccp_port_to_display */
2923
2924 gchar *
2925 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
2926 {
2927
2928     if (!gbl_resolv_flags.transport_name) {
2929         return wmem_utoa(allocator, port);
2930     }
2931
2932     return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
2933
2934 } /* tcp_port_to_display */
2935
2936 gchar *
2937 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
2938 {
2939
2940     if (!gbl_resolv_flags.transport_name) {
2941         return wmem_utoa(allocator, port);
2942     }
2943
2944     return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
2945
2946 } /* sctp_port_to_display */
2947
2948 gchar *
2949 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
2950 {
2951     const gchar *port_str;
2952
2953     if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2954         /* No name resolution support, just return port string */
2955         return wmem_strdup_printf(scope, "%u", port);
2956     }
2957     port_str = serv_name_lookup(proto, port);
2958     g_assert(port_str);
2959     return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
2960 }
2961
2962 int
2963 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
2964 {
2965     const gchar *port_str;
2966
2967     if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2968         /* No name resolution support, just return port string */
2969         return g_snprintf(buf, buf_size, "%u", port);
2970     }
2971     port_str = serv_name_lookup(proto, port);
2972     g_assert(port_str);
2973     return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
2974 }
2975
2976 gchar *
2977 get_ether_name(const guint8 *addr)
2978 {
2979     hashether_t *tp;
2980     gboolean resolve = gbl_resolv_flags.mac_name;
2981
2982     tp = eth_name_lookup(addr, resolve);
2983
2984     return resolve ? tp->resolved_name : tp->hexaddr;
2985
2986 } /* get_ether_name */
2987
2988 gchar *
2989 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
2990 {
2991     return get_ether_name(tvb_get_ptr(tvb, offset, 6));
2992 }
2993
2994 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2995  * If it's not found, simply return NULL.
2996  */
2997 gchar *
2998 get_ether_name_if_known(const guint8 *addr)
2999 {
3000     hashether_t *tp;
3001
3002     /* Initialize ether structs if we're the first
3003      * ether-related function called */
3004     if (!gbl_resolv_flags.mac_name)
3005         return NULL;
3006
3007     /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3008     tp = eth_name_lookup(addr, TRUE);
3009     g_assert(tp != NULL);
3010
3011     if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3012         /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
3013         return tp->resolved_name;
3014     }
3015     else {
3016         /* Name was created */
3017         return NULL;
3018     }
3019 }
3020
3021 guint8 *
3022 get_ether_addr(const gchar *name)
3023 {
3024
3025     /* force resolution (do not check gbl_resolv_flags) */
3026     return eth_addr_lookup(name);
3027
3028 } /* get_ether_addr */
3029
3030 void
3031 add_ether_byip(const guint ip, const guint8 *eth)
3032 {
3033     gboolean found;
3034     hashipv4_t *tp;
3035
3036     /* first check that IP address can be resolved */
3037     if (!gbl_resolv_flags.network_name)
3038         return;
3039
3040     tp = host_lookup(ip, &found);
3041     if (found) {
3042         /* ok, we can add this entry in the ethers hashtable */
3043         add_eth_name(eth, tp->name);
3044     }
3045
3046 } /* add_ether_byip */
3047
3048 gchar *
3049 ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct)
3050 {
3051     gchar *buf = (gchar *)wmem_alloc(scope, 12);
3052
3053     *dword_to_hex_punct(buf, ad, punct) = '\0';
3054     return buf;
3055 }
3056
3057 const gchar *
3058 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3059 {
3060
3061     if (!gbl_resolv_flags.network_name) {
3062         return ipxnet_to_str_punct(allocator, addr, '\0');
3063     }
3064
3065     return ipxnet_name_lookup(allocator, addr);
3066
3067 } /* get_ipxnet_name */
3068
3069 guint32
3070 get_ipxnet_addr(const gchar *name, gboolean *known)
3071 {
3072     guint32 addr;
3073     gboolean success;
3074
3075     /* force resolution (do not check gbl_resolv_flags) */
3076     addr =  ipxnet_addr_lookup(name, &success);
3077
3078     *known = success;
3079     return addr;
3080
3081 } /* get_ipxnet_addr */
3082
3083 const gchar *
3084 get_manuf_name(const guint8 *addr)
3085 {
3086     hashmanuf_t *manuf_value;
3087
3088     manuf_value = manuf_name_lookup(addr);
3089     if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3090         return manuf_value->resolved_name;
3091
3092     return manuf_value->hexaddr;
3093
3094 } /* get_manuf_name */
3095
3096 const gchar *
3097 uint_get_manuf_name(const guint oid)
3098 {
3099     guint8 addr[3];
3100
3101     addr[0] = (oid >> 16) & 0xFF;
3102     addr[1] = (oid >> 8) & 0xFF;
3103     addr[2] = (oid >> 0) & 0xFF;
3104     return get_manuf_name(addr);
3105 }
3106
3107 const gchar *
3108 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3109 {
3110     return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3111 }
3112
3113 const gchar *
3114 get_manuf_name_if_known(const guint8 *addr)
3115 {
3116     hashmanuf_t *manuf_value;
3117     int manuf_key;
3118     guint8 oct;
3119
3120     /* manuf needs only the 3 most significant octets of the ethernet address */
3121     manuf_key = addr[0];
3122     manuf_key = manuf_key<<8;
3123     oct = addr[1];
3124     manuf_key = manuf_key | oct;
3125     manuf_key = manuf_key<<8;
3126     oct = addr[2];
3127     manuf_key = manuf_key | oct;
3128
3129     manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3130     if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3131         return NULL;
3132     }
3133
3134     return manuf_value->resolved_name;
3135
3136 } /* get_manuf_name_if_known */
3137
3138 const gchar *
3139 uint_get_manuf_name_if_known(const guint manuf_key)
3140 {
3141     hashmanuf_t *manuf_value;
3142
3143     manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3144     if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3145         return NULL;
3146     }
3147
3148     return manuf_value->resolved_name;
3149 }
3150
3151 const gchar *
3152 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3153 {
3154     return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3155 }
3156
3157 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3158 {
3159     return manuf->resolved_name;
3160 }
3161
3162 const gchar *
3163 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3164 {
3165     guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3166     hashmanuf_t *manuf_value;
3167     const gchar *ret;
3168
3169     /* Copy and convert the address to network byte order. */
3170     *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3171
3172     manuf_value = manuf_name_lookup(addr);
3173     if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3174         ret = wmem_strdup_printf(allocator, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
3175     } else {
3176         ret = wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3177     }
3178
3179     wmem_free(NULL, addr);
3180     return ret;
3181 } /* eui64_to_display */
3182
3183 #ifdef HAVE_C_ARES
3184 #define GHI_TIMEOUT (250 * 1000)
3185 static void
3186 c_ares_ghi_cb(
3187         void *arg,
3188         int status,
3189 #if ( ( ARES_VERSION_MAJOR < 1 )                                     \
3190     || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3191         struct hostent *hp
3192 #else
3193         int timeouts _U_,
3194         struct hostent *hp
3195 #endif
3196         ) {
3197
3198     /*
3199      * XXX - If we wanted to be really fancy we could cache results here and
3200      * look them up in get_host_ipaddr* below.
3201      */
3202     async_hostent_t *ahp = (async_hostent_t *)arg;
3203     if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3204         memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3205         ahp->copied = hp->h_length;
3206     }
3207 }
3208 #endif /* HAVE_C_ARES */
3209
3210 /* Translate a string, assumed either to be a dotted-quad IP address or
3211  * a host name, to a numeric IP address.  Return TRUE if we succeed and
3212  * set "*addrp" to that numeric IP address; return FALSE if we fail.
3213  * Used more in the dfilter parser rather than in packet dissectors */
3214 gboolean
3215 get_host_ipaddr(const char *host, guint32 *addrp)
3216 {
3217     struct in_addr      ipaddr;
3218 #ifdef HAVE_C_ARES
3219     struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3220     int nfds;
3221     fd_set rfds, wfds;
3222     async_hostent_t ahe;
3223 #elif defined(HAVE_GETADDRINFO)
3224     struct addrinfo hint, *result = NULL;
3225 #elif defined(HAVE_GETHOSTBYNAME)
3226     struct hostent *hp;
3227 #endif
3228
3229     /*
3230      * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3231      * inet_pton(AF_INET) does not support hexadecimal notation nor
3232      * less-than-4 octet notation.
3233      */
3234     if (!inet_aton(host, &ipaddr)) {
3235
3236         /* It's not a valid dotted-quad IP address; is it a valid
3237          * host name?
3238          */
3239
3240         /* If we're not allowed to do name resolution, don't do name
3241          * resolution...
3242          */
3243         if (!gbl_resolv_flags.network_name ||
3244                 !gbl_resolv_flags.use_external_net_name_resolver) {
3245             return FALSE;
3246         }
3247
3248 #ifdef HAVE_C_ARES
3249         if (! (gbl_resolv_flags.concurrent_dns) ||
3250                 name_resolve_concurrency < 1 ||
3251                 ! async_dns_initialized) {
3252             return FALSE;
3253         }
3254         ahe.addr_size = (int) sizeof (struct in_addr);
3255         ahe.copied = 0;
3256         ahe.addrp = addrp;
3257         ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3258         FD_ZERO(&rfds);
3259         FD_ZERO(&wfds);
3260         nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3261         if (nfds > 0) {
3262             tvp = ares_timeout(ghbn_chan, &tv, &tv);
3263             if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3264                 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3265                 return FALSE;
3266             }
3267             ares_process(ghbn_chan, &rfds, &wfds);
3268         }
3269         ares_cancel(ghbn_chan);
3270         if (ahe.addr_size == ahe.copied) {
3271             return TRUE;
3272         }
3273         return FALSE;
3274 #elif defined(HAVE_GETADDRINFO)
3275         /*
3276          * This can be slow, particularly for capture files with lots of
3277          * addresses. Should we just return FALSE instead?
3278          */
3279         memset(&hint, 0, sizeof(hint));
3280         hint.ai_family = AF_INET;
3281         if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3282             /* Probably more checks than necessary */
3283             if (result != NULL) {
3284                 gboolean ret_val = FALSE;
3285                 if (result->ai_family == AF_INET && result->ai_addrlen == 4) {
3286                     memcpy(&ipaddr, result->ai_addr->sa_data, result->ai_addrlen);
3287                     ret_val = TRUE;
3288                 }
3289                 freeaddrinfo(result);
3290                 return ret_val;
3291             }
3292         }
3293 #elif defined(HAVE_GETHOSTBYNAME)
3294         hp = gethostbyname(host);
3295         if (hp == NULL) {
3296             /* No. */
3297             return FALSE;
3298             /* Apparently, some versions of gethostbyaddr can
3299              * return IPv6 addresses. */
3300          } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3301              memcpy(&ipaddr, hp->h_addr, hp->h_length);
3302          } else {
3303              return FALSE;
3304          }
3305 #endif
3306     } else {
3307         /* Does the string really contain dotted-quad IP?
3308          * Check against inet_atons that accept strings such as
3309          * "130.230" as valid addresses and try to convert them
3310          * to some form of a classful (host.net) notation.
3311          */
3312         unsigned int a0, a1, a2, a3;
3313         if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3314             return FALSE;
3315     }
3316
3317     *addrp = ipaddr.s_addr;
3318     return TRUE;
3319 }
3320
3321 /*
3322  * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3323  * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3324  * return FALSE if we fail.
3325  */
3326 gboolean
3327 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3328 {
3329 #ifdef HAVE_C_ARES
3330     struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3331     int nfds;
3332     fd_set rfds, wfds;
3333     async_hostent_t ahe;
3334 #elif defined(HAVE_GETADDRINFO)
3335     struct addrinfo hint, *result = NULL;
3336 #elif defined(HAVE_GETHOSTBYNAME2)
3337     struct hostent *hp;
3338 #endif /* HAVE_C_ARES */
3339
3340     if (str_to_ip6(host, addrp))
3341         return TRUE;
3342
3343     /* It's not a valid dotted-quad IP address; is it a valid
3344      * host name?
3345      */
3346
3347     /* If we're not allowed to do name resolution, don't do name
3348      * resolution...
3349      */
3350     if (!gbl_resolv_flags.network_name ||
3351             !gbl_resolv_flags.use_external_net_name_resolver) {
3352         return FALSE;
3353     }
3354
3355     /* try FQDN */
3356 #ifdef HAVE_C_ARES
3357     if (! (gbl_resolv_flags.concurrent_dns) ||
3358             name_resolve_concurrency < 1 ||
3359             ! async_dns_initialized) {
3360         return FALSE;
3361     }
3362     ahe.addr_size = (int) sizeof (struct e_in6_addr);
3363     ahe.copied = 0;
3364     ahe.addrp = addrp;
3365     ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3366     FD_ZERO(&rfds);
3367     FD_ZERO(&wfds);
3368     nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3369     if (nfds > 0) {
3370         tvp = ares_timeout(ghbn_chan, &tv, &tv);
3371         if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3372             fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3373             return FALSE;
3374         }
3375         ares_process(ghbn_chan, &rfds, &wfds);
3376     }
3377     ares_cancel(ghbn_chan);
3378     if (ahe.addr_size == ahe.copied) {
3379         return TRUE;
3380     }
3381 #elif defined(HAVE_GETADDRINFO)
3382     /*
3383      * This can be slow, particularly for capture files with lots of
3384      * addresses. Should we just return FALSE instead?
3385      */
3386     memset(&hint, 0, sizeof(hint));
3387     hint.ai_family = AF_INET6;
3388     if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3389         /* Probably more checks than necessary */
3390         if (result != NULL) {
3391             gboolean ret_val = FALSE;
3392             if (result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct e_in6_addr)) {
3393                 memcpy(addrp, result->ai_addr->sa_data, result->ai_addrlen);
3394                 ret_val = TRUE;
3395             }
3396             freeaddrinfo(result);
3397             return ret_val;
3398         }
3399     }
3400 #elif defined(HAVE_GETHOSTBYNAME2)
3401     hp = gethostbyname2(host, AF_INET6);
3402     if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3403         memcpy(addrp, hp->h_addr, hp->h_length);
3404         return TRUE;
3405     }
3406 #endif
3407
3408     return FALSE;
3409 }
3410
3411 /*
3412  * Find out whether a hostname resolves to an ip or ipv6 address
3413  * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3414  * that we don't know)
3415  */
3416 const char *
3417 host_ip_af(const char *host
3418 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETHOSTBYNAME2)
3419         _U_
3420 #endif
3421         )
3422 {
3423     const char *af = "ip";
3424 #ifdef HAVE_GETADDRINFO
3425     struct addrinfo hint, *result = NULL;
3426     memset(&hint, 0, sizeof(hint));
3427     hint.ai_family = AF_UNSPEC;
3428     if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3429         if (result->ai_family == AF_INET6) {
3430             af = "ip6";
3431         }
3432         freeaddrinfo(result);
3433     }
3434 #elif defined(HAVE_GETHOSTBYNAME2)
3435     struct hostent *h;
3436     return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
3437 #endif
3438     return af;
3439 }
3440
3441 GHashTable *
3442 get_manuf_hashtable(void)
3443 {
3444     return manuf_hashtable;
3445 }
3446
3447 GHashTable *
3448 get_wka_hashtable(void)
3449 {
3450     return wka_hashtable;
3451 }
3452
3453 GHashTable *
3454 get_eth_hashtable(void)
3455 {
3456     return eth_hashtable;
3457 }
3458
3459 GHashTable *
3460 get_serv_port_hashtable(void)
3461 {
3462     return serv_port_hashtable;
3463 }
3464
3465 GHashTable *
3466 get_ipxnet_hash_table(void)
3467 {
3468         return ipxnet_hash_table;
3469 }
3470
3471 GHashTable *
3472 get_ipv4_hash_table(void)
3473 {
3474         return ipv4_hash_table;
3475 }
3476
3477 GHashTable *
3478 get_ipv6_hash_table(void)
3479 {
3480         return ipv6_hash_table;
3481 }
3482 /* Initialize all the address resolution subsystems in this file */
3483 void
3484 addr_resolv_init(void)
3485 {
3486     initialize_services();
3487     initialize_ethers();
3488     initialize_ipxnets();
3489     /* host name initialization is done on a per-capture-file basis */
3490     /*host_name_lookup_init();*/
3491 }
3492
3493 /* Clean up all the address resolution subsystems in this file */
3494 void
3495 addr_resolv_cleanup(void)
3496 {
3497     service_name_lookup_cleanup();
3498     eth_name_lookup_cleanup();
3499     ipx_name_lookup_cleanup();
3500     /* host name initialization is done on a per-capture-file basis */
3501     /*host_name_lookup_cleanup();*/
3502 }
3503
3504 gboolean
3505 str_to_ip(const char *str, void *dst)
3506 {
3507     return inet_pton(AF_INET, str, dst) > 0;
3508 }
3509
3510 gboolean
3511 str_to_ip6(const char *str, void *dst)
3512 {
3513     return inet_pton(AF_INET6, str, dst) > 0;
3514 }
3515
3516 /*
3517  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
3518  *
3519  * Local variables:
3520  * c-basic-offset: 4
3521  * tab-width: 8
3522  * indent-tabs-mode: nil
3523  * End:
3524  *
3525  * vi: set shiftwidth=4 tabstop=8 expandtab:
3526  * :indentSize=4:tabSize=8:noTabs=true:
3527  */