+++ /dev/null
-/* 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();
-}
+++ /dev/null
-/* 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);
-
-/** @} */
* 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
/** @} */
#include <libknot/processing/layer.h>
#include <libknot/packet/pkt.h>
-#include "lib/context.h"
+#include "lib/resolve.h"
#include "lib/rplan.h"
/**
}
/* 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:
lib/utils.c \
lib/nsrep.c \
lib/module.c \
- lib/context.c \
lib/resolve.c \
lib/zonecut.c \
lib/rplan.c \
lib/utils.h \
lib/nsrep.h \
lib/module.h \
- lib/context.h \
lib/resolve.h \
lib/zonecut.h \
lib/rplan.h \
/*
* Forward decls
*/
-struct kr_context;
struct kr_module;
struct kr_prop;
* 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;
};
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);
}
#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.
#include <libknot/errcode.h>
#include "lib/rplan.h"
-#include "lib/context.h"
#include "lib/cache.h"
#include "lib/defines.h"
#include "lib/layer.h"
#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 */
/*
* Defines.
*/
-#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x)))
/** Concatenate N strings. */
char* kr_strcatdup(unsigned n, ...);
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);
#include <time.h>
#include "lib/module.h"
-#include "lib/context.h"
+#include "lib/resolve.h"
#include "lib/cache.h"
/*
* 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 */
* 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;
* 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) {
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"
+++ /dev/null
-/* 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);
-}
/*
* 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.
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. */
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) {
*/
#include "tests/test.h"
-#include <cmocka.h>
-
#include "lib/module.h"
static void test_module_params(void **state)
*/
#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)
{
{
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);
assert_non_null(kr_rplan_push(&rplan, NULL, (knot_dname_t *)"", 0, 0));
kr_rplan_deinit(&rplan);
- kr_context_deinit(&context);
}
int main(void)
#include <stdio.h>
#include "tests/test.h"
-#include <cmocka.h>
-
#include "lib/utils.h"
static void test_strcatdup(void **state)