#include "util/data/packed_rrset.h"
#include "util/data/msgreply.h"
#include "util/region-allocator.h"
+#include "util/alloc.h"
struct rrset_cache* rrset_cache_create(struct config_file* cfg,
struct alloc_cache* alloc)
/** see if rrset needs to be updated in the cache */
static int
-need_to_update_rrset(void* nd, void* cd, uint32_t timenow)
+need_to_update_rrset(void* nd, void* cd, uint32_t timenow, int equal)
{
struct packed_rrset_data* newd = (struct packed_rrset_data*)nd;
struct packed_rrset_data* cached = (struct packed_rrset_data*)cd;
if( cached->ttl < timenow )
return 1;
/* o same trust, but different in data - insert it */
- if( newd->trust == cached->trust &&
- !rrsetdata_equal(newd, cached))
+ if( newd->trust == cached->trust && !equal )
return 1;
/* o see if TTL is better than TTL in cache. */
/* if so, see if rrset+rdata is the same */
/* if so, update TTL in cache, even if trust is worse. */
- if( newd->ttl > cached->ttl &&
- rrsetdata_equal(newd, cached)) {
+ if( newd->ttl > cached->ttl && equal ) {
/* since all else is the same, use the best trust value */
- if(newd->trust < cached->trust)
+ if(newd->trust < cached->trust) {
newd->trust = cached->trust;
+ newd->security = cached->security;
+ }
return 1;
}
return 0;
}
+/** Update RRSet special key ID */
+static void
+rrset_update_id(struct rrset_ref* ref, struct alloc_cache* alloc)
+{
+ /* this may clear the cache and invalidate lock below */
+ uint64_t newid = alloc_get_id(alloc);
+ /* obtain writelock */
+ lock_rw_wrlock(&ref->key->entry.lock);
+ /* check if it was deleted in the meantime, if so, skip update */
+ if(ref->key->id == ref->id) {
+ ref->key->id = newid;
+ ref->id = newid;
+ }
+ lock_rw_unlock(&ref->key->entry.lock);
+}
+
int
rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref,
struct alloc_cache* alloc, uint32_t timenow)
struct lruhash_entry* e;
struct ub_packed_rrset_key* k = ref->key;
hashvalue_t h = k->entry.hash;
+ uint16_t rrset_type = ntohs(k->rk.type);
+ int equal = 0;
/* looks up item with a readlock - no editing! */
if((e=slabhash_lookup(&r->table, h, k, 0)) != 0) {
/* return id and key as they will be used in the cache
*/
ref->key = (struct ub_packed_rrset_key*)e->key;
ref->id = ref->key->id;
- if(!need_to_update_rrset(k->entry.data, e->data, timenow)) {
+ equal = rrsetdata_equal((struct packed_rrset_data*)k->entry.
+ data, (struct packed_rrset_data*)e->data);
+ if(!need_to_update_rrset(k->entry.data, e->data, timenow,
+ equal)) {
/* cache is superior, return that value */
lock_rw_unlock(&e->lock);
ub_packed_rrset_parsedelete(k, alloc);
* cache size values nicely. */
}
slabhash_insert(&r->table, h, &k->entry, k->entry.data, alloc);
- if(e)
+ if(e) {
+ /* For NSEC, NSEC3, DNAME, when rdata is updated, update
+ * the ID number so that proofs in message cache are
+ * invalidated */
+ if((rrset_type == LDNS_RR_TYPE_NSEC
+ || rrset_type == LDNS_RR_TYPE_NSEC3
+ || rrset_type == LDNS_RR_TYPE_DNAME) && !equal) {
+ rrset_update_id(ref, alloc);
+ }
return 1;
+ }
return 0;
}
alloc->num_quar = 0;
}
-/** get a new id */
-static void
-alloc_get_id(struct alloc_cache* alloc, alloc_special_t* t)
+uint64_t
+alloc_get_id(struct alloc_cache* alloc)
{
- t->id = alloc->next_id++;
- if(alloc->next_id == alloc->last_id) {
+ uint64_t id = alloc->next_id++;
+ if(id == alloc->last_id) {
/* TODO: clear the rrset cache */
log_warn("Out of ids. Clearing cache.");
+ /* start back at first number */ /* like in alloc_init*/
+ alloc->next_id = (uint64_t)alloc->thread_num;
+ alloc->next_id <<= THRNUM_SHIFT; /* in steps for comp. */
+ alloc->next_id += 1; /* portability. */
+ /* and generate new and safe id */
+ id = alloc->next_id++;
}
- alloc_set_special_next(t, 0);
+ return id;
}
alloc_special_t*
p = alloc->quar;
alloc->quar = alloc_special_next(p);
alloc->num_quar--;
- alloc_get_id(alloc, p);
+ p->id = alloc_get_id(alloc);
return p;
}
/* see if in global cache */
}
lock_quick_unlock(&alloc->super->lock);
if(p) {
- alloc_get_id(alloc, p);
+ p->id = alloc_get_id(alloc);
return p;
}
}
if(!(p = (alloc_special_t*)malloc(sizeof(alloc_special_t))))
fatal_exit("alloc_special_obtain: out of memory");
alloc_setup_special(p);
- alloc_get_id(alloc, p);
+ p->id = alloc_get_id(alloc);
return p;
}