]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
Merged the cookiectl module and cookiemonster layer into the cookies module.
authorKarel Slany <karel.slany@nic.cz>
Fri, 8 Jul 2016 10:54:02 +0000 (12:54 +0200)
committerOndřej Surý <ondrej@sury.org>
Thu, 11 Aug 2016 12:06:45 +0000 (14:06 +0200)
13 files changed:
daemon/engine.c
lib/cookies/cache.h
lib/lib.mk
lib/module.c
modules/cookiectl/cookiectl.mk [deleted file]
modules/cookies/README.rst [moved from modules/cookiectl/README.rst with 100% similarity]
modules/cookies/cookiectl.c [moved from modules/cookiectl/cookiectl.c with 83% similarity]
modules/cookies/cookiectl.h [new file with mode: 0644]
modules/cookies/cookiemonster.c [moved from lib/layer/cookiemonster.c with 95% similarity]
modules/cookies/cookiemonster.h [new file with mode: 0644]
modules/cookies/cookies.c [new file with mode: 0644]
modules/cookies/cookies.mk [new file with mode: 0644]
modules/modules.mk

index 13b14068ca234eb70fe8211e2b10d6ea52bcbebe..35fd331da25bac19a5a5add3f786672f6aad7dfa 100644 (file)
@@ -477,9 +477,6 @@ static int init_resolver(struct engine *engine)
 #endif /* defined(ENABLE_COOKIES) */
 
        /* Load basic modules */
-#if defined(ENABLE_COOKIES)
-       engine_register(engine, "cookiemonster", NULL, NULL);
-#endif /* defined(ENABLE_COOKIES) */
        engine_register(engine, "iterate", NULL, NULL);
        engine_register(engine, "validate", NULL, NULL);
        engine_register(engine, "rrcache", NULL, NULL);
index 09c218064ee1d719c84d2ca64dc82f82256e01cf..92a550d0cfcdcc4f0a7f3ea97ab482c1ce1fccbe 100644 (file)
@@ -16,6 +16,7 @@
 
 #pragma once
 
+#include <libknot/rrtype/opt.h>
 #include <libknot/rrtype/opt-cookie.h>
 #include <netinet/in.h>
 #include <stdint.h>
index 691164e4616a3771fe7ddfdf08da2ea7286d86c0..167e2fbebdee9ce875c74b7a57b6fb8c3604aa61 100644 (file)
@@ -46,7 +46,6 @@ libkres_TARGET := -L$(abspath lib) -lkres
 
 ifeq ($(HAS_nettle),yes)
 libkres_SOURCES += \
-       lib/layer/cookiemonster.c \
        lib/cookies/alg_containers.c \
        lib/cookies/alg_sha.c \
        lib/cookies/cache.c \
index a1c32fc412c66320fbd95881ed93597e9cdd046d..7c7a1398df7cd14bceac82ba8499a1ae1d1ae6b8 100644 (file)
 #include "lib/module.h"
 
 /* List of embedded modules */
