summaryrefslogtreecommitdiff
path: root/lib/hostip.c
diff options
context:
space:
mode:
authorSterling Hughes <sterling@bumblebury.com>2002-01-08 04:26:47 +0000
committerSterling Hughes <sterling@bumblebury.com>2002-01-08 04:26:47 +0000
commit22ac08e06db32cb2a7872316a669eb81ec3ea204 (patch)
tree1afa025886527ee24580723104e03357931cc786 /lib/hostip.c
parent87037136efd4d5eeefb7456c2ad14740a956e47d (diff)
downloadcurl-22ac08e06db32cb2a7872316a669eb81ec3ea204.tar.gz
Add support for DNS cache timeouts via the CURLOPT_DNS_CACHE_TIMEOUT option.
The default cache timeout for this is 60 seconds, which is arbitrary and completely subject to change :)
Diffstat (limited to 'lib/hostip.c')
-rw-r--r--lib/hostip.c61
1 files changed, 50 insertions, 11 deletions
diff --git a/lib/hostip.c b/lib/hostip.c
index 9d0013ffe..fc76242f7 100644
--- a/lib/hostip.c
+++ b/lib/hostip.c
@@ -93,24 +93,59 @@ void Curl_global_host_cache_dtor(void)
}
}
+struct curl_dns_cache_entry {
+ Curl_addrinfo *addr;
+ int timestamp;
+};
+
Curl_addrinfo *Curl_resolv(struct SessionHandle *data,
char *hostname,
int port,
char **bufp)
{
- Curl_addrinfo *addr = NULL;
- size_t hostname_len = strlen(hostname)+1;
+ struct curl_dns_cache_entry *p = NULL;
+ size_t hostname_len;
+ time_t now;
+
+ /* If the host cache timeout is 0, we don't do DNS cach'ing
+ so fall through */
+ if (data->set.dns_cache_timeout == 0) {
+ return Curl_getaddrinfo(data, hostname, port, bufp);
+ }
+
+ hostname_len = strlen(hostname)+1;
+
+ time(&now);
+ /* See if its already in our dns cache */
+ if (curl_hash_find(data->hostcache, hostname, hostname_len, (void **) &p)) {
+ /* Do we need to check for a cache timeout? */
+ if (data->set.dns_cache_timeout != -1) {
+ /* Return if the entry has not timed out */
+ if ((now - p->timestamp) < data->set.dns_cache_timeout) {
+ return p->addr;
+ }
+ }
+ else {
+ return p->addr;
+ }
+ }
- if (curl_hash_find(data->hostcache, hostname, hostname_len, (void **) &addr)) {
- return addr;
+ /* Create a new cache entry */
+ p = (struct curl_dns_cache_entry *) malloc(sizeof(struct curl_dns_cache_entry));
+ if (!p) {
+ return NULL;
}
-
- addr = Curl_getaddrinfo(data, hostname, port, bufp);
- if (!addr)
+
+ p->addr = Curl_getaddrinfo(data, hostname, port, bufp);
+ if (!p->addr) {
return NULL;
+ }
+ p->timestamp = now;
- curl_hash_add(data->hostcache, hostname, hostname_len, (const void *) addr);
- return addr;
+ /* Save it in our host cache */
+ curl_hash_update(data->hostcache, hostname, hostname_len, (const void *) p);
+
+ return p->addr;
}
/*
@@ -120,11 +155,15 @@ Curl_addrinfo *Curl_resolv(struct SessionHandle *data,
*/
void Curl_freeaddrinfo(void *freethis)
{
+ struct curl_dns_cache_entry *p = (struct curl_dns_cache_entry *) freethis;
+
#ifdef ENABLE_IPV6
- freeaddrinfo(freethis);
+ freeaddrinfo(p->addr);
#else
- free(freethis);
+ free(p->addr);
#endif
+
+ free(p);
}
/* --- resolve name or IP-number --- */