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.
72 #ifdef HAVE_NETINET_IN_H
73 # include <netinet/in.h>
80 #ifdef HAVE_ARPA_INET_H
81 #include <arpa/inet.h>
84 #ifdef HAVE_SYS_SOCKET_H
85 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
88 #ifdef HAVE_WINSOCK2_H
89 #include <winsock2.h> /* needed to define AF_ values on Windows */
92 #ifndef HAVE_INET_ATON
93 # include "wsutil/inet_aton.h"
96 #ifdef NEED_INET_V6DEFS_H
97 # include "wsutil/inet_v6defs.h"
100 #if defined(_WIN32) && defined(INET6)
101 # include <ws2tcpip.h>
105 # if defined(_WIN32) && !defined(INET6)
106 # define socklen_t unsigned int
109 # include <ares_version.h>
111 # ifdef HAVE_GNU_ADNS
114 # if defined(inet_aton) && defined(_WIN32)
117 # endif /* HAVE_GNU_ADNS */
118 #endif /* HAVE_C_ARES */
124 #include "addr_and_mask.h"
125 #include "ipv6-utils.h"
126 #include "addr_resolv.h"
127 #include "wsutil/filesystem.h"
129 #include <wsutil/report_err.h>
130 #include <wsutil/file_util.h>
131 #include <wsutil/pint.h>
133 #include <epan/strutil.h>
134 #include <epan/to_str-int.h>
135 #include <epan/prefs.h>
137 #define ENAME_HOSTS "hosts"
138 #define ENAME_SUBNETS "subnets"
139 #define ENAME_ETHERS "ethers"
140 #define ENAME_IPXNETS "ipxnets"
141 #define ENAME_MANUF "manuf"
142 #define ENAME_SERVICES "services"
144 #define HASHETHSIZE 2048
145 #define HASHHOSTSIZE 2048
146 #define HASHIPXNETSIZE 256
147 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
149 /* hash table used for IPv4 lookup */
151 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
154 typedef struct sub_net_hashipv4 {
156 guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
157 struct sub_net_hashipv4 *next;
159 gchar name[MAXNAMELEN];
160 } sub_net_hashipv4_t;
162 /* Array of entries of subnets of different lengths */
164 gsize mask_length; /*1-32*/
165 guint32 mask; /* e.g. 255.255.255.*/
166 sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
167 } subnet_length_entry_t;
171 typedef struct serv_port {
178 /* hash table used for IPX network lookup */
180 /* XXX - check goodness of hash function */
182 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
184 typedef struct hashipxnet {
186 struct hashipxnet *next;
187 gchar name[MAXNAMELEN];
190 /* hash tables used for ethernet and manufacturer lookup */
191 #define HASHETHER_STATUS_UNRESOLVED 1
192 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
193 #define HASHETHER_STATUS_RESOLVED_NAME 3
195 typedef struct hashether {
196 guint status; /* (See above) */
199 char resolved_name[MAXNAMELEN];
202 typedef struct hashmanuf {
203 guint status; /* (See above) */
206 char resolved_name[MAXNAMELEN];
209 /* internal ethernet type */
210 typedef struct _ether
213 char name[MAXNAMELEN];
216 /* internal ipxnet type */
217 typedef struct _ipxnet
220 char name[MAXNAMELEN];
223 static GHashTable *ipxnet_hash_table = NULL;
224 static GHashTable *ipv4_hash_table = NULL;
225 static GHashTable *ipv6_hash_table = NULL;
227 static GSList *manually_resolved_ipv4_list = NULL;
228 static GSList *manually_resolved_ipv6_list = NULL;
230 typedef struct _resolved_ipv4
233 char name[MAXNAMELEN];
236 typedef struct _resolved_ipv6
238 struct e_in6_addr ip6_addr;
239 char name[MAXNAMELEN];
242 static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
244 static gchar *cb_service;
245 static port_type cb_proto = PT_NONE;
248 static GHashTable *manuf_hashtable = NULL;
249 static GHashTable *wka_hashtable = NULL;
250 static GHashTable *eth_hashtable = NULL;
251 static GHashTable *serv_port_hashtable = NULL;
253 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
254 static gboolean have_subnet_entry = FALSE;
256 static gboolean new_resolved_objects = FALSE;
258 static GPtrArray* extra_hosts_files = NULL;
260 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
261 static void add_serv_port_cb(const guint32 port);
264 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
268 ipv6_oat_hash(gconstpointer key)
271 const unsigned char *p = (const unsigned char *)key;
275 for ( i = 0; i < len; i++ ) {
289 ipv6_equal(gconstpointer v1, gconstpointer v2)
292 if( memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0 ) {
300 * Flag controlling what names to resolve.
302 e_addr_resolve gbl_resolv_flags = {TRUE, FALSE, FALSE, TRUE, TRUE, FALSE};
303 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
304 static guint name_resolve_concurrency = 500;
308 * Global variables (can be changed in GUI sections)
309 * XXX - they could be changed in GUI code, but there's currently no
310 * GUI code to change them.
313 gchar *g_ethers_path = NULL; /* global ethers file */
314 gchar *g_pethers_path = NULL; /* personal ethers file */
315 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
316 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
317 gchar *g_services_path = NULL; /* global services file */
318 gchar *g_pservices_path = NULL; /* personal services file */
319 /* first resolving call */
324 * Submitted queries trigger a callback (c_ares_ghba_cb()).
325 * Queries are added to c_ares_queue_head. During processing, queries are
326 * popped off the front of c_ares_queue_head and submitted using
327 * ares_gethostbyaddr().
328 * The callback processes the response, then frees the request.
331 typedef struct _async_dns_queue_msg
335 struct e_in6_addr ip6;
338 } async_dns_queue_msg_t;
340 typedef struct _async_hostent {
346 #if ( ( ARES_VERSION_MAJOR < 1 ) \
347 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
348 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
350 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
353 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
354 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
361 * Submitted queries have to be checked individually using adns_check().
362 * Queries are added to adns_queue_head. During processing, the list is
363 * iterated twice: once to request queries up to the concurrency limit,
364 * and once to check the status of each query.
369 typedef struct _async_dns_queue_msg
375 } async_dns_queue_msg_t;
377 #endif /* HAVE_GNU_ADNS */
378 #endif /* HAVE_C_ARES */
380 static gboolean async_dns_initialized = FALSE;
381 static guint async_dns_in_flight = 0;
382 static GList *async_dns_queue_head = NULL;
384 /* push a dns request */
386 add_async_dns_ipv4(int type, guint32 addr)
388 async_dns_queue_msg_t *msg;
390 msg = g_new(async_dns_queue_msg_t,1);
393 msg->addr.ip4 = addr;
396 msg->ip4_addr = addr;
397 msg->submitted = FALSE;
399 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
407 const gchar* name; /* Shallow copy */
411 * Miscellaneous functions
415 fgetline(char **buf, int *size, FILE *fp)
420 if (fp == NULL || buf == NULL)
427 *buf = (char *)g_malloc(*size);
437 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
438 if (len+1 >= *size) {
439 *buf = (char *)g_realloc(*buf, *size += BUFSIZ);
444 if (len == 0 && c == EOF)
455 * Local function definitions
457 static subnet_entry_t subnet_lookup(const guint32 addr);
458 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
462 add_service_name(port_type proto, const guint port, const char *service_name)
464 serv_port_t *serv_port_table;
467 key = (int *)g_new(int, 1);
470 serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
471 if (serv_port_table == NULL) {
472 serv_port_table = g_new0(serv_port_t,1);
473 g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
481 g_free(serv_port_table->tcp_name);
482 serv_port_table->tcp_name = g_strdup(service_name);
485 g_free(serv_port_table->udp_name);
486 serv_port_table->udp_name = g_strdup(service_name);
489 g_free(serv_port_table->sctp_name);
490 serv_port_table->sctp_name = g_strdup(service_name);
493 g_free(serv_port_table->dccp_name);
494 serv_port_table->dccp_name = g_strdup(service_name);
498 /* Should not happen */
501 new_resolved_objects = TRUE;
506 parse_service_line (char *line)
509 * See the services(4) or services(5) man page for services file format
510 * (not available on all systems).
518 range_t *port_rng = NULL;
519 guint32 max_port = MAX_UDP_PORT;
521 if ((cp = strchr(line, '#')))
524 if ((cp = strtok(line, " \t")) == NULL)
529 if ((cp = strtok(NULL, " \t")) == NULL)
534 if (strtok(cp, "/") == NULL)
537 if ((cp = strtok(NULL, "/")) == NULL)
540 /* seems we got all interesting things from the file */
541 if(strcmp(cp, "tcp") == 0) {
542 max_port = MAX_TCP_PORT;
545 else if(strcmp(cp, "udp") == 0) {
546 max_port = MAX_UDP_PORT;
549 else if(strcmp(cp, "sctp") == 0) {
550 max_port = MAX_SCTP_PORT;
553 else if(strcmp(cp, "dccp") == 0) {
554 max_port = MAX_DCCP_PORT;
560 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
561 /* some assertion here? */
565 cb_service = service;
567 range_foreach(port_rng, add_serv_port_cb);
570 } /* parse_service_line */
574 add_serv_port_cb(const guint32 port)
577 add_service_name(cb_proto, port, cb_service);
583 parse_services_file(const char * path)
587 static char *buf = NULL;
589 /* services hash table initialization */
590 serv_p = ws_fopen(path, "r");
595 while (fgetline(&buf, &size, serv_p) >= 0) {
596 parse_service_line (buf);
603 * unsigned integer to ascii
606 wmem_utoa(wmem_allocator_t *allocator, guint port)
608 gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
610 /* XXX, guint32_to_str() ? */
611 guint32_to_str_buf(port, bp, MAXNAMELEN);
617 *serv_name_lookup(const guint port, const port_type proto)
619 serv_port_t *serv_port_table;
622 serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
625 /* Set which table we should look up port in */
628 if(serv_port_table->udp_name){
629 return serv_port_table->udp_name;
633 if(serv_port_table->tcp_name){
634 return serv_port_table->tcp_name;
638 if(serv_port_table->sctp_name){
639 return serv_port_table->sctp_name;
643 if(serv_port_table->dccp_name){
644 return serv_port_table->dccp_name;
648 /* not yet implemented */
654 /* getservbyport() was used here but it was to expensive, if the functionality is desired
655 * it would be better to pre parse etc/services or C:\Windows\System32\drivers\etc at
658 name = (gchar*)g_malloc(16);
659 guint32_to_str_buf(port, name, 16);
661 if(serv_port_table == NULL){
664 key = (int *)g_new(int, 1);
666 serv_port_table = g_new0(serv_port_t,1);
667 g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
671 serv_port_table->udp_name = name;
674 serv_port_table->tcp_name = name;
677 serv_port_table->sctp_name = name;
680 serv_port_table->dccp_name = name;
688 } /* serv_name_lookup */
691 destroy_serv_port(gpointer data)
693 serv_port_t *table = (serv_port_t*)data;
694 g_free(table->udp_name);
695 g_free(table->tcp_name);
696 g_free(table->sctp_name);
697 g_free(table->dccp_name);
702 initialize_services(void)
707 static char rootpath_nt[] = "\\system32\\drivers\\etc\\services";
710 /* the hash table won't ignore duplicates, so use the personal path first */
711 g_assert(serv_port_hashtable == NULL);
712 serv_port_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, destroy_serv_port);
714 /* Read the system services file first */
717 sysroot = getenv_utf8("WINDIR");
718 if (sysroot != NULL) {
720 * The file should be under WINDIR.
721 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
722 * %WINDIR%\system32\drivers\etc\services.
724 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
725 parse_services_file(hostspath);
729 parse_services_file("/etc/services");
733 /* set personal services path */
734 if (g_pservices_path == NULL)
735 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
737 parse_services_file(g_pservices_path);
739 /* Compute the pathname of the services file. */
740 if (g_services_path == NULL) {
741 g_services_path = get_datafile_path(ENAME_SERVICES);
744 parse_services_file(g_services_path);
746 } /* initialize_services */
749 service_name_lookup_cleanup(void)
751 if(serv_port_hashtable){
752 g_hash_table_destroy(serv_port_hashtable);
753 serv_port_hashtable = NULL;
757 /* Fill in an IP4 structure with info from subnets file or just with the
758 * string form of the address.
761 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
763 subnet_entry_t subnet_entry;
765 if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY)
766 return; /* already done */
768 tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY; /* Overwrite if we get async DNS reply */
770 /* Do we have a subnet for this address? */
771 subnet_entry = subnet_lookup(addr);
772 if(0 != subnet_entry.mask) {
773 /* Print name, then '.' then IP address after subnet mask */
775 gchar buffer[MAX_IP_STR_LEN];
779 host_addr = addr & (~(guint32)subnet_entry.mask);
780 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
783 /* Skip to first octet that is not totally masked
784 * If length of mask is 32, we chomp the whole address.
785 * If the address string starts '.' (should not happen?),
788 i = subnet_entry.mask_length / 8;
789 while(*(paddr) != '\0' && i > 0) {
790 if(*(++paddr) == '.') {
795 /* There are more efficient ways to do this, but this is safe if we
796 * trust g_snprintf and MAXNAMELEN
798 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
800 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
810 #if ( ( ARES_VERSION_MAJOR < 1 ) \
811 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
819 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
823 /* XXX, what to do if async_dns_in_flight == 0? */
824 async_dns_in_flight--;
826 if (status == ARES_SUCCESS) {
827 for (p = he->h_addr_list; *p != NULL; p++) {
828 switch(caqm->family) {
830 add_ipv4_name(caqm->addr.ip4, he->h_name);
833 add_ipv6_name(&caqm->addr.ip6, he->h_name);
836 /* Throw an exception? */
843 #endif /* HAVE_C_ARES */
845 /* --------------- */
847 new_ipv4(const guint addr)
849 hashipv4_t *tp = g_new(hashipv4_t, 1);
852 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
857 host_lookup(const guint addr, gboolean *found)
859 hashipv4_t * volatile tp;
863 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
866 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
868 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY){
871 if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
878 if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
879 tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
882 if (gbl_resolv_flags.concurrent_dns &&
883 name_resolve_concurrency > 0 &&
884 async_dns_initialized) {
885 add_async_dns_ipv4(AF_INET, addr);
886 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
887 * going to risk changing the semantics.
889 fill_dummy_ip4(addr, tp);
892 #endif /* ASYNC_DNS */
894 /* unknown host or DNS timeout */
900 fill_dummy_ip4(addr, tp);
905 /* --------------- */
907 new_ipv6(const struct e_in6_addr *addr)
909 hashipv6_t *tp = g_new(hashipv6_t,1);
912 ip6_to_str_buf(addr, tp->ip6);
916 /* ------------------------------------ */
918 host_lookup6(const struct e_in6_addr *addr, gboolean *found)
920 hashipv6_t * volatile tp;
923 async_dns_queue_msg_t *caqm;
924 #endif /* HAVE_C_ARES */
929 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
931 struct e_in6_addr *addr_key;
933 addr_key = g_new(struct e_in6_addr,1);
935 memcpy(addr_key, addr, 16);
936 g_hash_table_insert(ipv6_hash_table, addr_key, tp);
938 if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY){
941 if ((tp->flags & DUMMY_ADDRESS_ENTRY) == DUMMY_ADDRESS_ENTRY){
948 if (gbl_resolv_flags.network_name &&
949 gbl_resolv_flags.use_external_net_name_resolver) {
950 tp->flags = tp->flags|TRIED_RESOLVE_ADDRESS;
954 if ((gbl_resolv_flags.concurrent_dns) &&
955 name_resolve_concurrency > 0 &&
956 async_dns_initialized) {
957 caqm = g_new(async_dns_queue_msg_t,1);
958 caqm->family = AF_INET6;
959 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
960 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
962 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
963 * going to risk changing the semantics.
965 if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0){
966 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
967 ip6_to_str_buf(addr, tp->name);
968 tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
972 #endif /* HAVE_C_ARES */
977 /* unknown host or DNS timeout */
978 if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
979 tp->flags = tp->flags | DUMMY_ADDRESS_ENTRY;
980 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
988 solve_address_to_name(const address *addr)
990 switch (addr->type) {
993 return get_ether_name((const guint8 *)addr->data);
997 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
998 return get_hostname(ip4_addr);
1002 struct e_in6_addr ip6_addr;
1003 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
1004 return get_hostname6(&ip6_addr);
1008 return (const gchar *)addr->data;
1016 * Ethernet / manufacturer resolution
1018 * The following functions implement ethernet address resolution and
1019 * ethers files parsing (see ethers(4)).
1021 * The manuf file has the same format as ethers(4) except that names are
1022 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1023 * only 3 bytes (instead of 6).
1027 * I decide to not use the existing functions (see ethers(3) on some
1028 * operating systems) for the following reasons:
1029 * - performance gains (use of hash tables and some other enhancements),
1030 * - use of two ethers files (system-wide and per user),
1031 * - avoid the use of NIS maps,
1032 * - lack of these functions on some systems.
1034 * So the following functions do _not_ behave as the standard ones.
1041 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1042 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1046 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1047 const gboolean manuf_file)
1054 for (i = 0; i < 6; i++) {
1055 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1056 if (!g_ascii_isxdigit(*cp))
1058 num = strtoul(cp, &p, 16);
1060 return FALSE; /* failed */
1062 return FALSE; /* not a valid octet */
1063 eth->addr[i] = (guint8) num;
1064 cp = p; /* skip past the number */
1066 /* OK, what character terminated the octet? */
1068 /* "/" - this has a mask. */
1070 /* Entries with masks are allowed only in the "manuf" files. */
1073 cp++; /* skip past the '/' to get to the mask */
1074 if (!g_ascii_isdigit(*cp))
1075 return FALSE; /* no sign allowed */
1076 num = strtoul(cp, &p, 10);
1078 return FALSE; /* failed */
1079 cp = p; /* skip past the number */
1080 if (*cp != '\0' && !g_ascii_isspace(*cp))
1081 return FALSE; /* bogus terminator */
1082 if (num == 0 || num >= 48)
1083 return FALSE; /* bogus mask */
1084 /* Mask out the bits not covered by the mask */
1086 for (i = 0; num >= 8; i++, num -= 8)
1087 ; /* skip octets entirely covered by the mask */
1088 /* Mask out the first masked octet */
1089 eth->addr[i] &= (0xFF << (8 - num));
1091 /* Mask out completely-masked-out octets */
1097 /* We're at the end of the address, and there's no mask. */
1099 /* We got 3 bytes, so this is a manufacturer ID. */
1101 /* Manufacturer IDs are only allowed in the "manuf"
1105 /* Indicate that this is a manufacturer ID (0 is not allowed
1112 /* We got 6 bytes, so this is a MAC address.
1113 If we're reading one of the "manuf" files, indicate that
1114 this is a MAC address (48 is not allowed as a mask). */
1120 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1125 /* We don't know the separator used in this number; it can either
1126 be ':', '-', or '.'. */
1127 if (*cp != ':' && *cp != '-' && *cp != '.')
1129 sep = *cp; /* subsequent separators must be the same */
1131 /* It has to be the same as the first separator */
1143 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1144 const gboolean manuf_file)
1147 * See the ethers(4) or ethers(5) man page for ethers file format
1148 * (not available on all systems).
1149 * We allow both ethernet address separators (':' and '-'),
1150 * as well as Wireshark's '.' separator.
1155 if ((cp = strchr(line, '#')))
1158 if ((cp = strtok(line, " \t")) == NULL)
1161 if (!parse_ether_address(cp, eth, mask, manuf_file))
1164 if ((cp = strtok(NULL, " \t")) == NULL)
1167 g_strlcpy(eth->name, cp, MAXNAMELEN);
1171 } /* parse_ether_line */
1173 static FILE *eth_p = NULL;
1176 set_ethent(char *path)
1181 eth_p = ws_fopen(path, "r");
1194 get_ethent(unsigned int *mask, const gboolean manuf_file)
1198 static int size = 0;
1199 static char *buf = NULL;
1204 while (fgetline(&buf, &size, eth_p) >= 0) {
1205 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1216 get_ethbyname(const gchar *name)
1220 set_ethent(g_pethers_path);
1222 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1228 set_ethent(g_ethers_path);
1230 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1238 } /* get_ethbyname */
1242 get_ethbyaddr(const guint8 *addr)
1247 set_ethent(g_pethers_path);
1249 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1255 set_ethent(g_ethers_path);
1257 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1265 } /* get_ethbyaddr */
1267 static hashmanuf_t *manuf_hash_new_entry(const guint8 *addr, char* name)
1270 hashmanuf_t *manuf_value;
1273 /* manuf needs only the 3 most significant octets of the ethernet address */
1274 manuf_key = (int *)g_new(int, 1);
1275 *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1276 manuf_value = g_new(hashmanuf_t, 1);
1278 memcpy(manuf_value->addr, addr, 3);
1279 manuf_value->status = (name != NULL) ? HASHETHER_STATUS_RESOLVED_NAME : HASHETHER_STATUS_UNRESOLVED;
1281 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1282 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1285 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1286 manuf_value->resolved_name[0] = '\0';
1288 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1289 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->hexaddr), ':');
1292 g_hash_table_insert(manuf_hashtable, manuf_key, manuf_value);
1297 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1302 * XXX - can we use Standard Annotation Language annotations to
1303 * note that mask, as returned by parse_ethe)r_address() (and thus
1304 * by the routines that call it, and thus passed to us) cannot be > 48,
1305 * or is SAL too weak to express that?
1308 /* This is a well-known MAC address; just add this to the Ethernet
1310 add_eth_name(addr, name);
1315 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1316 manuf_hash_new_entry(addr, name);
1320 /* This is a range of well-known addresses; add it to the appropriate
1321 well-known-address table, creating that table if necessary. */
1323 wka_key = (guint8 *)g_malloc(6);
1324 memcpy(wka_key, addr, 6);
1326 g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
1328 } /* add_manuf_name */
1330 static hashmanuf_t *
1331 manuf_name_lookup(const guint8 *addr)
1333 gint32 manuf_key = 0;
1335 hashmanuf_t *manuf_value;
1337 /* manuf needs only the 3 most significant octets of the ethernet address */
1338 manuf_key = addr[0];
1339 manuf_key = manuf_key<<8;
1341 manuf_key = manuf_key | oct;
1342 manuf_key = manuf_key<<8;
1344 manuf_key = manuf_key | oct;
1347 /* first try to find a "perfect match" */
1348 manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1349 if (manuf_value != NULL) {
1353 /* Mask out the broadcast/multicast flag but not the locally
1354 * administered flag as localy administered means: not assigend
1355 * by the IEEE but the local administrator instead.
1356 * 0x01 multicast / broadcast bit
1357 * 0x02 locally administered bit */
1358 if((manuf_key & 0x00010000) != 0){
1359 manuf_key &= 0x00FEFFFF;
1360 manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1361 if (manuf_value != NULL) {
1366 /* Add the address as a hex string */
1367 return manuf_hash_new_entry(addr, NULL);
1369 } /* manuf_name_lookup */
1372 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1374 guint8 masked_addr[6];
1379 if(wka_hashtable == NULL){
1382 /* Get the part of the address covered by the mask. */
1383 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1384 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1385 /* Mask out the first masked octet */
1386 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1388 /* Zero out completely-masked-out octets */
1392 name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
1396 } /* wka_name_lookup */
1399 guint get_hash_ether_status(hashether_t* ether)
1401 return ether->status;
1404 char* get_hash_ether_hexaddr(hashether_t* ether)
1406 return ether->hexaddr;
1409 char* get_hash_ether_resolved_name(hashether_t* ether)
1411 return ether->resolved_name;
1415 eth_addr_hash(gconstpointer key)
1417 return wmem_strong_hash((const guint8 *)key, 6);
1421 eth_addr_cmp(gconstpointer a, gconstpointer b)
1423 return (memcmp(a, b, 6) == 0);
1427 initialize_ethers(void)
1433 /* hash table initialization */
1434 wka_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, g_free, g_free);
1435 manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
1436 eth_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, NULL, g_free);
1438 /* Compute the pathname of the ethers file. */
1439 if (g_ethers_path == NULL) {
1440 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1441 get_systemfile_dir(), ENAME_ETHERS);
1444 /* Set g_pethers_path here, but don't actually do anything
1445 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1447 if (g_pethers_path == NULL)
1448 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1450 /* Compute the pathname of the manuf file */
1451 manuf_path = get_datafile_path(ENAME_MANUF);
1453 /* Read it and initialize the hash table */
1454 set_ethent(manuf_path);
1456 while ((eth = get_ethent(&mask, TRUE))) {
1457 add_manuf_name(eth->addr, mask, eth->name);
1464 } /* initialize_ethers */
1466 /* this is only needed when shuting down application (if at all) */
1468 eth_name_lookup_cleanup(void)
1471 if(manuf_hashtable) {
1472 g_hash_table_destroy(manuf_hashtable);
1473 manuf_hashtable = NULL;
1476 g_hash_table_destroy(wka_hashtable);
1477 wka_hashtable = NULL;
1481 g_hash_table_destroy(eth_hashtable);
1482 eth_hashtable = NULL;
1487 /* Resolve ethernet address */
1488 static hashether_t *
1489 eth_addr_resolve(hashether_t *tp) {
1491 hashmanuf_t *manuf_value;
1492 const guint8 *addr = tp->addr;
1494 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1495 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1496 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1503 /* Unknown name. Try looking for it in the well-known-address
1504 tables for well-known address ranges smaller than 2^24. */
1507 /* Only the topmost 5 bytes participate fully */
1508 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1509 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1510 name, addr[5] & (0xFF >> mask));
1511 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1521 /* Only the topmost 4 bytes participate fully */
1522 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1523 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1524 name, addr[4] & (0xFF >> mask), addr[5]);
1525 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1535 /* Only the topmost 3 bytes participate fully */
1536 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1537 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1538 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1539 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1547 /* Now try looking in the manufacturer table. */
1548 manuf_value = manuf_name_lookup(addr);
1549 if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1550 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1551 manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1552 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1556 /* Now try looking for it in the well-known-address
1557 tables for well-known address ranges larger than 2^24. */
1560 /* Only the topmost 2 bytes participate fully */
1561 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1562 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1563 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1565 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1575 /* Only the topmost byte participates fully */
1576 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1577 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1578 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1580 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1588 for (mask = 7; mask > 0; mask--) {
1589 /* Not even the topmost byte participates fully */
1590 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1591 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1592 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1593 addr[3], addr[4], addr[5]);
1594 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1599 /* No match whatsoever. */
1600 SET_ADDRESS(ðer_addr, AT_ETHER, 6, addr);
1601 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1602 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1605 g_assert_not_reached();
1606 } /* eth_addr_resolve */
1608 static hashether_t *
1609 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1614 tp = g_new(hashether_t, 1);
1615 memcpy(tp->addr, addr, sizeof(tp->addr));
1616 tp->status = HASHETHER_STATUS_UNRESOLVED;
1617 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1618 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1620 tp->resolved_name[0] = '\0';
1623 eth_addr_resolve(tp);
1625 g_hash_table_insert(eth_hashtable, tp->addr, tp);
1628 } /* eth_hash_new_entry */
1630 static hashether_t *
1631 add_eth_name(const guint8 *addr, const gchar *name)
1635 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1638 tp = eth_hash_new_entry(addr, FALSE);
1641 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1642 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1643 new_resolved_objects = TRUE;
1646 } /* add_eth_name */
1648 static hashether_t *
1649 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1653 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
1655 tp = eth_hash_new_entry(addr, resolve);
1657 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)){
1658 eth_addr_resolve(tp); /* Found but needs to be resolved */
1664 } /* eth_name_lookup */
1667 eth_addr_lookup(const gchar *name _U_)
1670 /* XXX Do we need reverse lookup??? */
1673 hashether_t **table = eth_table;
1676 /* to be optimized (hash table from name to addr) */
1677 for (i = 0; i < HASHETHSIZE; i++) {
1680 if (strcmp(tp->resolved_name, name) == 0)
1686 /* not in hash table : performs a file lookup */
1688 if ((eth = get_ethbyname(name)) == NULL)
1691 /* add new entry in hash table */
1693 tp = add_eth_name(eth->addr, name);
1699 } /* eth_addr_lookup */
1704 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1707 * We allow three address separators (':', '-', and '.'),
1708 * as well as no separators
1712 guint32 a, a0, a1, a2, a3;
1713 gboolean found_single_number = FALSE;
1715 if ((cp = strchr(line, '#')))
1718 if ((cp = strtok(line, " \t\n")) == NULL)
1721 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1722 * fill a and found_single_number is TRUE,
1725 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1726 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1727 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1728 if (sscanf(cp, "%x", &a) == 1) {
1729 found_single_number = TRUE;
1738 if ((cp = strtok(NULL, " \t\n")) == NULL)
1741 if (found_single_number) {
1745 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1748 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1752 } /* parse_ipxnets_line */
1754 static FILE *ipxnet_p = NULL;
1757 set_ipxnetent(char *path)
1762 ipxnet_p = ws_fopen(path, "r");
1778 static ipxnet_t ipxnet;
1779 static int size = 0;
1780 static char *buf = NULL;
1782 if (ipxnet_p == NULL)
1785 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1786 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1793 } /* get_ipxnetent */
1798 get_ipxnetbyname(const gchar *name)
1802 set_ipxnetent(g_ipxnets_path);
1804 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1807 if (ipxnet == NULL) {
1810 set_ipxnetent(g_pipxnets_path);
1812 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1820 } /* get_ipxnetbyname */
1824 get_ipxnetbyaddr(guint32 addr)
1828 set_ipxnetent(g_ipxnets_path);
1830 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1832 if (ipxnet == NULL) {
1835 set_ipxnetent(g_pipxnets_path);
1837 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1845 } /* get_ipxnetbyaddr */
1848 initialize_ipxnets(void)
1850 /* Compute the pathname of the ipxnets file.
1852 * XXX - is there a notion of an "ipxnets file" in any flavor of
1853 * UNIX, or with any add-on Netware package for UNIX? If not,
1854 * should the UNIX version of the ipxnets file be in the datafile
1855 * directory as well?
1857 if (g_ipxnets_path == NULL) {
1858 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1859 get_systemfile_dir(), ENAME_IPXNETS);
1862 /* Set g_pipxnets_path here, but don't actually do anything
1863 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1865 if (g_pipxnets_path == NULL)
1866 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1868 } /* initialize_ipxnets */
1871 ipx_name_lookup_cleanup(void)
1873 if(ipxnet_hash_table){
1874 g_hash_table_destroy(ipxnet_hash_table);
1875 ipxnet_hash_table = NULL;
1881 static hashipxnet_t *
1882 add_ipxnet_name(guint addr, const gchar *name)
1886 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1888 g_strlcpy(tp->name, name, MAXNAMELEN);
1892 key = (int *)g_new(int, 1);
1894 tp = g_new(hashipxnet_t,1);
1895 g_strlcpy(tp->name, name, MAXNAMELEN);
1896 g_hash_table_insert(ipxnet_hash_table, key, tp);
1900 g_strlcpy(tp->name, name, MAXNAMELEN);
1902 new_resolved_objects = TRUE;
1906 } /* add_ipxnet_name */
1910 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1915 tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
1919 key = (int *)g_new(int, 1);
1921 tp = g_new(hashipxnet_t, 1);
1922 g_hash_table_insert(ipxnet_hash_table, key, tp);
1924 return wmem_strdup(allocator, tp->name);
1927 /* fill in a new entry */
1931 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1933 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1936 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1939 return wmem_strdup(allocator, tp->name);
1941 } /* ipxnet_name_lookup */
1944 ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
1949 /* XXX Do we need reverse lookup??? */
1952 hashipxnet_t **table = ipxnet_table;
1955 /* to be optimized (hash table from name to addr) */
1956 for (i = 0; i < HASHIPXNETSIZE; i++) {
1959 if (strcmp(tp->name, name) == 0) {
1967 /* not in hash table : performs a file lookup */
1969 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
1974 /* add new entry in hash table */
1976 tp = add_ipxnet_name(ipxnet->addr, name);
1981 } /* ipxnet_addr_lookup */
1984 read_hosts_file (const char *hostspath, gboolean store_entries)
1990 guint32 host_addr[4]; /* IPv4 or IPv6 */
1991 struct e_in6_addr ip6_addr;
1992 gboolean is_ipv6, entry_found = FALSE;
1996 * See the hosts(4) or hosts(5) man page for hosts file format
1997 * (not available on all systems).
1999 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2002 while (fgetline(&line, &size, hf) >= 0) {
2003 if ((cp = strchr(line, '#')))
2006 if ((cp = strtok(line, " \t")) == NULL)
2007 continue; /* no tokens in the line */
2009 ret = inet_pton(AF_INET6, cp, &host_addr);
2011 continue; /* error parsing */
2016 /* Not valid IPv6 - valid IPv4? */
2017 if (!str_to_ip(cp, &host_addr))
2022 if ((cp = strtok(NULL, " \t")) == NULL)
2023 continue; /* no host name */
2026 if (store_entries) {
2028 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2029 add_ipv6_name(&ip6_addr, cp);
2031 add_ipv4_name(host_addr[0], cp);
2034 * Add the aliases, too, if there are any.
2035 * XXX - host_lookup() only returns the first entry.
2037 while ((cp = strtok(NULL, " \t")) != NULL) {
2039 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2040 add_ipv6_name(&ip6_addr, cp);
2042 add_ipv4_name(host_addr[0], cp);
2049 return entry_found ? TRUE : FALSE;
2050 } /* read_hosts_file */
2053 add_hosts_file (const char *hosts_file)
2055 gboolean found = FALSE;
2061 if (!extra_hosts_files)
2062 extra_hosts_files = g_ptr_array_new();
2064 for (i = 0; i < extra_hosts_files->len; i++) {
2065 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2070 g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
2071 return read_hosts_file (hosts_file, FALSE);
2077 add_ip_name_from_string (const char *addr, const char *name)
2079 guint32 host_addr[4]; /* IPv4 */
2080 struct e_in6_addr ip6_addr; /* IPv6 */
2083 resolved_ipv4_t *resolved_ipv4_entry;
2084 resolved_ipv6_t *resolved_ipv6_entry;
2086 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2088 /* Error parsing address */
2095 /* Not valid IPv6 - valid IPv4? */
2096 if (!str_to_ip(addr, &host_addr))
2097 return FALSE; /* no */
2102 resolved_ipv6_entry = g_new(resolved_ipv6_t, 1);
2103 memcpy(&(resolved_ipv6_entry->ip6_addr), &ip6_addr, 16);
2104 g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2105 manually_resolved_ipv6_list = g_slist_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2107 resolved_ipv4_entry = g_new(resolved_ipv4_t, 1);
2108 resolved_ipv4_entry->host_addr = host_addr[0];
2109 g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2110 manually_resolved_ipv4_list = g_slist_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2114 } /* add_ip_name_from_string */
2117 * Add the resolved addresses that are in use to the list used to create the NRB
2120 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2122 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2123 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2125 if((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
2126 lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
2132 * Add the resolved addresses that are in use to the list used to create the NRB
2136 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2138 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2139 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2141 if((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED){
2142 lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2148 get_addrinfo_list(void) {
2150 if(ipv4_hash_table){
2151 g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2154 if(ipv6_hash_table){
2155 g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2158 return &addrinfo_lists;
2161 /* Read in a list of subnet definition - name pairs.
2162 * <line> = <comment> | <entry> | <whitespace>
2163 * <comment> = <whitespace>#<any>
2164 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2165 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2166 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2167 * <subnet_mask_length> is a decimal 1-31
2168 * <subnet_name> is a string containing no whitespace.
2169 * <whitespace> = (space | tab)+
2170 * Any malformed entries are ignored.
2171 * Any trailing data after the subnet_name is ignored.
2176 read_subnets_file (const char *subnetspath)
2182 guint32 host_addr; /* IPv4 ONLY */
2185 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2188 while (fgetline(&line, &size, hf) >= 0) {
2189 if ((cp = strchr(line, '#')))
2192 if ((cp = strtok(line, " \t")) == NULL)
2193 continue; /* no tokens in the line */
2196 /* Expected format is <IP4 address>/<subnet length> */
2197 cp2 = strchr(cp, '/');
2202 *cp2 = '\0'; /* Cut token */
2205 /* Check if this is a valid IPv4 address */
2206 if (!str_to_ip(cp, &host_addr)) {
2210 mask_length = atoi(cp2);
2211 if(0 >= mask_length || mask_length > 31) {
2212 continue; /* invalid mask length */
2215 if ((cp = strtok(NULL, " \t")) == NULL)
2216 continue; /* no subnet name */
2218 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2224 } /* read_subnets_file */
2226 static subnet_entry_t
2227 subnet_lookup(const guint32 addr)
2229 subnet_entry_t subnet_entry;
2232 /* Search mask lengths linearly, longest first */
2234 i = SUBNETLENGTHSIZE;
2235 while(have_subnet_entry && i > 0) {
2236 guint32 masked_addr;
2237 subnet_length_entry_t* length_entry;
2239 /* Note that we run from 31 (length 32) to 0 (length 1) */
2241 g_assert(i < SUBNETLENGTHSIZE);
2244 length_entry = &subnet_length_entries[i];
2246 if(NULL != length_entry->subnet_addresses) {
2247 sub_net_hashipv4_t * tp;
2250 masked_addr = addr & length_entry->mask;
2251 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2253 tp = length_entry->subnet_addresses[hash_idx];
2254 while(tp != NULL && tp->addr != masked_addr) {
2259 subnet_entry.mask = length_entry->mask;
2260 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2261 subnet_entry.name = tp->name;
2262 return subnet_entry;
2267 subnet_entry.mask = 0;
2268 subnet_entry.mask_length = 0;
2269 subnet_entry.name = NULL;
2271 return subnet_entry;
2274 /* Add a subnet-definition - name pair to the set.
2275 * The definition is taken by masking the address passed in with the mask of the
2279 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2281 subnet_length_entry_t* entry;
2282 sub_net_hashipv4_t * tp;
2285 g_assert(mask_length > 0 && mask_length <= 32);
2287 entry = &subnet_length_entries[mask_length - 1];
2289 subnet_addr &= entry->mask;
2291 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2293 if(NULL == entry->subnet_addresses) {
2294 entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2297 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2298 if(tp->addr == subnet_addr) {
2299 return; /* XXX provide warning that an address was repeated? */
2301 sub_net_hashipv4_t * new_tp = g_new(sub_net_hashipv4_t, 1);
2306 tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
2310 tp->addr = subnet_addr;
2311 /* Clear DUMMY_ADDRESS_ENTRY */
2312 tp->flags = tp->flags & 0xfe; /*Never used again...*/
2313 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2314 have_subnet_entry = TRUE;
2318 subnet_name_lookup_init(void)
2323 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2324 guint32 length = i + 1;
2326 subnet_length_entries[i].subnet_addresses = NULL;
2327 subnet_length_entries[i].mask_length = length;
2328 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2331 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2332 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2333 report_open_failure(subnetspath, errno, FALSE);
2335 g_free(subnetspath);
2338 * Load the global subnets file, if we have one.
2340 subnetspath = get_datafile_path(ENAME_SUBNETS);
2341 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2342 report_open_failure(subnetspath, errno, FALSE);
2344 g_free(subnetspath);
2348 cleanup_subnet_entry(sub_net_hashipv4_t* entry)
2350 if ((entry != NULL) && (entry->next != NULL)) {
2351 cleanup_subnet_entry(entry->next);
2358 * External Functions
2362 addr_resolve_pref_init(module_t *nameres)
2364 prefs_register_bool_preference(nameres, "mac_name",
2365 "Resolve MAC addresses",
2366 "Resolve Ethernet MAC address to manufacturer names",
2367 &gbl_resolv_flags.mac_name);
2369 prefs_register_bool_preference(nameres, "transport_name",
2370 "Resolve transport names",
2371 "Resolve TCP/UDP ports into service names",
2372 &gbl_resolv_flags.transport_name);
2374 prefs_register_bool_preference(nameres, "network_name",
2375 "Resolve network (IP) addresses",
2376 "Resolve IPv4, IPv6, and IPX addresses into host names."
2377 " The next set of check boxes determines how name resolution should be performed."
2378 " If no other options are checked name resolution is made from Wireshark's host file,"
2379 " capture file name resolution blocks and DNS packets in the capture.",
2380 &gbl_resolv_flags.network_name);
2382 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2383 "Use an external network name resolver",
2384 "Use your system's configured name resolver"
2385 " (usually DNS) to resolve network names."
2386 " Only applies when network name resolution"
2388 &gbl_resolv_flags.use_external_net_name_resolver);
2390 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
2391 prefs_register_bool_preference(nameres, "concurrent_dns",
2392 "Enable concurrent DNS name resolution",
2393 "Enable concurrent DNS name resolution. Only"
2394 " applies when network name resolution is"
2395 " enabled. You probably want to enable this.",
2396 &gbl_resolv_flags.concurrent_dns);
2398 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2399 "Maximum concurrent requests",
2400 "The maximum number of DNS requests that may"
2401 " be active at any time. A large value (many"
2402 " thousands) might overload the network or make"
2403 " your DNS server behave badly.",
2405 &name_resolve_concurrency);
2407 prefs_register_static_text_preference(nameres, "concurrent_dns",
2408 "Enable concurrent DNS name resolution: N/A",
2409 "Support for concurrent DNS name resolution was not"
2410 " compiled into this version of Wireshark");
2413 prefs_register_bool_preference(nameres, "hosts_file_handling",
2414 "Only use the profile \"hosts\" file",
2415 "By default \"hosts\" files will be loaded from multiple sources."
2416 " Checking this box only loads the \"hosts\" in the current profile.",
2417 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2423 host_name_lookup_process(void) {
2424 async_dns_queue_msg_t *caqm;
2425 struct timeval tv = { 0, 0 };
2428 gboolean nro = new_resolved_objects;
2430 new_resolved_objects = FALSE;
2432 if (!async_dns_initialized)
2433 /* c-ares not initialized. Bail out and cancel timers. */
2436 async_dns_queue_head = g_list_first(async_dns_queue_head);
2438 while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2439 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2440 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2441 if (caqm->family == AF_INET) {
2442 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2443 c_ares_ghba_cb, caqm);
2444 async_dns_in_flight++;
2445 } else if (caqm->family == AF_INET6) {
2446 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2447 AF_INET6, c_ares_ghba_cb, caqm);
2448 async_dns_in_flight++;
2454 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2456 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2457 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2460 ares_process(ghba_chan, &rfds, &wfds);
2463 /* Any new entries? */
2468 _host_name_lookup_cleanup(void) {
2471 cur = g_list_first(async_dns_queue_head);
2474 cur = g_list_next (cur);
2477 g_list_free(async_dns_queue_head);
2478 async_dns_queue_head = NULL;
2480 if (async_dns_initialized) {
2481 ares_destroy(ghba_chan);
2482 ares_destroy(ghbn_chan);
2484 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2485 ares_library_cleanup();
2487 async_dns_initialized = FALSE;
2490 #elif defined(HAVE_GNU_ADNS)
2492 /* XXX - The ADNS "documentation" isn't very clear:
2493 * - Do we need to keep our query structures around?
2496 host_name_lookup_process(void) {
2497 async_dns_queue_msg_t *almsg;
2499 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2504 gboolean nro = new_resolved_objects;
2506 new_resolved_objects = FALSE;
2507 async_dns_queue_head = g_list_first(async_dns_queue_head);
2509 cur = async_dns_queue_head;
2510 while (cur && async_dns_in_flight <= name_resolve_concurrency) {
2511 almsg = (async_dns_queue_msg_t *) cur->data;
2512 if (! almsg->submitted && almsg->type == AF_INET) {
2513 addr_bytes = (guint8 *) &almsg->ip4_addr;
2514 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2515 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2516 /* XXX - what if it fails? */
2517 adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
2518 almsg->submitted = TRUE;
2519 async_dns_in_flight++;
2524 cur = async_dns_queue_head;
2527 almsg = (async_dns_queue_msg_t *) cur->data;
2528 if (almsg->submitted) {
2529 ret = adns_check(ads, &almsg->query, &ans, NULL);
2531 if (ans->status == adns_s_ok) {
2532 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2539 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2541 /* XXX, what to do if async_dns_in_flight == 0? */
2542 async_dns_in_flight--;
2546 /* Keep the timeout in place */
2551 _host_name_lookup_cleanup(void) {
2554 async_dns_queue_head = g_list_first(async_dns_queue_head);
2555 while (async_dns_queue_head) {
2556 qdata = async_dns_queue_head->data;
2557 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2561 if (async_dns_initialized)
2563 async_dns_initialized = FALSE;
2566 #else /* HAVE_GNU_ADNS */
2569 host_name_lookup_process(void) {
2570 gboolean nro = new_resolved_objects;
2572 new_resolved_objects = FALSE;
2578 _host_name_lookup_cleanup(void) {
2581 #endif /* HAVE_C_ARES */
2584 get_hostname(const guint addr)
2588 /* XXX why do we call this if we're not resolving? To create hash entries?
2591 hashipv4_t *tp = host_lookup(addr, &found);
2593 if (!gbl_resolv_flags.network_name)
2596 tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
2601 /* -------------------------- */
2604 get_hostname6(const struct e_in6_addr *addr)
2608 /* XXX why do we call this if we're not resolving? To create hash entries?
2611 hashipv6_t *tp = host_lookup6(addr, &found);
2613 if (!gbl_resolv_flags.network_name)
2616 tp->flags = tp->flags | RESOLVED_ADDRESS_USED;
2621 /* -------------------------- */
2623 add_ipv4_name(const guint addr, const gchar *name)
2628 * Don't add zero-length names; apparently, some resolvers will return
2629 * them if they get them from DNS.
2631 if (name[0] == '\0')
2635 tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2637 g_strlcpy(tp->name, name, MAXNAMELEN);
2639 tp = new_ipv4(addr);
2640 g_strlcpy(tp->name, name, MAXNAMELEN);
2641 g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2644 g_strlcpy(tp->name, name, MAXNAMELEN);
2645 tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
2646 new_resolved_objects = TRUE;
2648 } /* add_ipv4_name */
2650 /* -------------------------- */
2652 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2657 * Don't add zero-length names; apparently, some resolvers will return
2658 * them if they get them from DNS.
2660 if (name[0] == '\0')
2663 tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
2665 g_strlcpy(tp->name, name, MAXNAMELEN);
2667 struct e_in6_addr *addr_key;
2669 addr_key = g_new(struct e_in6_addr,1);
2670 tp = new_ipv6(addrp);
2671 memcpy(addr_key, addrp, 16);
2672 g_strlcpy(tp->name, name, MAXNAMELEN);
2673 g_hash_table_insert(ipv6_hash_table, addr_key, tp);
2676 g_strlcpy(tp->name, name, MAXNAMELEN);
2677 tp->flags = tp->flags | TRIED_RESOLVE_ADDRESS;
2678 new_resolved_objects = TRUE;
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(port, PT_UDP));
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(port, PT_DCCP));
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(port, PT_TCP));
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(port, PT_SCTP));
2946 } /* sctp_port_to_display */
2949 address_to_display(wmem_allocator_t *allocator, const address *addr)
2952 const gchar *result = solve_address_to_name(addr);
2954 if (result != NULL) {
2955 str = wmem_strdup(allocator, result);
2957 else if (addr->type == AT_NONE) {
2958 str = wmem_strdup(allocator, "NONE");
2961 str = (gchar *) wmem_alloc(allocator, MAX_ADDR_STR_LEN);
2962 address_to_str_buf(addr, str, MAX_ADDR_STR_LEN);
2969 get_addr_name(const address *addr)
2972 struct e_in6_addr ip6_addr;
2975 * Try to look up a name for this address.
2976 * If it's not found, this might return a string corresponding to
2977 * the address, or it might return NULL.
2979 * Whatever string is returned has at least session scope.
2981 switch (addr->type) {
2984 return get_ether_name((const guint8 *)addr->data);
2987 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
2988 return get_hostname(ip4_addr);
2991 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
2992 return get_hostname6(&ip6_addr);
3000 get_ether_name(const guint8 *addr)
3003 gboolean resolve = gbl_resolv_flags.mac_name;
3005 tp = eth_name_lookup(addr, resolve);
3007 return resolve ? tp->resolved_name : tp->hexaddr;
3009 } /* get_ether_name */
3011 /* Look for a (non-dummy) ether name in the hash, and return it if found.
3012 * If it's not found, simply return NULL.
3015 get_ether_name_if_known(const guint8 *addr)
3019 /* Initialize ether structs if we're the first
3020 * ether-related function called */
3021 if (!gbl_resolv_flags.mac_name)
3024 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3025 tp = eth_name_lookup(addr, TRUE);
3026 g_assert(tp != NULL);
3028 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3029 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
3030 return tp->resolved_name;
3033 /* Name was created */
3039 get_ether_addr(const gchar *name)
3042 /* force resolution (do not check gbl_resolv_flags) */
3043 return eth_addr_lookup(name);
3045 } /* get_ether_addr */
3048 add_ether_byip(const guint ip, const guint8 *eth)
3053 /* first check that IP address can be resolved */
3054 if (!gbl_resolv_flags.network_name)
3057 tp = host_lookup(ip, &found);
3059 /* ok, we can add this entry in the ethers hashtable */
3060 add_eth_name(eth, tp->name);
3063 } /* add_ether_byip */
3066 ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct)
3068 gchar *buf = (gchar *)wmem_alloc(scope, 12);
3070 *dword_to_hex_punct(buf, ad, punct) = '\0';
3075 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3078 if (!gbl_resolv_flags.network_name) {
3079 return ipxnet_to_str_punct(allocator, addr, '\0');
3082 return ipxnet_name_lookup(allocator, addr);
3084 } /* get_ipxnet_name */
3087 get_ipxnet_addr(const gchar *name, gboolean *known)
3092 /* force resolution (do not check gbl_resolv_flags) */
3093 addr = ipxnet_addr_lookup(name, &success);
3098 } /* get_ipxnet_addr */
3101 get_manuf_name(const guint8 *addr)
3103 hashmanuf_t *manuf_value;
3107 /* manuf needs only the 3 most significant octets of the ethernet address */
3108 manuf_key = addr[0];
3109 manuf_key = manuf_key<<8;
3111 manuf_key = manuf_key | oct;
3112 manuf_key = manuf_key<<8;
3114 manuf_key = manuf_key | oct;
3116 manuf_value = manuf_name_lookup(addr);
3117 if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3118 return manuf_value->resolved_name;
3120 return manuf_value->hexaddr;
3122 } /* get_manuf_name */
3125 uint_get_manuf_name(const guint oid)
3129 addr[0] = (oid >> 16) & 0xFF;
3130 addr[1] = (oid >> 8) & 0xFF;
3131 addr[2] = (oid >> 0) & 0xFF;
3132 return get_manuf_name(addr);
3136 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3138 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3142 get_manuf_name_if_known(const guint8 *addr)
3144 hashmanuf_t *manuf_value;
3148 /* manuf needs only the 3 most significant octets of the ethernet address */
3149 manuf_key = addr[0];
3150 manuf_key = manuf_key<<8;
3152 manuf_key = manuf_key | oct;
3153 manuf_key = manuf_key<<8;
3155 manuf_key = manuf_key | oct;
3157 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3158 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3162 return manuf_value->resolved_name;
3164 } /* get_manuf_name_if_known */
3167 uint_get_manuf_name_if_known(const guint manuf_key)
3169 hashmanuf_t *manuf_value;
3171 manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
3172 if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
3176 return manuf_value->resolved_name;
3180 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3182 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3185 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3187 return manuf->resolved_name;
3191 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3193 guint8 *addr = (guint8 *)wmem_alloc(allocator, 8);
3194 hashmanuf_t *manuf_value;
3196 /* Copy and convert the address to network byte order. */
3197 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3199 manuf_value = manuf_name_lookup(addr);
3200 if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3201 return 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]);
3203 return wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3205 } /* eui64_to_display */
3208 #define GHI_TIMEOUT (250 * 1000)
3213 #if ( ( ARES_VERSION_MAJOR < 1 ) \
3214 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3223 * XXX - If we wanted to be really fancy we could cache results here and
3224 * look them up in get_host_ipaddr* below.
3226 async_hostent_t *ahp = (async_hostent_t *)arg;
3227 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3228 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3229 ahp->copied = hp->h_length;
3232 #endif /* HAVE_C_ARES */
3234 /* Translate a string, assumed either to be a dotted-quad IP address or
3235 * a host name, to a numeric IP address. Return TRUE if we succeed and
3236 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3237 * Used more in the dfilter parser rather than in packet dissectors */
3239 get_host_ipaddr(const char *host, guint32 *addrp)
3241 struct in_addr ipaddr;
3243 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3246 async_hostent_t ahe;
3247 #else /* HAVE_C_ARES */
3248 struct addrinfo hint, *result = NULL;
3249 #endif /* HAVE_C_ARES */
3252 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3253 * inet_pton(AF_INET) does not support hexadecimal notation nor
3254 * less-than-4 octet notation.
3256 if (!inet_aton(host, &ipaddr)) {
3258 /* It's not a valid dotted-quad IP address; is it a valid
3262 /* If we're not allowed to do name resolution, don't do name
3265 if (!gbl_resolv_flags.network_name ||
3266 !gbl_resolv_flags.use_external_net_name_resolver) {
3271 if (! (gbl_resolv_flags.concurrent_dns) ||
3272 name_resolve_concurrency < 1 ||
3273 ! async_dns_initialized) {
3276 ahe.addr_size = (int) sizeof (struct in_addr);
3279 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3282 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3284 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3285 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3286 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3289 ares_process(ghbn_chan, &rfds, &wfds);
3291 ares_cancel(ghbn_chan);
3292 if (ahe.addr_size == ahe.copied) {
3296 #else /* ! HAVE_C_ARES */
3298 * This can be slow, particularly for capture files with lots of
3299 * addresses. Should we just return FALSE instead?
3301 memset(&hint, 0, sizeof(hint));
3302 hint.ai_family = AF_INET;
3303 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3304 /* Probably more checks than necessary */
3305 if (result != NULL) {
3306 gboolean ret_val = FALSE;
3307 if (result->ai_family == AF_INET && result->ai_addrlen == 4) {
3308 memcpy(&ipaddr, result->ai_addr->sa_data, result->ai_addrlen);
3311 freeaddrinfo(result);
3315 #endif /* HAVE_C_ARES */
3317 /* Does the string really contain dotted-quad IP?
3318 * Check against inet_atons that accept strings such as
3319 * "130.230" as valid addresses and try to convert them
3320 * to some form of a classful (host.net) notation.
3322 unsigned int a0, a1, a2, a3;
3323 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3327 *addrp = ipaddr.s_addr;
3332 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3333 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3334 * return FALSE if we fail.
3337 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3340 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3343 async_hostent_t ahe;
3344 #elif defined(HAVE_GETADDRINFO)
3345 struct addrinfo hint, *result = NULL;
3346 #endif /* HAVE_C_ARES */
3348 if (str_to_ip6(host, addrp))
3351 /* It's not a valid dotted-quad IP address; is it a valid
3355 /* If we're not allowed to do name resolution, don't do name
3358 if (!gbl_resolv_flags.network_name ||
3359 !gbl_resolv_flags.use_external_net_name_resolver) {
3365 if (! (gbl_resolv_flags.concurrent_dns) ||
3366 name_resolve_concurrency < 1 ||
3367 ! async_dns_initialized) {
3370 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3373 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3376 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3378 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3379 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3380 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3383 ares_process(ghbn_chan, &rfds, &wfds);
3385 ares_cancel(ghbn_chan);
3386 if (ahe.addr_size == ahe.copied) {
3389 #elif defined(HAVE_GETADDRINFO)
3391 * This can be slow, particularly for capture files with lots of
3392 * addresses. Should we just return FALSE instead?
3394 memset(&hint, 0, sizeof(hint));
3395 hint.ai_family = AF_INET6;
3396 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3397 /* Probably more checks than necessary */
3398 if (result != NULL) {
3399 gboolean ret_val = FALSE;
3400 if (result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct e_in6_addr)) {
3401 memcpy(addrp, result->ai_addr->sa_data, result->ai_addrlen);
3404 freeaddrinfo(result);
3414 * Find out whether a hostname resolves to an ip or ipv6 address
3415 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3416 * that we don't know)
3418 const char* host_ip_af(const char *host
3419 #ifndef HAVE_GETADDRINFO
3424 const char *af = "ip";
3425 #ifdef HAVE_GETADDRINFO
3426 struct addrinfo hint, *result = NULL;
3427 memset(&hint, 0, sizeof(hint));
3428 hint.ai_family = AF_UNSPEC;
3429 if (getaddrinfo(host, NULL, &hint, &result) == 0) {
3430 if (result->ai_family == AF_INET6) {
3433 freeaddrinfo(result);
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: