Commit a1846a76 authored by Marek Vavruša's avatar Marek Vavruša

lib: demoted context to simple interface, not responsible for data

parent 052dcc74
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <limits.h>
#include <libknot/errcode.h>
#include <libknot/internal/sockaddr.h>
#include <libknot/internal/mem.h>
#include "lib/context.h"
#include "lib/defines.h"
#include "lib/rplan.h"
int kr_context_init(struct kr_context *ctx, mm_ctx_t *mm)
{
if (ctx == NULL) {
return KNOT_EINVAL;
}
memset(ctx, 0, sizeof(struct kr_context));
ctx->pool = mm;
return KNOT_EOK;
}
int kr_context_deinit(struct kr_context *ctx)
{
if (ctx == NULL) {
return KNOT_EINVAL;
}
if (ctx->cache) {
kr_cache_close(ctx->cache);
}
for (size_t i = 0; i < ctx->mod_loaded; ++i) {
kr_module_unload(&ctx->modules[i]);
}
return KNOT_EOK;
}
int kr_context_register(struct kr_context *ctx, const char *module_name)
{
size_t last = ctx->mod_loaded;
int ret = mreserve((char **) &ctx->modules, sizeof(struct kr_module),
last + 1, 0, &ctx->mod_reserved);
if (ret < 0) {
return kr_error(ENOMEM);
}
struct kr_module *mod = &ctx->modules[last];
ret = kr_module_load(mod, module_name, NULL);
if (ret != 0) {
return ret;
}
ctx->mod_loaded += 1;
return kr_ok();
}
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libknot/internal/mempattern.h>
#include <libknot/internal/lists.h>
#include "lib/module.h"
#include "lib/cache.h"
/** \addtogroup resolution
* @{
*/
/**
* Name resolution context.
*
* Resolution context provides basic services like cache, configuration and options.
*
* @note This structure is persistent between name resolutions and may
* be shared between threads.
*/
struct kr_context
{
mm_ctx_t *pool;
struct kr_cache *cache;
struct kr_module *modules;
size_t mod_loaded;
size_t mod_reserved;
uint32_t options;
};
/**
* Initialize query resolution context.
* @param ctx context to be initialized
* @param mm memory context
* @return KNOT_E*
*/
int kr_context_init(struct kr_context *ctx, mm_ctx_t *mm);
/**
* Deinitialize query resolution context.
* @param ctx context to be deinitialized
* @return KNOT_E*
*/
int kr_context_deinit(struct kr_context *ctx);
/**
* Register module to context.
* @param ctx context
* @param module_name
* @return KNOT_E*
*/
int kr_context_register(struct kr_context *ctx, const char *module_name);
/** @} */
......@@ -46,8 +46,7 @@
* Defines.
*/
#define KR_DNS_PORT 53
#define KR_DNAME_ROOT ((const knot_dname_t*)"")
#define KR_EDNS_VERSION 0
#define KR_EDNS_PAYLOAD 4096
#define KR_EDNS_PAYLOAD 4096
/** @} */
......@@ -23,7 +23,7 @@
#include <libknot/processing/layer.h>
#include <libknot/packet/pkt.h>
#include "lib/context.h"
#include "lib/resolve.h"
#include "lib/rplan.h"
/**
......
......@@ -434,7 +434,9 @@ static int resolve(knot_layer_t *ctx, knot_pkt_t *pkt)
}
/* Check response code. */
#ifndef NDEBUG
lookup_table_t *rcode = lookup_by_id(knot_rcode_names, knot_wire_get_rcode(pkt->wire));
#endif
switch(knot_wire_get_rcode(pkt->wire)) {
case KNOT_RCODE_NOERROR:
case KNOT_RCODE_NXDOMAIN:
......
......@@ -4,7 +4,6 @@ libkresolve_SOURCES := \
lib/utils.c \
lib/nsrep.c \
lib/module.c \
lib/context.c \
lib/resolve.c \
lib/zonecut.c \
lib/rplan.c \
......@@ -15,7 +14,6 @@ libkresolve_HEADERS := \
lib/utils.h \
lib/nsrep.h \
lib/module.h \
lib/context.h \
lib/resolve.h \
lib/zonecut.h \
lib/rplan.h \
......
......@@ -27,7 +27,6 @@
/*
* Forward decls
*/
struct kr_context;
struct kr_module;
struct kr_prop;
......@@ -48,7 +47,7 @@ typedef struct kr_prop *(module_prop_cb)(void);
* A module property has a free-form JSON output (and optional input).
*/
struct kr_prop {
char *(*cb)(struct kr_context*,struct kr_module *,const char*);
char *(*cb)(void *, struct kr_module *, const char *);
const char *name;
const char *info;
};
......
......@@ -148,8 +148,8 @@ static int iterate(struct knot_requestor *requestor, struct kr_layer_param *para
static void prepare_layers(struct knot_requestor *req, struct kr_layer_param *param)
{
struct kr_context *ctx = param->ctx;
for (size_t i = 0; i < ctx->mod_loaded; ++i) {
struct kr_module *mod = &ctx->modules[i];
for (size_t i = 0; i < ctx->modules->len; ++i) {
struct kr_module *mod = &ctx->modules->at[i];
if (mod->layer) {
knot_requestor_overlay(req, mod->layer(), param);
}
......
......@@ -21,7 +21,28 @@
#pragma once
#include <libknot/packet/pkt.h>
#include "context.h"
#include "lib/generic/array.h"
#include "lib/module.h"
/** Array of modules. */
typedef array_t(struct kr_module) modulelist_t;
/**
* Name resolution context.
*
* Resolution context provides basic services like cache, configuration and options.
*
* @note This structure is persistent between name resolutions and may
* be shared between threads.
*/
struct kr_context
{
mm_ctx_t *pool;
struct kr_cache *cache;
modulelist_t *modules;
uint32_t options;
};
/**
* Resolve an input query and produce a packet with an answer.
......
......@@ -19,7 +19,6 @@
#include <libknot/errcode.h>
#include "lib/rplan.h"
#include "lib/context.h"
#include "lib/cache.h"
#include "lib/defines.h"
#include "lib/layer.h"
......
......@@ -27,7 +27,7 @@
#include <libknot/internal/namedb/namedb.h>
#include <libknot/internal/sockaddr.h>
#include "lib/context.h"
#include "lib/cache.h"
#include "lib/zonecut.h"
/* Query flags */
......
......@@ -35,7 +35,6 @@ extern void _cleanup_fclose(FILE **p);
/*
* Defines.
*/
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x)))
/** Concatenate N strings. */
char* kr_strcatdup(unsigned n, ...);
......
......@@ -60,7 +60,7 @@ int kr_init_zone_cut(struct kr_zonecut *cut)
const unsigned hint_id = dnssec_random_uint16_t() % HINT_COUNT;
const struct hint_info *hint = &SBELT[hint_id];
kr_set_zone_cut(cut, KR_DNAME_ROOT, hint->name);
kr_set_zone_cut(cut, U8(""), hint->name);
/* Prefetch address. */
return sockaddr_set(&cut->addr, AF_INET, hint->addr, 53);
......
......@@ -28,7 +28,7 @@
#include <time.h>
#include "lib/module.h"
#include "lib/context.h"
#include "lib/resolve.h"
#include "lib/cache.h"
/*
......@@ -42,9 +42,10 @@
* Output: { size: int }
*
*/
static char* get_size(struct kr_context *ctx, struct kr_module *module, const char *args)
static char* get_size(void *env, struct kr_module *module, const char *args)
{
char *result = NULL;
struct kr_context *ctx = env;
const namedb_api_t *storage = kr_cache_storage();
/* Fetch item count */
......@@ -83,8 +84,9 @@ static int is_expired(struct kr_cache_rrset *rr, uint32_t drift)
* Output: { pruned: int }
*
*/
static char* prune(struct kr_context *ctx, struct kr_module *module, const char *args)
static char* prune(void *env, struct kr_module *module, const char *args)
{
struct kr_context *ctx = env;
const namedb_api_t *storage = kr_cache_storage();
namedb_txn_t txn;
......@@ -131,8 +133,10 @@ static char* prune(struct kr_context *ctx, struct kr_module *module, const char
* Output: { result: bool }
*
*/
static char* clear(struct kr_context *ctx, struct kr_module *module, const char *args)
static char* clear(void *env, struct kr_module *module, const char *args)
{
struct kr_context *ctx = env;
namedb_txn_t txn;
int ret = kr_cache_txn_begin(ctx->cache, &txn, 0);
if (ret != 0) {
......
......@@ -14,10 +14,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tests/test.h"
#include <cmocka.h>
#include <libknot/internal/mempool.h>
#include "tests/test.h"
#include "lib/cache.h"
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tests/test.h"
#include <cmocka.h>
#include "lib/context.h"
mm_ctx_t global_mm;
static struct kr_context global_context;
static void test_context_init(void **state)
{
int ret = kr_context_init(&global_context, &global_mm);
assert_int_equal(ret, KNOT_EOK);
*state = &global_context;
}
static void test_context_deinit(void **state)
{
int ret = kr_context_deinit(*state);
assert_int_equal(ret, KNOT_EOK);
}
static void test_context_params(void **state)
{
assert_int_equal(kr_context_init(NULL, NULL), KNOT_EINVAL);
assert_int_equal(kr_context_deinit(NULL), KNOT_EINVAL);
}
int main(void)
{
test_mm_ctx_init(&global_mm);
const UnitTest tests[] = {
unit_test(test_context_params),
unit_test_teardown(test_context_init, test_context_deinit),
};
return run_tests(tests);
}
......@@ -23,9 +23,10 @@
/*
* Globals
*/
static mm_ctx_t global_mm; /* Test memory context */
static struct kr_context global_context; /* Resolution context */
static const char *global_tmpdir = NULL; /* Temporary directory */
static mm_ctx_t global_mm; /* Test memory context */
static modulelist_t global_modules; /* Array of modules. */
static struct kr_context global_context; /* Resolution context */
static const char *global_tmpdir = NULL; /* Temporary directory */
/*
* Test driver global variables.
......@@ -48,15 +49,25 @@ static PyObject* init(PyObject* self, PyObject* args)
memset(&g_mock_time, 0, sizeof(struct timeval));
g_mock_server = NULL;
/* Load basic modules. */
array_init(global_modules);
int ret = array_reserve(global_modules, 2);
if (ret < 0) {
return NULL;
}
kr_module_load(&global_modules.at[0], "iterate", NULL);
kr_module_load(&global_modules.at[1], "itercache", NULL);
global_modules.len = 2;
/* Initialize resolution context */
#define CACHE_SIZE 100*1024
mm_ctx_init(&global_mm);
kr_context_init(&global_context, &global_mm);
kr_context_register(&global_context, "iterate");
kr_context_register(&global_context, "itercache");
memset(&global_context, 0, sizeof(struct kr_context));
global_context.pool = &global_mm;
global_context.modules = &global_modules;
global_tmpdir = test_tmpdir_create();
assert(global_tmpdir);
global_context.cache = kr_cache_open(global_tmpdir, &global_mm, CACHE_SIZE);
global_context.cache = kr_cache_open(global_tmpdir, &global_mm, 100 * 4096);
assert(global_context.cache);
/* No configuration parsing support yet. */
......@@ -73,7 +84,11 @@ static PyObject* deinit(PyObject* self, PyObject* args)
return NULL;
}
kr_context_deinit(&global_context);
for (size_t i = 0; i < global_modules.len; ++i) {
kr_module_unload(&global_modules.at[i]);
}
array_clear(global_modules);
test_tmpdir_remove(global_tmpdir);
global_tmpdir = NULL;
if (g_mock_server) {
......
......@@ -15,8 +15,6 @@
*/
#include "tests/test.h"
#include <cmocka.h>
#include "lib/module.h"
static void test_module_params(void **state)
......
......@@ -15,10 +15,8 @@
*/
#include "tests/test.h"
#include <cmocka.h>
#include "lib/resolve.h"
#include "lib/rplan.h"
#include "lib/context.h"
static void test_rplan_params(void **state)
{
......@@ -50,8 +48,10 @@ static void test_rplan_push(void **state)
{
mm_ctx_t mm;
test_mm_ctx_init(&mm);
struct kr_context context;
kr_context_init(&context, &mm);
struct kr_context context = {
.pool = &mm,
};
struct kr_rplan rplan;
kr_rplan_init(&rplan, &context, &mm);
......@@ -59,7 +59,6 @@ static void test_rplan_push(void **state)
assert_non_null(kr_rplan_push(&rplan, NULL, (knot_dname_t *)"", 0, 0));
kr_rplan_deinit(&rplan);
kr_context_deinit(&context);
}
int main(void)
......
......@@ -18,8 +18,6 @@
#include <stdio.h>
#include "tests/test.h"
#include <cmocka.h>
#include "lib/utils.h"
static void test_strcatdup(void **state)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment