/epan/addr_resolv.c
C | 3536 lines | 2427 code | 614 blank | 495 comment | 530 complexity | 5de0af8528f39a14046b5bf3993a9a62 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /* addr_resolv.c
- * Routines for network object lookup
- *
- * $Id$
- *
- * Laurent Deniel <laurent.deniel@free.fr>
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 1998 Gerald Combs
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include "config.h"
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- /*
- * Win32 doesn't have SIGALRM (and it's the OS where name lookup calls
- * are most likely to take a long time, given the way address-to-name
- * lookups are done over NBNS).
- *
- * Mac OS X does have SIGALRM, but if you longjmp() out of a name resolution
- * call in a signal handler, you might crash, because the state of the
- * resolution code that sends messages to lookupd might be inconsistent
- * if you jump out of it in middle of a call.
- *
- * In at least some Linux distributions (e.g., RedHat Linux 9), if ADNS
- * is used, we appear to hang in host_name_lookup6() in a gethostbyaddr()
- * call (and possibly in other gethostbyaddr() calls), because there's
- * a mutex lock held in gethostbyaddr() and it doesn't get released
- * if we longjmp out of it.
- *
- * There's no guarantee that longjmp()ing out of name resolution calls
- * will work on *any* platform; OpenBSD got rid of the alarm/longjmp
- * code in tcpdump, to avoid those sorts of problems, and that was
- * picked up by tcpdump.org tcpdump.
- *
- * So, for now, we do not define AVOID_DNS_TIMEOUT. If we get a
- * significantly more complaints about lookups taking a long time,
- * we can reconsider that decision. (Note that tcpdump originally
- * added that for the benefit of systems using NIS to look up host
- * names; that might now be fixed in NIS implementations, for those
- * sites still using NIS rather than DNS for that....)
- */
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef HAVE_NETINET_IN_H
- # include <netinet/in.h>
- #endif
- #ifdef HAVE_NETDB_H
- #include <netdb.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #include <signal.h>
- #ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h> /* needed to define AF_ values on UNIX */
- #endif
- #ifdef HAVE_WINSOCK2_H
- #include <winsock2.h> /* needed to define AF_ values on Windows */
- #endif
- #ifdef NEED_INET_ATON_H
- # include "wsutil/inet_aton.h"
- #endif
- #ifdef NEED_INET_V6DEFS_H
- # include "wsutil/inet_v6defs.h"
- #endif
- #if defined(_WIN32) && defined(INET6)
- # include <ws2tcpip.h>
- #endif
- #ifdef HAVE_C_ARES
- # if defined(_WIN32) && !defined(INET6)
- # define socklen_t unsigned int
- # endif
- # include <ares.h>
- # include <ares_version.h>
- #else
- # ifdef HAVE_GNU_ADNS
- # include <errno.h>
- # include <adns.h>
- # if defined(inet_aton) && defined(_WIN32)
- # undef inet_aton
- # endif
- # endif /* HAVE_GNU_ADNS */
- #endif /* HAVE_C_ARES */
- #include <glib.h>
- #include "packet.h"
- #include "addr_and_mask.h"
- #include "ipv6-utils.h"
- #include "addr_resolv.h"
- #include "filesystem.h"
- #include <wsutil/report_err.h>
- #include <wsutil/file_util.h>
- #include <epan/strutil.h>
- #include <epan/prefs.h>
- #include <epan/emem.h>
- #define ENAME_HOSTS "hosts"
- #define ENAME_SUBNETS "subnets"
- #define ENAME_ETHERS "ethers"
- #define ENAME_IPXNETS "ipxnets"
- #define ENAME_MANUF "manuf"
- #define ENAME_SERVICES "services"
- #define HASHETHSIZE 2048
- #define HASHHOSTSIZE 2048
- #define HASHIPXNETSIZE 256
- #define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
- /* g_int64_hash() and g_int64_equal() first appear in GLib 2.22, make a local copy here */
- #if !GLIB_CHECK_VERSION(2,22,0)
- /**
- * g_int64_equal:
- * @v1: a pointer to a #gint64 key
- * @v2: a pointer to a #gint64 key to compare with @v1
- *
- * Compares the two #gint64 values being pointed to and returns
- * %TRUE if they are equal.
- * It can be passed to g_hash_table_new() as the @key_equal_func
- * parameter, when using non-%NULL pointers to 64-bit integers as keys in a
- * #GHashTable.
- *
- * Returns: %TRUE if the two keys match.
- *
- * Since: 2.22
- */
- static gboolean
- g_int64_equal (gconstpointer v1,
- gconstpointer v2)
- {
- return *((const gint64*) v1) == *((const gint64*) v2);
- }
- /**
- * g_int64_hash:
- * @v: a pointer to a #gint64 key
- *
- * Converts a pointer to a #gint64 to a hash value.
- *
- * It can be passed to g_hash_table_new() as the @hash_func parameter,
- * when using non-%NULL pointers to 64-bit integer values as keys in a
- * #GHashTable.
- *
- * Returns: a hash value corresponding to the key.
- *
- * Since: 2.22
- */
- static guint
- g_int64_hash (gconstpointer v)
- {
- return (guint) *(const gint64*) v;
- }
- #endif /* GLIB_CHECK_VERSION(2,22,0) */
- /* hash table used for IPv4 lookup */
- #define HASH_IPV4_ADDRESS(addr) (g_htonl(addr) & (HASHHOSTSIZE - 1))
- #if 0
- /*
- * XXX Some of this is duplicated in addrinfo_list. We may want to replace the
- * addr and name parts with a struct addrinfo or create our own addrinfo-like
- * struct that simply points to the data below.
- */
- typedef struct hashipv4 {
- guint addr;
- gboolean is_dummy_entry; /* name is IPv4 address in dot format */
- gboolean resolve; /* already tried to resolve it */
- struct hashipv4 *next;
- gchar ip[16];
- gchar name[MAXNAMELEN];
- } hashipv4_t;
- #endif
- typedef struct sub_net_hashipv4 {
- guint addr;
- gboolean is_dummy_entry; /* name is IPv4 address in dot format */
- gboolean resolve; /* already tried to resolve it */
- struct sub_net_hashipv4 *next;
- gchar ip[16];
- gchar name[MAXNAMELEN];
- } sub_net_hashipv4_t;
- #if 0
- typedef struct hashipv6 {
- struct e_in6_addr addr;
- gboolean is_dummy_entry; /* name is IPv6 address in colon format */
- gboolean resolve; /* */
- struct hashipv6 *next;
- gchar ip6[MAX_IP6_STR_LEN]; /* XX */
- gchar name[MAXNAMELEN];
- } hashipv6_t;
- #endif
- /* Array of entries of subnets of different lengths */
- typedef struct {
- gsize mask_length; /*1-32*/
- guint32 mask; /* e.g. 255.255.255.*/
- sub_net_hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
- } subnet_length_entry_t;
- #if 0
- typedef struct serv_port {
- gchar *udp_name;
- gchar *tcp_name;
- gchar *sctp_name;
- gchar *dccp_name;
- } serv_port_t;
- #endif
- /* hash table used for IPX network lookup */
- /* XXX - check goodness of hash function */
- #define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
- typedef struct hashipxnet {
- guint addr;
- struct hashipxnet *next;
- gchar name[MAXNAMELEN];
- } hashipxnet_t;
- /* hash tables used for ethernet and manufacturer lookup */
- #define HASHETHER_STATUS_UNRESOLVED 1
- #define HASHETHER_STATUS_RESOLVED_DUMMY 2
- #define HASHETHER_STATUS_RESOLVED_NAME 3
- #if 0
- typedef struct hashether {
- struct hashether *next;
- guint status; /* (See above) */
- guint8 addr[6];
- char hexaddr[6*3];
- char resolved_name[MAXNAMELEN];
- } hashether_t;
- #endif
- /* internal ethernet type */
- typedef struct _ether
- {
- guint8 addr[6];
- char name[MAXNAMELEN];
- } ether_t;
- /* internal ipxnet type */
- typedef struct _ipxnet
- {
- guint addr;
- char name[MAXNAMELEN];
- } ipxnet_t;
- static GHashTable *ipxnet_hash_table = NULL;
- static GHashTable *ipv4_hash_table = NULL;
- static GHashTable *ipv6_hash_table = NULL;
- static gchar *cb_service;
- static port_type cb_proto = PT_NONE;
- static GHashTable *manuf_hashtable = NULL;
- static GHashTable *wka_hashtable = NULL;
- static GHashTable *eth_hashtable = NULL;
- static GHashTable *serv_port_hashtable = NULL;
- static subnet_length_entry_t subnet_length_entries[SUBNETLENGTHSIZE]; /* Ordered array of entries */
- static gboolean have_subnet_entry = FALSE;
- static gboolean new_resolved_objects = FALSE;
- static struct addrinfo *addrinfo_list = NULL; /* IPv4 and IPv6 */
- static struct addrinfo *addrinfo_list_last = NULL;
- static GPtrArray* extra_hosts_files = NULL;
- static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
- static void add_serv_port_cb(const guint32 port);
- /* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx#existing
- * One-at-a-Time hash
- */
- static guint32
- ipv6_oat_hash(gconstpointer key)
- {
- int len = 16;
- const unsigned char *p = (const unsigned char *)key;
- guint32 h = 0;
- int i;
- for ( i = 0; i < len; i++ ) {
- h += p[i];
- h += ( h << 10 );
- h ^= ( h >> 6 );
- }
- h += ( h << 3 );
- h ^= ( h >> 11 );
- h += ( h << 15 );
- return h;
- }
- static gboolean
- ipv6_equal(gconstpointer v1, gconstpointer v2)
- {
- if( memcmp(v1, v2, sizeof (struct e_in6_addr)) == 0 ) {
- return TRUE;
- }
- return FALSE;
- }
- /*
- * Flag controlling what names to resolve.
- */
- e_addr_resolve gbl_resolv_flags = {TRUE, FALSE, TRUE, TRUE, TRUE, FALSE};
- #if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
- static guint name_resolve_concurrency = 500;
- #endif
- /*
- * Global variables (can be changed in GUI sections)
- * XXX - they could be changed in GUI code, but there's currently no
- * GUI code to change them.
- */
- gchar *g_ethers_path = NULL; /* global ethers file */
- gchar *g_pethers_path = NULL; /* personal ethers file */
- gchar *g_ipxnets_path = NULL; /* global ipxnets file */
- gchar *g_pipxnets_path = NULL; /* personal ipxnets file */
- gchar *g_services_path = NULL; /* global services file */
- gchar *g_pservices_path = NULL; /* personal services file */
- /* first resolving call */
- /* c-ares */
- #ifdef HAVE_C_ARES
- /*
- * Submitted queries trigger a callback (c_ares_ghba_cb()).
- * Queries are added to c_ares_queue_head. During processing, queries are
- * popped off the front of c_ares_queue_head and submitted using
- * ares_gethostbyaddr().
- * The callback processes the response, then frees the request.
- */
- #define ASYNC_DNS
- typedef struct _async_dns_queue_msg
- {
- union {
- guint32 ip4;
- struct e_in6_addr ip6;
- } addr;
- int family;
- } async_dns_queue_msg_t;
- typedef struct _async_hostent {
- int addr_size;
- int copied;
- void *addrp;
- } async_hostent_t;
- #if ( ( ARES_VERSION_MAJOR < 1 ) \
- || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
- static void c_ares_ghba_cb(void *arg, int status, struct hostent *hostent);
- #else
- static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
- #endif
- ares_channel ghba_chan; /* ares_gethostbyaddr -- Usually non-interactive, no timeout */
- ares_channel ghbn_chan; /* ares_gethostbyname -- Usually interactive, timeout */
- #else
- /* GNU ADNS */
- #ifdef HAVE_GNU_ADNS
- #define ASYNC_DNS
- /*
- * Submitted queries have to be checked individually using adns_check().
- * Queries are added to adns_queue_head. During processing, the list is
- * iterated twice: once to request queries up to the concurrency limit,
- * and once to check the status of each query.
- */
- adns_state ads;
- typedef struct _async_dns_queue_msg
- {
- gboolean submitted;
- guint32 ip4_addr;
- int type;
- adns_query query;
- } async_dns_queue_msg_t;
- #endif /* HAVE_GNU_ADNS */
- #endif /* HAVE_C_ARES */
- #ifdef ASYNC_DNS
- static gboolean async_dns_initialized = FALSE;
- static guint async_dns_in_flight = 0;
- static GList *async_dns_queue_head = NULL;
- /* push a dns request */
- static void
- add_async_dns_ipv4(int type, guint32 addr)
- {
- async_dns_queue_msg_t *msg;
- msg = g_new(async_dns_queue_msg_t,1);
- #ifdef HAVE_C_ARES
- msg->family = type;
- msg->addr.ip4 = addr;
- #else
- msg->type = type;
- msg->ip4_addr = addr;
- msg->submitted = FALSE;
- #endif
- async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
- }
- #endif
- typedef struct {
- guint32 mask;
- gsize mask_length;
- const gchar* name; /* Shallow copy */
- } subnet_entry_t;
- /*
- * Miscellaneous functions
- */
- static int
- fgetline(char **buf, int *size, FILE *fp)
- {
- int len;
- int c;
- if (fp == NULL || buf == NULL)
- return -1;
- if (*buf == NULL) {
- if (*size == 0)
- *size = BUFSIZ;
- *buf = (char *)g_malloc(*size);
- }
- g_assert(*buf);
- g_assert(*size > 0);
- if (feof(fp))
- return -1;
- len = 0;
- while ((c = getc(fp)) != EOF && c != '\r' && c != '\n') {
- if (len+1 >= *size) {
- *buf = (char *)g_realloc(*buf, *size += BUFSIZ);
- }
- (*buf)[len++] = c;
- }
- if (len == 0 && c == EOF)
- return -1;
- (*buf)[len] = '\0';
- return len;
- } /* fgetline */
- /*
- * Local function definitions
- */
- static subnet_entry_t subnet_lookup(const guint32 addr);
- static void subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name);
- static void
- add_service_name(port_type proto, const guint port, const char *service_name)
- {
- serv_port_t *serv_port_table;
- int *key;
- key = (int *)g_new(int, 1);
- *key = port;
- serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
- if (serv_port_table == NULL) {
- serv_port_table = g_new0(serv_port_t,1);
- g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
- }
- else {
- g_free(key);
- }
- switch(proto){
- case PT_TCP:
- g_free(serv_port_table->tcp_name);
- serv_port_table->tcp_name = g_strdup(service_name);
- break;
- case PT_UDP:
- g_free(serv_port_table->udp_name);
- serv_port_table->udp_name = g_strdup(service_name);
- break;
- case PT_SCTP:
- g_free(serv_port_table->sctp_name);
- serv_port_table->sctp_name = g_strdup(service_name);
- break;
- case PT_DCCP:
- g_free(serv_port_table->dccp_name);
- serv_port_table->dccp_name = g_strdup(service_name);
- break;
- default:
- return;
- /* Should not happen */
- }
- new_resolved_objects = TRUE;
- }
- static void
- parse_service_line (char *line)
- {
- /*
- * See the services(4) or services(5) man page for services file format
- * (not available on all systems).
- */
- gchar *cp;
- gchar *service;
- gchar *port;
- port_type proto;
- range_t *port_rng = NULL;
- guint32 max_port = MAX_UDP_PORT;
- if ((cp = strchr(line, '#')))
- *cp = '\0';
- if ((cp = strtok(line, " \t")) == NULL)
- return;
- service = cp;
- if ((cp = strtok(NULL, " \t")) == NULL)
- return;
- port = cp;
- if (strtok(cp, "/") == NULL)
- return;
- if ((cp = strtok(NULL, "/")) == NULL)
- return;
- /* seems we got all interesting things from the file */
- if(strcmp(cp, "tcp") == 0) {
- max_port = MAX_TCP_PORT;
- proto = PT_TCP;
- }
- else if(strcmp(cp, "udp") == 0) {
- max_port = MAX_UDP_PORT;
- proto = PT_UDP;
- }
- else if(strcmp(cp, "sctp") == 0) {
- max_port = MAX_SCTP_PORT;
- proto = PT_SCTP;
- }
- else if(strcmp(cp, "dccp") == 0) {
- max_port = MAX_DCCP_PORT;
- proto = PT_DCCP;
- } else {
- return;
- }
- if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
- /* some assertion here? */
- return;
- }
- cb_service = service;
- cb_proto = proto;
- range_foreach(port_rng, add_serv_port_cb);
- g_free (port_rng);
- cb_proto = PT_NONE;
- } /* parse_service_line */
- static void
- add_serv_port_cb(const guint32 port)
- {
- if ( port ) {
- add_service_name(cb_proto, port, cb_service);
- }
- }
- static void
- parse_services_file(const char * path)
- {
- FILE *serv_p;
- static int size = 0;
- static char *buf = NULL;
- /* services hash table initialization */
- serv_p = ws_fopen(path, "r");
- if (serv_p == NULL)
- return;
- while (fgetline(&buf, &size, serv_p) >= 0) {
- parse_service_line (buf);
- }
- fclose(serv_p);
- }
- /* -----------------
- * unsigned integer to ascii
- */
- static gchar *
- ep_utoa(guint port)
- {
- gchar *bp = (gchar *)ep_alloc(MAXNAMELEN);
- /* XXX, guint32_to_str() ? */
- guint32_to_str_buf(port, bp, MAXNAMELEN);
- return bp;
- }
- static gchar
- *serv_name_lookup(const guint port, const port_type proto)
- {
- const char *serv_proto;
- struct servent *servp;
- serv_port_t *serv_port_table;
- gchar *name;
- switch(proto) {
- case PT_UDP:
- serv_proto = "udp";
- break;
- case PT_TCP:
- serv_proto = "tcp";
- break;
- case PT_SCTP:
- serv_proto = "sctp";
- break;
- case PT_DCCP:
- serv_proto = "dcp";
- break;
- default:
- /* not yet implemented */
- return NULL;
- /*NOTREACHED*/
- } /* proto */
- serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
- if(serv_port_table){
- /* Set which table we should look up port in */
- switch(proto) {
- case PT_UDP:
- if(serv_port_table->udp_name){
- return serv_port_table->udp_name;
- }
- break;
- case PT_TCP:
- if(serv_port_table->tcp_name){
- return serv_port_table->tcp_name;
- }
- break;
- case PT_SCTP:
- if(serv_port_table->sctp_name){
- return serv_port_table->sctp_name;
- }
- break;
- case PT_DCCP:
- if(serv_port_table->dccp_name){
- return serv_port_table->dccp_name;
- }
- break;
- default:
- /* not yet implemented */
- return NULL;
- /*NOTREACHED*/
- } /* proto */
- }
- if ((!gbl_resolv_flags.transport_name) ||
- (servp = getservbyport(g_htons(port), serv_proto)) == NULL) {
- /* unknown port */
- name = (gchar*)g_malloc(16);
- guint32_to_str_buf(port, name, 16);
- }else{
- name = g_strdup(servp->s_name);
- }
- if(serv_port_table == NULL){
- int *key;
- key = (int *)g_new(int, 1);
- *key = port;
- serv_port_table = g_new0(serv_port_t,1);
- g_hash_table_insert(serv_port_hashtable, key, serv_port_table);
- }
- switch(proto) {
- case PT_UDP:
- serv_port_table->udp_name = name;
- break;
- case PT_TCP:
- serv_port_table->tcp_name = name;
- break;
- case PT_SCTP:
- serv_port_table->sctp_name = name;
- break;
- case PT_DCCP:
- serv_port_table->dccp_name = name;
- break;
- default:
- return NULL;
- /*NOTREACHED*/
- }
- return name;
- } /* serv_name_lookup */
- static void
- destroy_serv_port(gpointer data)
- {
- serv_port_t *table = (serv_port_t*)data;
- g_free(table->udp_name);
- g_free(table->tcp_name);
- g_free(table->sctp_name);
- g_free(table->dccp_name);
- g_free(table);
- }
- static void
- initialize_services(void)
- {
- /* the hash table won't ignore duplicates, so use the personal path first */
- g_assert(serv_port_hashtable == NULL);
- serv_port_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, destroy_serv_port);
- /* set personal services path */
- if (g_pservices_path == NULL)
- g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
- parse_services_file(g_pservices_path);
- /* Compute the pathname of the services file. */
- if (g_services_path == NULL) {
- g_services_path = get_datafile_path(ENAME_SERVICES);
- }
- parse_services_file(g_services_path);
- } /* initialize_services */
- static void
- service_name_lookup_cleanup(void)
- {
- if(serv_port_hashtable){
- g_hash_table_destroy(serv_port_hashtable);
- serv_port_hashtable = NULL;
- }
- }
- /* Fill in an IP4 structure with info from subnets file or just with the
- * string form of the address.
- */
- static void
- fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
- {
- subnet_entry_t subnet_entry;
- if (tp->is_dummy_entry)
- return; /* already done */
- tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
- /* Do we have a subnet for this address? */
- subnet_entry = subnet_lookup(addr);
- if(0 != subnet_entry.mask) {
- /* Print name, then '.' then IP address after subnet mask */
- guint32 host_addr;
- gchar buffer[MAX_IP_STR_LEN];
- gchar* paddr;
- gsize i;
- host_addr = addr & (~(guint32)subnet_entry.mask);
- ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
- paddr = buffer;
- /* Skip to first octet that is not totally masked
- * If length of mask is 32, we chomp the whole address.
- * If the address string starts '.' (should not happen?),
- * we skip that '.'.
- */
- i = subnet_entry.mask_length / 8;
- while(*(paddr) != '\0' && i > 0) {
- if(*(++paddr) == '.') {
- --i;
- }
- }
- /* There are more efficient ways to do this, but this is safe if we
- * trust g_snprintf and MAXNAMELEN
- */
- g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
- } else {
- ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
- }
- }
- #ifdef HAVE_C_ARES
- static void
- c_ares_ghba_cb(
- void *arg,
- int status,
- #if ( ( ARES_VERSION_MAJOR < 1 ) \
- || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
- struct hostent *he
- #else
- int timeouts _U_,
- struct hostent *he
- #endif
- ) {
- async_dns_queue_msg_t *caqm = (async_dns_queue_msg_t *)arg;
- char **p;
- if (!caqm) return;
- /* XXX, what to do if async_dns_in_flight == 0? */
- async_dns_in_flight--;
- if (status == ARES_SUCCESS) {
- for (p = he->h_addr_list; *p != NULL; p++) {
- switch(caqm->family) {
- case AF_INET:
- add_ipv4_name(caqm->addr.ip4, he->h_name);
- break;
- case AF_INET6:
- add_ipv6_name(&caqm->addr.ip6, he->h_name);
- break;
- default:
- /* Throw an exception? */
- break;
- }
- }
- }
- g_free(caqm);
- }
- #endif /* HAVE_C_ARES */
- /* --------------- */
- static hashipv4_t *
- new_ipv4(const guint addr)
- {
- hashipv4_t *tp = g_new(hashipv4_t, 1);
- tp->addr = addr;
- tp->resolve = FALSE;
- tp->is_dummy_entry = FALSE;
- ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
- return tp;
- }
- static hashipv4_t *
- host_lookup(const guint addr, gboolean *found)
- {
- hashipv4_t * volatile tp;
- struct hostent *hostp;
- *found = TRUE;
- tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, &addr);
- if(tp == NULL){
- int *key;
- key = (int *)g_new(int, 1);
- *key = addr;
- tp = new_ipv4(addr);
- g_hash_table_insert(ipv4_hash_table, key, tp);
- }else{
- if (tp->is_dummy_entry && !tp->resolve){
- goto try_resolv;
- }
- if (tp->is_dummy_entry){
- *found = FALSE;
- }
- return tp;
- }
- try_resolv:
- if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
- tp->resolve = TRUE;
- #ifdef ASYNC_DNS
- if (gbl_resolv_flags.concurrent_dns &&
- name_resolve_concurrency > 0 &&
- async_dns_initialized) {
- add_async_dns_ipv4(AF_INET, addr);
- /* XXX found is set to TRUE, which seems a bit odd, but I'm not
- * going to risk changing the semantics.
- */
- fill_dummy_ip4(addr, tp);
- return tp;
- }
- #endif /* ASYNC_DNS */
- /*
- * The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
- * the name of the host on which it's running; to work around that
- * botch, we don't try to translate an all-zero IP address to a host
- * name.
- */
- if (addr != 0) {
- /* Use async DNS if possible, else fall back to timeouts,
- * else call gethostbyaddr and hope for the best
- */
- hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
- if (hostp != NULL && hostp->h_name[0] != '\0') {
- g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
- tp->is_dummy_entry = FALSE;
- return tp;
- }
- }
- /* unknown host or DNS timeout */
- }
- *found = FALSE;
- fill_dummy_ip4(addr, tp);
- return tp;
- } /* host_lookup */
- /* --------------- */
- static hashipv6_t *
- new_ipv6(const struct e_in6_addr *addr)
- {
- hashipv6_t *tp = g_new(hashipv6_t,1);
- tp->addr = *addr;
- tp->resolve = FALSE;
- tp->is_dummy_entry = FALSE;
- ip6_to_str_buf(addr, tp->ip6);
- return tp;
- }
- /* ------------------------------------ */
- static hashipv6_t *
- host_lookup6(const struct e_in6_addr *addr, gboolean *found)
- {
- hashipv6_t * volatile tp;
- #ifdef INET6
- #ifdef HAVE_C_ARES
- async_dns_queue_msg_t *caqm;
- #endif /* HAVE_C_ARES */
- struct hostent *hostp;
- #endif /* INET6 */
- *found = TRUE;
- tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
- if(tp == NULL){
- struct e_in6_addr *addr_key;
- addr_key = g_new(struct e_in6_addr,1);
- tp = new_ipv6(addr);
- memcpy(addr_key, addr, 16);
- g_hash_table_insert(ipv6_hash_table, addr_key, tp);
- }else{
- if (tp->is_dummy_entry && !tp->resolve){
- goto try_resolv;
- }
- if (tp->is_dummy_entry){
- *found = FALSE;
- }
- return tp;
- }
- try_resolv:
- if (gbl_resolv_flags.network_name &&
- gbl_resolv_flags.use_external_net_name_resolver) {
- tp->resolve = TRUE;
- #ifdef INET6
- #ifdef HAVE_C_ARES
- if ((gbl_resolv_flags.concurrent_dns) &&
- name_resolve_concurrency > 0 &&
- async_dns_initialized) {
- caqm = g_new(async_dns_queue_msg_t,1);
- caqm->family = AF_INET6;
- memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
- async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) caqm);
- /* XXX found is set to TRUE, which seems a bit odd, but I'm not
- * going to risk changing the semantics.
- */
- if (!tp->is_dummy_entry) {
- g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
- ip6_to_str_buf(addr, tp->name);
- tp->is_dummy_entry = TRUE;
- }
- return tp;
- }
- #endif /* HAVE_C_ARES */
- /* Quick hack to avoid DNS/YP timeout */
- hostp = gethostbyaddr((const char *)addr, sizeof(*addr), AF_INET6);
- if (hostp != NULL && hostp->h_name[0] != '\0') {
- g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
- tp->is_dummy_entry = FALSE;
- return tp;
- }
- #endif /* INET6 */
- }
- /* unknown host or DNS timeout */
- if (!tp->is_dummy_entry) {
- tp->is_dummy_entry = TRUE;
- g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
- }
- *found = FALSE;
- return tp;
- } /* host_lookup6 */
- static const gchar *
- solve_address_to_name(const address *addr)
- {
- switch (addr->type) {
- case AT_ETHER:
- return get_ether_name((const guint8 *)addr->data);
- case AT_IPv4: {
- guint32 ip4_addr;
- memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
- return get_hostname(ip4_addr);
- }
- case AT_IPv6: {
- struct e_in6_addr ip6_addr;
- memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
- return get_hostname6(&ip6_addr);
- }
- case AT_STRINGZ:
- return (const gchar *)addr->data;
- default:
- return NULL;
- }
- }
- static const gchar *
- se_solve_address_to_name(const address *addr)
- {
- switch (addr->type) {
- case AT_ETHER:
- return get_ether_name((const guint8 *)addr->data);
- case AT_IPv4: {
- guint32 ip4_addr;
- memcpy(&ip4_addr, addr->data, sizeof ip4_addr);
- return get_hostname(ip4_addr);
- }
- case AT_IPv6: {
- struct e_in6_addr ip6_addr;
- memcpy(&ip6_addr.bytes, addr->data, sizeof ip6_addr.bytes);
- return get_hostname6(&ip6_addr);
- }
- case AT_STRINGZ:
- return se_strdup((const gchar *)addr->data);
- default:
- return NULL;
- }
- }
- /*
- * Ethernet / manufacturer resolution
- *
- * The following functions implement ethernet address resolution and
- * ethers files parsing (see ethers(4)).
- *
- * The manuf file has the same format as ethers(4) except that names are
- * truncated to MAXMANUFLEN-1 (8) characters and that an address contains
- * only 3 bytes (instead of 6).
- *
- * Notes:
- *
- * I decide to not use the existing functions (see ethers(3) on some
- * operating systems) for the following reasons:
- * - performance gains (use of hash tables and some other enhancements),
- * - use of two ethers files (system-wide and per user),
- * - avoid the use of NIS maps,
- * - lack of these functions on some systems.
- *
- * So the following functions do _not_ behave as the standard ones.
- *
- * -- Laurent.
- */
- /*
- * If "manuf_file" is FALSE, parse a 6-byte MAC address.
- * If "manuf_file" is TRUE, parse an up-to-6-byte sequence with an optional
- * mask.
- */
- static gboolean
- parse_ether_address(const char *cp, ether_t *eth, unsigned int *mask,
- const gboolean manuf_file)
- {
- int i;
- unsigned long num;
- char *p;
- char sep = '\0';
- for (i = 0; i < 6; i++) {
- /* Get a hex number, 1 or 2 digits, no sign characters allowed. */
- if (!isxdigit((unsigned char)*cp))
- return FALSE;
- num = strtoul(cp, &p, 16);
- if (p == cp)
- return FALSE; /* failed */
- if (num > 0xFF)
- return FALSE; /* not a valid octet */
- eth->addr[i] = (guint8) num;
- cp = p; /* skip past the number */
- /* OK, what character terminated the octet? */
- if (*cp == '/') {
- /* "/" - this has a mask. */
- if (!manuf_file) {
- /* Entries with masks are allowed only in the "manuf" files. */
- return FALSE;
- }
- cp++; /* skip past the '/' to get to the mask */
- if (!isdigit((unsigned char)*cp))
- return FALSE; /* no sign allowed */
- num = strtoul(cp, &p, 10);
- if (p == cp)
- return FALSE; /* failed */
- cp = p; /* skip past the number */
- if (*cp != '\0' && !isspace((unsigned char)*cp))
- return FALSE; /* bogus terminator */
- if (num == 0 || num >= 48)
- return FALSE; /* bogus mask */
- /* Mask out the bits not covered by the mask */
- *mask = (int)num;
- for (i = 0; num >= 8; i++, num -= 8)
- ; /* skip octets entirely covered by the mask */
- /* Mask out the first masked octet */
- eth->addr[i] &= (0xFF << (8 - num));
- i++;
- /* Mask out completely-masked-out octets */
- for (; i < 6; i++)
- eth->addr[i] = 0;
- return TRUE;
- }
- if (*cp == '\0') {
- /* We're at the end of the address, and there's no mask. */
- if (i == 2) {
- /* We got 3 bytes, so this is a manufacturer ID. */
- if (!manuf_file) {
- /* Manufacturer IDs are only allowed in the "manuf"
- files. */
- return FALSE;
- }
- /* Indicate that this is a manufacturer ID (0 is not allowed
- as a mask). */
- *mask = 0;
- return TRUE;
- }
- if (i == 5) {
- /* We got 6 bytes, so this is a MAC address.
- If we're reading one of the "manuf" files, indicate that
- this is a MAC address (48 is not allowed as a mask). */
- if (manuf_file)
- *mask = 48;
- return TRUE;
- }
- /* We didn't get 3 or 6 bytes, and there's no mask; this is
- illegal. */
- return FALSE;
- } else {
- if (sep == '\0') {
- /* We don't know the separator used in this number; it can either
- be ':', '-', or '.'. */
- if (*cp != ':' && *cp != '-' && *cp != '.')
- return FALSE;
- sep = *cp; /* subsequent separators must be the same */
- } else {
- /* It has to be the same as the first separator */
- if (*cp != sep)
- return FALSE;
- }
- }
- cp++;
- }
- return TRUE;
- }
- static int
- parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
- const gboolean manuf_file)
- {
- /*
- * See the ethers(4) or ethers(5) man page for ethers file format
- * (not available on all systems).
- * We allow both ethernet address separators (':' and '-'),
- * as well as Wireshark's '.' separator.
- */
- gchar *cp;
- if ((cp = strchr(line, '#')))
- *cp = '\0';
- if ((cp = strtok(line, " \t")) == NULL)
- return -1;
- if (!parse_ether_address(cp, eth, mask, manuf_file))
- return -1;
- if ((cp = strtok(NULL, " \t")) == NULL)
- return -1;
- g_strlcpy(eth->name, cp, MAXNAMELEN);
- return 0;
- } /* parse_ether_line */
- static FILE *eth_p = NULL;
- static void
- set_ethent(char *path)
- {
- if (eth_p)
- rewind(eth_p);
- else
- eth_p = ws_fopen(path, "r");
- }
- static void
- end_ethent(void)
- {
- if (eth_p) {
- fclose(eth_p);
- eth_p = NULL;
- }
- }
- static ether_t *
- get_ethent(unsigned int *mask, const gboolean manuf_file)
- {
- static ether_t eth;
- static int size = 0;
- static char *buf = NULL;
- if (eth_p == NULL)
- return NULL;
- while (fgetline(&buf, &size, eth_p) >= 0) {
- if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
- return ð
- }
- }
- return NULL;
- } /* get_ethent */
- #if 0
- static ether_t *
- get_ethbyname(const gchar *name)
- {
- ether_t *eth;
- set_ethent(g_pethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
- ;
- if (eth == NULL) {
- end_ethent();
- set_ethent(g_ethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
- ;
- end_ethent();
- }
- return eth;
- } /* get_ethbyname */
- #endif
- static ether_t *
- get_ethbyaddr(const guint8 *addr)
- {
- ether_t *eth;
- set_ethent(g_pethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
- ;
- if (eth == NULL) {
- end_ethent();
- set_ethent(g_ethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
- ;
- end_ethent();
- }
- return eth;
- } /* get_ethbyaddr */
- static void
- add_manuf_name(const guint8 *addr, unsigned int mask, gchar *name)
- {
- guint8 oct;
- gint64 eth_as_int64, *wka_key;
- int eth_as_int, *manuf_key;
- /*
- * XXX - can we use Standard Annotation Language annotations to
- * note that mask, as returned by parse_ethe)r_address() (and thus
- * by the routines that call it, and thus passed to us) cannot be > 48,
- * or is SAL too weak to express that?
- */
- if (mask >= 48) {
- /* This is a well-known MAC address; just add this to the Ethernet
- hash table */
- add_eth_name(addr, name);
- return;
- }
- eth_as_int64 = addr[0];
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[1];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[2];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[3];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[4];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[5];
- eth_as_int64 = eth_as_int64 | oct;
- if (mask == 0) {
- /* This is a manufacturer ID; add it to the manufacturer ID hash table */
- /* manuf needs only the 3 most significant octets of the ethernet address */
- manuf_key = (int *)g_new(int, 1);
- eth_as_int = (int)(eth_as_int64>>24)&0xffffff;
- *manuf_key = eth_as_int;
- g_hash_table_insert(manuf_hashtable, manuf_key, g_strdup(name));
- return;
- } /* mask == 0 */
- /* This is a range of well-known addresses; add it to the appropriate
- well-known-address table, creating that table if necessary. */
- wka_key = (gint64 *)g_new(gint64, 1);
- *wka_key = eth_as_int64;
- g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
- } /* add_manuf_name */
- gchar *
- manuf_name_lookup(const guint8 *addr)
- {
- gint32 manuf_key = 0;
- guint8 oct;
- gchar *name;
- /* manuf needs only the 3 most significant octets of the ethernet address */
- manuf_key = addr[0];
- manuf_key = manuf_key<<8;
- oct = addr[1];
- manuf_key = manuf_key | oct;
- manuf_key = manuf_key<<8;
- oct = addr[2];
- manuf_key = manuf_key | oct;
- /* first try to find a "perfect match" */
- name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
- if(name != NULL){
- return name;
- }
- /* Mask out the broadcast/multicast flag but not the locally
- * administered flag as localy administered means: not assigend
- * by the IEEE but the local administrator instead.
- * 0x01 multicast / broadcast bit
- * 0x02 locally administered bit */
- if((manuf_key & 0x00010000) != 0){
- manuf_key &= 0x00FEFFFF;
- name = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
- if(name != NULL){
- return name;
- }
- }
- return NULL;
- } /* manuf_name_lookup */
- static gchar *
- wka_name_lookup(const guint8 *addr, const unsigned int mask)
- {
- guint8 masked_addr[6];
- guint num;
- gint i;
- gint64 eth_as_int64;
- guint8 oct;
- gchar *name;
- if(wka_hashtable == NULL){
- return NULL;
- }
- /* Get the part of the address covered by the mask. */
- for (i = 0, num = mask; num >= 8; i++, num -= 8)
- masked_addr[i] = addr[i]; /* copy octets entirely covered by the mask */
- /* Mask out the first masked octet */
- masked_addr[i] = addr[i] & (0xFF << (8 - num));
- i++;
- /* Zero out completely-masked-out octets */
- for (; i < 6; i++)
- masked_addr[i] = 0;
- eth_as_int64 = masked_addr[0];
- eth_as_int64 = eth_as_int64<<8;
- oct = masked_addr[1];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = masked_addr[2];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = masked_addr[3];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = masked_addr[4];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = masked_addr[5];
- eth_as_int64 = eth_as_int64 | oct;
- name = (gchar *)g_hash_table_lookup(wka_hashtable, ð_as_int64);
- return name;
- } /* wka_name_lookup */
- static void
- initialize_ethers(void)
- {
- ether_t *eth;
- char *manuf_path;
- guint mask;
- /* hash table initialization */
- wka_hashtable = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, g_free);
- manuf_hashtable = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
- eth_hashtable = g_hash_table_new_full(g_int64_hash, g_int64_equal, g_free, g_free);
- /* Compute the pathname of the ethers file. */
- if (g_ethers_path == NULL) {
- g_ethers_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
- get_systemfile_dir(), ENAME_ETHERS);
- }
- /* Set g_pethers_path here, but don't actually do anything
- * with it. It's used in get_ethbyname() and get_ethbyaddr()
- */
- if (g_pethers_path == NULL)
- g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE);
- /* Compute the pathname of the manuf file */
- manuf_path = get_datafile_path(ENAME_MANUF);
- /* Read it and initialize the hash table */
- set_ethent(manuf_path);
- while ((eth = get_ethent(&mask, TRUE))) {
- add_manuf_name(eth->addr, mask, eth->name);
- }
- end_ethent();
- g_free(manuf_path);
- } /* initialize_ethers */
- /* this is only needed when shuting down application (if at all) */
- static void
- eth_name_lookup_cleanup(void)
- {
- if(manuf_hashtable) {
- g_hash_table_destroy(manuf_hashtable);
- manuf_hashtable = NULL;
- }
- if(wka_hashtable) {
- g_hash_table_destroy(wka_hashtable);
- wka_hashtable = NULL;
- }
- if(eth_hashtable) {
- g_hash_table_destroy(eth_hashtable);
- eth_hashtable = NULL;
- }
- }
- /* Resolve ethernet address */
- static hashether_t *
- eth_addr_resolve(hashether_t *tp) {
- ether_t *eth;
- const guint8 *addr = tp->addr;
- if ( (eth = get_ethbyaddr(addr)) != NULL) {
- g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
- tp->status = HASHETHER_STATUS_RESOLVED_NAME;
- return tp;
- } else {
- guint mask;
- gchar *name;
- /* Unknown name. Try looking for it in the well-known-address
- tables for well-known address ranges smaller than 2^24. */
- mask = 7;
- for (;;) {
- /* Only the topmost 5 bytes participate fully */
- if ((name = wka_name_lookup(addr, mask+40)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x",
- name, addr[5] & (0xFF >> mask));
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- if (mask == 0)
- break;
- mask--;
- }
- mask = 7;
- for (;;) {
- /* Only the topmost 4 bytes participate fully */
- if ((name = wka_name_lookup(addr, mask+32)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x",
- name, addr[4] & (0xFF >> mask), addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- if (mask == 0)
- break;
- mask--;
- }
- mask = 7;
- for (;;) {
- /* Only the topmost 3 bytes participate fully */
- if ((name = wka_name_lookup(addr, mask+24)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
- name, addr[3] & (0xFF >> mask), addr[4], addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- if (mask == 0)
- break;
- mask--;
- }
- /* Now try looking in the manufacturer table. */
- if ((name = manuf_name_lookup(addr)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
- name, addr[3], addr[4], addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- /* Now try looking for it in the well-known-address
- tables for well-known address ranges larger than 2^24. */
- mask = 7;
- for (;;) {
- /* Only the topmost 2 bytes participate fully */
- if ((name = wka_name_lookup(addr, mask+16)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x",
- name, addr[2] & (0xFF >> mask), addr[3], addr[4],
- addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- if (mask == 0)
- break;
- mask--;
- }
- mask = 7;
- for (;;) {
- /* Only the topmost byte participates fully */
- if ((name = wka_name_lookup(addr, mask+8)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x",
- name, addr[1] & (0xFF >> mask), addr[2], addr[3],
- addr[4], addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- if (mask == 0)
- break;
- mask--;
- }
- for (mask = 7; mask > 0; mask--) {
- /* Not even the topmost byte participates fully */
- if ((name = wka_name_lookup(addr, mask)) != NULL) {
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x:%02x:%02x:%02x",
- name, addr[0] & (0xFF >> mask), addr[1], addr[2],
- addr[3], addr[4], addr[5]);
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- }
- /* No match whatsoever. */
- g_snprintf(tp->resolved_name, MAXNAMELEN, "%s", ether_to_str(addr));
- tp->status = HASHETHER_STATUS_RESOLVED_DUMMY;
- return tp;
- }
- g_assert_not_reached();
- } /* eth_addr_resolve */
- static gint64
- eth_to_int64(const guint8 *addr)
- {
- guint8 oct;
- gint64 eth_as_int64;
- eth_as_int64 = addr[0];
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[1];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[2];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[3];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[4];
- eth_as_int64 = eth_as_int64 | oct;
- eth_as_int64 = eth_as_int64<<8;
- oct = addr[5];
- eth_as_int64 = eth_as_int64 | oct;
- return eth_as_int64;
- }
- static hashether_t *
- eth_hash_new_entry(const guint8 *addr, const gboolean resolve)
- {
- hashether_t *tp;
- gint64 eth_as_int64, *key;
- eth_as_int64 = eth_to_int64(addr);
- key = (gint64 *)g_new(gint64, 1);
- *key = eth_as_int64;
- tp = g_new(hashether_t, 1);
- memcpy(tp->addr, addr, sizeof(tp->addr));
- tp->status = HASHETHER_STATUS_UNRESOLVED;
- g_strlcpy(tp->hexaddr, bytestring_to_str(addr, sizeof(tp->addr), ':'), sizeof(tp->hexaddr));
- tp->resolved_name[0] = '\0';
- if (resolve)
- eth_addr_resolve(tp);
- g_hash_table_insert(eth_hashtable, key, tp);
- return tp;
- } /* eth_hash_new_entry */
- static hashether_t *
- add_eth_name(const guint8 *addr, const gchar *name)
- {
- hashether_t *tp;
- gint64 eth_as_int64;
- eth_as_int64 = eth_to_int64(addr);
- tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, ð_as_int64);
- if( tp == NULL ){
- tp = eth_hash_new_entry(addr, FALSE);
- }
- g_strlcpy(tp->resolved_name, name, MAXNAMELEN);
- tp->status = HASHETHER_STATUS_RESOLVED_NAME;
- new_resolved_objects = TRUE;
- return tp;
- } /* add_eth_name */
- static hashether_t *
- eth_name_lookup(const guint8 *addr, const gboolean resolve)
- {
- hashether_t *tp;
- gint64 eth_as_int64;
- eth_as_int64 = eth_to_int64(addr);
- tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, ð_as_int64);
- if( tp == NULL ) {
- tp = eth_hash_new_entry(addr, resolve);
- } else {
- if (resolve && (tp->status == HASHETHER_STATUS_UNRESOLVED)){
- eth_addr_resolve(tp); /* Found but needs to be resolved */
- }
- }
- return tp;
- } /* eth_name_lookup */
- static guint8 *
- eth_addr_lookup(const gchar *name _U_)
- {
- #if 0
- /* XXX Do we need reverse lookup??? */
- ether_t *eth;
- hashether_t *tp;
- hashether_t **table = eth_table;
- gint i;
- /* to be optimized (hash table from name to addr) */
- for (i = 0; i < HASHETHSIZE; i++) {
- tp = table[i];
- while (tp) {
- if (strcmp(tp->resolved_name, name) == 0)
- return tp->addr;
- tp = tp->next;
- }
- }
- /* not in hash table : performs a file lookup */
- if ((eth = get_ethbyname(name)) == NULL)
- return NULL;
- /* add new entry in hash table */
- tp = add_eth_name(eth->addr, name);
- return tp->addr;
- #endif
- return NULL;
- } /* eth_addr_lookup */
- /* IPXNETS */
- static int
- parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
- {
- /*
- * We allow three address separators (':', '-', and '.'),
- * a…
Large files files are truncated, but you can click here to view the full file