2 * Routines for network object lookup
6 * Laurent Deniel <laurent.deniel@free.fr>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
37 * are most likely to take a long time, given the way address-to-name
38 * lookups are done over NBNS).
40 * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
41 * call in a signal handler, you might crash, because the state of the
42 * resolution code that sends messages to lookupd might be inconsistent
43 * if you jump out of it in middle of a call.
45 * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
46 * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
47 * call (and possibly in other gethostbyaddr() calls), because there's
48 * a mutex lock held in gethostbyaddr() and it doesn't get released
49 * if we longjmp out of it.
51 * There's no guarantee that longjmp()ing out of name resolution calls
52 * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
53 * code in tcpdump, to avoid those sorts of problems, and that was
54 * picked up by tcpdump.org tcpdump.
56 * So, for now, we do not define AVOID_DNS_TIMEOUT. If we get a
57 * significantly more complaints about lookups taking a long time,
58 * we can reconsider that decision. (Note that tcpdump originally
59 * added that for the benefit of systems using NIS to look up host
60 * names; that might now be fixed in NIS implementations, for those
61 * sites still using NIS rather than DNS for that....)
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
76 #ifdef HAVE_ARPA_INET_H
77 #include <arpa/inet.h>
82 #ifdef HAVE_SYS_SOCKET_H
83 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
86 #ifdef HAVE_WINSOCK2_H
87 #include <winsock2.h> /* needed to define AF_ values on Windows */
90 #ifdef NEED_INET_ATON_H
91 # include "wsutil/inet_aton.h"
94 #ifdef NEED_INET_V6DEFS_H
95 # include "wsutil/inet_v6defs.h"
98 #if defined(_WIN32) && defined(INET6)
99 # include <ws2tcpip.h>
103 # if defined(_WIN32) && !defined(INET6)
104 # define socklen_t unsigned int
107 # include <ares_version.h>
109 # ifdef HAVE_GNU_ADNS
112 # if defined(inet_aton) && defined(_WIN32)
115 # endif /* HAVE_GNU_ADNS */
116 #endif /* HAVE_C_ARES */
122 #include "addr_and_mask.h"
123 #include "ipv6-utils.h"
124 #include "addr_resolv.h"
125 #include "filesystem.h"
127 #include <wsutil/report_err.h>
128 #include <wsutil/file_util.h>
130 #include <epan/strutil.h>
131 #include <epan/prefs.h>
132 #include <epan/emem.h>
134 #define ENAME_HOSTS "hosts"
135 #define ENAME_SUBNETS "subnets"
136 #define ENAME_ETHERS "ethers"
137 #define ENAME_IPXNETS "ipxnets"
138 #define ENAME_MANUF "manuf"
139 #define ENAME_SERVICES "services"
141 #define HASHETHSIZE 2048
142 #define HASHHOSTSIZE 2048
143 #define HASHIPXNETSIZE 256
144 #define HASHMANUFSIZE 256
145 #define HASHPORTSIZE 256
146 #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
148 /* g_int64_hash() and g_int64_equal() first appear in GLib 2.22, make a local copy here */
149 #if !GLIB_CHECK_VERSION(2,22,0)
152 * @v1: a pointer to a #gint64 key
153 * @v2: a pointer to a #gint64 key to compare with @v1
155 * Compares the two #gint64 values being pointed to and returns
156 * %TRUE if they are equal.
157 * It can be passed to g_hash_table_new() as the @key_equal_func
158 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
161 * Returns: %TRUE if the two keys match.
166 g_int64_equal (gconstpointer v1,
169 return *((const gint64*) v1) == *((const gint64*) v2);
174 * @v: a pointer to a #gint64 key
176 * Converts a pointer to a #gint64 to a hash value.
178 * It can be passed to g_hash_table_new() as the @hash_func parameter,
179 * when using non-%NULL pointers to 64-bit integer values as keys in a
182 * Returns: a hash value corresponding to the key.
187 g_int64_hash (gconstpointer v)
189 return (guint) *(const gint64*) v;
192 #endif /* GLIB_CHECK_VERSION(2,22,0) */
193 /* hash table used for IPv4 lookup */
195 #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
198 * XXX Some of this is duplicated in addrinfo_list. We may want to replace the
199 * addr and name parts with a struct addrinfo or create our own addrinfo-like
200 * struct that simply points to the data below.
202 typedef struct hashipv4 {
204 gboolean is_dummy_entry; /* name is IPv4 address in dot format */
205 gboolean resolve; /* already tried to resolve it */
206 struct hashipv4 *next;
208 gchar name[MAXNAMELEN];
211 /* hash table used for IPv6 lookup */
213 #define HASH_IPV6_ADDRESS(addr) \
214 ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
216 typedef struct hashipv6 {
217 struct e_in6_addr addr;
218 gboolean is_dummy_entry; /* name is IPv6 address in colon format */
219 gboolean resolve; /* */
220 struct hashipv6 *next;
221 gchar ip6[MAX_IP6_STR_LEN]; /* XX */
222 gchar name[MAXNAMELEN];
225 /* Array of entries of subnets of different lengths */
227 gsize mask_length; /*1-32*/
228 guint32 mask; /* e.g. 255.255.255.*/
229 hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
230 } subnet_length_entry_t;
232 /* hash table used for TCP/UDP/SCTP port lookup */
234 #define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
238 typedef struct serv_port {
245 /* hash table used for IPX network lookup */
247 /* XXX - check goodness of hash function */
249 #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
251 typedef struct hashipxnet {
253 struct hashipxnet *next;
254 gchar name[MAXNAMELEN];
257 /* hash tables used for ethernet and manufacturer lookup */
258 #define HASHETHER_STATUS_UNRESOLVED 1
259 #define HASHETHER_STATUS_RESOLVED_DUMMY 2
260 #define HASHETHER_STATUS_RESOLVED_NAME 3
263 typedef struct hashether {
264 struct hashether *next;
265 guint status; /* (See above) */
268 char resolved_name[MAXNAMELEN];
271 /* internal ethernet type */
273 typedef struct _ether
276 char name[MAXNAMELEN];
279 /* internal ipxnet type */
281 typedef struct _ipxnet
284 char name[MAXNAMELEN];
287 static hashipv4_t *ipv4_table[HASHHOSTSIZE];
288 static hashipv6_t *ipv6_table[HASHHOSTSIZE];
290 static gchar *cb_service;
291 static port_type cb_proto = PT_NONE;
294 static GHashTable *manuf_hashtable = NULL;
295 static GHashTable *wka_hashtable = NULL;
296 static GHashTable *eth_hashtable = NULL;
297 static GHashTable *serv_port_hashtable = NULL;
299 static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
301 static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
302 static gboolean have_subnet_entry = FALSE;
304 static gboolean new_resolved_objects = FALSE;
306 static struct addrinfo *addrinfo_list = NULL; /* IPv4 and IPv6 */
307 static struct addrinfo *addrinfo_list_last = NULL;
308 static GPtrArray* extra_hosts_files = NULL;
310 static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
311 static void add_serv_port_cb(const guint32 port);
314 * Flag controlling what names to resolve.
316 e_addr_resolve gbl_resolv_flags = {TRUE, FALSE, TRUE, TRUE, TRUE, FALSE};
317 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
318 static guint name_resolve_concurrency = 500;
322 * Global variables (can be changed in GUI sections)
323 * XXX - they could be changed in GUI code, but there's currently no
324 * GUI code to change them.
327 gchar *g_ethers_path = NULL; /* global ethers file */
328 gchar *g_pethers_path = NULL; /* personal ethers file */
329 gchar *g_ipxnets_path = NULL; /* global ipxnets file */
330 gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
331 gchar *g_services_path = NULL; /* global services file */
332 gchar *g_pservices_path = NULL; /* personal services file */
333 /* first resolving call */
338 * Submitted queries trigger a callback (c_ares_ghba_cb()).
339 * Queries are added to c_ares_queue_head. During processing, queries are
340 * popped off the front of c_ares_queue_head and submitted using
341 * ares_gethostbyaddr().
342 * The callback processes the response, then frees the request.
345 typedef struct _async_dns_queue_msg
349 struct e_in6_addr ip6;
352 } async_dns_queue_msg_t;
354 typedef struct _async_hostent {
360 #if ( ( ARES_VERSION_MAJOR < 1 ) \
361 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
362 static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
364 static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
367 ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
368 ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
375 * Submitted queries have to be checked individually using adns_check().
376 * Queries are added to adns_queue_head. During processing, the list is
377 * iterated twice: once to request queries up to the concurrency limit,
378 * and once to check the status of each query.
383 typedef struct _async_dns_queue_msg
389 } async_dns_queue_msg_t;
391 #endif /* HAVE_GNU_ADNS */
392 #endif /* HAVE_C_ARES */
394 static gboolean async_dns_initialized = FALSE;
395 static guint async_dns_in_flight = 0;
396 static GList *async_dns_queue_head = NULL;
398 /* push a dns request */
400 add_async_dns_ipv4(int type, guint32 addr)
402 async_dns_queue_msg_t *msg;
404 msg = g_new(async_dns_queue_msg_t,1);
407 msg->addr.ip4 = addr;
410 msg->ip4_addr = addr;
411 msg->submitted = FALSE;
413 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
421 const gchar* name; /* Shallow copy */
425 * Miscellaneous functions
429 fgetline(char **buf, int *size, FILE *fp)
434 if (fp == NULL || buf == NULL)
441 *buf = (char *)g_malloc(*size);
451 while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
452 if (len+1 >= *size) {
453 *buf = (char *)g_realloc(*buf, *size += BUFSIZ);
458 if (len == 0 && c == EOF)
469 * Local function definitions
471 static subnet_entry_t subnet_lookup(const guint32 addr);
472 static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
476 add_service_name(port_type proto, const guint port, const char *service_name)
478 serv_port_t *serv_port_table;
481 key = (int *)g_new(int, 1);
484 serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
485 if (serv_port_table == NULL) {
486 serv_port_table = g_new0(serv_port_t,1);
487 g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
492 serv_port_table->tcp_name = g_strdup(service_name);
495 serv_port_table->udp_name = g_strdup(service_name);
498 serv_port_table->sctp_name = g_strdup(service_name);
501 serv_port_table->dccp_name = g_strdup(service_name);
505 /* Should not happen */
508 new_resolved_objects = TRUE;
513 parse_service_line (char *line)
516 * See the services(4) or services(5) man page for services file format
517 * (not available on all systems).
525 range_t *port_rng = NULL;
526 guint32 max_port = MAX_UDP_PORT;
528 if ((cp = strchr(line, '#')))
531 if ((cp = strtok(line, " \t")) == NULL)
536 if ((cp = strtok(NULL, " \t")) == NULL)
541 if (strtok(cp, "/") == NULL)
544 if ((cp = strtok(NULL, "/")) == NULL)
547 /* seems we got all interesting things from the file */
548 if(strcmp(cp, "tcp") == 0) {
549 max_port = MAX_TCP_PORT;
552 else if(strcmp(cp, "udp") == 0) {
553 max_port = MAX_UDP_PORT;
556 else if(strcmp(cp, "sctp") == 0) {
557 max_port = MAX_SCTP_PORT;
560 else if(strcmp(cp, "dccp") == 0) {
561 max_port = MAX_DCCP_PORT;
567 if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
568 /* some assertion here? */
572 cb_service = service;
574 range_foreach(port_rng, add_serv_port_cb);
577 } /* parse_service_line */
581 add_serv_port_cb(const guint32 port)
584 add_service_name(cb_proto, port, cb_service);
590 parse_services_file(const char * path)
594 static char *buf = NULL;
596 /* services hash table initialization */
597 serv_p = ws_fopen(path, "r");
602 while (fgetline(&buf, &size, serv_p) >= 0) {
603 parse_service_line (buf);
610 * unsigned integer to ascii
615 gchar *bp = (gchar *)ep_alloc(MAXNAMELEN);
617 /* XXX, guint32_to_str() ? */
618 guint32_to_str_buf(port, bp, MAXNAMELEN);
624 *serv_name_lookup(const guint port, const port_type proto)
626 const char *serv_proto;
627 struct servent *servp;
628 serv_port_t *serv_port_table;
645 /* not yet implemented */
650 serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
653 /* Set which table we should look up port in */
656 if(serv_port_table->udp_name){
657 return serv_port_table->udp_name;
661 if(serv_port_table->tcp_name){
662 return serv_port_table->tcp_name;
666 if(serv_port_table->sctp_name){
667 return serv_port_table->sctp_name;
671 if(serv_port_table->dccp_name){
672 return serv_port_table->dccp_name;
676 /* not yet implemented */
682 if ((!gbl_resolv_flags.transport_name) ||
683 (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
685 name = (gchar*)g_malloc(16);
686 guint32_to_str_buf(port, name, 16);
688 name = g_strdup(servp->s_name);
690 if(serv_port_table == NULL){
693 key = (int *)g_new(int, 1);
695 serv_port_table = g_new0(serv_port_t,1);
696 g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
700 serv_port_table->udp_name = name;
703 serv_port_table->tcp_name = name;
706 serv_port_table->sctp_name = name;
709 serv_port_table->dccp_name = name;
717 } /* serv_name_lookup */
720 initialize_services(void)
723 /* the hash table won't ignore duplicates, so use the personal path first */
724 g_assert(serv_port_hashtable == NULL);
725 serv_port_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
727 /* set personal services path */
728 if (g_pservices_path == NULL)
729 g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
731 parse_services_file(g_pservices_path);
733 /* Compute the pathname of the services file. */
734 if (g_services_path == NULL) {
735 g_services_path = get_datafile_path(ENAME_SERVICES);
738 parse_services_file(g_services_path);
740 } /* initialize_services */
743 service_name_lookup_cleanup(void)
745 if(serv_port_hashtable){
746 g_hash_table_destroy(serv_port_hashtable);
747 serv_port_hashtable = NULL;
751 /* Fill in an IP4 structure with info from subnets file or just with the
752 * string form of the address.
755 fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
757 subnet_entry_t subnet_entry;
759 if (tp->is_dummy_entry)
760 return; /* already done */
762 tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
764 /* Do we have a subnet for this address? */
765 subnet_entry = subnet_lookup(addr);
766 if(0 != subnet_entry.mask) {
767 /* Print name, then '.' then IP address after subnet mask */
769 gchar buffer[MAX_IP_STR_LEN];
773 host_addr = addr & (~(guint32)subnet_entry.mask);
774 ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
777 /* Skip to first octet that is not totally masked
778 * If length of mask is 32, we chomp the whole address.
779 * If the address string starts '.' (should not happen?),
782 i = subnet_entry.mask_length / 8;
783 while(*(paddr) != '\0' && i > 0) {
784 if(*(++paddr) == '.') {
789 /* There are more efficient ways to do this, but this is safe if we
790 * trust g_snprintf and MAXNAMELEN
792 g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
794 ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
801 #if ( ( ARES_VERSION_MAJOR < 1 ) \
802 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
803 c_ares_ghba_cb(void *arg, int status, struct hostent *he) {
805 c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
807 async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
811 /* XXX, what to do if async_dns_in_flight == 0? */
812 async_dns_in_flight--;
814 if (status == ARES_SUCCESS) {
815 for (p = he->h_addr_list; *p != NULL; p++) {
816 switch(caqm->family) {
818 add_ipv4_name(caqm->addr.ip4, he->h_name);
821 add_ipv6_name(&caqm->addr.ip6, he->h_name);
824 /* Throw an exception? */
831 #endif /* HAVE_C_ARES */
833 /* --------------- */
835 new_ipv4(const guint addr)
837 hashipv4_t *tp = se_new(hashipv4_t);
841 tp->is_dummy_entry = FALSE;
842 ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
847 host_lookup(const guint addr, gboolean *found)
850 hashipv4_t * volatile tp;
851 struct hostent *hostp;
855 hash_idx = HASH_IPV4_ADDRESS(addr);
857 tp = ipv4_table[hash_idx];
860 tp = ipv4_table[hash_idx] = new_ipv4(addr);
863 if( tp->addr == addr ) {
864 if (tp->is_dummy_entry && !tp->resolve)
866 if (tp->is_dummy_entry)
870 if (tp->next == NULL) {
871 tp->next = new_ipv4(addr);
879 if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
882 if (gbl_resolv_flags.concurrent_dns &&
883 name_resolve_concurrency > 0 &&
884 async_dns_initialized) {
885 add_async_dns_ipv4(AF_INET, addr);
886 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
887 * going to risk changing the semantics.
889 fill_dummy_ip4(addr, tp);
892 #endif /* ASYNC_DNS */
895 * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
896 * the name of the host on which it's running; to work around that
897 * botch, we don't try to translate an all-zero IP address to a host
901 /* Use async DNS if possible, else fall back to timeouts,
902 * else call gethostbyaddr and hope for the best
905 hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
907 if (hostp != NULL && hostp->h_name[0] != '\0') {
908 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
909 tp->is_dummy_entry = FALSE;
914 /* unknown host or DNS timeout */
920 fill_dummy_ip4(addr, tp);
925 /* --------------- */
927 new_ipv6(const struct e_in6_addr *addr)
929 hashipv6_t *tp = se_new(hashipv6_t);
933 tp->is_dummy_entry = FALSE;
934 ip6_to_str_buf(addr, tp->ip6);
938 /* ------------------------------------ */
940 host_lookup6(const struct e_in6_addr *addr, gboolean *found)
943 hashipv6_t * volatile tp;
946 async_dns_queue_msg_t *caqm;
947 #endif /* HAVE_C_ARES */
948 struct hostent *hostp;
953 hash_idx = HASH_IPV6_ADDRESS(*addr);
955 tp = ipv6_table[hash_idx];
958 tp = ipv6_table[hash_idx] = new_ipv6(addr);
961 if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
962 if (tp->is_dummy_entry && !tp->resolve)
964 if (tp->is_dummy_entry)
968 if (tp->next == NULL) {
969 tp->next = new_ipv6(addr);
977 if (gbl_resolv_flags.network_name &&
978 gbl_resolv_flags.use_external_net_name_resolver) {
983 if ((gbl_resolv_flags.concurrent_dns) &&
984 name_resolve_concurrency > 0 &&
985 async_dns_initialized) {
986 caqm = g_new(async_dns_queue_msg_t,1);
987 caqm->family = AF_INET6;
988 memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
989 async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
991 /* XXX found is set to TRUE, which seems a bit odd, but I'm not
992 * going to risk changing the semantics.
994 if (!tp->is_dummy_entry) {
995 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
996 ip6_to_str_buf(addr, tp->name);
997 tp->is_dummy_entry = TRUE;
1001 #endif /* HAVE_C_ARES */
1003 /* Quick hack to avoid DNS/YP timeout */
1004 hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
1006 if (hostp != NULL && hostp->h_name[0] != '\0') {
1007 g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
1008 tp->is_dummy_entry = FALSE;
1014 /* unknown host or DNS timeout */
1015 if (!tp->is_dummy_entry) {
1016 tp->is_dummy_entry = TRUE;
1017 g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
1022 } /* host_lookup6 */
1024 static const gchar *
1025 solve_address_to_name(const address *addr)
1027 switch (addr->type) {
1030 return get_ether_name((const guint8 *)addr->data);
1034 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
1035 return get_hostname(ip4_addr);
1039 struct e_in6_addr ip6_addr;
1040 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
1041 return get_hostname6(&ip6_addr);
1045 return (const gchar *)addr->data;
1052 static const gchar *
1053 se_solve_address_to_name(const address *addr)
1055 switch (addr->type) {
1058 return get_ether_name((const guint8 *)addr->data);
1062 memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
1063 return get_hostname(ip4_addr);
1067 struct e_in6_addr ip6_addr;
1068 memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
1069 return get_hostname6(&ip6_addr);
1073 return se_strdup((const gchar *)addr->data);
1081 * Ethernet / manufacturer resolution
1083 * The following functions implement ethernet address resolution and
1084 * ethers files parsing (see ethers(4)).
1086 * The manuf file has the same format as ethers(4) except that names are
1087 * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
1088 * only 3 bytes (instead of 6).
1092 * I decide to not use the existing functions (see ethers(3) on some
1093 * operating systems) for the following reasons:
1094 * - performance gains (use of hash tables and some other enhancements),
1095 * - use of two ethers files (system-wide and per user),
1096 * - avoid the use of NIS maps,
1097 * - lack of these functions on some systems.
1099 * So the following functions do _not_ behave as the standard ones.
1106 * If "manuf_file" is FALSE, parse a 6-byte MAC address.
1107 * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
1111 parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
1112 const gboolean manuf_file)
1119 for (i = 0; i < 6; i++) {
1120 /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
1121 if (!isxdigit((unsigned char)*cp))
1123 num = strtoul(cp, &p, 16);
1125 return FALSE; /* failed */
1127 return FALSE; /* not a valid octet */
1128 eth->addr[i] = (guint8) num;
1129 cp = p; /* skip past the number */
1131 /* OK, what character terminated the octet? */
1133 /* "/" - this has a mask. */
1135 /* Entries with masks are allowed only in the "manuf" files. */
1138 cp++; /* skip past the '/' to get to the mask */
1139 if (!isdigit((unsigned char)*cp))
1140 return FALSE; /* no sign allowed */
1141 num = strtoul(cp, &p, 10);
1143 return FALSE; /* failed */
1144 cp = p; /* skip past the number */
1145 if (*cp != '\0' && !isspace((unsigned char)*cp))
1146 return FALSE; /* bogus terminator */
1147 if (num == 0 || num >= 48)
1148 return FALSE; /* bogus mask */
1149 /* Mask out the bits not covered by the mask */
1151 for (i = 0; num >= 8; i++, num -= 8)
1152 ; /* skip octets entirely covered by the mask */
1153 /* Mask out the first masked octet */
1154 eth->addr[i] &= (0xFF << (8 - num));
1156 /* Mask out completely-masked-out octets */
1162 /* We're at the end of the address, and there's no mask. */
1164 /* We got 3 bytes, so this is a manufacturer ID. */
1166 /* Manufacturer IDs are only allowed in the "manuf"
1170 /* Indicate that this is a manufacturer ID (0 is not allowed
1177 /* We got 6 bytes, so this is a MAC address.
1178 If we're reading one of the "manuf" files, indicate that
1179 this is a MAC address (48 is not allowed as a mask). */
1185 /* We didn't get 3 or 6 bytes, and there's no mask; this is
1190 /* We don't know the separator used in this number; it can either
1191 be ':', '-', or '.'. */
1192 if (*cp != ':' && *cp != '-' && *cp != '.')
1194 sep = *cp; /* subsequent separators must be the same */
1196 /* It has to be the same as the first separator */
1208 parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
1209 const gboolean manuf_file)
1212 * See the ethers(4) or ethers(5) man page for ethers file format
1213 * (not available on all systems).
1214 * We allow both ethernet address separators (':' and '-'),
1215 * as well as Wireshark's '.' separator.
1220 if ((cp = strchr(line, '#')))
1223 if ((cp = strtok(line, " \t")) == NULL)
1226 if (!parse_ether_address(cp, eth, mask, manuf_file))
1229 if ((cp = strtok(NULL, " \t")) == NULL)
1232 g_strlcpy(eth->name, cp, MAXNAMELEN);
1236 } /* parse_ether_line */
1238 static FILE *eth_p = NULL;
1241 set_ethent(char *path)
1246 eth_p = ws_fopen(path, "r");
1259 get_ethent(unsigned int *mask, const gboolean manuf_file)
1263 static int size = 0;
1264 static char *buf = NULL;
1269 while (fgetline(&buf, &size, eth_p) >= 0) {
1270 if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
1281 get_ethbyname(const gchar *name)
1285 set_ethent(g_pethers_path);
1287 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1293 set_ethent(g_ethers_path);
1295 while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
1303 } /* get_ethbyname */
1307 get_ethbyaddr(const guint8 *addr)
1312 set_ethent(g_pethers_path);
1314 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1320 set_ethent(g_ethers_path);
1322 while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
1330 } /* get_ethbyaddr */
1334 add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
1337 gint64 eth_as_int64, *wka_key;
1338 int eth_as_int, *manuf_key;
1341 * XXX - can we use Standard Annotation Language annotations to
1342 * note that mask, as returned by parse_ethe)r_address() (and thus
1343 * by the routines that call it, and thus passed to us) cannot be > 48,
1344 * or is SAL too weak to express that?
1347 /* This is a well-known MAC address; just add this to the Ethernet
1349 add_eth_name(addr, name);
1353 eth_as_int64 = addr[0];
1354 eth_as_int64 = eth_as_int64<<8;
1356 eth_as_int64 = eth_as_int64 | oct;
1357 eth_as_int64 = eth_as_int64<<8;
1359 eth_as_int64 = eth_as_int64 | oct;
1360 eth_as_int64 = eth_as_int64<<8;
1362 eth_as_int64 = eth_as_int64 | oct;
1363 eth_as_int64 = eth_as_int64<<8;
1365 eth_as_int64 = eth_as_int64 | oct;
1366 eth_as_int64 = eth_as_int64<<8;
1368 eth_as_int64 = eth_as_int64 | oct;
1371 /* This is a manufacturer ID; add it to the manufacturer ID hash table */
1373 /* manuf needs only the 3 most significant octets of the ethernet address */
1374 manuf_key = (int *)g_new(int, 1);
1375 eth_as_int = (int)(eth_as_int64>>24)&0xffffff;
1376 *manuf_key = eth_as_int;
1378 g_hash_table_insert(manuf_hashtable, manuf_key, g_strdup(name));
1382 /* This is a range of well-known addresses; add it to the appropriate
1383 well-known-address table, creating that table if necessary. */
1385 wka_key = (gint64 *)g_new(gint64, 1);
1386 *wka_key = eth_as_int64;
1388 g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
1390 } /* add_manuf_name */
1393 manuf_name_lookup(const guint8 *addr)
1395 gint32 manuf_key = 0;
1399 /* manuf needs only the 3 most significant octets of the ethernet address */
1400 manuf_key = addr[0];
1401 manuf_key = manuf_key<<8;
1403 manuf_key = manuf_key | oct;
1404 manuf_key = manuf_key<<8;
1406 manuf_key = manuf_key | oct;
1409 /* first try to find a "perfect match" */
1410 name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1415 /* Mask out the broadcast/multicast flag but not the locally
1416 * administered flag as localy administered means: not assigend
1417 * by the IEEE but the local administrator instead.
1418 * 0x01 multicast / broadcast bit
1419 * 0x02 locally administered bit */
1420 if((manuf_key & 0x00010000) != 0){
1421 manuf_key &= 0x00FEFFFF;
1422 name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
1430 } /* manuf_name_lookup */
1433 wka_name_lookup(const guint8 *addr, const unsigned int mask)
1435 guint8 masked_addr[6];
1438 gint64 eth_as_int64;
1442 if(wka_hashtable == NULL){
1445 /* Get the part of the address covered by the mask. */
1446 for (i = 0, num = mask; num >= 8; i++, num -= 8)
1447 masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
1448 /* Mask out the first masked octet */
1449 masked_addr[i] = addr[i] & (0xFF << (8 - num));
1451 /* Zero out completely-masked-out octets */
1455 eth_as_int64 = masked_addr[0];
1456 eth_as_int64 = eth_as_int64<<8;
1457 oct = masked_addr[1];
1458 eth_as_int64 = eth_as_int64 | oct;
1459 eth_as_int64 = eth_as_int64<<8;
1460 oct = masked_addr[2];
1461 eth_as_int64 = eth_as_int64 | oct;
1462 eth_as_int64 = eth_as_int64<<8;
1463 oct = masked_addr[3];
1464 eth_as_int64 = eth_as_int64 | oct;
1465 eth_as_int64 = eth_as_int64<<8;
1466 oct = masked_addr[4];
1467 eth_as_int64 = eth_as_int64 | oct;
1468 eth_as_int64 = eth_as_int64<<8;
1469 oct = masked_addr[5];
1470 eth_as_int64 = eth_as_int64 | oct;
1472 name = (gchar *)g_hash_table_lookup(wka_hashtable, ð_as_int64);
1476 } /* wka_name_lookup */
1479 initialize_ethers(void)
1485 /* hash table initialization */
1486 wka_hashtable = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, g_free);
1487 manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
1488 eth_hashtable = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, g_free);
1490 /* Compute the pathname of the ethers file. */
1491 if (g_ethers_path == NULL) {
1492 g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1493 get_systemfile_dir(), ENAME_ETHERS);
1496 /* Set g_pethers_path here, but don't actually do anything
1497 * with it. It's used in get_ethbyname() and get_ethbyaddr()
1499 if (g_pethers_path == NULL)
1500 g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
1502 /* Compute the pathname of the manuf file */
1503 manuf_path = get_datafile_path(ENAME_MANUF);
1505 /* Read it and initialize the hash table */
1506 set_ethent(manuf_path);
1508 while ((eth = get_ethent(&mask, TRUE))) {
1509 add_manuf_name(eth->addr, mask, eth->name);
1516 } /* initialize_ethers */
1518 /* this is only needed when shuting down application (if at all) */
1520 eth_name_lookup_cleanup(void)
1523 if(manuf_hashtable) {
1524 g_hash_table_destroy(manuf_hashtable);
1525 manuf_hashtable = NULL;
1528 g_hash_table_destroy(wka_hashtable);
1529 wka_hashtable = NULL;
1533 g_hash_table_destroy(eth_hashtable);
1534 eth_hashtable = NULL;
1539 /* Resolve ethernet address */
1540 static hashether_t *
1541 eth_addr_resolve(hashether_t *tp) {
1543 const guint8 *addr = tp->addr;
1545 if ( (eth = get_ethbyaddr(addr)) != NULL) {
1546 g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
1547 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1553 /* Unknown name. Try looking for it in the well-known-address
1554 tables for well-known address ranges smaller than 2^24. */
1557 /* Only the topmost 5 bytes participate fully */
1558 if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
1559 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
1560 name, addr[5] & (0xFF >> mask));
1561 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1571 /* Only the topmost 4 bytes participate fully */
1572 if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
1573 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
1574 name, addr[4] & (0xFF >> mask), addr[5]);
1575 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1585 /* Only the topmost 3 bytes participate fully */
1586 if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
1587 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1588 name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
1589 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1597 /* Now try looking in the manufacturer table. */
1598 if ((name = manuf_name_lookup(addr)) != NULL) {
1599 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
1600 name, addr[3], addr[4], addr[5]);
1601 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1605 /* Now try looking for it in the well-known-address
1606 tables for well-known address ranges larger than 2^24. */
1609 /* Only the topmost 2 bytes participate fully */
1610 if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
1611 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
1612 name, addr[2] & (0xFF >> mask), addr[3], addr[4],
1614 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1624 /* Only the topmost byte participates fully */
1625 if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
1626 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
1627 name, addr[1] & (0xFF >> mask), addr[2], addr[3],
1629 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1637 for (mask = 7; mask > 0; mask--) {
1638 /* Not even the topmost byte participates fully */
1639 if ((name = wka_name_lookup(addr, mask)) != NULL) {
1640 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
1641 name, addr[0] & (0xFF >> mask), addr[1], addr[2],
1642 addr[3], addr[4], addr[5]);
1643 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1648 /* No match whatsoever. */
1649 g_snprintf(tp->resolved_name, MAXNAMELEN, "%s", ether_to_str(addr));
1650 tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
1653 g_assert_not_reached();
1654 } /* eth_addr_resolve */
1656 static hashether_t *
1657 eth_hash_new_entry(const guint8 *addr, const gboolean resolve) {
1659 gint64 eth_as_int64, *key;
1662 eth_as_int64 = addr[0];
1663 eth_as_int64 = eth_as_int64<<8;
1665 eth_as_int64 = eth_as_int64 | oct;
1666 eth_as_int64 = eth_as_int64<<8;
1668 eth_as_int64 = eth_as_int64 | oct;
1669 eth_as_int64 = eth_as_int64<<8;
1671 eth_as_int64 = eth_as_int64 | oct;
1672 eth_as_int64 = eth_as_int64<<8;
1674 eth_as_int64 = eth_as_int64 | oct;
1675 eth_as_int64 = eth_as_int64<<8;
1677 eth_as_int64 = eth_as_int64 | oct;
1679 key = (gint64 *)g_new(gint64, 1);
1680 *key = eth_as_int64;
1682 tp = g_new(hashether_t, 1);
1683 memcpy(tp->addr, addr, sizeof(tp->addr));
1684 tp->status = HASHETHER_STATUS_UNRESOLVED;
1685 g_strlcpy(tp->hexaddr, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexaddr));
1686 tp->resolved_name[0] = '\0';
1689 eth_addr_resolve(tp);
1691 g_hash_table_insert(eth_hashtable, key, tp);
1694 } /* eth_hash_new_entry */
1696 static hashether_t *
1697 add_eth_name(const guint8 *addr, const gchar *name)
1700 gint64 eth_as_int64, *key;
1703 eth_as_int64 = addr[0];
1704 eth_as_int64 = eth_as_int64<<8;
1706 eth_as_int64 = eth_as_int64 | oct;
1707 eth_as_int64 = eth_as_int64<<8;
1709 eth_as_int64 = eth_as_int64 | oct;
1710 eth_as_int64 = eth_as_int64<<8;
1712 eth_as_int64 = eth_as_int64 | oct;
1713 eth_as_int64 = eth_as_int64<<8;
1715 eth_as_int64 = eth_as_int64 | oct;
1716 eth_as_int64 = eth_as_int64<<8;
1718 eth_as_int64 = eth_as_int64 | oct;
1720 key = (gint64 *)g_new(gint64, 1);
1721 *key = eth_as_int64;
1723 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, key);
1726 tp = eth_hash_new_entry(addr, FALSE);
1729 g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
1730 tp->status = HASHETHER_STATUS_RESOLVED_NAME;
1731 new_resolved_objects = TRUE;
1734 } /* add_eth_name */
1736 static hashether_t *
1737 eth_name_lookup(const guint8 *addr, const gboolean resolve) {
1739 gint64 eth_as_int64, *key;
1742 eth_as_int64 = addr[0];
1743 eth_as_int64 = eth_as_int64<<8;
1745 eth_as_int64 = eth_as_int64 | oct;
1746 eth_as_int64 = eth_as_int64<<8;
1748 eth_as_int64 = eth_as_int64 | oct;
1749 eth_as_int64 = eth_as_int64<<8;
1751 eth_as_int64 = eth_as_int64 | oct;
1752 eth_as_int64 = eth_as_int64<<8;
1754 eth_as_int64 = eth_as_int64 | oct;
1755 eth_as_int64 = eth_as_int64<<8;
1757 eth_as_int64 = eth_as_int64 | oct;
1759 key = (gint64 *)g_new(gint64, 1);
1760 *key = eth_as_int64;
1762 tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, key);
1764 tp = eth_hash_new_entry(addr, resolve);
1766 eth_addr_resolve(tp); /* Found but needs to be resolved */
1771 } /* eth_name_lookup */
1774 eth_addr_lookup(const gchar *name _U_)
1779 hashether_t **table = eth_table;
1782 /* to be optimized (hash table from name to addr) */
1783 for (i = 0; i < HASHETHSIZE; i++) {
1786 if (strcmp(tp->resolved_name, name) == 0)
1792 /* not in hash table : performs a file lookup */
1794 if ((eth = get_ethbyname(name)) == NULL)
1797 /* add new entry in hash table */
1799 tp = add_eth_name(eth->addr, name);
1805 } /* eth_addr_lookup */
1810 parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
1813 * We allow three address separators (':', '-', and '.'),
1814 * as well as no separators
1818 guint32 a, a0, a1, a2, a3;
1819 gboolean found_single_number = FALSE;
1821 if ((cp = strchr(line, '#')))
1824 if ((cp = strtok(line, " \t\n")) == NULL)
1827 /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
1828 * fill a and found_single_number is TRUE,
1831 if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
1832 if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
1833 if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
1834 if (sscanf(cp, "%x", &a) == 1) {
1835 found_single_number = TRUE;
1844 if ((cp = strtok(NULL, " \t\n")) == NULL)
1847 if (found_single_number) {
1851 ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
1854 g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
1858 } /* parse_ipxnets_line */
1860 static FILE *ipxnet_p = NULL;
1863 set_ipxnetent(char *path)
1868 ipxnet_p = ws_fopen(path, "r");
1884 static ipxnet_t ipxnet;
1885 static int size = 0;
1886 static char *buf = NULL;
1888 if (ipxnet_p == NULL)
1891 while (fgetline(&buf, &size, ipxnet_p) >= 0) {
1892 if (parse_ipxnets_line(buf, &ipxnet) == 0) {
1899 } /* get_ipxnetent */
1902 get_ipxnetbyname(const gchar *name)
1906 set_ipxnetent(g_ipxnets_path);
1908 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1911 if (ipxnet == NULL) {
1914 set_ipxnetent(g_pipxnets_path);
1916 while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
1924 } /* get_ipxnetbyname */
1927 get_ipxnetbyaddr(guint32 addr)
1931 set_ipxnetent(g_ipxnets_path);
1933 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
1935 if (ipxnet == NULL) {
1938 set_ipxnetent(g_pipxnets_path);
1940 while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
1948 } /* get_ipxnetbyaddr */
1951 initialize_ipxnets(void)
1953 /* Compute the pathname of the ipxnets file.
1955 * XXX - is there a notion of an "ipxnets file" in any flavor of
1956 * UNIX, or with any add-on Netware package for UNIX? If not,
1957 * should the UNIX version of the ipxnets file be in the datafile
1958 * directory as well?
1960 if (g_ipxnets_path == NULL) {
1961 g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1962 get_systemfile_dir(), ENAME_IPXNETS);
1965 /* Set g_pipxnets_path here, but don't actually do anything
1966 * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
1968 if (g_pipxnets_path == NULL)
1969 g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
1971 } /* initialize_ipxnets */
1974 ipx_name_lookup_cleanup(void)
1976 /* The memory pointed to by this table is se_ allocated so we don't have to
1979 memset(ipxnet_table, 0, sizeof(ipxnet_table));
1982 static hashipxnet_t *
1983 add_ipxnet_name(guint addr, const gchar *name)
1988 hash_idx = HASH_IPX_NET(addr);
1990 tp = ipxnet_table[hash_idx];
1993 tp = ipxnet_table[hash_idx] = se_new(hashipxnet_t);
1996 if (tp->next == NULL) {
1997 tp->next = se_new(hashipxnet_t);
2006 g_strlcpy(tp->name, name, MAXNAMELEN);
2008 new_resolved_objects = TRUE;
2012 } /* add_ipxnet_name */
2015 ipxnet_name_lookup(const guint addr)
2021 hash_idx = HASH_IPX_NET(addr);
2023 tp = ipxnet_table[hash_idx];
2026 tp = ipxnet_table[hash_idx] = se_new(hashipxnet_t);
2029 if (tp->addr == addr) {
2032 if (tp->next == NULL) {
2033 tp->next = se_new(hashipxnet_t);
2041 /* fill in a new entry */
2046 if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
2048 g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
2051 g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
2056 } /* ipxnet_name_lookup */
2059 ipxnet_addr_lookup(const gchar *name, gboolean *success)
2063 hashipxnet_t **table = ipxnet_table;
2066 /* to be optimized (hash table from name to addr) */
2067 for (i = 0; i < HASHIPXNETSIZE; i++) {
2070 if (strcmp(tp->name, name) == 0) {
2078 /* not in hash table : performs a file lookup */
2080 if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
2085 /* add new entry in hash table */
2087 tp = add_ipxnet_name(ipxnet->addr, name);
2092 } /* ipxnet_addr_lookup */
2095 read_hosts_file (const char *hostspath)
2101 guint32 host_addr[4]; /* IPv4 or IPv6 */
2102 struct e_in6_addr ip6_addr;
2107 * See the hosts(4) or hosts(5) man page for hosts file format
2108 * (not available on all systems).
2110 if ((hf = ws_fopen(hostspath, "r")) == NULL)
2113 while (fgetline(&line, &size, hf) >= 0) {
2114 if ((cp = strchr(line, '#')))
2117 if ((cp = strtok(line, " \t")) == NULL)
2118 continue; /* no tokens in the line */
2120 ret = inet_pton(AF_INET6, cp, &host_addr);
2122 continue; /* error parsing */
2127 /* Not valid IPv6 - valid IPv4? */
2128 if (inet_pton(AF_INET, cp, &host_addr) <= 0)
2133 if ((cp = strtok(NULL, " \t")) == NULL)
2134 continue; /* no host name */
2137 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2138 add_ipv6_name(&ip6_addr, cp);
2140 add_ipv4_name(host_addr[0], cp);
2143 * Add the aliases, too, if there are any.
2144 * XXX - host_lookup() only returns the first entry.
2146 while ((cp = strtok(NULL, " \t")) != NULL) {
2148 memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
2149 add_ipv6_name(&ip6_addr, cp);
2151 add_ipv4_name(host_addr[0], cp);
2158 } /* read_hosts_file */
2161 add_hosts_file (const char *hosts_file)
2163 gboolean found = FALSE;
2169 if (!extra_hosts_files)
2170 extra_hosts_files = g_ptr_array_new();
2172 for (i = 0; i < extra_hosts_files->len; i++) {
2173 if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
2178 g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
2179 if (addrinfo_list) {
2180 return read_hosts_file (hosts_file);
2187 add_ip_name_from_string (const char *addr, const char *name)
2189 guint32 host_addr[4]; /* IPv4 */
2190 struct e_in6_addr ip6_addr; /* IPv6 */
2194 ret = inet_pton(AF_INET6, addr, &ip6_addr);
2196 /* Error parsing address */
2203 /* Not valid IPv6 - valid IPv4? */
2204 if (inet_pton(AF_INET, addr, &host_addr) <= 0)
2205 return FALSE; /* no */
2210 add_ipv6_name(&ip6_addr, name);
2212 add_ipv4_name(host_addr[0], name);
2216 } /* add_ip_name_from_string */
2219 get_addrinfo_list(void) {
2220 return addrinfo_list;
2223 /* Read in a list of subnet definition - name pairs.
2224 * <line> = <comment> | <entry> | <whitespace>
2225 * <comment> = <whitespace>#<any>
2226 * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
2227 * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
2228 * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
2229 * <subnet_mask_length> is a decimal 1-31
2230 * <subnet_name> is a string containing no whitespace.
2231 * <whitespace> = (space | tab)+
2232 * Any malformed entries are ignored.
2233 * Any trailing data after the subnet_name is ignored.
2238 read_subnets_file (const char *subnetspath)
2244 guint32 host_addr; /* IPv4 ONLY */
2247 if ((hf = ws_fopen(subnetspath, "r")) == NULL)
2250 while (fgetline(&line, &size, hf) >= 0) {
2251 if ((cp = strchr(line, '#')))
2254 if ((cp = strtok(line, " \t")) == NULL)
2255 continue; /* no tokens in the line */
2258 /* Expected format is <IP4 address>/<subnet length> */
2259 cp2 = strchr(cp, '/');
2264 *cp2 = '\0'; /* Cut token */
2267 /* Check if this is a valid IPv4 address */
2268 if (inet_pton(AF_INET, cp, &host_addr) <= 0) {
2272 mask_length = atoi(cp2);
2273 if(0 >= mask_length || mask_length > 31) {
2274 continue; /* invalid mask length */
2277 if ((cp = strtok(NULL, " \t")) == NULL)
2278 continue; /* no subnet name */
2280 subnet_entry_set(host_addr, (guint32)mask_length, cp);
2286 } /* read_subnets_file */
2288 static subnet_entry_t
2289 subnet_lookup(const guint32 addr)
2291 subnet_entry_t subnet_entry;
2294 /* Search mask lengths linearly, longest first */
2296 i = SUBNETLENGTHSIZE;
2297 while(have_subnet_entry && i > 0) {
2298 guint32 masked_addr;
2299 subnet_length_entry_t* length_entry;
2301 /* Note that we run from 31 (length 32) to 0 (length 1) */
2303 g_assert(i < SUBNETLENGTHSIZE);
2306 length_entry = &subnet_length_entries[i];
2308 if(NULL != length_entry->subnet_addresses) {
2312 masked_addr = addr & length_entry->mask;
2313 hash_idx = HASH_IPV4_ADDRESS(masked_addr);
2315 tp = length_entry->subnet_addresses[hash_idx];
2316 while(tp != NULL && tp->addr != masked_addr) {
2321 subnet_entry.mask = length_entry->mask;
2322 subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
2323 subnet_entry.name = tp->name;
2324 return subnet_entry;
2329 subnet_entry.mask = 0;
2330 subnet_entry.mask_length = 0;
2331 subnet_entry.name = NULL;
2333 return subnet_entry;
2336 /* Add a subnet-definition - name pair to the set.
2337 * The definition is taken by masking the address passed in with the mask of the
2341 subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
2343 subnet_length_entry_t* entry;
2347 g_assert(mask_length > 0 && mask_length <= 32);
2349 entry = &subnet_length_entries[mask_length - 1];
2351 subnet_addr &= entry->mask;
2353 hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
2355 if(NULL == entry->subnet_addresses) {
2356 entry->subnet_addresses = (hashipv4_t**) se_alloc0(sizeof(hashipv4_t*) * HASHHOSTSIZE);
2359 if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
2360 if(tp->addr == subnet_addr) {
2361 return; /* XXX provide warning that an address was repeated? */
2363 hashipv4_t * new_tp = se_new(hashipv4_t);
2368 tp = entry->subnet_addresses[hash_idx] = se_new(hashipv4_t);
2372 tp->addr = subnet_addr;
2373 tp->is_dummy_entry = FALSE; /*Never used again...*/
2374 g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
2375 have_subnet_entry = TRUE;
2379 subnet_name_lookup_init(void)
2384 for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
2385 guint32 length = i + 1;
2387 subnet_length_entries[i].subnet_addresses = NULL;
2388 subnet_length_entries[i].mask_length = length;
2389 subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
2392 subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
2393 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2394 report_open_failure(subnetspath, errno, FALSE);
2396 g_free(subnetspath);
2399 * Load the global subnets file, if we have one.
2401 subnetspath = get_datafile_path(ENAME_SUBNETS);
2402 if (!read_subnets_file(subnetspath) && errno != ENOENT) {
2403 report_open_failure(subnetspath, errno, FALSE);
2405 g_free(subnetspath);
2410 * External Functions
2414 addr_resolve_pref_init(module_t *nameres)
2416 prefs_register_bool_preference(nameres, "mac_name",
2417 "Resolve MAC addresses",
2418 "Resolve Ethernet MAC address to manufacturer names",
2419 &gbl_resolv_flags.mac_name);
2421 prefs_register_bool_preference(nameres, "transport_name",
2422 "Resolve transport names",
2423 "Resolve TCP/UDP ports into service names",
2424 &gbl_resolv_flags.transport_name);
2426 prefs_register_bool_preference(nameres, "network_name",
2427 "Resolve network (IP) addresses",
2428 "Resolve IPv4, IPv6, and IPX addresses into host names."
2429 " The next set of check boxes determines how name resolution should be performed."
2430 " If no other options are checked name resolution is made from Wireshark's host file,"
2431 " capture file name resolution blocks and DNS packets in the capture.",
2432 &gbl_resolv_flags.network_name);
2434 prefs_register_bool_preference(nameres, "use_external_name_resolver",
2435 "Use an external network name resolver",
2436 "Use your system's configured name resolver"
2437 " (usually DNS) to resolve network names."
2438 " Only applies when network name resolution"
2440 &gbl_resolv_flags.use_external_net_name_resolver);
2442 #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
2443 prefs_register_bool_preference(nameres, "concurrent_dns",
2444 "Enable concurrent DNS name resolution",
2445 "Enable concurrent DNS name resolution. Only"
2446 " applies when network name resolution is"
2447 " enabled. You probably want to enable this.",
2448 &gbl_resolv_flags.concurrent_dns);
2450 prefs_register_uint_preference(nameres, "name_resolve_concurrency",
2451 "Maximum concurrent requests",
2452 "The maximum number of DNS requests that may"
2453 " be active at any time. A large value (many"
2454 " thousands) might overload the network or make"
2455 " your DNS server behave badly.",
2457 &name_resolve_concurrency);
2459 prefs_register_static_text_preference(nameres, "concurrent_dns",
2460 "Enable concurrent DNS name resolution: N/A",
2461 "Support for concurrent DNS name resolution was not"
2462 " compiled into this version of Wireshark");
2465 prefs_register_bool_preference(nameres, "hosts_file_handling",
2466 "Only use the profile \"hosts\" file",
2467 "By default \"hosts\" files will be loaded from multiple sources."
2468 " Checking this box only loads the \"hosts\" in the current profile.",
2469 &gbl_resolv_flags.load_hosts_file_from_profile_only);
2475 host_name_lookup_process(void) {
2476 async_dns_queue_msg_t *caqm;
2477 struct timeval tv = { 0, 0 };
2480 gboolean nro = new_resolved_objects;
2482 new_resolved_objects = FALSE;
2484 if (!async_dns_initialized)
2485 /* c-ares not initialized. Bail out and cancel timers. */
2488 async_dns_queue_head = g_list_first(async_dns_queue_head);
2490 while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
2491 caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
2492 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
2493 if (caqm->family == AF_INET) {
2494 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
2495 c_ares_ghba_cb, caqm);
2496 async_dns_in_flight++;
2497 } else if (caqm->family == AF_INET6) {
2498 ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
2499 AF_INET6, c_ares_ghba_cb, caqm);
2500 async_dns_in_flight++;
2506 nfds = ares_fds(ghba_chan, &rfds, &wfds);
2508 if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
2509 fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
2512 ares_process(ghba_chan, &rfds, &wfds);
2515 /* Any new entries? */
2520 _host_name_lookup_cleanup(void) {
2523 cur = g_list_first(async_dns_queue_head);
2526 cur = g_list_next (cur);
2529 g_list_free(async_dns_queue_head);
2530 async_dns_queue_head = NULL;
2532 if (async_dns_initialized) {
2533 ares_destroy(ghba_chan);
2534 ares_destroy(ghbn_chan);
2536 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2537 ares_library_cleanup();
2539 async_dns_initialized = FALSE;
2542 #elif defined(HAVE_GNU_ADNS)
2544 /* XXX - The ADNS "documentation" isn't very clear:
2545 * - Do we need to keep our query structures around?
2548 host_name_lookup_process(void) {
2549 async_dns_queue_msg_t *almsg;
2551 char addr_str[] = "111.222.333.444.in-addr.arpa.";
2556 gboolean nro = new_resolved_objects;
2558 new_resolved_objects = FALSE;
2559 async_dns_queue_head = g_list_first(async_dns_queue_head);
2561 cur = async_dns_queue_head;
2562 while (cur && async_dns_in_flight <= name_resolve_concurrency) {
2563 almsg = (async_dns_queue_msg_t *) cur->data;
2564 if (! almsg->submitted && almsg->type == AF_INET) {
2565 addr_bytes = (guint8 *) &almsg->ip4_addr;
2566 g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
2567 addr_bytes[2], addr_bytes[1], addr_bytes[0]);
2568 /* XXX - what if it fails? */
2569 adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
2570 almsg->submitted = TRUE;
2571 async_dns_in_flight++;
2576 cur = async_dns_queue_head;
2579 almsg = (async_dns_queue_msg_t *) cur->data;
2580 if (almsg->submitted) {
2581 ret = adns_check(ads, &almsg->query, &ans, NULL);
2583 if (ans->status == adns_s_ok) {
2584 add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
2591 async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
2593 /* XXX, what to do if async_dns_in_flight == 0? */
2594 async_dns_in_flight--;
2598 /* Keep the timeout in place */
2603 _host_name_lookup_cleanup(void) {
2606 async_dns_queue_head = g_list_first(async_dns_queue_head);
2607 while (async_dns_queue_head) {
2608 qdata = async_dns_queue_head->data;
2609 async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
2613 if (async_dns_initialized)
2615 async_dns_initialized = FALSE;
2618 #else /* HAVE_GNU_ADNS */
2621 host_name_lookup_process(void) {
2622 gboolean nro = new_resolved_objects;
2624 new_resolved_objects = FALSE;
2630 _host_name_lookup_cleanup(void) {
2633 #endif /* HAVE_C_ARES */
2636 get_hostname(const guint addr)
2640 /* XXX why do we call this if we're not resolving? To create hash entries?
2643 hashipv4_t *tp = host_lookup(addr, &found);
2645 if (!gbl_resolv_flags.network_name)
2651 /* -------------------------- */
2654 get_hostname6(const struct e_in6_addr *addr)
2658 /* XXX why do we call this if we're not resolving? To create hash entries?
2661 hashipv6_t *tp = host_lookup6(addr, &found);
2663 if (!gbl_resolv_flags.network_name)
2669 /* -------------------------- */
2671 add_ipv4_name(const guint addr, const gchar *name)
2675 struct addrinfo *ai;
2676 struct sockaddr_in *sa4;
2679 * Don't add zero-length names; apparently, some resolvers will return
2680 * them if they get them from DNS.
2682 if (name[0] == '\0')
2685 hash_idx = HASH_IPV4_ADDRESS(addr);
2687 tp = ipv4_table[hash_idx];
2690 tp = ipv4_table[hash_idx] = new_ipv4(addr);
2693 if (tp->addr == addr) {
2694 /* address already known */
2695 if (!tp->is_dummy_entry) {
2698 /* replace this dummy entry with the new one */
2702 if (tp->next == NULL) {
2703 tp->next = new_ipv4(addr);
2710 g_strlcpy(tp->name, name, MAXNAMELEN);
2712 new_resolved_objects = TRUE;
2714 if (!addrinfo_list) {
2715 ai = se_new0(struct addrinfo);
2716 addrinfo_list = addrinfo_list_last = ai;
2719 sa4 = se_new0(struct sockaddr_in);
2720 sa4->sin_family = AF_INET;
2721 sa4->sin_addr.s_addr = addr;
2723 ai = se_new0(struct addrinfo);
2724 ai->ai_family = AF_INET;
2725 ai->ai_addrlen = sizeof(struct sockaddr_in);
2726 ai->ai_canonname = (char *) tp->name;
2727 ai->ai_addr = (struct sockaddr*) sa4;
2729 addrinfo_list_last->ai_next = ai;
2730 addrinfo_list_last = ai;
2732 } /* add_ipv4_name */
2734 /* -------------------------- */
2736 add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
2740 struct addrinfo *ai;
2741 struct sockaddr_in6 *sa6;
2744 * Don't add zero-length names; apparently, some resolvers will return
2745 * them if they get them from DNS.
2747 if (name[0] == '\0')
2750 hash_idx = HASH_IPV6_ADDRESS(*addrp);
2752 tp = ipv6_table[hash_idx];
2755 tp = ipv6_table[hash_idx] = new_ipv6(addrp);
2758 if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
2759 /* address already known */
2760 if (!tp->is_dummy_entry) {
2763 /* replace this dummy entry with the new one */
2767 if (tp->next == NULL) {
2768 tp->next = new_ipv6(addrp);
2775 g_strlcpy(tp->name, name, MAXNAMELEN);
2777 new_resolved_objects = TRUE;
2779 if (!addrinfo_list) {
2780 ai = se_new0(struct addrinfo);
2781 addrinfo_list = addrinfo_list_last = ai;
2784 sa6 = se_new0(struct sockaddr_in6);
2785 sa6->sin6_family = AF_INET;
2786 memcpy(sa6->sin6_addr.s6_addr, addrp, 16);
2788 ai = se_new0(struct addrinfo);
2789 ai->ai_family = AF_INET6;
2790 ai->ai_addrlen = sizeof(struct sockaddr_in);
2791 ai->ai_canonname = (char *) tp->name;
2792 ai->ai_addr = (struct sockaddr *) sa6;
2794 addrinfo_list_last->ai_next = ai;
2795 addrinfo_list_last = ai;
2797 } /* add_ipv6_name */
2800 host_name_lookup_init(void)
2803 struct addrinfo *ai;
2806 #ifdef HAVE_GNU_ADNS
2809 static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
2810 static char rootpath_ot[] = "\\hosts";
2812 #endif /*GNU_ADNS */
2814 if (!addrinfo_list) {
2815 ai = se_new0(struct addrinfo);
2816 addrinfo_list = addrinfo_list_last = ai;
2820 * Load the user's hosts file no matter what, if they have one.
2822 hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
2823 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2824 report_open_failure(hostspath, errno, FALSE);
2828 * Load the global hosts file, if we have one.
2830 if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
2831 hostspath = get_datafile_path(ENAME_HOSTS);
2832 if (!read_hosts_file(hostspath) && errno != ENOENT) {
2833 report_open_failure(hostspath, errno, FALSE);
2838 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2839 if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
2841 if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
2842 async_dns_initialized = TRUE;
2844 #ifdef CARES_HAVE_ARES_LIBRARY_INIT
2848 #ifdef HAVE_GNU_ADNS
2850 * We're using GNU ADNS, which doesn't check the system hosts file;
2851 * we load that file ourselves.
2855 sysroot = getenv_utf8("WINDIR");
2856 if (sysroot != NULL) {
2858 * The file should be under WINDIR.
2859 * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
2860 * %WINDIR%\system32\drivers\etc\hosts.
2861 * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
2863 * XXX - should we base it on the dwPlatformId value from
2866 if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
2867 hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
2868 if (!read_hosts_file(hostspath)) {
2870 hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
2871 read_hosts_file(hostspath);
2877 if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
2878 read_hosts_file("/etc/hosts");
2882 /* XXX - Any flags we should be using? */
2883 /* XXX - We could provide config settings for DNS servers, and
2884 pass them to ADNS with adns_init_strcfg */
2885 if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
2887 * XXX - should we report the error? I'm assuming that some crashes
2888 * reported on a Windows machine with TCP/IP not configured are due
2889 * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
2890 * ADNS in a state where it crashes due to that. We'll still try
2891 * doing name resolution anyway.
2895 async_dns_initialized = TRUE;
2896 async_dns_in_flight = 0;
2897 #endif /* HAVE_GNU_ADNS */
2898 #endif /* HAVE_C_ARES */
2900 if(extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only){
2901 for (i = 0; i < extra_hosts_files->len; i++) {
2902 read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i));
2906 subnet_name_lookup_init();
2910 host_name_lookup_cleanup(void)
2912 _host_name_lookup_cleanup();
2914 memset(ipv4_table, 0, sizeof(ipv4_table));
2915 memset(ipv6_table, 0, sizeof(ipv6_table));
2917 memset(subnet_length_entries, 0, sizeof(subnet_length_entries));
2919 addrinfo_list = addrinfo_list_last = NULL;
2921 have_subnet_entry = FALSE;
2922 new_resolved_objects = FALSE;
2926 get_udp_port(guint port)
2929 if (!gbl_resolv_flags.transport_name) {
2930 return ep_utoa(port);
2933 return serv_name_lookup(port, PT_UDP);
2935 } /* get_udp_port */
2938 get_dccp_port(guint port)
2941 if (!gbl_resolv_flags.transport_name) {
2942 return ep_utoa(port);
2945 return serv_name_lookup(port, PT_DCCP);
2947 } /* get_dccp_port */
2951 get_tcp_port(guint port)
2954 if (!gbl_resolv_flags.transport_name) {
2955 return ep_utoa(port);
2958 return serv_name_lookup(port, PT_TCP);
2960 } /* get_tcp_port */
2963 get_sctp_port(guint port)
2966 if (!gbl_resolv_flags.transport_name) {
2967 return ep_utoa(port);
2970 return serv_name_lookup(port, PT_SCTP);
2972 } /* get_sctp_port */
2975 get_addr_name(const address *addr)
2977 const gchar *result;
2979 result = solve_address_to_name(addr);
2984 /* if it gets here, either it is of type AT_NONE, */
2985 /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
2987 if (addr->type == AT_NONE){
2991 /* We need an ephemeral allocated string */
2992 return ep_address_to_str(addr);
2996 se_get_addr_name(const address *addr)
2998 const gchar *result;
3000 result = se_solve_address_to_name(addr);
3005 /* if it gets here, either it is of type AT_NONE, */
3006 /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
3008 if (addr->type == AT_NONE){
3012 /* We need a "permanently" allocated string */
3013 return se_address_to_str(addr);
3017 get_addr_name_buf(const address *addr, gchar *buf, gsize size)
3019 const gchar *result = get_addr_name(addr);
3021 g_strlcpy(buf, result, size);
3022 } /* get_addr_name_buf */
3026 get_ether_name(const guint8 *addr)
3029 gboolean resolve = gbl_resolv_flags.mac_name;
3031 tp = eth_name_lookup(addr, resolve);
3033 return resolve ? tp->resolved_name : tp->hexaddr;
3035 } /* get_ether_name */
3037 /* Look for a (non-dummy) ether name in the hash, and return it if found.
3038 * If it's not found, simply return NULL.
3041 get_ether_name_if_known(const guint8 *addr)
3045 /* Initialize ether structs if we're the first
3046 * ether-related function called */
3047 if (!gbl_resolv_flags.mac_name)
3050 /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
3051 tp = eth_name_lookup(addr, TRUE);
3052 g_assert(tp != NULL);
3054 if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
3055 /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
3056 return tp->resolved_name;
3059 /* Name was created */
3065 get_ether_addr(const gchar *name)
3068 /* force resolution (do not check gbl_resolv_flags) */
3069 return eth_addr_lookup(name);
3071 } /* get_ether_addr */
3074 add_ether_byip(const guint ip, const guint8 *eth)
3079 /* first check that IP address can be resolved */
3080 if (!gbl_resolv_flags.network_name)
3083 tp = host_lookup(ip, &found);
3085 /* ok, we can add this entry in the ethers hashtable */
3086 add_eth_name(eth, tp->name);
3089 } /* add_ether_byip */
3092 get_ipxnet_name(const guint32 addr)
3095 if (!gbl_resolv_flags.network_name) {
3096 return ipxnet_to_str_punct(addr, '\0');
3099 return ipxnet_name_lookup(addr);
3101 } /* get_ipxnet_name */
3104 get_ipxnet_addr(const gchar *name, gboolean *known)
3109 /* force resolution (do not check gbl_resolv_flags) */
3110 addr = ipxnet_addr_lookup(name, &success);
3115 } /* get_ipxnet_addr */
3118 get_manuf_name(const guint8 *addr)
3124 /* manuf needs only the 3 most significant octets of the ethernet address */
3125 manuf_key = addr[0];
3126 manuf_key = manuf_key<<8;
3128 manuf_key = manuf_key | oct;
3129 manuf_key = manuf_key<<8;
3131 manuf_key = manuf_key | oct;
3133 if (!gbl_resolv_flags.mac_name || ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL)) {
3134 cur=ep_strdup_printf("%02x:%02x:%02x", addr[0], addr[1], addr[2]);
3140 } /* get_manuf_name */
3143 uint_get_manuf_name(const guint oid)
3147 addr[0] = (oid >> 16) & 0xFF;
3148 addr[1] = (oid >> 8) & 0xFF;
3149 addr[2] = (oid >> 0) & 0xFF;
3150 return get_manuf_name(addr);
3154 tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
3156 return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
3160 get_manuf_name_if_known(const guint8 *addr)
3166 /* manuf needs only the 3 most significant octets of the ethernet address */
3167 manuf_key = addr[0];
3168 manuf_key = manuf_key<<8;
3170 manuf_key = manuf_key | oct;
3171 manuf_key = manuf_key<<8;
3173 manuf_key = manuf_key | oct;
3175 if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
3181 } /* get_manuf_name_if_known */
3184 uint_get_manuf_name_if_known(const guint manuf_key)
3188 if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
3196 tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
3198 return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
3202 get_eui64_name(const guint64 addr_eui64)
3205 guint8 *addr = (guint8 *)ep_alloc(8);
3207 /* Copy and convert the address to network byte order. */
3208 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3210 if (!gbl_resolv_flags.mac_name || ((name = manuf_name_lookup(addr)) == NULL)) {
3211 cur=ep_strdup_printf("%02x:%02x:%02x%02x:%02x:%02x%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
3214 cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3217 } /* get_eui64_name */
3221 get_eui64_name_if_known(const guint64 addr_eui64)
3224 guint8 *addr = (guint8 *)ep_alloc(8);
3226 /* Copy and convert the address to network byte order. */
3227 *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
3229 if ((name = manuf_name_lookup(addr)) == NULL) {
3233 cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", name, addr[3], addr[4], addr[5], addr[6], addr[7]);
3236 } /* get_eui64_name_if_known */
3239 #define GHI_TIMEOUT (250 * 1000)
3241 #if ( ( ARES_VERSION_MAJOR < 1 ) \
3242 || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
3243 c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
3245 c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
3248 * XXX - If we wanted to be really fancy we could cache results here and
3249 * look them up in get_host_ipaddr* below.
3251 async_hostent_t *ahp = (async_hostent_t *)arg;
3252 if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
3253 memcpy(ahp->addrp, hp->h_addr, hp->h_length);
3254 ahp->copied = hp->h_length;
3257 #endif /* HAVE_C_ARES */
3259 /* Translate a string, assumed either to be a dotted-quad IP address or
3260 * a host name, to a numeric IP address. Return TRUE if we succeed and
3261 * set "*addrp" to that numeric IP address; return FALSE if we fail.
3262 * Used more in the dfilter parser rather than in packet dissectors */
3264 get_host_ipaddr(const char *host, guint32 *addrp)
3266 struct in_addr ipaddr;
3268 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3271 async_hostent_t ahe;
3272 #else /* HAVE_C_ARES */
3274 #endif /* HAVE_C_ARES */
3277 * don't change it to inet_pton(AF_INET), they are not 100% compatible.
3278 * inet_pton(AF_INET) does not support hexadecimal notation nor
3279 * less-than-4 octet notation.
3281 if (!inet_aton(host, &ipaddr)) {
3283 /* It's not a valid dotted-quad IP address; is it a valid
3287 /* If we're not allowed to do name resolution, don't do name
3290 if (!gbl_resolv_flags.network_name ||
3291 !gbl_resolv_flags.use_external_net_name_resolver) {
3296 if (! (gbl_resolv_flags.concurrent_dns) ||
3297 name_resolve_concurrency < 1 ||
3298 ! async_dns_initialized) {
3301 ahe.addr_size = (int) sizeof (struct in_addr);
3304 ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
3307 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3309 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3310 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3311 fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
3314 ares_process(ghbn_chan, &rfds, &wfds);
3316 ares_cancel(ghbn_chan);
3317 if (ahe.addr_size == ahe.copied) {
3321 #else /* ! HAVE_C_ARES */
3322 hp = gethostbyname(host);
3326 /* Apparently, some versions of gethostbyaddr can
3327 * return IPv6 addresses. */
3328 } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
3329 memcpy(&ipaddr, hp->h_addr, hp->h_length);
3333 #endif /* HAVE_C_ARES */
3335 /* Does the string really contain dotted-quad IP?
3336 * Check against inet_atons that accept strings such as
3337 * "130.230" as valid addresses and try to convert them
3338 * to some form of a classful (host.net) notation.
3340 unsigned int a0, a1, a2, a3;
3341 if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
3345 *addrp = ipaddr.s_addr;
3350 * Translate IPv6 numeric address or FQDN hostname, into binary IPv6 address.
3351 * Return TRUE if we succeed and set "*addrp" to that numeric IP address;
3352 * return FALSE if we fail.
3355 get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
3358 struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
3361 async_hostent_t ahe;
3362 #elif defined(HAVE_GETHOSTBYNAME2)
3364 #endif /* HAVE_C_ARES */
3366 if (inet_pton(AF_INET6, host, addrp) > 0)
3369 /* It's not a valid dotted-quad IP address; is it a valid
3373 /* If we're not allowed to do name resolution, don't do name
3376 if (!gbl_resolv_flags.network_name ||
3377 !gbl_resolv_flags.use_external_net_name_resolver) {
3383 if (! (gbl_resolv_flags.concurrent_dns) ||
3384 name_resolve_concurrency < 1 ||
3385 ! async_dns_initialized) {
3388 ahe.addr_size = (int) sizeof (struct e_in6_addr);
3391 ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
3394 nfds = ares_fds(ghbn_chan, &rfds, &wfds);
3396 tvp = ares_timeout(ghbn_chan, &tv, &tv);
3397 if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
3398 fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
3401 ares_process(ghbn_chan, &rfds, &wfds);
3403 ares_cancel(ghbn_chan);
3404 if (ahe.addr_size == ahe.copied) {
3407 #elif defined(HAVE_GETHOSTBYNAME2)
3408 hp = gethostbyname2(host, AF_INET6);
3409 if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
3410 memcpy(addrp, hp->h_addr, hp->h_length);
3419 * Find out whether a hostname resolves to an ip or ipv6 address
3420 * Return "ip6" if it is IPv6, "ip" otherwise (including the case
3421 * that we don't know)
3423 const char* host_ip_af(const char *host
3424 #ifndef HAVE_GETHOSTBYNAME2
3429 #ifdef HAVE_GETHOSTBYNAME2
3431 return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
3438 get_manuf_hashtable(void)
3440 return manuf_hashtable;
3444 get_wka_hashtable(void)
3446 return wka_hashtable;
3450 get_eth_hashtable(void)
3452 return eth_hashtable;
3456 get_serv_port_hashtable(void)
3458 return serv_port_hashtable;
3461 /* Initialize all the address resolution subsystems in this file */
3463 addr_resolv_init(void)
3465 initialize_services();
3466 initialize_ethers();
3467 initialize_ipxnets();
3468 /* host name initialization is done on a per-capture-file basis */
3469 /*host_name_lookup_init();*/
3472 /* Clean up all the address resolution subsystems in this file */
3474 addr_resolv_cleanup(void)
3476 service_name_lookup_cleanup();
3477 eth_name_lookup_cleanup();
3478 ipx_name_lookup_cleanup();
3479 /* host name initialization is done on a per-capture-file basis */
3480 /*host_name_lookup_cleanup();*/