2 * Routines for network object lookup
4 * Laurent Deniel <laurent.deniel@free.fr>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
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).
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.
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.
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.
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.)
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.
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
76 #ifdef HAVE_ARPA_INET_H
77 #include <arpa/inet.h>
80 #ifdef HAVE_SYS_SOCKET_H
81 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
84 #ifdef HAVE_WINSOCK2_H
85 #include <winsock2.h> /* needed to define AF_ values on Windows */
88 #ifndef HAVE_INET_ATON
89 # include "wsutil/inet_aton.h"
92 #ifdef NEED_INET_V6DEFS_H
93 # include "wsutil/inet_v6defs.h"
97 # include <ws2tcpip.h>
102 # define socklen_t unsigned int
105 # include <ares_version.h>
107 # ifdef HAVE_GNU_ADNS
110 # if defined(inet_aton) && defined(_WIN32)
113 # endif /* HAVE_GNU_ADNS */
114 #endif /* HAVE_C_ARES */
120 #include "addr_and_mask.h"
122 #include "addr_resolv.h"
123 #include "wsutil/filesystem.h"
125 #include <wsutil/report_err.h>
126 #include <wsutil/file_util.h>
127 #include <wsutil/pint.h>
129 #include <epan/strutil.h>
130 #include <epan/to_str-int.h>
131 #include <epan/prefs.h>
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"
140 #define HASHETHSIZE 2048
141 #define HASHHOSTSIZE 2048
142 #define HASHIPXNETSIZE 256
143 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
145 /* hash table used for IPv4 lookup */
147 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
150 typedef struct sub_net_hashipv4 {
152 guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
153 struct sub_net_hashipv4 *next;
155 gchar name[MAXNAMELEN];
156 } sub_net_hashipv4_t;
158 /* Array of entries of subnets of different lengths */
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;
166 /* hash table used for IPX network lookup */
168 /* XXX - check goodness of hash function */
170 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
172 typedef struct hashipxnet {
174 struct hashipxnet *next;
175 gchar name[MAXNAMELEN];
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
184 guint status; /* (See above) */
187 char resolved_name[MAXNAMELEN];
191 guint status; /* (See above) */
194 char resolved_name[MAXNAMELEN];
197 /* internal ethernet type */
198 typedef struct _ether
201 char name[MAXNAMELEN];
204 /* internal ipxnet type */
205 typedef struct _ipxnet
208 char name[MAXNAMELEN];
211 static GHashTable *ipxnet_hash_table = NULL;
212 static GHashTable *ipv4_hash_table = NULL;
213 static GHashTable *ipv6_hash_table = NULL;
215 static GSList *manually_resolved_ipv4_list = NULL;
216 static GSList *manually_resolved_ipv6_list = NULL;
218 typedef struct _resolved_ipv4
221 char name[MAXNAMELEN];
224 typedef struct _resolved_ipv6
226 struct e_in6_addr ip6_addr;
227 char name[MAXNAMELEN];
230 static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
232 static gchar *cb_service;
233 static port_type cb_proto = PT_NONE;
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;
241 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
242 static gboolean have_subnet_entry = FALSE;
244 static gboolean new_resolved_objects = FALSE;
246 static GPtrArray* extra_hosts_files = NULL;
248 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
249 static void add_serv_port_cb(const guint32 port);
252 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
256 ipv6_oat_hash(gconstpointer key)
259 const unsigned char *p = (const unsigned char *)key;
263 for ( i = 0; i < len; i++ ) {
277 ipv6_equal(gconstpointer v1, gconstpointer v2)
280 if (memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0) {
288 * Flag controlling what names to resolve.
290 e_addr_resolve gbl_resolv_flags = {
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 */
299 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
300 static guint name_resolve_concurrency = 500;
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.
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 */
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.
327 typedef struct _async_dns_queue_msg
331 struct e_in6_addr ip6;
334 } async_dns_queue_msg_t;
336 typedef struct _async_hostent {
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);
346 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
349 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
350 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
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.
365 typedef struct _async_dns_queue_msg
371 } async_dns_queue_msg_t;
373 #endif /* HAVE_GNU_ADNS */
374 #endif /* HAVE_C_ARES */
376 static gboolean async_dns_initialized = FALSE;
377 static guint async_dns_in_flight = 0;
378 static GList *async_dns_queue_head = NULL;
380 /* push a dns request */
382 add_async_dns_ipv4(int type, guint32 addr)
384 async_dns_queue_msg_t *msg;
386 msg = g_new(async_dns_queue_msg_t,1);
389 msg->addr.ip4 = addr;
392 msg->ip4_addr = addr;
393 msg->submitted = FALSE;
395 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
403 const gchar* name; /* Shallow copy */
407 * Miscellaneous functions
411 fgetline(char **buf, int *size, FILE *fp)
416 if (fp == NULL || buf == NULL)
423 *buf = (char *)g_malloc(*size);
433 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
434 if (len+1 >= *size) {
435 *buf = (char *)g_realloc(*buf, *size += BUFSIZ);
440 if (len == 0 && c == EOF)
451 * Local function definitions
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);
458 add_service_name(port_type proto, const guint port, const char *service_name)
460 serv_port_t *serv_port_table;
463 key = (int *)g_new(int, 1);
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);
477 g_free(serv_port_table->tcp_name);
478 serv_port_table->tcp_name = g_strdup(service_name);
481 g_free(serv_port_table->udp_name);
482 serv_port_table->udp_name = g_strdup(service_name);
485 g_free(serv_port_table->sctp_name);
486 serv_port_table->sctp_name = g_strdup(service_name);
489 g_free(serv_port_table->dccp_name);
490 serv_port_table->dccp_name = g_strdup(service_name);
494 /* Should not happen */
497 new_resolved_objects = TRUE;
502 parse_service_line (char *line)
505 * See the services(4) or services(5) man page for services file format
506 * (not available on all systems).
514 range_t *port_rng = NULL;
515 guint32 max_port = MAX_UDP_PORT;
517 if ((cp = strchr(line, '#')))
520 if ((cp = strtok(line, " \t")) == NULL)
525 if ((cp = strtok(NULL, " \t")) == NULL)
530 if (strtok(cp, "/") == NULL)
533 if ((cp = strtok(NULL, "/")) == NULL)
536 /* seems we got all interesting things from the file */
537 if (strcmp(cp, "tcp") == 0) {
538 max_port = MAX_TCP_PORT;
541 else if (strcmp(cp, "udp") == 0) {
542 max_port = MAX_UDP_PORT;
545 else if (strcmp(cp, "sctp") == 0) {
546 max_port = MAX_SCTP_PORT;
549 else if (strcmp(cp, "dccp") == 0) {
550 max_port = MAX_DCCP_PORT;
556 if (CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port)) {
557 /* some assertion here? */
561 cb_service = service;
563 range_foreach(port_rng, add_serv_port_cb);
566 } /* parse_service_line */
570 add_serv_port_cb(const guint32 port)
573 add_service_name(cb_proto, port, cb_service);
579 parse_services_file(const char * path)
583 static char *buf = NULL;
585 /* services hash table initialization */
586 serv_p = ws_fopen(path, "r");
591 while (fgetline(&buf, &size, serv_p) >= 0) {
592 parse_service_line (buf);
599 * unsigned integer to ascii
602 wmem_utoa(wmem_allocator_t *allocator, guint port)
604 gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
606 /* XXX, guint32_to_str() ? */
607 guint32_to_str_buf(port, bp, MAXNAMELEN);
612 _serv_name_lookup(port_type proto, guint port, serv_port_t **value_ret)
614 serv_port_t *serv_port_table;
616 serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
618 if (value_ret != NULL)
619 *value_ret = serv_port_table;
621 if (serv_port_table == NULL)
626 return serv_port_table->udp_name;
628 return serv_port_table->tcp_name;
630 return serv_port_table->sctp_name;
632 return serv_port_table->dccp_name;
640 try_serv_name_lookup(port_type proto, guint port)
642 return _serv_name_lookup(proto, port, NULL);
646 serv_name_lookup(port_type proto, guint port)
648 serv_port_t *serv_port_table = NULL;
652 name = _serv_name_lookup(proto, port, &serv_port_table);
656 if (serv_port_table == NULL) {
657 key = (guint *)g_new(guint, 1);
659 serv_port_table = g_new0(serv_port_t, 1);
660 g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
662 if (serv_port_table->numeric == NULL) {
663 serv_port_table->numeric = g_strdup_printf("%u", port);
666 return serv_port_table->numeric;
670 destroy_serv_port(gpointer data)
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);
682 initialize_services(void)
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);
687 /* Compute the pathname of the services file. */
688 if (g_services_path == NULL) {
689 g_services_path = get_datafile_path(ENAME_SERVICES);
691 parse_services_file(g_services_path);
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);
697 parse_services_file(g_pservices_path);
701 service_name_lookup_cleanup(void)
703 if (serv_port_hashtable) {
704 g_hash_table_destroy(serv_port_hashtable);
705 serv_port_hashtable = NULL;
709 /* Fill in an IP4 structure with info from subnets file or just with the
710 * string form of the address.
713 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
715 subnet_entry_t subnet_entry;
717 if (tp->flags & DUMMY_ADDRESS_ENTRY)
718 return; /* already done */
720 tp->flags |= DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
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 */
727 gchar buffer[MAX_IP_STR_LEN];
731 host_addr = addr & (~(guint32)subnet_entry.mask);
732 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
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?),
740 i = subnet_entry.mask_length / 8;
741 while(*(paddr) != '\0' && i > 0) {
742 if (*(++paddr) == '.') {
747 /* There are more efficient ways to do this, but this is safe if we
748 * trust g_snprintf and MAXNAMELEN
750 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
752 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
757 /* Fill in an IP6 structure with the string form of the address.
760 fill_dummy_ip6(hashipv6_t* volatile tp)
762 if (tp->flags & DUMMY_ADDRESS_ENTRY)
763 return; /* already done */
765 tp->flags |= DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
766 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
775 #if ( ( ARES_VERSION_MAJOR < 1 ) \
776 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
784 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
788 /* XXX, what to do if async_dns_in_flight == 0? */
789 async_dns_in_flight--;
791 if (status == ARES_SUCCESS) {
792 for (p = he->h_addr_list; *p != NULL; p++) {
793 switch(caqm->family) {
795 add_ipv4_name(caqm->addr.ip4, he->h_name);
798 add_ipv6_name(&caqm->addr.ip6, he->h_name);
801 /* Throw an exception? */
808 #endif /* HAVE_C_ARES */
810 /* --------------- */
812 new_ipv4(const guint addr)
814 hashipv4_t *tp = g_new(hashipv4_t, 1);
818 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
823 host_lookup(const guint addr)
825 hashipv4_t * volatile tp;
827 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
830 * We don't already have an entry for this host name; create one,
831 * and then try to resolve it.
834 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
836 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
838 * This hasn't been resolved yet, and we haven't tried to
839 * resolve it already, so try.
847 if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
848 tp->flags |= TRIED_RESOLVE_ADDRESS;
851 if (gbl_resolv_flags.concurrent_dns &&
852 name_resolve_concurrency > 0 &&
853 async_dns_initialized) {
854 add_async_dns_ipv4(AF_INET, addr);
855 fill_dummy_ip4(addr, tp);
860 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
861 * the name of the host on which it's running; to work around that
862 * botch, we don't try to translate an all-zero IP address to a host
865 * Presumably getaddrinfo() behaves the same way. Anyway, we should
866 * never get to this code on Windows since those builds include c-ares.
868 #elif defined(HAVE_GETADDRINFO)
870 struct sockaddr_in sin;
872 memset(&sin, 0, sizeof(sin));
873 sin.sin_family = AF_INET;
874 sin.sin_addr.s_addr = addr;
875 if (getnameinfo((struct sockaddr *)&sin, sizeof(sin),
876 tp->name, sizeof(tp->name),
877 NULL, 0, NI_NAMEREQD) == 0) {
881 #elif defined(HAVE_GETHOSTBYNAME)
883 struct hostent *hostp;
885 hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
887 if (hostp != NULL && hostp->h_name[0] != '\0') {
888 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
894 /* unknown host or DNS timeout */
897 fill_dummy_ip4(addr, tp);
902 /* --------------- */
904 new_ipv6(const struct e_in6_addr *addr)
906 hashipv6_t *tp = g_new(hashipv6_t,1);
907 memcpy(tp->addr, addr->bytes, sizeof tp->addr);
910 ip6_to_str_buf(addr, tp->ip6);
914 /* ------------------------------------ */
916 host_lookup6(const struct e_in6_addr *addr)
918 hashipv6_t * volatile tp;
920 async_dns_queue_msg_t *caqm;
921 #elif defined(HAVE_GETADDRINFO)
922 struct sockaddr_in6 sin6;
923 #elif defined(HAVE_GETHOSTBYNAME)
924 struct hostent *hostp;
927 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
930 * We don't already have an entry for this host name; create one,
931 * and then try to resolve it.
933 struct e_in6_addr *addr_key;
935 addr_key = g_new(struct e_in6_addr,1);
937 memcpy(addr_key, addr, 16);
938 g_hash_table_insert(ipv6_hash_table, addr_key, tp);
940 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
942 * This hasn't been resolved yet, and we haven't tried to
943 * resolve it already, so try.
951 if (gbl_resolv_flags.network_name &&
952 gbl_resolv_flags.use_external_net_name_resolver) {
953 tp->flags |= TRIED_RESOLVE_ADDRESS;
955 if ((gbl_resolv_flags.concurrent_dns) &&
956 name_resolve_concurrency > 0 &&
957 async_dns_initialized) {
958 caqm = g_new(async_dns_queue_msg_t,1);
959 caqm->family = AF_INET6;
960 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
961 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
965 #elif defined(HAVE_GETADDRINFO)
966 memset(&sin6, 0, sizeof(sin6));
967 sin6.sin6_family = AF_INET6;
968 memcpy(sin6.sin6_addr.s6_addr, addr, sizeof(*addr));
969 if (getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
970 tp->name, sizeof(tp->name),
971 NULL, 0, NI_NAMEREQD) == 0) {
974 #elif defined(HAVE_GETHOSTBYNAME)
975 /* Quick hack to avoid DNS/YP timeout */
976 hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
978 if (hostp != NULL && hostp->h_name[0] != '\0') {
979 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
985 /* unknown host or DNS timeout */
992 * Ethernet / manufacturer resolution
994 * The following functions implement ethernet address resolution and
995 * ethers files parsing (see ethers(4)).
997 * The manuf file has the same format as ethers(4) except that names are
998 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
999 * only 3 bytes (instead of 6).
1003 * I decide to not use the existing functions (see ethers(3) on some
1004 * operating systems) for the following reasons:
1005 * - performance gains (use of hash tables and some other enhancements),
1006 * - use of two ethers files (system-wide and per user),
1007 * - avoid the use of NIS maps,
1008 * - lack of these functions on some systems.
1010 * So the following functions do _not_ behave as the standard ones.
1017 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1018 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1022 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1023 const gboolean manuf_file)
1030 for (i = 0; i < 6; i++) {
1031 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1032 if (!g_ascii_isxdigit(*cp))
1034 num = strtoul(cp, &p, 16);
1036 return FALSE; /* failed */
1038 return FALSE; /* not a valid octet */
1039 eth->addr[i] = (guint8) num;
1040 cp = p; /* skip past the number */
1042 /* OK, what character terminated the octet? */
1044 /* "/" - this has a mask. */
1046 /* Entries with masks are allowed only in the "manuf" files. */
1049 cp++; /* skip past the '/' to get to the mask */
1050 if (!g_ascii_isdigit(*cp))
1051 return FALSE; /* no sign allowed */
1052 num = strtoul(cp, &p, 10);
1054 return FALSE; /* failed */
1055 cp = p; /* skip past the number */
1056 if (*cp != '\0' && !g_ascii_isspace(*cp))
1057 return FALSE; /* bogus terminator */
1058 if (num == 0 || num >= 48)
1059 return FALSE; /* bogus mask */
1060 /* Mask out the bits not covered by the mask */
1062 for (i = 0; num >= 8; i++, num -= 8)
1063 ; /* skip octets entirely covered by the mask */
1064 /* Mask out the first masked octet */
1065 eth->addr[i] &= (0xFF << (8 - num));
1067 /* Mask out completely-masked-out octets */
1073 /* We're at the end of the address, and there's no mask. */
1075 /* We got 3 bytes, so this is a manufacturer ID. */
1077 /* Manufacturer IDs are only allowed in the "manuf"
1081 /* Indicate that this is a manufacturer ID (0 is not allowed
1088 /* We got 6 bytes, so this is a MAC address.
1089 If we're reading one of the "manuf" files, indicate that
1090 this is a MAC address (48 is not allowed as a mask). */
1096 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1101 /* We don't know the separator used in this number; it can either
1102 be ':', '-', or '.'. */
1103 if (*cp != ':' && *cp != '-' && *cp != '.')
1105 sep = *cp; /* subsequent separators must be the same */
1107 /* It has to be the same as the first separator */
1119 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1120 const gboolean manuf_file)
1123 * See the ethers(4) or ethers(5) man page for ethers file format
1124 * (not available on all systems).
1125 * We allow both ethernet address separators (':' and '-'),
1126 * as well as Wireshark's '.' separator.
1131 if ((cp = strchr(line, '#')))
1134 if ((cp = strtok(line, " \t")) == NULL)
1137 if (!parse_ether_address(cp, eth, mask, manuf_file))
1140 if ((cp = strtok(NULL, " \t")) == NULL)
1143 g_strlcpy(eth->name, cp, MAXNAMELEN);
1147 } /* parse_ether_line */
1149 static FILE *eth_p = NULL;
1152 set_ethent(char *path)
1157 eth_p = ws_fopen(path, "r");
1170 get_ethent(unsigned int *mask, const gboolean manuf_file)
1174 static int size = 0;
1175 static char *buf = NULL;
1180 while (fgetline(&buf, &size, eth_p) >= 0) {
1181 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1192 get_ethbyname(const gchar *name)
1196 set_ethent(g_pethers_path);
1198 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1204 set_ethent(g_ethers_path);
1206 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1214 } /* get_ethbyname */
1218 get_ethbyaddr(const guint8 *addr)
1223 set_ethent(g_pethers_path);
1225 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1231 set_ethent(g_ethers_path);
1233 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1241 } /* get_ethbyaddr */
1243 static hashmanuf_t *manuf_hash_new_entry(const guint8 *addr, char* name)
1246 hashmanuf_t *manuf_value;
1249 /* manuf needs only the 3 most significant octets of the ethernet address */
1250 manuf_key = (int *)g_new(int, 1);
1251 *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1252 manuf_value = g_new(hashmanuf_t, 1);
1254 memcpy(manuf_value->addr, addr, 3);
1255 manuf_value->status = (name != NULL) ? HASHETHER_STATUS_RESOLVED_NAME : HASHETHER_STATUS_UNRESOLVED;
1257 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1258 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1261 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1262 manuf_value->resolved_name[0] = '\0';
1264 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1265 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1268 g_hash_table_insert(manuf_hashtable, manuf_key, manuf_value);
1273 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1278 * XXX - can we use Standard Annotation Language annotations to
1279 * note that mask, as returned by parse_ethe)r_address() (and thus
1280 * by the routines that call it, and thus passed to us) cannot be > 48,
1281 * or is SAL too weak to express that?
1284 /* This is a well-known MAC address; just add this to the Ethernet
1286 add_eth_name(addr, name);
1291 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1292 manuf_hash_new_entry(addr, name);
1296 /* This is a range of well-known addresses; add it to the appropriate
1297 well-known-address table, creating that table if necessary. */
1299 wka_key = (guint8 *)g_malloc(6);
1300 memcpy(wka_key, addr, 6);
1302 g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
1304 } /* add_manuf_name */
1306 static hashmanuf_t *
1307 manuf_name_lookup(const guint8 *addr)
1309 gint32 manuf_key = 0;
1311 hashmanuf_t *manuf_value;
1313 /* manuf needs only the 3 most significant octets of the ethernet address */
1314 manuf_key = addr[0];
1315 manuf_key = manuf_key<<8;
1317 manuf_key = manuf_key | oct;
1318 manuf_key = manuf_key<<8;
1320 manuf_key = manuf_key | oct;
1323 /* first try to find a "perfect match" */
1324 manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1325 if (manuf_value != NULL) {
1329 /* Mask out the broadcast/multicast flag but not the locally
1330 * administered flag as locally administered means: not assigned
1331 * by the IEEE but the local administrator instead.
1332 * 0x01 multicast / broadcast bit
1333 * 0x02 locally administered bit */
1334 if ((manuf_key & 0x00010000) != 0) {
1335 manuf_key &= 0x00FEFFFF;
1336 manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1337 if (manuf_value != NULL) {
1342 /* Add the address as a hex string */
1343 return manuf_hash_new_entry(addr, NULL);
1345 } /* manuf_name_lookup */
1348 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1350 guint8 masked_addr[6];
1355 if (wka_hashtable == NULL) {
1358 /* Get the part of the address covered by the mask. */
1359 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1360 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1361 /* Mask out the first masked octet */
1362 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1364 /* Zero out completely-masked-out octets */
1368 name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
1372 } /* wka_name_lookup */
1375 guint get_hash_ether_status(hashether_t* ether)
1377 return ether->status;
1380 char* get_hash_ether_hexaddr(hashether_t* ether)
1382 return ether->hexaddr;
1385 char* get_hash_ether_resolved_name(hashether_t* ether)
1387 return ether->resolved_name;
1391 eth_addr_hash(gconstpointer key)
1393 return wmem_strong_hash((const guint8 *)key, 6);
1397 eth_addr_cmp(gconstpointer a, gconstpointer b)
1399 return (memcmp(a, b, 6) == 0);
1403 initialize_ethers(void)
1409 /* hash table initialization */
1410 wka_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, g_free, g_free);
1411 manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
1412 eth_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, NULL, g_free);
1414 /* Compute the pathname of the ethers file. */
1415 if (g_ethers_path == NULL) {
1416 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1417 get_systemfile_dir(), ENAME_ETHERS);
1420 /* Set g_pethers_path here, but don't actually do anything
1421 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1423 if (g_pethers_path == NULL)
1424 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1426 /* Compute the pathname of the manuf file */
1427 manuf_path = get_datafile_path(ENAME_MANUF);
1429 /* Read it and initialize the hash table */
1430 set_ethent(manuf_path);
1432 while ((eth = get_ethent(&mask, TRUE))) {
1433 add_manuf_name(eth->addr, mask, eth->name);
1440 } /* initialize_ethers */
1442 /* this is only needed when shuting down application (if at all) */
1444 eth_name_lookup_cleanup(void)
1447 if (manuf_hashtable) {
1448 g_hash_table_destroy(manuf_hashtable);
1449 manuf_hashtable = NULL;
1451 if (wka_hashtable) {
1452 g_hash_table_destroy(wka_hashtable);
1453 wka_hashtable = NULL;
1456 if (eth_hashtable) {
1457 g_hash_table_destroy(eth_hashtable);
1458 eth_hashtable = NULL;
1463 /* Resolve ethernet address */
1464 static hashether_t *
1465 eth_addr_resolve(hashether_t *tp) {
1467 hashmanuf_t *manuf_value;
1468 const guint8 *addr = tp->addr;
1470 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1471 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1472 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1479 /* Unknown name. Try looking for it in the well-known-address
1480 tables for well-known address ranges smaller than 2^24. */
1483 /* Only the topmost 5 bytes participate fully */
1484 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1485 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1486 name, addr[5] & (0xFF >> mask));
1487 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1497 /* Only the topmost 4 bytes participate fully */
1498 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1499 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1500 name, addr[4] & (0xFF >> mask), addr[5]);
1501 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1511 /* Only the topmost 3 bytes participate fully */
1512 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1513 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1514 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1515 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1523 /* Now try looking in the manufacturer table. */
1524 manuf_value = manuf_name_lookup(addr);
1525 if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1526 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1527 manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1528 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1532 /* Now try looking for it in the well-known-address
1533 tables for well-known address ranges larger than 2^24. */
1536 /* Only the topmost 2 bytes participate fully */
1537 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1538 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1539 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1541 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1551 /* Only the topmost byte participates fully */
1552 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1553 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1554 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1556 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1564 for (mask = 7; mask > 0; mask--) {
1565 /* Not even the topmost byte participates fully */
1566 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1567 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1568 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1569 addr[3], addr[4], addr[5]);
1570 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1575 /* No match whatsoever. */
1576 set_address(ðer_addr, AT_ETHER, 6, addr);
1577 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1578 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1581 g_assert_not_reached();
1582 } /* eth_addr_resolve */
1584 static hashether_t *
1585 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1590 tp = g_new(hashether_t, 1);
1591 memcpy(tp->addr, addr, sizeof(tp->addr));
1592 tp->status = HASHETHER_STATUS_UNRESOLVED;
1593 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1594 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1596 tp->resolved_name[0] = '\0';
1599 eth_addr_resolve(tp);
1601 g_hash_table_insert(eth_hashtable, tp->addr, tp);
1604 } /* eth_hash_new_entry */
1606 static hashether_t *
1607 add_eth_name(const guint8 *addr, const gchar *name)
1611 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1614 tp = eth_hash_new_entry(addr, FALSE);
1617 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1618 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1619 new_resolved_objects = TRUE;
1622 } /* add_eth_name */
1624 static hashether_t *
1625 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1629 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1631 tp = eth_hash_new_entry(addr, resolve);
1633 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1634 eth_addr_resolve(tp); /* Found but needs to be resolved */
1640 } /* eth_name_lookup */
1643 eth_addr_lookup(const gchar *name _U_)
1646 /* XXX Do we need reverse lookup??? */
1649 hashether_t **table = eth_table;
1652 /* to be optimized (hash table from name to addr) */
1653 for (i = 0; i < HASHETHSIZE; i++) {
1656 if (strcmp(tp->resolved_name, name) == 0)
1662 /* not in hash table : performs a file lookup */
1664 if ((eth = get_ethbyname(name)) == NULL)
1667 /* add new entry in hash table */
1669 tp = add_eth_name(eth->addr, name);
1675 } /* eth_addr_lookup */
1680 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1683 * We allow three address separators (':', '-', and '.'),
1684 * as well as no separators
1688 guint32 a, a0, a1, a2, a3;
1689 gboolean found_single_number = FALSE;
1691 if ((cp = strchr(line, '#')))
1694 if ((cp = strtok(line, " \t\n")) == NULL)
1697 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1698 * fill a and found_single_number is TRUE,
1701 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
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", &a) == 1) {
1705 found_single_number = TRUE;
1714 if ((cp = strtok(NULL, " \t\n")) == NULL)
1717 if (found_single_number) {
1721 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1724 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1728 } /* parse_ipxnets_line */
1730 static FILE *ipxnet_p = NULL;
1733 set_ipxnetent(char *path)
1738 ipxnet_p = ws_fopen(path, "r");
1754 static ipxnet_t ipxnet;
1755 static int size = 0;
1756 static char *buf = NULL;
1758 if (ipxnet_p == NULL)
1761 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1762 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1769 } /* get_ipxnetent */
1774 get_ipxnetbyname(const gchar *name)
1778 set_ipxnetent(g_ipxnets_path);
1780 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1783 if (ipxnet == NULL) {
1786 set_ipxnetent(g_pipxnets_path);
1788 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1796 } /* get_ipxnetbyname */
1800 get_ipxnetbyaddr(guint32 addr)
1804 set_ipxnetent(g_ipxnets_path);
1806 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1808 if (ipxnet == NULL) {
1811 set_ipxnetent(g_pipxnets_path);
1813 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1821 } /* get_ipxnetbyaddr */
1824 initialize_ipxnets(void)
1826 /* Compute the pathname of the ipxnets file.
1828 * XXX - is there a notion of an "ipxnets file" in any flavor of
1829 * UNIX, or with any add-on Netware package for UNIX? If not,
1830 * should the UNIX version of the ipxnets file be in the datafile
1831 * directory as well?
1833 if (g_ipxnets_path == NULL) {
1834 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1835 get_systemfile_dir(), ENAME_IPXNETS);
1838 /* Set g_pipxnets_path here, but don't actually do anything
1839 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1841 if (g_pipxnets_path == NULL)
1842 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1844 } /* initialize_ipxnets */
1847 ipx_name_lookup_cleanup(void)
1849 if (ipxnet_hash_table) {
1850 g_hash_table_destroy(ipxnet_hash_table);
1851 ipxnet_hash_table = NULL;
1857 static hashipxnet_t *
1858 add_ipxnet_name(guint addr, const gchar *name)
1862 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1864 g_strlcpy(tp->name, name, MAXNAMELEN);
1868 key = (int *)g_new(int, 1);
1870 tp = g_new(hashipxnet_t,1);
1871 g_strlcpy(tp->name, name, MAXNAMELEN);
1872 g_hash_table_insert(ipxnet_hash_table, key, tp);
1876 g_strlcpy(tp->name, name, MAXNAMELEN);
1878 new_resolved_objects = TRUE;
1882 } /* add_ipxnet_name */
1886 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1891 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1895 key = (int *)g_new(int, 1);
1897 tp = g_new(hashipxnet_t, 1);
1898 g_hash_table_insert(ipxnet_hash_table, key, tp);
1900 return wmem_strdup(allocator, tp->name);
1903 /* fill in a new entry */
1907 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1909 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1912 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1915 return wmem_strdup(allocator, tp->name);
1917 } /* ipxnet_name_lookup */
1920 ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
1925 /* XXX Do we need reverse lookup??? */
1928 hashipxnet_t **table = ipxnet_table;
1931 /* to be optimized (hash table from name to addr) */
1932 for (i = 0; i < HASHIPXNETSIZE; i++) {
1935 if (strcmp(tp->name, name) == 0) {
1943 /* not in hash table : performs a file lookup */
1945 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1950 /* add new entry in hash table */
1952 tp = add_ipxnet_name(ipxnet->addr, name);
1957 } /* ipxnet_addr_lookup */
1960 read_hosts_file (const char *hostspath, gboolean store_entries)
1966 guint32 host_addr[4]; /* IPv4 or IPv6 */
1967 struct e_in6_addr ip6_addr;
1968 gboolean is_ipv6, entry_found = FALSE;
1972 * See the hosts(4) or hosts(5) man page for hosts file format
1973 * (not available on all systems).
1975 if ((hf = ws_fopen(hostspath, "r")) == NULL)
1978 while (fgetline(&line, &size, hf) >= 0) {
1979 if ((cp = strchr(line, '#')))
1982 if ((cp = strtok(line, " \t")) == NULL)
1983 continue; /* no tokens in the line */
1985 ret = inet_pton(AF_INET6, cp, &host_addr);
1987 continue; /* error parsing */
1992 /* Not valid IPv6 - valid IPv4? */
1993 if (!str_to_ip(cp, &host_addr))
1998 if ((cp = strtok(NULL, " \t")) == NULL)
1999 continue; /* no host name */
2002 if (store_entries) {
2004 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2005 add_ipv6_name(&ip6_addr, cp);
2007 add_ipv4_name(host_addr[0], cp);
2011 * Add the aliases, too, if there are any.
2012 * XXX - except we only store the last one added. The name
2013 * resolver returns the first name in the hosts file, we should
2016 while ((cp = strtok(NULL, " \t")) != NULL) {
2018 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2019 add_ipv6_name(&ip6_addr, cp);
2021 add_ipv4_name(host_addr[0], cp);
2029 return entry_found ? TRUE : FALSE;
2030 } /* read_hosts_file */
2033 add_hosts_file (const char *hosts_file)
2035 gboolean found = FALSE;
2041 if (!extra_hosts_files)
2042 extra_hosts_files = g_ptr_array_new();
2044 for (i = 0; i < extra_hosts_files->len; i++) {
2045 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2050 g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
2051 return read_hosts_file (hosts_file, FALSE);
2057 add_ip_name_from_string (const char *addr, const char *name)
2059 guint32 host_addr[4]; /* IPv4 */
2060 struct e_in6_addr ip6_addr; /* IPv6 */
2063 resolved_ipv4_t *resolved_ipv4_entry;
2064 resolved_ipv6_t *resolved_ipv6_entry;
2066 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2068 /* Error parsing address */
2075 /* Not valid IPv6 - valid IPv4? */
2076 if (!str_to_ip(addr, &host_addr))
2077 return FALSE; /* no */
2082 resolved_ipv6_entry = g_new(resolved_ipv6_t, 1);
2083 memcpy(&(resolved_ipv6_entry->ip6_addr), &ip6_addr, 16);
2084 g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2085 manually_resolved_ipv6_list = g_slist_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2087 resolved_ipv4_entry = g_new(resolved_ipv4_t, 1);
2088 resolved_ipv4_entry->host_addr = host_addr[0];
2089 g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2090 manually_resolved_ipv4_list = g_slist_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2094 } /* add_ip_name_from_string */
2097 * Add the resolved addresses that are in use to the list used to create the NRB
2100 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2102 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2103 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2105 if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
2106 lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
2112 * Add the resolved addresses that are in use to the list used to create the NRB
2116 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2118 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2119 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2121 if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
2122 lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2128 get_addrinfo_list(void) {
2130 if (ipv4_hash_table) {
2131 g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2134 if (ipv6_hash_table) {
2135 g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2138 return &addrinfo_lists;
2141 /* Read in a list of subnet definition - name pairs.
2142 * <line> = <comment> | <entry> | <whitespace>
2143 * <comment> = <whitespace>#<any>
2144 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2145 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2146 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2147 * <subnet_mask_length> is a decimal 1-31
2148 * <subnet_name> is a string containing no whitespace.
2149 * <whitespace> = (space | tab)+
2150 * Any malformed entries are ignored.
2151 * Any trailing data after the subnet_name is ignored.
2156 read_subnets_file (const char *subnetspath)
2162 guint32 host_addr; /* IPv4 ONLY */
2165 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2168 while (fgetline(&line, &size, hf) >= 0) {
2169 if ((cp = strchr(line, '#')))
2172 if ((cp = strtok(line, " \t")) == NULL)
2173 continue; /* no tokens in the line */
2176 /* Expected format is <IP4 address>/<subnet length> */
2177 cp2 = strchr(cp, '/');
2182 *cp2 = '\0'; /* Cut token */
2185 /* Check if this is a valid IPv4 address */
2186 if (!str_to_ip(cp, &host_addr)) {
2190 mask_length = atoi(cp2);
2191 if (0 >= mask_length || mask_length > 32) {
2192 continue; /* invalid mask length */
2195 if ((cp = strtok(NULL, " \t")) == NULL)
2196 continue; /* no subnet name */
2198 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2204 } /* read_subnets_file */
2206 static subnet_entry_t
2207 subnet_lookup(const guint32 addr)
2209 subnet_entry_t subnet_entry;
2212 /* Search mask lengths linearly, longest first */
2214 i = SUBNETLENGTHSIZE;
2215 while(have_subnet_entry && i > 0) {
2216 guint32 masked_addr;
2217 subnet_length_entry_t* length_entry;
2219 /* Note that we run from 31 (length 32) to 0 (length 1) */
2221 g_assert(i < SUBNETLENGTHSIZE);
2224 length_entry = &subnet_length_entries[i];
2226 if (NULL != length_entry->subnet_addresses) {
2227 sub_net_hashipv4_t * tp;
2230 masked_addr = addr & length_entry->mask;
2231 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2233 tp = length_entry->subnet_addresses[hash_idx];
2234 while(tp != NULL && tp->addr != masked_addr) {
2239 subnet_entry.mask = length_entry->mask;
2240 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2241 subnet_entry.name = tp->name;
2242 return subnet_entry;
2247 subnet_entry.mask = 0;
2248 subnet_entry.mask_length = 0;
2249 subnet_entry.name = NULL;
2251 return subnet_entry;
2254 /* Add a subnet-definition - name pair to the set.
2255 * The definition is taken by masking the address passed in with the mask of the
2259 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2261 subnet_length_entry_t* entry;
2262 sub_net_hashipv4_t * tp;
2265 g_assert(mask_length > 0 && mask_length <= 32);
2267 entry = &subnet_length_entries[mask_length - 1];
2269 subnet_addr &= entry->mask;
2271 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2273 if (NULL == entry->subnet_addresses) {
2274 entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2277 if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2278 sub_net_hashipv4_t * new_tp;
2281 if (tp->addr == subnet_addr) {
2282 return; /* XXX provide warning that an address was repeated? */
2288 new_tp = g_new(sub_net_hashipv4_t, 1);
2292 tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
2296 tp->addr = subnet_addr;
2297 /* Clear DUMMY_ADDRESS_ENTRY */
2298 tp->flags &= ~DUMMY_ADDRESS_ENTRY; /*Never used again...*/
2299 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2300 have_subnet_entry = TRUE;
2304 subnet_name_lookup_init(void)
2309 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2310 guint32 length = i + 1;
2312 subnet_length_entries[i].subnet_addresses = NULL;
2313 subnet_length_entries[i].mask_length = length;
2314 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2317 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2318 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2319 report_open_failure(subnetspath, errno, FALSE);
2321 g_free(subnetspath);
2324 * Load the global subnets file, if we have one.
2326 subnetspath = get_datafile_path(ENAME_SUBNETS);
2327 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2328 report_open_failure(subnetspath, errno, FALSE);
2330 g_free(subnetspath);
2334 cleanup_subnet_entry(sub_net_hashipv4_t* entry)
2336 if ((entry != NULL) && (entry->next != NULL)) {
2337 cleanup_subnet_entry(entry->next);
2344 * External Functions
2348 addr_resolve_pref_init(module_t *nameres)
2350 prefs_register_bool_preference(nameres, "mac_name",
2351 "Resolve MAC addresses",
2352 "Resolve Ethernet MAC address to manufacturer names",
2353 &gbl_resolv_flags.mac_name);
2355 prefs_register_bool_preference(nameres, "transport_name",
2356 "Resolve transport names",
2357 "Resolve TCP/UDP ports into service names",
2358 &gbl_resolv_flags.transport_name);
2360 prefs_register_bool_preference(nameres, "network_name",
2361 "Resolve network (IP) addresses",
2362 "Resolve IPv4, IPv6, and IPX addresses into host names."
2363 " The next set of check boxes determines how name resolution should be performed."
2364 " If no other options are checked name resolution is made from Wireshark's host file,"
2365 " capture file name resolution blocks and DNS packets in the capture.",
2366 &gbl_resolv_flags.network_name);
2368 prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
2369 "Use captured DNS packet data for address resolution",
2370 "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
2371 &gbl_resolv_flags.dns_pkt_addr_resolution);
2373 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2374 "Use an external network name resolver",
2375 "Use your system's configured name resolver"
2376 " (usually DNS) to resolve network names."
2377 " Only applies when network name resolution"
2379 &gbl_resolv_flags.use_external_net_name_resolver);
2381 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
2382 prefs_register_bool_preference(nameres, "concurrent_dns",
2383 "Enable concurrent DNS name resolution",
2384 "Enable concurrent DNS name resolution. Only"
2385 " applies when network name resolution is"
2386 " enabled. You probably want to enable this.",
2387 &gbl_resolv_flags.concurrent_dns);
2389 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2390 "Maximum concurrent requests",
2391 "The maximum number of DNS requests that may"
2392 " be active at any time. A large value (many"
2393 " thousands) might overload the network or make"
2394 " your DNS server behave badly.",
2396 &name_resolve_concurrency);
2398 prefs_register_static_text_preference(nameres, "concurrent_dns",
2399 "Enable concurrent DNS name resolution: N/A",
2400 "Support for concurrent DNS name resolution was not"
2401 " compiled into this version of Wireshark");
2404 prefs_register_bool_preference(nameres, "hosts_file_handling",
2405 "Only use the profile \"hosts\" file",
2406 "By default \"hosts\" files will be loaded from multiple sources."
2407 " Checking this box only loads the \"hosts\" in the current profile.",
2408 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2413 disable_name_resolution(void) {
2414 gbl_resolv_flags.mac_name = FALSE;
2415 gbl_resolv_flags.network_name = FALSE;
2416 gbl_resolv_flags.transport_name = FALSE;
2417 gbl_resolv_flags.concurrent_dns = FALSE;
2418 gbl_resolv_flags.dns_pkt_addr_resolution = FALSE;
2419 gbl_resolv_flags.use_external_net_name_resolver = FALSE;
2424 host_name_lookup_process(void) {
2425 async_dns_queue_msg_t *caqm;
2426 struct timeval tv = { 0, 0 };
2429 gboolean nro = new_resolved_objects;
2431 new_resolved_objects = FALSE;
2433 if (!async_dns_initialized)
2434 /* c-ares not initialized. Bail out and cancel timers. */
2437 async_dns_queue_head = g_list_first(async_dns_queue_head);
2439 while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2440 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2441 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2442 if (caqm->family == AF_INET) {
2443 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2444 c_ares_ghba_cb, caqm);
2445 async_dns_in_flight++;
2446 } else if (caqm->family == AF_INET6) {
2447 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2448 AF_INET6, c_ares_ghba_cb, caqm);
2449 async_dns_in_flight++;
2455 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2457 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2458 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2461 ares_process(ghba_chan, &rfds, &wfds);
2464 /* Any new entries? */
2469 _host_name_lookup_cleanup(void) {
2472 cur = g_list_first(async_dns_queue_head);
2475 cur = g_list_next (cur);
2478 g_list_free(async_dns_queue_head);
2479 async_dns_queue_head = NULL;
2481 if (async_dns_initialized) {
2482 ares_destroy(ghba_chan);
2483 ares_destroy(ghbn_chan);
2485 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2486 ares_library_cleanup();
2488 async_dns_initialized = FALSE;
2491 #elif defined(HAVE_GNU_ADNS)
2493 /* XXX - The ADNS "documentation" isn't very clear:
2494 * - Do we need to keep our query structures around?
2497 host_name_lookup_process(void) {
2498 async_dns_queue_msg_t *almsg;
2500 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2505 gboolean nro = new_resolved_objects;
2507 new_resolved_objects = FALSE;
2508 async_dns_queue_head = g_list_first(async_dns_queue_head);
2510 cur = async_dns_queue_head;
2511 while (cur && async_dns_in_flight <= name_resolve_concurrency) {
2512 almsg = (async_dns_queue_msg_t *) cur->data;
2513 if (! almsg->submitted && almsg->type == AF_INET) {
2514 addr_bytes = (guint8 *) &almsg->ip4_addr;
2515 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2516 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2517 /* XXX - what if it fails? */
2518 adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
2519 almsg->submitted = TRUE;
2520 async_dns_in_flight++;
2525 cur = async_dns_queue_head;
2528 almsg = (async_dns_queue_msg_t *) cur->data;
2529 if (almsg->submitted) {
2530 ret = adns_check(ads, &almsg->query, &ans, NULL);
2532 if (ans->status == adns_s_ok) {
2533 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2540 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2542 /* XXX, what to do if async_dns_in_flight == 0? */
2543 async_dns_in_flight--;
2547 /* Keep the timeout in place */
2552 _host_name_lookup_cleanup(void) {
2555 async_dns_queue_head = g_list_first(async_dns_queue_head);
2556 while (async_dns_queue_head) {
2557 qdata = async_dns_queue_head->data;
2558 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2562 if (async_dns_initialized)
2564 async_dns_initialized = FALSE;
2567 #else /* HAVE_GNU_ADNS */
2570 host_name_lookup_process(void) {
2571 gboolean nro = new_resolved_objects;
2573 new_resolved_objects = FALSE;
2579 _host_name_lookup_cleanup(void) {
2582 #endif /* HAVE_C_ARES */
2585 get_hostname(const guint addr)
2587 /* XXX why do we call this if we're not resolving? To create hash entries?
2590 hashipv4_t *tp = host_lookup(addr);
2592 if (!gbl_resolv_flags.network_name)
2595 tp->flags |= RESOLVED_ADDRESS_USED;
2600 /* -------------------------- */
2603 get_hostname6(const struct e_in6_addr *addr)
2605 /* XXX why do we call this if we're not resolving? To create hash entries?
2608 hashipv6_t *tp = host_lookup6(addr);
2610 if (!gbl_resolv_flags.network_name)
2613 tp->flags |= RESOLVED_ADDRESS_USED;
2618 /* -------------------------- */
2620 add_ipv4_name(const guint addr, const gchar *name)
2625 * Don't add zero-length names; apparently, some resolvers will return
2626 * them if they get them from DNS.
2628 if (!name || name[0] == '\0')
2631 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2633 tp = new_ipv4(addr);
2634 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2637 if (g_ascii_strcasecmp(tp->name, name)) {
2638 g_strlcpy(tp->name, name, MAXNAMELEN);
2639 new_resolved_objects = TRUE;
2641 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2642 } /* add_ipv4_name */
2644 /* -------------------------- */
2646 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2651 * Don't add zero-length names; apparently, some resolvers will return
2652 * them if they get them from DNS.
2654 if (!name || name[0] == '\0')
2657 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
2659 struct e_in6_addr *addr_key;
2661 addr_key = g_new(struct e_in6_addr,1);
2662 tp = new_ipv6(addrp);
2663 memcpy(addr_key, addrp, 16);
2664 g_hash_table_insert(ipv6_hash_table, addr_key, tp);
2667 if (g_ascii_strcasecmp(tp->name, name)) {
2668 g_strlcpy(tp->name, name, MAXNAMELEN);
2669 new_resolved_objects = TRUE;
2671 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2672 } /* add_ipv6_name */
2675 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2677 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2679 add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2683 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2685 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2687 add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2691 add_manually_resolved(void)
2693 if (manually_resolved_ipv4_list) {
2694 g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2697 if (manually_resolved_ipv6_list) {
2698 g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2703 host_name_lookup_init(void)
2708 #ifdef HAVE_GNU_ADNS
2711 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2712 static char rootpath_ot[] = "\\hosts";
2714 #endif /*GNU_ADNS */
2716 g_assert(ipxnet_hash_table == NULL);
2717 ipxnet_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
2719 g_assert(ipv4_hash_table == NULL);
2720 ipv4_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
2722 g_assert(ipv6_hash_table == NULL);
2723 ipv6_hash_table = g_hash_table_new_full(ipv6_oat_hash, ipv6_equal, g_free, g_free);
2726 * Load the global hosts file, if we have one.
2728 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2729 hostspath = get_datafile_path(ENAME_HOSTS);
2730 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2731 report_open_failure(hostspath, errno, FALSE);
2736 * Load the user's hosts file no matter what, if they have one.
2738 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2739 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2740 report_open_failure(hostspath, errno, FALSE);
2744 if (gbl_resolv_flags.concurrent_dns) {
2745 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2746 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2748 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2749 async_dns_initialized = TRUE;
2751 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2756 #ifdef HAVE_GNU_ADNS
2758 * We're using GNU ADNS, which doesn't check the system hosts file;
2759 * we load that file ourselves.
2763 sysroot = getenv_utf8("WINDIR");
2764 if (sysroot != NULL) {
2766 * The file should be under WINDIR.
2767 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2768 * %WINDIR%\system32\drivers\etc\hosts.
2769 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2771 * XXX - should we base it on the dwPlatformId value from
2774 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2775 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2776 if (!read_hosts_file(hostspath, TRUE)) {
2778 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2779 read_hosts_file(hostspath, TRUE);
2785 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2786 read_hosts_file("/etc/hosts", TRUE);
2790 if (gbl_resolv_flags.concurrent_dns) {
2791 /* XXX - Any flags we should be using? */
2792 /* XXX - We could provide config settings for DNS servers, and
2793 pass them to ADNS with adns_init_strcfg */
2794 if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
2796 * XXX - should we report the error? I'm assuming that some crashes
2797 * reported on a Windows machine with TCP/IP not configured are due
2798 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2799 * ADNS in a state where it crashes due to that. We'll still try
2800 * doing name resolution anyway.
2804 async_dns_initialized = TRUE;
2805 async_dns_in_flight = 0;
2807 #endif /* HAVE_GNU_ADNS */
2808 #endif /* HAVE_C_ARES */
2810 if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
2811 for (i = 0; i < extra_hosts_files->len; i++) {
2812 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
2816 subnet_name_lookup_init();
2818 add_manually_resolved();
2822 host_name_lookup_cleanup(void)
2825 _host_name_lookup_cleanup();
2827 if (ipxnet_hash_table) {
2828 g_hash_table_destroy(ipxnet_hash_table);
2829 ipxnet_hash_table = NULL;
2832 if (ipv4_hash_table) {
2833 g_hash_table_destroy(ipv4_hash_table);
2834 ipv4_hash_table = NULL;
2837 if (ipv6_hash_table) {
2838 g_hash_table_destroy(ipv6_hash_table);
2839 ipv6_hash_table = NULL;
2842 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2843 if (subnet_length_entries[i].subnet_addresses != NULL) {
2844 for (j = 0; j < HASHHOSTSIZE; j++) {
2845 if (subnet_length_entries[i].subnet_addresses[j] != NULL)
2847 cleanup_subnet_entry(subnet_length_entries[i].subnet_addresses[j]);
2850 g_free(subnet_length_entries[i].subnet_addresses);
2851 subnet_length_entries[i].subnet_addresses = NULL;
2855 have_subnet_entry = FALSE;
2856 new_resolved_objects = FALSE;
2860 free_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2862 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2864 g_free(resolved_ipv4_entry);
2868 free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2870 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2872 g_free(resolved_ipv6_entry);
2876 manually_resolve_cleanup(void)
2878 if (manually_resolved_ipv4_list) {
2879 g_slist_foreach(manually_resolved_ipv4_list, free_manually_resolved_ipv4, NULL);
2880 g_slist_free(manually_resolved_ipv4_list);
2881 manually_resolved_ipv4_list = NULL;
2884 if (manually_resolved_ipv6_list) {
2885 g_slist_foreach(manually_resolved_ipv6_list, free_manually_resolved_ipv6, NULL);
2886 g_slist_free(manually_resolved_ipv6_list);
2887 manually_resolved_ipv6_list = NULL;
2893 udp_port_to_display(wmem_allocator_t *allocator, guint port)
2896 if (!gbl_resolv_flags.transport_name) {
2897 return wmem_utoa(allocator, port);
2900 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
2902 } /* udp_port_to_display */
2905 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
2908 if (!gbl_resolv_flags.transport_name) {
2909 return wmem_utoa(allocator, port);
2912 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
2914 } /* dccp_port_to_display */
2917 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
2920 if (!gbl_resolv_flags.transport_name) {
2921 return wmem_utoa(allocator, port);
2924 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
2926 } /* tcp_port_to_display */
2929 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
2932 if (!gbl_resolv_flags.transport_name) {
2933 return wmem_utoa(allocator, port);
2936 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
2938 } /* sctp_port_to_display */
2941 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
2943 const gchar *port_str;
2945 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2946 /* No name resolution support, just return port string */
2947 return wmem_strdup_printf(scope, "%u", port);
2949 port_str = serv_name_lookup(proto, port);
2951 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
2955 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
2957 const gchar *port_str;
2959 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2960 /* No name resolution support, just return port string */
2961 return g_snprintf(buf, buf_size, "%u", port);
2963 port_str = serv_name_lookup(proto, port);
2965 return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
2969 get_ether_name(const guint8 *addr)
2972 gboolean resolve = gbl_resolv_flags.mac_name;
2974 tp = eth_name_lookup(addr, resolve);
2976 return resolve ? tp->resolved_name : tp->hexaddr;
2978 } /* get_ether_name */
2981 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
2983 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
2986 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2987 * If it's not found, simply return NULL.
2990 get_ether_name_if_known(const guint8 *addr)
2994 /* Initialize ether structs if we're the first
2995 * ether-related function called */
2996 if (!gbl_resolv_flags.mac_name)
2999 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3000 tp = eth_name_lookup(addr, TRUE);
3001 g_assert(tp != NULL);
3003 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3004 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
3005 return tp->resolved_name;
3008 /* Name was created */
3014 get_ether_addr(const gchar *name)
3017 /* force resolution (do not check gbl_resolv_flags) */
3018 return eth_addr_lookup(name);
3020 } /* get_ether_addr */
3023 add_ether_byip(const guint ip, const guint8 *eth)
3027 /* first check that IP address can be resolved */
3028 if (!gbl_resolv_flags.network_name)
3031 tp = host_lookup(ip);
3034 * Was this IP address resolved to a host name?
3036 if (tp->flags & NAME_RESOLVED) {
3038 * Yes, so add an entry in the ethers hashtable resolving
3039 * the MAC address to that name.
3041 add_eth_name(eth, tp->name);
3044 } /* add_ether_byip */
3047 ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct)
3049 gchar *buf = (gchar *)wmem_alloc(scope, 12);
3051 *dword_to_hex_punct(buf, ad, punct) = '\0';
3056 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3059 if (!gbl_resolv_flags.network_name) {
3060 return ipxnet_to_str_punct(allocator, addr, '\0');
3063 return ipxnet_name_lookup(allocator, addr);
3065 } /* get_ipxnet_name */
3068 get_ipxnet_addr(const gchar *name, gboolean *known)
3073 /* force resolution (do not check gbl_resolv_flags) */
3074 addr = ipxnet_addr_lookup(name, &success);
3079 } /* get_ipxnet_addr */
3082 get_manuf_name(const guint8 *addr)
3084 hashmanuf_t *manuf_value;
3086 manuf_value = manuf_name_lookup(addr);
3087 if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3088 return manuf_value->resolved_name;
3090 return manuf_value->hexaddr;
3092 } /* get_manuf_name */
3095 uint_get_manuf_name(const guint oid)
3099 addr[0] = (oid >> 16) & 0xFF;
3100 addr[1] = (oid >> 8) & 0xFF;
3101 addr[2] = (oid >> 0) & 0xFF;
3102 return get_manuf_name(addr);
3106 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3108 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3112 get_manuf_name_if_known(const guint8 *addr)
3114 hashmanuf_t *manuf_value;
3118 /* manuf needs only the 3 most significant octets of the ethernet address */
3119 manuf_key = addr[0];
3120 manuf_key = manuf_key<<8;
3122 manuf_key = manuf_key | oct;
3123 manuf_key = manuf_key<<8;
3125 manuf_key = manuf_key | oct;
3127 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3128 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3132 return manuf_value->resolved_name;
3134 } /* get_manuf_name_if_known */
3137 uint_get_manuf_name_if_known(const guint manuf_key)
3139 hashmanuf_t *manuf_value;
3141 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3142 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3146 return manuf_value->resolved_name;
3150 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3152 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3155 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3157 return manuf->resolved_name;
3161 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3163 guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3164 hashmanuf_t *manuf_value;
3167 /* Copy and convert the address to network byte order. */
3168 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3170 manuf_value = manuf_name_lookup(addr);
3171 if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3172 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]);
3174 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 wmem_free(NULL, addr);
3179 } /* eui64_to_display */
3182 #define GHI_TIMEOUT (250 * 1000)
3187 #if ( ( ARES_VERSION_MAJOR < 1 ) \
3188 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3197 * XXX - If we wanted to be really fancy we could cache results here and
3198 * look them up in get_host_ipaddr* below.
3200 async_hostent_t *ahp = (async_hostent_t *)arg;
3201 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3202 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3203 ahp->copied = hp->h_length;
3206 #endif /* HAVE_C_ARES */
3208 /* Translate a string, assumed either to be a dotted-quad IP address or
3209 * a host name, to a numeric IP address. Return TRUE if we succeed and
3210 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3211 * Used more in the dfilter parser rather than in packet dissectors */
3213 get_host_ipaddr(const char *host, guint32 *addrp)
3215 struct in_addr ipaddr;
3217 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3220 async_hostent_t ahe;
3221 #elif defined(HAVE_GETADDRINFO)
3222 struct addrinfo hint, *result = NULL;
3223 #elif defined(HAVE_GETHOSTBYNAME)
3228 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3229 * inet_pton(AF_INET) does not support hexadecimal notation nor
3230 * less-than-4 octet notation.
3232 if (!inet_aton(host, &ipaddr)) {
3234 /* It's not a valid dotted-quad IP address; is it a valid
3238 /* If we're not allowed to do name resolution, don't do name
3241 if (!gbl_resolv_flags.network_name ||
3242 !gbl_resolv_flags.use_external_net_name_resolver) {
3247 if (! (gbl_resolv_flags.concurrent_dns) ||
3248 name_resolve_concurrency < 1 ||
3249 ! async_dns_initialized) {
3252 ahe.addr_size = (int) sizeof (struct in_addr);
3255 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3258 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3260 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3261 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3262 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3265 ares_process(ghbn_chan, &rfds, &wfds);
3267 ares_cancel(ghbn_chan);
3268 if (ahe.addr_size == ahe.copied) {
3272 #elif defined(HAVE_GETADDRINFO)
3274 * This can be slow, particularly for capture files with lots of
3275 * addresses. Should we just return FALSE instead?
3277 memset(&hint, 0, sizeof(hint));
3278 hint.ai_family = AF_INET;
3279 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3280 /* Probably more checks than necessary */
3281 if (result != NULL) {
3282 gboolean ret_val = FALSE;
3283 if (result->ai_family == AF_INET && result->ai_addrlen == 4) {
3284 memcpy(&ipaddr, result->ai_addr->sa_data, result->ai_addrlen);
3287 freeaddrinfo(result);
3291 #elif defined(HAVE_GETHOSTBYNAME)
3292 hp = gethostbyname(host);
3296 /* Apparently, some versions of gethostbyaddr can
3297 * return IPv6 addresses. */
3298 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3299 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3305 /* Does the string really contain dotted-quad IP?
3306 * Check against inet_atons that accept strings such as
3307 * "130.230" as valid addresses and try to convert them
3308 * to some form of a classful (host.net) notation.
3310 unsigned int a0, a1, a2, a3;
3311 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3315 *addrp = ipaddr.s_addr;
3320 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3321 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3322 * return FALSE if we fail.
3325 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3328 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3331 async_hostent_t ahe;
3332 #elif defined(HAVE_GETADDRINFO)
3333 struct addrinfo hint, *result = NULL;
3334 #elif defined(HAVE_GETHOSTBYNAME2)
3336 #endif /* HAVE_C_ARES */
3338 if (str_to_ip6(host, addrp))
3341 /* It's not a valid dotted-quad IP address; is it a valid
3345 /* If we're not allowed to do name resolution, don't do name
3348 if (!gbl_resolv_flags.network_name ||
3349 !gbl_resolv_flags.use_external_net_name_resolver) {
3355 if (! (gbl_resolv_flags.concurrent_dns) ||
3356 name_resolve_concurrency < 1 ||
3357 ! async_dns_initialized) {
3360 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3363 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3366 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3368 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3369 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3370 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3373 ares_process(ghbn_chan, &rfds, &wfds);
3375 ares_cancel(ghbn_chan);
3376 if (ahe.addr_size == ahe.copied) {
3379 #elif defined(HAVE_GETADDRINFO)
3381 * This can be slow, particularly for capture files with lots of
3382 * addresses. Should we just return FALSE instead?
3384 memset(&hint, 0, sizeof(hint));
3385 hint.ai_family = AF_INET6;
3386 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3387 /* Probably more checks than necessary */
3388 if (result != NULL) {
3389 gboolean ret_val = FALSE;
3390 if (result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct e_in6_addr)) {
3391 memcpy(addrp, result->ai_addr->sa_data, result->ai_addrlen);
3394 freeaddrinfo(result);
3398 #elif defined(HAVE_GETHOSTBYNAME2)
3399 hp = gethostbyname2(host, AF_INET6);
3400 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3401 memcpy(addrp, hp->h_addr, hp->h_length);
3410 * Find out whether a hostname resolves to an ip or ipv6 address
3411 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3412 * that we don't know)
3415 host_ip_af(const char *host
3416 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETHOSTBYNAME2)
3421 const char *af = "ip";
3422 #ifdef HAVE_GETADDRINFO
3423 struct addrinfo hint, *result = NULL;
3424 memset(&hint, 0, sizeof(hint));
3425 hint.ai_family = AF_UNSPEC;
3426 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3427 if (result->ai_family == AF_INET6) {
3430 freeaddrinfo(result);
3432 #elif defined(HAVE_GETHOSTBYNAME2)
3434 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
3440 get_manuf_hashtable(void)
3442 return manuf_hashtable;
3446 get_wka_hashtable(void)
3448 return wka_hashtable;
3452 get_eth_hashtable(void)
3454 return eth_hashtable;
3458 get_serv_port_hashtable(void)
3460 return serv_port_hashtable;
3464 get_ipxnet_hash_table(void)
3466 return ipxnet_hash_table;
3470 get_ipv4_hash_table(void)
3472 return ipv4_hash_table;
3476 get_ipv6_hash_table(void)
3478 return ipv6_hash_table;
3480 /* Initialize all the address resolution subsystems in this file */
3482 addr_resolv_init(void)
3484 initialize_services();
3485 initialize_ethers();
3486 initialize_ipxnets();
3487 /* host name initialization is done on a per-capture-file basis */
3488 /*host_name_lookup_init();*/
3491 /* Clean up all the address resolution subsystems in this file */
3493 addr_resolv_cleanup(void)
3495 service_name_lookup_cleanup();
3496 eth_name_lookup_cleanup();
3497 ipx_name_lookup_cleanup();
3498 /* host name initialization is done on a per-capture-file basis */
3499 /*host_name_lookup_cleanup();*/
3503 str_to_ip(const char *str, void *dst)
3505 return inet_pton(AF_INET, str, dst) > 0;
3509 str_to_ip6(const char *str, void *dst)
3511 return inet_pton(AF_INET6, str, dst) > 0;
3515 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3520 * indent-tabs-mode: nil
3523 * vi: set shiftwidth=4 tabstop=8 expandtab:
3524 * :indentSize=4:tabSize=8:noTabs=true: