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 * SPDX-License-Identifier: GPL-2.0-or-later
23 #include <wsutil/strtoi.h>
26 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
27 * are most likely to take a long time, given the way address-to-name
28 * lookups are done over NBNS).
30 * macOS does have SIGALRM, but if you longjmp() out of a name resolution
31 * call in a signal handler, you might crash, because the state of the
32 * resolution code that sends messages to lookupd might be inconsistent
33 * if you jump out of it in middle of a call.
35 * There's no guarantee that longjmp()ing out of name resolution calls
36 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
37 * code in tcpdump, to avoid those sorts of problems, and that was
38 * picked up by tcpdump.org tcpdump.
40 * So, for now, we do not use alarm() and SIGALRM to time out host name
41 * lookups. If we get a lot of complaints about lookups taking a long time,
42 * we can reconsider that decision. (Note that tcpdump originally added
43 * such a timeout mechanism that for the benefit of systems using NIS to
44 * look up host names; that might now be fixed in NIS implementations, for
45 * those sites still using NIS rather than DNS for that.... tcpdump no
46 * longer does that, for the same reasons that we don't.)
48 * If we're using an asynchronous DNS resolver, that shouldn't be an issue.
49 * If we're using a synchronous name lookup mechanism (which we'd do mainly
50 * to support resolving addresses and host names using more mechanisms than
51 * just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
52 * a separate thread, making it, in effect, asynchronous.
55 #ifdef HAVE_NETINET_IN_H
56 # include <netinet/in.h>
63 #ifdef HAVE_SYS_SOCKET_H
64 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
68 #include <winsock2.h> /* needed to define AF_ values on Windows */
74 # define socklen_t unsigned int
77 # include <ares_version.h>
78 #endif /* HAVE_C_ARES */
83 #include "addr_and_mask.h"
85 #include "addr_resolv.h"
86 #include "wsutil/filesystem.h"
88 #include <wsutil/report_message.h>
89 #include <wsutil/file_util.h>
90 #include <wsutil/pint.h>
91 #include <wsutil/inet_addr.h>
93 #include <epan/strutil.h>
94 #include <epan/to_str-int.h>
95 #include <epan/maxmind_db.h>
96 #include <epan/prefs.h>
98 #define ENAME_HOSTS "hosts"
99 #define ENAME_SUBNETS "subnets"
100 #define ENAME_ETHERS "ethers"
101 #define ENAME_IPXNETS "ipxnets"
102 #define ENAME_MANUF "manuf"
103 #define ENAME_WKA "wka"
104 #define ENAME_SERVICES "services"
105 #define ENAME_VLANS "vlans"
106 #define ENAME_SS7PCS "ss7pcs"
107 #define ENAME_ENTERPRISES "enterprises.tsv"
109 #define HASHETHSIZE 2048
110 #define HASHHOSTSIZE 2048
111 #define HASHIPXNETSIZE 256
112 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
114 /* hash table used for IPv4 lookup */
116 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
119 typedef struct sub_net_hashipv4 {
121 /* XXX: No longer needed?*/
122 guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
123 struct sub_net_hashipv4 *next;
124 gchar name[MAXNAMELEN];
125 } sub_net_hashipv4_t;
127 /* Array of entries of subnets of different lengths */
129 gsize mask_length; /*1-32*/
130 guint32 mask; /* e.g. 255.255.255.*/
131 sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
132 } subnet_length_entry_t;
135 /* hash table used for IPX network lookup */
137 /* XXX - check goodness of hash function */
139 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
141 typedef struct hashipxnet {
143 struct hashipxnet *next;
144 gchar name[MAXNAMELEN];
147 typedef struct hashvlan {
149 /* struct hashvlan *next; */
150 gchar name[MAXVLANNAMELEN];
153 typedef struct ss7pc {
154 guint32 id; /* 1st byte NI, 3 following bytes: Point Code */
155 gchar pc_addr[MAXNAMELEN];
156 gchar name[MAXNAMELEN];
159 /* hash tables used for ethernet and manufacturer lookup */
160 #define HASHETHER_STATUS_UNRESOLVED 1
161 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
162 #define HASHETHER_STATUS_RESOLVED_NAME 3
165 guint status; /* (See above) */
168 char resolved_name[MAXNAMELEN];
172 guint status; /* (See above) */
175 char resolved_name[MAXNAMELEN];
176 char resolved_longname[MAXNAMELEN];
179 /* internal ethernet type */
180 typedef struct _ether
183 char name[MAXNAMELEN];
184 char longname[MAXNAMELEN];
187 /* internal ipxnet type */
188 typedef struct _ipxnet
191 char name[MAXNAMELEN];
194 /* internal vlan type */
198 char name[MAXVLANNAMELEN];
201 static wmem_map_t *ipxnet_hash_table = NULL;
202 static wmem_map_t *ipv4_hash_table = NULL;
203 static wmem_map_t *ipv6_hash_table = NULL;
204 static wmem_map_t *vlan_hash_table = NULL;
205 static wmem_map_t *ss7pc_hash_table = NULL;
207 static wmem_list_t *manually_resolved_ipv4_list = NULL;
208 static wmem_list_t *manually_resolved_ipv6_list = NULL;
210 typedef struct _resolved_ipv4
213 char name[MAXNAMELEN];
216 typedef struct _resolved_ipv6
218 ws_in6_addr ip6_addr;
219 char name[MAXNAMELEN];
222 static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
224 struct cb_serv_data {
229 static wmem_map_t *manuf_hashtable = NULL;
230 static wmem_map_t *wka_hashtable = NULL;
231 static wmem_map_t *eth_hashtable = NULL;
232 static wmem_map_t *serv_port_hashtable = NULL;
233 static GHashTable *enterprises_hashtable = NULL;
235 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
236 static gboolean have_subnet_entry = FALSE;
238 static gboolean new_resolved_objects = FALSE;
240 static GPtrArray* extra_hosts_files = NULL;
242 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
243 static void add_serv_port_cb(const guint32 port, gpointer ptr);
246 /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
250 ipv6_oat_hash(gconstpointer key)
253 const unsigned char *p = (const unsigned char *)key;
257 for ( i = 0; i < len; i++ ) {
271 ipv6_equal(gconstpointer v1, gconstpointer v2)
274 if (memcmp(v1, v2, sizeof (ws_in6_addr)) == 0) {
282 * Flag controlling what names to resolve.
284 e_addr_resolve gbl_resolv_flags = {
286 FALSE, /* network_name */
287 FALSE, /* transport_name */
288 TRUE, /* dns_pkt_addr_resolution */
289 TRUE, /* use_external_net_name_resolver */
290 FALSE, /* load_hosts_file_from_profile_only */
291 FALSE, /* vlan_name */
292 FALSE /* ss7 point code names */
295 static guint name_resolve_concurrency = 500;
296 static gboolean resolve_synchronously = FALSE;
300 * Global variables (can be changed in GUI sections)
301 * XXX - they could be changed in GUI code, but there's currently no
302 * GUI code to change them.
305 gchar *g_ethers_path = NULL; /* global ethers file */
306 gchar *g_pethers_path = NULL; /* personal ethers file */
307 gchar *g_wka_path = NULL; /* global well-known-addresses file */
308 gchar *g_manuf_path = NULL; /* global manuf file */
309 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
310 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
311 gchar *g_services_path = NULL; /* global services file */
312 gchar *g_pservices_path = NULL; /* personal services file */
313 gchar *g_pvlan_path = NULL; /* personal vlans file */
314 gchar *g_ss7pcs_path = NULL; /* personal ss7pcs file */
315 gchar *g_enterprises_path = NULL; /* global enterprises file */
316 gchar *g_penterprises_path = NULL; /* personal enterprises file */
317 /* first resolving call */
322 * Submitted asynchronous queries trigger a callback (c_ares_ghba_cb()).
323 * Queries are added to c_ares_queue_head. During processing, queries are
324 * popped off the front of c_ares_queue_head and submitted using
325 * ares_gethostbyaddr().
326 * The callback processes the response, then frees the request.
328 typedef struct _async_dns_queue_msg
335 } async_dns_queue_msg_t;
337 typedef struct _async_hostent {
344 * Submitted synchronous queries trigger a callback (c_ares_ghba_sync_cb()).
345 * The callback processes the response, sets completed to TRUE if
346 * completed is non-NULL, then frees the request.
348 typedef struct _sync_dns_data
358 static ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
359 static ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
361 static gboolean async_dns_initialized = FALSE;
362 static guint async_dns_in_flight = 0;
363 static wmem_list_t *async_dns_queue_head = NULL;
366 c_ares_ghba_sync_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
367 sync_dns_data_t *sdd = (sync_dns_data_t *)arg;
370 if (status == ARES_SUCCESS) {
371 for (p = he->h_addr_list; *p != NULL; p++) {
372 switch(sdd->family) {
374 add_ipv4_name(sdd->addr.ip4, he->h_name);
377 add_ipv6_name(&sdd->addr.ip6, he->h_name);
380 /* Throw an exception? */
388 * Let our caller know that this is complete.
390 *sdd->completed = TRUE;
393 * Free the structure for this call.
399 wait_for_sync_resolv(gboolean *completed) {
404 while (!*completed) {
406 * Not yet resolved; wait for something to show up on the
407 * address-to-name C-ARES channel.
409 * To quote the source code for ares_timeout() as of C-ARES
410 * 1.12.0, "WARNING: Beware that this is linear in the number
411 * of outstanding requests! You are probably far better off
412 * just calling ares_process() once per second, rather than
413 * calling ares_timeout() to figure out when to next call
414 * ares_process().", although we should have only one request
417 * And, yes, we have to reset it each time, as select(), in
418 * some OSes modifies the timeout to reflect the time remaining
419 * (e.g., Linux) and select() in other OSes doesn't (most if not
420 * all other UN*Xes, Windows?), so we can't rely on *either*
428 nfds = ares_fds(ghba_chan, &rfds, &wfds);
430 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
431 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
434 ares_process(ghba_chan, &rfds, &wfds);
440 sync_lookup_ip4(const guint32 addr)
442 gboolean completed = FALSE;
443 sync_dns_data_t *sdd;
445 if (!async_dns_initialized) {
447 * c-ares not initialized. Bail out.
455 sdd = g_new(sync_dns_data_t, 1);
456 sdd->family = AF_INET;
457 sdd->addr.ip4 = addr;
458 sdd->completed = &completed;
459 ares_gethostbyaddr(ghba_chan, &addr, sizeof(guint32), AF_INET,
460 c_ares_ghba_sync_cb, sdd);
463 * Now wait for it to finish.
465 wait_for_sync_resolv(&completed);
469 sync_lookup_ip6(const ws_in6_addr *addr)
471 gboolean completed = FALSE;
472 sync_dns_data_t *sdd;
474 if (!async_dns_initialized) {
476 * c-ares not initialized. Bail out.
484 sdd = g_new(sync_dns_data_t, 1);
485 sdd->family = AF_INET6;
486 memcpy(&sdd->addr.ip6, addr, sizeof(sdd->addr.ip6));
487 sdd->completed = &completed;
488 ares_gethostbyaddr(ghba_chan, &addr, sizeof(ws_in6_addr), AF_INET6,
489 c_ares_ghba_sync_cb, sdd);
492 * Now wait for it to finish.
494 wait_for_sync_resolv(&completed);
498 set_resolution_synchrony(gboolean synchronous)
500 resolve_synchronously = synchronous;
504 set_resolution_synchrony(gboolean synchronous _U_)
506 /* Nothing to set. */
508 #endif /* HAVE_C_ARES */
513 const gchar* name; /* Shallow copy */
517 * Miscellaneous functions
521 fgetline(char **buf, int *size, FILE *fp)
526 if (fp == NULL || buf == NULL)
533 *buf = (char *)wmem_alloc(wmem_epan_scope(), *size);
543 while ((c = ws_getc_unlocked(fp)) != EOF && c != '\r' && c != '\n') {
544 if (len+1 >= *size) {
545 *buf = (char *)wmem_realloc(wmem_epan_scope(), *buf, *size += BUFSIZ);
550 if (len == 0 && c == EOF)
561 * Local function definitions
563 static subnet_entry_t subnet_lookup(const guint32 addr);
564 static void subnet_entry_set(guint32 subnet_addr, const guint8 mask_length, const gchar* name);
568 add_service_name(port_type proto, const guint port, const char *service_name)
570 serv_port_t *serv_port_table;
573 key = (int *)wmem_new(wmem_epan_scope(), int);
576 serv_port_table = (serv_port_t *)wmem_map_lookup(serv_port_hashtable, &port);
577 if (serv_port_table == NULL) {
578 serv_port_table = wmem_new0(wmem_epan_scope(), serv_port_t);
579 wmem_map_insert(serv_port_hashtable, key, serv_port_table);
582 wmem_free(wmem_epan_scope(), key);
587 wmem_free(wmem_epan_scope(), serv_port_table->tcp_name);
588 serv_port_table->tcp_name = wmem_strdup(wmem_epan_scope(), service_name);
591 wmem_free(wmem_epan_scope(), serv_port_table->udp_name);
592 serv_port_table->udp_name = wmem_strdup(wmem_epan_scope(), service_name);
595 wmem_free(wmem_epan_scope(), serv_port_table->sctp_name);
596 serv_port_table->sctp_name = wmem_strdup(wmem_epan_scope(), service_name);
599 wmem_free(wmem_epan_scope(), serv_port_table->dccp_name);
600 serv_port_table->dccp_name = wmem_strdup(wmem_epan_scope(), service_name);
604 /* Should not happen */
607 new_resolved_objects = TRUE;
612 parse_service_line (char *line)
618 struct cb_serv_data cb_data;
619 range_t *port_rng = NULL;
621 if ((cp = strchr(line, '#')))
624 if ((cp = strtok(line, " \t")) == NULL)
629 if ((cp = strtok(NULL, " \t")) == NULL)
634 if (strtok(cp, "/") == NULL)
637 if (range_convert_str(NULL, &port_rng, port, G_MAXUINT16) != CVT_NO_ERROR) {
638 wmem_free (NULL, port_rng);
642 while ((cp = strtok(NULL, "/")) != NULL) {
643 if (strcmp(cp, "tcp") == 0) {
646 else if (strcmp(cp, "udp") == 0) {
649 else if (strcmp(cp, "sctp") == 0) {
652 else if (strcmp(cp, "dccp") == 0) {
658 cb_data.service = service;
659 cb_data.proto = proto;
660 range_foreach(port_rng, add_serv_port_cb, &cb_data);
663 wmem_free (NULL, port_rng);
664 } /* parse_service_line */
668 add_serv_port_cb(const guint32 port, gpointer ptr)
670 struct cb_serv_data *cb_data = (struct cb_serv_data *)ptr;
673 add_service_name(cb_data->proto, port, cb_data->service);
679 parse_services_file(const char * path)
683 static char *buf = NULL;
685 /* services hash table initialization */
686 serv_p = ws_fopen(path, "r");
691 while (fgetline(&buf, &size, serv_p) >= 0) {
692 parse_service_line(buf);
700 * unsigned integer to ascii
703 wmem_utoa(wmem_allocator_t *allocator, guint port)
705 gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
707 /* XXX, guint32_to_str() ? */
708 guint32_to_str_buf(port, bp, MAXNAMELEN);
713 _serv_name_lookup(port_type proto, guint port, serv_port_t **value_ret)
715 serv_port_t *serv_port_table;
717 serv_port_table = (serv_port_t *)wmem_map_lookup(serv_port_hashtable, &port);
719 if (value_ret != NULL)
720 *value_ret = serv_port_table;
722 if (serv_port_table == NULL)
727 return serv_port_table->udp_name;
729 return serv_port_table->tcp_name;
731 return serv_port_table->sctp_name;
733 return serv_port_table->dccp_name;
741 try_serv_name_lookup(port_type proto, guint port)
743 return _serv_name_lookup(proto, port, NULL);
747 serv_name_lookup(port_type proto, guint port)
749 serv_port_t *serv_port_table = NULL;
753 name = _serv_name_lookup(proto, port, &serv_port_table);
757 if (serv_port_table == NULL) {
758 key = (guint *)wmem_new(wmem_epan_scope(), guint);
760 serv_port_table = wmem_new0(wmem_epan_scope(), serv_port_t);
761 wmem_map_insert(serv_port_hashtable, key, serv_port_table);
763 if (serv_port_table->numeric == NULL) {
764 serv_port_table->numeric = wmem_strdup_printf(wmem_epan_scope(), "%u", port);
767 return serv_port_table->numeric;
771 initialize_services(void)
773 gboolean parse_file = TRUE;
774 g_assert(serv_port_hashtable == NULL);
775 serv_port_hashtable = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
777 /* Compute the pathname of the services file. */
778 if (g_services_path == NULL) {
779 g_services_path = get_datafile_path(ENAME_SERVICES);
781 parse_services_file(g_services_path);
783 /* Compute the pathname of the personal services file */
784 if (g_pservices_path == NULL) {
785 /* Check profile directory before personal configuration */
786 g_pservices_path = get_persconffile_path(ENAME_SERVICES, TRUE);
787 if (!parse_services_file(g_pservices_path)) {
788 g_free(g_pservices_path);
789 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
795 parse_services_file(g_pservices_path);
800 service_name_lookup_cleanup(void)
802 serv_port_hashtable = NULL;
803 g_free(g_services_path);
804 g_services_path = NULL;
805 g_free(g_pservices_path);
806 g_pservices_path = NULL;
810 parse_enterprises_line (char *line)
812 char *tok, *dec_str, *org_str;
815 if ((tok = strchr(line, '#')))
817 dec_str = strtok(line, " \t");
820 org_str = strtok(NULL, ""); /* everything else */
822 org_str = g_strstrip(org_str);
825 if (!ws_strtou32(dec_str, NULL, &dec))
827 g_hash_table_replace(enterprises_hashtable, GUINT_TO_POINTER(dec), g_strdup(org_str));
832 parse_enterprises_file(const char * path)
836 static char *buf = NULL;
838 fp = ws_fopen(path, "r");
842 while (fgetline(&buf, &size, fp) >= 0) {
843 parse_enterprises_line(buf);
851 initialize_enterprises(void)
853 g_assert(enterprises_hashtable == NULL);
854 enterprises_hashtable = g_hash_table_new_full(NULL, NULL, NULL, g_free);
856 if (g_enterprises_path == NULL) {
857 g_enterprises_path = get_datafile_path(ENAME_ENTERPRISES);
859 parse_enterprises_file(g_enterprises_path);
861 if (g_penterprises_path == NULL) {
862 g_penterprises_path = get_persconffile_path(ENAME_ENTERPRISES, FALSE);
864 parse_enterprises_file(g_penterprises_path);
868 try_enterprises_lookup(guint32 value)
870 return (const gchar *)g_hash_table_lookup(enterprises_hashtable, GUINT_TO_POINTER(value));
874 enterprises_lookup(guint32 value, const char *unknown_str)
878 s = try_enterprises_lookup(value);
881 if (unknown_str != NULL)
887 enterprises_base_custom(char *buf, guint32 value)
891 if ((s = try_enterprises_lookup(value)) == NULL)
892 s = ITEM_LABEL_UNKNOWN_STR;
893 g_snprintf(buf, ITEM_LABEL_LENGTH, "%s (%u)", s, value);
897 enterprises_cleanup(void)
899 g_assert(enterprises_hashtable);
900 g_hash_table_destroy(enterprises_hashtable);
901 enterprises_hashtable = NULL;
902 g_assert(g_enterprises_path);
903 g_free(g_enterprises_path);
904 g_enterprises_path = NULL;
905 g_free(g_penterprises_path);
906 g_penterprises_path = NULL;
907 g_free(g_pservices_path);
908 g_pservices_path = NULL;
911 /* Fill in an IP4 structure with info from subnets file or just with the
912 * string form of the address.
915 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
917 subnet_entry_t subnet_entry;
919 /* Overwrite if we get async DNS reply */
921 /* Do we have a subnet for this address? */
922 subnet_entry = subnet_lookup(addr);
923 if (0 != subnet_entry.mask) {
924 /* Print name, then '.' then IP address after subnet mask */
926 gchar buffer[WS_INET_ADDRSTRLEN];
930 host_addr = addr & (~(guint32)subnet_entry.mask);
931 ip_to_str_buf((guint8 *)&host_addr, buffer, WS_INET_ADDRSTRLEN);
934 /* Skip to first octet that is not totally masked
935 * If length of mask is 32, we chomp the whole address.
936 * If the address string starts '.' (should not happen?),
939 i = subnet_entry.mask_length / 8;
940 while(*(paddr) != '\0' && i > 0) {
941 if (*(++paddr) == '.') {
946 /* There are more efficient ways to do this, but this is safe if we
947 * trust g_snprintf and MAXNAMELEN
949 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
951 /* XXX: This means we end up printing "1.2.3.4 (1.2.3.4)" in many cases */
952 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
957 /* Fill in an IP6 structure with the string form of the address.
960 fill_dummy_ip6(hashipv6_t* volatile tp)
962 /* Overwrite if we get async DNS reply */
963 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
969 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
970 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
974 /* XXX, what to do if async_dns_in_flight == 0? */
975 async_dns_in_flight--;
977 if (status == ARES_SUCCESS) {
978 for (p = he->h_addr_list; *p != NULL; p++) {
979 switch(caqm->family) {
981 add_ipv4_name(caqm->addr.ip4, he->h_name);
984 add_ipv6_name(&caqm->addr.ip6, he->h_name);
987 /* Throw an exception? */
992 wmem_free(wmem_epan_scope(), caqm);
994 #endif /* HAVE_C_ARES */
996 /* --------------- */
998 new_ipv4(const guint addr)
1000 hashipv4_t *tp = wmem_new(wmem_epan_scope(), hashipv4_t);
1004 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
1009 host_lookup(const guint addr)
1011 hashipv4_t * volatile tp;
1013 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
1016 * We don't already have an entry for this host name; create one,
1017 * and then try to resolve it.
1019 tp = new_ipv4(addr);
1020 fill_dummy_ip4(addr, tp);
1021 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
1022 } else if (tp->flags & TRIED_OR_RESOLVED_MASK) {
1027 * This hasn't been resolved yet, and we haven't tried to
1028 * resolve it already.
1031 if (!gbl_resolv_flags.network_name)
1034 if (gbl_resolv_flags.use_external_net_name_resolver) {
1035 tp->flags |= TRIED_RESOLVE_ADDRESS;
1038 if (async_dns_initialized) {
1039 /* c-ares is initialized, so we can use it */
1040 if (resolve_synchronously || name_resolve_concurrency == 0) {
1042 * Either all names are to be resolved synchronously or
1043 * the concurrencly level is 0; do the resolution
1046 sync_lookup_ip4(addr);
1049 * Names are to be resolved asynchronously, and we
1050 * allow at least one asynchronous request in flight;
1051 * post an asynchronous request.
1053 async_dns_queue_msg_t *caqm;
1055 caqm = wmem_new(wmem_epan_scope(), async_dns_queue_msg_t);
1056 caqm->family = AF_INET;
1057 caqm->addr.ip4 = addr;
1058 wmem_list_append(async_dns_queue_head, (gpointer) caqm);
1068 /* --------------- */
1070 new_ipv6(const ws_in6_addr *addr)
1072 hashipv6_t *tp = wmem_new(wmem_epan_scope(), hashipv6_t);
1073 memcpy(tp->addr, addr->bytes, sizeof tp->addr);
1076 ip6_to_str_buf(addr, tp->ip6, sizeof(tp->ip6));
1080 /* ------------------------------------ */
1082 host_lookup6(const ws_in6_addr *addr)
1084 hashipv6_t * volatile tp;
1086 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addr);
1089 * We don't already have an entry for this host name; create one,
1090 * and then try to resolve it.
1092 ws_in6_addr *addr_key;
1094 addr_key = wmem_new(wmem_epan_scope(), ws_in6_addr);
1095 tp = new_ipv6(addr);
1096 memcpy(addr_key, addr, 16);
1098 wmem_map_insert(ipv6_hash_table, addr_key, tp);
1099 } else if (tp->flags & TRIED_OR_RESOLVED_MASK) {
1104 * This hasn't been resolved yet, and we haven't tried to
1105 * resolve it already.
1108 if (!gbl_resolv_flags.network_name)
1111 if (gbl_resolv_flags.use_external_net_name_resolver) {
1112 tp->flags |= TRIED_RESOLVE_ADDRESS;
1115 if (async_dns_initialized) {
1116 /* c-ares is initialized, so we can use it */
1117 if (resolve_synchronously || name_resolve_concurrency == 0) {
1119 * Either all names are to be resolved synchronously or
1120 * the concurrencly level is 0; do the resolution
1123 sync_lookup_ip6(addr);
1126 * Names are to be resolved asynchronously, and we
1127 * allow at least one asynchronous request in flight;
1128 * post an asynchronous request.
1130 async_dns_queue_msg_t *caqm;
1132 caqm = wmem_new(wmem_epan_scope(), async_dns_queue_msg_t);
1133 caqm->family = AF_INET6;
1134 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
1135 wmem_list_append(async_dns_queue_head, (gpointer) caqm);
1143 } /* host_lookup6 */
1146 * Ethernet / manufacturer resolution
1148 * The following functions implement ethernet address resolution and
1149 * ethers files parsing (see ethers(4)).
1151 * The manuf file has the same format as ethers(4) except that names are
1152 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1153 * only 3 bytes (instead of 6).
1157 * I decide to not use the existing functions (see ethers(3) on some
1158 * operating systems) for the following reasons:
1159 * - performance gains (use of hash tables and some other enhancements),
1160 * - use of two ethers files (system-wide and per user),
1161 * - avoid the use of NIS maps,
1162 * - lack of these functions on some systems.
1164 * So the following functions do _not_ behave as the standard ones.
1171 * If "accept_mask" is FALSE, either 3 or 6 bytes are valid, but no other number of bytes is.
1172 * If "accept_mask" is TRUE, parse an up-to-6-byte sequence with an optional
1176 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1177 const gboolean accept_mask)
1184 for (i = 0; i < 6; i++) {
1185 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1186 if (!g_ascii_isxdigit(*cp))
1188 num = strtoul(cp, &p, 16);
1190 return FALSE; /* failed */
1192 return FALSE; /* not a valid octet */
1193 eth->addr[i] = (guint8) num;
1194 cp = p; /* skip past the number */
1196 /* OK, what character terminated the octet? */
1198 /* "/" - this has a mask. */
1200 /* Entries with masks are not allowed in this file. */
1203 cp++; /* skip past the '/' to get to the mask */
1204 if (!g_ascii_isdigit(*cp))
1205 return FALSE; /* no sign allowed */
1206 num = strtoul(cp, &p, 10);
1208 return FALSE; /* failed */
1209 cp = p; /* skip past the number */
1210 if (*cp != '\0' && !g_ascii_isspace(*cp))
1211 return FALSE; /* bogus terminator */
1212 if (num == 0 || num >= 48)
1213 return FALSE; /* bogus mask */
1214 /* Mask out the bits not covered by the mask */
1216 for (i = 0; num >= 8; i++, num -= 8)
1217 ; /* skip octets entirely covered by the mask */
1218 /* Mask out the first masked octet */
1219 eth->addr[i] &= (0xFF << (8 - num));
1221 /* Mask out completely-masked-out octets */
1227 /* We're at the end of the address, and there's no mask. */
1229 /* We got 3 bytes, so this is a manufacturer ID. */
1231 /* Manufacturer IDs are not allowed in this file */
1234 /* Indicate that this is a manufacturer ID (0 is not allowed
1241 /* We got 6 bytes, so this is a MAC address (48 is not allowed as a mask). */
1247 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1252 /* We don't know the separator used in this number; it can either
1253 be ':', '-', or '.'. */
1254 if (*cp != ':' && *cp != '-' && *cp != '.')
1256 sep = *cp; /* subsequent separators must be the same */
1258 /* It has to be the same as the first separator */
1270 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1271 const gboolean accept_mask)
1274 * See the ethers(4) or ethers(5) man page for ethers file format
1275 * (not available on all systems).
1276 * We allow both ethernet address separators (':' and '-'),
1277 * as well as Wireshark's '.' separator.
1282 line = g_strstrip(line);
1283 if (line[0] == '\0' || line[0] == '#')
1286 if ((cp = strchr(line, '#'))) {
1291 if ((cp = strtok(line, " \t")) == NULL)
1294 if (!parse_ether_address(cp, eth, mask, accept_mask))
1297 if ((cp = strtok(NULL, " \t")) == NULL)
1300 g_strlcpy(eth->name, cp, MAXNAMELEN);
1302 if ((cp = strtok(NULL, "\t")) != NULL)
1304 g_strlcpy(eth->longname, cp, MAXNAMELEN);
1306 /* Make the long name the short name */
1307 g_strlcpy(eth->longname, eth->name, MAXNAMELEN);
1312 } /* parse_ether_line */
1314 static FILE *eth_p = NULL;
1317 set_ethent(char *path)
1322 eth_p = ws_fopen(path, "r");
1335 get_ethent(unsigned int *mask, const gboolean accept_mask)
1339 static int size = 0;
1340 static char *buf = NULL;
1345 while (fgetline(&buf, &size, eth_p) >= 0) {
1346 if (parse_ether_line(buf, ð, mask, accept_mask) == 0) {
1356 get_ethbyaddr(const guint8 *addr)
1361 set_ethent(g_pethers_path);
1363 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1369 set_ethent(g_ethers_path);
1371 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1379 } /* get_ethbyaddr */
1381 static hashmanuf_t *
1382 manuf_hash_new_entry(const guint8 *addr, char* name, char* longname)
1385 hashmanuf_t *manuf_value;
1388 /* manuf needs only the 3 most significant octets of the ethernet address */
1389 manuf_key = (int *)wmem_new(wmem_epan_scope(), int);
1390 *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1391 manuf_value = wmem_new(wmem_epan_scope(), hashmanuf_t);
1393 memcpy(manuf_value->addr, addr, 3);
1395 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1396 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1397 if (longname != NULL) {
1398 g_strlcpy(manuf_value->resolved_longname, longname, MAXNAMELEN);
1401 g_strlcpy(manuf_value->resolved_longname, name, MAXNAMELEN);
1405 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1406 manuf_value->resolved_name[0] = '\0';
1407 manuf_value->resolved_longname[0] = '\0';
1409 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1410 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1413 wmem_map_insert(manuf_hashtable, manuf_key, manuf_value);
1418 wka_hash_new_entry(const guint8 *addr, char* name)
1422 wka_key = (guint8 *)wmem_alloc(wmem_epan_scope(), 6);
1423 memcpy(wka_key, addr, 6);
1425 wmem_map_insert(wka_hashtable, wka_key, wmem_strdup(wmem_epan_scope(), name));
1429 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name, gchar *longname)
1434 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1435 manuf_hash_new_entry(addr, name, longname);
1439 /* This is a well-known MAC address; add it to the Ethernet hash table */
1440 add_eth_name(addr, name);
1444 /* This is a range of well-known addresses; add it to the well-known-address table */
1445 wka_hash_new_entry(addr, name);
1448 } /* add_manuf_name */
1450 static hashmanuf_t *
1451 manuf_name_lookup(const guint8 *addr)
1453 gint32 manuf_key = 0;
1455 hashmanuf_t *manuf_value;
1457 /* manuf needs only the 3 most significant octets of the ethernet address */
1458 manuf_key = addr[0];
1459 manuf_key = manuf_key<<8;
1461 manuf_key = manuf_key | oct;
1462 manuf_key = manuf_key<<8;
1464 manuf_key = manuf_key | oct;
1467 /* first try to find a "perfect match" */
1468 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1469 if (manuf_value != NULL) {
1473 /* Mask out the broadcast/multicast flag but not the locally
1474 * administered flag as locally administered means: not assigned
1475 * by the IEEE but the local administrator instead.
1476 * 0x01 multicast / broadcast bit
1477 * 0x02 locally administered bit */
1478 if ((manuf_key & 0x00010000) != 0) {
1479 manuf_key &= 0x00FEFFFF;
1480 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1481 if (manuf_value != NULL) {
1486 /* Add the address as a hex string */
1487 return manuf_hash_new_entry(addr, NULL, NULL);
1489 } /* manuf_name_lookup */
1492 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1494 guint8 masked_addr[6];
1499 if (wka_hashtable == NULL) {
1502 /* Get the part of the address covered by the mask. */
1503 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1504 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1505 /* Mask out the first masked octet */
1506 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1508 /* Zero out completely-masked-out octets */
1512 name = (gchar *)wmem_map_lookup(wka_hashtable, masked_addr);
1516 } /* wka_name_lookup */
1519 guint get_hash_ether_status(hashether_t* ether)
1521 return ether->status;
1524 char* get_hash_ether_hexaddr(hashether_t* ether)
1526 return ether->hexaddr;
1529 char* get_hash_ether_resolved_name(hashether_t* ether)
1531 return ether->resolved_name;
1535 eth_addr_hash(gconstpointer key)
1537 return wmem_strong_hash((const guint8 *)key, 6);
1541 eth_addr_cmp(gconstpointer a, gconstpointer b)
1543 return (memcmp(a, b, 6) == 0);
1547 initialize_ethers(void)
1552 /* hash table initialization */
1553 wka_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1554 manuf_hashtable = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
1555 eth_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1557 /* Compute the pathname of the ethers file. */
1558 if (g_ethers_path == NULL) {
1559 g_ethers_path = g_build_filename(get_systemfile_dir(), ENAME_ETHERS, NULL);
1562 /* Set g_pethers_path here, but don't actually do anything
1563 * with it. It's used in get_ethbyaddr().
1565 if (g_pethers_path == NULL)
1566 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1568 /* Compute the pathname of the manuf file */
1569 if (g_manuf_path == NULL)
1570 g_manuf_path = get_datafile_path(ENAME_MANUF);
1572 /* Read it and initialize the hash table */
1573 set_ethent(g_manuf_path);
1574 while ((eth = get_ethent(&mask, TRUE))) {
1575 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1579 /* Compute the pathname of the wka file */
1580 if (g_wka_path == NULL)
1581 g_wka_path = get_datafile_path(ENAME_WKA);
1583 /* Read it and initialize the hash table */
1584 set_ethent(g_wka_path);
1585 while ((eth = get_ethent(&mask, TRUE))) {
1586 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1590 } /* initialize_ethers */
1593 ethers_cleanup(void)
1595 g_free(g_ethers_path);
1596 g_ethers_path = NULL;
1597 g_free(g_pethers_path);
1598 g_pethers_path = NULL;
1599 g_free(g_manuf_path);
1600 g_manuf_path = NULL;
1605 /* Resolve ethernet address */
1606 static hashether_t *
1607 eth_addr_resolve(hashether_t *tp) {
1609 hashmanuf_t *manuf_value;
1610 const guint8 *addr = tp->addr;
1612 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1613 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1614 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1621 /* Unknown name. Try looking for it in the well-known-address
1622 tables for well-known address ranges smaller than 2^24. */
1625 /* Only the topmost 5 bytes participate fully */
1626 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1627 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1628 name, addr[5] & (0xFF >> mask));
1629 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1636 /* Only the topmost 4 bytes participate fully */
1637 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1638 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1639 name, addr[4] & (0xFF >> mask), addr[5]);
1640 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1647 /* Only the topmost 3 bytes participate fully */
1648 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1649 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1650 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1651 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1656 /* Now try looking in the manufacturer table. */
1657 manuf_value = manuf_name_lookup(addr);
1658 if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1659 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1660 manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1661 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1665 /* Now try looking for it in the well-known-address
1666 tables for well-known address ranges larger than 2^24. */
1669 /* Only the topmost 2 bytes participate fully */
1670 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1671 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1672 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1674 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1681 /* Only the topmost byte participates fully */
1682 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1683 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1684 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1686 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1693 /* Not even the topmost byte participates fully */
1694 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1695 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1696 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1697 addr[3], addr[4], addr[5]);
1698 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1701 } while (--mask); /* Work down to the last bit */
1703 /* No match whatsoever. */
1704 set_address(ðer_addr, AT_ETHER, 6, addr);
1705 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1706 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1709 g_assert_not_reached();
1710 } /* eth_addr_resolve */
1712 static hashether_t *
1713 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1718 tp = wmem_new(wmem_epan_scope(), hashether_t);
1719 memcpy(tp->addr, addr, sizeof(tp->addr));
1720 tp->status = HASHETHER_STATUS_UNRESOLVED;
1721 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1722 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1724 tp->resolved_name[0] = '\0';
1727 eth_addr_resolve(tp);
1729 wmem_map_insert(eth_hashtable, tp->addr, tp);
1732 } /* eth_hash_new_entry */
1734 static hashether_t *
1735 add_eth_name(const guint8 *addr, const gchar *name)
1739 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1742 tp = eth_hash_new_entry(addr, FALSE);
1745 if (strcmp(tp->resolved_name, name) != 0) {
1746 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1747 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1748 new_resolved_objects = TRUE;
1752 } /* add_eth_name */
1754 static hashether_t *
1755 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1759 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1762 tp = eth_hash_new_entry(addr, resolve);
1764 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1765 eth_addr_resolve(tp); /* Found but needs to be resolved */
1771 } /* eth_name_lookup */
1776 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1779 * We allow three address separators (':', '-', and '.'),
1780 * as well as no separators
1784 guint32 a, a0, a1, a2, a3;
1785 gboolean found_single_number = FALSE;
1787 if ((cp = strchr(line, '#')))
1790 if ((cp = strtok(line, " \t\n")) == NULL)
1793 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1794 * fill a and found_single_number is TRUE,
1797 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1798 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1799 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1800 if (sscanf(cp, "%x", &a) == 1) {
1801 found_single_number = TRUE;
1810 if ((cp = strtok(NULL, " \t\n")) == NULL)
1813 if (found_single_number) {
1817 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1820 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1824 } /* parse_ipxnets_line */
1826 static FILE *ipxnet_p = NULL;
1829 set_ipxnetent(char *path)
1834 ipxnet_p = ws_fopen(path, "r");
1850 static ipxnet_t ipxnet;
1851 static int size = 0;
1852 static char *buf = NULL;
1854 if (ipxnet_p == NULL)
1857 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1858 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1865 } /* get_ipxnetent */
1868 get_ipxnetbyaddr(guint32 addr)
1872 set_ipxnetent(g_ipxnets_path);
1874 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1876 if (ipxnet == NULL) {
1879 set_ipxnetent(g_pipxnets_path);
1881 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1889 } /* get_ipxnetbyaddr */
1892 initialize_ipxnets(void)
1894 /* Compute the pathname of the ipxnets file.
1896 * XXX - is there a notion of an "ipxnets file" in any flavor of
1897 * UNIX, or with any add-on Netware package for UNIX? If not,
1898 * should the UNIX version of the ipxnets file be in the datafile
1899 * directory as well?
1901 if (g_ipxnets_path == NULL) {
1902 g_ipxnets_path = wmem_strdup_printf(wmem_epan_scope(), "%s" G_DIR_SEPARATOR_S "%s",
1903 get_systemfile_dir(), ENAME_IPXNETS);
1906 /* Set g_pipxnets_path here, but don't actually do anything
1907 * with it. It's used in get_ipxnetbyaddr().
1909 if (g_pipxnets_path == NULL)
1910 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1912 } /* initialize_ipxnets */
1915 ipx_name_lookup_cleanup(void)
1917 ipxnet_hash_table = NULL;
1918 g_free(g_pipxnets_path);
1919 g_pipxnets_path = NULL;
1923 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1928 tp = (hashipxnet_t *)wmem_map_lookup(ipxnet_hash_table, &addr);
1932 key = (int *)wmem_new(wmem_epan_scope(), int);
1934 tp = wmem_new(wmem_epan_scope(), hashipxnet_t);
1935 wmem_map_insert(ipxnet_hash_table, key, tp);
1937 return wmem_strdup(allocator, tp->name);
1940 /* fill in a new entry */
1944 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1946 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1949 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1952 return wmem_strdup(allocator, tp->name);
1954 } /* ipxnet_name_lookup */
1958 parse_vlan_line(char *line, vlan_t *vlan)
1963 if ((cp = strchr(line, '#')))
1966 if ((cp = strtok(line, " \t\n")) == NULL)
1969 if (sscanf(cp, "%" G_GUINT16_FORMAT, &id) == 1) {
1976 if ((cp = strtok(NULL, "\t\n")) == NULL)
1979 g_strlcpy(vlan->name, cp, MAXVLANNAMELEN);
1983 } /* parse_vlan_line */
1985 static FILE *vlan_p = NULL;
1988 set_vlanent(char *path)
1993 vlan_p = ws_fopen(path, "r");
2010 static int size = 0;
2011 static char *buf = NULL;
2016 while (fgetline(&buf, &size, vlan_p) >= 0) {
2017 if (parse_vlan_line(buf, &vlan) == 0) {
2027 get_vlannamebyid(guint16 id)
2031 set_vlanent(g_pvlan_path);
2033 while (((vlan = get_vlanent()) != NULL) && (id != vlan->id) ) ;
2042 } /* get_vlannamebyid */
2045 initialize_vlans(void)
2047 g_assert(vlan_hash_table == NULL);
2048 vlan_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
2050 /* Set g_pvlan_path here, but don't actually do anything
2051 * with it. It's used in get_vlannamebyid()
2053 if (g_pvlan_path == NULL)
2054 g_pvlan_path = get_persconffile_path(ENAME_VLANS, FALSE);
2056 } /* initialize_vlans */
2059 vlan_name_lookup_cleanup(void)
2061 vlan_hash_table = NULL;
2062 g_free(g_pvlan_path);
2063 g_pvlan_path = NULL;
2066 static const gchar *
2067 vlan_name_lookup(const guint id)
2072 tp = (hashvlan_t *)wmem_map_lookup(vlan_hash_table, &id);
2076 key = (int *)wmem_new(wmem_epan_scope(), int);
2078 tp = wmem_new(wmem_epan_scope(), hashvlan_t);
2079 wmem_map_insert(vlan_hash_table, key, tp);
2084 /* fill in a new entry */
2088 if ( (vlan = get_vlannamebyid(id)) == NULL) {
2090 g_snprintf(tp->name, MAXVLANNAMELEN, "<%u>", id);
2093 g_strlcpy(tp->name, vlan->name, MAXVLANNAMELEN);
2098 } /* vlan_name_lookup */
2102 read_hosts_file (const char *hostspath, gboolean store_entries)
2110 ws_in6_addr ip6_addr;
2112 gboolean is_ipv6, entry_found = FALSE;
2115 * See the hosts(4) or hosts(5) man page for hosts file format
2116 * (not available on all systems).
2118 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2121 while (fgetline(&line, &size, hf) >= 0) {
2122 if ((cp = strchr(line, '#')))
2125 if ((cp = strtok(line, " \t")) == NULL)
2126 continue; /* no tokens in the line */
2128 if (ws_inet_pton6(cp, &host_addr.ip6_addr)) {
2131 } else if (ws_inet_pton4(cp, &host_addr.ip4_addr)) {
2138 if ((cp = strtok(NULL, " \t")) == NULL)
2139 continue; /* no host name */
2142 if (store_entries) {
2144 add_ipv6_name(&host_addr.ip6_addr, cp);
2146 add_ipv4_name(host_addr.ip4_addr, cp);
2150 wmem_free(wmem_epan_scope(), line);
2153 return entry_found ? TRUE : FALSE;
2154 } /* read_hosts_file */
2157 add_hosts_file (const char *hosts_file)
2159 gboolean found = FALSE;
2165 if (!extra_hosts_files)
2166 extra_hosts_files = g_ptr_array_new();
2168 for (i = 0; i < extra_hosts_files->len; i++) {
2169 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2174 g_ptr_array_add(extra_hosts_files, wmem_strdup(wmem_epan_scope(), hosts_file));
2175 return read_hosts_file (hosts_file, FALSE);
2181 add_ip_name_from_string (const char *addr, const char *name)
2185 ws_in6_addr ip6_addr;
2188 resolved_ipv4_t *resolved_ipv4_entry;
2189 resolved_ipv6_t *resolved_ipv6_entry;
2191 if (ws_inet_pton6(addr, &host_addr.ip6_addr)) {
2193 } else if (ws_inet_pton4(addr, &host_addr.ip4_addr)) {
2200 resolved_ipv6_entry = wmem_new(wmem_epan_scope(), resolved_ipv6_t);
2201 memcpy(&(resolved_ipv6_entry->ip6_addr), &host_addr.ip6_addr, 16);
2202 g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2203 wmem_list_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2205 resolved_ipv4_entry = wmem_new(wmem_epan_scope(), resolved_ipv4_t);
2206 resolved_ipv4_entry->host_addr = host_addr.ip4_addr;
2207 g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2208 wmem_list_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2212 } /* add_ip_name_from_string */
2215 * Add the resolved addresses that are in use to the list used to create the NRB
2218 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2220 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2221 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2223 if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2224 lists->ipv4_addr_list = g_list_prepend(lists->ipv4_addr_list, ipv4_hash_table_entry);
2230 * Add the resolved addresses that are in use to the list used to create the NRB
2234 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2236 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2237 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2239 if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2240 lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2246 get_addrinfo_list(void)
2248 if (ipv4_hash_table) {
2249 wmem_map_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2252 if (ipv6_hash_table) {
2253 wmem_map_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2256 return &addrinfo_lists;
2259 /* Read in a list of subnet definition - name pairs.
2260 * <line> = <comment> | <entry> | <whitespace>
2261 * <comment> = <whitespace>#<any>
2262 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2263 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2264 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2265 * <subnet_mask_length> is a decimal 1-31
2266 * <subnet_name> is a string containing no whitespace.
2267 * <whitespace> = (space | tab)+
2268 * Any malformed entries are ignored.
2269 * Any trailing data after the subnet_name is ignored.
2274 read_subnets_file (const char *subnetspath)
2280 guint32 host_addr; /* IPv4 ONLY */
2283 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2286 while (fgetline(&line, &size, hf) >= 0) {
2287 if ((cp = strchr(line, '#')))
2290 if ((cp = strtok(line, " \t")) == NULL)
2291 continue; /* no tokens in the line */
2294 /* Expected format is <IP4 address>/<subnet length> */
2295 cp2 = strchr(cp, '/');
2300 *cp2 = '\0'; /* Cut token */
2303 /* Check if this is a valid IPv4 address */
2304 if (!str_to_ip(cp, &host_addr)) {
2308 if (!ws_strtou8(cp2, NULL, &mask_length) || mask_length == 0 || mask_length > 32) {
2309 continue; /* invalid mask length */
2312 if ((cp = strtok(NULL, " \t")) == NULL)
2313 continue; /* no subnet name */
2315 subnet_entry_set(host_addr, mask_length, cp);
2317 wmem_free(wmem_epan_scope(), line);
2321 } /* read_subnets_file */
2323 static subnet_entry_t
2324 subnet_lookup(const guint32 addr)
2326 subnet_entry_t subnet_entry;
2329 /* Search mask lengths linearly, longest first */
2331 i = SUBNETLENGTHSIZE;
2332 while(have_subnet_entry && i > 0) {
2333 guint32 masked_addr;
2334 subnet_length_entry_t* length_entry;
2336 /* Note that we run from 31 (length 32) to 0 (length 1) */
2338 g_assert(i < SUBNETLENGTHSIZE);
2341 length_entry = &subnet_length_entries[i];
2343 if (NULL != length_entry->subnet_addresses) {
2344 sub_net_hashipv4_t * tp;
2347 masked_addr = addr & length_entry->mask;
2348 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2350 tp = length_entry->subnet_addresses[hash_idx];
2351 while(tp != NULL && tp->addr != masked_addr) {
2356 subnet_entry.mask = length_entry->mask;
2357 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2358 subnet_entry.name = tp->name;
2359 return subnet_entry;
2364 subnet_entry.mask = 0;
2365 subnet_entry.mask_length = 0;
2366 subnet_entry.name = NULL;
2368 return subnet_entry;
2371 /* Add a subnet-definition - name pair to the set.
2372 * The definition is taken by masking the address passed in with the mask of the
2376 subnet_entry_set(guint32 subnet_addr, const guint8 mask_length, const gchar* name)
2378 subnet_length_entry_t* entry;
2379 sub_net_hashipv4_t * tp;
2382 g_assert(mask_length > 0 && mask_length <= 32);
2384 entry = &subnet_length_entries[mask_length - 1];
2386 subnet_addr &= entry->mask;
2388 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2390 if (NULL == entry->subnet_addresses) {
2391 entry->subnet_addresses = (sub_net_hashipv4_t**)wmem_alloc0(wmem_epan_scope(), sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2394 if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2395 sub_net_hashipv4_t * new_tp;
2398 if (tp->addr == subnet_addr) {
2399 return; /* XXX provide warning that an address was repeated? */
2405 new_tp = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2409 tp = entry->subnet_addresses[hash_idx] = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2413 tp->addr = subnet_addr;
2414 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2415 have_subnet_entry = TRUE;
2419 subnet_name_lookup_init(void)
2424 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2425 guint32 length = i + 1;
2427 subnet_length_entries[i].subnet_addresses = NULL;
2428 subnet_length_entries[i].mask_length = length;
2429 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2432 /* Check profile directory before personal configuration */
2433 subnetspath = get_persconffile_path(ENAME_SUBNETS, TRUE);
2434 if (!read_subnets_file(subnetspath)) {
2435 if (errno != ENOENT) {
2436 report_open_failure(subnetspath, errno, FALSE);
2439 g_free(subnetspath);
2440 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2441 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2442 report_open_failure(subnetspath, errno, FALSE);
2445 g_free(subnetspath);
2448 * Load the global subnets file, if we have one.
2450 subnetspath = get_datafile_path(ENAME_SUBNETS);
2451 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2452 report_open_failure(subnetspath, errno, FALSE);
2454 g_free(subnetspath);
2457 /* SS7 PC Name Resolution Portion */
2458 static hashss7pc_t *
2459 new_ss7pc(const guint8 ni, const guint32 pc)
2461 hashss7pc_t *tp = wmem_new(wmem_epan_scope(), hashss7pc_t);
2462 tp->id = (ni<<24) + (pc&0xffffff);
2463 tp->pc_addr[0] = '\0';
2469 static hashss7pc_t *
2470 host_lookup_ss7pc(const guint8 ni, const guint32 pc)
2472 hashss7pc_t * volatile tp;
2475 id = (ni<<24) + (pc&0xffffff);
2477 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2479 tp = new_ss7pc(ni, pc);
2480 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2486 void fill_unresolved_ss7pc(const gchar * pc_addr, const guint8 ni, const guint32 pc)
2488 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2490 g_strlcpy(tp->pc_addr, pc_addr, MAXNAMELEN);
2494 get_hostname_ss7pc(const guint8 ni, const guint32 pc)
2496 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2498 /* never resolved yet*/
2499 if (tp->pc_addr[0] == '\0')
2502 /* Don't have name in file */
2503 if (tp->name[0] == '\0')
2506 if (!gbl_resolv_flags.ss7pc_name)
2513 add_ss7pc_name(const guint8 ni, guint32 pc, const gchar *name)
2518 if (!name || name[0] == '\0')
2521 id = (ni<<24) + (pc&0xffffff);
2522 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2524 tp = new_ss7pc(ni, pc);
2525 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2528 if (g_ascii_strcasecmp(tp->name, name)) {
2529 g_strlcpy(tp->name, name, MAXNAMELEN);
2534 read_ss7pcs_file(const char *ss7pcspath)
2542 gboolean entry_found = FALSE;
2545 * File format is Network Indicator (decimal)<dash>Point Code (Decimal)<tab/space>Hostname
2547 if ((hf = ws_fopen(ss7pcspath, "r")) == NULL)
2550 while (fgetline(&line, &size, hf) >= 0) {
2551 if ((cp = strchr(line, '#')))
2554 if ((cp = strtok(line, "-")) == NULL)
2555 continue; /*no ni-pc separator*/
2556 if (!ws_strtou8(cp, NULL, &ni))
2561 if ((cp = strtok(NULL, " \t")) == NULL)
2562 continue; /* no tokens for pc and name */
2563 if (!ws_strtou32(cp, NULL, &pc))
2568 if ((cp = strtok(NULL, " \t")) == NULL)
2569 continue; /* no host name */
2572 add_ss7pc_name(ni, pc, cp);
2574 wmem_free(wmem_epan_scope(), line);
2577 return entry_found ? TRUE : FALSE;
2581 ss7pc_name_lookup_init(void)
2585 g_assert(ss7pc_hash_table == NULL);
2587 ss7pc_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2590 * Load the user's ss7pcs file
2592 ss7pcspath = get_persconffile_path(ENAME_SS7PCS, TRUE);
2593 if (!read_ss7pcs_file(ss7pcspath) && errno != ENOENT) {
2594 report_open_failure(ss7pcspath, errno, FALSE);
2599 /* SS7PC Name Resolution End*/
2603 * External Functions
2607 addr_resolve_pref_init(module_t *nameres)
2609 prefs_register_bool_preference(nameres, "mac_name",
2610 "Resolve MAC addresses",
2611 "Resolve Ethernet MAC addresses to host names from the preferences"
2612 " or system's Ethers file, or to a manufacturer based name.",
2613 &gbl_resolv_flags.mac_name);
2615 prefs_register_bool_preference(nameres, "transport_name",
2616 "Resolve transport names",
2617 "Resolve TCP/UDP ports into service names",
2618 &gbl_resolv_flags.transport_name);
2620 prefs_register_bool_preference(nameres, "network_name",
2621 "Resolve network (IP) addresses",
2622 "Resolve IPv4, IPv6, and IPX addresses into host names."
2623 " The next set of check boxes determines how name resolution should be performed."
2624 " If no other options are checked name resolution is made from Wireshark's host file,"
2625 " capture file name resolution blocks and DNS packets in the capture.",
2626 &gbl_resolv_flags.network_name);
2628 prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
2629 "Use captured DNS packet data for address resolution",
2630 "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
2631 &gbl_resolv_flags.dns_pkt_addr_resolution);
2634 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2635 "Use an external network name resolver",
2636 "Use your system's configured name resolver"
2637 " (usually DNS) to resolve network names."
2638 " Only applies when network name resolution"
2640 &gbl_resolv_flags.use_external_net_name_resolver);
2642 prefs_register_obsolete_preference(nameres, "concurrent_dns");
2644 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2645 "Maximum concurrent requests",
2646 "The maximum number of DNS requests that may"
2647 " be active at any time. A large value (many"
2648 " thousands) might overload the network or make"
2649 " your DNS server behave badly.",
2651 &name_resolve_concurrency);
2653 prefs_register_static_text_preference(nameres, "use_external_name_resolver",
2654 "Use an external network name resolver: N/A",
2655 "Support for using a concurrent external name resolver was not"
2656 " compiled into this version of Wireshark");
2659 prefs_register_bool_preference(nameres, "hosts_file_handling",
2660 "Only use the profile \"hosts\" file",
2661 "By default \"hosts\" files will be loaded from multiple sources."
2662 " Checking this box only loads the \"hosts\" in the current profile.",
2663 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2665 prefs_register_bool_preference(nameres, "vlan_name",
2667 "Resolve VLAN IDs to network names from the preferences \"vlans\" file."
2668 " Format of the file is: \"ID<Tab>Name\"."
2669 " One line per VLAN, e.g.: 1 Management",
2670 &gbl_resolv_flags.vlan_name);
2672 prefs_register_bool_preference(nameres, "ss7_pc_name",
2674 "Resolve SS7 Point Codes to node names from the profiles \"ss7pcs\" file."
2675 " Format of the file is: \"Network_Indicator<Dash>PC_Decimal<Tab>Name\"."
2676 " One line per Point Code, e.g.: 2-1234 MyPointCode1",
2677 &gbl_resolv_flags.ss7pc_name);
2682 disable_name_resolution(void) {
2683 gbl_resolv_flags.mac_name = FALSE;
2684 gbl_resolv_flags.network_name = FALSE;
2685 gbl_resolv_flags.transport_name = FALSE;
2686 gbl_resolv_flags.dns_pkt_addr_resolution = FALSE;
2687 gbl_resolv_flags.use_external_net_name_resolver = FALSE;
2688 gbl_resolv_flags.vlan_name = FALSE;
2689 gbl_resolv_flags.ss7pc_name = FALSE;
2694 host_name_lookup_process(void) {
2695 async_dns_queue_msg_t *caqm;
2696 struct timeval tv = { 0, 0 };
2699 gboolean nro = new_resolved_objects;
2700 wmem_list_frame_t* head;
2702 new_resolved_objects = FALSE;
2703 nro |= maxmind_db_lookup_process();
2705 if (!async_dns_initialized)
2706 /* c-ares not initialized. Bail out and cancel timers. */
2709 head = wmem_list_head(async_dns_queue_head);
2711 while (head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2712 caqm = (async_dns_queue_msg_t *)wmem_list_frame_data(head);
2713 wmem_list_remove_frame(async_dns_queue_head, head);
2714 if (caqm->family == AF_INET) {
2715 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2716 c_ares_ghba_cb, caqm);
2717 async_dns_in_flight++;
2718 } else if (caqm->family == AF_INET6) {
2719 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(ws_in6_addr),
2720 AF_INET6, c_ares_ghba_cb, caqm);
2721 async_dns_in_flight++;
2724 head = wmem_list_head(async_dns_queue_head);
2729 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2731 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2732 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2735 ares_process(ghba_chan, &rfds, &wfds);
2738 /* Any new entries? */
2743 _host_name_lookup_cleanup(void) {
2744 async_dns_queue_head = NULL;
2746 if (async_dns_initialized) {
2747 ares_destroy(ghba_chan);
2748 ares_destroy(ghbn_chan);
2750 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2751 ares_library_cleanup();
2753 async_dns_initialized = FALSE;
2759 host_name_lookup_process(void) {
2760 gboolean nro = new_resolved_objects;
2762 new_resolved_objects = FALSE;
2764 nro |= maxmind_db_lookup_process();
2770 _host_name_lookup_cleanup(void) {
2773 #endif /* HAVE_C_ARES */
2776 get_hostname(const guint addr)
2778 /* XXX why do we call this if we're not resolving? To create hash entries?
2781 hashipv4_t *tp = host_lookup(addr);
2783 if (!gbl_resolv_flags.network_name)
2786 tp->flags |= RESOLVED_ADDRESS_USED;
2791 /* -------------------------- */
2794 get_hostname6(const ws_in6_addr *addr)
2796 /* XXX why do we call this if we're not resolving? To create hash entries?
2799 hashipv6_t *tp = host_lookup6(addr);
2801 if (!gbl_resolv_flags.network_name)
2804 tp->flags |= RESOLVED_ADDRESS_USED;
2809 /* -------------------------- */
2811 add_ipv4_name(const guint addr, const gchar *name)
2816 * Don't add zero-length names; apparently, some resolvers will return
2817 * them if they get them from DNS.
2819 if (!name || name[0] == '\0')
2822 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2824 tp = new_ipv4(addr);
2825 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2828 if (g_ascii_strcasecmp(tp->name, name)) {
2829 g_strlcpy(tp->name, name, MAXNAMELEN);
2830 new_resolved_objects = TRUE;
2832 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2833 } /* add_ipv4_name */
2835 /* -------------------------- */
2837 add_ipv6_name(const ws_in6_addr *addrp, const gchar *name)
2842 * Don't add zero-length names; apparently, some resolvers will return
2843 * them if they get them from DNS.
2845 if (!name || name[0] == '\0')
2848 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addrp);
2850 ws_in6_addr *addr_key;
2852 addr_key = wmem_new(wmem_epan_scope(), ws_in6_addr);
2853 tp = new_ipv6(addrp);
2854 memcpy(addr_key, addrp, 16);
2855 wmem_map_insert(ipv6_hash_table, addr_key, tp);
2858 if (g_ascii_strcasecmp(tp->name, name)) {
2859 g_strlcpy(tp->name, name, MAXNAMELEN);
2860 new_resolved_objects = TRUE;
2862 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2863 } /* add_ipv6_name */
2866 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2868 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2870 add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2874 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2876 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2878 add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2882 add_manually_resolved(void)
2884 if (manually_resolved_ipv4_list) {
2885 wmem_list_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2888 if (manually_resolved_ipv6_list) {
2889 wmem_list_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2894 host_name_lookup_init(void)
2899 g_assert(ipxnet_hash_table == NULL);
2900 ipxnet_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
2902 g_assert(ipv4_hash_table == NULL);
2903 ipv4_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2905 g_assert(ipv6_hash_table == NULL);
2906 ipv6_hash_table = wmem_map_new(wmem_epan_scope(), ipv6_oat_hash, ipv6_equal);
2909 g_assert(async_dns_queue_head == NULL);
2910 async_dns_queue_head = wmem_list_new(wmem_epan_scope());
2913 if (manually_resolved_ipv4_list == NULL)
2914 manually_resolved_ipv4_list = wmem_list_new(wmem_epan_scope());
2916 if (manually_resolved_ipv6_list == NULL)
2917 manually_resolved_ipv6_list = wmem_list_new(wmem_epan_scope());
2920 * Load the global hosts file, if we have one.
2922 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2923 hostspath = get_datafile_path(ENAME_HOSTS);
2924 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2925 report_open_failure(hostspath, errno, FALSE);
2930 * Load the user's hosts file no matter what, if they have one.
2932 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2933 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2934 report_open_failure(hostspath, errno, FALSE);
2938 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2939 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2941 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2942 async_dns_initialized = TRUE;
2944 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2948 #endif /* HAVE_C_ARES */
2950 if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
2951 for (i = 0; i < extra_hosts_files->len; i++) {
2952 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
2956 subnet_name_lookup_init();
2958 add_manually_resolved();
2960 ss7pc_name_lookup_init();
2964 host_name_lookup_cleanup(void)
2967 sub_net_hashipv4_t *entry, *next_entry;
2969 _host_name_lookup_cleanup();
2971 ipxnet_hash_table = NULL;
2972 ipv4_hash_table = NULL;
2973 ipv6_hash_table = NULL;
2974 ss7pc_hash_table = NULL;
2976 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2977 if (subnet_length_entries[i].subnet_addresses != NULL) {
2978 for (j = 0; j < HASHHOSTSIZE; j++) {
2979 for (entry = subnet_length_entries[i].subnet_addresses[j];
2980 entry != NULL; entry = next_entry) {
2981 next_entry = entry->next;
2982 wmem_free(wmem_epan_scope(), entry);
2985 wmem_free(wmem_epan_scope(), subnet_length_entries[i].subnet_addresses);
2986 subnet_length_entries[i].subnet_addresses = NULL;
2990 have_subnet_entry = FALSE;
2991 new_resolved_objects = FALSE;
2995 void host_name_lookup_reset(void)
2997 host_name_lookup_cleanup();
2998 host_name_lookup_init();
3002 manually_resolve_cleanup(void)
3004 if (manually_resolved_ipv4_list) {
3005 wmem_destroy_list(manually_resolved_ipv4_list);
3006 manually_resolved_ipv4_list = NULL;
3008 if (manually_resolved_ipv6_list) {
3009 wmem_destroy_list(manually_resolved_ipv6_list);
3010 manually_resolved_ipv6_list = NULL;
3015 udp_port_to_display(wmem_allocator_t *allocator, guint port)
3018 if (!gbl_resolv_flags.transport_name) {
3019 return wmem_utoa(allocator, port);
3022 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
3024 } /* udp_port_to_display */
3027 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
3030 if (!gbl_resolv_flags.transport_name) {
3031 return wmem_utoa(allocator, port);
3034 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
3036 } /* dccp_port_to_display */
3039 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
3042 if (!gbl_resolv_flags.transport_name) {
3043 return wmem_utoa(allocator, port);
3046 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
3048 } /* tcp_port_to_display */
3051 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
3054 if (!gbl_resolv_flags.transport_name) {
3055 return wmem_utoa(allocator, port);
3058 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
3060 } /* sctp_port_to_display */
3063 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
3065 const gchar *port_str;
3067 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3068 /* No name resolution support, just return port string */
3069 return wmem_strdup_printf(scope, "%u", port);
3071 port_str = serv_name_lookup(proto, port);
3073 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
3077 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
3079 const gchar *port_str;
3081 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3082 /* No name resolution support, just return port string */
3083 return g_snprintf(buf, buf_size, "%u", port);
3085 port_str = serv_name_lookup(proto, port);
3087 return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
3091 get_ether_name(const guint8 *addr)
3094 gboolean resolve = gbl_resolv_flags.mac_name;
3096 tp = eth_name_lookup(addr, resolve);
3098 return resolve ? tp->resolved_name : tp->hexaddr;
3100 } /* get_ether_name */
3103 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
3105 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
3108 /* Look for a (non-dummy) ether name in the hash, and return it if found.
3109 * If it's not found, simply return NULL.
3112 get_ether_name_if_known(const guint8 *addr)
3116 /* Initialize ether structs if we're the first
3117 * ether-related function called */
3118 if (!gbl_resolv_flags.mac_name)
3121 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3122 tp = eth_name_lookup(addr, TRUE);
3123 g_assert(tp != NULL);
3125 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3126 /* Name is from an ethers file */
3127 return tp->resolved_name;
3130 /* Name was created */
3136 add_ether_byip(const guint ip, const guint8 *eth)
3140 /* first check that IP address can be resolved */
3141 if (!gbl_resolv_flags.network_name)
3144 tp = host_lookup(ip);
3147 * Was this IP address resolved to a host name?
3149 if (tp->flags & NAME_RESOLVED) {
3151 * Yes, so add an entry in the ethers hashtable resolving
3152 * the MAC address to that name.
3154 add_eth_name(eth, tp->name);
3157 } /* add_ether_byip */
3160 ipxnet_to_str_punct(wmem_allocator_t *allocator, const guint32 ad, const char punct)
3162 gchar *buf = (gchar *)wmem_alloc(allocator, 12);
3164 *dword_to_hex_punct(buf, ad, punct) = '\0';
3169 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3172 if (!gbl_resolv_flags.network_name) {
3173 return ipxnet_to_str_punct(allocator, addr, '\0');
3176 return ipxnet_name_lookup(allocator, addr);
3178 } /* get_ipxnet_name */
3181 get_vlan_name(wmem_allocator_t *allocator, const guint16 id)
3184 if (!gbl_resolv_flags.vlan_name) {
3188 return wmem_strdup(allocator, vlan_name_lookup(id));
3190 } /* get_vlan_name */
3193 get_manuf_name(const guint8 *addr)
3195 hashmanuf_t *manuf_value;
3197 manuf_value = manuf_name_lookup(addr);
3198 if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3199 return manuf_value->resolved_name;
3201 return manuf_value->hexaddr;
3203 } /* get_manuf_name */
3206 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3208 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3212 get_manuf_name_if_known(const guint8 *addr)
3214 hashmanuf_t *manuf_value;
3218 /* manuf needs only the 3 most significant octets of the ethernet address */
3219 manuf_key = addr[0];
3220 manuf_key = manuf_key<<8;
3222 manuf_key = manuf_key | oct;
3223 manuf_key = manuf_key<<8;
3225 manuf_key = manuf_key | oct;
3227 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3228 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3232 return manuf_value->resolved_longname;
3234 } /* get_manuf_name_if_known */
3237 uint_get_manuf_name_if_known(const guint manuf_key)
3239 hashmanuf_t *manuf_value;
3241 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3242 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3246 return manuf_value->resolved_longname;
3250 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3252 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3255 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3257 return manuf->resolved_longname;
3261 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3263 guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3264 hashmanuf_t *manuf_value;
3267 /* Copy and convert the address to network byte order. */
3268 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3270 manuf_value = manuf_name_lookup(addr);
3271 if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3272 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]);
3274 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]);
3277 wmem_free(NULL, addr);
3279 } /* eui64_to_display */
3282 #define GHI_TIMEOUT (250 * 1000)
3284 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3286 * XXX - If we wanted to be really fancy we could cache results here and
3287 * look them up in get_host_ipaddr* below.
3289 async_hostent_t *ahp = (async_hostent_t *)arg;
3290 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3291 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3292 ahp->copied = hp->h_length;
3295 #endif /* HAVE_C_ARES */
3297 /* Translate a string, assumed either to be a dotted-quad IPv4 address or
3298 * a host name, to a numeric IPv4 address. Return TRUE if we succeed and
3299 * set "*addrp" to that numeric IPv4 address; return FALSE if we fail. */
3301 get_host_ipaddr(const char *host, guint32 *addrp)
3304 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3307 async_hostent_t ahe;
3311 * XXX - are there places where this is used to translate something
3312 * that's *only* supposed to be an IPv4 address, and where it
3313 * *shouldn't* translate host names?
3315 if (!ws_inet_pton4(host, addrp)) {
3317 /* It's not a valid dotted-quad IP address; is it a valid
3321 /* If we're not allowed to do name resolution, don't do name
3324 if (!gbl_resolv_flags.network_name ||
3325 !gbl_resolv_flags.use_external_net_name_resolver) {
3330 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3333 ahe.addr_size = (int) sizeof (struct in_addr);
3336 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3339 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3341 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3342 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3343 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3346 ares_process(ghbn_chan, &rfds, &wfds);
3348 ares_cancel(ghbn_chan);
3349 if (ahe.addr_size == ahe.copied) {
3360 * Translate IPv6 numeric address or FQDN hostname into binary IPv6 address.
3361 * Return TRUE if we succeed and set "*addrp" to that numeric IPv6 address;
3362 * return FALSE if we fail.
3365 get_host_ipaddr6(const char *host, ws_in6_addr *addrp)
3368 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3371 async_hostent_t ahe;
3372 #endif /* HAVE_C_ARES */
3374 if (str_to_ip6(host, addrp))
3377 /* It's not a valid dotted-quad IP address; is it a valid
3380 * XXX - are there places where this is used to translate something
3381 * that's *only* supposed to be an IPv6 address, and where it
3382 * *shouldn't* translate host names?
3385 /* If we're not allowed to do name resolution, don't do name
3388 if (!gbl_resolv_flags.network_name ||
3389 !gbl_resolv_flags.use_external_net_name_resolver) {
3395 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3398 ahe.addr_size = (int) sizeof (ws_in6_addr);
3401 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3404 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3406 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3407 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3408 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3411 ares_process(ghbn_chan, &rfds, &wfds);
3413 ares_cancel(ghbn_chan);
3414 if (ahe.addr_size == ahe.copied) {
3423 get_manuf_hashtable(void)
3425 return manuf_hashtable;
3429 get_wka_hashtable(void)
3431 return wka_hashtable;
3435 get_eth_hashtable(void)
3437 return eth_hashtable;
3441 get_serv_port_hashtable(void)
3443 return serv_port_hashtable;
3447 get_ipxnet_hash_table(void)
3449 return ipxnet_hash_table;
3453 get_vlan_hash_table(void)
3455 return vlan_hash_table;
3459 get_ipv4_hash_table(void)
3461 return ipv4_hash_table;
3465 get_ipv6_hash_table(void)
3467 return ipv6_hash_table;
3469 /* Initialize all the address resolution subsystems in this file */
3471 addr_resolv_init(void)
3473 initialize_services();
3474 initialize_ethers();
3475 initialize_ipxnets();
3477 initialize_enterprises();
3478 host_name_lookup_init();
3481 /* Clean up all the address resolution subsystems in this file */
3483 addr_resolv_cleanup(void)
3485 vlan_name_lookup_cleanup();
3486 service_name_lookup_cleanup();
3488 ipx_name_lookup_cleanup();
3489 enterprises_cleanup();
3490 host_name_lookup_cleanup();
3494 str_to_ip(const char *str, void *dst)
3496 return ws_inet_pton4(str, (guint32 *)dst);
3500 str_to_ip6(const char *str, void *dst)
3502 return ws_inet_pton6(str, (ws_in6_addr *)dst);
3506 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3511 * indent-tabs-mode: nil
3514 * vi: set shiftwidth=4 tabstop=8 expandtab:
3515 * :indentSize=4:tabSize=8:noTabs=true: