]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib: demoted context to simple interface, not responsible for data
authorMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 27 Mar 2015 12:26:44 +0000 (13:26 +0100)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Fri, 27 Mar 2015 12:26:44 +0000 (13:26 +0100)
20 files changed:
lib/context.c [deleted file]
lib/context.h [deleted file]
lib/defines.h
lib/layer.h
lib/layer/iterate.c
lib/lib.mk
lib/module.h
lib/resolve.c
lib/resolve.h
lib/rplan.c
lib/rplan.h
lib/utils.h
lib/zonecut.c
modules/cachectl/cachectl.c
tests/test_cache.c
tests/test_context.c [deleted file]
tests/test_integration.c
tests/test_module.c
tests/test_rplan.c
tests/test_utils.c

diff --git a/lib/context.c b/lib/context.c
deleted file mode 100644 (file)
index 16789a8..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/*  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();
-}
diff --git a/lib/context.h b/lib/context.h
deleted file mode 100644 (file)
index a7628ce..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*  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);
-
-/** @} */
index 25811bf914a1344306d9f3ca59feee5e0b53b7fa..3604b0039003cf722d8e2c2f6cd9a5992ed91316 100644 (file)
@@ -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  
 
 /** @} */
index c830401910bfab8bc5b99893db527461dd6c131f..1d25a3d2f349d01ccf0fa5a80323e8ec5059b0c9 100644 (file)
@@ -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"
 
 /**
index 2eb9962c441a8d911b7478ba89eb02c40bdce1ec..bbd5e8fd4e9db88ed828676f47868f3014fe82e4 100644 (file)
@@ -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:
index 3015ad5d6f6543993b3d9400e348f5696f7de703..151b1446bab83dcb27b61397fae6faa013f23290 100644 (file)
@@ -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            \
index bb16815cfb78ea7093c7551c8d0fdb356d9a22b5..f2faee568f6208b8412ab4cf9f1f247c4b6d5f9a 100644 (file)
@@ -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;
 };
index 7720018130e495ff6f62b16523828f625fbd8794..5b62364f230e92ba9410d13d9eac75f427b61c1c 100755 (executable)
@@ -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);
                }
index eeb9d10ec212af1c1777da78ae28cf0626b55184..e5aedfee91e712dd47238d0e3bf7cf0bceff69cf 100644 (file)
 #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.
index 713e8093eedd6938c62965afbea98c619063dbf4..12cb9b0ccd122ad1750547bb271a9de362389d57 100644 (file)
@@ -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"
index febd18959dd27343eed18700e7c2b494292fe66b..5c40a182f9b0bb55c0ce5e02501df13ae094bd67 100644 (file)
@@ -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 */
index afb47d3233d34a7dfaebe4b543cf5c27370a7a87..0ad06a4f652d35067e6e6dabf8d07d3484cc28b7 100644 (file)
@@ -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, ...);
index 939c993c73a7e32d86b6f67a41ff32440807f19e..efbab766bbaf20d0152108a0e49273bf243c64d9 100644 (file)
@@ -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);
index 92e7888f4a903dc060b350d2ad6a2f3a03a4d583..ec3ad8b953e897a0841a14cd71958f47773a3c40 100644 (file)
@@ -28,7 +28,7 @@
 #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 */
@@ -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) {
index 2b62d16ab13e962487c045c1430b5ccfd176c2cd..960307635a0eaa44c780140479153701f92ecd4d 100644 (file)
     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"
 
 
diff --git a/tests/test_context.c b/tests/test_context.c
deleted file mode 100644 (file)
index 3a5fe4c..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*  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);
-}
index b2425b9bc8a5dffb23758b01e14c474f67bebe56..38b58cdf121dc2ffd31eefe2d6b9a14f3130a6a8 100644 (file)
 /*
  * 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) {
index 3152d0b77da8eda27a4e7f2cd2655d51889157f3..031000e9662aef4e29114a4e6e1108cbf1b0a998 100644 (file)
@@ -15,8 +15,6 @@
  */
 
 #include "tests/test.h"
-#include <cmocka.h>
-
 #include "lib/module.h"
 
 static void test_module_params(void **state)
index 88ad4c29ca80004019d908c84bb140ce14db553c..e1f53c359abc0546f326b279cf1a1bcb27342c05 100644 (file)
  */
 
 #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)
index f0f25ca6670761de5340a91292ef662134f2c69d..36b0b3235bc468de5c514f2c719e9110d133ac30 100644 (file)
@@ -18,8 +18,6 @@
 #include <stdio.h>
 
 #include "tests/test.h"
-#include <cmocka.h>
-
 #include "lib/utils.h"
 
 static void test_strcatdup(void **state)