2000-09-26 12:59:48 +08:00
|
|
|
/* $Id: dnscache.c,v 1.6 2000-09-26 04:59:48 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* hashed linked list. If the name is not here, then we need to look it up and
|
|
|
|
* add it to the system. This really speeds up the connection to servers since
|
|
|
|
* the DNS name does not need to be looked up each time. It's kind of cool. :)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2000-09-12 07:42:43 +08:00
|
|
|
# include <config.h>
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif
|
|
|
|
|
2000-04-27 00:31:29 +08:00
|
|
|
#include <sys/types.h>
|
2000-02-17 01:32:49 +08:00
|
|
|
#include <ctype.h>
|
2000-09-12 07:42:43 +08:00
|
|
|
#include <unistd.h>
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
#include "dnscache.h"
|
2000-09-12 07:42:43 +08:00
|
|
|
#include "log.h"
|
|
|
|
#include "ternary.h"
|
|
|
|
#include "utils.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
#define DNSEXPIRE (5 * 60)
|
2000-09-12 07:42:43 +08:00
|
|
|
#define DNS_GARBAGE_COL 10
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
struct dnscache_s {
|
|
|
|
struct in_addr ipaddr;
|
|
|
|
time_t expire;
|
|
|
|
};
|
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
static TERNARY dns_tree;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
static int dns_lookup(struct in_addr *addr, char *domain)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:42:43 +08:00
|
|
|
struct dnscache_s *ptr;
|
|
|
|
|
|
|
|
if (dns_tree == 0) {
|
|
|
|
if (TE_ISERROR(dns_tree = ternary_new()))
|
|
|
|
return dns_tree;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
if (TE_ISERROR(ternary_search(dns_tree, domain, (void *)&ptr)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (difftime(time(NULL), ptr->expire) > DNSEXPIRE) {
|
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
*addr = ptr->ipaddr;
|
|
|
|
return 0;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
static int dns_insert(struct in_addr *addr, char *domain)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
struct dnscache_s *newptr;
|
|
|
|
|
2000-09-12 07:42:43 +08:00
|
|
|
if (!(newptr = malloc(sizeof(struct dnscache_s)))) {
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
newptr->ipaddr = *addr;
|
|
|
|
newptr->expire = time(NULL);
|
|
|
|
|
2000-09-26 12:59:48 +08:00
|
|
|
if (TE_ISERROR(ternary_insert(dns_tree, domain, newptr)))
|
|
|
|
safefree(newptr);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dnscache(struct in_addr *addr, char *domain)
|
|
|
|
{
|
2000-09-12 07:42:43 +08:00
|
|
|
static unsigned int dns_garbage_collect = 0;
|
2000-02-17 01:32:49 +08:00
|
|
|
struct hostent *resolv;
|
2000-09-12 07:42:43 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (++dns_garbage_collect > DNS_GARBAGE_COL) {
|
|
|
|
ternary_destroy(&dns_root, &free);
|
|
|
|
DEBUG1("Doing garbage collection.");
|
|
|
|
dns_garbage_collect = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
if (inet_aton(domain, (struct in_addr *) addr) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Well, we're not dotted-decimal so we need to look it up */
|
2000-09-12 07:42:43 +08:00
|
|
|
if (dns_lookup(addr, domain) == 0)
|
2000-02-17 01:32:49 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Okay, so not in the list... need to actually look it up. */
|
|
|
|
if (!(resolv = gethostbyname(domain)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(addr, resolv->h_addr_list[0], resolv->h_length);
|
2000-09-12 07:42:43 +08:00
|
|
|
dns_insert(addr, domain);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|