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"
96 #if defined(_WIN32) && defined(INET6)
97 # include <ws2tcpip.h>
101 # if defined(_WIN32) && !defined(INET6)
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"
121 #include "ipv6-utils.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);
762 #if ( ( ARES_VERSION_MAJOR < 1 ) \
763 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
771 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
775 /* XXX, what to do if async_dns_in_flight == 0? */
776 async_dns_in_flight--;
778 if (status == ARES_SUCCESS) {
779 for (p = he->h_addr_list; *p != NULL; p++) {
780 switch(caqm->family) {
782 add_ipv4_name(caqm->addr.ip4, he->h_name);
785 add_ipv6_name(&caqm->addr.ip6, he->h_name);
788 /* Throw an exception? */
795 #endif /* HAVE_C_ARES */
797 /* --------------- */
799 new_ipv4(const guint addr)
801 hashipv4_t *tp = g_new(hashipv4_t, 1);
805 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
810 host_lookup(const guint addr, gboolean *found)
812 hashipv4_t * volatile tp;
816 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
819 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
821 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
824 if (tp->flags & DUMMY_ADDRESS_ENTRY) {
831 if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
832 tp->flags |= TRIED_RESOLVE_ADDRESS;
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.
842 fill_dummy_ip4(addr, tp);
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
852 * Presumably getaddrinfo() behaves the same way. Anyway, we should
853 * never get to this code on Windows since those builds include c-ares.
855 #elif defined(HAVE_GETADDRINFO)
857 struct sockaddr_in sin;
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) {
868 #elif defined(HAVE_GETHOSTBYNAME)
870 struct hostent *hostp;
872 hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
874 if (hostp != NULL && hostp->h_name[0] != '\0') {
875 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
881 /* unknown host or DNS timeout */
885 fill_dummy_ip4(addr, tp);
890 /* --------------- */
892 new_ipv6(const struct e_in6_addr *addr)
894 hashipv6_t *tp = g_new(hashipv6_t,1);
895 memcpy(tp->addr, addr->bytes, sizeof tp->addr);
898 ip6_to_str_buf(addr, tp->ip6);
902 /* ------------------------------------ */
904 host_lookup6(const struct e_in6_addr *addr, gboolean *found)
906 hashipv6_t * volatile tp;
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;
919 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
921 struct e_in6_addr *addr_key;
923 addr_key = g_new(struct e_in6_addr,1);
925 memcpy(addr_key, addr, 16);
926 g_hash_table_insert(ipv6_hash_table, addr_key, tp);
928 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
931 if (tp->flags & DUMMY_ADDRESS_ENTRY) {
938 if (gbl_resolv_flags.network_name &&
939 gbl_resolv_flags.use_external_net_name_resolver) {
940 tp->flags |= TRIED_RESOLVE_ADDRESS;
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);
951 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
952 * going to risk changing the semantics.
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;
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) {
970 #elif defined(HAVE_GETHOSTBYNAME)
971 /* Quick hack to avoid DNS/YP timeout */
972 hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
974 if (hostp != NULL && hostp->h_name[0] != '\0') {
975 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
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);
993 * Ethernet / manufacturer resolution
995 * The following functions implement ethernet address resolution and
996 * ethers files parsing (see ethers(4)).
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).
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.
1011 * So the following functions do _not_ behave as the standard ones.
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
1023 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1024 const gboolean manuf_file)
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))
1035 num = strtoul(cp, &p, 16);
1037 return FALSE; /* failed */
1039 return FALSE; /* not a valid octet */
1040 eth->addr[i] = (guint8) num;
1041 cp = p; /* skip past the number */
1043 /* OK, what character terminated the octet? */
1045 /* "/" - this has a mask. */
1047 /* Entries with masks are allowed only in the "manuf" files. */
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);
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 */
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));
1068 /* Mask out completely-masked-out octets */
1074 /* We're at the end of the address, and there's no mask. */
1076 /* We got 3 bytes, so this is a manufacturer ID. */
1078 /* Manufacturer IDs are only allowed in the "manuf"
1082 /* Indicate that this is a manufacturer ID (0 is not allowed
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). */
1097 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1102 /* We don't know the separator used in this number; it can either
1103 be ':', '-', or '.'. */
1104 if (*cp != ':' && *cp != '-' && *cp != '.')
1106 sep = *cp; /* subsequent separators must be the same */
1108 /* It has to be the same as the first separator */
1120 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1121 const gboolean manuf_file)
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.
1132 if ((cp = strchr(line, '#')))
1135 if ((cp = strtok(line, " \t")) == NULL)
1138 if (!parse_ether_address(cp, eth, mask, manuf_file))
1141 if ((cp = strtok(NULL, " \t")) == NULL)
1144 g_strlcpy(eth->name, cp, MAXNAMELEN);
1148 } /* parse_ether_line */
1150 static FILE *eth_p = NULL;
1153 set_ethent(char *path)
1158 eth_p = ws_fopen(path, "r");
1171 get_ethent(unsigned int *mask, const gboolean manuf_file)
1175 static int size = 0;
1176 static char *buf = NULL;
1181 while (fgetline(&buf, &size, eth_p) >= 0) {
1182 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1193 get_ethbyname(const gchar *name)
1197 set_ethent(g_pethers_path);
1199 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1205 set_ethent(g_ethers_path);
1207 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1215 } /* get_ethbyname */
1219 get_ethbyaddr(const guint8 *addr)
1224 set_ethent(g_pethers_path);
1226 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1232 set_ethent(g_ethers_path);
1234 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1242 } /* get_ethbyaddr */
1244 static hashmanuf_t *manuf_hash_new_entry(const guint8 *addr, char* name)
1247 hashmanuf_t *manuf_value;
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);
1255 memcpy(manuf_value->addr, addr, 3);
1256 manuf_value->status = (name != NULL) ? HASHETHER_STATUS_RESOLVED_NAME : HASHETHER_STATUS_UNRESOLVED;
1258 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1259 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1262 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1263 manuf_value->resolved_name[0] = '\0';
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), ':');
1269 g_hash_table_insert(manuf_hashtable, manuf_key, manuf_value);
1274 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
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?
1285 /* This is a well-known MAC address; just add this to the Ethernet
1287 add_eth_name(addr, name);
1292 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1293 manuf_hash_new_entry(addr, name);
1297 /* This is a range of well-known addresses; add it to the appropriate
1298 well-known-address table, creating that table if necessary. */
1300 wka_key = (guint8 *)g_malloc(6);
1301 memcpy(wka_key, addr, 6);
1303 g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
1305 } /* add_manuf_name */
1307 static hashmanuf_t *
1308 manuf_name_lookup(const guint8 *addr)
1310 gint32 manuf_key = 0;
1312 hashmanuf_t *manuf_value;
1314 /* manuf needs only the 3 most significant octets of the ethernet address */
1315 manuf_key = addr[0];
1316 manuf_key = manuf_key<<8;
1318 manuf_key = manuf_key | oct;
1319 manuf_key = manuf_key<<8;
1321 manuf_key = manuf_key | oct;
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) {
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) {
1343 /* Add the address as a hex string */
1344 return manuf_hash_new_entry(addr, NULL);
1346 } /* manuf_name_lookup */
1349 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1351 guint8 masked_addr[6];
1356 if (wka_hashtable == NULL) {
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));
1365 /* Zero out completely-masked-out octets */
1369 name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
1373 } /* wka_name_lookup */
1376 guint get_hash_ether_status(hashether_t* ether)
1378 return ether->status;
1381 char* get_hash_ether_hexaddr(hashether_t* ether)
1383 return ether->hexaddr;
1386 char* get_hash_ether_resolved_name(hashether_t* ether)
1388 return ether->resolved_name;
1392 eth_addr_hash(gconstpointer key)
1394 return wmem_strong_hash((const guint8 *)key, 6);
1398 eth_addr_cmp(gconstpointer a, gconstpointer b)
1400 return (memcmp(a, b, 6) == 0);
1404 initialize_ethers(void)
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);
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);
1421 /* Set g_pethers_path here, but don't actually do anything
1422 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1424 if (g_pethers_path == NULL)
1425 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1427 /* Compute the pathname of the manuf file */
1428 manuf_path = get_datafile_path(ENAME_MANUF);
1430 /* Read it and initialize the hash table */
1431 set_ethent(manuf_path);
1433 while ((eth = get_ethent(&mask, TRUE))) {
1434 add_manuf_name(eth->addr, mask, eth->name);
1441 } /* initialize_ethers */
1443 /* this is only needed when shuting down application (if at all) */
1445 eth_name_lookup_cleanup(void)
1448 if (manuf_hashtable) {
1449 g_hash_table_destroy(manuf_hashtable);
1450 manuf_hashtable = NULL;
1452 if (wka_hashtable) {
1453 g_hash_table_destroy(wka_hashtable);
1454 wka_hashtable = NULL;
1457 if (eth_hashtable) {
1458 g_hash_table_destroy(eth_hashtable);
1459 eth_hashtable = NULL;
1464 /* Resolve ethernet address */
1465 static hashether_t *
1466 eth_addr_resolve(hashether_t *tp) {
1468 hashmanuf_t *manuf_value;
1469 const guint8 *addr = tp->addr;
1471 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1472 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1473 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1480 /* Unknown name. Try looking for it in the well-known-address
1481 tables for well-known address ranges smaller than 2^24. */
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;
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;
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;
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;
1533 /* Now try looking for it in the well-known-address
1534 tables for well-known address ranges larger than 2^24. */
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],
1542 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
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],
1557 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
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;
1576 /* No match whatsoever. */
1577 set_address(ðer_addr, AT_ETHER, 6, addr);
1578 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1579 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1582 g_assert_not_reached();
1583 } /* eth_addr_resolve */
1585 static hashether_t *
1586 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
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), ':');
1597 tp->resolved_name[0] = '\0';
1600 eth_addr_resolve(tp);
1602 g_hash_table_insert(eth_hashtable, tp->addr, tp);
1605 } /* eth_hash_new_entry */
1607 static hashether_t *
1608 add_eth_name(const guint8 *addr, const gchar *name)
1612 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1615 tp = eth_hash_new_entry(addr, FALSE);
1618 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1619 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1620 new_resolved_objects = TRUE;
1623 } /* add_eth_name */
1625 static hashether_t *
1626 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1630 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1632 tp = eth_hash_new_entry(addr, resolve);
1634 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1635 eth_addr_resolve(tp); /* Found but needs to be resolved */
1641 } /* eth_name_lookup */
1644 eth_addr_lookup(const gchar *name _U_)
1647 /* XXX Do we need reverse lookup??? */
1650 hashether_t **table = eth_table;
1653 /* to be optimized (hash table from name to addr) */
1654 for (i = 0; i < HASHETHSIZE; i++) {
1657 if (strcmp(tp->resolved_name, name) == 0)
1663 /* not in hash table : performs a file lookup */
1665 if ((eth = get_ethbyname(name)) == NULL)
1668 /* add new entry in hash table */
1670 tp = add_eth_name(eth->addr, name);
1676 } /* eth_addr_lookup */
1681 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1684 * We allow three address separators (':', '-', and '.'),
1685 * as well as no separators
1689 guint32 a, a0, a1, a2, a3;
1690 gboolean found_single_number = FALSE;
1692 if ((cp = strchr(line, '#')))
1695 if ((cp = strtok(line, " \t\n")) == NULL)
1698 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1699 * fill a and found_single_number is TRUE,
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;
1715 if ((cp = strtok(NULL, " \t\n")) == NULL)
1718 if (found_single_number) {
1722 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1725 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1729 } /* parse_ipxnets_line */
1731 static FILE *ipxnet_p = NULL;
1734 set_ipxnetent(char *path)
1739 ipxnet_p = ws_fopen(path, "r");
1755 static ipxnet_t ipxnet;
1756 static int size = 0;
1757 static char *buf = NULL;
1759 if (ipxnet_p == NULL)
1762 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1763 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1770 } /* get_ipxnetent */
1775 get_ipxnetbyname(const gchar *name)
1779 set_ipxnetent(g_ipxnets_path);
1781 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1784 if (ipxnet == NULL) {
1787 set_ipxnetent(g_pipxnets_path);
1789 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1797 } /* get_ipxnetbyname */
1801 get_ipxnetbyaddr(guint32 addr)
1805 set_ipxnetent(g_ipxnets_path);
1807 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1809 if (ipxnet == NULL) {
1812 set_ipxnetent(g_pipxnets_path);
1814 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1822 } /* get_ipxnetbyaddr */
1825 initialize_ipxnets(void)
1827 /* Compute the pathname of the ipxnets file.
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?
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);
1839 /* Set g_pipxnets_path here, but don't actually do anything
1840 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1842 if (g_pipxnets_path == NULL)
1843 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1845 } /* initialize_ipxnets */
1848 ipx_name_lookup_cleanup(void)
1850 if (ipxnet_hash_table) {
1851 g_hash_table_destroy(ipxnet_hash_table);
1852 ipxnet_hash_table = NULL;
1858 static hashipxnet_t *
1859 add_ipxnet_name(guint addr, const gchar *name)
1863 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1865 g_strlcpy(tp->name, name, MAXNAMELEN);
1869 key = (int *)g_new(int, 1);
1871 tp = g_new(hashipxnet_t,1);
1872 g_strlcpy(tp->name, name, MAXNAMELEN);
1873 g_hash_table_insert(ipxnet_hash_table, key, tp);
1877 g_strlcpy(tp->name, name, MAXNAMELEN);
1879 new_resolved_objects = TRUE;
1883 } /* add_ipxnet_name */
1887 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1892 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1896 key = (int *)g_new(int, 1);
1898 tp = g_new(hashipxnet_t, 1);
1899 g_hash_table_insert(ipxnet_hash_table, key, tp);
1901 return wmem_strdup(allocator, tp->name);
1904 /* fill in a new entry */
1908 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1910 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1913 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1916 return wmem_strdup(allocator, tp->name);
1918 } /* ipxnet_name_lookup */
1921 ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
1926 /* XXX Do we need reverse lookup??? */
1929 hashipxnet_t **table = ipxnet_table;
1932 /* to be optimized (hash table from name to addr) */
1933 for (i = 0; i < HASHIPXNETSIZE; i++) {
1936 if (strcmp(tp->name, name) == 0) {
1944 /* not in hash table : performs a file lookup */
1946 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1951 /* add new entry in hash table */
1953 tp = add_ipxnet_name(ipxnet->addr, name);
1958 } /* ipxnet_addr_lookup */
1961 read_hosts_file (const char *hostspath, gboolean store_entries)
1967 guint32 host_addr[4]; /* IPv4 or IPv6 */
1968 struct e_in6_addr ip6_addr;
1969 gboolean is_ipv6, entry_found = FALSE;
1973 * See the hosts(4) or hosts(5) man page for hosts file format
1974 * (not available on all systems).
1976 if ((hf = ws_fopen(hostspath, "r")) == NULL)
1979 while (fgetline(&line, &size, hf) >= 0) {
1980 if ((cp = strchr(line, '#')))
1983 if ((cp = strtok(line, " \t")) == NULL)
1984 continue; /* no tokens in the line */
1986 ret = inet_pton(AF_INET6, cp, &host_addr);
1988 continue; /* error parsing */
1993 /* Not valid IPv6 - valid IPv4? */
1994 if (!str_to_ip(cp, &host_addr))
1999 if ((cp = strtok(NULL, " \t")) == NULL)
2000 continue; /* no host name */
2003 if (store_entries) {
2005 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2006 add_ipv6_name(&ip6_addr, cp);
2008 add_ipv4_name(host_addr[0], cp);
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
2017 while ((cp = strtok(NULL, " \t")) != NULL) {
2019 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2020 add_ipv6_name(&ip6_addr, cp);
2022 add_ipv4_name(host_addr[0], cp);
2030 return entry_found ? TRUE : FALSE;
2031 } /* read_hosts_file */
2034 add_hosts_file (const char *hosts_file)
2036 gboolean found = FALSE;
2042 if (!extra_hosts_files)
2043 extra_hosts_files = g_ptr_array_new();
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)
2051 g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
2052 return read_hosts_file (hosts_file, FALSE);
2058 add_ip_name_from_string (const char *addr, const char *name)
2060 guint32 host_addr[4]; /* IPv4 */
2061 struct e_in6_addr ip6_addr; /* IPv6 */
2064 resolved_ipv4_t *resolved_ipv4_entry;
2065 resolved_ipv6_t *resolved_ipv6_entry;
2067 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2069 /* Error parsing address */
2076 /* Not valid IPv6 - valid IPv4? */
2077 if (!str_to_ip(addr, &host_addr))
2078 return FALSE; /* no */
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);
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);
2095 } /* add_ip_name_from_string */
2098 * Add the resolved addresses that are in use to the list used to create the NRB
2101 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2103 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2104 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
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);
2113 * Add the resolved addresses that are in use to the list used to create the NRB
2117 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2119 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2120 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
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);
2129 get_addrinfo_list(void) {
2131 if (ipv4_hash_table) {
2132 g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2135 if (ipv6_hash_table) {
2136 g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2139 return &addrinfo_lists;
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.
2157 read_subnets_file (const char *subnetspath)
2163 guint32 host_addr; /* IPv4 ONLY */
2166 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2169 while (fgetline(&line, &size, hf) >= 0) {
2170 if ((cp = strchr(line, '#')))
2173 if ((cp = strtok(line, " \t")) == NULL)
2174 continue; /* no tokens in the line */
2177 /* Expected format is <IP4 address>/<subnet length> */
2178 cp2 = strchr(cp, '/');
2183 *cp2 = '\0'; /* Cut token */
2186 /* Check if this is a valid IPv4 address */
2187 if (!str_to_ip(cp, &host_addr)) {
2191 mask_length = atoi(cp2);
2192 if (0 >= mask_length || mask_length > 32) {
2193 continue; /* invalid mask length */
2196 if ((cp = strtok(NULL, " \t")) == NULL)
2197 continue; /* no subnet name */
2199 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2205 } /* read_subnets_file */
2207 static subnet_entry_t
2208 subnet_lookup(const guint32 addr)
2210 subnet_entry_t subnet_entry;
2213 /* Search mask lengths linearly, longest first */
2215 i = SUBNETLENGTHSIZE;
2216 while(have_subnet_entry && i > 0) {
2217 guint32 masked_addr;
2218 subnet_length_entry_t* length_entry;
2220 /* Note that we run from 31 (length 32) to 0 (length 1) */
2222 g_assert(i < SUBNETLENGTHSIZE);
2225 length_entry = &subnet_length_entries[i];
2227 if (NULL != length_entry->subnet_addresses) {
2228 sub_net_hashipv4_t * tp;
2231 masked_addr = addr & length_entry->mask;
2232 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2234 tp = length_entry->subnet_addresses[hash_idx];
2235 while(tp != NULL && tp->addr != masked_addr) {
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;
2248 subnet_entry.mask = 0;
2249 subnet_entry.mask_length = 0;
2250 subnet_entry.name = NULL;
2252 return subnet_entry;
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
2260 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2262 subnet_length_entry_t* entry;
2263 sub_net_hashipv4_t * tp;
2266 g_assert(mask_length > 0 && mask_length <= 32);
2268 entry = &subnet_length_entries[mask_length - 1];
2270 subnet_addr &= entry->mask;
2272 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2274 if (NULL == entry->subnet_addresses) {
2275 entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2278 if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2279 sub_net_hashipv4_t * new_tp;
2282 if (tp->addr == subnet_addr) {
2283 return; /* XXX provide warning that an address was repeated? */
2289 new_tp = g_new(sub_net_hashipv4_t, 1);
2293 tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
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;
2305 subnet_name_lookup_init(void)
2310 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2311 guint32 length = i + 1;
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));
2318 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2319 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2320 report_open_failure(subnetspath, errno, FALSE);
2322 g_free(subnetspath);
2325 * Load the global subnets file, if we have one.
2327 subnetspath = get_datafile_path(ENAME_SUBNETS);
2328 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2329 report_open_failure(subnetspath, errno, FALSE);
2331 g_free(subnetspath);
2335 cleanup_subnet_entry(sub_net_hashipv4_t* entry)
2337 if ((entry != NULL) && (entry->next != NULL)) {
2338 cleanup_subnet_entry(entry->next);
2345 * External Functions
2349 addr_resolve_pref_init(module_t *nameres)
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);
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);
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);
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);
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"
2380 &gbl_resolv_flags.use_external_net_name_resolver);
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);
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.",
2397 &name_resolve_concurrency);
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");
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);
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;
2425 host_name_lookup_process(void) {
2426 async_dns_queue_msg_t *caqm;
2427 struct timeval tv = { 0, 0 };
2430 gboolean nro = new_resolved_objects;
2432 new_resolved_objects = FALSE;
2434 if (!async_dns_initialized)
2435 /* c-ares not initialized. Bail out and cancel timers. */
2438 async_dns_queue_head = g_list_first(async_dns_queue_head);
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++;
2456 nfds = ares_fds(ghba_chan, &rfds, &wfds);
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));
2462 ares_process(ghba_chan, &rfds, &wfds);
2465 /* Any new entries? */
2470 _host_name_lookup_cleanup(void) {
2473 cur = g_list_first(async_dns_queue_head);
2476 cur = g_list_next (cur);
2479 g_list_free(async_dns_queue_head);
2480 async_dns_queue_head = NULL;
2482 if (async_dns_initialized) {
2483 ares_destroy(ghba_chan);
2484 ares_destroy(ghbn_chan);
2486 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2487 ares_library_cleanup();
2489 async_dns_initialized = FALSE;
2492 #elif defined(HAVE_GNU_ADNS)
2494 /* XXX - The ADNS "documentation" isn't very clear:
2495 * - Do we need to keep our query structures around?
2498 host_name_lookup_process(void) {
2499 async_dns_queue_msg_t *almsg;
2501 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2506 gboolean nro = new_resolved_objects;
2508 new_resolved_objects = FALSE;
2509 async_dns_queue_head = g_list_first(async_dns_queue_head);
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++;
2526 cur = async_dns_queue_head;
2529 almsg = (async_dns_queue_msg_t *) cur->data;
2530 if (almsg->submitted) {
2531 ret = adns_check(ads, &almsg->query, &ans, NULL);
2533 if (ans->status == adns_s_ok) {
2534 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2541 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2543 /* XXX, what to do if async_dns_in_flight == 0? */
2544 async_dns_in_flight--;
2548 /* Keep the timeout in place */
2553 _host_name_lookup_cleanup(void) {
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);
2563 if (async_dns_initialized)
2565 async_dns_initialized = FALSE;
2568 #else /* HAVE_GNU_ADNS */
2571 host_name_lookup_process(void) {
2572 gboolean nro = new_resolved_objects;
2574 new_resolved_objects = FALSE;
2580 _host_name_lookup_cleanup(void) {
2583 #endif /* HAVE_C_ARES */
2586 get_hostname(const guint addr)
2590 /* XXX why do we call this if we're not resolving? To create hash entries?
2593 hashipv4_t *tp = host_lookup(addr, &found);
2595 if (!gbl_resolv_flags.network_name)
2598 tp->flags |= RESOLVED_ADDRESS_USED;
2603 /* -------------------------- */
2606 get_hostname6(const struct e_in6_addr *addr)
2610 /* XXX why do we call this if we're not resolving? To create hash entries?
2613 hashipv6_t *tp = host_lookup6(addr, &found);
2615 if (!gbl_resolv_flags.network_name)
2618 tp->flags |= RESOLVED_ADDRESS_USED;
2623 /* -------------------------- */
2625 add_ipv4_name(const guint addr, const gchar *name)
2630 * Don't add zero-length names; apparently, some resolvers will return
2631 * them if they get them from DNS.
2633 if (!name || name[0] == '\0')
2637 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2639 tp = new_ipv4(addr);
2640 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2643 if (g_ascii_strcasecmp(tp->name, name)) {
2644 g_strlcpy(tp->name, name, MAXNAMELEN);
2645 new_resolved_objects = TRUE;
2647 tp->flags |= TRIED_RESOLVE_ADDRESS;
2649 } /* add_ipv4_name */
2651 /* -------------------------- */
2653 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2658 * Don't add zero-length names; apparently, some resolvers will return
2659 * them if they get them from DNS.
2661 if (!name || name[0] == '\0')
2664 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
2666 struct e_in6_addr *addr_key;
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);
2674 if (g_ascii_strcasecmp(tp->name, name)) {
2675 g_strlcpy(tp->name, name, MAXNAMELEN);
2676 new_resolved_objects = TRUE;
2678 tp->flags |= TRIED_RESOLVE_ADDRESS;
2680 } /* add_ipv6_name */
2683 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2685 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2687 add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2691 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2693 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2695 add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2699 add_manually_resolved(void)
2701 if (manually_resolved_ipv4_list) {
2702 g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2705 if (manually_resolved_ipv6_list) {
2706 g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2711 host_name_lookup_init(void)
2716 #ifdef HAVE_GNU_ADNS
2719 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2720 static char rootpath_ot[] = "\\hosts";
2722 #endif /*GNU_ADNS */
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);
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);
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);
2734 * Load the global hosts file, if we have one.
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);
2744 * Load the user's hosts file no matter what, if they have one.
2746 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2747 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2748 report_open_failure(hostspath, errno, FALSE);
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) {
2756 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2757 async_dns_initialized = TRUE;
2759 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2764 #ifdef HAVE_GNU_ADNS
2766 * We're using GNU ADNS, which doesn't check the system hosts file;
2767 * we load that file ourselves.
2771 sysroot = getenv_utf8("WINDIR");
2772 if (sysroot != NULL) {
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.
2779 * XXX - should we base it on the dwPlatformId value from
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)) {
2786 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2787 read_hosts_file(hostspath, TRUE);
2793 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2794 read_hosts_file("/etc/hosts", TRUE);
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) {
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.
2812 async_dns_initialized = TRUE;
2813 async_dns_in_flight = 0;
2815 #endif /* HAVE_GNU_ADNS */
2816 #endif /* HAVE_C_ARES */
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);
2824 subnet_name_lookup_init();
2826 add_manually_resolved();
2830 host_name_lookup_cleanup(void)
2833 _host_name_lookup_cleanup();
2835 if (ipxnet_hash_table) {
2836 g_hash_table_destroy(ipxnet_hash_table);
2837 ipxnet_hash_table = NULL;
2840 if (ipv4_hash_table) {
2841 g_hash_table_destroy(ipv4_hash_table);
2842 ipv4_hash_table = NULL;
2845 if (ipv6_hash_table) {
2846 g_hash_table_destroy(ipv6_hash_table);
2847 ipv6_hash_table = NULL;
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)
2855 cleanup_subnet_entry(subnet_length_entries[i].subnet_addresses[j]);
2858 g_free(subnet_length_entries[i].subnet_addresses);
2859 subnet_length_entries[i].subnet_addresses = NULL;
2863 have_subnet_entry = FALSE;
2864 new_resolved_objects = FALSE;
2868 free_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2870 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2872 g_free(resolved_ipv4_entry);
2876 free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2878 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2880 g_free(resolved_ipv6_entry);
2884 manually_resolve_cleanup(void)
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;
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;
2901 udp_port_to_display(wmem_allocator_t *allocator, guint port)
2904 if (!gbl_resolv_flags.transport_name) {
2905 return wmem_utoa(allocator, port);
2908 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
2910 } /* udp_port_to_display */
2913 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
2916 if (!gbl_resolv_flags.transport_name) {
2917 return wmem_utoa(allocator, port);
2920 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
2922 } /* dccp_port_to_display */
2925 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
2928 if (!gbl_resolv_flags.transport_name) {
2929 return wmem_utoa(allocator, port);
2932 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
2934 } /* tcp_port_to_display */
2937 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
2940 if (!gbl_resolv_flags.transport_name) {
2941 return wmem_utoa(allocator, port);
2944 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
2946 } /* sctp_port_to_display */
2949 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
2951 const gchar *port_str;
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);
2957 port_str = serv_name_lookup(proto, port);
2959 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
2963 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
2965 const gchar *port_str;
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);
2971 port_str = serv_name_lookup(proto, port);
2973 return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
2977 get_ether_name(const guint8 *addr)
2980 gboolean resolve = gbl_resolv_flags.mac_name;
2982 tp = eth_name_lookup(addr, resolve);
2984 return resolve ? tp->resolved_name : tp->hexaddr;
2986 } /* get_ether_name */
2989 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
2991 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
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.
2998 get_ether_name_if_known(const guint8 *addr)
3002 /* Initialize ether structs if we're the first
3003 * ether-related function called */
3004 if (!gbl_resolv_flags.mac_name)
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);
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;
3016 /* Name was created */
3022 get_ether_addr(const gchar *name)
3025 /* force resolution (do not check gbl_resolv_flags) */
3026 return eth_addr_lookup(name);
3028 } /* get_ether_addr */
3031 add_ether_byip(const guint ip, const guint8 *eth)
3036 /* first check that IP address can be resolved */
3037 if (!gbl_resolv_flags.network_name)
3040 tp = host_lookup(ip, &found);
3042 /* ok, we can add this entry in the ethers hashtable */
3043 add_eth_name(eth, tp->name);
3046 } /* add_ether_byip */
3049 ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct)
3051 gchar *buf = (gchar *)wmem_alloc(scope, 12);
3053 *dword_to_hex_punct(buf, ad, punct) = '\0';
3058 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3061 if (!gbl_resolv_flags.network_name) {
3062 return ipxnet_to_str_punct(allocator, addr, '\0');
3065 return ipxnet_name_lookup(allocator, addr);
3067 } /* get_ipxnet_name */
3070 get_ipxnet_addr(const gchar *name, gboolean *known)
3075 /* force resolution (do not check gbl_resolv_flags) */
3076 addr = ipxnet_addr_lookup(name, &success);
3081 } /* get_ipxnet_addr */
3084 get_manuf_name(const guint8 *addr)
3086 hashmanuf_t *manuf_value;
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;
3092 return manuf_value->hexaddr;
3094 } /* get_manuf_name */
3097 uint_get_manuf_name(const guint oid)
3101 addr[0] = (oid >> 16) & 0xFF;
3102 addr[1] = (oid >> 8) & 0xFF;
3103 addr[2] = (oid >> 0) & 0xFF;
3104 return get_manuf_name(addr);
3108 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3110 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3114 get_manuf_name_if_known(const guint8 *addr)
3116 hashmanuf_t *manuf_value;
3120 /* manuf needs only the 3 most significant octets of the ethernet address */
3121 manuf_key = addr[0];
3122 manuf_key = manuf_key<<8;
3124 manuf_key = manuf_key | oct;
3125 manuf_key = manuf_key<<8;
3127 manuf_key = manuf_key | oct;
3129 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3130 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3134 return manuf_value->resolved_name;
3136 } /* get_manuf_name_if_known */
3139 uint_get_manuf_name_if_known(const guint manuf_key)
3141 hashmanuf_t *manuf_value;
3143 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3144 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3148 return manuf_value->resolved_name;
3152 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3154 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3157 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3159 return manuf->resolved_name;
3163 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3165 guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3166 hashmanuf_t *manuf_value;
3169 /* Copy and convert the address to network byte order. */
3170 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
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]);
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]);
3179 wmem_free(NULL, addr);
3181 } /* eui64_to_display */
3184 #define GHI_TIMEOUT (250 * 1000)
3189 #if ( ( ARES_VERSION_MAJOR < 1 ) \
3190 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3199 * XXX - If we wanted to be really fancy we could cache results here and
3200 * look them up in get_host_ipaddr* below.
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;
3208 #endif /* HAVE_C_ARES */
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 */
3215 get_host_ipaddr(const char *host, guint32 *addrp)
3217 struct in_addr ipaddr;
3219 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3222 async_hostent_t ahe;
3223 #elif defined(HAVE_GETADDRINFO)
3224 struct addrinfo hint, *result = NULL;
3225 #elif defined(HAVE_GETHOSTBYNAME)
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.
3234 if (!inet_aton(host, &ipaddr)) {
3236 /* It's not a valid dotted-quad IP address; is it a valid
3240 /* If we're not allowed to do name resolution, don't do name
3243 if (!gbl_resolv_flags.network_name ||
3244 !gbl_resolv_flags.use_external_net_name_resolver) {
3249 if (! (gbl_resolv_flags.concurrent_dns) ||
3250 name_resolve_concurrency < 1 ||
3251 ! async_dns_initialized) {
3254 ahe.addr_size = (int) sizeof (struct in_addr);
3257 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3260 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
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));
3267 ares_process(ghbn_chan, &rfds, &wfds);
3269 ares_cancel(ghbn_chan);
3270 if (ahe.addr_size == ahe.copied) {
3274 #elif defined(HAVE_GETADDRINFO)
3276 * This can be slow, particularly for capture files with lots of
3277 * addresses. Should we just return FALSE instead?
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);
3289 freeaddrinfo(result);
3293 #elif defined(HAVE_GETHOSTBYNAME)
3294 hp = gethostbyname(host);
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);
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.
3312 unsigned int a0, a1, a2, a3;
3313 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3317 *addrp = ipaddr.s_addr;
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.
3327 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3330 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3333 async_hostent_t ahe;
3334 #elif defined(HAVE_GETADDRINFO)
3335 struct addrinfo hint, *result = NULL;
3336 #elif defined(HAVE_GETHOSTBYNAME2)
3338 #endif /* HAVE_C_ARES */
3340 if (str_to_ip6(host, addrp))
3343 /* It's not a valid dotted-quad IP address; is it a valid
3347 /* If we're not allowed to do name resolution, don't do name
3350 if (!gbl_resolv_flags.network_name ||
3351 !gbl_resolv_flags.use_external_net_name_resolver) {
3357 if (! (gbl_resolv_flags.concurrent_dns) ||
3358 name_resolve_concurrency < 1 ||
3359 ! async_dns_initialized) {
3362 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3365 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3368 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
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));
3375 ares_process(ghbn_chan, &rfds, &wfds);
3377 ares_cancel(ghbn_chan);
3378 if (ahe.addr_size == ahe.copied) {
3381 #elif defined(HAVE_GETADDRINFO)
3383 * This can be slow, particularly for capture files with lots of
3384 * addresses. Should we just return FALSE instead?
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);
3396 freeaddrinfo(result);
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);
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)
3417 host_ip_af(const char *host
3418 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETHOSTBYNAME2)
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) {
3432 freeaddrinfo(result);
3434 #elif defined(HAVE_GETHOSTBYNAME2)
3436 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
3442 get_manuf_hashtable(void)
3444 return manuf_hashtable;
3448 get_wka_hashtable(void)
3450 return wka_hashtable;
3454 get_eth_hashtable(void)
3456 return eth_hashtable;
3460 get_serv_port_hashtable(void)
3462 return serv_port_hashtable;
3466 get_ipxnet_hash_table(void)
3468 return ipxnet_hash_table;
3472 get_ipv4_hash_table(void)
3474 return ipv4_hash_table;
3478 get_ipv6_hash_table(void)
3480 return ipv6_hash_table;
3482 /* Initialize all the address resolution subsystems in this file */
3484 addr_resolv_init(void)
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();*/
3493 /* Clean up all the address resolution subsystems in this file */
3495 addr_resolv_cleanup(void)
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();*/
3505 str_to_ip(const char *str, void *dst)
3507 return inet_pton(AF_INET, str, dst) > 0;
3511 str_to_ip6(const char *str, void *dst)
3513 return inet_pton(AF_INET6, str, dst) > 0;
3517 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3522 * indent-tabs-mode: nil
3525 * vi: set shiftwidth=4 tabstop=8 expandtab:
3526 * :indentSize=4:tabSize=8:noTabs=true: