summaryrefslogtreecommitdiff
path: root/cachedb
diff options
context:
space:
mode:
Diffstat (limited to 'cachedb')
-rw-r--r--cachedb/cachedb.c44
1 files changed, 31 insertions, 13 deletions
diff --git a/cachedb/cachedb.c b/cachedb/cachedb.c
index 9a63101edee38..f5f6937eeeedf 100644
--- a/cachedb/cachedb.c
+++ b/cachedb/cachedb.c
@@ -61,6 +61,8 @@
/** the unit test testframe for cachedb, its module state contains
* a cache for a couple queries (in memory). */
struct testframe_moddata {
+ /** lock for mutex */
+ lock_basic_type lock;
/** key for single stored data element, NULL if none */
char* stored_key;
/** data for single stored data element, NULL if none */
@@ -72,14 +74,18 @@ struct testframe_moddata {
static int
testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
{
+ struct testframe_moddata* d;
(void)env;
verbose(VERB_ALGO, "testframe_init");
- cachedb_env->backend_data = (void*)calloc(1,
+ d = (struct testframe_moddata*)calloc(1,
sizeof(struct testframe_moddata));
+ cachedb_env->backend_data = (void*)d;
if(!cachedb_env->backend_data) {
log_err("out of memory");
return 0;
}
+ lock_basic_init(&d->lock);
+ lock_protect(&d->lock, d, sizeof(*d));
return 1;
}
@@ -92,6 +98,7 @@ testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
verbose(VERB_ALGO, "testframe_deinit");
if(!d)
return;
+ lock_basic_destroy(&d->lock);
free(d->stored_key);
free(d->stored_data);
free(d);
@@ -105,17 +112,22 @@ testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
cachedb_env->backend_data;
(void)env;
verbose(VERB_ALGO, "testframe_lookup of %s", key);
+ lock_basic_lock(&d->lock);
if(d->stored_key && strcmp(d->stored_key, key) == 0) {
- if(d->stored_datalen > sldns_buffer_capacity(result_buffer))
+ if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
+ lock_basic_unlock(&d->lock);
return 0; /* too large */
+ }
verbose(VERB_ALGO, "testframe_lookup found %d bytes",
(int)d->stored_datalen);
sldns_buffer_clear(result_buffer);
sldns_buffer_write(result_buffer, d->stored_data,
d->stored_datalen);
sldns_buffer_flip(result_buffer);
+ lock_basic_unlock(&d->lock);
return 1;
}
+ lock_basic_unlock(&d->lock);
return 0;
}
@@ -126,6 +138,7 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
struct testframe_moddata* d = (struct testframe_moddata*)
cachedb_env->backend_data;
(void)env;
+ lock_basic_lock(&d->lock);
verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
/* free old data element (if any) */
@@ -137,6 +150,7 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
d->stored_data = memdup(data, data_len);
if(!d->stored_data) {
+ lock_basic_unlock(&d->lock);
log_err("out of memory");
return;
}
@@ -146,8 +160,10 @@ testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
free(d->stored_data);
d->stored_data = NULL;
d->stored_datalen = 0;
+ lock_basic_unlock(&d->lock);
return;
}
+ lock_basic_unlock(&d->lock);
/* (key,data) successfully stored */
}
@@ -170,16 +186,17 @@ cachedb_find_backend(const char* str)
static int
cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
{
- const char* backend_str = "testframe"; /* TODO get from cfg */
- (void)cfg; /* need this until the TODO is implemented */
- if(backend_str && backend_str[0]) {
- cachedb_env->backend = cachedb_find_backend(backend_str);
- if(!cachedb_env->backend) {
- log_err("cachedb: cannot find backend name '%s",
- backend_str);
- return 0;
- }
+ const char* backend_str = cfg->cachedb_backend;
+
+ /* If unspecified we use the in-memory test DB. */
+ if(!backend_str)
+ backend_str = "testframe";
+ cachedb_env->backend = cachedb_find_backend(backend_str);
+ if(!cachedb_env->backend) {
+ log_err("cachedb: cannot find backend name '%s'", backend_str);
+ return 0;
}
+
/* TODO see if more configuration needs to be applied or not */
return 1;
}
@@ -277,9 +294,10 @@ calc_hash(struct module_qstate* qstate, char* buf, size_t len)
size_t clen = 0;
uint8_t hash[CACHEDB_HASHSIZE/8];
const char* hex = "0123456789ABCDEF";
- const char* secret = "default"; /* TODO: from qstate->env->cfg */
+ const char* secret = qstate->env->cfg->cachedb_secret ?
+ qstate->env->cfg->cachedb_secret : "default";
size_t i;
-
+
/* copy the hash info into the clear buffer */
if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
memmove(clear+clen, qstate->qinfo.qname,