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, cp must point to an address that consists
1172 * of exactly 6 bytes.
1173 * If "accept_mask" is TRUE, parse an up-to-6-byte sequence with an optional
1177 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1178 const gboolean accept_mask)
1185 for (i = 0; i < 6; i++) {
1186 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1187 if (!g_ascii_isxdigit(*cp))
1189 num = strtoul(cp, &p, 16);
1191 return FALSE; /* failed */
1193 return FALSE; /* not a valid octet */
1194 eth->addr[i] = (guint8) num;
1195 cp = p; /* skip past the number */
1197 /* OK, what character terminated the octet? */
1199 /* "/" - this has a mask. */
1201 /* Entries with masks are not allowed in this file. */
1204 cp++; /* skip past the '/' to get to the mask */
1205 if (!g_ascii_isdigit(*cp))
1206 return FALSE; /* no sign allowed */
1207 num = strtoul(cp, &p, 10);
1209 return FALSE; /* failed */
1210 cp = p; /* skip past the number */
1211 if (*cp != '\0' && !g_ascii_isspace(*cp))
1212 return FALSE; /* bogus terminator */
1213 if (num == 0 || num >= 48)
1214 return FALSE; /* bogus mask */
1215 /* Mask out the bits not covered by the mask */
1217 for (i = 0; num >= 8; i++, num -= 8)
1218 ; /* skip octets entirely covered by the mask */
1219 /* Mask out the first masked octet */
1220 eth->addr[i] &= (0xFF << (8 - num));
1222 /* Mask out completely-masked-out octets */
1228 /* We're at the end of the address, and there's no mask. */
1230 /* We got 3 bytes, so this is a manufacturer ID. */
1232 /* Manufacturer IDs are not allowed in this file */
1235 /* Indicate that this is a manufacturer ID (0 is not allowed
1242 /* We got 6 bytes, so this is a MAC address (48 is not allowed as a mask). */
1248 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1253 /* We don't know the separator used in this number; it can either
1254 be ':', '-', or '.'. */
1255 if (*cp != ':' && *cp != '-' && *cp != '.')
1257 sep = *cp; /* subsequent separators must be the same */
1259 /* It has to be the same as the first separator */
1271 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1272 const gboolean accept_mask)
1275 * See the ethers(4) or ethers(5) man page for ethers file format
1276 * (not available on all systems).
1277 * We allow both ethernet address separators (':' and '-'),
1278 * as well as Wireshark's '.' separator.
1283 line = g_strstrip(line);
1284 if (line[0] == '\0' || line[0] == '#')
1287 if ((cp = strchr(line, '#'))) {
1292 if ((cp = strtok(line, " \t")) == NULL)
1295 if (!parse_ether_address(cp, eth, mask, accept_mask))
1298 if ((cp = strtok(NULL, " \t")) == NULL)
1301 g_strlcpy(eth->name, cp, MAXNAMELEN);
1303 if ((cp = strtok(NULL, "\t")) != NULL)
1305 g_strlcpy(eth->longname, cp, MAXNAMELEN);
1307 /* Make the long name the short name */
1308 g_strlcpy(eth->longname, eth->name, MAXNAMELEN);
1313 } /* parse_ether_line */
1315 static FILE *eth_p = NULL;
1318 set_ethent(char *path)
1323 eth_p = ws_fopen(path, "r");
1336 get_ethent(unsigned int *mask, const gboolean accept_mask)
1340 static int size = 0;
1341 static char *buf = NULL;
1346 while (fgetline(&buf, &size, eth_p) >= 0) {
1347 if (parse_ether_line(buf, ð, mask, accept_mask) == 0) {
1357 get_ethbyaddr(const guint8 *addr)
1362 set_ethent(g_pethers_path);
1364 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1370 set_ethent(g_ethers_path);
1372 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1380 } /* get_ethbyaddr */
1382 static hashmanuf_t *
1383 manuf_hash_new_entry(const guint8 *addr, char* name, char* longname)
1386 hashmanuf_t *manuf_value;
1389 /* manuf needs only the 3 most significant octets of the ethernet address */
1390 manuf_key = (int *)wmem_new(wmem_epan_scope(), int);
1391 *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
1392 manuf_value = wmem_new(wmem_epan_scope(), hashmanuf_t);
1394 memcpy(manuf_value->addr, addr, 3);
1396 g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
1397 manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
1398 if (longname != NULL) {
1399 g_strlcpy(manuf_value->resolved_longname, longname, MAXNAMELEN);
1402 g_strlcpy(manuf_value->resolved_longname, name, MAXNAMELEN);
1406 manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
1407 manuf_value->resolved_name[0] = '\0';
1408 manuf_value->resolved_longname[0] = '\0';
1410 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1411 endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
1414 wmem_map_insert(manuf_hashtable, manuf_key, manuf_value);
1419 wka_hash_new_entry(const guint8 *addr, char* name)
1423 wka_key = (guint8 *)wmem_alloc(wmem_epan_scope(), 6);
1424 memcpy(wka_key, addr, 6);
1426 wmem_map_insert(wka_hashtable, wka_key, wmem_strdup(wmem_epan_scope(), name));
1430 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name, gchar *longname)
1435 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1436 manuf_hash_new_entry(addr, name, longname);
1440 /* This is a well-known MAC address; add it to the Ethernet hash table */
1441 add_eth_name(addr, name);
1445 /* This is a range of well-known addresses; add it to the well-known-address table */
1446 wka_hash_new_entry(addr, name);
1449 } /* add_manuf_name */
1451 static hashmanuf_t *
1452 manuf_name_lookup(const guint8 *addr)
1454 gint32 manuf_key = 0;
1456 hashmanuf_t *manuf_value;
1458 /* manuf needs only the 3 most significant octets of the ethernet address */
1459 manuf_key = addr[0];
1460 manuf_key = manuf_key<<8;
1462 manuf_key = manuf_key | oct;
1463 manuf_key = manuf_key<<8;
1465 manuf_key = manuf_key | oct;
1468 /* first try to find a "perfect match" */
1469 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1470 if (manuf_value != NULL) {
1474 /* Mask out the broadcast/multicast flag but not the locally
1475 * administered flag as locally administered means: not assigned
1476 * by the IEEE but the local administrator instead.
1477 * 0x01 multicast / broadcast bit
1478 * 0x02 locally administered bit */
1479 if ((manuf_key & 0x00010000) != 0) {
1480 manuf_key &= 0x00FEFFFF;
1481 manuf_value = (hashmanuf_t*)wmem_map_lookup(manuf_hashtable, &manuf_key);
1482 if (manuf_value != NULL) {
1487 /* Add the address as a hex string */
1488 return manuf_hash_new_entry(addr, NULL, NULL);
1490 } /* manuf_name_lookup */
1493 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1495 guint8 masked_addr[6];
1500 if (wka_hashtable == NULL) {
1503 /* Get the part of the address covered by the mask. */
1504 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1505 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1506 /* Mask out the first masked octet */
1507 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1509 /* Zero out completely-masked-out octets */
1513 name = (gchar *)wmem_map_lookup(wka_hashtable, masked_addr);
1517 } /* wka_name_lookup */
1520 guint get_hash_ether_status(hashether_t* ether)
1522 return ether->status;
1525 char* get_hash_ether_hexaddr(hashether_t* ether)
1527 return ether->hexaddr;
1530 char* get_hash_ether_resolved_name(hashether_t* ether)
1532 return ether->resolved_name;
1536 eth_addr_hash(gconstpointer key)
1538 return wmem_strong_hash((const guint8 *)key, 6);
1542 eth_addr_cmp(gconstpointer a, gconstpointer b)
1544 return (memcmp(a, b, 6) == 0);
1548 initialize_ethers(void)
1553 /* hash table initialization */
1554 wka_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1555 manuf_hashtable = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
1556 eth_hashtable = wmem_map_new(wmem_epan_scope(), eth_addr_hash, eth_addr_cmp);
1558 /* Compute the pathname of the ethers file. */
1559 if (g_ethers_path == NULL) {
1560 g_ethers_path = g_build_filename(get_systemfile_dir(), ENAME_ETHERS, NULL);
1563 /* Set g_pethers_path here, but don't actually do anything
1564 * with it. It's used in get_ethbyaddr().
1566 if (g_pethers_path == NULL)
1567 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1569 /* Compute the pathname of the manuf file */
1570 if (g_manuf_path == NULL)
1571 g_manuf_path = get_datafile_path(ENAME_MANUF);
1573 /* Read it and initialize the hash table */
1574 set_ethent(g_manuf_path);
1575 while ((eth = get_ethent(&mask, TRUE))) {
1576 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1580 /* Compute the pathname of the wka file */
1581 if (g_wka_path == NULL)
1582 g_wka_path = get_datafile_path(ENAME_WKA);
1584 /* Read it and initialize the hash table */
1585 set_ethent(g_wka_path);
1586 while ((eth = get_ethent(&mask, TRUE))) {
1587 add_manuf_name(eth->addr, mask, eth->name, eth->longname);
1591 } /* initialize_ethers */
1594 ethers_cleanup(void)
1596 g_free(g_ethers_path);
1597 g_ethers_path = NULL;
1598 g_free(g_pethers_path);
1599 g_pethers_path = NULL;
1600 g_free(g_manuf_path);
1601 g_manuf_path = NULL;
1606 /* Resolve ethernet address */
1607 static hashether_t *
1608 eth_addr_resolve(hashether_t *tp) {
1610 hashmanuf_t *manuf_value;
1611 const guint8 *addr = tp->addr;
1613 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1614 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1615 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1622 /* Unknown name. Try looking for it in the well-known-address
1623 tables for well-known address ranges smaller than 2^24. */
1626 /* Only the topmost 5 bytes participate fully */
1627 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1628 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1629 name, addr[5] & (0xFF >> mask));
1630 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1637 /* Only the topmost 4 bytes participate fully */
1638 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1639 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1640 name, addr[4] & (0xFF >> mask), addr[5]);
1641 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1648 /* Only the topmost 3 bytes participate fully */
1649 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1650 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1651 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1652 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1657 /* Now try looking in the manufacturer table. */
1658 manuf_value = manuf_name_lookup(addr);
1659 if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
1660 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1661 manuf_value->resolved_name, addr[3], addr[4], addr[5]);
1662 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1666 /* Now try looking for it in the well-known-address
1667 tables for well-known address ranges larger than 2^24. */
1670 /* Only the topmost 2 bytes participate fully */
1671 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1672 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1673 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1675 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1682 /* Only the topmost byte participates fully */
1683 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1684 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1685 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1687 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1694 /* Not even the topmost byte participates fully */
1695 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1696 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1697 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1698 addr[3], addr[4], addr[5]);
1699 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1702 } while (--mask); /* Work down to the last bit */
1704 /* No match whatsoever. */
1705 set_address(ðer_addr, AT_ETHER, 6, addr);
1706 address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
1707 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1710 g_assert_not_reached();
1711 } /* eth_addr_resolve */
1713 static hashether_t *
1714 eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
1719 tp = wmem_new(wmem_epan_scope(), hashether_t);
1720 memcpy(tp->addr, addr, sizeof(tp->addr));
1721 tp->status = HASHETHER_STATUS_UNRESOLVED;
1722 /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
1723 endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
1725 tp->resolved_name[0] = '\0';
1728 eth_addr_resolve(tp);
1730 wmem_map_insert(eth_hashtable, tp->addr, tp);
1733 } /* eth_hash_new_entry */
1735 static hashether_t *
1736 add_eth_name(const guint8 *addr, const gchar *name)
1740 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1743 tp = eth_hash_new_entry(addr, FALSE);
1746 if (strcmp(tp->resolved_name, name) != 0) {
1747 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1748 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1749 new_resolved_objects = TRUE;
1753 } /* add_eth_name */
1755 static hashether_t *
1756 eth_name_lookup(const guint8 *addr, const gboolean resolve)
1760 tp = (hashether_t *)wmem_map_lookup(eth_hashtable, addr);
1763 tp = eth_hash_new_entry(addr, resolve);
1765 if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)) {
1766 eth_addr_resolve(tp); /* Found but needs to be resolved */
1772 } /* eth_name_lookup */
1777 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1780 * We allow three address separators (':', '-', and '.'),
1781 * as well as no separators
1785 guint32 a, a0, a1, a2, a3;
1786 gboolean found_single_number = FALSE;
1788 if ((cp = strchr(line, '#')))
1791 if ((cp = strtok(line, " \t\n")) == NULL)
1794 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1795 * fill a and found_single_number is TRUE,
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.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1801 if (sscanf(cp, "%x", &a) == 1) {
1802 found_single_number = TRUE;
1811 if ((cp = strtok(NULL, " \t\n")) == NULL)
1814 if (found_single_number) {
1818 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1821 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1825 } /* parse_ipxnets_line */
1827 static FILE *ipxnet_p = NULL;
1830 set_ipxnetent(char *path)
1835 ipxnet_p = ws_fopen(path, "r");
1851 static ipxnet_t ipxnet;
1852 static int size = 0;
1853 static char *buf = NULL;
1855 if (ipxnet_p == NULL)
1858 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1859 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1866 } /* get_ipxnetent */
1869 get_ipxnetbyaddr(guint32 addr)
1873 set_ipxnetent(g_ipxnets_path);
1875 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1877 if (ipxnet == NULL) {
1880 set_ipxnetent(g_pipxnets_path);
1882 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1890 } /* get_ipxnetbyaddr */
1893 initialize_ipxnets(void)
1895 /* Compute the pathname of the ipxnets file.
1897 * XXX - is there a notion of an "ipxnets file" in any flavor of
1898 * UNIX, or with any add-on Netware package for UNIX? If not,
1899 * should the UNIX version of the ipxnets file be in the datafile
1900 * directory as well?
1902 if (g_ipxnets_path == NULL) {
1903 g_ipxnets_path = wmem_strdup_printf(wmem_epan_scope(), "%s" G_DIR_SEPARATOR_S "%s",
1904 get_systemfile_dir(), ENAME_IPXNETS);
1907 /* Set g_pipxnets_path here, but don't actually do anything
1908 * with it. It's used in get_ipxnetbyaddr().
1910 if (g_pipxnets_path == NULL)
1911 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1913 } /* initialize_ipxnets */
1916 ipx_name_lookup_cleanup(void)
1918 ipxnet_hash_table = NULL;
1919 g_free(g_pipxnets_path);
1920 g_pipxnets_path = NULL;
1924 ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
1929 tp = (hashipxnet_t *)wmem_map_lookup(ipxnet_hash_table, &addr);
1933 key = (int *)wmem_new(wmem_epan_scope(), int);
1935 tp = wmem_new(wmem_epan_scope(), hashipxnet_t);
1936 wmem_map_insert(ipxnet_hash_table, key, tp);
1938 return wmem_strdup(allocator, tp->name);
1941 /* fill in a new entry */
1945 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
1947 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
1950 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
1953 return wmem_strdup(allocator, tp->name);
1955 } /* ipxnet_name_lookup */
1959 parse_vlan_line(char *line, vlan_t *vlan)
1964 if ((cp = strchr(line, '#')))
1967 if ((cp = strtok(line, " \t\n")) == NULL)
1970 if (sscanf(cp, "%" G_GUINT16_FORMAT, &id) == 1) {
1977 if ((cp = strtok(NULL, "\t\n")) == NULL)
1980 g_strlcpy(vlan->name, cp, MAXVLANNAMELEN);
1984 } /* parse_vlan_line */
1986 static FILE *vlan_p = NULL;
1989 set_vlanent(char *path)
1994 vlan_p = ws_fopen(path, "r");
2011 static int size = 0;
2012 static char *buf = NULL;
2017 while (fgetline(&buf, &size, vlan_p) >= 0) {
2018 if (parse_vlan_line(buf, &vlan) == 0) {
2028 get_vlannamebyid(guint16 id)
2032 set_vlanent(g_pvlan_path);
2034 while (((vlan = get_vlanent()) != NULL) && (id != vlan->id) ) ;
2043 } /* get_vlannamebyid */
2046 initialize_vlans(void)
2048 g_assert(vlan_hash_table == NULL);
2049 vlan_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
2051 /* Set g_pvlan_path here, but don't actually do anything
2052 * with it. It's used in get_vlannamebyid()
2054 if (g_pvlan_path == NULL)
2055 g_pvlan_path = get_persconffile_path(ENAME_VLANS, FALSE);
2057 } /* initialize_vlans */
2060 vlan_name_lookup_cleanup(void)
2062 vlan_hash_table = NULL;
2063 g_free(g_pvlan_path);
2064 g_pvlan_path = NULL;
2067 static const gchar *
2068 vlan_name_lookup(const guint id)
2073 tp = (hashvlan_t *)wmem_map_lookup(vlan_hash_table, &id);
2077 key = (int *)wmem_new(wmem_epan_scope(), int);
2079 tp = wmem_new(wmem_epan_scope(), hashvlan_t);
2080 wmem_map_insert(vlan_hash_table, key, tp);
2085 /* fill in a new entry */
2089 if ( (vlan = get_vlannamebyid(id)) == NULL) {
2091 g_snprintf(tp->name, MAXVLANNAMELEN, "<%u>", id);
2094 g_strlcpy(tp->name, vlan->name, MAXVLANNAMELEN);
2099 } /* vlan_name_lookup */
2103 read_hosts_file (const char *hostspath, gboolean store_entries)
2111 ws_in6_addr ip6_addr;
2113 gboolean is_ipv6, entry_found = FALSE;
2116 * See the hosts(4) or hosts(5) man page for hosts file format
2117 * (not available on all systems).
2119 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2122 while (fgetline(&line, &size, hf) >= 0) {
2123 if ((cp = strchr(line, '#')))
2126 if ((cp = strtok(line, " \t")) == NULL)
2127 continue; /* no tokens in the line */
2129 if (ws_inet_pton6(cp, &host_addr.ip6_addr)) {
2132 } else if (ws_inet_pton4(cp, &host_addr.ip4_addr)) {
2139 if ((cp = strtok(NULL, " \t")) == NULL)
2140 continue; /* no host name */
2143 if (store_entries) {
2145 add_ipv6_name(&host_addr.ip6_addr, cp);
2147 add_ipv4_name(host_addr.ip4_addr, cp);
2151 wmem_free(wmem_epan_scope(), line);
2154 return entry_found ? TRUE : FALSE;
2155 } /* read_hosts_file */
2158 add_hosts_file (const char *hosts_file)
2160 gboolean found = FALSE;
2166 if (!extra_hosts_files)
2167 extra_hosts_files = g_ptr_array_new();
2169 for (i = 0; i < extra_hosts_files->len; i++) {
2170 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2175 g_ptr_array_add(extra_hosts_files, wmem_strdup(wmem_epan_scope(), hosts_file));
2176 return read_hosts_file (hosts_file, FALSE);
2182 add_ip_name_from_string (const char *addr, const char *name)
2186 ws_in6_addr ip6_addr;
2189 resolved_ipv4_t *resolved_ipv4_entry;
2190 resolved_ipv6_t *resolved_ipv6_entry;
2192 if (ws_inet_pton6(addr, &host_addr.ip6_addr)) {
2194 } else if (ws_inet_pton4(addr, &host_addr.ip4_addr)) {
2201 resolved_ipv6_entry = wmem_new(wmem_epan_scope(), resolved_ipv6_t);
2202 memcpy(&(resolved_ipv6_entry->ip6_addr), &host_addr.ip6_addr, 16);
2203 g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
2204 wmem_list_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
2206 resolved_ipv4_entry = wmem_new(wmem_epan_scope(), resolved_ipv4_t);
2207 resolved_ipv4_entry->host_addr = host_addr.ip4_addr;
2208 g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
2209 wmem_list_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
2213 } /* add_ip_name_from_string */
2216 * Add the resolved addresses that are in use to the list used to create the NRB
2219 ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2221 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2222 hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
2224 if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2225 lists->ipv4_addr_list = g_list_prepend(lists->ipv4_addr_list, ipv4_hash_table_entry);
2231 * Add the resolved addresses that are in use to the list used to create the NRB
2235 ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
2237 addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
2238 hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
2240 if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == USED_AND_RESOLVED_MASK) {
2241 lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
2247 get_addrinfo_list(void)
2249 if (ipv4_hash_table) {
2250 wmem_map_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
2253 if (ipv6_hash_table) {
2254 wmem_map_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
2257 return &addrinfo_lists;
2260 /* Read in a list of subnet definition - name pairs.
2261 * <line> = <comment> | <entry> | <whitespace>
2262 * <comment> = <whitespace>#<any>
2263 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2264 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2265 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2266 * <subnet_mask_length> is a decimal 1-31
2267 * <subnet_name> is a string containing no whitespace.
2268 * <whitespace> = (space | tab)+
2269 * Any malformed entries are ignored.
2270 * Any trailing data after the subnet_name is ignored.
2275 read_subnets_file (const char *subnetspath)
2281 guint32 host_addr; /* IPv4 ONLY */
2284 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2287 while (fgetline(&line, &size, hf) >= 0) {
2288 if ((cp = strchr(line, '#')))
2291 if ((cp = strtok(line, " \t")) == NULL)
2292 continue; /* no tokens in the line */
2295 /* Expected format is <IP4 address>/<subnet length> */
2296 cp2 = strchr(cp, '/');
2301 *cp2 = '\0'; /* Cut token */
2304 /* Check if this is a valid IPv4 address */
2305 if (!str_to_ip(cp, &host_addr)) {
2309 if (!ws_strtou8(cp2, NULL, &mask_length) || mask_length == 0 || mask_length > 32) {
2310 continue; /* invalid mask length */
2313 if ((cp = strtok(NULL, " \t")) == NULL)
2314 continue; /* no subnet name */
2316 subnet_entry_set(host_addr, mask_length, cp);
2318 wmem_free(wmem_epan_scope(), line);
2322 } /* read_subnets_file */
2324 static subnet_entry_t
2325 subnet_lookup(const guint32 addr)
2327 subnet_entry_t subnet_entry;
2330 /* Search mask lengths linearly, longest first */
2332 i = SUBNETLENGTHSIZE;
2333 while(have_subnet_entry && i > 0) {
2334 guint32 masked_addr;
2335 subnet_length_entry_t* length_entry;
2337 /* Note that we run from 31 (length 32) to 0 (length 1) */
2339 g_assert(i < SUBNETLENGTHSIZE);
2342 length_entry = &subnet_length_entries[i];
2344 if (NULL != length_entry->subnet_addresses) {
2345 sub_net_hashipv4_t * tp;
2348 masked_addr = addr & length_entry->mask;
2349 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2351 tp = length_entry->subnet_addresses[hash_idx];
2352 while(tp != NULL && tp->addr != masked_addr) {
2357 subnet_entry.mask = length_entry->mask;
2358 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2359 subnet_entry.name = tp->name;
2360 return subnet_entry;
2365 subnet_entry.mask = 0;
2366 subnet_entry.mask_length = 0;
2367 subnet_entry.name = NULL;
2369 return subnet_entry;
2372 /* Add a subnet-definition - name pair to the set.
2373 * The definition is taken by masking the address passed in with the mask of the
2377 subnet_entry_set(guint32 subnet_addr, const guint8 mask_length, const gchar* name)
2379 subnet_length_entry_t* entry;
2380 sub_net_hashipv4_t * tp;
2383 g_assert(mask_length > 0 && mask_length <= 32);
2385 entry = &subnet_length_entries[mask_length - 1];
2387 subnet_addr &= entry->mask;
2389 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2391 if (NULL == entry->subnet_addresses) {
2392 entry->subnet_addresses = (sub_net_hashipv4_t**)wmem_alloc0(wmem_epan_scope(), sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
2395 if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
2396 sub_net_hashipv4_t * new_tp;
2399 if (tp->addr == subnet_addr) {
2400 return; /* XXX provide warning that an address was repeated? */
2406 new_tp = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2410 tp = entry->subnet_addresses[hash_idx] = wmem_new(wmem_epan_scope(), sub_net_hashipv4_t);
2414 tp->addr = subnet_addr;
2415 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2416 have_subnet_entry = TRUE;
2420 subnet_name_lookup_init(void)
2425 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2426 guint32 length = i + 1;
2428 subnet_length_entries[i].subnet_addresses = NULL;
2429 subnet_length_entries[i].mask_length = length;
2430 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2433 /* Check profile directory before personal configuration */
2434 subnetspath = get_persconffile_path(ENAME_SUBNETS, TRUE);
2435 if (!read_subnets_file(subnetspath)) {
2436 if (errno != ENOENT) {
2437 report_open_failure(subnetspath, errno, FALSE);
2440 g_free(subnetspath);
2441 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2442 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2443 report_open_failure(subnetspath, errno, FALSE);
2446 g_free(subnetspath);
2449 * Load the global subnets file, if we have one.
2451 subnetspath = get_datafile_path(ENAME_SUBNETS);
2452 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2453 report_open_failure(subnetspath, errno, FALSE);
2455 g_free(subnetspath);
2458 /* SS7 PC Name Resolution Portion */
2459 static hashss7pc_t *
2460 new_ss7pc(const guint8 ni, const guint32 pc)
2462 hashss7pc_t *tp = wmem_new(wmem_epan_scope(), hashss7pc_t);
2463 tp->id = (ni<<24) + (pc&0xffffff);
2464 tp->pc_addr[0] = '\0';
2470 static hashss7pc_t *
2471 host_lookup_ss7pc(const guint8 ni, const guint32 pc)
2473 hashss7pc_t * volatile tp;
2476 id = (ni<<24) + (pc&0xffffff);
2478 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2480 tp = new_ss7pc(ni, pc);
2481 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2487 void fill_unresolved_ss7pc(const gchar * pc_addr, const guint8 ni, const guint32 pc)
2489 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2491 g_strlcpy(tp->pc_addr, pc_addr, MAXNAMELEN);
2495 get_hostname_ss7pc(const guint8 ni, const guint32 pc)
2497 hashss7pc_t *tp = host_lookup_ss7pc(ni, pc);
2499 /* never resolved yet*/
2500 if (tp->pc_addr[0] == '\0')
2503 /* Don't have name in file */
2504 if (tp->name[0] == '\0')
2507 if (!gbl_resolv_flags.ss7pc_name)
2514 add_ss7pc_name(const guint8 ni, guint32 pc, const gchar *name)
2519 if (!name || name[0] == '\0')
2522 id = (ni<<24) + (pc&0xffffff);
2523 tp = (hashss7pc_t *)wmem_map_lookup(ss7pc_hash_table, GUINT_TO_POINTER(id));
2525 tp = new_ss7pc(ni, pc);
2526 wmem_map_insert(ss7pc_hash_table, GUINT_TO_POINTER(id), tp);
2529 if (g_ascii_strcasecmp(tp->name, name)) {
2530 g_strlcpy(tp->name, name, MAXNAMELEN);
2535 read_ss7pcs_file(const char *ss7pcspath)
2543 gboolean entry_found = FALSE;
2546 * File format is Network Indicator (decimal)<dash>Point Code (Decimal)<tab/space>Hostname
2548 if ((hf = ws_fopen(ss7pcspath, "r")) == NULL)
2551 while (fgetline(&line, &size, hf) >= 0) {
2552 if ((cp = strchr(line, '#')))
2555 if ((cp = strtok(line, "-")) == NULL)
2556 continue; /*no ni-pc separator*/
2557 if (!ws_strtou8(cp, NULL, &ni))
2562 if ((cp = strtok(NULL, " \t")) == NULL)
2563 continue; /* no tokens for pc and name */
2564 if (!ws_strtou32(cp, NULL, &pc))
2569 if ((cp = strtok(NULL, " \t")) == NULL)
2570 continue; /* no host name */
2573 add_ss7pc_name(ni, pc, cp);
2575 wmem_free(wmem_epan_scope(), line);
2578 return entry_found ? TRUE : FALSE;
2582 ss7pc_name_lookup_init(void)
2586 g_assert(ss7pc_hash_table == NULL);
2588 ss7pc_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2591 * Load the user's ss7pcs file
2593 ss7pcspath = get_persconffile_path(ENAME_SS7PCS, TRUE);
2594 if (!read_ss7pcs_file(ss7pcspath) && errno != ENOENT) {
2595 report_open_failure(ss7pcspath, errno, FALSE);
2600 /* SS7PC Name Resolution End*/
2604 * External Functions
2608 addr_resolve_pref_init(module_t *nameres)
2610 prefs_register_bool_preference(nameres, "mac_name",
2611 "Resolve MAC addresses",
2612 "Resolve Ethernet MAC addresses to host names from the preferences"
2613 " or system's Ethers file, or to a manufacturer based name.",
2614 &gbl_resolv_flags.mac_name);
2616 prefs_register_bool_preference(nameres, "transport_name",
2617 "Resolve transport names",
2618 "Resolve TCP/UDP ports into service names",
2619 &gbl_resolv_flags.transport_name);
2621 prefs_register_bool_preference(nameres, "network_name",
2622 "Resolve network (IP) addresses",
2623 "Resolve IPv4, IPv6, and IPX addresses into host names."
2624 " The next set of check boxes determines how name resolution should be performed."
2625 " If no other options are checked name resolution is made from Wireshark's host file,"
2626 " capture file name resolution blocks and DNS packets in the capture.",
2627 &gbl_resolv_flags.network_name);
2629 prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
2630 "Use captured DNS packet data for address resolution",
2631 "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
2632 &gbl_resolv_flags.dns_pkt_addr_resolution);
2635 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2636 "Use an external network name resolver",
2637 "Use your system's configured name resolver"
2638 " (usually DNS) to resolve network names."
2639 " Only applies when network name resolution"
2641 &gbl_resolv_flags.use_external_net_name_resolver);
2643 prefs_register_obsolete_preference(nameres, "concurrent_dns");
2645 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2646 "Maximum concurrent requests",
2647 "The maximum number of DNS requests that may"
2648 " be active at any time. A large value (many"
2649 " thousands) might overload the network or make"
2650 " your DNS server behave badly.",
2652 &name_resolve_concurrency);
2654 prefs_register_static_text_preference(nameres, "use_external_name_resolver",
2655 "Use an external network name resolver: N/A",
2656 "Support for using a concurrent external name resolver was not"
2657 " compiled into this version of Wireshark");
2660 prefs_register_bool_preference(nameres, "hosts_file_handling",
2661 "Only use the profile \"hosts\" file",
2662 "By default \"hosts\" files will be loaded from multiple sources."
2663 " Checking this box only loads the \"hosts\" in the current profile.",
2664 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2666 prefs_register_bool_preference(nameres, "vlan_name",
2668 "Resolve VLAN IDs to network names from the preferences \"vlans\" file."
2669 " Format of the file is: \"ID<Tab>Name\"."
2670 " One line per VLAN, e.g.: 1 Management",
2671 &gbl_resolv_flags.vlan_name);
2673 prefs_register_bool_preference(nameres, "ss7_pc_name",
2675 "Resolve SS7 Point Codes to node names from the profiles \"ss7pcs\" file."
2676 " Format of the file is: \"Network_Indicator<Dash>PC_Decimal<Tab>Name\"."
2677 " One line per Point Code, e.g.: 2-1234 MyPointCode1",
2678 &gbl_resolv_flags.ss7pc_name);
2683 disable_name_resolution(void) {
2684 gbl_resolv_flags.mac_name = FALSE;
2685 gbl_resolv_flags.network_name = FALSE;
2686 gbl_resolv_flags.transport_name = FALSE;
2687 gbl_resolv_flags.dns_pkt_addr_resolution = FALSE;
2688 gbl_resolv_flags.use_external_net_name_resolver = FALSE;
2689 gbl_resolv_flags.vlan_name = FALSE;
2690 gbl_resolv_flags.ss7pc_name = FALSE;
2695 host_name_lookup_process(void) {
2696 async_dns_queue_msg_t *caqm;
2697 struct timeval tv = { 0, 0 };
2700 gboolean nro = new_resolved_objects;
2701 wmem_list_frame_t* head;
2703 new_resolved_objects = FALSE;
2704 nro |= maxmind_db_lookup_process();
2706 if (!async_dns_initialized)
2707 /* c-ares not initialized. Bail out and cancel timers. */
2710 head = wmem_list_head(async_dns_queue_head);
2712 while (head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2713 caqm = (async_dns_queue_msg_t *)wmem_list_frame_data(head);
2714 wmem_list_remove_frame(async_dns_queue_head, head);
2715 if (caqm->family == AF_INET) {
2716 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2717 c_ares_ghba_cb, caqm);
2718 async_dns_in_flight++;
2719 } else if (caqm->family == AF_INET6) {
2720 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(ws_in6_addr),
2721 AF_INET6, c_ares_ghba_cb, caqm);
2722 async_dns_in_flight++;
2725 head = wmem_list_head(async_dns_queue_head);
2730 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2732 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2733 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
2736 ares_process(ghba_chan, &rfds, &wfds);
2739 /* Any new entries? */
2744 _host_name_lookup_cleanup(void) {
2745 async_dns_queue_head = NULL;
2747 if (async_dns_initialized) {
2748 ares_destroy(ghba_chan);
2749 ares_destroy(ghbn_chan);
2751 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2752 ares_library_cleanup();
2754 async_dns_initialized = FALSE;
2760 host_name_lookup_process(void) {
2761 gboolean nro = new_resolved_objects;
2763 new_resolved_objects = FALSE;
2765 nro |= maxmind_db_lookup_process();
2771 _host_name_lookup_cleanup(void) {
2774 #endif /* HAVE_C_ARES */
2777 get_hostname(const guint addr)
2779 /* XXX why do we call this if we're not resolving? To create hash entries?
2782 hashipv4_t *tp = host_lookup(addr);
2784 if (!gbl_resolv_flags.network_name)
2787 tp->flags |= RESOLVED_ADDRESS_USED;
2792 /* -------------------------- */
2795 get_hostname6(const ws_in6_addr *addr)
2797 /* XXX why do we call this if we're not resolving? To create hash entries?
2800 hashipv6_t *tp = host_lookup6(addr);
2802 if (!gbl_resolv_flags.network_name)
2805 tp->flags |= RESOLVED_ADDRESS_USED;
2810 /* -------------------------- */
2812 add_ipv4_name(const guint addr, const gchar *name)
2817 * Don't add zero-length names; apparently, some resolvers will return
2818 * them if they get them from DNS.
2820 if (!name || name[0] == '\0')
2823 tp = (hashipv4_t *)wmem_map_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
2825 tp = new_ipv4(addr);
2826 wmem_map_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
2829 if (g_ascii_strcasecmp(tp->name, name)) {
2830 g_strlcpy(tp->name, name, MAXNAMELEN);
2831 new_resolved_objects = TRUE;
2833 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2834 } /* add_ipv4_name */
2836 /* -------------------------- */
2838 add_ipv6_name(const ws_in6_addr *addrp, const gchar *name)
2843 * Don't add zero-length names; apparently, some resolvers will return
2844 * them if they get them from DNS.
2846 if (!name || name[0] == '\0')
2849 tp = (hashipv6_t *)wmem_map_lookup(ipv6_hash_table, addrp);
2851 ws_in6_addr *addr_key;
2853 addr_key = wmem_new(wmem_epan_scope(), ws_in6_addr);
2854 tp = new_ipv6(addrp);
2855 memcpy(addr_key, addrp, 16);
2856 wmem_map_insert(ipv6_hash_table, addr_key, tp);
2859 if (g_ascii_strcasecmp(tp->name, name)) {
2860 g_strlcpy(tp->name, name, MAXNAMELEN);
2861 new_resolved_objects = TRUE;
2863 tp->flags |= TRIED_RESOLVE_ADDRESS|NAME_RESOLVED;
2864 } /* add_ipv6_name */
2867 add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
2869 resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
2871 add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
2875 add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
2877 resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
2879 add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
2883 add_manually_resolved(void)
2885 if (manually_resolved_ipv4_list) {
2886 wmem_list_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
2889 if (manually_resolved_ipv6_list) {
2890 wmem_list_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
2895 host_name_lookup_init(void)
2900 g_assert(ipxnet_hash_table == NULL);
2901 ipxnet_hash_table = wmem_map_new(wmem_epan_scope(), g_int_hash, g_int_equal);
2903 g_assert(ipv4_hash_table == NULL);
2904 ipv4_hash_table = wmem_map_new(wmem_epan_scope(), g_direct_hash, g_direct_equal);
2906 g_assert(ipv6_hash_table == NULL);
2907 ipv6_hash_table = wmem_map_new(wmem_epan_scope(), ipv6_oat_hash, ipv6_equal);
2910 g_assert(async_dns_queue_head == NULL);
2911 async_dns_queue_head = wmem_list_new(wmem_epan_scope());
2914 if (manually_resolved_ipv4_list == NULL)
2915 manually_resolved_ipv4_list = wmem_list_new(wmem_epan_scope());
2917 if (manually_resolved_ipv6_list == NULL)
2918 manually_resolved_ipv6_list = wmem_list_new(wmem_epan_scope());
2921 * Load the global hosts file, if we have one.
2923 if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
2924 hostspath = get_datafile_path(ENAME_HOSTS);
2925 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2926 report_open_failure(hostspath, errno, FALSE);
2931 * Load the user's hosts file no matter what, if they have one.
2933 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2934 if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
2935 report_open_failure(hostspath, errno, FALSE);
2939 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2940 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2942 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2943 async_dns_initialized = TRUE;
2945 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2949 #endif /* HAVE_C_ARES */
2951 if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
2952 for (i = 0; i < extra_hosts_files->len; i++) {
2953 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
2957 subnet_name_lookup_init();
2959 add_manually_resolved();
2961 ss7pc_name_lookup_init();
2965 host_name_lookup_cleanup(void)
2968 sub_net_hashipv4_t *entry, *next_entry;
2970 _host_name_lookup_cleanup();
2972 ipxnet_hash_table = NULL;
2973 ipv4_hash_table = NULL;
2974 ipv6_hash_table = NULL;
2975 ss7pc_hash_table = NULL;
2977 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2978 if (subnet_length_entries[i].subnet_addresses != NULL) {
2979 for (j = 0; j < HASHHOSTSIZE; j++) {
2980 for (entry = subnet_length_entries[i].subnet_addresses[j];
2981 entry != NULL; entry = next_entry) {
2982 next_entry = entry->next;
2983 wmem_free(wmem_epan_scope(), entry);
2986 wmem_free(wmem_epan_scope(), subnet_length_entries[i].subnet_addresses);
2987 subnet_length_entries[i].subnet_addresses = NULL;
2991 have_subnet_entry = FALSE;
2992 new_resolved_objects = FALSE;
2996 void host_name_lookup_reset(void)
2998 host_name_lookup_cleanup();
2999 host_name_lookup_init();
3003 manually_resolve_cleanup(void)
3005 if (manually_resolved_ipv4_list) {
3006 wmem_destroy_list(manually_resolved_ipv4_list);
3007 manually_resolved_ipv4_list = NULL;
3009 if (manually_resolved_ipv6_list) {
3010 wmem_destroy_list(manually_resolved_ipv6_list);
3011 manually_resolved_ipv6_list = NULL;
3016 udp_port_to_display(wmem_allocator_t *allocator, guint port)
3019 if (!gbl_resolv_flags.transport_name) {
3020 return wmem_utoa(allocator, port);
3023 return wmem_strdup(allocator, serv_name_lookup(PT_UDP, port));
3025 } /* udp_port_to_display */
3028 dccp_port_to_display(wmem_allocator_t *allocator, guint port)
3031 if (!gbl_resolv_flags.transport_name) {
3032 return wmem_utoa(allocator, port);
3035 return wmem_strdup(allocator, serv_name_lookup(PT_DCCP, port));
3037 } /* dccp_port_to_display */
3040 tcp_port_to_display(wmem_allocator_t *allocator, guint port)
3043 if (!gbl_resolv_flags.transport_name) {
3044 return wmem_utoa(allocator, port);
3047 return wmem_strdup(allocator, serv_name_lookup(PT_TCP, port));
3049 } /* tcp_port_to_display */
3052 sctp_port_to_display(wmem_allocator_t *allocator, guint port)
3055 if (!gbl_resolv_flags.transport_name) {
3056 return wmem_utoa(allocator, port);
3059 return wmem_strdup(allocator, serv_name_lookup(PT_SCTP, port));
3061 } /* sctp_port_to_display */
3064 port_with_resolution_to_str(wmem_allocator_t *scope, port_type proto, guint port)
3066 const gchar *port_str;
3068 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3069 /* No name resolution support, just return port string */
3070 return wmem_strdup_printf(scope, "%u", port);
3072 port_str = serv_name_lookup(proto, port);
3074 return wmem_strdup_printf(scope, "%s (%u)", port_str, port);
3078 port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type proto, guint port)
3080 const gchar *port_str;
3082 if (!gbl_resolv_flags.transport_name || (proto == PT_NONE)) {
3083 /* No name resolution support, just return port string */
3084 return g_snprintf(buf, buf_size, "%u", port);
3086 port_str = serv_name_lookup(proto, port);
3088 return g_snprintf(buf, buf_size, "%s (%u)", port_str, port);
3092 get_ether_name(const guint8 *addr)
3095 gboolean resolve = gbl_resolv_flags.mac_name;
3097 tp = eth_name_lookup(addr, resolve);
3099 return resolve ? tp->resolved_name : tp->hexaddr;
3101 } /* get_ether_name */
3104 tvb_get_ether_name(tvbuff_t *tvb, gint offset)
3106 return get_ether_name(tvb_get_ptr(tvb, offset, 6));
3109 /* Look for a (non-dummy) ether name in the hash, and return it if found.
3110 * If it's not found, simply return NULL.
3113 get_ether_name_if_known(const guint8 *addr)
3117 /* Initialize ether structs if we're the first
3118 * ether-related function called */
3119 if (!gbl_resolv_flags.mac_name)
3122 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3123 tp = eth_name_lookup(addr, TRUE);
3124 g_assert(tp != NULL);
3126 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3127 /* Name is from an ethers file */
3128 return tp->resolved_name;
3131 /* Name was created */
3137 add_ether_byip(const guint ip, const guint8 *eth)
3141 /* first check that IP address can be resolved */
3142 if (!gbl_resolv_flags.network_name)
3145 tp = host_lookup(ip);
3148 * Was this IP address resolved to a host name?
3150 if (tp->flags & NAME_RESOLVED) {
3152 * Yes, so add an entry in the ethers hashtable resolving
3153 * the MAC address to that name.
3155 add_eth_name(eth, tp->name);
3158 } /* add_ether_byip */
3161 ipxnet_to_str_punct(wmem_allocator_t *allocator, const guint32 ad, const char punct)
3163 gchar *buf = (gchar *)wmem_alloc(allocator, 12);
3165 *dword_to_hex_punct(buf, ad, punct) = '\0';
3170 get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
3173 if (!gbl_resolv_flags.network_name) {
3174 return ipxnet_to_str_punct(allocator, addr, '\0');
3177 return ipxnet_name_lookup(allocator, addr);
3179 } /* get_ipxnet_name */
3182 get_vlan_name(wmem_allocator_t *allocator, const guint16 id)
3185 if (!gbl_resolv_flags.vlan_name) {
3189 return wmem_strdup(allocator, vlan_name_lookup(id));
3191 } /* get_vlan_name */
3194 get_manuf_name(const guint8 *addr)
3196 hashmanuf_t *manuf_value;
3198 manuf_value = manuf_name_lookup(addr);
3199 if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
3200 return manuf_value->resolved_name;
3202 return manuf_value->hexaddr;
3204 } /* get_manuf_name */
3207 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3209 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3213 get_manuf_name_if_known(const guint8 *addr)
3215 hashmanuf_t *manuf_value;
3219 /* manuf needs only the 3 most significant octets of the ethernet address */
3220 manuf_key = addr[0];
3221 manuf_key = manuf_key<<8;
3223 manuf_key = manuf_key | oct;
3224 manuf_key = manuf_key<<8;
3226 manuf_key = manuf_key | oct;
3228 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3229 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3233 return manuf_value->resolved_longname;
3235 } /* get_manuf_name_if_known */
3238 uint_get_manuf_name_if_known(const guint manuf_key)
3240 hashmanuf_t *manuf_value;
3242 manuf_value = (hashmanuf_t *)wmem_map_lookup(manuf_hashtable, &manuf_key);
3243 if ((manuf_value == NULL) || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3247 return manuf_value->resolved_longname;
3251 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3253 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3256 char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
3258 return manuf->resolved_longname;
3262 eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
3264 guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
3265 hashmanuf_t *manuf_value;
3268 /* Copy and convert the address to network byte order. */
3269 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3271 manuf_value = manuf_name_lookup(addr);
3272 if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
3273 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]);
3275 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]);
3278 wmem_free(NULL, addr);
3280 } /* eui64_to_display */
3283 #define GHI_TIMEOUT (250 * 1000)
3285 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3287 * XXX - If we wanted to be really fancy we could cache results here and
3288 * look them up in get_host_ipaddr* below.
3290 async_hostent_t *ahp = (async_hostent_t *)arg;
3291 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3292 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3293 ahp->copied = hp->h_length;
3296 #endif /* HAVE_C_ARES */
3298 /* Translate a string, assumed either to be a dotted-quad IPv4 address or
3299 * a host name, to a numeric IPv4 address. Return TRUE if we succeed and
3300 * set "*addrp" to that numeric IPv4 address; return FALSE if we fail. */
3302 get_host_ipaddr(const char *host, guint32 *addrp)
3305 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3308 async_hostent_t ahe;
3312 * XXX - are there places where this is used to translate something
3313 * that's *only* supposed to be an IPv4 address, and where it
3314 * *shouldn't* translate host names?
3316 if (!ws_inet_pton4(host, addrp)) {
3318 /* It's not a valid dotted-quad IP address; is it a valid
3322 /* If we're not allowed to do name resolution, don't do name
3325 if (!gbl_resolv_flags.network_name ||
3326 !gbl_resolv_flags.use_external_net_name_resolver) {
3331 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3334 ahe.addr_size = (int) sizeof (struct in_addr);
3337 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3340 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3342 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3343 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3344 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3347 ares_process(ghbn_chan, &rfds, &wfds);
3349 ares_cancel(ghbn_chan);
3350 if (ahe.addr_size == ahe.copied) {
3361 * Translate IPv6 numeric address or FQDN hostname into binary IPv6 address.
3362 * Return TRUE if we succeed and set "*addrp" to that numeric IPv6 address;
3363 * return FALSE if we fail.
3366 get_host_ipaddr6(const char *host, ws_in6_addr *addrp)
3369 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3372 async_hostent_t ahe;
3373 #endif /* HAVE_C_ARES */
3375 if (str_to_ip6(host, addrp))
3378 /* It's not a valid dotted-quad IP address; is it a valid
3381 * XXX - are there places where this is used to translate something
3382 * that's *only* supposed to be an IPv6 address, and where it
3383 * *shouldn't* translate host names?
3386 /* If we're not allowed to do name resolution, don't do name
3389 if (!gbl_resolv_flags.network_name ||
3390 !gbl_resolv_flags.use_external_net_name_resolver) {
3396 if (!async_dns_initialized || name_resolve_concurrency < 1) {
3399 ahe.addr_size = (int) sizeof (ws_in6_addr);
3402 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3405 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3407 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3408 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3409 fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
3412 ares_process(ghbn_chan, &rfds, &wfds);
3414 ares_cancel(ghbn_chan);
3415 if (ahe.addr_size == ahe.copied) {
3424 get_manuf_hashtable(void)
3426 return manuf_hashtable;
3430 get_wka_hashtable(void)
3432 return wka_hashtable;
3436 get_eth_hashtable(void)
3438 return eth_hashtable;
3442 get_serv_port_hashtable(void)
3444 return serv_port_hashtable;
3448 get_ipxnet_hash_table(void)
3450 return ipxnet_hash_table;
3454 get_vlan_hash_table(void)
3456 return vlan_hash_table;
3460 get_ipv4_hash_table(void)
3462 return ipv4_hash_table;
3466 get_ipv6_hash_table(void)
3468 return ipv6_hash_table;
3470 /* Initialize all the address resolution subsystems in this file */
3472 addr_resolv_init(void)
3474 initialize_services();
3475 initialize_ethers();
3476 initialize_ipxnets();
3478 initialize_enterprises();
3479 host_name_lookup_init();
3482 /* Clean up all the address resolution subsystems in this file */
3484 addr_resolv_cleanup(void)
3486 vlan_name_lookup_cleanup();
3487 service_name_lookup_cleanup();
3489 ipx_name_lookup_cleanup();
3490 enterprises_cleanup();
3491 host_name_lookup_cleanup();
3495 str_to_ip(const char *str, void *dst)
3497 return ws_inet_pton4(str, (guint32 *)dst);
3501 str_to_ip6(const char *str, void *dst)
3503 return ws_inet_pton6(str, (ws_in6_addr *)dst);
3507 * convert a 0-terminated string that contains an ethernet address into
3508 * the corresponding sequence of 6 bytes
3509 * eth_bytes is a buffer >= 6 bytes that was allocated by the caller
3512 str_to_eth(const char *str, char *eth_bytes)
3516 if (!parse_ether_address(str, ð, NULL, FALSE))
3519 memcpy(eth_bytes, eth.addr, sizeof(eth.addr));
3524 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3529 * indent-tabs-mode: nil
3532 * vi: set shiftwidth=4 tabstop=8 expandtab:
3533 * :indentSize=4:tabSize=8:noTabs=true: