From: Karel Slany Date: Fri, 8 Jul 2016 10:54:02 +0000 (+0200) Subject: Merged the cookiectl module and cookiemonster layer into the cookies module. X-Git-Tag: v1.1.0~2^2~78 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe386e4c13a0bc192307cf0227afb138412aba09;p=thirdparty%2Fknot-resolver.git Merged the cookiectl module and cookiemonster layer into the cookies module. --- diff --git a/daemon/engine.c b/daemon/engine.c index 13b14068c..35fd331da 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -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); diff --git a/lib/cookies/cache.h b/lib/cookies/cache.h index 09c218064..92a550d0c 100644 --- a/lib/cookies/cache.h +++ b/lib/cookies/cache.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include diff --git a/lib/lib.mk b/lib/lib.mk index 691164e46..167e2fbeb 100644 --- a/lib/lib.mk +++ b/lib/lib.mk @@ -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 \ diff --git a/lib/module.c b/lib/module.c index a1c32fc41..7c7a1398d 100644 --- a/lib/module.c +++ b/lib/module.c @@ -24,17 +24,11 @@ #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 index f495577d4..000000000 --- a/modules/cookiectl/cookiectl.mk +++ /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) diff --git a/modules/cookiectl/README.rst b/modules/cookies/README.rst similarity index 100% rename from modules/cookiectl/README.rst rename to modules/cookies/README.rst diff --git a/modules/cookiectl/cookiectl.c b/modules/cookies/cookiectl.c similarity index 83% rename from modules/cookiectl/cookiectl.c rename to modules/cookies/cookiectl.c index 6182a55c9..e1bafe599 100644 --- a/modules/cookiectl/cookiectl.c +++ b/modules/cookies/cookiectl.c @@ -21,12 +21,8 @@ #include #include -#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 index 000000000..e2f9f954d --- /dev/null +++ b/modules/cookies/cookiectl.h @@ -0,0 +1,47 @@ +/* Copyright (C) 2016 CZ.NIC, z.s.p.o. + + 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 . + */ + +#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); diff --git a/lib/layer/cookiemonster.c b/modules/cookies/cookiemonster.c similarity index 95% rename from lib/layer/cookiemonster.c rename to modules/cookies/cookiemonster.c index 16f50a1e9..90b6b46d5 100644 --- a/lib/layer/cookiemonster.c +++ b/modules/cookies/cookiemonster.c @@ -19,21 +19,18 @@ #include #include #include -#include #include // branch dns-cookies-wip #include #include -#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 index 000000000..7eaf318ea --- /dev/null +++ b/modules/cookies/cookiemonster.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2016 CZ.NIC, z.s.p.o. + + 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 . + */ + +#pragma once + +#include + +#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 index 000000000..0706ee5f8 --- /dev/null +++ b/modules/cookies/cookies.c @@ -0,0 +1,102 @@ +/* Copyright (C) 2016 CZ.NIC, z.s.p.o. + + 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 . + */ + +#include + +#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 index 000000000..37c1df2b6 --- /dev/null +++ b/modules/cookies/cookies.mk @@ -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) diff --git a/modules/modules.mk b/modules/modules.mk index 1d3304fd8..b47fcd9ae 100644 --- a/modules/modules.mk +++ b/modules/modules.mk @@ -4,7 +4,7 @@ modules_TARGETS := hints \ # DNS cookies ifeq ($(HAS_nettle),yes) -modules_TARGETS += cookiectl +modules_TARGETS += cookies endif # Memcached