Commit 13c9d9cf authored by Karel Slaný's avatar Karel Slaný Committed by Ondřej Surý
Browse files

Cookies cache TTL can be configured.

parent 4bc0c6d6
......@@ -21,8 +21,6 @@
/** DNS cookie cache entry tag. */
#define KR_CACHE_COOKIE (KR_CACHE_USER + 'C')
#define COOKIE_TTL 72000
/**
* Peek the cache for asset (tag, socket address).
* @note The 'drift' is the time passed between the inception time and now (in seconds).
......
......@@ -36,12 +36,17 @@ struct kr_cookie_secret {
KR_EXPORT
extern struct kr_cookie_secret dflt_cs;
/** Default cookie TTL. */
#define DFLT_COOKIE_TTL 72000
/** DNS cookies controlling structure. */
struct kr_cookie_ctx {
bool enabled; /*!< Enabled/disables DNS cookies functionality. */
bool enabled; /**< Enabled/disables DNS cookies functionality. */
struct kr_cookie_secret *current_cs; /**< current client secret */
struct kr_cookie_secret *recent_cs; /**< recent client secret */
struct kr_cookie_secret *current_cs; /*!< current client secret */
struct kr_cookie_secret *recent_cs; /*!< recent client secret */
uint32_t cache_ttl; /**< TTL used when caching cookies */
// struct kr_cache cache; /*!< Server cookies cache. */
};
......
......@@ -262,11 +262,12 @@ static bool is_cookie_cached(struct kr_cache *cache,
/**
* Check cookie content and store it to cache.
*/
static bool check_cookie_content_and_cache(struct kr_query *qry,
static bool check_cookie_content_and_cache(struct kr_cookie_ctx *cntrl,
struct kr_query *qry,
uint8_t *pkt_cookie_opt,
struct kr_cache *cache)
{
assert(pkt_cookie_opt);
assert(cntrl && qry && pkt_cookie_opt && cache);
uint8_t *pkt_cookie_data = knot_edns_opt_get_data(pkt_cookie_opt);
uint16_t pkt_cookie_len = knot_edns_opt_get_length(pkt_cookie_opt);
......@@ -289,7 +290,7 @@ static bool check_cookie_content_and_cache(struct kr_query *qry,
const struct sockaddr *srvr_sockaddr = NULL;
bool returned_current = false;
ret = srvr_sockaddr_cc_check(&srvr_sockaddr, &returned_current, qry,
pkt_cc, &kr_glob_cookie_ctx);
pkt_cc, cntrl);
if (ret != kr_ok()) {
DEBUG_MSG(NULL, "%s\n", "could not match received cookie");
return false;
......@@ -303,7 +304,7 @@ static bool check_cookie_content_and_cache(struct kr_query *qry,
struct kr_cache_txn txn;
if (kr_cache_txn_begin(cache, &txn, 0) == kr_ok()) {
struct timed_cookie timed_cookie = { COOKIE_TTL, pkt_cookie_opt };
struct timed_cookie timed_cookie = { cntrl->cache_ttl, pkt_cookie_opt };
ret = kr_cookie_cache_insert_cookie(&txn, srvr_sockaddr,
&timed_cookie,
......@@ -356,8 +357,8 @@ static int check_response(knot_layer_t *ctx, knot_pkt_t *pkt)
return ctx->state;
}
if (!check_cookie_content_and_cache(qry, pkt_cookie_opt,
cookie_cache)) {
if (!check_cookie_content_and_cache(&kr_glob_cookie_ctx, qry,
pkt_cookie_opt, cookie_cache)) {
return KNOT_STATE_FAIL;
}
......
......@@ -45,6 +45,7 @@ static struct storage_api *find_storage_api(const storage_registry_t *registry,
#define NAME_ENABLED "enabled"
#define NAME_CLIENT_SECRET "client_secret"
#define NAME_CACHE_TTL "cache_ttl"
static bool aply_enabled(struct kr_cookie_ctx *cntrl, const JsonNode *node)
{
......@@ -141,6 +142,16 @@ static bool apply_client_secret(struct kr_cookie_ctx *cntrl, const JsonNode *nod
return true;
}
static bool apply_cache_ttl(struct kr_cookie_ctx *cntrl, const JsonNode *node)
{
if (node->tag == JSON_NUMBER) {
cntrl->cache_ttl = node->number_;
return true;
}
return false;
}
static bool apply_configuration(struct kr_cookie_ctx *cntrl, const JsonNode *node)
{
assert(cntrl && node);
......@@ -154,6 +165,8 @@ static bool apply_configuration(struct kr_cookie_ctx *cntrl, const JsonNode *nod
return aply_enabled(cntrl, node);
} else if (strcmp(node->key, NAME_CLIENT_SECRET) == 0) {
return apply_client_secret(cntrl, node);
} else if (strcmp(node->key, NAME_CACHE_TTL) == 0) {
return apply_cache_ttl(cntrl, node);
}
return false;
......@@ -210,6 +223,8 @@ static char *cookiectl_config(void *env, struct kr_module *module, const char *a
json_append_member(root_node, NAME_ENABLED,
json_mkbool(kr_glob_cookie_ctx.enabled));
read_secret(root_node, &kr_glob_cookie_ctx);
json_append_member(root_node, NAME_CACHE_TTL,
json_mknumber(kr_glob_cookie_ctx.cache_ttl));
result = json_encode(root_node);
json_delete(root_node);
return result;
......@@ -256,6 +271,7 @@ int cookiectl_init(struct kr_module *module)
kr_glob_cookie_ctx.enabled = false;
kr_glob_cookie_ctx.current_cs = &dflt_cs;
kr_glob_cookie_ctx.cache_ttl = DFLT_COOKIE_TTL;
// cookies_cache_init(&kr_glob_cookie_ctx.cache, engine);
......
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