/* addr_resolv.c
* Routines for network object lookup
*
- * $Id$
- *
* Laurent Deniel <laurent.deniel@free.fr>
*
* Wireshark - Network traffic analyzer
#include "config.h"
-#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
* 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....)
+ * So, for now, we do not use alarm() and SIGALRM to time out host name
+ * lookups. If we get a lot of complaints about lookups taking a long time,
+ * we can reconsider that decision. (Note that tcpdump originally added
+ * such a timeout mechanism 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.... tcpdump no
+ * longer does that, for the same reasons that we don't.)
+ *
+ * If we're using an asynchronous DNS resolver, that shouldn't be an issue.
+ * If we're using a synchronous name lookup mechanism (which we'd do mainly
+ * to support resolving addresses and host names using more mechanisms than
+ * just DNS, such as NIS, NBNS, or Mr. Hosts File), we could do that in
+ * a separate thread, making it, in effect, asynchronous.
*/
#ifdef HAVE_UNISTD_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
#include <winsock2.h> /* needed to define AF_ values on Windows */
#endif
-#ifdef NEED_INET_ATON_H
+#ifndef HAVE_INET_ATON
# include "wsutil/inet_aton.h"
#endif
#include "addr_and_mask.h"
#include "ipv6-utils.h"
#include "addr_resolv.h"
-#include "filesystem.h"
+#include "wsutil/filesystem.h"
#include <wsutil/report_err.h>
#include <wsutil/file_util.h>
+#include <wsutil/pint.h>
#include <epan/strutil.h>
+#include <epan/to_str-int.h>
#include <epan/prefs.h>
-#include <epan/emem.h>
#define ENAME_HOSTS "hosts"
#define ENAME_SUBNETS "subnets"
#define HASHETHSIZE 2048
#define HASHHOSTSIZE 2048
#define HASHIPXNETSIZE 256
-#define HASHMANUFSIZE 256
-#define HASHPORTSIZE 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))
-/*
- * 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;
-
-/* hash table used for IPv6 lookup */
-
-#define HASH_IPV6_ADDRESS(addr) \
- ((((addr).bytes[14] << 8)|((addr).bytes[15])) & (HASHHOSTSIZE - 1))
-
-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;
+
+typedef struct sub_net_hashipv4 {
+ guint addr;
+ guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
+ struct sub_net_hashipv4 *next;
+ gchar ip[16];
+ gchar name[MAXNAMELEN];
+} sub_net_hashipv4_t;
/* Array of entries of subnets of different lengths */
typedef struct {
- gsize mask_length; /*1-32*/
- guint32 mask; /* e.g. 255.255.255.*/
- hashipv4_t** subnet_addresses; /* Hash table of subnet addresses */
+ 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;
-/* hash table used for TCP/UDP/SCTP port lookup */
-
-#define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
-
#if 0
typedef struct serv_port {
- gchar *udp_name;
- gchar *tcp_name;
- gchar *sctp_name;
- gchar *dccp_name;
+ gchar *udp_name;
+ gchar *tcp_name;
+ gchar *sctp_name;
+ gchar *dccp_name;
} serv_port_t;
#endif
/* hash table used for IPX network lookup */
#define HASH_IPX_NET(net) ((net) & (HASHIPXNETSIZE - 1))
typedef struct hashipxnet {
- guint addr;
- struct hashipxnet *next;
- gchar name[MAXNAMELEN];
+ guint addr;
+ struct hashipxnet *next;
+ gchar name[MAXNAMELEN];
} hashipxnet_t;
/* hash tables used for ethernet and manufacturer lookup */
#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 */
+struct hashether {
+ guint status; /* (See above) */
+ guint8 addr[6];
+ char hexaddr[6*3];
+ char resolved_name[MAXNAMELEN];
+};
+struct hashmanuf {
+ guint status; /* (See above) */
+ guint8 addr[3];
+ char hexaddr[3*3];
+ char resolved_name[MAXNAMELEN];
+};
+
+/* internal ethernet type */
typedef struct _ether
{
- guint8 addr[6];
- char name[MAXNAMELEN];
+ guint8 addr[6];
+ char name[MAXNAMELEN];
} ether_t;
/* internal ipxnet type */
-
typedef struct _ipxnet
{
- guint addr;
- char name[MAXNAMELEN];
+ guint addr;
+ char name[MAXNAMELEN];
} ipxnet_t;
-static hashipv4_t *ipv4_table[HASHHOSTSIZE];
-static hashipv6_t *ipv6_table[HASHHOSTSIZE];
+static GHashTable *ipxnet_hash_table = NULL;
+static GHashTable *ipv4_hash_table = NULL;
+static GHashTable *ipv6_hash_table = NULL;
+
+static GSList *manually_resolved_ipv4_list = NULL;
+static GSList *manually_resolved_ipv6_list = NULL;
+
+typedef struct _resolved_ipv4
+{
+ guint32 host_addr;
+ char name[MAXNAMELEN];
+} resolved_ipv4_t;
+
+typedef struct _resolved_ipv6
+{
+ struct e_in6_addr ip6_addr;
+ char name[MAXNAMELEN];
+} resolved_ipv6_t;
+
+static addrinfo_lists_t addrinfo_lists = { NULL, NULL};
static gchar *cb_service;
static port_type cb_proto = PT_NONE;
static GHashTable *eth_hashtable = NULL;
static GHashTable *serv_port_hashtable = NULL;
-static hashipxnet_t *ipxnet_table[HASHIPXNETSIZE];
-
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};
+e_addr_resolve gbl_resolv_flags = {
+ TRUE, /* mac_name */
+ FALSE, /* network_name */
+ FALSE, /* transport_name */
+ TRUE, /* concurrent_dns */
+ TRUE, /* dns_pkt_addr_resolution */
+ TRUE, /* use_external_net_name_resolver */
+ FALSE /* load_hosts_file_from_profile_only */
+};
#if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
static guint name_resolve_concurrency = 500;
#endif
#define ASYNC_DNS
typedef struct _async_dns_queue_msg
{
- union {
- guint32 ip4;
- struct e_in6_addr ip6;
- } addr;
- int family;
+ 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;
+ int addr_size;
+ int copied;
+ void *addrp;
} async_hostent_t;
#if ( ( ARES_VERSION_MAJOR < 1 ) \
- || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
+ || ( 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);
typedef struct _async_dns_queue_msg
{
- gboolean submitted;
- guint32 ip4_addr;
- int type;
- adns_query query;
+ gboolean submitted;
+ guint32 ip4_addr;
+ int type;
+ adns_query query;
} async_dns_queue_msg_t;
#endif /* HAVE_GNU_ADNS */
static void
add_async_dns_ipv4(int type, guint32 addr)
{
- async_dns_queue_msg_t *msg;
+ async_dns_queue_msg_t *msg;
- msg = g_new(async_dns_queue_msg_t,1);
+ msg = g_new(async_dns_queue_msg_t,1);
#ifdef HAVE_C_ARES
- msg->family = type;
- msg->addr.ip4 = addr;
+ msg->family = type;
+ msg->addr.ip4 = addr;
#else
- msg->type = type;
- msg->ip4_addr = addr;
- msg->submitted = FALSE;
+ msg->type = type;
+ msg->ip4_addr = addr;
+ msg->submitted = FALSE;
#endif
- async_dns_queue_head = g_list_append(async_dns_queue_head, (gpointer) msg);
+ 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 */
+ guint32 mask;
+ gsize mask_length;
+ const gchar* name; /* Shallow copy */
} subnet_entry_t;
/*
static int
fgetline(char **buf, int *size, FILE *fp)
{
- int len;
- int c;
+ int len;
+ int c;
- if (fp == NULL || buf == NULL)
- return -1;
+ if (fp == NULL || buf == NULL)
+ return -1;
- if (*buf == NULL) {
- if (*size == 0)
- *size = BUFSIZ;
+ if (*buf == NULL) {
+ if (*size == 0)
+ *size = BUFSIZ;
- *buf = (char *)g_malloc(*size);
- }
+ *buf = (char *)g_malloc(*size);
+ }
- g_assert(*buf);
- g_assert(*size > 0);
+ g_assert(*buf);
+ g_assert(*size > 0);
- if (feof(fp))
- return -1;
+ 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);
+ 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;
}
- (*buf)[len++] = c;
- }
- if (len == 0 && c == EOF)
- return -1;
+ if (len == 0 && c == EOF)
+ return -1;
- (*buf)[len] = '\0';
+ (*buf)[len] = '\0';
- return len;
+ return len;
} /* fgetline */
static void
add_service_name(port_type proto, const guint port, const char *service_name)
{
- serv_port_t *serv_port_table;
- int *key;
+ serv_port_t *serv_port_table;
+ int *key;
- key = (int *)g_new(int, 1);
- *key = port;
+ 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);
- }
+ 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:
- serv_port_table->tcp_name = g_strdup(service_name);
- break;
- case PT_UDP:
- serv_port_table->udp_name = g_strdup(service_name);
- break;
- case PT_SCTP:
- serv_port_table->sctp_name = g_strdup(service_name);
- break;
- case PT_DCCP:
- serv_port_table->dccp_name = g_strdup(service_name);
- break;
- default:
- return;
- /* Should not happen */
- }
+ 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;
+ 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;
+ /*
+ * 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);
- }
+ 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;
+ FILE *serv_p;
+ static int size = 0;
+ static char *buf = NULL;
- /* services hash table initialization */
- serv_p = ws_fopen(path, "r");
+ /* services hash table initialization */
+ serv_p = ws_fopen(path, "r");
- if (serv_p == NULL)
- return;
+ if (serv_p == NULL)
+ return;
- while (fgetline(&buf, &size, serv_p) >= 0) {
- parse_service_line (buf);
- }
+ while (fgetline(&buf, &size, serv_p) >= 0) {
+ parse_service_line (buf);
+ }
- fclose(serv_p);
+ 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 */
+wmem_utoa(wmem_allocator_t *allocator, guint port)
+{
+ gchar *bp = (gchar *)wmem_alloc(allocator, MAXNAMELEN);
- serv_port_table = (serv_port_t *)g_hash_table_lookup(serv_port_hashtable, &port);
+ /* XXX, guint32_to_str() ? */
+ guint32_to_str_buf(port, bp, MAXNAMELEN);
+ return bp;
+}
- 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 */
+
+static const gchar *
+serv_name_lookup(const guint port, const port_type proto)
+{
+ serv_port_t *serv_port_table;
+ gchar *name;
+
+ 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 */
+ }
+
+ /* Use numerical port string */
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;
+ 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)
{
+#ifdef _WIN32
+ char *hostspath;
+ char *sysroot;
+ static char rootpath_nt[] = "\\system32\\drivers\\etc\\services";
+#endif /* _WIN32 */
+
+ /* 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);
+
+/* Read the system services file first */
+#ifdef _WIN32
+
+ sysroot = getenv_utf8("WINDIR");
+ if (sysroot != NULL) {
+ /*
+ * The file should be under WINDIR.
+ * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
+ * %WINDIR%\system32\drivers\etc\services.
+ */
+ hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
+ parse_services_file(hostspath);
+ g_free(hostspath);
+ }
+#else
+ parse_services_file("/etc/services");
- /* 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, g_free);
+#endif /* _WIN32 */
- /* set personal services path */
- if (g_pservices_path == NULL)
- g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
+ /* set personal services path */
+ if (g_pservices_path == NULL)
+ g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
- parse_services_file(g_pservices_path);
+ 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);
- }
+ /* 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);
+ 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;
- }
+ 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
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;
+ subnet_entry_t subnet_entry;
+
+ if (tp->flags & DUMMY_ADDRESS_ENTRY)
+ return; /* already done */
+
+ tp->flags |= DUMMY_ADDRESS_ENTRY; /* 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;
+ }
+ }
- /* 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);
}
-
- /* 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 ) )
-c_ares_ghba_cb(void *arg, int status, struct hostent *he) {
+ || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
+ struct hostent *he
#else
-c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
+ 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);
+ ) {
+
+ 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 = se_new(hashipv4_t);
- tp->addr = addr;
- tp->next = NULL;
- tp->resolve = FALSE;
- tp->is_dummy_entry = FALSE;
- ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
- return tp;
+ hashipv4_t *tp = g_new(hashipv4_t, 1);
+ tp->addr = addr;
+ tp->flags = 0;
+ tp->name[0] = '\0';
+ ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
+ return tp;
}
static hashipv4_t *
host_lookup(const guint addr, gboolean *found)
{
- int hash_idx;
- hashipv4_t * volatile tp;
- struct hostent *hostp;
+ hashipv4_t * volatile tp;
- *found = TRUE;
+ *found = TRUE;
- hash_idx = HASH_IPV4_ADDRESS(addr);
-
- tp = ipv4_table[hash_idx];
-
- if( tp == NULL ) {
- tp = ipv4_table[hash_idx] = new_ipv4(addr);
- } else {
- while(1) {
- if( tp->addr == addr ) {
- if (tp->is_dummy_entry && !tp->resolve)
- break;
- if (tp->is_dummy_entry)
- *found = FALSE;
+ tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
+ if (tp == NULL) {
+ tp = new_ipv4(addr);
+ g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
+ } else {
+ if ((tp->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
+ goto try_resolv;
+ }
+ if (tp->flags & DUMMY_ADDRESS_ENTRY) {
+ *found = FALSE;
+ }
return tp;
- }
- if (tp->next == NULL) {
- tp->next = new_ipv4(addr);
- tp = tp->next;
- break;
- }
- tp = tp->next;
- }
- }
-
- 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
- */
+try_resolv:
+ if (gbl_resolv_flags.network_name && gbl_resolv_flags.use_external_net_name_resolver) {
+ tp->flags |= TRIED_RESOLVE_ADDRESS;
- hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
+#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;
+ }
- if (hostp != NULL && hostp->h_name[0] != '\0') {
- g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
- tp->is_dummy_entry = FALSE;
- return tp;
- }
- }
+ /*
+ * 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.
+ *
+ * Presumably getaddrinfo() behaves the same way. Anyway, we should
+ * never get to this code on Windows since those builds include c-ares.
+ */
+#elif defined(HAVE_GETADDRINFO)
+ if (addr != 0) {
+ struct sockaddr_in sin;
+
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = addr;
+ if (getnameinfo((struct sockaddr *)&sin, sizeof(sin),
+ tp->name, sizeof(tp->name),
+ NULL, 0, NI_NAMEREQD) == 0) {
+ return tp;
+ }
+ }
+#elif defined(HAVE_GETHOSTBYNAME)
+ if (addr != 0) {
+ struct hostent *hostp;
- /* unknown host or DNS timeout */
+ hostp = gethostbyaddr((const char *)&addr, 4, AF_INET);
- }
+ if (hostp != NULL && hostp->h_name[0] != '\0') {
+ g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
+ return tp;
+ }
+ }
+#endif
- *found = FALSE;
+ /* unknown host or DNS timeout */
+ }
- fill_dummy_ip4(addr, tp);
- return tp;
+ *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 = se_new(hashipv6_t);
- tp->addr = *addr;
- tp->next = NULL;
- tp->resolve = FALSE;
- tp->is_dummy_entry = FALSE;
- ip6_to_str_buf(addr, tp->ip6);
- return tp;
+ hashipv6_t *tp = g_new(hashipv6_t,1);
+ tp->addr = *addr;
+ tp->flags = 0;
+ tp->name[0] = '\0';
+ ip6_to_str_buf(addr, tp->ip6);
+ return tp;
}
/* ------------------------------------ */
static hashipv6_t *
host_lookup6(const struct e_in6_addr *addr, gboolean *found)
{
- int hash_idx;
- hashipv6_t * volatile tp;
+ hashipv6_t * volatile tp;
#ifdef INET6
#ifdef HAVE_C_ARES
- async_dns_queue_msg_t *caqm;
-#endif /* HAVE_C_ARES */
- struct hostent *hostp;
+ async_dns_queue_msg_t *caqm;
+#elif defined(HAVE_GETADDRINFO)
+ struct sockaddr_in6 sin6;
+#elif defined(HAVE_GETHOSTBYNAME)
+ struct hostent *hostp;
+#endif
#endif /* INET6 */
- *found = TRUE;
+ *found = TRUE;
- hash_idx = HASH_IPV6_ADDRESS(*addr);
+ tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addr);
+ if (tp == NULL) {
+ struct e_in6_addr *addr_key;
- tp = ipv6_table[hash_idx];
-
- if( tp == NULL ) {
- tp = ipv6_table[hash_idx] = new_ipv6(addr);
- } else {
- while(1) {
- if( memcmp(&tp->addr, addr, sizeof (struct e_in6_addr)) == 0 ) {
- if (tp->is_dummy_entry && !tp->resolve)
- break;
- if (tp->is_dummy_entry)
- *found = FALSE;
+ 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->flags & DUMMY_AND_RESOLVE_FLGS) == DUMMY_ADDRESS_ENTRY) {
+ goto try_resolv;
+ }
+ if (tp->flags & DUMMY_ADDRESS_ENTRY) {
+ *found = FALSE;
+ }
return tp;
- }
- if (tp->next == NULL) {
- tp->next = new_ipv6(addr);
- tp = tp->next;
- break;
- }
- tp = tp->next;
- }
- }
-
- 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);
+try_resolv:
+ if (gbl_resolv_flags.network_name &&
+ gbl_resolv_flags.use_external_net_name_resolver) {
+ tp->flags |= TRIED_RESOLVE_ADDRESS;
+#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->flags & DUMMY_ADDRESS_ENTRY) == 0) {
+ g_strlcpy(tp->name, tp->ip6, MAXNAMELEN);
+ ip6_to_str_buf(addr, tp->name);
+ tp->flags |= DUMMY_ADDRESS_ENTRY;
+ }
+ return tp;
+ }
+#elif defined(HAVE_GETADDRINFO)
+ memset(&sin6, 0, sizeof(sin6));
+ sin6.sin6_family = AF_INET6;
+ memcpy(sin6.sin6_addr.s6_addr, addr, sizeof(*addr));
+ if (getnameinfo((struct sockaddr *)&sin6, sizeof(sin6),
+ tp->name, sizeof(tp->name),
+ NULL, 0, NI_NAMEREQD) == 0) {
+ return tp;
+ }
+#elif defined(HAVE_GETHOSTBYNAME)
+ /* 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;
- }
+ if (hostp != NULL && hostp->h_name[0] != '\0') {
+ g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
+ return tp;
+ }
+#endif
#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;
+ /* unknown host or DNS timeout */
+ if ((tp->flags & DUMMY_ADDRESS_ENTRY) == 0) {
+ tp->flags |= DUMMY_ADDRESS_ENTRY;
+ 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
*
*/
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;
+ 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 (!g_ascii_isxdigit(*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 (!g_ascii_isdigit(*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' && !g_ascii_isspace(*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;
}
- /* 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;
- }
+ 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++;
}
- cp++;
- }
- return TRUE;
+ return TRUE;
}
static int
parse_ether_line(char *line, ether_t *eth, unsigned int *mask,
- const gboolean manuf_file)
+ 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.
- */
+ /*
+ * 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;
+ gchar *cp;
- if ((cp = strchr(line, '#')))
- *cp = '\0';
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
- if ((cp = strtok(line, " \t")) == NULL)
- return -1;
+ if ((cp = strtok(line, " \t")) == NULL)
+ return -1;
- if (!parse_ether_address(cp, eth, mask, manuf_file))
- return -1;
+ if (!parse_ether_address(cp, eth, mask, manuf_file))
+ return -1;
- if ((cp = strtok(NULL, " \t")) == NULL)
- return -1;
+ if ((cp = strtok(NULL, " \t")) == NULL)
+ return -1;
- g_strlcpy(eth->name, cp, MAXNAMELEN);
+ g_strlcpy(eth->name, cp, MAXNAMELEN);
- return 0;
+ return 0;
} /* parse_ether_line */
static void
set_ethent(char *path)
{
- if (eth_p)
- rewind(eth_p);
- else
- eth_p = ws_fopen(path, "r");
+ 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;
- }
+ 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;
+ static ether_t eth;
+ static int size = 0;
+ static char *buf = NULL;
- if (eth_p == NULL)
- return NULL;
+ if (eth_p == NULL)
+ return NULL;
- while (fgetline(&buf, &size, eth_p) >= 0) {
- if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
- return ð
+ while (fgetline(&buf, &size, eth_p) >= 0) {
+ if (parse_ether_line(buf, ð, mask, manuf_file) == 0) {
+ return ð
+ }
}
- }
- return NULL;
+ return NULL;
} /* get_ethent */
static ether_t *
get_ethbyname(const gchar *name)
{
- ether_t *eth;
+ ether_t *eth;
- set_ethent(g_pethers_path);
+ set_ethent(g_pethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
- ;
+ while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
+ ;
- if (eth == NULL) {
- end_ethent();
+ if (eth == NULL) {
+ end_ethent();
- set_ethent(g_ethers_path);
+ set_ethent(g_ethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
- ;
+ while (((eth = get_ethent(NULL, FALSE)) != NULL) && strncmp(name, eth->name, MAXNAMELEN) != 0)
+ ;
- end_ethent();
- }
+ end_ethent();
+ }
- return eth;
+ return eth;
} /* get_ethbyname */
#endif
get_ethbyaddr(const guint8 *addr)
{
- ether_t *eth;
+ ether_t *eth;
- set_ethent(g_pethers_path);
+ set_ethent(g_pethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
- ;
+ while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
+ ;
- if (eth == NULL) {
- end_ethent();
+ if (eth == NULL) {
+ end_ethent();
- set_ethent(g_ethers_path);
+ set_ethent(g_ethers_path);
- while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
- ;
+ while (((eth = get_ethent(NULL, FALSE)) != NULL) && memcmp(addr, eth->addr, 6) != 0)
+ ;
- end_ethent();
- }
+ end_ethent();
+ }
- return eth;
+ return eth;
} /* get_ethbyaddr */
+static hashmanuf_t *manuf_hash_new_entry(const guint8 *addr, char* name)
+{
+ int *manuf_key;
+ hashmanuf_t *manuf_value;
+ char *endp;
+
+ /* manuf needs only the 3 most significant octets of the ethernet address */
+ manuf_key = (int *)g_new(int, 1);
+ *manuf_key = (int)((addr[0] << 16) + (addr[1] << 8) + addr[2]);
+ manuf_value = g_new(hashmanuf_t, 1);
+
+ memcpy(manuf_value->addr, addr, 3);
+ manuf_value->status = (name != NULL) ? HASHETHER_STATUS_RESOLVED_NAME : HASHETHER_STATUS_UNRESOLVED;
+ if (name != NULL) {
+ g_strlcpy(manuf_value->resolved_name, name, MAXNAMELEN);
+ manuf_value->status = HASHETHER_STATUS_RESOLVED_NAME;
+ }
+ else {
+ manuf_value->status = HASHETHER_STATUS_UNRESOLVED;
+ manuf_value->resolved_name[0] = '\0';
+ }
+ /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
+ endp = bytes_to_hexstr_punct(manuf_value->hexaddr, addr, sizeof(manuf_value->addr), ':');
+ *endp = '\0';
+
+ g_hash_table_insert(manuf_hashtable, manuf_key, manuf_value);
+ return manuf_value;
+}
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 */
+ guint8 *wka_key;
- /* 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;
+ /*
+ * 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;
+ }
- g_hash_table_insert(manuf_hashtable, manuf_key, g_strdup(name));
- return;
- } /* mask == 0 */
+ if (mask == 0) {
+ /* This is a manufacturer ID; add it to the manufacturer ID hash table */
+ manuf_hash_new_entry(addr, 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. */
+ /* 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;
+ wka_key = (guint8 *)g_malloc(6);
+ memcpy(wka_key, addr, 6);
- g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
+ g_hash_table_insert(wka_hashtable, wka_key, g_strdup(name));
} /* add_manuf_name */
-gchar *
+static hashmanuf_t *
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;
+ gint32 manuf_key = 0;
+ guint8 oct;
+ hashmanuf_t *manuf_value;
+ /* 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" */
+ manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
+ if (manuf_value != NULL) {
+ return manuf_value;
+ }
- /* 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;
+ /* 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;
+ manuf_value = (hashmanuf_t*)g_hash_table_lookup(manuf_hashtable, &manuf_key);
+ if (manuf_value != NULL) {
+ return manuf_value;
+ }
}
- }
- return NULL;
+ /* Add the address as a hex string */
+ return manuf_hash_new_entry(addr, 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;
+ guint8 masked_addr[6];
+ guint num;
+ gint i;
+ 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;
+ 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;
+
+ name = (gchar *)g_hash_table_lookup(wka_hashtable, masked_addr);
+
+ 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);
+guint get_hash_ether_status(hashether_t* ether)
+{
+ return ether->status;
+}
- /* 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);
- }
+char* get_hash_ether_hexaddr(hashether_t* ether)
+{
+ return ether->hexaddr;
+}
- /* 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);
+char* get_hash_ether_resolved_name(hashether_t* ether)
+{
+ return ether->resolved_name;
+}
+
+static guint
+eth_addr_hash(gconstpointer key)
+{
+ return wmem_strong_hash((const guint8 *)key, 6);
+}
+
+static gboolean
+eth_addr_cmp(gconstpointer a, gconstpointer b)
+{
+ return (memcmp(a, b, 6) == 0);
+}
+
+static void
+initialize_ethers(void)
+{
+ ether_t *eth;
+ char *manuf_path;
+ guint mask = 0;
+
+ /* hash table initialization */
+ wka_hashtable = g_hash_table_new_full(eth_addr_hash, eth_addr_cmp, 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(eth_addr_hash, eth_addr_cmp, NULL, g_free);
- /* Compute the pathname of the manuf file */
- manuf_path = get_datafile_path(ENAME_MANUF);
+ /* 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);
+ /* 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);
- }
+ while ((eth = get_ethent(&mask, TRUE))) {
+ add_manuf_name(eth->addr, mask, eth->name);
+ }
- end_ethent();
+ end_ethent();
- g_free(manuf_path);
+ g_free(manuf_path);
} /* initialize_ethers */
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 (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;
- }
+ 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;
+ ether_t *eth;
+ hashmanuf_t *manuf_value;
+ 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;
+ if ( (eth = get_ethbyaddr(addr)) != NULL) {
+ g_strlcpy(tp->resolved_name, eth->name, MAXNAMELEN);
+ tp->status = HASHETHER_STATUS_RESOLVED_NAME;
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]);
+ } else {
+ guint mask;
+ gchar *name;
+ address ether_addr;
+
+ /* 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. */
+ manuf_value = manuf_name_lookup(addr);
+ if ((manuf_value != NULL) && (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
+ g_snprintf(tp->resolved_name, MAXNAMELEN, "%s_%02x:%02x:%02x",
+ manuf_value->resolved_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. */
+ SET_ADDRESS(ðer_addr, AT_ETHER, 6, addr);
+ address_to_str_buf(ðer_addr, tp->resolved_name, MAXNAMELEN);
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();
+ g_assert_not_reached();
} /* eth_addr_resolve */
static hashether_t *
-eth_hash_new_entry(const guint8 *addr, const gboolean resolve) {
- hashether_t *tp;
- gint64 eth_as_int64, *key;
- guint8 oct;
-
- 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;
-
- 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(const guint8 *addr, const gboolean resolve)
+{
+ hashether_t *tp;
+ char *endp;
+
+ tp = g_new(hashether_t, 1);
+ memcpy(tp->addr, addr, sizeof(tp->addr));
+ tp->status = HASHETHER_STATUS_UNRESOLVED;
+ /* Values returned by bytes_to_hexstr_punct() are *not* null-terminated */
+ endp = bytes_to_hexstr_punct(tp->hexaddr, addr, sizeof(tp->addr), ':');
+ *endp = '\0';
+ tp->resolved_name[0] = '\0';
+
+ if (resolve)
+ eth_addr_resolve(tp);
+
+ g_hash_table_insert(eth_hashtable, tp->addr, 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, *key;
- guint8 oct;
-
- 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;
-
- key = (gint64 *)g_new(gint64, 1);
- *key = eth_as_int64;
-
- tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, key);
-
- 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;
+ hashether_t *tp;
+
+ tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
+
+ 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, *key;
- guint8 oct;
-
- 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;
-
- key = (gint64 *)g_new(gint64, 1);
- *key = eth_as_int64;
-
- tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, key);
- if( tp == NULL ) {
- tp = eth_hash_new_entry(addr, resolve);
- } else {
- eth_addr_resolve(tp); /* Found but needs to be resolved */
- }
-
- return tp;
+eth_name_lookup(const guint8 *addr, const gboolean resolve)
+{
+ hashether_t *tp;
+
+ tp = (hashether_t *)g_hash_table_lookup(eth_hashtable, addr);
+ 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 */
eth_addr_lookup(const gchar *name _U_)
{
#if 0
- 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;
+ /* 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 */
+ /* not in hash table : performs a file lookup */
- if ((eth = get_ethbyname(name)) == NULL)
- return NULL;
+ if ((eth = get_ethbyname(name)) == NULL)
+ return NULL;
- /* add new entry in hash table */
+ /* add new entry in hash table */
- tp = add_eth_name(eth->addr, name);
+ tp = add_eth_name(eth->addr, name);
- return tp->addr;
+ return tp->addr;
#endif
- return NULL;
+ return NULL;
} /* eth_addr_lookup */
static int
parse_ipxnets_line(char *line, ipxnet_t *ipxnet)
{
- /*
- * We allow three address separators (':', '-', and '.'),
- * as well as no separators
- */
-
- gchar *cp;
- guint32 a, a0, a1, a2, a3;
- gboolean found_single_number = FALSE;
-
- if ((cp = strchr(line, '#')))
- *cp = '\0';
-
- if ((cp = strtok(line, " \t\n")) == NULL)
- return -1;
-
- /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
- * fill a and found_single_number is TRUE,
- * or return -1
- */
- if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
- if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
- if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
- if (sscanf(cp, "%x", &a) == 1) {
- found_single_number = TRUE;
- }
- else {
- return -1;
+ /*
+ * We allow three address separators (':', '-', and '.'),
+ * as well as no separators
+ */
+
+ gchar *cp;
+ guint32 a, a0, a1, a2, a3;
+ gboolean found_single_number = FALSE;
+
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
+
+ if ((cp = strtok(line, " \t\n")) == NULL)
+ return -1;
+
+ /* Either fill a0,a1,a2,a3 and found_single_number is FALSE,
+ * fill a and found_single_number is TRUE,
+ * or return -1
+ */
+ if (sscanf(cp, "%x:%x:%x:%x", &a0, &a1, &a2, &a3) != 4) {
+ if (sscanf(cp, "%x-%x-%x-%x", &a0, &a1, &a2, &a3) != 4) {
+ if (sscanf(cp, "%x.%x.%x.%x", &a0, &a1, &a2, &a3) != 4) {
+ if (sscanf(cp, "%x", &a) == 1) {
+ found_single_number = TRUE;
+ }
+ else {
+ return -1;
+ }
+ }
}
- }
}
- }
- if ((cp = strtok(NULL, " \t\n")) == NULL)
- return -1;
+ if ((cp = strtok(NULL, " \t\n")) == NULL)
+ return -1;
- if (found_single_number) {
- ipxnet->addr = a;
- }
- else {
- ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
- }
+ if (found_single_number) {
+ ipxnet->addr = a;
+ }
+ else {
+ ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
+ }
- g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
+ g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
- return 0;
+ return 0;
} /* parse_ipxnets_line */
static void
set_ipxnetent(char *path)
{
- if (ipxnet_p)
- rewind(ipxnet_p);
- else
- ipxnet_p = ws_fopen(path, "r");
+ if (ipxnet_p)
+ rewind(ipxnet_p);
+ else
+ ipxnet_p = ws_fopen(path, "r");
}
static void
end_ipxnetent(void)
{
- if (ipxnet_p) {
- fclose(ipxnet_p);
- ipxnet_p = NULL;
- }
+ if (ipxnet_p) {
+ fclose(ipxnet_p);
+ ipxnet_p = NULL;
+ }
}
static ipxnet_t *
get_ipxnetent(void)
{
- static ipxnet_t ipxnet;
- static int size = 0;
- static char *buf = NULL;
+ static ipxnet_t ipxnet;
+ static int size = 0;
+ static char *buf = NULL;
- if (ipxnet_p == NULL)
- return NULL;
+ if (ipxnet_p == NULL)
+ return NULL;
- while (fgetline(&buf, &size, ipxnet_p) >= 0) {
- if (parse_ipxnets_line(buf, &ipxnet) == 0) {
- return &ipxnet;
+ while (fgetline(&buf, &size, ipxnet_p) >= 0) {
+ if (parse_ipxnets_line(buf, &ipxnet) == 0) {
+ return &ipxnet;
+ }
}
- }
- return NULL;
+ return NULL;
} /* get_ipxnetent */
+/* Unused ??? */
+#if 0
static ipxnet_t *
get_ipxnetbyname(const gchar *name)
{
- ipxnet_t *ipxnet;
+ ipxnet_t *ipxnet;
- set_ipxnetent(g_ipxnets_path);
+ set_ipxnetent(g_ipxnets_path);
- while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
- ;
+ while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
+ ;
- if (ipxnet == NULL) {
- end_ipxnetent();
+ if (ipxnet == NULL) {
+ end_ipxnetent();
- set_ipxnetent(g_pipxnets_path);
+ set_ipxnetent(g_pipxnets_path);
- while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
- ;
+ while (((ipxnet = get_ipxnetent()) != NULL) && strncmp(name, ipxnet->name, MAXNAMELEN) != 0)
+ ;
- end_ipxnetent();
- }
+ end_ipxnetent();
+ }
- return ipxnet;
+ return ipxnet;
} /* get_ipxnetbyname */
+#endif
static ipxnet_t *
get_ipxnetbyaddr(guint32 addr)
{
- ipxnet_t *ipxnet;
+ ipxnet_t *ipxnet;
- set_ipxnetent(g_ipxnets_path);
+ set_ipxnetent(g_ipxnets_path);
- while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
+ while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) ) ;
- if (ipxnet == NULL) {
- end_ipxnetent();
+ if (ipxnet == NULL) {
+ end_ipxnetent();
- set_ipxnetent(g_pipxnets_path);
+ set_ipxnetent(g_pipxnets_path);
- while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
- ;
+ while (((ipxnet = get_ipxnetent()) != NULL) && (addr != ipxnet->addr) )
+ ;
- end_ipxnetent();
- }
+ end_ipxnetent();
+ }
- return ipxnet;
+ return ipxnet;
} /* get_ipxnetbyaddr */
static void
initialize_ipxnets(void)
{
- /* Compute the pathname of the ipxnets file.
- *
- * XXX - is there a notion of an "ipxnets file" in any flavor of
- * UNIX, or with any add-on Netware package for UNIX? If not,
- * should the UNIX version of the ipxnets file be in the datafile
- * directory as well?
- */
- if (g_ipxnets_path == NULL) {
- g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
- get_systemfile_dir(), ENAME_IPXNETS);
- }
-
- /* Set g_pipxnets_path here, but don't actually do anything
- * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
- */
- if (g_pipxnets_path == NULL)
- g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
+ /* Compute the pathname of the ipxnets file.
+ *
+ * XXX - is there a notion of an "ipxnets file" in any flavor of
+ * UNIX, or with any add-on Netware package for UNIX? If not,
+ * should the UNIX version of the ipxnets file be in the datafile
+ * directory as well?
+ */
+ if (g_ipxnets_path == NULL) {
+ g_ipxnets_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
+ get_systemfile_dir(), ENAME_IPXNETS);
+ }
+
+ /* Set g_pipxnets_path here, but don't actually do anything
+ * with it. It's used in get_ipxnetbyname() and get_ipxnetbyaddr()
+ */
+ if (g_pipxnets_path == NULL)
+ g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE);
} /* initialize_ipxnets */
static void
ipx_name_lookup_cleanup(void)
{
- /* The memory pointed to by this table is se_ allocated so we don't have to
- * free it here.
- */
- memset(ipxnet_table, 0, sizeof(ipxnet_table));
+ if (ipxnet_hash_table) {
+ g_hash_table_destroy(ipxnet_hash_table);
+ ipxnet_hash_table = NULL;
+ }
+
}
+#if 0
static hashipxnet_t *
add_ipxnet_name(guint addr, const gchar *name)
{
- int hash_idx;
- hashipxnet_t *tp;
+ hashipxnet_t *tp;
- hash_idx = HASH_IPX_NET(addr);
-
- tp = ipxnet_table[hash_idx];
+ tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
+ if (tp) {
+ g_strlcpy(tp->name, name, MAXNAMELEN);
+ } else {
+ int *key;
- if( tp == NULL ) {
- tp = ipxnet_table[hash_idx] = se_new(hashipxnet_t);
- } else {
- while(1) {
- if (tp->next == NULL) {
- tp->next = se_new(hashipxnet_t);
- tp = tp->next;
- break;
- }
- tp = tp->next;
+ key = (int *)g_new(int, 1);
+ *key = addr;
+ tp = g_new(hashipxnet_t,1);
+ g_strlcpy(tp->name, name, MAXNAMELEN);
+ g_hash_table_insert(ipxnet_hash_table, key, tp);
}
- }
- tp->addr = addr;
- g_strlcpy(tp->name, name, MAXNAMELEN);
- tp->next = NULL;
- new_resolved_objects = TRUE;
+ tp->addr = addr;
+ g_strlcpy(tp->name, name, MAXNAMELEN);
+ tp->next = NULL;
+ new_resolved_objects = TRUE;
- return tp;
+ return tp;
} /* add_ipxnet_name */
+#endif
static gchar *
-ipxnet_name_lookup(const guint addr)
+ipxnet_name_lookup(wmem_allocator_t *allocator, const guint addr)
{
- int hash_idx;
- hashipxnet_t *tp;
- ipxnet_t *ipxnet;
+ hashipxnet_t *tp;
+ ipxnet_t *ipxnet;
- hash_idx = HASH_IPX_NET(addr);
+ tp = (hashipxnet_t *)g_hash_table_lookup(ipxnet_hash_table, &addr);
+ if (tp == NULL) {
+ int *key;
- tp = ipxnet_table[hash_idx];
-
- if( tp == NULL ) {
- tp = ipxnet_table[hash_idx] = se_new(hashipxnet_t);
- } else {
- while(1) {
- if (tp->addr == addr) {
- return tp->name;
- }
- if (tp->next == NULL) {
- tp->next = se_new(hashipxnet_t);
- tp = tp->next;
- break;
- }
- tp = tp->next;
+ key = (int *)g_new(int, 1);
+ *key = addr;
+ tp = g_new(hashipxnet_t, 1);
+ g_hash_table_insert(ipxnet_hash_table, key, tp);
+ } else {
+ return wmem_strdup(allocator, tp->name);
}
- }
- /* fill in a new entry */
+ /* fill in a new entry */
- tp->addr = addr;
- tp->next = NULL;
+ tp->addr = addr;
- if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
- /* unknown name */
- g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
+ if ( (ipxnet = get_ipxnetbyaddr(addr)) == NULL) {
+ /* unknown name */
+ g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
- } else {
- g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
- }
+ } else {
+ g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
+ }
- return (tp->name);
+ return wmem_strdup(allocator, tp->name);
} /* ipxnet_name_lookup */
static guint
-ipxnet_addr_lookup(const gchar *name, gboolean *success)
+ipxnet_addr_lookup(const gchar *name _U_, gboolean *success)
{
- ipxnet_t *ipxnet;
- hashipxnet_t *tp;
- hashipxnet_t **table = ipxnet_table;
- int i;
-
- /* to be optimized (hash table from name to addr) */
- for (i = 0; i < HASHIPXNETSIZE; i++) {
- tp = table[i];
- while (tp) {
- if (strcmp(tp->name, name) == 0) {
- *success = TRUE;
- return tp->addr;
- }
- tp = tp->next;
- }
- }
-
- /* not in hash table : performs a file lookup */
-
- if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
*success = FALSE;
return 0;
- }
+#if 0
+ /* XXX Do we need reverse lookup??? */
+ ipxnet_t *ipxnet;
+ hashipxnet_t *tp;
+ hashipxnet_t **table = ipxnet_table;
+ int i;
+
+ /* to be optimized (hash table from name to addr) */
+ for (i = 0; i < HASHIPXNETSIZE; i++) {
+ tp = table[i];
+ while (tp) {
+ if (strcmp(tp->name, name) == 0) {
+ *success = TRUE;
+ return tp->addr;
+ }
+ tp = tp->next;
+ }
+ }
- /* add new entry in hash table */
+ /* not in hash table : performs a file lookup */
- tp = add_ipxnet_name(ipxnet->addr, name);
+ if ((ipxnet = get_ipxnetbyname(name)) == NULL) {
+ *success = FALSE;
+ return 0;
+ }
+
+ /* add new entry in hash table */
- *success = TRUE;
- return tp->addr;
+ tp = add_ipxnet_name(ipxnet->addr, name);
+ *success = TRUE;
+ return tp->addr;
+#endif
} /* ipxnet_addr_lookup */
static gboolean
-read_hosts_file (const char *hostspath)
-{
- FILE *hf;
- char *line = NULL;
- int size = 0;
- gchar *cp;
- guint32 host_addr[4]; /* IPv4 or IPv6 */
- struct e_in6_addr ip6_addr;
- gboolean is_ipv6;
- int ret;
-
- /*
- * See the hosts(4) or hosts(5) man page for hosts file format
- * (not available on all systems).
- */
- if ((hf = ws_fopen(hostspath, "r")) == NULL)
- return FALSE;
+read_hosts_file (const char *hostspath, gboolean store_entries)
+{
+ FILE *hf;
+ char *line = NULL;
+ int size = 0;
+ gchar *cp;
+ guint32 host_addr[4]; /* IPv4 or IPv6 */
+ struct e_in6_addr ip6_addr;
+ gboolean is_ipv6, entry_found = FALSE;
+ int ret;
- while (fgetline(&line, &size, hf) >= 0) {
- if ((cp = strchr(line, '#')))
- *cp = '\0';
+ /*
+ * See the hosts(4) or hosts(5) man page for hosts file format
+ * (not available on all systems).
+ */
+ if ((hf = ws_fopen(hostspath, "r")) == NULL)
+ return FALSE;
- if ((cp = strtok(line, " \t")) == NULL)
- continue; /* no tokens in the line */
+ while (fgetline(&line, &size, hf) >= 0) {
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
- ret = inet_pton(AF_INET6, cp, &host_addr);
- if (ret < 0)
- continue; /* error parsing */
- if (ret > 0) {
- /* Valid IPv6 */
- is_ipv6 = TRUE;
- } else {
- /* Not valid IPv6 - valid IPv4? */
- if (inet_pton(AF_INET, cp, &host_addr) <= 0)
- continue; /* no */
- is_ipv6 = FALSE;
- }
+ if ((cp = strtok(line, " \t")) == NULL)
+ continue; /* no tokens in the line */
- if ((cp = strtok(NULL, " \t")) == NULL)
- continue; /* no host name */
+ ret = inet_pton(AF_INET6, cp, &host_addr);
+ if (ret < 0)
+ continue; /* error parsing */
+ if (ret > 0) {
+ /* Valid IPv6 */
+ is_ipv6 = TRUE;
+ } else {
+ /* Not valid IPv6 - valid IPv4? */
+ if (!str_to_ip(cp, &host_addr))
+ continue; /* no */
+ is_ipv6 = FALSE;
+ }
- if (is_ipv6) {
- memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
- add_ipv6_name(&ip6_addr, cp);
- } else
- add_ipv4_name(host_addr[0], cp);
+ if ((cp = strtok(NULL, " \t")) == NULL)
+ continue; /* no host name */
- /*
- * Add the aliases, too, if there are any.
- * XXX - host_lookup() only returns the first entry.
- */
- while ((cp = strtok(NULL, " \t")) != NULL) {
- if (is_ipv6) {
- memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
- add_ipv6_name(&ip6_addr, cp);
- } else
- add_ipv4_name(host_addr[0], cp);
- }
- }
- g_free(line);
-
- fclose(hf);
- return TRUE;
+ entry_found = TRUE;
+ if (store_entries) {
+ if (is_ipv6) {
+ memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
+ add_ipv6_name(&ip6_addr, cp);
+ } else
+ add_ipv4_name(host_addr[0], cp);
+
+#if 0
+ /*
+ * Add the aliases, too, if there are any.
+ * XXX - except we only store the last one added. The name
+ * resolver returns the first name in the hosts file, we should
+ * too.
+ */
+ while ((cp = strtok(NULL, " \t")) != NULL) {
+ if (is_ipv6) {
+ memcpy(&ip6_addr, host_addr, sizeof ip6_addr);
+ add_ipv6_name(&ip6_addr, cp);
+ } else
+ add_ipv4_name(host_addr[0], cp);
+ }
+#endif
+ }
+ }
+ g_free(line);
+
+ fclose(hf);
+ return entry_found ? TRUE : FALSE;
} /* read_hosts_file */
gboolean
add_hosts_file (const char *hosts_file)
{
- gboolean found = FALSE;
- guint i;
+ gboolean found = FALSE;
+ guint i;
- if (!hosts_file)
- return FALSE;
+ if (!hosts_file)
+ return FALSE;
- if (!extra_hosts_files)
- extra_hosts_files = g_ptr_array_new();
+ if (!extra_hosts_files)
+ extra_hosts_files = g_ptr_array_new();
- for (i = 0; i < extra_hosts_files->len; i++) {
- if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
- found = TRUE;
- }
+ for (i = 0; i < extra_hosts_files->len; i++) {
+ if (strcmp(hosts_file, (const char *) g_ptr_array_index(extra_hosts_files, i)) == 0)
+ found = TRUE;
+ }
- if (!found) {
- g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
- if (addrinfo_list) {
- return read_hosts_file (hosts_file);
+ if (!found) {
+ g_ptr_array_add(extra_hosts_files, g_strdup(hosts_file));
+ return read_hosts_file (hosts_file, FALSE);
}
- }
- return TRUE;
+ return TRUE;
}
gboolean
add_ip_name_from_string (const char *addr, const char *name)
{
- guint32 host_addr[4]; /* IPv4 */
- struct e_in6_addr ip6_addr; /* IPv6 */
- gboolean is_ipv6;
- int ret;
+ guint32 host_addr[4]; /* IPv4 */
+ struct e_in6_addr ip6_addr; /* IPv6 */
+ gboolean is_ipv6;
+ int ret;
+ resolved_ipv4_t *resolved_ipv4_entry;
+ resolved_ipv6_t *resolved_ipv6_entry;
- ret = inet_pton(AF_INET6, addr, &ip6_addr);
- if (ret < 0)
- /* Error parsing address */
- return FALSE;
+ ret = inet_pton(AF_INET6, addr, &ip6_addr);
+ if (ret < 0)
+ /* Error parsing address */
+ return FALSE;
- if (ret > 0) {
- /* Valid IPv6 */
- is_ipv6 = TRUE;
- } else {
- /* Not valid IPv6 - valid IPv4? */
- if (inet_pton(AF_INET, addr, &host_addr) <= 0)
- return FALSE; /* no */
- is_ipv6 = FALSE;
- }
-
- if (is_ipv6) {
- add_ipv6_name(&ip6_addr, name);
- } else {
- add_ipv4_name(host_addr[0], name);
- }
-
- return TRUE;
+ if (ret > 0) {
+ /* Valid IPv6 */
+ is_ipv6 = TRUE;
+ } else {
+ /* Not valid IPv6 - valid IPv4? */
+ if (!str_to_ip(addr, &host_addr))
+ return FALSE; /* no */
+ is_ipv6 = FALSE;
+ }
+
+ if (is_ipv6) {
+ resolved_ipv6_entry = g_new(resolved_ipv6_t, 1);
+ memcpy(&(resolved_ipv6_entry->ip6_addr), &ip6_addr, 16);
+ g_strlcpy(resolved_ipv6_entry->name, name, MAXNAMELEN);
+ manually_resolved_ipv6_list = g_slist_prepend(manually_resolved_ipv6_list, resolved_ipv6_entry);
+ } else {
+ resolved_ipv4_entry = g_new(resolved_ipv4_t, 1);
+ resolved_ipv4_entry->host_addr = host_addr[0];
+ g_strlcpy(resolved_ipv4_entry->name, name, MAXNAMELEN);
+ manually_resolved_ipv4_list = g_slist_prepend(manually_resolved_ipv4_list, resolved_ipv4_entry);
+ }
+
+ return TRUE;
} /* add_ip_name_from_string */
-struct addrinfo *
+/*
+ * Add the resolved addresses that are in use to the list used to create the NRB
+ */
+static void
+ipv4_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
+{
+ addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
+ hashipv4_t *ipv4_hash_table_entry = (hashipv4_t *)value;
+
+ if ((ipv4_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
+ lists->ipv4_addr_list = g_list_prepend (lists->ipv4_addr_list, ipv4_hash_table_entry);
+ }
+
+}
+
+/*
+ * Add the resolved addresses that are in use to the list used to create the NRB
+ */
+
+static void
+ipv6_hash_table_resolved_to_list(gpointer key _U_, gpointer value, gpointer user_data)
+{
+ addrinfo_lists_t *lists = (addrinfo_lists_t*)user_data;
+ hashipv6_t *ipv6_hash_table_entry = (hashipv6_t *)value;
+
+ if ((ipv6_hash_table_entry->flags & USED_AND_RESOLVED_MASK) == RESOLVED_ADDRESS_USED) {
+ lists->ipv6_addr_list = g_list_prepend (lists->ipv6_addr_list, ipv6_hash_table_entry);
+ }
+
+}
+
+addrinfo_lists_t *
get_addrinfo_list(void) {
- return addrinfo_list;
+
+ if (ipv4_hash_table) {
+ g_hash_table_foreach(ipv4_hash_table, ipv4_hash_table_resolved_to_list, &addrinfo_lists);
+ }
+
+ if (ipv6_hash_table) {
+ g_hash_table_foreach(ipv6_hash_table, ipv6_hash_table_resolved_to_list, &addrinfo_lists);
+ }
+
+ return &addrinfo_lists;
}
/* Read in a list of subnet definition - name pairs.
static gboolean
read_subnets_file (const char *subnetspath)
{
- FILE *hf;
- char *line = NULL;
- int size = 0;
- gchar *cp, *cp2;
- guint32 host_addr; /* IPv4 ONLY */
- int mask_length;
+ FILE *hf;
+ char *line = NULL;
+ int size = 0;
+ gchar *cp, *cp2;
+ guint32 host_addr; /* IPv4 ONLY */
+ int mask_length;
- if ((hf = ws_fopen(subnetspath, "r")) == NULL)
- return FALSE;
+ if ((hf = ws_fopen(subnetspath, "r")) == NULL)
+ return FALSE;
- while (fgetline(&line, &size, hf) >= 0) {
- if ((cp = strchr(line, '#')))
- *cp = '\0';
+ while (fgetline(&line, &size, hf) >= 0) {
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
- if ((cp = strtok(line, " \t")) == NULL)
- continue; /* no tokens in the line */
+ if ((cp = strtok(line, " \t")) == NULL)
+ continue; /* no tokens in the line */
- /* Expected format is <IP4 address>/<subnet length> */
- cp2 = strchr(cp, '/');
- if(NULL == cp2) {
- /* No length */
- continue;
- }
- *cp2 = '\0'; /* Cut token */
- ++cp2 ;
+ /* Expected format is <IP4 address>/<subnet length> */
+ cp2 = strchr(cp, '/');
+ if (NULL == cp2) {
+ /* No length */
+ continue;
+ }
+ *cp2 = '\0'; /* Cut token */
+ ++cp2 ;
- /* Check if this is a valid IPv4 address */
- if (inet_pton(AF_INET, cp, &host_addr) <= 0) {
- continue; /* no */
- }
+ /* Check if this is a valid IPv4 address */
+ if (!str_to_ip(cp, &host_addr)) {
+ continue; /* no */
+ }
- mask_length = atoi(cp2);
- if(0 >= mask_length || mask_length > 31) {
- continue; /* invalid mask length */
- }
+ mask_length = atoi(cp2);
+ if (0 >= mask_length || mask_length > 32) {
+ continue; /* invalid mask length */
+ }
- if ((cp = strtok(NULL, " \t")) == NULL)
- continue; /* no subnet name */
+ if ((cp = strtok(NULL, " \t")) == NULL)
+ continue; /* no subnet name */
- subnet_entry_set(host_addr, (guint32)mask_length, cp);
- }
- g_free(line);
+ subnet_entry_set(host_addr, (guint32)mask_length, cp);
+ }
+ g_free(line);
- fclose(hf);
- return TRUE;
+ fclose(hf);
+ return TRUE;
} /* read_subnets_file */
static subnet_entry_t
subnet_lookup(const guint32 addr)
{
- subnet_entry_t subnet_entry;
- guint32 i;
+ subnet_entry_t subnet_entry;
+ guint32 i;
- /* Search mask lengths linearly, longest first */
+ /* Search mask lengths linearly, longest first */
- i = SUBNETLENGTHSIZE;
- while(have_subnet_entry && i > 0) {
- guint32 masked_addr;
- subnet_length_entry_t* length_entry;
+ i = SUBNETLENGTHSIZE;
+ while(have_subnet_entry && i > 0) {
+ guint32 masked_addr;
+ subnet_length_entry_t* length_entry;
- /* Note that we run from 31 (length 32) to 0 (length 1) */
- --i;
- g_assert(i < SUBNETLENGTHSIZE);
+ /* Note that we run from 31 (length 32) to 0 (length 1) */
+ --i;
+ g_assert(i < SUBNETLENGTHSIZE);
- length_entry = &subnet_length_entries[i];
+ length_entry = &subnet_length_entries[i];
- if(NULL != length_entry->subnet_addresses) {
- hashipv4_t * tp;
- guint32 hash_idx;
+ if (NULL != length_entry->subnet_addresses) {
+ sub_net_hashipv4_t * tp;
+ guint32 hash_idx;
- masked_addr = addr & length_entry->mask;
- hash_idx = HASH_IPV4_ADDRESS(masked_addr);
+ masked_addr = addr & length_entry->mask;
+ hash_idx = HASH_IPV4_ADDRESS(masked_addr);
- tp = length_entry->subnet_addresses[hash_idx];
- while(tp != NULL && tp->addr != masked_addr) {
- tp = tp->next;
- }
+ tp = length_entry->subnet_addresses[hash_idx];
+ while(tp != NULL && tp->addr != masked_addr) {
+ tp = tp->next;
+ }
- if(NULL != tp) {
- subnet_entry.mask = length_entry->mask;
- subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
- subnet_entry.name = tp->name;
- return subnet_entry;
- }
+ if (NULL != tp) {
+ subnet_entry.mask = length_entry->mask;
+ subnet_entry.mask_length = i + 1; /* Length is offset + 1 */
+ subnet_entry.name = tp->name;
+ return subnet_entry;
+ }
+ }
}
- }
- subnet_entry.mask = 0;
- subnet_entry.mask_length = 0;
- subnet_entry.name = NULL;
+ subnet_entry.mask = 0;
+ subnet_entry.mask_length = 0;
+ subnet_entry.name = NULL;
- return subnet_entry;
+ return subnet_entry;
}
/* Add a subnet-definition - name pair to the set.
static void
subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* name)
{
- subnet_length_entry_t* entry;
- hashipv4_t * tp;
- gsize hash_idx;
+ subnet_length_entry_t* entry;
+ sub_net_hashipv4_t * tp;
+ gsize hash_idx;
- g_assert(mask_length > 0 && mask_length <= 32);
+ g_assert(mask_length > 0 && mask_length <= 32);
- entry = &subnet_length_entries[mask_length - 1];
+ entry = &subnet_length_entries[mask_length - 1];
- subnet_addr &= entry->mask;
+ subnet_addr &= entry->mask;
- hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
+ hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
- if(NULL == entry->subnet_addresses) {
- entry->subnet_addresses = (hashipv4_t**) se_alloc0(sizeof(hashipv4_t*) * HASHHOSTSIZE);
- }
+ if (NULL == entry->subnet_addresses) {
+ entry->subnet_addresses = (sub_net_hashipv4_t**) g_malloc0(sizeof(sub_net_hashipv4_t*) * HASHHOSTSIZE);
+ }
+
+ if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
+ sub_net_hashipv4_t * new_tp;
+
+ while (tp->next) {
+ if (tp->addr == subnet_addr) {
+ return; /* XXX provide warning that an address was repeated? */
+ } else {
+ tp = tp->next;
+ }
+ }
- if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
- if(tp->addr == subnet_addr) {
- return; /* XXX provide warning that an address was repeated? */
+ new_tp = g_new(sub_net_hashipv4_t, 1);
+ tp->next = new_tp;
+ tp = new_tp;
} else {
- hashipv4_t * new_tp = se_new(hashipv4_t);
- tp->next = new_tp;
- tp = new_tp;
+ tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
}
- } else {
- tp = entry->subnet_addresses[hash_idx] = se_new(hashipv4_t);
- }
- tp->next = NULL;
- tp->addr = subnet_addr;
- tp->is_dummy_entry = FALSE; /*Never used again...*/
- g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
- have_subnet_entry = TRUE;
+ tp->next = NULL;
+ tp->addr = subnet_addr;
+ /* Clear DUMMY_ADDRESS_ENTRY */
+ tp->flags &= ~DUMMY_ADDRESS_ENTRY; /*Never used again...*/
+ g_strlcpy(tp->name, name, MAXNAMELEN); /* This is longer than subnet names can actually be */
+ have_subnet_entry = TRUE;
}
static void
subnet_name_lookup_init(void)
{
- gchar* subnetspath;
- guint32 i;
+ gchar* subnetspath;
+ guint32 i;
- for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
- guint32 length = i + 1;
+ for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
+ guint32 length = i + 1;
- subnet_length_entries[i].subnet_addresses = NULL;
- subnet_length_entries[i].mask_length = length;
- subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
- }
+ subnet_length_entries[i].subnet_addresses = NULL;
+ subnet_length_entries[i].mask_length = length;
+ subnet_length_entries[i].mask = g_htonl(ip_get_subnet_mask(length));
+ }
- subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
- if (!read_subnets_file(subnetspath) && errno != ENOENT) {
- report_open_failure(subnetspath, errno, FALSE);
- }
- g_free(subnetspath);
+ subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE);
+ if (!read_subnets_file(subnetspath) && errno != ENOENT) {
+ report_open_failure(subnetspath, errno, FALSE);
+ }
+ g_free(subnetspath);
- /*
- * Load the global subnets file, if we have one.
- */
- subnetspath = get_datafile_path(ENAME_SUBNETS);
- if (!read_subnets_file(subnetspath) && errno != ENOENT) {
- report_open_failure(subnetspath, errno, FALSE);
- }
- g_free(subnetspath);
+ /*
+ * Load the global subnets file, if we have one.
+ */
+ subnetspath = get_datafile_path(ENAME_SUBNETS);
+ if (!read_subnets_file(subnetspath) && errno != ENOENT) {
+ report_open_failure(subnetspath, errno, FALSE);
+ }
+ g_free(subnetspath);
}
+static void
+cleanup_subnet_entry(sub_net_hashipv4_t* entry)
+{
+ if ((entry != NULL) && (entry->next != NULL)) {
+ cleanup_subnet_entry(entry->next);
+ }
+
+ g_free(entry);
+}
/*
* External Functions
addr_resolve_pref_init(module_t *nameres)
{
prefs_register_bool_preference(nameres, "mac_name",
- "Resolve MAC addresses",
- "Resolve Ethernet MAC address to manufacturer names",
- &gbl_resolv_flags.mac_name);
+ "Resolve MAC addresses",
+ "Resolve Ethernet MAC address to manufacturer names",
+ &gbl_resolv_flags.mac_name);
prefs_register_bool_preference(nameres, "transport_name",
- "Resolve transport names",
- "Resolve TCP/UDP ports into service names",
- &gbl_resolv_flags.transport_name);
+ "Resolve transport names",
+ "Resolve TCP/UDP ports into service names",
+ &gbl_resolv_flags.transport_name);
prefs_register_bool_preference(nameres, "network_name",
- "Resolve network (IP) addresses",
- "Resolve IPv4, IPv6, and IPX addresses into host names."
- " The next set of check boxes determines how name resolution should be performed."
- " If no other options are checked name resolution is made from Wireshark's host file,"
- " capture file name resolution blocks and DNS packets in the capture.",
- &gbl_resolv_flags.network_name);
+ "Resolve network (IP) addresses",
+ "Resolve IPv4, IPv6, and IPX addresses into host names."
+ " The next set of check boxes determines how name resolution should be performed."
+ " If no other options are checked name resolution is made from Wireshark's host file,"
+ " capture file name resolution blocks and DNS packets in the capture.",
+ &gbl_resolv_flags.network_name);
+
+ prefs_register_bool_preference(nameres, "dns_pkt_addr_resolution",
+ "Use captured DNS packet data for address resolution",
+ "Whether address/name pairs found in captured DNS packets should be used by Wireshark for name resolution.",
+ &gbl_resolv_flags.dns_pkt_addr_resolution);
prefs_register_bool_preference(nameres, "use_external_name_resolver",
- "Use an external network name resolver",
- "Use your system's configured name resolver"
- " (usually DNS) to resolve network names."
- " Only applies when network name resolution"
- " is enabled.",
- &gbl_resolv_flags.use_external_net_name_resolver);
+ "Use an external network name resolver",
+ "Use your system's configured name resolver"
+ " (usually DNS) to resolve network names."
+ " Only applies when network name resolution"
+ " is enabled.",
+ &gbl_resolv_flags.use_external_net_name_resolver);
#if defined(HAVE_C_ARES) || defined(HAVE_GNU_ADNS)
prefs_register_bool_preference(nameres, "concurrent_dns",
- "Enable concurrent DNS name resolution",
- "Enable concurrent DNS name resolution. Only"
- " applies when network name resolution is"
- " enabled. You probably want to enable this.",
- &gbl_resolv_flags.concurrent_dns);
+ "Enable concurrent DNS name resolution",
+ "Enable concurrent DNS name resolution. Only"
+ " applies when network name resolution is"
+ " enabled. You probably want to enable this.",
+ &gbl_resolv_flags.concurrent_dns);
prefs_register_uint_preference(nameres, "name_resolve_concurrency",
- "Maximum concurrent requests",
- "The maximum number of DNS requests that may"
- " be active at any time. A large value (many"
- " thousands) might overload the network or make"
- " your DNS server behave badly.",
- 10,
- &name_resolve_concurrency);
+ "Maximum concurrent requests",
+ "The maximum number of DNS requests that may"
+ " be active at any time. A large value (many"
+ " thousands) might overload the network or make"
+ " your DNS server behave badly.",
+ 10,
+ &name_resolve_concurrency);
#else
prefs_register_static_text_preference(nameres, "concurrent_dns",
- "Enable concurrent DNS name resolution: N/A",
- "Support for concurrent DNS name resolution was not"
- " compiled into this version of Wireshark");
+ "Enable concurrent DNS name resolution: N/A",
+ "Support for concurrent DNS name resolution was not"
+ " compiled into this version of Wireshark");
#endif
prefs_register_bool_preference(nameres, "hosts_file_handling",
- "Only use the profile \"hosts\" file",
- "By default \"hosts\" files will be loaded from multiple sources."
- " Checking this box only loads the \"hosts\" in the current profile.",
- &gbl_resolv_flags.load_hosts_file_from_profile_only);
+ "Only use the profile \"hosts\" file",
+ "By default \"hosts\" files will be loaded from multiple sources."
+ " Checking this box only loads the \"hosts\" in the current profile.",
+ &gbl_resolv_flags.load_hosts_file_from_profile_only);
}
+void
+disable_name_resolution(void) {
+ gbl_resolv_flags.mac_name = FALSE;
+ gbl_resolv_flags.network_name = FALSE;
+ gbl_resolv_flags.transport_name = FALSE;
+ gbl_resolv_flags.concurrent_dns = FALSE;
+ gbl_resolv_flags.dns_pkt_addr_resolution = FALSE;
+ gbl_resolv_flags.use_external_net_name_resolver = FALSE;
+}
+
#ifdef HAVE_C_ARES
gboolean
host_name_lookup_process(void) {
- async_dns_queue_msg_t *caqm;
- struct timeval tv = { 0, 0 };
- int nfds;
- fd_set rfds, wfds;
- gboolean nro = new_resolved_objects;
+ async_dns_queue_msg_t *caqm;
+ struct timeval tv = { 0, 0 };
+ int nfds;
+ fd_set rfds, wfds;
+ gboolean nro = new_resolved_objects;
- new_resolved_objects = FALSE;
-
- if (!async_dns_initialized)
- /* c-ares not initialized. Bail out and cancel timers. */
- return nro;
+ new_resolved_objects = FALSE;
- async_dns_queue_head = g_list_first(async_dns_queue_head);
-
- while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
- caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
- async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
- if (caqm->family == AF_INET) {
- ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
- c_ares_ghba_cb, caqm);
- async_dns_in_flight++;
- } else if (caqm->family == AF_INET6) {
- ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
- AF_INET6, c_ares_ghba_cb, caqm);
- async_dns_in_flight++;
- }
- }
-
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- nfds = ares_fds(ghba_chan, &rfds, &wfds);
- if (nfds > 0) {
- if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
- fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
+ if (!async_dns_initialized)
+ /* c-ares not initialized. Bail out and cancel timers. */
return nro;
+
+ async_dns_queue_head = g_list_first(async_dns_queue_head);
+
+ while (async_dns_queue_head != NULL && async_dns_in_flight <= name_resolve_concurrency) {
+ caqm = (async_dns_queue_msg_t *) async_dns_queue_head->data;
+ async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) caqm);
+ if (caqm->family == AF_INET) {
+ ares_gethostbyaddr(ghba_chan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
+ c_ares_ghba_cb, caqm);
+ async_dns_in_flight++;
+ } else if (caqm->family == AF_INET6) {
+ ares_gethostbyaddr(ghba_chan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
+ AF_INET6, c_ares_ghba_cb, caqm);
+ async_dns_in_flight++;
+ }
}
- ares_process(ghba_chan, &rfds, &wfds);
- }
- /* Any new entries? */
- return nro;
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ nfds = ares_fds(ghba_chan, &rfds, &wfds);
+ if (nfds > 0) {
+ if (select(nfds, &rfds, &wfds, NULL, &tv) == -1) { /* call to select() failed */
+ fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
+ return nro;
+ }
+ ares_process(ghba_chan, &rfds, &wfds);
+ }
+
+ /* Any new entries? */
+ return nro;
}
static void
_host_name_lookup_cleanup(void) {
- GList *cur;
+ GList *cur;
- cur = g_list_first(async_dns_queue_head);
- while (cur) {
- g_free(cur->data);
- cur = g_list_next (cur);
- }
+ cur = g_list_first(async_dns_queue_head);
+ while (cur) {
+ g_free(cur->data);
+ cur = g_list_next (cur);
+ }
- g_list_free(async_dns_queue_head);
- async_dns_queue_head = NULL;
+ g_list_free(async_dns_queue_head);
+ async_dns_queue_head = NULL;
- if (async_dns_initialized) {
- ares_destroy(ghba_chan);
- ares_destroy(ghbn_chan);
- }
+ if (async_dns_initialized) {
+ ares_destroy(ghba_chan);
+ ares_destroy(ghbn_chan);
+ }
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
- ares_library_cleanup();
+ ares_library_cleanup();
#endif
- async_dns_initialized = FALSE;
+ async_dns_initialized = FALSE;
}
#elif defined(HAVE_GNU_ADNS)
*/
gboolean
host_name_lookup_process(void) {
- async_dns_queue_msg_t *almsg;
- GList *cur;
- char addr_str[] = "111.222.333.444.in-addr.arpa.";
- guint8 *addr_bytes;
- adns_answer *ans;
- int ret;
- gboolean dequeue;
- gboolean nro = new_resolved_objects;
-
- new_resolved_objects = FALSE;
- async_dns_queue_head = g_list_first(async_dns_queue_head);
-
- cur = async_dns_queue_head;
- while (cur && async_dns_in_flight <= name_resolve_concurrency) {
- almsg = (async_dns_queue_msg_t *) cur->data;
- if (! almsg->submitted && almsg->type == AF_INET) {
- addr_bytes = (guint8 *) &almsg->ip4_addr;
- g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
- addr_bytes[2], addr_bytes[1], addr_bytes[0]);
- /* XXX - what if it fails? */
- adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
- almsg->submitted = TRUE;
- async_dns_in_flight++;
- }
- cur = cur->next;
- }
-
- cur = async_dns_queue_head;
- while (cur) {
- dequeue = FALSE;
- almsg = (async_dns_queue_msg_t *) cur->data;
- if (almsg->submitted) {
- ret = adns_check(ads, &almsg->query, &ans, NULL);
- if (ret == 0) {
- if (ans->status == adns_s_ok) {
- add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
+ async_dns_queue_msg_t *almsg;
+ GList *cur;
+ char addr_str[] = "111.222.333.444.in-addr.arpa.";
+ guint8 *addr_bytes;
+ adns_answer *ans;
+ int ret;
+ gboolean dequeue;
+ gboolean nro = new_resolved_objects;
+
+ new_resolved_objects = FALSE;
+ async_dns_queue_head = g_list_first(async_dns_queue_head);
+
+ cur = async_dns_queue_head;
+ while (cur && async_dns_in_flight <= name_resolve_concurrency) {
+ almsg = (async_dns_queue_msg_t *) cur->data;
+ if (! almsg->submitted && almsg->type == AF_INET) {
+ addr_bytes = (guint8 *) &almsg->ip4_addr;
+ g_snprintf(addr_str, sizeof addr_str, "%u.%u.%u.%u.in-addr.arpa.", addr_bytes[3],
+ addr_bytes[2], addr_bytes[1], addr_bytes[0]);
+ /* XXX - what if it fails? */
+ adns_submit (ads, addr_str, adns_r_ptr, adns_qf_none, NULL, &almsg->query);
+ almsg->submitted = TRUE;
+ async_dns_in_flight++;
}
- dequeue = TRUE;
- }
+ cur = cur->next;
}
- cur = cur->next;
- if (dequeue) {
- async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
- g_free(almsg);
- /* XXX, what to do if async_dns_in_flight == 0? */
- async_dns_in_flight--;
+
+ cur = async_dns_queue_head;
+ while (cur) {
+ dequeue = FALSE;
+ almsg = (async_dns_queue_msg_t *) cur->data;
+ if (almsg->submitted) {
+ ret = adns_check(ads, &almsg->query, &ans, NULL);
+ if (ret == 0) {
+ if (ans->status == adns_s_ok) {
+ add_ipv4_name(almsg->ip4_addr, *ans->rrs.str);
+ }
+ dequeue = TRUE;
+ }
+ }
+ cur = cur->next;
+ if (dequeue) {
+ async_dns_queue_head = g_list_remove(async_dns_queue_head, (void *) almsg);
+ g_free(almsg);
+ /* XXX, what to do if async_dns_in_flight == 0? */
+ async_dns_in_flight--;
+ }
}
- }
- /* Keep the timeout in place */
- return nro;
+ /* Keep the timeout in place */
+ return nro;
}
static void
_host_name_lookup_cleanup(void) {
- void *qdata;
+ void *qdata;
- async_dns_queue_head = g_list_first(async_dns_queue_head);
- while (async_dns_queue_head) {
- qdata = async_dns_queue_head->data;
- async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
- g_free(qdata);
- }
+ async_dns_queue_head = g_list_first(async_dns_queue_head);
+ while (async_dns_queue_head) {
+ qdata = async_dns_queue_head->data;
+ async_dns_queue_head = g_list_remove(async_dns_queue_head, qdata);
+ g_free(qdata);
+ }
- if (async_dns_initialized)
- adns_finish(ads);
- async_dns_initialized = FALSE;
+ if (async_dns_initialized)
+ adns_finish(ads);
+ async_dns_initialized = FALSE;
}
#else /* HAVE_GNU_ADNS */
gboolean
host_name_lookup_process(void) {
- gboolean nro = new_resolved_objects;
+ gboolean nro = new_resolved_objects;
- new_resolved_objects = FALSE;
+ new_resolved_objects = FALSE;
- return nro;
+ return nro;
}
static void
const gchar *
get_hostname(const guint addr)
{
- gboolean found;
+ gboolean found;
- /* XXX why do we call this if we're not resolving? To create hash entries?
- * Why?
- */
- hashipv4_t *tp = host_lookup(addr, &found);
+ /* XXX why do we call this if we're not resolving? To create hash entries?
+ * Why?
+ */
+ hashipv4_t *tp = host_lookup(addr, &found);
+
+ if (!gbl_resolv_flags.network_name)
+ return tp->ip;
- if (!gbl_resolv_flags.network_name)
- return tp->ip;
+ tp->flags |= RESOLVED_ADDRESS_USED;
- return tp->name;
+ return tp->name;
}
/* -------------------------- */
const gchar *
get_hostname6(const struct e_in6_addr *addr)
{
- gboolean found;
+ gboolean found;
+
+ /* XXX why do we call this if we're not resolving? To create hash entries?
+ * Why?
+ */
+ hashipv6_t *tp = host_lookup6(addr, &found);
- /* XXX why do we call this if we're not resolving? To create hash entries?
- * Why?
- */
- hashipv6_t *tp = host_lookup6(addr, &found);
+ if (!gbl_resolv_flags.network_name)
+ return tp->ip6;
- if (!gbl_resolv_flags.network_name)
- return tp->ip6;
+ tp->flags |= RESOLVED_ADDRESS_USED;
- return tp->name;
+ return tp->name;
}
/* -------------------------- */
void
add_ipv4_name(const guint addr, const gchar *name)
{
- int hash_idx;
- hashipv4_t *tp;
- struct addrinfo *ai;
- struct sockaddr_in *sa4;
-
- /*
- * Don't add zero-length names; apparently, some resolvers will return
- * them if they get them from DNS.
- */
- if (name[0] == '\0')
- return;
-
- hash_idx = HASH_IPV4_ADDRESS(addr);
-
- tp = ipv4_table[hash_idx];
-
- if( tp == NULL ) {
- tp = ipv4_table[hash_idx] = new_ipv4(addr);
- } else {
- while(1) {
- if (tp->addr == addr) {
- /* address already known */
- if (!tp->is_dummy_entry) {
- return;
- } else {
- /* replace this dummy entry with the new one */
- break;
- }
- }
- if (tp->next == NULL) {
- tp->next = new_ipv4(addr);
- tp = tp->next;
- break;
- }
- tp = tp->next;
- }
- }
- g_strlcpy(tp->name, name, MAXNAMELEN);
- tp->resolve = TRUE;
- new_resolved_objects = TRUE;
-
- if (!addrinfo_list) {
- ai = se_new0(struct addrinfo);
- addrinfo_list = addrinfo_list_last = ai;
- }
-
- sa4 = se_new0(struct sockaddr_in);
- sa4->sin_family = AF_INET;
- sa4->sin_addr.s_addr = addr;
-
- ai = se_new0(struct addrinfo);
- ai->ai_family = AF_INET;
- ai->ai_addrlen = sizeof(struct sockaddr_in);
- ai->ai_canonname = (char *) tp->name;
- ai->ai_addr = (struct sockaddr*) sa4;
-
- addrinfo_list_last->ai_next = ai;
- addrinfo_list_last = ai;
+ hashipv4_t *tp;
+
+ /*
+ * Don't add zero-length names; apparently, some resolvers will return
+ * them if they get them from DNS.
+ */
+ if (!name || name[0] == '\0')
+ return;
+
+
+ tp = (hashipv4_t *)g_hash_table_lookup(ipv4_hash_table, GUINT_TO_POINTER(addr));
+ if (!tp) {
+ tp = new_ipv4(addr);
+ g_hash_table_insert(ipv4_hash_table, GUINT_TO_POINTER(addr), tp);
+ }
+
+ if (g_ascii_strcasecmp(tp->name, name)) {
+ g_strlcpy(tp->name, name, MAXNAMELEN);
+ new_resolved_objects = TRUE;
+ }
+ tp->flags |= TRIED_RESOLVE_ADDRESS;
} /* add_ipv4_name */
void
add_ipv6_name(const struct e_in6_addr *addrp, const gchar *name)
{
- int hash_idx;
- hashipv6_t *tp;
- struct addrinfo *ai;
- struct sockaddr_in6 *sa6;
-
- /*
- * Don't add zero-length names; apparently, some resolvers will return
- * them if they get them from DNS.
- */
- if (name[0] == '\0')
- return;
-
- hash_idx = HASH_IPV6_ADDRESS(*addrp);
-
- tp = ipv6_table[hash_idx];
-
- if( tp == NULL ) {
- tp = ipv6_table[hash_idx] = new_ipv6(addrp);
- } else {
- while(1) {
- if (memcmp(&tp->addr, addrp, sizeof (struct e_in6_addr)) == 0) {
- /* address already known */
- if (!tp->is_dummy_entry) {
- return;
- } else {
- /* replace this dummy entry with the new one */
- break;
- }
- }
- if (tp->next == NULL) {
- tp->next = new_ipv6(addrp);
- tp = tp->next;
- break;
- }
- tp = tp->next;
- }
- }
- g_strlcpy(tp->name, name, MAXNAMELEN);
- tp->resolve = TRUE;
- new_resolved_objects = TRUE;
-
- if (!addrinfo_list) {
- ai = se_new0(struct addrinfo);
- addrinfo_list = addrinfo_list_last = ai;
- }
-
- sa6 = se_new0(struct sockaddr_in6);
- sa6->sin6_family = AF_INET;
- memcpy(sa6->sin6_addr.s6_addr, addrp, 16);
-
- ai = se_new0(struct addrinfo);
- ai->ai_family = AF_INET6;
- ai->ai_addrlen = sizeof(struct sockaddr_in);
- ai->ai_canonname = (char *) tp->name;
- ai->ai_addr = (struct sockaddr *) sa6;
-
- addrinfo_list_last->ai_next = ai;
- addrinfo_list_last = ai;
+ hashipv6_t *tp;
+
+ /*
+ * Don't add zero-length names; apparently, some resolvers will return
+ * them if they get them from DNS.
+ */
+ if (!name || name[0] == '\0')
+ return;
+
+ tp = (hashipv6_t *)g_hash_table_lookup(ipv6_hash_table, addrp);
+ if (!tp) {
+ struct e_in6_addr *addr_key;
+
+ addr_key = g_new(struct e_in6_addr,1);
+ tp = new_ipv6(addrp);
+ memcpy(addr_key, addrp, 16);
+ g_hash_table_insert(ipv6_hash_table, addr_key, tp);
+ }
+
+ if (g_ascii_strcasecmp(tp->name, name)) {
+ g_strlcpy(tp->name, name, MAXNAMELEN);
+ new_resolved_objects = TRUE;
+ }
+ tp->flags |= TRIED_RESOLVE_ADDRESS;
} /* add_ipv6_name */
+static void
+add_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
+{
+ resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
+
+ add_ipv4_name(resolved_ipv4_entry->host_addr, resolved_ipv4_entry->name);
+}
+
+static void
+add_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
+{
+ resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
+
+ add_ipv6_name(&(resolved_ipv6_entry->ip6_addr), resolved_ipv6_entry->name);
+}
+
+static void
+add_manually_resolved(void)
+{
+ if (manually_resolved_ipv4_list) {
+ g_slist_foreach(manually_resolved_ipv4_list, add_manually_resolved_ipv4, NULL);
+ }
+
+ if (manually_resolved_ipv6_list) {
+ g_slist_foreach(manually_resolved_ipv6_list, add_manually_resolved_ipv6, NULL);
+ }
+}
+
void
host_name_lookup_init(void)
{
- char *hostspath;
- struct addrinfo *ai;
- guint i;
+ char *hostspath;
+ guint i;
#ifdef HAVE_GNU_ADNS
#ifdef _WIN32
- char *sysroot;
- static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
- static char rootpath_ot[] = "\\hosts";
+ char *sysroot;
+ static char rootpath_nt[] = "\\system32\\drivers\\etc\\hosts";
+ static char rootpath_ot[] = "\\hosts";
#endif /* _WIN32 */
#endif /*GNU_ADNS */
- if (!addrinfo_list) {
- ai = se_new0(struct addrinfo);
- addrinfo_list = addrinfo_list_last = ai;
- }
-
- /*
- * Load the user's hosts file no matter what, if they have one.
- */
- hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
- if (!read_hosts_file(hostspath) && errno != ENOENT) {
- report_open_failure(hostspath, errno, FALSE);
- }
- g_free(hostspath);
- /*
- * Load the global hosts file, if we have one.
- */
- if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
- hostspath = get_datafile_path(ENAME_HOSTS);
- if (!read_hosts_file(hostspath) && errno != ENOENT) {
- report_open_failure(hostspath, errno, FALSE);
+ g_assert(ipxnet_hash_table == NULL);
+ ipxnet_hash_table = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
+
+ g_assert(ipv4_hash_table == NULL);
+ ipv4_hash_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
+
+ g_assert(ipv6_hash_table == NULL);
+ ipv6_hash_table = g_hash_table_new_full(ipv6_oat_hash, ipv6_equal, g_free, g_free);
+
+ /*
+ * Load the global hosts file, if we have one.
+ */
+ if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
+ hostspath = get_datafile_path(ENAME_HOSTS);
+ if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
+ report_open_failure(hostspath, errno, FALSE);
+ }
+ g_free(hostspath);
+ }
+ /*
+ * Load the user's hosts file no matter what, if they have one.
+ */
+ hostspath = get_persconffile_path(ENAME_HOSTS, TRUE);
+ if (!read_hosts_file(hostspath, TRUE) && errno != ENOENT) {
+ report_open_failure(hostspath, errno, FALSE);
}
g_free(hostspath);
- }
#ifdef HAVE_C_ARES
+ if (gbl_resolv_flags.concurrent_dns) {
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
- if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
+ if (ares_library_init(ARES_LIB_INIT_ALL) == ARES_SUCCESS) {
#endif
- if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
- async_dns_initialized = TRUE;
- }
+ if (ares_init(&ghba_chan) == ARES_SUCCESS && ares_init(&ghbn_chan) == ARES_SUCCESS) {
+ async_dns_initialized = TRUE;
+ }
#ifdef CARES_HAVE_ARES_LIBRARY_INIT
- }
+ }
#endif
+ }
#else
#ifdef HAVE_GNU_ADNS
- /*
- * We're using GNU ADNS, which doesn't check the system hosts file;
- * we load that file ourselves.
- */
-#ifdef _WIN32
-
- sysroot = getenv_utf8("WINDIR");
- if (sysroot != NULL) {
/*
- * The file should be under WINDIR.
- * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
- * %WINDIR%\system32\drivers\etc\hosts.
- * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
- * Try both.
- * XXX - should we base it on the dwPlatformId value from
- * GetVersionEx()?
+ * We're using GNU ADNS, which doesn't check the system hosts file;
+ * we load that file ourselves.
*/
- if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
- hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
- if (!read_hosts_file(hostspath)) {
- g_free(hostspath);
- hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
- read_hosts_file(hostspath);
- }
- g_free(hostspath);
+#ifdef _WIN32
+
+ sysroot = getenv_utf8("WINDIR");
+ if (sysroot != NULL) {
+ /*
+ * The file should be under WINDIR.
+ * If this is Windows NT (NT 4.0,2K,XP,Server2K3), it's in
+ * %WINDIR%\system32\drivers\etc\hosts.
+ * If this is Windows OT (95,98,Me), it's in %WINDIR%\hosts.
+ * Try both.
+ * XXX - should we base it on the dwPlatformId value from
+ * GetVersionEx()?
+ */
+ if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
+ hostspath = g_strconcat(sysroot, rootpath_nt, NULL);
+ if (!read_hosts_file(hostspath, TRUE)) {
+ g_free(hostspath);
+ hostspath = g_strconcat(sysroot, rootpath_ot, NULL);
+ read_hosts_file(hostspath, TRUE);
+ }
+ g_free(hostspath);
+ }
}
- }
#else /* _WIN32 */
- if(!gbl_resolv_flags.load_hosts_file_from_profile_only){
- read_hosts_file("/etc/hosts");
- }
+ if (!gbl_resolv_flags.load_hosts_file_from_profile_only) {
+ read_hosts_file("/etc/hosts", TRUE);
+ }
#endif /* _WIN32 */
- /* XXX - Any flags we should be using? */
- /* XXX - We could provide config settings for DNS servers, and
+ if (gbl_resolv_flags.concurrent_dns) {
+ /* XXX - Any flags we should be using? */
+ /* XXX - We could provide config settings for DNS servers, and
pass them to ADNS with adns_init_strcfg */
- if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
- /*
- * XXX - should we report the error? I'm assuming that some crashes
- * reported on a Windows machine with TCP/IP not configured are due
- * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
- * ADNS in a state where it crashes due to that. We'll still try
- * doing name resolution anyway.
- */
- return;
- }
- async_dns_initialized = TRUE;
- async_dns_in_flight = 0;
+ if (adns_init(&ads, adns_if_none, 0 /*0=>stderr*/) != 0) {
+ /*
+ * XXX - should we report the error? I'm assuming that some crashes
+ * reported on a Windows machine with TCP/IP not configured are due
+ * to "adns_init()" failing (due to the lack of TCP/IP) and leaving
+ * ADNS in a state where it crashes due to that. We'll still try
+ * doing name resolution anyway.
+ */
+ return;
+ }
+ async_dns_initialized = TRUE;
+ async_dns_in_flight = 0;
+ }
#endif /* HAVE_GNU_ADNS */
#endif /* HAVE_C_ARES */
- if(extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only){
- for (i = 0; i < extra_hosts_files->len; i++) {
- read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i));
+ if (extra_hosts_files && !gbl_resolv_flags.load_hosts_file_from_profile_only) {
+ for (i = 0; i < extra_hosts_files->len; i++) {
+ read_hosts_file((const char *) g_ptr_array_index(extra_hosts_files, i), TRUE);
+ }
}
- }
- subnet_name_lookup_init();
+ subnet_name_lookup_init();
+
+ add_manually_resolved();
}
void
host_name_lookup_cleanup(void)
{
- _host_name_lookup_cleanup();
+ guint32 i, j;
+ _host_name_lookup_cleanup();
- memset(ipv4_table, 0, sizeof(ipv4_table));
- memset(ipv6_table, 0, sizeof(ipv6_table));
+ if (ipxnet_hash_table) {
+ g_hash_table_destroy(ipxnet_hash_table);
+ ipxnet_hash_table = NULL;
+ }
- memset(subnet_length_entries, 0, sizeof(subnet_length_entries));
+ if (ipv4_hash_table) {
+ g_hash_table_destroy(ipv4_hash_table);
+ ipv4_hash_table = NULL;
+ }
- addrinfo_list = addrinfo_list_last = NULL;
+ if (ipv6_hash_table) {
+ g_hash_table_destroy(ipv6_hash_table);
+ ipv6_hash_table = NULL;
+ }
- have_subnet_entry = FALSE;
- new_resolved_objects = FALSE;
+ for(i = 0; i < SUBNETLENGTHSIZE; ++i) {
+ if (subnet_length_entries[i].subnet_addresses != NULL) {
+ for (j = 0; j < HASHHOSTSIZE; j++) {
+ if (subnet_length_entries[i].subnet_addresses[j] != NULL)
+ {
+ cleanup_subnet_entry(subnet_length_entries[i].subnet_addresses[j]);
+ }
+ }
+ g_free(subnet_length_entries[i].subnet_addresses);
+ subnet_length_entries[i].subnet_addresses = NULL;
+ }
+ }
+
+ have_subnet_entry = FALSE;
+ new_resolved_objects = FALSE;
}
-gchar *
-get_udp_port(guint port)
+static void
+free_manually_resolved_ipv4(gpointer data, gpointer user_data _U_)
{
+ resolved_ipv4_t *resolved_ipv4_entry = (resolved_ipv4_t *)data;
- if (!gbl_resolv_flags.transport_name) {
- return ep_utoa(port);
- }
-
- return serv_name_lookup(port, PT_UDP);
-
-} /* get_udp_port */
+ g_free(resolved_ipv4_entry);
+}
-gchar *
-get_dccp_port(guint port)
+static void
+free_manually_resolved_ipv6(gpointer data, gpointer user_data _U_)
{
+ resolved_ipv6_t *resolved_ipv6_entry = (resolved_ipv6_t *)data;
- if (!gbl_resolv_flags.transport_name) {
- return ep_utoa(port);
- }
+ g_free(resolved_ipv6_entry);
+}
- return serv_name_lookup(port, PT_DCCP);
+void
+manually_resolve_cleanup(void)
+{
+ if (manually_resolved_ipv4_list) {
+ g_slist_foreach(manually_resolved_ipv4_list, free_manually_resolved_ipv4, NULL);
+ g_slist_free(manually_resolved_ipv4_list);
+ manually_resolved_ipv4_list = NULL;
+ }
-} /* get_dccp_port */
+ if (manually_resolved_ipv6_list) {
+ g_slist_foreach(manually_resolved_ipv6_list, free_manually_resolved_ipv6, NULL);
+ g_slist_free(manually_resolved_ipv6_list);
+ manually_resolved_ipv6_list = NULL;
+ }
+}
gchar *
-get_tcp_port(guint port)
+udp_port_to_display(wmem_allocator_t *allocator, guint port)
{
- if (!gbl_resolv_flags.transport_name) {
- return ep_utoa(port);
- }
+ if (!gbl_resolv_flags.transport_name) {
+ return wmem_utoa(allocator, port);
+ }
- return serv_name_lookup(port, PT_TCP);
+ return wmem_strdup(allocator, serv_name_lookup(port, PT_UDP));
-} /* get_tcp_port */
+} /* udp_port_to_display */
gchar *
-get_sctp_port(guint port)
+dccp_port_to_display(wmem_allocator_t *allocator, guint port)
{
- if (!gbl_resolv_flags.transport_name) {
- return ep_utoa(port);
- }
+ if (!gbl_resolv_flags.transport_name) {
+ return wmem_utoa(allocator, port);
+ }
- return serv_name_lookup(port, PT_SCTP);
+ return wmem_strdup(allocator, serv_name_lookup(port, PT_DCCP));
-} /* get_sctp_port */
+} /* dccp_port_to_display */
-const gchar *
-get_addr_name(const address *addr)
+gchar *
+tcp_port_to_display(wmem_allocator_t *allocator, guint port)
{
- const gchar *result;
- result = solve_address_to_name(addr);
-
- if (result != NULL)
- return result;
-
- /* if it gets here, either it is of type AT_NONE, */
- /* or it should be solvable in address_to_str -unless addr->type is wrongly defined */
+ if (!gbl_resolv_flags.transport_name) {
+ return wmem_utoa(allocator, port);
+ }
- if (addr->type == AT_NONE){
- return "NONE";
- }
+ return wmem_strdup(allocator, serv_name_lookup(port, PT_TCP));
- /* We need an ephemeral allocated string */
- return ep_address_to_str(addr);
-}
+} /* tcp_port_to_display */
-const gchar *
-se_get_addr_name(const address *addr)
+gchar *
+sctp_port_to_display(wmem_allocator_t *allocator, guint port)
{
- const gchar *result;
-
- result = se_solve_address_to_name(addr);
-
- if (result != NULL)
- return result;
- /* if it gets here, either it is of type AT_NONE, */
- /* or it should be solvable in se_address_to_str -unless addr->type is wrongly defined */
+ if (!gbl_resolv_flags.transport_name) {
+ return wmem_utoa(allocator, port);
+ }
- if (addr->type == AT_NONE){
- return "NONE";
- }
+ return wmem_strdup(allocator, serv_name_lookup(port, PT_SCTP));
- /* We need a "permanently" allocated string */
- return se_address_to_str(addr);
-}
+} /* sctp_port_to_display */
-void
-get_addr_name_buf(const address *addr, gchar *buf, gsize size)
+int
+port_with_resolution_to_str_buf(gchar *buf, gulong buf_size, port_type port_typ, guint16 port_num)
{
- const gchar *result = get_addr_name(addr);
-
- g_strlcpy(buf, result, size);
-} /* get_addr_name_buf */
+ const gchar *port_res_str;
+ if (!gbl_resolv_flags.transport_name ||
+ (port_typ == PT_NONE) ||
+ ((port_res_str = serv_name_lookup(port_num, port_typ)) == NULL)) {
+ /* No name resolution support, just return port string */
+ return g_snprintf(buf, buf_size, "%u", port_num);
+ }
+ return g_snprintf(buf, buf_size, "%s (%u)", port_res_str, port_num);
+}
gchar *
get_ether_name(const guint8 *addr)
{
- hashether_t *tp;
- gboolean resolve = gbl_resolv_flags.mac_name;
+ hashether_t *tp;
+ gboolean resolve = gbl_resolv_flags.mac_name;
- tp = eth_name_lookup(addr, resolve);
+ tp = eth_name_lookup(addr, resolve);
- return resolve ? tp->resolved_name : tp->hexaddr;
+ return resolve ? tp->resolved_name : tp->hexaddr;
} /* get_ether_name */
+gchar *
+tvb_get_ether_name(tvbuff_t *tvb, gint offset)
+{
+ return get_ether_name(tvb_get_ptr(tvb, offset, 6));
+}
+
/* Look for a (non-dummy) ether name in the hash, and return it if found.
* If it's not found, simply return NULL.
*/
gchar *
get_ether_name_if_known(const guint8 *addr)
{
- hashether_t *tp;
+ hashether_t *tp;
- /* Initialize ether structs if we're the first
- * ether-related function called */
- if (!gbl_resolv_flags.mac_name)
- return NULL;
+ /* Initialize ether structs if we're the first
+ * ether-related function called */
+ if (!gbl_resolv_flags.mac_name)
+ return NULL;
- /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
- tp = eth_name_lookup(addr, TRUE);
- g_assert(tp != NULL);
+ /* eth_name_lookup will create a (resolved) hash entry if it doesn't exist */
+ tp = eth_name_lookup(addr, TRUE);
+ g_assert(tp != NULL);
- if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
- /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
- return tp->resolved_name;
- }
- else {
- /* Name was created */
- return NULL;
- }
+ if (tp->status == HASHETHER_STATUS_RESOLVED_NAME) {
+ /* Name is from an ethers file (or is a "well-known" MAC address name from the manuf file) */
+ return tp->resolved_name;
+ }
+ else {
+ /* Name was created */
+ return NULL;
+ }
}
guint8 *
get_ether_addr(const gchar *name)
{
- /* force resolution (do not check gbl_resolv_flags) */
- return eth_addr_lookup(name);
+ /* force resolution (do not check gbl_resolv_flags) */
+ return eth_addr_lookup(name);
} /* get_ether_addr */
void
add_ether_byip(const guint ip, const guint8 *eth)
{
- gboolean found;
- hashipv4_t *tp;
+ gboolean found;
+ hashipv4_t *tp;
- /* first check that IP address can be resolved */
- if (!gbl_resolv_flags.network_name)
- return;
+ /* first check that IP address can be resolved */
+ if (!gbl_resolv_flags.network_name)
+ return;
- tp = host_lookup(ip, &found);
- if (found) {
- /* ok, we can add this entry in the ethers hashtable */
- add_eth_name(eth, tp->name);
- }
+ tp = host_lookup(ip, &found);
+ if (found) {
+ /* ok, we can add this entry in the ethers hashtable */
+ add_eth_name(eth, tp->name);
+ }
} /* add_ether_byip */
+gchar *
+ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct)
+{
+ gchar *buf = (gchar *)wmem_alloc(scope, 12);
+
+ *dword_to_hex_punct(buf, ad, punct) = '\0';
+ return buf;
+}
+
const gchar *
-get_ipxnet_name(const guint32 addr)
+get_ipxnet_name(wmem_allocator_t *allocator, const guint32 addr)
{
- if (!gbl_resolv_flags.network_name) {
- return ipxnet_to_str_punct(addr, '\0');
- }
+ if (!gbl_resolv_flags.network_name) {
+ return ipxnet_to_str_punct(allocator, addr, '\0');
+ }
- return ipxnet_name_lookup(addr);
+ return ipxnet_name_lookup(allocator, addr);
} /* get_ipxnet_name */
guint32
get_ipxnet_addr(const gchar *name, gboolean *known)
{
- guint32 addr;
- gboolean success;
+ guint32 addr;
+ gboolean success;
- /* force resolution (do not check gbl_resolv_flags) */
- addr = ipxnet_addr_lookup(name, &success);
+ /* force resolution (do not check gbl_resolv_flags) */
+ addr = ipxnet_addr_lookup(name, &success);
- *known = success;
- return addr;
+ *known = success;
+ return addr;
} /* get_ipxnet_addr */
const gchar *
get_manuf_name(const guint8 *addr)
{
- gchar *cur;
- int manuf_key;
- guint8 oct;
+ hashmanuf_t *manuf_value;
- /* 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;
+ manuf_value = manuf_name_lookup(addr);
+ if (gbl_resolv_flags.mac_name && manuf_value->status != HASHETHER_STATUS_UNRESOLVED)
+ return manuf_value->resolved_name;
- if (!gbl_resolv_flags.mac_name || ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL)) {
- cur=ep_strdup_printf("%02x:%02x:%02x", addr[0], addr[1], addr[2]);
- return cur;
- }
-
- return cur;
+ return manuf_value->hexaddr;
} /* get_manuf_name */
const gchar *
uint_get_manuf_name(const guint oid)
{
- guint8 addr[3];
+ guint8 addr[3];
- addr[0] = (oid >> 16) & 0xFF;
- addr[1] = (oid >> 8) & 0xFF;
- addr[2] = (oid >> 0) & 0xFF;
- return get_manuf_name(addr);
+ addr[0] = (oid >> 16) & 0xFF;
+ addr[1] = (oid >> 8) & 0xFF;
+ addr[2] = (oid >> 0) & 0xFF;
+ return get_manuf_name(addr);
}
const gchar *
tvb_get_manuf_name(tvbuff_t *tvb, gint offset)
{
- return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
+ return get_manuf_name(tvb_get_ptr(tvb, offset, 3));
}
const gchar *
get_manuf_name_if_known(const guint8 *addr)
{
- gchar *cur;
- int manuf_key;
- guint8 oct;
-
- /* 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;
+ hashmanuf_t *manuf_value;
+ int manuf_key;
+ guint8 oct;
- if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
- return NULL;
- }
+ /* 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;
+
+ manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
+ if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
+ return NULL;
+ }
- return cur;
+ return manuf_value->resolved_name;
} /* get_manuf_name_if_known */
const gchar *
uint_get_manuf_name_if_known(const guint manuf_key)
{
- gchar *cur;
+ hashmanuf_t *manuf_value;
- if ((cur = (gchar *)g_hash_table_lookup(manuf_hashtable, &manuf_key)) == NULL) {
- return NULL;
- }
+ manuf_value = (hashmanuf_t *)g_hash_table_lookup(manuf_hashtable, &manuf_key);
+ if ((manuf_value == NULL) || (manuf_value->status != HASHETHER_STATUS_UNRESOLVED)) {
+ return NULL;
+ }
- return cur;
+ return manuf_value->resolved_name;
}
const gchar *
tvb_get_manuf_name_if_known(tvbuff_t *tvb, gint offset)
{
- return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
+ return get_manuf_name_if_known(tvb_get_ptr(tvb, offset, 3));
}
-const gchar *
-get_eui64_name(const guint64 addr_eui64)
+char* get_hash_manuf_resolved_name(hashmanuf_t* manuf)
{
- gchar *cur, *name;
- guint8 *addr = (guint8 *)ep_alloc(8);
-
- /* Copy and convert the address to network byte order. */
- *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
-
- if (!gbl_resolv_flags.mac_name || ((name = manuf_name_lookup(addr)) == NULL)) {
- 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]);
- return cur;
- }
- cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", name, addr[3], addr[4], addr[5], addr[6], addr[7]);
- return cur;
-
-} /* get_eui64_name */
-
+ return manuf->resolved_name;
+}
const gchar *
-get_eui64_name_if_known(const guint64 addr_eui64)
+eui64_to_display(wmem_allocator_t *allocator, const guint64 addr_eui64)
{
- gchar *cur, *name;
- guint8 *addr = (guint8 *)ep_alloc(8);
-
- /* Copy and convert the address to network byte order. */
- *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
+ guint8 *addr = (guint8 *)wmem_alloc(NULL, 8);
+ hashmanuf_t *manuf_value;
+ const gchar *ret;
- if ((name = manuf_name_lookup(addr)) == NULL) {
- return NULL;
- }
+ /* Copy and convert the address to network byte order. */
+ *(guint64 *)(void *)(addr) = pntoh64(&(addr_eui64));
- cur=ep_strdup_printf("%s_%02x:%02x:%02x:%02x:%02x", name, addr[3], addr[4], addr[5], addr[6], addr[7]);
- return cur;
+ manuf_value = manuf_name_lookup(addr);
+ if (!gbl_resolv_flags.mac_name || (manuf_value->status == HASHETHER_STATUS_UNRESOLVED)) {
+ ret = wmem_strdup_printf(allocator, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]);
+ } else {
+ ret = wmem_strdup_printf(allocator, "%s_%02x:%02x:%02x:%02x:%02x", manuf_value->resolved_name, addr[3], addr[4], addr[5], addr[6], addr[7]);
+ }
-} /* get_eui64_name_if_known */
+ wmem_free(NULL, addr);
+ return ret;
+} /* eui64_to_display */
#ifdef HAVE_C_ARES
#define GHI_TIMEOUT (250 * 1000)
static void
+c_ares_ghi_cb(
+ void *arg,
+ int status,
#if ( ( ARES_VERSION_MAJOR < 1 ) \
- || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
-c_ares_ghi_cb(void *arg, int status, struct hostent *hp) {
+ || ( 1 == ARES_VERSION_MAJOR && ARES_VERSION_MINOR < 5 ) )
+ struct hostent *hp
#else
-c_ares_ghi_cb(void *arg, int status, int timeouts _U_, struct hostent *hp) {
+ int timeouts _U_,
+ struct hostent *hp
#endif
- /*
- * XXX - If we wanted to be really fancy we could cache results here and
- * look them up in get_host_ipaddr* below.
- */
- async_hostent_t *ahp = (async_hostent_t *)arg;
- if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
- memcpy(ahp->addrp, hp->h_addr, hp->h_length);
- ahp->copied = hp->h_length;
- }
+ ) {
+
+ /*
+ * XXX - If we wanted to be really fancy we could cache results here and
+ * look them up in get_host_ipaddr* below.
+ */
+ async_hostent_t *ahp = (async_hostent_t *)arg;
+ if (status == ARES_SUCCESS && hp && ahp && hp->h_length == ahp->addr_size) {
+ memcpy(ahp->addrp, hp->h_addr, hp->h_length);
+ ahp->copied = hp->h_length;
+ }
}
#endif /* HAVE_C_ARES */
gboolean
get_host_ipaddr(const char *host, guint32 *addrp)
{
- struct in_addr ipaddr;
+ struct in_addr ipaddr;
#ifdef HAVE_C_ARES
- struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
- int nfds;
- fd_set rfds, wfds;
- async_hostent_t ahe;
-#else /* HAVE_C_ARES */
- struct hostent *hp;
-#endif /* HAVE_C_ARES */
-
- /*
- * don't change it to inet_pton(AF_INET), they are not 100% compatible.
- * inet_pton(AF_INET) does not support hexadecimal notation nor
- * less-than-4 octet notation.
- */
- if (!inet_aton(host, &ipaddr)) {
-
- /* It's not a valid dotted-quad IP address; is it a valid
- * host name?
- */
+ struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
+ int nfds;
+ fd_set rfds, wfds;
+ async_hostent_t ahe;
+#elif defined(HAVE_GETADDRINFO)
+ struct addrinfo hint, *result = NULL;
+#elif defined(HAVE_GETHOSTBYNAME)
+ struct hostent *hp;
+#endif
- /* If we're not allowed to do name resolution, don't do name
- * resolution...
+ /*
+ * don't change it to inet_pton(AF_INET), they are not 100% compatible.
+ * inet_pton(AF_INET) does not support hexadecimal notation nor
+ * less-than-4 octet notation.
*/
- if (!gbl_resolv_flags.network_name ||
- !gbl_resolv_flags.use_external_net_name_resolver) {
- return FALSE;
- }
+ if (!inet_aton(host, &ipaddr)) {
+
+ /* It's not a valid dotted-quad IP address; is it a valid
+ * host name?
+ */
+
+ /* If we're not allowed to do name resolution, don't do name
+ * resolution...
+ */
+ if (!gbl_resolv_flags.network_name ||
+ !gbl_resolv_flags.use_external_net_name_resolver) {
+ return FALSE;
+ }
#ifdef HAVE_C_ARES
- if (! (gbl_resolv_flags.concurrent_dns) ||
- name_resolve_concurrency < 1 ||
- ! async_dns_initialized) {
- return FALSE;
- }
- ahe.addr_size = (int) sizeof (struct in_addr);
- ahe.copied = 0;
- ahe.addrp = addrp;
- ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- nfds = ares_fds(ghbn_chan, &rfds, &wfds);
- if (nfds > 0) {
- tvp = ares_timeout(ghbn_chan, &tv, &tv);
- if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
- fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
+ if (! (gbl_resolv_flags.concurrent_dns) ||
+ name_resolve_concurrency < 1 ||
+ ! async_dns_initialized) {
+ return FALSE;
+ }
+ ahe.addr_size = (int) sizeof (struct in_addr);
+ ahe.copied = 0;
+ ahe.addrp = addrp;
+ ares_gethostbyname(ghbn_chan, host, AF_INET, c_ares_ghi_cb, &ahe);
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ nfds = ares_fds(ghbn_chan, &rfds, &wfds);
+ if (nfds > 0) {
+ tvp = ares_timeout(ghbn_chan, &tv, &tv);
+ if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
+ fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
+ return FALSE;
+ }
+ ares_process(ghbn_chan, &rfds, &wfds);
+ }
+ ares_cancel(ghbn_chan);
+ if (ahe.addr_size == ahe.copied) {
+ return TRUE;
+ }
return FALSE;
- }
- ares_process(ghbn_chan, &rfds, &wfds);
- }
- ares_cancel(ghbn_chan);
- if (ahe.addr_size == ahe.copied) {
- return TRUE;
- }
- return FALSE;
-#else /* ! HAVE_C_ARES */
- hp = gethostbyname(host);
- if (hp == NULL) {
- /* No. */
- return FALSE;
- /* Apparently, some versions of gethostbyaddr can
- * return IPv6 addresses. */
- } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
- memcpy(&ipaddr, hp->h_addr, hp->h_length);
+#elif defined(HAVE_GETADDRINFO)
+ /*
+ * This can be slow, particularly for capture files with lots of
+ * addresses. Should we just return FALSE instead?
+ */
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = AF_INET;
+ if (getaddrinfo(host, NULL, &hint, &result) == 0) {
+ /* Probably more checks than necessary */
+ if (result != NULL) {
+ gboolean ret_val = FALSE;
+ if (result->ai_family == AF_INET && result->ai_addrlen == 4) {
+ memcpy(&ipaddr, result->ai_addr->sa_data, result->ai_addrlen);
+ ret_val = TRUE;
+ }
+ freeaddrinfo(result);
+ return ret_val;
+ }
+ }
+#elif defined(HAVE_GETHOSTBYNAME)
+ hp = gethostbyname(host);
+ if (hp == NULL) {
+ /* No. */
+ return FALSE;
+ /* Apparently, some versions of gethostbyaddr can
+ * return IPv6 addresses. */
+ } else if (hp->h_length <= (int) sizeof (struct in_addr)) {
+ memcpy(&ipaddr, hp->h_addr, hp->h_length);
+ } else {
+ return FALSE;
+ }
+#endif
} else {
- return FALSE;
+ /* Does the string really contain dotted-quad IP?
+ * Check against inet_atons that accept strings such as
+ * "130.230" as valid addresses and try to convert them
+ * to some form of a classful (host.net) notation.
+ */
+ unsigned int a0, a1, a2, a3;
+ if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
+ return FALSE;
}
-#endif /* HAVE_C_ARES */
- } else {
- /* Does the string really contain dotted-quad IP?
- * Check against inet_atons that accept strings such as
- * "130.230" as valid addresses and try to convert them
- * to some form of a classful (host.net) notation.
- */
- unsigned int a0, a1, a2, a3;
- if (sscanf(host, "%u.%u.%u.%u", &a0, &a1, &a2, &a3) != 4)
- return FALSE;
- }
- *addrp = ipaddr.s_addr;
- return TRUE;
+ *addrp = ipaddr.s_addr;
+ return TRUE;
}
/*
get_host_ipaddr6(const char *host, struct e_in6_addr *addrp)
{
#ifdef HAVE_C_ARES
- struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
- int nfds;
- fd_set rfds, wfds;
- async_hostent_t ahe;
+ struct timeval tv = { 0, GHI_TIMEOUT }, *tvp;
+ int nfds;
+ fd_set rfds, wfds;
+ async_hostent_t ahe;
+#elif defined(HAVE_GETADDRINFO)
+ struct addrinfo hint, *result = NULL;
#elif defined(HAVE_GETHOSTBYNAME2)
- struct hostent *hp;
+ struct hostent *hp;
#endif /* HAVE_C_ARES */
- if (inet_pton(AF_INET6, host, addrp) > 0)
- return TRUE;
+ if (str_to_ip6(host, addrp))
+ return TRUE;
- /* It's not a valid dotted-quad IP address; is it a valid
- * host name?
- */
+ /* It's not a valid dotted-quad IP address; is it a valid
+ * host name?
+ */
- /* If we're not allowed to do name resolution, don't do name
- * resolution...
- */
- if (!gbl_resolv_flags.network_name ||
- !gbl_resolv_flags.use_external_net_name_resolver) {
- return FALSE;
- }
+ /* If we're not allowed to do name resolution, don't do name
+ * resolution...
+ */
+ if (!gbl_resolv_flags.network_name ||
+ !gbl_resolv_flags.use_external_net_name_resolver) {
+ return FALSE;
+ }
- /* try FQDN */
+ /* try FQDN */
#ifdef HAVE_C_ARES
- if (! (gbl_resolv_flags.concurrent_dns) ||
- name_resolve_concurrency < 1 ||
- ! async_dns_initialized) {
- return FALSE;
- }
- ahe.addr_size = (int) sizeof (struct e_in6_addr);
- ahe.copied = 0;
- ahe.addrp = addrp;
- ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- nfds = ares_fds(ghbn_chan, &rfds, &wfds);
- if (nfds > 0) {
- tvp = ares_timeout(ghbn_chan, &tv, &tv);
- if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
- fprintf(stderr, "Warning: call to select() failed, error is %s\n", strerror(errno));
+ if (! (gbl_resolv_flags.concurrent_dns) ||
+ name_resolve_concurrency < 1 ||
+ ! async_dns_initialized) {
return FALSE;
}
- ares_process(ghbn_chan, &rfds, &wfds);
- }
- ares_cancel(ghbn_chan);
- if (ahe.addr_size == ahe.copied) {
- return TRUE;
- }
+ ahe.addr_size = (int) sizeof (struct e_in6_addr);
+ ahe.copied = 0;
+ ahe.addrp = addrp;
+ ares_gethostbyname(ghbn_chan, host, AF_INET6, c_ares_ghi_cb, &ahe);
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ nfds = ares_fds(ghbn_chan, &rfds, &wfds);
+ if (nfds > 0) {
+ tvp = ares_timeout(ghbn_chan, &tv, &tv);
+ if (select(nfds, &rfds, &wfds, NULL, tvp) == -1) { /* call to select() failed */
+ fprintf(stderr, "Warning: call to select() failed, error is %s\n", g_strerror(errno));
+ return FALSE;
+ }
+ ares_process(ghbn_chan, &rfds, &wfds);
+ }
+ ares_cancel(ghbn_chan);
+ if (ahe.addr_size == ahe.copied) {
+ return TRUE;
+ }
+#elif defined(HAVE_GETADDRINFO)
+ /*
+ * This can be slow, particularly for capture files with lots of
+ * addresses. Should we just return FALSE instead?
+ */
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = AF_INET6;
+ if (getaddrinfo(host, NULL, &hint, &result) == 0) {
+ /* Probably more checks than necessary */
+ if (result != NULL) {
+ gboolean ret_val = FALSE;
+ if (result->ai_family == AF_INET6 && result->ai_addrlen == sizeof(struct e_in6_addr)) {
+ memcpy(addrp, result->ai_addr->sa_data, result->ai_addrlen);
+ ret_val = TRUE;
+ }
+ freeaddrinfo(result);
+ return ret_val;
+ }
+ }
#elif defined(HAVE_GETHOSTBYNAME2)
- hp = gethostbyname2(host, AF_INET6);
- if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
- memcpy(addrp, hp->h_addr, hp->h_length);
- return TRUE;
- }
+ hp = gethostbyname2(host, AF_INET6);
+ if (hp != NULL && hp->h_length == sizeof(struct e_in6_addr)) {
+ memcpy(addrp, hp->h_addr, hp->h_length);
+ return TRUE;
+ }
#endif
- return FALSE;
+ return FALSE;
}
/*
* Return "ip6" if it is IPv6, "ip" otherwise (including the case
* that we don't know)
*/
-const char* host_ip_af(const char *host
-#ifndef HAVE_GETHOSTBYNAME2
-_U_
+const char *
+host_ip_af(const char *host
+#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETHOSTBYNAME2)
+ _U_
#endif
-)
-{
-#ifdef HAVE_GETHOSTBYNAME2
- struct hostent *h;
- return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
-#else
- return "ip";
+ )
+{
+ const char *af = "ip";
+#ifdef HAVE_GETADDRINFO
+ struct addrinfo hint, *result = NULL;
+ memset(&hint, 0, sizeof(hint));
+ hint.ai_family = AF_UNSPEC;
+ if (getaddrinfo(host, NULL, &hint, &result) == 0) {
+ if (result->ai_family == AF_INET6) {
+ af = "ip6";
+ }
+ freeaddrinfo(result);
+ }
+#elif defined(HAVE_GETHOSTBYNAME2)
+ struct hostent *h;
+ return (h = gethostbyname2(host, AF_INET6)) && h->h_addrtype == AF_INET6 ? "ip6" : "ip";
#endif
+ return af;
}
GHashTable *
get_manuf_hashtable(void)
{
- return manuf_hashtable;
+ return manuf_hashtable;
}
GHashTable *
get_wka_hashtable(void)
{
- return wka_hashtable;
+ return wka_hashtable;
}
GHashTable *
get_eth_hashtable(void)
{
- return eth_hashtable;
+ return eth_hashtable;
}
GHashTable *
get_serv_port_hashtable(void)
{
- return serv_port_hashtable;
+ return serv_port_hashtable;
}
+GHashTable *
+get_ipxnet_hash_table(void)
+{
+ return ipxnet_hash_table;
+}
+
+GHashTable *
+get_ipv4_hash_table(void)
+{
+ return ipv4_hash_table;
+}
+
+GHashTable *
+get_ipv6_hash_table(void)
+{
+ return ipv6_hash_table;
+}
/* Initialize all the address resolution subsystems in this file */
void
addr_resolv_init(void)
/* host name initialization is done on a per-capture-file basis */
/*host_name_lookup_cleanup();*/
}
+
+gboolean
+str_to_ip(const char *str, void *dst)
+{
+ return inet_pton(AF_INET, str, dst) > 0;
+}
+
+gboolean
+str_to_ip6(const char *str, void *dst)
+{
+ return inet_pton(AF_INET6, str, dst) > 0;
+}
+
+/*
+ * Editor modelines - http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */