resolved-dns-cache.c revision 322345fdb9865ef2477fba8e4bdde0e1183ef505
/*-*- 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"
#define CACHE_MAX 1024
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->rrsets)))
hashmap_free(c->rrsets);
prioq_free(c->expire);
}
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->expire) <= 0)
break;
break;
i = prioq_peek(c->expire);
}
}
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->expire);
if (!i)
break;
if (ttl > CACHE_TTL_MAX_USEC)
if (t <= 0)
t = now(CLOCK_MONOTONIC);
break;
}
}
static int dns_cache_item_prioq_compare_func(const void *a, const void *b) {
usec_t t, z;
const DnsCacheItem *x = a, *y = b;
if (t < z)
return -1;
if (t > z)
return 1;
return 0;
}
static void dns_cache_item_update(DnsCache *c, DnsCacheItem *i, DnsResourceRecord *rr, usec_t timestamp) {
assert(c);
assert(i);
if (!i->rrsets_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 */
if (r < 0)
return r;
r = hashmap_ensure_allocated(&c->rrsets, dns_resource_key_hash_func, dns_resource_key_compare_func);
if (r < 0)
return r;
dns_cache_make_space(c, 1);
if (!i)
return -ENOMEM;
if (r < 0)
return r;
if (first) {
} else {
if (r < 0) {
return r;
}
}
i = NULL;
return 0;
}
unsigned i, added = 0;
int r;
assert(c);
if (n_rrs <= 0)
return 0;
/* First iteration, delete all matching old RRs, so that we
* only keep complete rrsets in place. */
for (i = 0; i < n_rrs; i++)
/* Second iteration, add in new RRs */
if (timestamp <= 0)
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 < added; i++)
return r;
}
assert(c);
}
DnsCacheItem *i;
assert(c);
return i;
return NULL;
}
int dns_cache_lookup_many(DnsCache *c, DnsResourceKey *keys, unsigned n_keys, DnsResourceRecord ***rrs) {
DnsResourceRecord **p = NULL;
unsigned i;
int r;
assert(c);
if (n_keys <= 0) {
return 0;
}
for (i = 0; i < n_keys; i++) {
DnsCacheItem *j;
j = dns_cache_lookup(c, &keys[i]);
if (!j) {
r = 0;
goto fail;
}
LIST_FOREACH(rrsets, j, j) {
r = -ENOMEM;
goto fail;
}
}
}
*rrs = p;
return (int) used;
fail:
return r;
}