* sites still using NIS rather than DNS for that....)
*/
-#ifdef HAVE_GNU_ADNS
-# include <errno.h>
-# include <adns.h>
-# ifdef inet_aton
-# undef inet_aton
-# endif
-#endif
-
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
# include <ws2tcpip.h>
#endif
+#ifdef HAVE_C_ARES
+# if defined(_WIN32) && !defined(INET6)
+# define socklen_t unsigned int
+# endif
+# include <ares.h>
+#else
+# ifdef HAVE_GNU_ADNS
+# include <errno.h>
+# include <adns.h>
+# ifdef inet_aton
+# undef inet_aton
+# endif
+# endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
+
+
#include <glib.h>
#include "report_err.h"
#include "ipv6-utils.h"
#include "addr_resolv.h"
#include "filesystem.h"
-#include <wiretap/file_util.h>
+
+#include <epan/strutil.h>
+#include <wsutil/file_util.h>
#include <epan/prefs.h>
#include <epan/emem.h>
#define ENAME_HOSTS "hosts"
+#define ENAME_SUBNETS "subnets"
#define ENAME_ETHERS "ethers"
#define ENAME_IPXNETS "ipxnets"
#define ENAME_MANUF "manuf"
#define HASHIPXNETSIZE 256
#define HASHMANUFSIZE 256
#define HASHPORTSIZE 256
+#define SUBNETLENGTHSIZE 32 /*1-32 inc.*/
/* hash table used for IPv4 lookup */
struct hashipv6 *next;
} hashipv6_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 */
+} subnet_length_entry_t;
+
/* hash table used for TCP/UDP/SCTP port lookup */
#define HASH_PORT(port) ((port) & (HASHPORTSIZE - 1))
static hashipv4_t *ipv4_table[HASHHOSTSIZE];
static hashipv6_t *ipv6_table[HASHHOSTSIZE];
+
+static hashport_t **cb_port_table;
+static gchar *cb_service;
+
static hashport_t *udp_port_table[HASHPORTSIZE];
static hashport_t *tcp_port_table[HASHPORTSIZE];
static hashport_t *sctp_port_table[HASHPORTSIZE];
static hashether_t *(*wka_table[48])[HASHETHSIZE];
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 int eth_resolution_initialized = 0;
static int ipxnet_resolution_initialized = 0;
static int service_resolution_initialized = 0;
static hashether_t *add_eth_name(const guint8 *addr, const gchar *name);
+static void add_serv_port_cb(guint32 port);
/*
* Flag controlling what names to resolve.
gchar *g_pservices_path = NULL; /* personal services file */
/* first resolving call */
-/* GNU ADNS */
+/* c-ares */
+#ifdef HAVE_C_ARES
+/*
+ * Submitted queries trigger a callback (c_ares_ghba_cb()).
+ * Queries are added to c_ares_queue_head. During processing, queries are
+ * popped off the front of c_ares_queue_head and submitted using
+ * ares_gethostbyaddr().
+ * The callback processes the response, then frees the request.
+ */
+static gboolean c_ares_initialized = FALSE;
+ares_channel alchan;
+int c_ares_in_flight = 0;
+GList *c_ares_queue_head = NULL;
+
+typedef struct _c_ares_queue_msg
+{
+ union {
+ guint32 ip4;
+ struct e_in6_addr ip6;
+ } addr;
+ int family;
+} c_ares_queue_msg_t;
+
+static void c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *hostent);
+
+#else
+/* GNU ADNS */
#ifdef HAVE_GNU_ADNS
-static gboolean gnu_adns_initialized = FALSE;
+/*
+ * Submitted queries have to be checked individually using adns_check().
+ * Queries are added to adns_queue_head. During processing, the list is
+ * iterated twice: once to request queries up to the concurrency limit,
+ * and once to check the status of each query.
+ */
+static gboolean gnu_adns_initialized = FALSE;
adns_state ads;
-
-int adns_currently_queued = 0;
+int adns_in_flight = 0;
+GList *adns_queue_head = NULL;
typedef struct _adns_queue_msg
{
gboolean submitted;
guint32 ip4_addr;
- struct e_in6_addr ip6_addr;
int type;
adns_query query;
} adns_queue_msg_t;
-GList *adns_queue_head = NULL;
-
#endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
+typedef struct {
+ guint32 mask;
+ gsize mask_length;
+ const gchar* name; /* Shallow copy */
+} subnet_entry_t;
/*
* Miscellaneous functions
/*
* Local function definitions
*/
+static subnet_entry_t subnet_lookup(const guint32 addr);
+static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name);
static void add_service_name(hashport_t **proto_table, guint port, const char *service_name)
{
int hash_idx;
hashport_t *tp;
-
+
hash_idx = HASH_PORT(port);
tp = proto_table[hash_idx];
tp->port = port;
tp->next = NULL;
- strncpy(tp->name, service_name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, service_name, MAXNAMELEN);
}
gchar *service;
gchar *port;
+ range_t *port_rng = NULL;
+ guint32 max_port = MAX_UDP_PORT;
if ((cp = strchr(line, '#')))
*cp = '\0';
/* seems we got all interesting things from the file */
if(strcmp(cp, "tcp") == 0) {
- add_service_name(tcp_port_table, atoi(port), service);
- return;
+ max_port = MAX_TCP_PORT;
+ cb_port_table = tcp_port_table;
}
-
- if(strcmp(cp, "udp") == 0) {
- add_service_name(udp_port_table, atoi(port), service);
- return;
+ else if(strcmp(cp, "udp") == 0) {
+ max_port = MAX_UDP_PORT;
+ cb_port_table = udp_port_table;
}
-
- if(strcmp(cp, "sctp") == 0) {
- add_service_name(sctp_port_table, atoi(port), service);
+ else if(strcmp(cp, "sctp") == 0) {
+ max_port = MAX_SCTP_PORT;
+ cb_port_table = sctp_port_table;
+ }
+ else if(strcmp(cp, "dccp") == 0) {
+ max_port = MAX_DCCP_PORT;
+ cb_port_table = dccp_port_table;
+ } else {
return;
}
- if(strcmp(cp, "dcp") == 0) {
- add_service_name(dccp_port_table, atoi(port), service);
+ if(CVT_NO_ERROR != range_convert_str(&port_rng, port, max_port) ) {
+ /* some assertion here? */
return;
}
+ cb_service = service;
+ range_foreach(port_rng, add_serv_port_cb);
+ g_free (port_rng);
} /* parse_service_line */
+static void
+add_serv_port_cb(guint32 port)
+{
+ if ( port ) {
+ add_service_name(cb_port_table, port, cb_service);
+ }
+}
+
static void parse_services_file(const char * path)
{
static char *buf = NULL;
/* services hash table initialization */
- serv_p = eth_fopen(path, "r");
+ serv_p = ws_fopen(path, "r");
if (serv_p == NULL)
return;
/* set personal services path */
if (g_pservices_path == NULL)
- g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE);
+ g_pservices_path = get_persconffile_path(ENAME_SERVICES, FALSE, FALSE);
parse_services_file(g_pservices_path);
/* not yet implemented */
return NULL;
/*NOTREACHED*/
- break;
} /* proto */
hash_idx = HASH_PORT(port);
/* unknown port */
g_snprintf(tp->name, MAXNAMELEN, "%d", port);
} else {
- strncpy(tp->name, servp->s_name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, servp->s_name, MAXNAMELEN);
}
return (tp->name);
}
#endif /* AVOID_DNS_TIMEOUT */
+/* Fill in an IP4 structure with info from subnets file or just with the
+ * string form of the address.
+ */
+static void fill_dummy_ip4(guint addr, hashipv4_t* volatile tp)
+{
+ subnet_entry_t subnet_entry;
+ tp->is_dummy_entry = TRUE; /* Overwrite if we get async DNS reply */
+
+ /* Do we have a subnet for this address? */
+ subnet_entry = subnet_lookup(addr);
+ if(0 != subnet_entry.mask) {
+ /* Print name, then '.' then IP address after subnet mask */
+ guint32 host_addr;
+ gchar buffer[MAX_IP_STR_LEN];
+ gchar* paddr;
+ gsize i;
+
+ host_addr = addr & (~(guint32)subnet_entry.mask);
+ ip_to_str_buf((guint8 *)&host_addr, buffer, MAX_IP_STR_LEN);
+ paddr = buffer;
+
+ /* Skip to first octet that is not totally masked
+ * If length of mask is 32, we chomp the whole address.
+ * If the address string starts '.' (should not happen?),
+ * we skip that '.'.
+ */
+ i = subnet_entry.mask_length / 8;
+ while(*(paddr) != '\0' && i > 0) {
+ if(*(++paddr) == '.') {
+ --i;
+ }
+ }
+
+ /* There are more efficient ways to do this, but this is safe if we
+ * trust g_snprintf and MAXNAMELEN
+ */
+ g_snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
+ } else {
+ ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
+ }
+}
+
+#ifdef HAVE_C_ARES
+
+static void
+c_ares_ghba_cb(void *arg, int status, int timeouts _U_, struct hostent *he) {
+ c_ares_queue_msg_t *caqm = arg;
+ char **p;
+ char name[MAXNAMELEN];
+
+ if (!caqm) return;
+ c_ares_in_flight--;
+
+ if (status == ARES_SUCCESS) {
+ for (p = he->h_addr_list; *p != NULL; p++) {
+ inet_ntop(he->h_addrtype, *p, name, sizeof(name));
+ switch(caqm->family) {
+ case AF_INET:
+ add_ipv4_name(caqm->addr.ip4, name);
+ break;
+ case AF_INET6:
+ add_ipv6_name(&caqm->addr.ip6, name);
+ break;
+ default:
+ /* Throw an exception? */
+ break;
+ }
+ }
+ }
+ g_free(caqm);
+}
+#endif /* HAVE_C_ARES */
+
static gchar *host_name_lookup(guint addr, gboolean *found)
{
int hash_idx;
hashipv4_t * volatile tp;
struct hostent *hostp;
+#ifdef HAVE_C_ARES
+ c_ares_queue_msg_t *caqm;
+#else
#ifdef HAVE_GNU_ADNS
adns_queue_msg_t *qmsg;
-#endif
+#endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
*found = TRUE;
tp->addr = addr;
tp->next = NULL;
+#ifdef HAVE_C_ARES
+ if ((g_resolv_flags & RESOLV_CONCURRENT) &&
+ prefs.name_resolve_concurrency > 0 &&
+ c_ares_initialized) {
+ caqm = g_malloc(sizeof(c_ares_queue_msg_t));
+ caqm->family = AF_INET;
+ caqm->addr.ip4 = addr;
+ c_ares_queue_head = g_list_append(c_ares_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.
+ */
+ fill_dummy_ip4(addr, tp);
+ return tp->name;
+ }
+#else
#ifdef HAVE_GNU_ADNS
if ((g_resolv_flags & RESOLV_CONCURRENT) &&
prefs.name_resolve_concurrency > 0 &&
qmsg->submitted = FALSE;
adns_queue_head = g_list_append(adns_queue_head, (gpointer) qmsg);
- tp->is_dummy_entry = TRUE;
- ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
+ /* 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->name;
}
#endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
/*
* The Windows "gethostbyaddr()" insists on translating 0.0.0.0 to
# endif /* AVOID_DNS_TIMEOUT */
if (hostp != NULL) {
- strncpy(tp->name, hostp->h_name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
tp->is_dummy_entry = FALSE;
return tp->name;
}
}
/* unknown host or DNS timeout */
-
- ip_to_str_buf((guint8 *)&addr, tp->name, MAXNAMELEN);
- tp->is_dummy_entry = TRUE;
*found = FALSE;
+ fill_dummy_ip4(addr, tp);
return (tp->name);
} /* host_name_lookup */
{
int hash_idx;
hashipv6_t * volatile tp;
+#ifdef HAVE_C_ARES
+ c_ares_queue_msg_t *caqm;
+#endif /* HAVE_C_ARES */
#ifdef INET6
struct hostent *hostp;
#endif
tp->addr = *addr;
tp->next = NULL;
+#ifdef HAVE_C_ARES
+ if ((g_resolv_flags & RESOLV_CONCURRENT) &&
+ prefs.name_resolve_concurrency > 0 &&
+ c_ares_initialized) {
+ caqm = g_malloc(sizeof(c_ares_queue_msg_t));
+ caqm->family = AF_INET6;
+ memcpy(&caqm->addr.ip6, addr, sizeof(caqm->addr.ip6));
+ c_ares_queue_head = g_list_append(c_ares_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.
+ */
+ ip6_to_str_buf(addr, tp->name);
+ tp->is_dummy_entry = TRUE;
+ return tp->name;
+ }
+#endif /* HAVE_C_ARES */
+
#ifdef INET6
if (g_resolv_flags & RESOLV_NETWORK) {
#ifdef AVOID_DNS_TIMEOUT
# endif /* AVOID_DNS_TIMEOUT */
if (hostp != NULL) {
- strncpy(tp->name, hostp->h_name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, hostp->h_name, MAXNAMELEN);
tp->is_dummy_entry = FALSE;
return tp->name;
}
if ((cp = strtok(NULL, " \t")) == NULL)
return -1;
- strncpy(eth->name, cp, MAXNAMELEN);
- eth->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(eth->name, cp, MAXNAMELEN);
return 0;
if (eth_p)
rewind(eth_p);
else
- eth_p = eth_fopen(path, "r");
+ eth_p = ws_fopen(path, "r");
}
static void end_ethent(void)
}
memcpy(tp->addr, addr, sizeof(tp->addr));
- strncpy(tp->name, name, MAXMANUFLEN);
- tp->name[MAXMANUFLEN-1] = '\0';
+ g_strlcpy(tp->name, name, MAXMANUFLEN);
tp->next = NULL;
return;
}
}
memcpy(etp->addr, addr, sizeof(etp->addr));
- strncpy(etp->name, name, MAXNAMELEN);
- etp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(etp->name, name, MAXNAMELEN);
etp->next = NULL;
etp->is_dummy_entry = FALSE;
{
int hash_idx;
hashmanuf_t *tp;
+ guint8 stripped_addr[3];
hash_idx = HASH_ETH_MANUF(addr);
+ /* first try to find a "perfect match" */
tp = manuf_table[hash_idx];
-
while(tp != NULL) {
if (memcmp(tp->addr, addr, sizeof(tp->addr)) == 0) {
return tp;
tp = tp->next;
}
+ /* 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 */
+ memcpy(stripped_addr, addr, 3);
+ stripped_addr[0] &= 0xFE;
+
+ tp = manuf_table[hash_idx];
+ while(tp != NULL) {
+ if (memcmp(tp->addr, stripped_addr, sizeof(tp->addr)) == 0) {
+ return tp;
+ }
+ tp = tp->next;
+ }
+
return NULL;
} /* manuf_name_lookup */
* 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);
+ g_pethers_path = get_persconffile_path(ENAME_ETHERS, FALSE, FALSE);
/* manuf hash table initialization */
}
}
- strncpy(tp->name, name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, name, MAXNAMELEN);
if (new_one) {
memcpy(tp->addr, addr, sizeof(tp->addr));
tp->next = NULL;
tp->is_dummy_entry = TRUE;
} else {
- strncpy(tp->name, eth->name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, eth->name, MAXNAMELEN);
tp->is_dummy_entry = FALSE;
}
ipxnet->addr = (a0 << 24) | (a1 << 16) | (a2 << 8) | a3;
}
- strncpy(ipxnet->name, cp, MAXNAMELEN);
- ipxnet->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(ipxnet->name, cp, MAXNAMELEN);
return 0;
if (ipxnet_p)
rewind(ipxnet_p);
else
- ipxnet_p = eth_fopen(path, "r");
+ ipxnet_p = ws_fopen(path, "r");
}
static void end_ipxnetent(void)
* 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);
+ g_pipxnets_path = get_persconffile_path(ENAME_IPXNETS, FALSE, FALSE);
} /* initialize_ipxnets */
}
tp->addr = addr;
- strncpy(tp->name, name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, name, MAXNAMELEN);
tp->next = NULL;
return tp;
g_snprintf(tp->name, MAXNAMELEN, "%X", addr);
} else {
- strncpy(tp->name, ipxnet->name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, ipxnet->name, MAXNAMELEN);
}
return (tp->name);
* See the hosts(4) or hosts(5) man page for hosts file format
* (not available on all systems).
*/
- if ((hf = eth_fopen(hostspath, "r")) == NULL)
+ if ((hf = ws_fopen(hostspath, "r")) == NULL)
return FALSE;
while (fgetline(&line, &size, hf) >= 0) {
return TRUE;
} /* read_hosts_file */
+
+/* Read in a list of subnet definition - name pairs.
+ * <line> = <comment> | <entry> | <whitespace>
+ * <comment> = <whitespace>#<any>
+ * <entry> = <subnet_definition> <whitespace> <subnet_name> [<comment>|<whitespace><any>]
+ * <subnet_definition> = <ipv4_address> / <subnet_mask_length>
+ * <ipv4_address> is a full address; it will be masked to get the subnet-ID.
+ * <subnet_mask_length> is a decimal 1-31
+ * <subnet_name> is a string containing no whitespace.
+ * <whitespace> = (space | tab)+
+ * Any malformed entries are ignored.
+ * Any trailing data after the subnet_name is ignored.
+ *
+ * XXX Support IPv6
+ */
+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;
+
+ if ((hf = ws_fopen(subnetspath, "r")) == NULL)
+ return FALSE;
+
+ while (fgetline(&line, &size, hf) >= 0) {
+ if ((cp = strchr(line, '#')))
+ *cp = '\0';
+
+ 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 ;
+
+ /* Check if this is a valid IPv4 address */
+ if (inet_pton(AF_INET, cp, &host_addr) != 1) {
+ continue; /* no */
+ }
+
+ mask_length = atoi(cp2);
+ if(0 >= mask_length || mask_length > 31) {
+ continue; /* invalid mask length */
+ }
+
+ if ((cp = strtok(NULL, " \t")) == NULL)
+ continue; /* no subnet name */
+
+ subnet_entry_set(host_addr, (guint32)mask_length, cp);
+ }
+ if (line != NULL)
+ g_free(line);
+
+ fclose(hf);
+ return TRUE;
+} /* read_subnets_file */
+
+static subnet_entry_t subnet_lookup(const guint32 addr)
+{
+ subnet_entry_t subnet_entry;
+ guint32 i;
+
+ /* Search mask lengths linearly, longest first */
+
+ 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);
+
+
+ length_entry = &subnet_length_entries[i];
+
+ if(NULL != length_entry->subnet_addresses) {
+ hashipv4_t * tp;
+ guint32 hash_idx;
+
+ 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;
+ }
+
+ 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;
+
+ return subnet_entry;
+}
+
+/* Add a subnet-definition - name pair to the set.
+ * The definition is taken by masking the address passed in with the mask of the
+ * given length.
+ */
+static void subnet_entry_set(guint32 subnet_addr, guint32 mask_length, const gchar* name)
+{
+ subnet_length_entry_t* entry;
+ hashipv4_t * tp;
+ gsize hash_idx;
+
+ g_assert(mask_length > 0 && mask_length <= 32);
+
+ entry = &subnet_length_entries[mask_length - 1];
+
+ subnet_addr &= entry->mask;
+
+ hash_idx = HASH_IPV4_ADDRESS(subnet_addr);
+
+ if(NULL == entry->subnet_addresses) {
+ entry->subnet_addresses = g_new0(hashipv4_t*,HASHHOSTSIZE);
+ }
+
+ if(NULL != (tp = entry->subnet_addresses[hash_idx])) {
+ if(tp->addr == subnet_addr) {
+ return; /* XXX provide warning that an address was repeated? */
+ } else {
+ hashipv4_t * new_tp = g_new(hashipv4_t,1);
+ tp->next = new_tp;
+ tp = new_tp;
+ }
+ } else {
+ tp = entry->subnet_addresses[hash_idx] = g_new(hashipv4_t,1);
+ }
+
+ 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;
+}
+
+static guint32 get_subnet_mask(guint32 mask_length) {
+
+ static guint32 masks[SUBNETLENGTHSIZE];
+ static gboolean initialised = FALSE;
+
+ if(!initialised) {
+ memset(masks, 0, sizeof(masks));
+
+ initialised = TRUE;
+
+ /* XXX There must be a better way to do this than
+ * hand-coding the values, but I can't seem to
+ * come up with one!
+ */
+
+ inet_pton(AF_INET, "128.0.0.0", &masks[0]);
+ inet_pton(AF_INET, "192.0.0.0", &masks[1]);
+ inet_pton(AF_INET, "224.0.0.0", &masks[2]);
+ inet_pton(AF_INET, "240.0.0.0", &masks[3]);
+ inet_pton(AF_INET, "248.0.0.0", &masks[4]);
+ inet_pton(AF_INET, "252.0.0.0", &masks[5]);
+ inet_pton(AF_INET, "254.0.0.0", &masks[6]);
+ inet_pton(AF_INET, "255.0.0.0", &masks[7]);
+
+ inet_pton(AF_INET, "255.128.0.0", &masks[8]);
+ inet_pton(AF_INET, "255.192.0.0", &masks[9]);
+ inet_pton(AF_INET, "255.224.0.0", &masks[10]);
+ inet_pton(AF_INET, "255.240.0.0", &masks[11]);
+ inet_pton(AF_INET, "255.248.0.0", &masks[12]);
+ inet_pton(AF_INET, "255.252.0.0", &masks[13]);
+ inet_pton(AF_INET, "255.254.0.0", &masks[14]);
+ inet_pton(AF_INET, "255.255.0.0", &masks[15]);
+
+ inet_pton(AF_INET, "255.255.128.0", &masks[16]);
+ inet_pton(AF_INET, "255.255.192.0", &masks[17]);
+ inet_pton(AF_INET, "255.255.224.0", &masks[18]);
+ inet_pton(AF_INET, "255.255.240.0", &masks[19]);
+ inet_pton(AF_INET, "255.255.248.0", &masks[20]);
+ inet_pton(AF_INET, "255.255.252.0", &masks[21]);
+ inet_pton(AF_INET, "255.255.254.0", &masks[22]);
+ inet_pton(AF_INET, "255.255.255.0", &masks[23]);
+
+ inet_pton(AF_INET, "255.255.255.128", &masks[24]);
+ inet_pton(AF_INET, "255.255.255.192", &masks[25]);
+ inet_pton(AF_INET, "255.255.255.224", &masks[26]);
+ inet_pton(AF_INET, "255.255.255.240", &masks[27]);
+ inet_pton(AF_INET, "255.255.255.248", &masks[28]);
+ inet_pton(AF_INET, "255.255.255.252", &masks[29]);
+ inet_pton(AF_INET, "255.255.255.254", &masks[30]);
+ inet_pton(AF_INET, "255.255.255.255", &masks[31]);
+ }
+
+ if(mask_length == 0 || mask_length > SUBNETLENGTHSIZE) {
+ g_assert_not_reached();
+ return 0;
+ } else {
+ return masks[mask_length - 1];
+ }
+}
+
+static void subnet_name_lookup_init(void)
+{
+ gchar* subnetspath;
+
+ guint32 i;
+ 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 = get_subnet_mask(length);
+ }
+
+ subnetspath = get_persconffile_path(ENAME_SUBNETS, FALSE, 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);
+}
+
/*
* External Functions
*/
/*
* Load the user's hosts file, if they have one.
*/
- hostspath = get_persconffile_path(ENAME_HOSTS, FALSE);
+ hostspath = get_persconffile_path(ENAME_HOSTS, FALSE, FALSE);
if (!read_hosts_file(hostspath) && errno != ENOENT) {
report_open_failure(hostspath, errno, FALSE);
}
}
g_free(hostspath);
+#ifdef HAVE_C_ARES
+ if (ares_init(&alchan) == ARES_SUCCESS) {
+ c_ares_initialized = TRUE;
+ }
+#else
#ifdef HAVE_GNU_ADNS
/*
* We're using GNU ADNS, which doesn't check the system hosts file;
return;
}
gnu_adns_initialized = TRUE;
- adns_currently_queued = 0;
+ adns_in_flight = 0;
#endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
+
+ subnet_name_lookup_init();
}
-#ifdef HAVE_GNU_ADNS
+#ifdef HAVE_C_ARES
+gboolean
+host_name_lookup_process(gpointer data _U_) {
+ c_ares_queue_msg_t *caqm;
+ struct timeval tv = { 0, 0 };
+ int nfds;
+ fd_set rfds, wfds;
+
+ c_ares_queue_head = g_list_first(c_ares_queue_head);
+
+ while (c_ares_queue_head && c_ares_in_flight <= prefs.name_resolve_concurrency) {
+ caqm = (c_ares_queue_msg_t *) c_ares_queue_head->data;
+ c_ares_queue_head = g_list_remove(c_ares_queue_head, (void *) caqm);
+ if (caqm->family == AF_INET) {
+ ares_gethostbyaddr(alchan, &caqm->addr.ip4, sizeof(guint32), AF_INET,
+ c_ares_ghba_cb, caqm);
+ c_ares_in_flight++;
+ } else if (caqm->family == AF_INET6) {
+ ares_gethostbyaddr(alchan, &caqm->addr.ip6, sizeof(struct e_in6_addr),
+ AF_INET, c_ares_ghba_cb, caqm);
+ c_ares_in_flight++;
+ }
+ }
+
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ nfds = ares_fds(alchan, &rfds, &wfds);
+ if (nfds > 0) {
+ select(nfds, &rfds, &wfds, NULL, &tv);
+ ares_process(alchan, &rfds, &wfds);
+ }
+
+ /* Keep the timeout in place */
+ return TRUE;
+}
+
+void
+host_name_lookup_cleanup(void) {
+ GList *cur;
+
+ cur = g_list_first(c_ares_queue_head);
+ while (cur) {
+ g_free(cur->data);
+ cur = g_list_next (cur);
+ }
+
+ g_list_free(c_ares_queue_head);
+
+ if (c_ares_initialized)
+ ares_destroy(alchan);
+ c_ares_initialized = FALSE;
+}
+
+#elif defined(HAVE_GNU_ADNS)
/* XXX - The ADNS "documentation" isn't very clear:
* - Do we need to keep our query structures around?
*/
-gint
+gboolean
host_name_lookup_process(gpointer data _U_) {
adns_queue_msg_t *almsg;
GList *cur;
adns_queue_head = g_list_first(adns_queue_head);
cur = adns_queue_head;
- while (cur && adns_currently_queued <= prefs.name_resolve_concurrency) {
+ while (cur && adns_in_flight <= prefs.name_resolve_concurrency) {
almsg = (adns_queue_msg_t *) cur->data;
if (! almsg->submitted && almsg->type == AF_INET) {
addr_bytes = (guint8 *) &almsg->ip4_addr;
/* XXX - what if it fails? */
adns_submit (ads, addr_str, adns_r_ptr, 0, NULL, &almsg->query);
almsg->submitted = TRUE;
- adns_currently_queued++;
+ adns_in_flight++;
}
cur = cur->next;
}
if (dequeue) {
adns_queue_head = g_list_remove(adns_queue_head, (void *) almsg);
g_free(almsg);
- adns_currently_queued--;
+ adns_in_flight--;
}
}
/* Keep the timeout in place */
- return 1;
+ return TRUE;
}
void
if (gnu_adns_initialized)
adns_finish(ads);
+ gnu_adns_initialized = FALSE;
}
-#else
+#else /* HAVE_GNU_ADNS */
-gint
+gboolean
host_name_lookup_process(gpointer data _U_) {
/* Kill the timeout, as there's nothing for it to do */
- return 0;
+ return FALSE;
}
void
host_name_lookup_cleanup(void) {
}
-#endif /* HAVE_GNU_ADNS */
+#endif /* HAVE_C_ARES */
-extern gchar *get_hostname(guint addr)
+extern const gchar *get_hostname(guint addr)
{
gboolean found;
}
}
- strncpy(tp->name, name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, name, MAXNAMELEN);
if (new_one) {
tp->addr = addr;
tp->next = NULL;
}
}
- strncpy(tp->name, name, MAXNAMELEN);
- tp->name[MAXNAMELEN-1] = '\0';
+ g_strlcpy(tp->name, name, MAXNAMELEN);
if (new_one) {
tp->addr = *addrp;
tp->next = NULL;
} /* add_ipv6_name */
+/* -----------------
+ * unsigned integer to ascii
+*/
+static gchar *ep_utoa(guint port)
+{
+ gchar *bp = ep_alloc(MAXNAMELEN);
+
+ bp = &bp[MAXNAMELEN -1];
+
+ *bp = 0;
+ do {
+ *--bp = (port % 10) +'0';
+ } while ((port /= 10) != 0);
+ return bp;
+}
+
+
extern gchar *get_udp_port(guint port)
{
- gchar *cur;
if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
- cur=ep_alloc(MAXNAMELEN);
- g_snprintf(cur, MAXNAMELEN, "%u", port);
- return cur;
+ return ep_utoa(port);
}
return serv_name_lookup(port, PT_UDP);
extern gchar *get_dccp_port(guint port)
{
- gchar *cur;
if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
- cur=ep_alloc(MAXNAMELEN);
- g_snprintf(cur, MAXNAMELEN, "%u", port);
- return cur;
+ return ep_utoa(port);
}
return serv_name_lookup(port, PT_DCCP);
extern gchar *get_tcp_port(guint port)
{
- gchar *cur;
if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
- cur=ep_alloc(MAXNAMELEN);
- g_snprintf(cur, MAXNAMELEN, "%u", port);
- return cur;
+ return ep_utoa(port);
}
return serv_name_lookup(port, PT_TCP);
extern gchar *get_sctp_port(guint port)
{
- gchar *cur;
if (!(g_resolv_flags & RESOLV_TRANSPORT)) {
- cur=ep_alloc(MAXNAMELEN);
- g_snprintf(cur, MAXNAMELEN, "%u", port);
- return cur;
+ return ep_utoa(port);
}
return serv_name_lookup(port, PT_SCTP);
void get_addr_name_buf(address *addr, gchar *buf, guint size)
{
- const gchar *result;
-
- result = get_addr_name(addr);
-
- strncpy(buf,result,size);
- buf[size]='\0';
- return;
+ const gchar *result = get_addr_name(addr);
+ g_snprintf(buf, size, "%s", result);
} /* get_addr_name_buf */