Added a more fine grained locking system for multiple threads.

Removed the new_dnscache() function and moved it into dnscache().
Fixed a memory leak in dns_insert().
This commit is contained in:
Robert James Kaes 2001-08-29 03:57:51 +00:00
parent f864ff281f
commit c9a772e36b

View File

@ -1,4 +1,4 @@
/* $Id: dnscache.c,v 1.9 2001-05-27 02:24:00 rjkaes Exp $ /* $Id: dnscache.c,v 1.10 2001-08-29 03:57:51 rjkaes Exp $
* *
* This is a caching DNS system. When a host name is needed we look it up here * This is a caching DNS system. When a host name is needed we look it up here
* and see if there is already an answer for it. The domains are placed in a * and see if there is already an answer for it. The domains are placed in a
@ -34,6 +34,14 @@
#include "ternary.h" #include "ternary.h"
#include "utils.h" #include "utils.h"
/*
* The mutex is used for locking around accesses to the ternary tree.
*/
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() pthread_mutex_lock(&mutex);
#define UNLOCK() pthread_mutex_unlock(&mutex);
#define DNSEXPIRE (5 * 60) #define DNSEXPIRE (5 * 60)
struct dnscache_s { struct dnscache_s {
@ -41,49 +49,94 @@ struct dnscache_s {
time_t expire; time_t expire;
}; };
static TERNARY dns_tree; static TERNARY dns_tree = -1;
TERNARY new_dnscache(void) /*
* Insert the data into the DNS tree.
*/
static int insert_data(char *domain, struct dnscache_s *newptr)
{ {
dns_tree = ternary_new(); int ret;
return dns_tree; LOCK();
ret = ternary_insert(dns_tree, domain, newptr);
UNLOCK();
return ret;
} }
static int dns_lookup(struct in_addr *addr, char *domain) static int dns_lookup(struct in_addr *addr, char *domain)
{ {
int ret;
struct dnscache_s *ptr; struct dnscache_s *ptr;
assert(addr != NULL); assert(addr != NULL);
assert(domain != NULL); assert(domain != NULL);
if (TE_ISERROR(ternary_search(dns_tree, domain, (void *)&ptr))) LOCK();
return -1; ret = ternary_search(dns_tree, domain, (void *)&ptr);
if (difftime(time(NULL), ptr->expire) > (double)DNSEXPIRE) { if (TE_ISERROR(ret)
|| difftime(time(NULL), ptr->expire) > DNSEXPIRE) {
UNLOCK();
return -1; return -1;
} }
*addr = ptr->ipaddr; memcpy(addr, &ptr->ipaddr, sizeof(struct in_addr));
UNLOCK();
return 0; return 0;
} }
static int dns_insert(struct in_addr *addr, char *domain) static int dns_insert(struct in_addr *addr, char *domain)
{ {
struct dnscache_s *newptr; struct dnscache_s *newptr;
int ret;
assert(addr != NULL); assert(addr != NULL);
assert(domain != NULL); assert(domain != NULL);
DEBUG2("Inserting [%s] into DNS cache", domain);
if (!(newptr = malloc(sizeof(struct dnscache_s)))) { if (!(newptr = malloc(sizeof(struct dnscache_s)))) {
return -1; return -1;
} }
newptr->ipaddr = *addr; memcpy(&newptr->ipaddr, addr, sizeof(struct in_addr));
newptr->expire = time(NULL); newptr->expire = time(NULL);
if (TE_ISERROR(ternary_insert(dns_tree, domain, newptr))) ret = insert_data(domain, newptr);
safefree(newptr);
if (TE_ISERROR(ret)) {
if (ret == TE_EXISTS) {
/*
* The value already exists. First search for the
* value and then delete the data before inserting
* the new value.
*/
struct dnscache_s *existing;
DEBUG2("[%s] already exists in DNS cache", domain);
LOCK();
ret = ternary_search(dns_tree, domain, (void *)&existing);
UNLOCK();
if (TE_ISERROR(ret))
goto INSERT_ERROR;
safefree(existing);
ret = insert_data(domain, newptr);
if (TE_ISERROR(ret))
goto INSERT_ERROR;
} else {
INSERT_ERROR:
safefree(newptr);
return -1;
}
}
return 0; return 0;
} }
@ -95,7 +148,13 @@ int dnscache(struct in_addr *addr, char *domain)
assert(addr != NULL); assert(addr != NULL);
assert(domain != NULL); assert(domain != NULL);
if (inet_aton(domain, (struct in_addr *) addr) != 0) /* If the DNS tree doesn't exist, build a new one */
LOCK();
if (dns_tree < 0)
dns_tree = ternary_new();
UNLOCK();
if (inet_aton(domain, (struct in_addr *)addr) != 0)
return 0; return 0;
/* Well, we're not dotted-decimal so we need to look it up */ /* Well, we're not dotted-decimal so we need to look it up */
@ -103,10 +162,15 @@ int dnscache(struct in_addr *addr, char *domain)
return 0; return 0;
/* Okay, so not in the list... need to actually look it up. */ /* Okay, so not in the list... need to actually look it up. */
if (!(resolv = gethostbyname(domain))) LOCK();
if (!(resolv = gethostbyname(domain))) {
UNLOCK();
return -1; return -1;
}
memcpy(addr, resolv->h_addr_list[0], (size_t)resolv->h_length); memcpy(addr, resolv->h_addr_list[0], (size_t)resolv->h_length);
UNLOCK();
dns_insert(addr, domain); dns_insert(addr, domain);
return 0; return 0;