2 * Routines for network object lookup
4 * Laurent Deniel <laurent.deniel@free.fr>
6 * Add option to resolv VLAN ID to describing name
7 * Uli Heilmeier, March 2016
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <wsutil/strtoi.h>
38 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
39 * are most likely to take a long time, given the way address-to-name
40 * lookups are done over NBNS).
42 * macOS does have SIGALRM, but if you longjmp() out of a name resolution
43 * call in a signal handler, you might crash, because the state of the
44 * resolution code that sends messages to lookupd might be inconsistent
45 * if you jump out of it in middle of a call.
47 * There's no guarantee that longjmp()ing out of name resolution calls
48 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
49 * code in tcpdump, to avoid those sorts of problems, and that was
50 * picked up by tcpdump.org tcpdump.
52 * So, for now, we do not use alarm() and SIGALRM to time out host name
53 * lookups. If we get a lot of complaints about lookups taking a long time,
54 * we can reconsider that decision. (Note that tcpdump originally added
55 * such a timeout mechanism that for the benefit of systems using NIS to
56 * look up host names; that might now be fixed in NIS implementations, for
57 * those sites still using NIS rather than DNS for that.... tcpdump no
58 * longer does that, for the same reasons that we don't.)
60 * If we're using an asynchronous DNS resolver, that shouldn't be an issue.
61 * If we're using a synchronous name lookup mechanism (which we'd do mainly
62 * to support resolving addresses and host names using more mechanisms than
63 * just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
64 * a separate thread, making it, in effect, asynchronous.
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
75 #ifdef HAVE_SYS_SOCKET_H
76 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
79 #ifdef HAVE_WINSOCK2_H
80 #include <winsock2.h> /* needed to define AF_ values on Windows */
84 # include <ws2tcpip.h>
89 # define socklen_t unsigned int
92 # include <ares_version.h>
93 #endif /* HAVE_C_ARES */
98 #include "addr_and_mask.h"
100 #include "addr_resolv.h"
101 #include "wsutil/filesystem.h"
103 #include <wsutil/report_message.h>
104 #include <wsutil/file_util.h>
105 #include <wsutil/pint.h>
106 #include <wsutil/inet_addr.h>
108 #include <epan/strutil.h>
109 #include <epan/to_str-int.h>
110 #include <epan/prefs.h>
112 #define ENAME_HOSTS "hosts"
113 #define ENAME_SUBNETS "subnets"
114 #define ENAME_ETHERS "ethers"
115 #define ENAME_IPXNETS "ipxnets"
116 #define ENAME_MANUF "manuf"
117 #define ENAME_WKA "wka"
118 #define ENAME_SERVICES "services"
119 #define ENAME_VLANS "vlans"
120 #define ENAME_SS7PCS "ss7pcs"
121 #define ENAME_ENTERPRISES "enterprises.tsv"
123 #define HASHETHSIZE 2048
124 #define HASHHOSTSIZE 2048
125 #define HASHIPXNETSIZE 256
126 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
128 /* hash table used for IPv4 lookup */
130 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
133 typedef struct sub_net_hashipv4 {
135 /* XXX: No longer needed?*/
136 guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
137 struct sub_net_hashipv4 *next;
138 gchar name[MAXNAMELEN];
139 } sub_net_hashipv4_t;
141 /* Array of entries of subnets of different lengths */
143 gsize mask_length; /*1-32*/
144 guint32 mask; /* e.g. 255.255.255.*/
145 sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
146 } subnet_length_entry_t;
149 /* hash table used for IPX network lookup */
151 /* XXX - check goodness of hash function */
153 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
155 typedef struct hashipxnet {
157 struct hashipxnet *next;
158 gchar name[MAXNAMELEN];
161 typedef struct hashvlan {
163 /* struct hashvlan *next; */
164 gchar name[MAXVLANNAMELEN];
167 typedef struct ss7pc {
168 guint32 id; /* 1st byte NI, 3 following bytes: Point Code */
169 gchar pc_addr[MAXNAMELEN];
170 gchar name[MAXNAMELEN];
173 /* hash tables used for ethernet and manufacturer lookup */
174 #define HASHETHER_STATUS_UNRESOLVED 1
175 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
176 #define HASHETHER_STATUS_RESOLVED_NAME 3
179 guint status; /* (See above) */
182 char resolved_name[MAXNAMELEN];
186 guint status; /* (See above) */
189 char resolved_name[MAXNAMELEN];
190 char resolved_longname[MAXNAMELEN];
193 /* internal ethernet type */
194 typedef struct _ether
197 char name[MAXNAMELEN];
198 char longname[MAXNAMELEN];
201 /* internal ipxnet type */
202 typedef struct _ipxnet
205 char name[MAXNAMELEN];
208 /* internal vlan type */
212 char name[MAXVLANNAMELEN];
215 static wmem_map_t *ipxnet_hash_table = NULL;
216 static wmem_map_t *ipv4_hash_table = NULL;
217 static wmem_map_t *ipv6_hash_table = NULL;
218 static wmem_map_t *vlan_hash_table = NULL;
219 static wmem_map_t *ss7pc_hash_table = NULL;
221 static wmem_list_t *manually_resolved_ipv4_list = NULL;
222 static wmem_list_t *manually_resolved_ipv6_list = NULL;
224 typedef struct _resolved_ipv4
227 char name[MAXNAMELEN];
230 typedef struct _resolved_ipv6
232 struct e_in6_addr ip6_addr;
233 char name[MAXNAMELEN];
236 static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
238 struct cb_serv_data {
243 static wmem_map_t *manuf_hashtable = NULL;
244 static wmem_map_t *wka_hashtable = NULL;
245 static wmem_map_t *eth_hashtable = NULL;
246 static wmem_map_t *serv_port_hashtable = NULL;
247 static GHashTable *enterprises_hashtable = NULL;
249 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
250 static gboolean have_subnet_entry = FALSE;
252 static gboolean new_resolved_objects = FALSE;
254 static GPtrArray* extra_hosts_files = NULL;
256 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
257 static void add_serv_port_cb(const guint32 port, gpointer ptr);
260 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
264 ipv6_oat_hash(gconstpointer key)
267 const unsigned char *p = (const unsigned char *)key;
271 for ( i = 0; i < len; i++ ) {
285 ipv6_equal(gconstpointer v1, gconstpointer v2)
288 if (memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0) {
296 * Flag controlling what names to resolve.
298 e_addr_resolve gbl_resolv_flags = {
300 FALSE, /* network_name */
301 FALSE, /* transport_name */
302 TRUE, /* dns_pkt_addr_resolution */
303 TRUE, /* use_external_net_name_resolver */
304 FALSE, /* load_hosts_file_from_profile_only */
305 FALSE, /* vlan_name */
306 FALSE /* ss7 point code names */
309 static guint name_resolve_concurrency = 500;
313 * Global variables (can be changed in GUI sections)
314 * XXX - they could be changed in GUI code, but there's currently no
315 * GUI code to change them.
318 gchar *g_ethers_path = NULL; /* global ethers file */
319 gchar *g_pethers_path = NULL; /* personal ethers file */
320 gchar *g_wka_path = NULL; /* global well-known-addresses file */
321 gchar *g_manuf_path = NULL; /* global manuf file */
322 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
323 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
324 gchar *g_services_path = NULL; /* global services file */
325 gchar *g_pservices_path = NULL; /* personal services file */
326 gchar *g_pvlan_path = NULL; /* personal vlans file */
327 gchar *g_ss7pcs_path = NULL; /* personal ss7pcs file */
328 gchar *g_enterprises_path = NULL; /* global enterprises file */
329 gchar *g_penterprises_path = NULL; /* personal enterprises file */
330 /* first resolving call */
335 * Submitted queries trigger a callback (c_ares_ghba_cb()).
336 * Queries are added to c_ares_queue_head. During processing, queries are
337 * popped off the front of c_ares_queue_head and submitted using
338 * ares_gethostbyaddr().
339 * The callback processes the response, then frees the request.
341 typedef struct _async_dns_queue_msg
345 struct e_in6_addr ip6;
348 } async_dns_queue_msg_t;
350 typedef struct _async_hostent {
356 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
357 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
359 static gboolean async_dns_initialized = FALSE;
360 static guint async_dns_in_flight = 0;
361 static wmem_list_t *async_dns_queue_head = NULL;
363 /* push a dns request */
365 add_async_dns_ipv4(int type, guint32 addr)
367 async_dns_queue_msg_t *msg;
369 msg = wmem_new(wmem_epan_scope(), async_dns_queue_msg_t);
371 msg->addr.ip4 = addr;
372 wmem_list_append(async_dns_queue_head, (gpointer) msg);
374 #endif /* HAVE_C_ARES */
379 const gchar* name; /* Shallow copy */
383 * Miscellaneous functions
387 fgetline(char **buf, int *size, FILE *fp)
392 if (fp == NULL || buf == NULL)
399 *buf = (char *)wmem_alloc(wmem_epan_scope(), *size);
409 while ((c = ws_getc_unlocked(fp)) != EOF && c != '\r' && c != '\n') {
410 if (len+1 >= *size) {
411 *buf = (char *)wmem_realloc(wmem_epan_scope(), *buf, *size += BUFSIZ);
416 if (len == 0 && c == EOF)
427 * Local function definitions
429 static subnet_entry_t subnet_lookup(const guint32 addr);
430 static void subnet_entry_set(guint32 subnet_addr, const guint8 mask_length, const gchar* name);
434 add_service_name(port_type proto, const guint port, const char *service_name)
436 serv_port_t *serv_port_table;
439 key = (int *)wmem_new(wmem_epan_scope(), int);
442 serv_port_table = (serv_port_t *)wmem_map_lookup(serv_port_hashtable, &port);
443 if (serv_port_table == NULL) {
444 serv_port_table = wmem_new0(wmem_epan_scope(), serv_port_t);
445 wmem_map_insert(serv_port_hashtable, key, serv_port_table);
448 wmem_free(wmem_epan_scope(), key);
453 wmem_free(wmem_epan_scope(), serv_port_table->tcp_name);
454 serv_port_table->tcp_name = wmem_strdup(wmem_epan_scope(), service_name);
457 wmem_free(wmem_epan_scope(), serv_port_table->udp_name);
458 serv_port_table->udp_name = wmem_strdup(wmem_epan_scope(), service_name);
461 wmem_free(wmem_epan_scope(), serv_port_table->sctp_name);
462 serv_port_table->sctp_name = wmem_strdup(wmem_epan_scope(), service_name);
465 wmem_free(wmem_epan_scope(), serv_port_table->dccp_name);
466 serv_port_table->dccp_name = wmem_strdup(wmem_epan_scope(), service_name);
470 /* Should not happen */
473 new_resolved_objects = TRUE;
478 parse_service_line (char *line)
484 struct cb_serv_data cb_data;
485 range_t *port_rng = NULL;
487 if ((cp = strchr(line, '#')))
490 if ((cp = strtok(line, " \t")) == NULL)
495 if ((cp = strtok(NULL, " \t")) == NULL)
500 if (strtok(cp, "/") == NULL)
503 if (range_convert_str(NULL, &port_rng, port, G_MAXUINT16) != CVT_NO_ERROR) {
504 wmem_free (NULL, port_rng);
508 while ((cp = strtok(NULL, "/")) != NULL) {
509 if (strcmp(cp, "tcp") == 0) {
512 else if (strcmp(cp, "udp") == 0) {
515 else if (strcmp(cp, "sctp") == 0) {
518 else if (strcmp(cp, "dccp") == 0) {
524 cb_data.service = service;
525 cb_data.proto = proto;
526 range_foreach(port_rng, add_serv_port_cb, &cb_data);
529 wmem_free (NULL, port_rng);
530 } /* parse_service_line */
534 add_serv_port_cb(const guint32 port, gpointer ptr)
536 struct cb_serv_data *cb_data = (struct cb_serv_data *)ptr;
539 add_service_name(cb_data->proto, port, cb_data->service);
545 parse_services_file(const char * path)
549 static char *buf = NULL;
551 /* services hash table initialization */
552 serv_p = ws_fopen(path, "r");
557 while (fgetline(&buf, &size, serv_p) >= 0) {
558 parse_service_line(buf);
566 * unsigned integer to ascii
569 wmem_utoa(wmem_allocator_t *allocator, guint port)
571 gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
573 /* XXX, guint32_to_str() ? */
574 guint32_to_str_buf(port, bp, MAXNAMELEN);
579 _serv_name_lookup(port_type proto, guint port, serv_port_t **value_ret)
581 serv_port_t *serv_port_table;
583 serv_port_table = (serv_port_t *)wmem_map_lookup(serv_port_hashtable, &port);
585 if (value_ret != NULL)
586 *value_ret = serv_port_table;
588 if (serv_port_table == NULL)
593 return serv_port_table->udp_name;
595 return serv_port_table->tcp_name;
597 return serv_port_table->sctp_name;
599 return serv_port_table->dccp_name;
607 try_serv_name_lookup(port_type proto, guint port)
609 return _serv_name_lookup(proto, port, NULL);
613 serv_name_lookup(port_type proto, guint port)
615 serv_port_t *serv_port_table = NULL;
619 name = _serv_name_lookup(proto, port, &serv_port_table);
623 if (serv_port_table == NULL) {
624 key = (guint *)wmem_new(wmem_epan_scope(), guint);
626 serv_port_table = wmem_new0(wmem_epan_scope(), serv_port_t);
627 wmem_map_insert(serv_port_hashtable, key, serv_port_table);
629 if (serv_port_table->numeric == NULL) {
630 serv_port_table->numeric = wmem_strdup_printf(wmem_epan_scope(), "%u", port);
633 return serv_port_table->numeric;
637 initialize_services(void)
639 gboolean parse_file = TRUE;
640 g_assert(serv_port_hashtable == NULL);
641 serv_port_hashtable = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
643 /* Compute the pathname of the services file. */
644 if (g_services_path == NULL) {
645 g_services_path = get_datafile_path(ENAME_SERVICES);
647 parse_services_file(g_services_path);
649 /* Compute the pathname of the personal services file */
650 if (g_pservices_path == NULL) {
651 /* Check profile directory before personal configuration */
652 g_pservices_path = get_persconffile_path(ENAME_SERVICES, TRUE);
653 if (!parse_services_file(g_pservices_path)) {
654 g_free(g_pservices_path);
655 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
661 parse_services_file(g_pservices_path);
666 service_name_lookup_cleanup(void)
668 serv_port_hashtable = NULL;
669 g_free(g_services_path);
670 g_services_path = NULL;
671 g_free(g_pservices_path);
672 g_pservices_path = NULL;
676 parse_enterprises_line (char *line)
678 char *tok, *dec_str, *org_str;
681 if ((tok = strchr(line, '#')))
683 dec_str = strtok(line, " \t");
686 org_str = strtok(NULL, ""); /* everything else */
688 org_str = g_strstrip(org_str);
691 if (!ws_strtou32(dec_str, NULL, &dec))
693 g_hash_table_replace(enterprises_hashtable, GUINT_TO_POINTER(dec), g_strdup(org_str));
698 parse_enterprises_file(const char * path)
702 static char *buf = NULL;
704 fp = ws_fopen(path, "r");
708 while (fgetline(&buf, &size, fp) >= 0) {
709 parse_enterprises_line(buf);
717 initialize_enterprises(void)
719 g_assert(enterprises_hashtable == NULL);
720 enterprises_hashtable = g_hash_table_new_full(NULL, NULL, NULL, g_free);
722 if (g_enterprises_path == NULL) {
723 g_enterprises_path = get_datafile_path(ENAME_ENTERPRISES);
725 parse_enterprises_file(g_enterprises_path);
727 if (g_penterprises_path == NULL) {
728 g_penterprises_path = get_persconffile_path(ENAME_ENTERPRISES, FALSE);
730 parse_enterprises_file(g_penterprises_path);
734 try_enterprises_lookup(guint32 value)
736 return (const gchar *)g_hash_table_lookup(enterprises_hashtable, GUINT_TO_POINTER(value));
740 enterprises_lookup(guint32 value, const char *unknown_str)
744 s = try_enterprises_lookup(value);
747 if (unknown_str != NULL)
753 enterprises_base_custom(char *buf, guint32 value)
757 if ((s = try_enterprises_lookup(value)) == NULL)
758 s = ITEM_LABEL_UNKNOWN_STR;
759 g_snprintf(buf, ITEM_LABEL_LENGTH, "%s (%u)", s, value);
763 enterprises_cleanup(void)
765 g_assert(enterprises_hashtable);
766 g_hash_table_destroy(enterprises_hashtable);
767 enterprises_hashtable = NULL;
768 g_assert(g_enterprises_path);
769 g_free(g_enterprises_path);
770 g_enterprises_path = NULL;
771 if (g_pservices_path) {
772 g_free(g_pservices_path);
773 g_pservices_path = NULL;
777 /* Fill in an IP4 structure with info from subnets file or just with the
778 * string form of the address.
781 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
783 subnet_entry_t subnet_entry;
785 /* Overwrite if we get async DNS reply */
787 /* Do we have a subnet for this address? */
788 subnet_entry = subnet_lookup(addr);
789 if (0 != subnet_entry.mask) {
790 /* Print name, then '.' then IP address after subnet mask */
792 gchar buffer[MAX_IP_STR_LEN];
796 host_addr = addr & (~(guint32)subnet_entry.mask);
797 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
800 /* Skip to first octet that is not totally masked
801 * If length of mask is 32, we chomp the whole address.
802 * If the address string starts '.' (should not happen?),
805 i = subnet_entry.mask_length / 8;
806 while(*(paddr) != '\0' && i > 0) {
807 if (*(++paddr) == '.') {
812 /* There are more efficient ways to do this, but this is safe if we
813 * trust g_snprintf and MAXNAMELEN
815 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
817 /* XXX: This means we end up printing "1.2.3.4 (1.2.3.4)" in many cases */
818 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
823 /* Fill in an IP6 structure with the string form of the address.
826 fill_dummy_ip6(hashipv6_t* volatile tp)
828 /* Overwrite if we get async DNS reply */
829 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
835 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
836 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
840 /* XXX, what to do if async_dns_in_flight == 0? */
841 async_dns_in_flight--;
843 if (status == ARES_SUCCESS) {
844 for (p = he->h_addr_list; *p != NULL; p++) {
845 switch(caqm->family) {
847 add_ipv4_name(caqm->addr.ip4, he->h_name);
850 add_ipv6_name(&caqm->addr.ip6, he->h_name);
853 /* Throw an exception? */
858 wmem_free(wmem_epan_scope(), caqm);
860 #endif /* HAVE_C_ARES */
862 /* --------------- */
864 new_ipv4(const guint addr)
866 hashipv4_t *tp = wmem_new(wmem_epan_scope(), hashipv4_t);
870 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
875 host_lookup(const guint addr)
877 hashipv4_t * volatile tp;
879 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
882 * We don't already have an entry for this host name; create one,
883 * and then try to resolve it.
886 fill_dummy_ip4(addr, tp);
887 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
888 } else if (tp->flags & TRIED_OR_RESOLVED_MASK) {
893 * This hasn't been resolved yet, and we haven't tried to
894 * resolve it already.
897 if (!gbl_resolv_flags.network_name)
900 if (gbl_resolv_flags.use_external_net_name_resolver) {
901 tp->flags |= TRIED_RESOLVE_ADDRESS;
904 if (async_dns_initialized && name_resolve_concurrency > 0) {
905 add_async_dns_ipv4(AF_INET, addr);
914 /* --------------- */
916 new_ipv6(const struct e_in6_addr *addr)
918 hashipv6_t *tp = wmem_new(wmem_epan_scope(), hashipv6_t);
919 memcpy(tp->addr, addr->bytes, sizeof tp->addr);
922 ip6_to_str_buf(addr, tp->ip6, sizeof(tp->ip6));
926 /* ------------------------------------ */
928 host_lookup6(const struct e_in6_addr *addr)
930 hashipv6_t * volatile tp;
932 async_dns_queue_msg_t *caqm;
935 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addr);
938 * We don't already have an entry for this host name; create one,
939 * and then try to resolve it.
941 struct e_in6_addr *addr_key;
943 addr_key = wmem_new(wmem_epan_scope(), struct e_in6_addr);
945 memcpy(addr_key, addr, 16);
947 wmem_map_insert(ipv6_hash_table, addr_key, tp);
948 } else if (tp->flags & TRIED_OR_RESOLVED_MASK) {
953 * This hasn't been resolved yet, and we haven't tried to
954 * resolve it already.
957 if (!gbl_resolv_flags.network_name)
960 if (gbl_resolv_flags.use_external_net_name_resolver) {
961 tp->flags |= TRIED_RESOLVE_ADDRESS;
963 if (async_dns_initialized && name_resolve_concurrency > 0) {
964 caqm = wmem_new(wmem_epan_scope(), async_dns_queue_msg_t);
965 caqm->family = AF_INET6;
966 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
967 wmem_list_append(async_dns_queue_head, (gpointer) caqm);
977 * Ethernet / manufacturer resolution
979 * The following functions implement ethernet address resolution and
980 * ethers files parsing (see ethers(4)).
982 * The manuf file has the same format as ethers(4) except that names are
983 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
984 * only 3 bytes (instead of 6).
988 * I decide to not use the existing functions (see ethers(3) on some
989 * operating systems) for the following reasons:
990 * - performance gains (use of hash tables and some other enhancements),
991 * - use of two ethers files (system-wide and per user),
992 * - avoid the use of NIS maps,
993 * - lack of these functions on some systems.
995 * So the following functions do _not_ behave as the standard ones.
1002 * If "accept_mask" is FALSE, either 3 or 6 bytes are valid, but no other number of bytes is.
1003 * If "accept_mask" is TRUE, parse an up-to-6-byte sequence with an optional
1007 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1008 const gboolean accept_mask)
1015 for (i = 0; i < 6; i++) {
1016 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1017 if (!g_ascii_isxdigit(*cp))
1019 num = strtoul(cp, &p, 16);
1021 return FALSE; /* failed */
1023 return FALSE; /* not a valid octet */
1024 eth->addr[i] = (guint8) num;
1025 cp = p; /* skip past the number */
1027 /* OK, what character terminated the octet? */
1029 /* "/" - this has a mask. */
1031 /* Entries with masks are not allowed in this file. */
1034 cp++; /* skip past the '/' to get to the mask */
1035 if (!g_ascii_isdigit(*cp))
1036 return FALSE; /* no sign allowed */
1037 num = strtoul(cp, &p, 10);
1039 return FALSE; /* failed */
1040 cp = p; /* skip past the number */
1041 if (*cp != '\0' && !g_ascii_isspace(*cp))
1042 return FALSE; /* bogus terminator */
1043 if (num == 0 || num >= 48)
1044 return FALSE; /* bogus mask */
1045 /* Mask out the bits not covered by the mask */
1047 for (i = 0; num >= 8; i++, num -= 8)
1048 ; /* skip octets entirely covered by the mask */
1049 /* Mask out the first masked octet */
1050 eth->addr[i] &= (0xFF << (8 - num));
1052 /* Mask out completely-masked-out octets */
1058 /* We're at the end of the address, and there's no mask. */
1060 /* We got 3 bytes, so this is a manufacturer ID. */
1062 /* Manufacturer IDs are not allowed in this file */
1065 /* Indicate that this is a manufacturer ID (0 is not allowed
1072 /* We got 6 bytes, so this is a MAC address (48 is not allowed as a mask). */
1078 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1083 /* We don't know the separator used in this number; it can either
1084 be ':', '-', or '.'. */
1085 if (*cp != ':' && *cp != '-' && *cp != '.')
1087 sep = *cp; /* subsequent separators must be the same */
1089 /* It has to be the same as the first separator */
1101 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1102 const gboolean accept_mask)
1105 * See the ethers(4) or ethers(5) man page for ethers file format
1106 * (not available on all systems).
1107 * We allow both ethernet address separators (':' and '-'),
1108 * as well as Wireshark's '.' separator.
1113 line = g_strstrip(line);
1114 if (line[0] == '\0' || line[0] == '#')
1117 if ((cp = strchr(line, '#'))) {
1119 while (g_ascii_isspace(*cp)) {
1125 if ((cp = strtok(line, " \t")) == NULL)
1128 if (!parse_ether_address(cp, eth, mask, accept_mask))
1131 if ((cp = strtok(NULL, " \t")) == NULL)
1134 g_strlcpy(eth->name, cp, MAXNAMELEN);
1136 if ((cp = strtok(NULL, "")) != NULL)
1138 g_strlcpy(eth->longname, cp, MAXNAMELEN);
1140 /* Make the long name the short name */
1141 g_strlcpy(eth->longname, eth->name, MAXNAMELEN);
1146 } /* parse_ether_line */
1148 static FILE *eth_p = NULL;
1151 set_ethent(char *path)
1156 eth_p = ws_fopen(path, "r");
1169 get_ethent(unsigned int *mask, const gboolean accept_mask)
1173 static int size = 0;
1174 static char *buf = NULL;
1179 while (fgetline(&buf, &size, eth_p) >= 0) {
1180 if (parse_ether_line(buf, ð, mask, accept_mask) == 0) {
1190 get_ethbyaddr(const guint8 *addr)
1195 set_ethent(g_pethers_path);
1197 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1203 set_ethent(g_ethers_path);
1205 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1213 } /* get_ethbyaddr */
1215 static hashmanuf_t *
1216 manuf_hash_new_entry(const guint8 *addr, char* name, char* longname)
1219 hashmanuf_t *manuf_value;
1222 /* manuf needs only the 3 most significant octets of the ethernet address */
1223 manuf_key = (int *)wmem_new(wmem_epan_scope(), int);
1224 *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1225 manuf_value = wmem_new(wmem_epan_scope(), hashmanuf_t);
1227 memcpy(manuf_value->addr, addr, 3);
1229 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1230 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1231 if (longname != NULL) {
1232 g_strlcpy(manuf_value->resolved_longname, longname, MAXNAMELEN);
1235 g_strlcpy(manuf_value->resolved_longname, name, MAXNAMELEN);
1239 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1240 manuf_value->resolved_name[0] = '\0';
1241 manuf_value->resolved_longname[0] = '\0';
1243 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1244 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1247 wmem_map_insert(manuf_hashtable, manuf_key, manuf_value);
1252 wka_hash_new_entry(const guint8 *addr, char* name)
1256 wka_key = (guint8 *)wmem_alloc(wmem_epan_scope(), 6);
1257 memcpy(wka_key, addr, 6);
1259 wmem_map_insert(wka_hashtable, wka_key, wmem_strdup(wmem_epan_scope(), name));
1263 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name, gchar *longname)
1268 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1269 manuf_hash_new_entry(addr, name, longname);
1273 /* This is a well-known MAC address; add it to the Ethernet hash table */
1274 add_eth_name(addr, name);
1278 /* This is a range of well-known addresses; add it to the well-known-address table */
1279 wka_hash_new_entry(addr, name);
1282 } /* add_manuf_name */
1284 static hashmanuf_t *
1285 manuf_name_lookup(const guint8 *addr)
1287 gint32 manuf_key = 0;
1289 hashmanuf_t *manuf_value;
1291 /* manuf needs only the 3 most significant octets of the ethernet address */
1292 manuf_key = addr[0];
1293 manuf_key = manuf_key<<8;
1295 manuf_key = manuf_key | oct;
1296 manuf_key = manuf_key<<8;
1298 manuf_key = manuf_key | oct;
1301 /* first try to find a "perfect match" */
1302 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1303 if (manuf_value != NULL) {
1307 /* Mask out the broadcast/multicast flag but not the locally
1308 * administered flag as locally administered means: not assigned
1309 * by the IEEE but the local administrator instead.
1310 * 0x01 multicast / broadcast bit
1311 * 0x02 locally administered bit */
1312 if ((manuf_key & 0x00010000) != 0) {
1313 manuf_key &= 0x00FEFFFF;
1314 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1315 if (manuf_value != NULL) {
1320 /* Add the address as a hex string */
1321 return manuf_hash_new_entry(addr, NULL, NULL);
1323 } /* manuf_name_lookup */
1326 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1328 guint8 masked_addr[6];
1333 if (wka_hashtable == NULL) {
1336 /* Get the part of the address covered by the mask. */
1337 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1338 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1339 /* Mask out the first masked octet */
1340 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1342 /* Zero out completely-masked-out octets */
1346 name = (gchar *)wmem_map_lookup(wka_hashtable, masked_addr);
1350 } /* wka_name_lookup */
1353 guint get_hash_ether_status(hashether_t* ether)
1355 return ether->status;
1358 char* get_hash_ether_hexaddr(hashether_t* ether)
1360 return ether->hexaddr;
1363 char* get_hash_ether_resolved_name(hashether_t* ether)
1365 return ether->resolved_name;
1369 eth_addr_hash(gconstpointer key)
1371 return wmem_strong_hash((const guint8 *)key, 6);
1375 eth_addr_cmp(gconstpointer a, gconstpointer b)
1377 return (memcmp(a, b, 6) == 0);
1381 initialize_ethers(void)
1386 /* hash table initialization */
1387 wka_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1388 manuf_hashtable = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
1389 eth_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1391 /* Compute the pathname of the ethers file. */
1392 if (g_ethers_path == NULL) {
1393 g_ethers_path = g_build_filename(get_systemfile_dir(), ENAME_ETHERS, NULL);
1396 /* Set g_pethers_path here, but don't actually do anything
1397 * with it. It's used in get_ethbyaddr().
1399 if (g_pethers_path == NULL)
1400 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1402 /* Compute the pathname of the manuf file */
1403 if (g_manuf_path == NULL)
1404 g_manuf_path = get_datafile_path(ENAME_MANUF);
1406 /* Read it and initialize the hash table */
1407 set_ethent(g_manuf_path);
1408 while ((eth = get_ethent(&mask, TRUE))) {
1409 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1413 /* Compute the pathname of the wka file */
1414 if (g_wka_path == NULL)
1415 g_wka_path = get_datafile_path(ENAME_WKA);
1417 /* Read it and initialize the hash table */
1418 set_ethent(g_wka_path);
1419 while ((eth = get_ethent(&mask, TRUE))) {
1420 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1424 } /* initialize_ethers */
1427 ethers_cleanup(void)
1429 g_free(g_ethers_path);
1430 g_ethers_path = NULL;
1431 g_free(g_pethers_path);
1432 g_pethers_path = NULL;
1433 g_free(g_manuf_path);
1434 g_manuf_path = NULL;
1439 /* Resolve ethernet address */
1440 static hashether_t *
1441 eth_addr_resolve(hashether_t *tp) {
1443 hashmanuf_t *manuf_value;
1444 const guint8 *addr = tp->addr;
1446 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1447 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1448 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1455 /* Unknown name. Try looking for it in the well-known-address
1456 tables for well-known address ranges smaller than 2^24. */
1459 /* Only the topmost 5 bytes participate fully */
1460 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1461 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1462 name, addr[5] & (0xFF >> mask));
1463 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1470 /* Only the topmost 4 bytes participate fully */
1471 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1472 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1473 name, addr[4] & (0xFF >> mask), addr[5]);
1474 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1481 /* Only the topmost 3 bytes participate fully */
1482 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1483 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1484 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1485 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1490 /* Now try looking in the manufacturer table. */
1491 manuf_value = manuf_name_lookup(addr);
1492 if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1493 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1494 manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1495 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1499 /* Now try looking for it in the well-known-address
1500 tables for well-known address ranges larger than 2^24. */
1503 /* Only the topmost 2 bytes participate fully */
1504 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1505 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1506 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1508 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1515 /* Only the topmost byte participates fully */
1516 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1517 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1518 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1520 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1527 /* Not even the topmost byte participates fully */
1528 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1529 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1530 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1531 addr[3], addr[4], addr[5]);
1532 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1535 } while (--mask); /* Work down to the last bit */
1537 /* No match whatsoever. */
1538 set_address(ðer_addr, AT_ETHER, 6, addr);
1539 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1540 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1543 g_assert_not_reached();
1544 } /* eth_addr_resolve */
1546 static hashether_t *
1547 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1552 tp = wmem_new(wmem_epan_scope(), hashether_t);
1553 memcpy(tp->addr, addr, sizeof(tp->addr));
1554 tp->status = HASHETHER_STATUS_UNRESOLVED;
1555 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1556 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1558 tp->resolved_name[0] = '\0';
1561 eth_addr_resolve(tp);
1563 wmem_map_insert(eth_hashtable, tp->addr, tp);
1566 } /* eth_hash_new_entry */
1568 static hashether_t *
1569 add_eth_name(const guint8 *addr, const gchar *name)
1573 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1576 tp = eth_hash_new_entry(addr, FALSE);
1579 if (strcmp(tp->resolved_name, name) != 0) {
1580 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1581 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1582 new_resolved_objects = TRUE;
1586 } /* add_eth_name */
1588 static hashether_t *
1589 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1593 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1596 tp = eth_hash_new_entry(addr, resolve);
1598 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1599 eth_addr_resolve(tp); /* Found but needs to be resolved */
1605 } /* eth_name_lookup */
1610 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1613 * We allow three address separators (':', '-', and '.'),
1614 * as well as no separators
1618 guint32 a, a0, a1, a2, a3;
1619 gboolean found_single_number = FALSE;
1621 if ((cp = strchr(line, '#')))
1624 if ((cp = strtok(line, " \t\n")) == NULL)
1627 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1628 * fill a and found_single_number is TRUE,
1631 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1632 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1633 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1634 if (sscanf(cp, "%x", &a) == 1) {
1635 found_single_number = TRUE;
1644 if ((cp = strtok(NULL, " \t\n")) == NULL)
1647 if (found_single_number) {
1651 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1654 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1658 } /* parse_ipxnets_line */
1660 static FILE *ipxnet_p = NULL;
1663 set_ipxnetent(char *path)
1668 ipxnet_p = ws_fopen(path, "r");
1684 static ipxnet_t ipxnet;
1685 static int size = 0;
1686 static char *buf = NULL;
1688 if (ipxnet_p == NULL)
1691 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1692 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1699 } /* get_ipxnetent */
1702 get_ipxnetbyaddr(guint32 addr)
1706 set_ipxnetent(g_ipxnets_path);
1708 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1710 if (ipxnet == NULL) {
1713 set_ipxnetent(g_pipxnets_path);
1715 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1723 } /* get_ipxnetbyaddr */
1726 initialize_ipxnets(void)
1728 /* Compute the pathname of the ipxnets file.
1730 * XXX - is there a notion of an "ipxnets file" in any flavor of
1731 * UNIX, or with any add-on Netware package for UNIX? If not,
1732 * should the UNIX version of the ipxnets file be in the datafile
1733 * directory as well?
1735 if (g_ipxnets_path == NULL) {
1736 g_ipxnets_path = wmem_strdup_printf(wmem_epan_scope(), "%s" G_DIR_SEPARATOR_S "%s",
1737 get_systemfile_dir(), ENAME_IPXNETS);
1740 /* Set g_pipxnets_path here, but don't actually do anything
1741 * with it. It's used in get_ipxnetbyaddr().
1743 if (g_pipxnets_path == NULL)
1744 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1746 } /* initialize_ipxnets */
1749 ipx_name_lookup_cleanup(void)
1751 ipxnet_hash_table = NULL;
1752 g_free(g_pipxnets_path);
1753 g_pipxnets_path = NULL;
1757 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1762 tp = (hashipxnet_t *)wmem_map_lookup(ipxnet_hash_table, &addr);
1766 key = (int *)wmem_new(wmem_epan_scope(), int);
1768 tp = wmem_new(wmem_epan_scope(), hashipxnet_t);
1769 wmem_map_insert(ipxnet_hash_table, key, tp);
1771 return wmem_strdup(allocator, tp->name);
1774 /* fill in a new entry */
1778 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1780 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1783 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1786 return wmem_strdup(allocator, tp->name);
1788 } /* ipxnet_name_lookup */
1792 parse_vlan_line(char *line, vlan_t *vlan)
1797 if ((cp = strchr(line, '#')))
1800 if ((cp = strtok(line, " \t\n")) == NULL)
1803 if (sscanf(cp, "%" G_GUINT16_FORMAT, &id) == 1) {
1810 if ((cp = strtok(NULL, "\t\n")) == NULL)
1813 g_strlcpy(vlan->name, cp, MAXVLANNAMELEN);
1817 } /* parse_vlan_line */
1819 static FILE *vlan_p = NULL;
1822 set_vlanent(char *path)
1827 vlan_p = ws_fopen(path, "r");
1844 static int size = 0;
1845 static char *buf = NULL;
1850 while (fgetline(&buf, &size, vlan_p) >= 0) {
1851 if (parse_vlan_line(buf, &vlan) == 0) {
1861 get_vlannamebyid(guint16 id)
1865 set_vlanent(g_pvlan_path);
1867 while (((vlan = get_vlanent()) != NULL) && (id != vlan->id) ) ;
1876 } /* get_vlannamebyid */
1879 initialize_vlans(void)
1881 g_assert(vlan_hash_table == NULL);
1882 vlan_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
1884 /* Set g_pvlan_path here, but don't actually do anything
1885 * with it. It's used in get_vlannamebyid()
1887 if (g_pvlan_path == NULL)
1888 g_pvlan_path = get_persconffile_path(ENAME_VLANS, FALSE);
1890 } /* initialize_vlans */
1893 vlan_name_lookup_cleanup(void)
1895 vlan_hash_table = NULL;
1896 g_free(g_pvlan_path);
1897 g_pvlan_path = NULL;
1900 static const gchar *
1901 vlan_name_lookup(const guint id)
1906 tp = (hashvlan_t *)wmem_map_lookup(vlan_hash_table, &id);
1910 key = (int *)wmem_new(wmem_epan_scope(), int);
1912 tp = wmem_new(wmem_epan_scope(), hashvlan_t);
1913 wmem_map_insert(vlan_hash_table, key, tp);
1918 /* fill in a new entry */
1922 if ( (vlan = get_vlannamebyid(id)) == NULL) {
1924 g_snprintf(tp->name, MAXVLANNAMELEN, "<%u>", id);
1927 g_strlcpy(tp->name, vlan->name, MAXVLANNAMELEN);
1932 } /* vlan_name_lookup */
1936 read_hosts_file (const char *hostspath, gboolean store_entries)
1944 struct e_in6_addr ip6_addr;
1946 gboolean is_ipv6, entry_found = FALSE;
1949 * See the hosts(4) or hosts(5) man page for hosts file format
1950 * (not available on all systems).
1952 if ((hf = ws_fopen(hostspath, "r")) == NULL)
1955 while (fgetline(&line, &size, hf) >= 0) {
1956 if ((cp = strchr(line, '#')))
1959 if ((cp = strtok(line, " \t")) == NULL)
1960 continue; /* no tokens in the line */
1962 if (ws_inet_pton6(cp, &host_addr.ip6_addr)) {
1965 } else if (ws_inet_pton4(cp, &host_addr.ip4_addr)) {
1972 if ((cp = strtok(NULL, " \t")) == NULL)
1973 continue; /* no host name */
1976 if (store_entries) {
1978 add_ipv6_name(&host_addr.ip6_addr, cp);
1980 add_ipv4_name(host_addr.ip4_addr, cp);
1984 wmem_free(wmem_epan_scope(), line);
1987 return entry_found ? TRUE : FALSE;
1988 } /* read_hosts_file */
1991 add_hosts_file (const char *hosts_file)
1993 gboolean found = FALSE;
1999 if (!extra_hosts_files)
2000 extra_hosts_files = g_ptr_array_new();
2002 for (i = 0; i < extra_hosts_files->len; i++) {
2003 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2008 g_ptr_array_add(extra_hosts_files, wmem_strdup(wmem_epan_scope(), hosts_file));
2009 return read_hosts_file (hosts_file, FALSE);
2015 add_ip_name_from_string (const char *addr, const char *name)
2019 struct e_in6_addr ip6_addr;
2022 resolved_ipv4_t *resolved_ipv4_entry;
2023 resolved_ipv6_t *resolved_ipv6_entry;
2025 if (ws_inet_pton6(addr, &host_addr.ip6_addr)) {
2027 } else if (ws_inet_pton4(addr, &host_addr.ip4_addr)) {
2034 resolved_ipv6_entry = wmem_new(wmem_epan_scope(), resolved_ipv6_t);
2035 memcpy(&(resolved_ipv6_entry->ip6_addr), &host_addr.ip6_addr, 16);
2036 g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2037 wmem_list_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2039 resolved_ipv4_entry = wmem_new(wmem_epan_scope(), resolved_ipv4_t);
2040 resolved_ipv4_entry->host_addr = host_addr.ip4_addr;
2041 g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2042 wmem_list_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2046 } /* add_ip_name_from_string */
2049 * Add the resolved addresses that are in use to the list used to create the NRB
2052 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2054 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2055 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2057 if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2058 lists->ipv4_addr_list = g_list_prepend(lists->ipv4_addr_list, ipv4_hash_table_entry);
2064 * Add the resolved addresses that are in use to the list used to create the NRB
2068 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2070 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2071 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2073 if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2074 lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2080 get_addrinfo_list(void)
2082 if (ipv4_hash_table) {
2083 wmem_map_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2086 if (ipv6_hash_table) {
2087 wmem_map_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2090 return &addrinfo_lists;
2093 /* Read in a list of subnet definition - name pairs.
2094 * <line> = <comment> | <entry> | <whitespace>
2095 * <comment> = <whitespace>#<any>
2096 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2097 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2098 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2099 * <subnet_mask_length> is a decimal 1-31
2100 * <subnet_name> is a string containing no whitespace.
2101 * <whitespace> = (space | tab)+
2102 * Any malformed entries are ignored.
2103 * Any trailing data after the subnet_name is ignored.
2108 read_subnets_file (const char *subnetspath)
2114 guint32 host_addr; /* IPv4 ONLY */
2117 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2120 while (fgetline(&line, &size, hf) >= 0) {
2121 if ((cp = strchr(line, '#')))
2124 if ((cp = strtok(line, " \t")) == NULL)
2125 continue; /* no tokens in the line */
2128 /* Expected format is <IP4 address>/<subnet length> */
2129 cp2 = strchr(cp, '/');
2134 *cp2 = '\0'; /* Cut token */
2137 /* Check if this is a valid IPv4 address */
2138 if (!str_to_ip(cp, &host_addr)) {
2142 if (!ws_strtou8(cp2, NULL, &mask_length) || mask_length == 0 || mask_length > 32) {
2143 continue; /* invalid mask length */
2146 if ((cp = strtok(NULL, " \t")) == NULL)
2147 continue; /* no subnet name */
2149 subnet_entry_set(host_addr, mask_length, cp);
2151 wmem_free(wmem_epan_scope(), line);
2155 } /* read_subnets_file */
2157 static subnet_entry_t
2158 subnet_lookup(const guint32 addr)
2160 subnet_entry_t subnet_entry;
2163 /* Search mask lengths linearly, longest first */
2165 i = SUBNETLENGTHSIZE;
2166 while(have_subnet_entry && i > 0) {
2167 guint32 masked_addr;
2168 subnet_length_entry_t* length_entry;
2170 /* Note that we run from 31 (length 32) to 0 (length 1) */
2172 g_assert(i < SUBNETLENGTHSIZE);
2175 length_entry = &subnet_length_entries[i];
2177 if (NULL != length_entry->subnet_addresses) {
2178 sub_net_hashipv4_t * tp;
2181 masked_addr = addr & length_entry->mask;
2182 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2184 tp = length_entry->subnet_addresses[hash_idx];
2185 while(tp != NULL && tp->addr != masked_addr) {
2190 subnet_entry.mask = length_entry->mask;
2191 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2192 subnet_entry.name = tp->name;
2193 return subnet_entry;
2198 subnet_entry.mask = 0;
2199 subnet_entry.mask_length = 0;
2200 subnet_entry.name = NULL;
2202 return subnet_entry;
2205 /* Add a subnet-definition - name pair to the set.
2206 * The definition is taken by masking the address passed in with the mask of the
2210 subnet_entry_set(guint32 subnet_addr, const guint8 mask_length, const gchar* name)
2212 subnet_length_entry_t* entry;
2213 sub_net_hashipv4_t * tp;
2216 g_assert(mask_length > 0 && mask_length <= 32);
2218 entry = &subnet_length_entries[mask_length - 1];
2220 subnet_addr &= entry->mask;
2222 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2224 if (NULL == entry->subnet_addresses) {
2225 entry->subnet_addresses = (sub_net_hashipv4_t**)wmem_alloc0(wmem_epan_scope(), sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2228 if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2229 sub_net_hashipv4_t * new_tp;
2232 if (tp->addr == subnet_addr) {
2233 return; /* XXX provide warning that an address was repeated? */
2239 new_tp = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2243 tp = entry->subnet_addresses[hash_idx] = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2247 tp->addr = subnet_addr;
2248 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2249 have_subnet_entry = TRUE;
2253 subnet_name_lookup_init(void)
2258 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2259 guint32 length = i + 1;
2261 subnet_length_entries[i].subnet_addresses = NULL;
2262 subnet_length_entries[i].mask_length = length;
2263 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2266 /* Check profile directory before personal configuration */
2267 subnetspath = get_persconffile_path(ENAME_SUBNETS, TRUE);
2268 if (!read_subnets_file(subnetspath)) {
2269 if (errno != ENOENT) {
2270 report_open_failure(subnetspath, errno, FALSE);
2273 g_free(subnetspath);
2274 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2275 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2276 report_open_failure(subnetspath, errno, FALSE);
2279 g_free(subnetspath);
2282 * Load the global subnets file, if we have one.
2284 subnetspath = get_datafile_path(ENAME_SUBNETS);
2285 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2286 report_open_failure(subnetspath, errno, FALSE);
2288 g_free(subnetspath);
2291 /* SS7 PC Name Resolution Portion */
2292 static hashss7pc_t *
2293 new_ss7pc(const guint8 ni, const guint32 pc)
2295 hashss7pc_t *tp = wmem_new(wmem_epan_scope(), hashss7pc_t);
2296 tp->id = (ni<<24) + (pc&0xffffff);
2297 tp->pc_addr[0] = '\0';
2303 static hashss7pc_t *
2304 host_lookup_ss7pc(const guint8 ni, const guint32 pc)
2306 hashss7pc_t * volatile tp;
2309 id = (ni<<24) + (pc&0xffffff);
2311 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2313 tp = new_ss7pc(ni, pc);
2314 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2320 void fill_unresolved_ss7pc(const gchar * pc_addr, const guint8 ni, const guint32 pc)
2322 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2324 g_strlcpy(tp->pc_addr, pc_addr, MAXNAMELEN);
2328 get_hostname_ss7pc(const guint8 ni, const guint32 pc)
2330 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2332 /* never resolved yet*/
2333 if (tp->pc_addr[0] == '\0')
2336 /* Don't have name in file */
2337 if (tp->name[0] == '\0')
2340 if (!gbl_resolv_flags.ss7pc_name)
2347 add_ss7pc_name(const guint8 ni, guint32 pc, const gchar *name)
2352 if (!name || name[0] == '\0')
2355 id = (ni<<24) + (pc&0xffffff);
2356 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2358 tp = new_ss7pc(ni, pc);
2359 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2362 if (g_ascii_strcasecmp(tp->name, name)) {
2363 g_strlcpy(tp->name, name, MAXNAMELEN);
2368 read_ss7pcs_file(const char *ss7pcspath)
2376 gboolean entry_found = FALSE;
2379 * File format is Network Indicator (decimal)<dash>Point Code (Decimal)<tab/space>Hostname
2381 if ((hf = ws_fopen(ss7pcspath, "r")) == NULL)
2384 while (fgetline(&line, &size, hf) >= 0) {
2385 if ((cp = strchr(line, '#')))
2388 if ((cp = strtok(line, "-")) == NULL)
2389 continue; /*no ni-pc separator*/
2390 if (!ws_strtou8(cp, NULL, &ni))
2395 if ((cp = strtok(NULL, " \t")) == NULL)
2396 continue; /* no tokens for pc and name */
2397 if (!ws_strtou32(cp, NULL, &pc))
2402 if ((cp = strtok(NULL, " \t")) == NULL)
2403 continue; /* no host name */
2406 add_ss7pc_name(ni, pc, cp);
2408 wmem_free(wmem_epan_scope(), line);
2411 return entry_found ? TRUE : FALSE;
2415 ss7pc_name_lookup_init(void)
2419 g_assert(ss7pc_hash_table == NULL);
2421 ss7pc_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2424 * Load the user's ss7pcs file
2426 ss7pcspath = get_persconffile_path(ENAME_SS7PCS, TRUE);
2427 if (!read_ss7pcs_file(ss7pcspath) && errno != ENOENT) {
2428 report_open_failure(ss7pcspath, errno, FALSE);
2433 /* SS7PC Name Resolution End*/
2437 * External Functions
2441 addr_resolve_pref_init(module_t *nameres)
2443 prefs_register_bool_preference(nameres, "mac_name",
2444 "Resolve MAC addresses",
2445 "Resolve Ethernet MAC addresses to host names from the preferences"
2446 " or system's Ethers file, or to a manufacturer based name.",
2447 &gbl_resolv_flags.mac_name);
2449 prefs_register_bool_preference(nameres, "transport_name",
2450 "Resolve transport names",
2451 "Resolve TCP/UDP ports into service names",
2452 &gbl_resolv_flags.transport_name);
2454 prefs_register_bool_preference(nameres, "network_name",
2455 "Resolve network (IP) addresses",
2456 "Resolve IPv4, IPv6, and IPX addresses into host names."
2457 " The next set of check boxes determines how name resolution should be performed."
2458 " If no other options are checked name resolution is made from Wireshark's host file,"
2459 " capture file name resolution blocks and DNS packets in the capture.",
2460 &gbl_resolv_flags.network_name);
2462 prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
2463 "Use captured DNS packet data for address resolution",
2464 "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
2465 &gbl_resolv_flags.dns_pkt_addr_resolution);
2468 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2469 "Use an external network name resolver",
2470 "Use your system's configured name resolver"
2471 " (usually DNS) to resolve network names."
2472 " Only applies when network name resolution"
2474 &gbl_resolv_flags.use_external_net_name_resolver);
2476 prefs_register_obsolete_preference(nameres, "concurrent_dns");
2478 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2479 "Maximum concurrent requests",
2480 "The maximum number of DNS requests that may"
2481 " be active at any time. A large value (many"
2482 " thousands) might overload the network or make"
2483 " your DNS server behave badly.",
2485 &name_resolve_concurrency);
2487 prefs_register_static_text_preference(nameres, "use_external_name_resolver",
2488 "Use an external network name resolver: N/A",
2489 "Support for using a concurrent external name resolver was not"
2490 " compiled into this version of Wireshark");
2493 prefs_register_bool_preference(nameres, "hosts_file_handling",
2494 "Only use the profile \"hosts\" file",
2495 "By default \"hosts\" files will be loaded from multiple sources."
2496 " Checking this box only loads the \"hosts\" in the current profile.",
2497 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2499 prefs_register_bool_preference(nameres, "vlan_name",
2501 "Resolve VLAN IDs to network names from the preferences \"vlans\" file."
2502 " Format of the file is: \"ID<Tab>Name\"."
2503 " One line per VLAN, e.g.: 1 Management",
2504 &gbl_resolv_flags.vlan_name);
2506 prefs_register_bool_preference(nameres, "ss7_pc_name",
2508 "Resolve SS7 Point Codes to node names from the profiles \"ss7pcs\" file."
2509 " Format of the file is: \"Network_Indicator<Dash>PC_Decimal<Tab>Name\"."
2510 " One line per Point Code, e.g.: 2-1234 MyPointCode1",
2511 &gbl_resolv_flags.ss7pc_name);
2516 disable_name_resolution(void) {
2517 gbl_resolv_flags.mac_name = FALSE;
2518 gbl_resolv_flags.network_name = FALSE;
2519 gbl_resolv_flags.transport_name = FALSE;
2520 gbl_resolv_flags.dns_pkt_addr_resolution = FALSE;
2521 gbl_resolv_flags.use_external_net_name_resolver = FALSE;
2522 gbl_resolv_flags.vlan_name = FALSE;
2523 gbl_resolv_flags.ss7pc_name = FALSE;
2528 host_name_lookup_process(void) {
2529 async_dns_queue_msg_t *caqm;
2530 struct timeval tv = { 0, 0 };
2533 gboolean nro = new_resolved_objects;
2534 wmem_list_frame_t* head;
2536 new_resolved_objects = FALSE;
2538 if (!async_dns_initialized)
2539 /* c-ares not initialized. Bail out and cancel timers. */
2542 head = wmem_list_head(async_dns_queue_head);
2544 while (head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2545 caqm = (async_dns_queue_msg_t *)wmem_list_frame_data(head);
2546 wmem_list_remove_frame(async_dns_queue_head, head);
2547 if (caqm->family == AF_INET) {
2548 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2549 c_ares_ghba_cb, caqm);
2550 async_dns_in_flight++;
2551 } else if (caqm->family == AF_INET6) {
2552 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2553 AF_INET6, c_ares_ghba_cb, caqm);
2554 async_dns_in_flight++;
2557 head = wmem_list_head(async_dns_queue_head);
2562 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2564 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2565 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2568 ares_process(ghba_chan, &rfds, &wfds);
2571 /* Any new entries? */
2576 _host_name_lookup_cleanup(void) {
2577 async_dns_queue_head = NULL;
2579 if (async_dns_initialized) {
2580 ares_destroy(ghba_chan);
2581 ares_destroy(ghbn_chan);
2583 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2584 ares_library_cleanup();
2586 async_dns_initialized = FALSE;
2592 host_name_lookup_process(void) {
2593 gboolean nro = new_resolved_objects;
2595 new_resolved_objects = FALSE;
2601 _host_name_lookup_cleanup(void) {
2604 #endif /* HAVE_C_ARES */
2607 get_hostname(const guint addr)
2609 /* XXX why do we call this if we're not resolving? To create hash entries?
2612 hashipv4_t *tp = host_lookup(addr);
2614 if (!gbl_resolv_flags.network_name)
2617 tp->flags |= RESOLVED_ADDRESS_USED;
2622 /* -------------------------- */
2625 get_hostname6(const struct e_in6_addr *addr)
2627 /* XXX why do we call this if we're not resolving? To create hash entries?
2630 hashipv6_t *tp = host_lookup6(addr);
2632 if (!gbl_resolv_flags.network_name)
2635 tp->flags |= RESOLVED_ADDRESS_USED;
2640 /* -------------------------- */
2642 add_ipv4_name(const guint addr, const gchar *name)
2647 * Don't add zero-length names; apparently, some resolvers will return
2648 * them if they get them from DNS.
2650 if (!name || name[0] == '\0')
2653 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2655 tp = new_ipv4(addr);
2656 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2659 if (g_ascii_strcasecmp(tp->name, name)) {
2660 g_strlcpy(tp->name, name, MAXNAMELEN);
2661 new_resolved_objects = TRUE;
2663 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2664 } /* add_ipv4_name */
2666 /* -------------------------- */
2668 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2673 * Don't add zero-length names; apparently, some resolvers will return
2674 * them if they get them from DNS.
2676 if (!name || name[0] == '\0')
2679 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addrp);
2681 struct e_in6_addr *addr_key;
2683 addr_key = wmem_new(wmem_epan_scope(), struct e_in6_addr);
2684 tp = new_ipv6(addrp);
2685 memcpy(addr_key, addrp, 16);
2686 wmem_map_insert(ipv6_hash_table, addr_key, tp);
2689 if (g_ascii_strcasecmp(tp->name, name)) {
2690 g_strlcpy(tp->name, name, MAXNAMELEN);
2691 new_resolved_objects = TRUE;
2693 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2694 } /* add_ipv6_name */
2697 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2699 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2701 add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2705 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2707 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2709 add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2713 add_manually_resolved(void)
2715 if (manually_resolved_ipv4_list) {
2716 wmem_list_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2719 if (manually_resolved_ipv6_list) {
2720 wmem_list_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2725 host_name_lookup_init(void)
2730 g_assert(ipxnet_hash_table == NULL);
2731 ipxnet_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
2733 g_assert(ipv4_hash_table == NULL);
2734 ipv4_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2736 g_assert(ipv6_hash_table == NULL);
2737 ipv6_hash_table = wmem_map_new(wmem_epan_scope(), ipv6_oat_hash, ipv6_equal);
2740 g_assert(async_dns_queue_head == NULL);
2741 async_dns_queue_head = wmem_list_new(wmem_epan_scope());
2744 if (manually_resolved_ipv4_list == NULL)
2745 manually_resolved_ipv4_list = wmem_list_new(wmem_epan_scope());
2747 if (manually_resolved_ipv6_list == NULL)
2748 manually_resolved_ipv6_list = wmem_list_new(wmem_epan_scope());
2751 * Load the global hosts file, if we have one.
2753 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2754 hostspath = get_datafile_path(ENAME_HOSTS);
2755 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2756 report_open_failure(hostspath, errno, FALSE);
2761 * Load the user's hosts file no matter what, if they have one.
2763 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2764 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2765 report_open_failure(hostspath, errno, FALSE);
2769 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2770 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2772 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2773 async_dns_initialized = TRUE;
2775 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2779 #endif /* HAVE_C_ARES */
2781 if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
2782 for (i = 0; i < extra_hosts_files->len; i++) {
2783 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
2787 subnet_name_lookup_init();
2789 add_manually_resolved();
2791 ss7pc_name_lookup_init();
2795 host_name_lookup_cleanup(void)
2798 sub_net_hashipv4_t *entry, *next_entry;
2800 _host_name_lookup_cleanup();
2802 ipxnet_hash_table = NULL;
2803 ipv4_hash_table = NULL;
2804 ipv6_hash_table = NULL;
2805 ss7pc_hash_table = NULL;
2807 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2808 if (subnet_length_entries[i].subnet_addresses != NULL) {
2809 for (j = 0; j < HASHHOSTSIZE; j++) {
2810 for (entry = subnet_length_entries[i].subnet_addresses[j];
2811 entry != NULL; entry = next_entry) {
2812 next_entry = entry->next;
2813 wmem_free(wmem_epan_scope(), entry);
2816 wmem_free(wmem_epan_scope(), subnet_length_entries[i].subnet_addresses);
2817 subnet_length_entries[i].subnet_addresses = NULL;
2821 have_subnet_entry = FALSE;
2822 new_resolved_objects = FALSE;
2826 manually_resolve_cleanup(void)
2828 wmem_destroy_list(manually_resolved_ipv4_list);
2829 manually_resolved_ipv4_list = NULL;
2830 wmem_destroy_list(manually_resolved_ipv6_list);
2831 manually_resolved_ipv6_list = NULL;
2835 udp_port_to_display(wmem_allocator_t *allocator, guint port)
2838 if (!gbl_resolv_flags.transport_name) {
2839 return wmem_utoa(allocator, port);
2842 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
2844 } /* udp_port_to_display */
2847 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
2850 if (!gbl_resolv_flags.transport_name) {
2851 return wmem_utoa(allocator, port);
2854 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
2856 } /* dccp_port_to_display */
2859 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
2862 if (!gbl_resolv_flags.transport_name) {
2863 return wmem_utoa(allocator, port);
2866 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
2868 } /* tcp_port_to_display */
2871 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
2874 if (!gbl_resolv_flags.transport_name) {
2875 return wmem_utoa(allocator, port);
2878 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
2880 } /* sctp_port_to_display */
2883 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
2885 const gchar *port_str;
2887 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2888 /* No name resolution support, just return port string */
2889 return wmem_strdup_printf(scope, "%u", port);
2891 port_str = serv_name_lookup(proto, port);
2893 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
2897 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
2899 const gchar *port_str;
2901 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
2902 /* No name resolution support, just return port string */
2903 return g_snprintf(buf, buf_size, "%u", port);
2905 port_str = serv_name_lookup(proto, port);
2907 return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
2911 get_ether_name(const guint8 *addr)
2914 gboolean resolve = gbl_resolv_flags.mac_name;
2916 tp = eth_name_lookup(addr, resolve);
2918 return resolve ? tp->resolved_name : tp->hexaddr;
2920 } /* get_ether_name */
2923 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
2925 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
2928 /* Look for a (non-dummy) ether name in the hash, and return it if found.
2929 * If it's not found, simply return NULL.
2932 get_ether_name_if_known(const guint8 *addr)
2936 /* Initialize ether structs if we're the first
2937 * ether-related function called */
2938 if (!gbl_resolv_flags.mac_name)
2941 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
2942 tp = eth_name_lookup(addr, TRUE);
2943 g_assert(tp != NULL);
2945 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
2946 /* Name is from an ethers file */
2947 return tp->resolved_name;
2950 /* Name was created */
2956 add_ether_byip(const guint ip, const guint8 *eth)
2960 /* first check that IP address can be resolved */
2961 if (!gbl_resolv_flags.network_name)
2964 tp = host_lookup(ip);
2967 * Was this IP address resolved to a host name?
2969 if (tp->flags & NAME_RESOLVED) {
2971 * Yes, so add an entry in the ethers hashtable resolving
2972 * the MAC address to that name.
2974 add_eth_name(eth, tp->name);
2977 } /* add_ether_byip */
2980 ipxnet_to_str_punct(wmem_allocator_t *allocator, const guint32 ad, const char punct)
2982 gchar *buf = (gchar *)wmem_alloc(allocator, 12);
2984 *dword_to_hex_punct(buf, ad, punct) = '\0';
2989 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
2992 if (!gbl_resolv_flags.network_name) {
2993 return ipxnet_to_str_punct(allocator, addr, '\0');
2996 return ipxnet_name_lookup(allocator, addr);
2998 } /* get_ipxnet_name */
3001 get_vlan_name(wmem_allocator_t *allocator, const guint16 id)
3004 if (!gbl_resolv_flags.vlan_name) {
3008 return wmem_strdup(allocator, vlan_name_lookup(id));
3010 } /* get_vlan_name */
3013 get_manuf_name(const guint8 *addr)
3015 hashmanuf_t *manuf_value;
3017 manuf_value = manuf_name_lookup(addr);
3018 if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3019 return manuf_value->resolved_name;
3021 return manuf_value->hexaddr;
3023 } /* get_manuf_name */
3026 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3028 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3032 get_manuf_name_if_known(const guint8 *addr)
3034 hashmanuf_t *manuf_value;
3038 /* manuf needs only the 3 most significant octets of the ethernet address */
3039 manuf_key = addr[0];
3040 manuf_key = manuf_key<<8;
3042 manuf_key = manuf_key | oct;
3043 manuf_key = manuf_key<<8;
3045 manuf_key = manuf_key | oct;
3047 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3048 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3052 return manuf_value->resolved_longname;
3054 } /* get_manuf_name_if_known */
3057 uint_get_manuf_name_if_known(const guint manuf_key)
3059 hashmanuf_t *manuf_value;
3061 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3062 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3066 return manuf_value->resolved_longname;
3070 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3072 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3075 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3077 return manuf->resolved_longname;
3081 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3083 guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3084 hashmanuf_t *manuf_value;
3087 /* Copy and convert the address to network byte order. */
3088 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3090 manuf_value = manuf_name_lookup(addr);
3091 if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3092 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]);
3094 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]);
3097 wmem_free(NULL, addr);
3099 } /* eui64_to_display */
3102 #define GHI_TIMEOUT (250 * 1000)
3104 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3106 * XXX - If we wanted to be really fancy we could cache results here and
3107 * look them up in get_host_ipaddr* below.
3109 async_hostent_t *ahp = (async_hostent_t *)arg;
3110 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3111 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3112 ahp->copied = hp->h_length;
3115 #endif /* HAVE_C_ARES */
3117 /* Translate a string, assumed either to be a dotted-quad IP address or
3118 * a host name, to a numeric IP address. Return TRUE if we succeed and
3119 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3120 * Used more in the dfilter parser rather than in packet dissectors */
3122 get_host_ipaddr(const char *host, guint32 *addrp)
3126 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3129 async_hostent_t ahe;
3132 if (!ws_inet_pton4(host, &ipaddr)) {
3134 /* It's not a valid dotted-quad IP address; is it a valid
3138 /* If we're not allowed to do name resolution, don't do name
3141 if (!gbl_resolv_flags.network_name ||
3142 !gbl_resolv_flags.use_external_net_name_resolver) {
3147 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3150 ahe.addr_size = (int) sizeof (struct in_addr);
3153 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3156 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3158 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3159 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3160 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3163 ares_process(ghbn_chan, &rfds, &wfds);
3165 ares_cancel(ghbn_chan);
3166 if (ahe.addr_size == ahe.copied) {
3172 /* Does the string really contain dotted-quad IP?
3173 * Check against inet_atons that accept strings such as
3174 * "130.230" as valid addresses and try to convert them
3175 * to some form of a classful (host.net) notation.
3177 unsigned int a0, a1, a2, a3;
3178 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3187 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3188 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3189 * return FALSE if we fail.
3192 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3195 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3198 async_hostent_t ahe;
3199 #endif /* HAVE_C_ARES */
3201 if (str_to_ip6(host, addrp))
3204 /* It's not a valid dotted-quad IP address; is it a valid
3208 /* If we're not allowed to do name resolution, don't do name
3211 if (!gbl_resolv_flags.network_name ||
3212 !gbl_resolv_flags.use_external_net_name_resolver) {
3218 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3221 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3224 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3227 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3229 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3230 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3231 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3234 ares_process(ghbn_chan, &rfds, &wfds);
3236 ares_cancel(ghbn_chan);
3237 if (ahe.addr_size == ahe.copied) {
3246 get_manuf_hashtable(void)
3248 return manuf_hashtable;
3252 get_wka_hashtable(void)
3254 return wka_hashtable;
3258 get_eth_hashtable(void)
3260 return eth_hashtable;
3264 get_serv_port_hashtable(void)
3266 return serv_port_hashtable;
3270 get_ipxnet_hash_table(void)
3272 return ipxnet_hash_table;
3276 get_vlan_hash_table(void)
3278 return vlan_hash_table;
3282 get_ipv4_hash_table(void)
3284 return ipv4_hash_table;
3288 get_ipv6_hash_table(void)
3290 return ipv6_hash_table;
3292 /* Initialize all the address resolution subsystems in this file */
3294 addr_resolv_init(void)
3296 initialize_services();
3297 initialize_ethers();
3298 initialize_ipxnets();
3300 initialize_enterprises();
3301 /* host name initialization is done on a per-capture-file basis */
3302 /*host_name_lookup_init();*/
3305 /* Clean up all the address resolution subsystems in this file */
3307 addr_resolv_cleanup(void)
3309 vlan_name_lookup_cleanup();
3310 service_name_lookup_cleanup();
3312 ipx_name_lookup_cleanup();
3313 enterprises_cleanup();
3314 /* host name initialization is done on a per-capture-file basis */
3315 /*host_name_lookup_cleanup();*/
3319 str_to_ip(const char *str, void *dst)
3321 return ws_inet_pton4(str, (guint32 *)dst);
3325 str_to_ip6(const char *str, void *dst)
3327 return ws_inet_pton6(str, (struct e_in6_addr *)dst);
3331 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3336 * indent-tabs-mode: nil
3339 * vi: set shiftwidth=4 tabstop=8 expandtab:
3340 * :indentSize=4:tabSize=8:noTabs=true: