resolved-dns-cache.c revision d2f47562d5d834339ef3030e345a76a8c6f09c74
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "resolved-dns-cache.h"
#include "resolved-dns-packet.h"
/* Never cache more than 1K entries */
#define CACHE_MAX 1024
/* We never keep any item longer than 10min in our cache */
typedef enum DnsCacheItemType DnsCacheItemType;
typedef struct DnsCacheItem DnsCacheItem;
enum DnsCacheItemType {
};
struct DnsCacheItem {
unsigned prioq_idx;
};
static void dns_cache_item_free(DnsCacheItem *i) {
if (!i)
return;
free(i);
}
assert(c);
if (!i)
return;
if (first)
else
}
void dns_cache_flush(DnsCache *c) {
DnsCacheItem *i;
assert(c);
while ((i = hashmap_first(c->by_key)))
hashmap_free(c->by_key);
prioq_free(c->by_expiry);
}
DnsCacheItem *i;
assert(c);
}
assert(c);
if (add <= 0)
return;
/* Makes space for n new entries. Note that we actually allow
* the cache to grow beyond CACHE_MAX, but only when we shall
* add more RRs to the cache than CACHE_MAX at once. In that
* case the cache will be emptied completely otherwise. */
for (;;) {
DnsCacheItem *i;
if (prioq_size(c->by_expiry) <= 0)
break;
break;
i = prioq_peek(c->by_expiry);
assert(i);
/* Take an extra reference to the key so that it
* doesn't go away in the middle of the remove call */
dns_cache_remove(c, key);
}
}
void dns_cache_prune(DnsCache *c) {
usec_t t = 0;
assert(c);
/* Remove all entries that are past their TTL */
for (;;) {
DnsCacheItem *i;
i = prioq_peek(c->by_expiry);
if (!i)
break;
if (t <= 0)
t = now(CLOCK_MONOTONIC);
if (i->until > t)
break;
/* Take an extra reference to the key so that it
* doesn't go away in the middle of the remove call */
dns_cache_remove(c, key);
}
}
static int dns_cache_item_prioq_compare_func(const void *a, const void *b) {
const DnsCacheItem *x = a, *y = b;
return -1;
return 1;
return 0;
}
static int dns_cache_init(DnsCache *c) {
int r;
assert(c);
if (r < 0)
return r;
r = hashmap_ensure_allocated(&c->by_key, dns_resource_key_hash_func, dns_resource_key_compare_func);
if (r < 0)
return r;
return r;
}
int r;
assert(c);
assert(i);
if (r < 0)
return r;
if (first) {
} else {
if (r < 0) {
return r;
}
}
return 0;
}
DnsCacheItem *i;
assert(c);
return i;
return NULL;
}
static void dns_cache_item_update_positive(DnsCache *c, DnsCacheItem *i, DnsResourceRecord *rr, usec_t timestamp) {
assert(c);
assert(i);
i->type = DNS_CACHE_POSITIVE;
if (!i->by_key_prev) {
/* We are the first item in the list, we need to
* update the key used in the hashmap */
}
}
int r;
assert(c);
/* New TTL is 0? Delete the entry... */
return 0;
}
/* Entry exists already? Update TTL and timestamp */
if (existing) {
return 0;
}
/* Otherwise, add the new RR */
r = dns_cache_init(c);
if (r < 0)
return r;
dns_cache_make_space(c, 1);
if (!i)
return -ENOMEM;
i->type = DNS_CACHE_POSITIVE;
i->prioq_idx = PRIOQ_IDX_NULL;
r = dns_cache_link_item(c, i);
if (r < 0)
return r;
i = NULL;
return 0;
}
static int dns_cache_put_negative(DnsCache *c, DnsResourceKey *key, int rcode, usec_t timestamp, uint32_t soa_ttl) {
int r;
assert(c);
dns_cache_remove(c, key);
return 0;
r = dns_cache_init(c);
if (r < 0)
return r;
dns_cache_make_space(c, 1);
if (!i)
return -ENOMEM;
i->prioq_idx = PRIOQ_IDX_NULL;
r = dns_cache_link_item(c, i);
if (r < 0)
return r;
i = NULL;
return 0;
}
int dns_cache_put(DnsCache *c, DnsQuestion *q, int rcode, DnsAnswer *answer, unsigned max_rrs, usec_t timestamp) {
unsigned i;
int r;
assert(c);
/* First, delete all matching old RRs, so that we only keep
* complete by_key in place. */
for (i = 0; i < q->n_keys; i++)
dns_cache_remove(c, q->keys[i]);
/* We only care for positive replies and NXDOMAINs, on all
* other replies we will simply flush the respective entries,
* and that's it */
return 0;
/* Make some space for our new entries */
if (timestamp <= 0)
/* Second, add in positive entries for all contained RRs */
if (r < 0)
goto fail;
}
/* Third, add in negative entries for all keys with no RR */
for (i = 0; i < q->n_keys; i++) {
if (r < 0)
goto fail;
if (r > 0)
continue;
if (r < 0)
goto fail;
if (r == 0)
continue;
if (r < 0)
goto fail;
}
return 0;
fail:
/* Adding all RRs failed. Let's clean up what we already
* added, just in case */
for (i = 0; i < q->n_keys; i++)
dns_cache_remove(c, q->keys[i]);
return r;
}
unsigned i, n = 0;
int r;
bool nxdomain = false;
assert(c);
assert(q);
if (q->n_keys <= 0) {
*rcode = 0;
return 0;
}
for (i = 0; i < q->n_keys; i++) {
DnsCacheItem *j;
if (!j) {
/* If one question cannot be answered we need to refresh */
*rcode = 0;
return 0;
}
LIST_FOREACH(by_key, j, j) {
if (j->rr)
n++;
else if (j->type == DNS_CACHE_NXDOMAIN)
nxdomain = true;
}
}
if (n <= 0) {
return 1;
}
answer = dns_answer_new(n);
if (!answer)
return -ENOMEM;
for (i = 0; i < q->n_keys; i++) {
DnsCacheItem *j;
LIST_FOREACH(by_key, j, j) {
if (j->rr) {
if (r < 0)
return r;
}
}
}
return n;
}