-#if defined(ENABLE_COOKIES)
-const knot_layer_api_t *cookiemonster_layer(struct kr_module *module);
-#endif /* defined(ENABLE_COOKIES) */
 const knot_layer_api_t *iterate_layer(struct kr_module *module);
 const knot_layer_api_t *validate_layer(struct kr_module *module);
 const knot_layer_api_t *rrcache_layer(struct kr_module *module);
 const knot_layer_api_t *pktcache_layer(struct kr_module *module);
 static const struct kr_module embedded_modules[] = {
-#if defined(ENABLE_COOKIES)
-       { "cookiemonster", NULL, NULL, NULL, cookiemonster_layer, NULL, NULL, NULL },
-#endif /* defined(ENABLE_COOKIES) */
        { "iterate",  NULL, NULL, NULL, iterate_layer, NULL, NULL, NULL },
        { "validate", NULL, NULL, NULL, validate_layer, NULL, NULL, NULL },
        { "rrcache",  NULL, NULL, NULL, rrcache_layer, NULL, NULL, NULL },
diff --git a/modules/cookiectl/cookiectl.mk b/modules/cookiectl/cookiectl.mk
deleted file mode 100644 (file)
index f495577..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-cookiectl_CFLAGS := -fvisibility=hidden -fPIC
-cookiectl_SOURCES := \
-       modules/cookiectl/contrib/openbsd/strlcat.c \
-       modules/cookiectl/contrib/openbsd/strlcpy.c \
-       modules/cookiectl/contrib/print.c \
-       modules/cookiectl/contrib/sockaddr.c \
-       modules/cookiectl/print_pkt.c \
-       modules/cookiectl/cookiectl.c
-cookiectl_DEPEND := $(libkres)
-cookiectl_LIBS := $(contrib_TARGET) $(libkres_TARGET) $(libkres_LIBS)
-$(call make_c_module,cookiectl)
similarity index 83%
rename from modules/cookiectl/cookiectl.c
rename to modules/cookies/cookiectl.c
index 6182a55c926977a1762cef9e13ef9d4bafedfd7b..e1bafe5996eaacc67cbb1873c4018e08fae91704 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include "daemon/engine.h"
 #include "lib/cookies/alg_containers.h"
-#include "lib/cookies/control.h"
-#include "lib/layer.h"
-
-#define DEBUG_MSG(qry, fmt...) QRDEBUG(qry, "cookiectl",  fmt)
+#include "modules/cookies/cookiectl.h"
 
 #define NAME_CLIENT_ENABLED "client_enabled"
 #define NAME_CLIENT_SECRET "client_secret"
@@ -284,7 +280,7 @@ static void apply_from_copy(struct kr_cookie_ctx *running,
        running->srvr.enabled = shallow->srvr.enabled;
 }
 
-static bool apply_config(struct kr_cookie_ctx *ctx, const char *args)
+bool config_apply(struct kr_cookie_ctx *ctx, const char *args)
 {
        if (!ctx) {
                return false;
@@ -326,7 +322,7 @@ static bool apply_config(struct kr_cookie_ctx *ctx, const char *args)
        return success;
 }
 
-char *read_config(struct kr_cookie_ctx *ctx)
+char *config_read(struct kr_cookie_ctx *ctx)
 {
        if (!ctx) {
                return NULL;
@@ -372,36 +368,13 @@ char *read_config(struct kr_cookie_ctx *ctx)
        return result;
 }
 
-/**
- * Get/set DNS cookie related stuff.
- *
- * Input: { name: value, ... }
- * Output: current configuration
- */
-static char *cookiectl_config(void *env, struct kr_module *module, const char *args)
+int config_init(struct kr_cookie_ctx *ctx)
 {
-       struct kr_cookie_ctx *cookie_ctx = module->data;
-       assert(cookie_ctx);
-
-       /* Apply configuration, if any. */
-       apply_config(cookie_ctx, args);
-
-       /* Return current configuration. */
-       return read_config(cookie_ctx);
-}
-
-/*
- * Module implementation.
- */
-
-KR_EXPORT
-int cookiectl_init(struct kr_module *module)
-{
-       struct engine *engine = module->data;
-
-       struct kr_cookie_ctx *cookie_ctx = &engine->resolver.cookie_ctx;
+       if (!ctx) {
+               return kr_error(EINVAL);
+       }
 
-       kr_cookie_ctx_init(cookie_ctx);
+       kr_cookie_ctx_init(ctx);
 
        struct kr_cookie_secret *cs = new_cookie_secret(KNOT_OPT_COOKIE_CLNT,
                                                        true);
@@ -423,52 +396,34 @@ int cookiectl_init(struct kr_module *module)
                return kr_error(ENOKEY);
        }
 
-       cookie_ctx->clnt.current.secr = cs;
-       cookie_ctx->clnt.current.alg_id = clookup->id;
-
-       cookie_ctx->srvr.current.secr = ss;
-       cookie_ctx->srvr.current.alg_id = slookup->id;
+       ctx->clnt.current.secr = cs;
+       ctx->clnt.current.alg_id = clookup->id;
 
-       /* Replace engine pointer. */
-       module->data = cookie_ctx;
+       ctx->srvr.current.secr = ss;
+       ctx->srvr.current.alg_id = slookup->id;
 
        return kr_ok();
 }
 
-KR_EXPORT
-int cookiectl_deinit(struct kr_module *module)
+void config_deinit(struct kr_cookie_ctx *ctx)
 {
-       struct engine *engine = module->data;
-
-       struct kr_cookie_ctx *cookie_ctx = module->data;
-
-       cookie_ctx->clnt.enabled = false;
-
-       free(cookie_ctx->clnt.recent.secr);
-       cookie_ctx->clnt.recent.secr = NULL;
+       if (!ctx) {
+               return;
+       }
 
-       free(cookie_ctx->clnt.current.secr);
-       cookie_ctx->clnt.current.secr = NULL;
+       ctx->clnt.enabled = false;
 
-       cookie_ctx->srvr.enabled = false;
+       free(ctx->clnt.recent.secr);
+       ctx->clnt.recent.secr = NULL;
 
-       free(cookie_ctx->srvr.recent.secr);
-       cookie_ctx->srvr.recent.secr = NULL;
+       free(ctx->clnt.current.secr);
+       ctx->clnt.current.secr = NULL;
 
-       free(cookie_ctx->srvr.current.secr);
-       cookie_ctx->srvr.current.secr = NULL;
+       ctx->srvr.enabled = false;
 
-       return kr_ok();
-}
+       free(ctx->srvr.recent.secr);
+       ctx->srvr.recent.secr = NULL;
 
-KR_EXPORT
-struct kr_prop *cookiectl_props(void)
-{
-       static struct kr_prop prop_list[] = {
-           { &cookiectl_config, "config", "Empty value to return current configuration.", },
-           { NULL, NULL, NULL }
-       };
-       return prop_list;
+       free(ctx->srvr.current.secr);
+       ctx->srvr.current.secr = NULL;
 }
-
-KR_MODULE_EXPORT(cookiectl);
diff --git a/modules/cookies/cookiectl.h b/modules/cookies/cookiectl.h
new file mode 100644 (file)
index 0000000..e2f9f95
--- /dev/null
@@ -0,0 +1,47 @@
+/*  Copyright (C) 2016 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 "lib/cookies/control.h"
+
+/**
+ * @brief Sets cookie control context structure.
+ * @param ctx cookie control context
+ * @param args JSON string describing configuration changes
+ * @return true if changes successfully applied
+ */
+bool config_apply(struct kr_cookie_ctx *ctx, const char *args);
+
+/**
+ * @brief Reads cookie control context structure.
+ * @param ctx cookie control context
+ * @return JSON string or NULL on error
+ */
+char *config_read(struct kr_cookie_ctx *ctx);
+
+/**
+ * @brief Initialises cookie control context to default values.
+ * @param ctx cookie control context
+ * @return kr_ok() or error code
+ */
+int config_init(struct kr_cookie_ctx *ctx);
+
+/**
+ * @brief Clears the cookie control context.
+ * @param ctx cookie control context
+ */
+void config_deinit(struct kr_cookie_ctx *ctx);
similarity index 95%
rename from lib/layer/cookiemonster.c
rename to modules/cookies/cookiemonster.c
index 16f50a1e978c6ddfa2ae5d8225dd1a50f75520b1..90b6b46d537ec1bd1402050d254a15c93f6d0fdb 100644 (file)
 #include <libknot/db/db_lmdb.h>
 #include <libknot/error.h>
 #include <libknot/mm_ctx.h>
-#include <libknot/packet/pkt.h>
 #include <libknot/rrtype/opt-cookie.h> // branch dns-cookies-wip
 #include <stdlib.h>
 #include <string.h>
 
-#include "daemon/engine.h"
 #include "lib/cookies/alg_containers.h"
 #include "lib/cookies/cache.h"
 #include "lib/cookies/control.h"
 #include "lib/cookies/helper.h"
 #include "lib/cookies/nonce.h"
-#include "lib/module.h"
-#include "lib/layer.h"
+#include "modules/cookies/cookiemonster.h"
 
-#define DEBUG_MSG(qry, fmt...) QRDEBUG(qry, "cookiemonster",  fmt)
+#define DEBUG_MSG(qry, fmt...) QRDEBUG(qry, "cookies",  fmt)
 
 /* TODO -- The context must store sent cookies and server addresses in order
  * to make the process more reliable. */
@@ -275,7 +272,7 @@ static bool check_cookie_content_and_cache(const struct kr_cookie_settings *clnt
 }
 
 /** Process incoming response. */
-static int check_response(knot_layer_t *ctx, knot_pkt_t *pkt)
+int check_response(knot_layer_t *ctx, knot_pkt_t *pkt)
 {
        struct kr_request *req = ctx->data;
        struct kr_query *qry = req->current_query;
@@ -351,7 +348,7 @@ static inline uint8_t *req_cookie_option(struct kr_request *req)
        return knot_edns_get_option(req->qsource.opt, KNOT_EDNS_OPTION_COOKIE);
 }
 
-static int check_request(knot_layer_t *ctx, void *module_param)
+int check_request(knot_layer_t *ctx, void *module_param)
 {
        struct kr_request *req = ctx->data;
        struct kr_cookie_settings *srvr_sett = &req->ctx->cookie_ctx.srvr;
@@ -461,22 +458,3 @@ answer_add_cookies:
        }
        return return_state;
 }
-
-/** Module implementation. */
-
-KR_EXPORT
-const knot_layer_api_t *cookiemonster_layer(struct kr_module *module)
-{
-       /* The function answer_finalize() in resolver is called before any
-        * .finish callback. Therefore this layer does not use it. */
-
-       static knot_layer_api_t _layer = {
-               .begin = &check_request,
-               .consume = &check_response
-       };
-       /* Store module reference */
-       _layer.data = module;
-       return &_layer;
-}
-
-KR_MODULE_EXPORT(cookiemonster)
diff --git a/modules/cookies/cookiemonster.h b/modules/cookies/cookiemonster.h
new file mode 100644 (file)
index 0000000..7eaf318
--- /dev/null
@@ -0,0 +1,37 @@
+/*  Copyright (C) 2016 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/packet/pkt.h>
+
+#include "lib/layer.h"
+
+/**
+ * @brief Checks cookies of inbound requests.
+ * @param ctx layer context
+ * @param module_param module parameters
+ * @return layer state
+ */
+int check_request(knot_layer_t *ctx, void *module_param);
+
+/**
+ * @brief Checks cookies of received responses.
+ * @param ctx layer context
+ * @param pkt response packet
+ * @return layer state
+ */
+int check_response(knot_layer_t *ctx, knot_pkt_t *pkt);
diff --git a/modules/cookies/cookies.c b/modules/cookies/cookies.c
new file mode 100644 (file)
index 0000000..0706ee5
--- /dev/null
@@ -0,0 +1,102 @@
+/*  Copyright (C) 2016 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 <assert.h>
+
+#include "daemon/engine.h"
+#include "lib/layer.h"
+#include "modules/cookies/cookiectl.h"
+#include "modules/cookies/cookiemonster.h"
+
+/**
+ * Get/set DNS cookie related stuff.
+ *
+ * Input: { name: value, ... }
+ * Output: current configuration
+ */
+static char *cookies_config(void *env, struct kr_module *module,
+                            const char *args)
+{
+       struct kr_cookie_ctx *cookie_ctx = module->data;
+       assert(cookie_ctx);
+
+       /* Apply configuration, if any. */
+       config_apply(cookie_ctx, args);
+
+       /* Return current configuration. */
+       return config_read(cookie_ctx);
+}
+
+/*
+ * Module implementation.
+ */
+
+KR_EXPORT
+int cookies_init(struct kr_module *module)
+{
+       struct engine *engine = module->data;
+
+       struct kr_cookie_ctx *cookie_ctx = &engine->resolver.cookie_ctx;
+
+       int ret = config_init(cookie_ctx);
+       if (ret != kr_ok()) {
+               return ret;
+       }
+
+       /* Replace engine pointer. */
+       module->data = cookie_ctx;
+
+       return kr_ok();
+}
+
+KR_EXPORT
+int cookies_deinit(struct kr_module *module)
+{
+       struct engine *engine = module->data;
+
+       struct kr_cookie_ctx *cookie_ctx = module->data;
+
+       config_deinit(cookie_ctx);
+
+       return kr_ok();
+}
+
+KR_EXPORT
+const knot_layer_api_t *cookies_layer(struct kr_module *module)
+{
+       /* The function answer_finalize() in resolver is called before any
+        * .finish callback. Therefore this layer does not use it. */
+
+       static knot_layer_api_t _layer = {
+               .begin = &check_request,
+               .consume = &check_response
+       };
+       /* Store module reference */
+       _layer.data = module;
+       return &_layer;
+}
+
+KR_EXPORT
+struct kr_prop *cookies_props(void)
+{
+       static struct kr_prop prop_list[] = {
+           { &cookies_config, "config", "Empty value to return current configuration.", },
+           { NULL, NULL, NULL }
+       };
+       return prop_list;
+}
+
+KR_MODULE_EXPORT(cookies);
diff --git a/modules/cookies/cookies.mk b/modules/cookies/cookies.mk
new file mode 100644 (file)
index 0000000..37c1df2
--- /dev/null
@@ -0,0 +1,8 @@
+cookies_CFLAGS := -fvisibility=hidden -fPIC
+cookies_SOURCES := \
+       modules/cookies/cookiectl.c \
+       modules/cookies/cookiemonster.c \
+       modules/cookies/cookies.c
+cookies_DEPEND := $(libkres)
+cookies_LIBS := $(contrib_TARGET) $(libkres_TARGET) $(libkres_LIBS)
+$(call make_c_module,cookies)
index 1d3304fd866ce56e352e3e4727816c4830cbf47a..b47fcd9ae4dd071ef998f16b04ab065ba6b571b7 100644 (file)
@@ -4,7 +4,7 @@ modules_TARGETS := hints \
 
 # DNS cookies
 ifeq ($(HAS_nettle),yes)
-modules_TARGETS += cookiectl
+modules_TARGETS += cookies
 endif
 
 # Memcached