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

Using common cache for cookies.

Cookies are not stored in separate cache.
parent ecbdad70
......@@ -443,6 +443,7 @@ static void on_write(uv_write_t *req, int status)
/** Update DNS cookie data in packet. */
static bool subreq_update_cookies(uv_udp_t *handle, struct sockaddr *srvr_addr,
struct kr_cache *cookie_cache,
knot_pkt_t *pkt)
{
assert(handle);
......@@ -472,7 +473,7 @@ static bool subreq_update_cookies(uv_udp_t *handle, struct sockaddr *srvr_addr,
sockaddr_ptr = NULL;
}
kr_request_put_cookie(&kr_cookies_control,
kr_request_put_cookie(&kr_cookies_control, cookie_cache,
(struct sockaddr*) sockaddr_ptr, srvr_addr, pkt);
return true;
......@@ -500,7 +501,8 @@ static int qr_task_send(struct qr_task *task, uv_handle_t *handle, struct sockad
if (handle->type == UV_UDP) {
if (knot_wire_get_qr(pkt->wire) == 0) {
/* Update DNS cookies data in query. */
subreq_update_cookies((uv_udp_t *) handle, addr, pkt);
subreq_update_cookies((uv_udp_t *) handle, addr,
&task->worker->engine->resolver.cache, pkt);
}
uv_buf_t buf = { (char *)pkt->wire, pkt->size };
......
......@@ -208,6 +208,7 @@ static const uint8_t *peek_and_check_cc(struct kr_cache_txn *txn,
}
int kr_request_put_cookie(const struct cookies_control *cntrl,
struct kr_cache *cookie_cache,
const void *clnt_sockaddr, const void *srvr_sockaddr,
knot_pkt_t *pkt)
{
......@@ -218,7 +219,7 @@ int kr_request_put_cookie(const struct cookies_control *cntrl,
return kr_ok();
}
if (!cntrl->current_cs) {
if (!cntrl->current_cs || !cookie_cache) {
return kr_error(EINVAL);
}
......@@ -233,7 +234,7 @@ int kr_request_put_cookie(const struct cookies_control *cntrl,
}
struct kr_cache_txn txn;
kr_cache_txn_begin(&kr_cookies_control.cache, &txn, KNOT_DB_RDONLY);
kr_cache_txn_begin(cookie_cache, &txn, KNOT_DB_RDONLY);
const uint8_t *cached_cookie = peek_and_check_cc(&txn, srvr_sockaddr,
cc);
......
......@@ -42,7 +42,7 @@ struct cookies_control {
struct secret_quantity *current_cs; /*!< current client secret */
struct secret_quantity *recent_cs; /*!< recent client secret */
struct kr_cache cache; /*!< Server cookies cache. */
// struct kr_cache cache; /*!< Server cookies cache. */
};
/** Global cookies control. */
......@@ -74,11 +74,13 @@ int kr_client_cokie_fnv64(uint8_t cc_buf[KNOT_OPT_COOKIE_CLNT],
* Insert a DNS cookie into query packet.
* @note The packet must already contain ENDS section.
* @param cntrl Cookie control structure.
* @param cookie_cache Cookie cache.
* @param clnt_sockaddr Client address.
* @param srvr_sockaddr Server address.
* @param pkt DNS request packet.
*/
KR_EXPORT
int kr_request_put_cookie(const struct cookies_control *cntrl,
struct kr_cache *cookie_cache,
const void *clnt_sockaddr, const void *srvr_sockaddr,
knot_pkt_t *pkt);
......@@ -266,12 +266,12 @@ static int check_response(knot_layer_t *ctx, knot_pkt_t *pkt)
struct kr_request *req = ctx->data;
struct kr_query *qry = req->current_query;
struct kr_cache *cache = &kr_cookies_control.cache;
struct kr_cache *cookie_cache = &req->ctx->cache; //&kr_cookies_control.cache;
const struct sockaddr *srvr_sockaddr = passed_server_sockaddr(qry);
if (!pkt_cookie_opt && srvr_sockaddr &&
materialise_cookie_opt(cache, srvr_sockaddr, qry->timestamp.tv_sec,
materialise_cookie_opt(cookie_cache, srvr_sockaddr, qry->timestamp.tv_sec,
true, NULL)) {
/* We haven't received any cookies although we should. */
DEBUG_MSG(NULL, "%s\n", "expected to receive a cookie but none received");
......@@ -315,12 +315,12 @@ static int check_response(knot_layer_t *ctx, knot_pkt_t *pkt)
/* Don't cache received cookies that don't match the current secret. */
if (returned_current &&
!is_cookie_cached(cache, srvr_sockaddr, qry->timestamp.tv_sec,
!is_cookie_cached(cookie_cache, srvr_sockaddr, qry->timestamp.tv_sec,
pkt_cookie_opt)) {
DEBUG_MSG(NULL, "%s\n", "caching server cookie");
struct kr_cache_txn txn;
if (kr_cache_txn_begin(cache, &txn, 0) != 0) {
if (kr_cache_txn_begin(cookie_cache, &txn, 0) != 0) {
/* Could not acquire cache. */
return ctx->state;
}
......
......@@ -214,24 +214,13 @@ static char *cookies_control_config(void *env, struct kr_module *module, const c
return result;
}
/*
* Module implementation.
*/
KR_EXPORT
int cookies_control_init(struct kr_module *module)
static int cookies_cache_init(struct kr_cache *cache, struct engine *engine)
{
const char *storage_prefix = "lmdb://";
struct engine *engine = module->data;
DEBUG_MSG(NULL, "initialising with engine %p\n", (void *) engine);
assert(cache);
memset(&kr_cookies_control, 0, sizeof(kr_cookies_control));
kr_cookies_control.enabled = false;
kr_cookies_control.current_cs = &dflt_cs;
const char *storage_prefix = "lmdb://";
memset(&kr_cookies_control.cache, 0, sizeof(kr_cookies_control.cache));
memset(cache, 0, sizeof(*cache));
struct storage_api *lmdb_storage_api = find_storage_api(&engine->storage_registry,
storage_prefix);
......@@ -246,10 +235,30 @@ int cookies_control_init(struct kr_module *module)
opts.flags.env = 0x80000 | 0x100000; /* MDB_WRITEMAP|MDB_MAPASYNC */
errno = 0;
int ret = kr_cache_open(&kr_cookies_control.cache,
lmdb_storage_api->api(), &opts, engine->pool);
int ret = kr_cache_open(cache, lmdb_storage_api->api(), &opts,
engine->pool);
DEBUG_MSG(NULL, "cache_open retval %d: %s\n", ret, kr_strerror(ret));
return ret;
}
/*
* Module implementation.
*/
KR_EXPORT
int cookies_control_init(struct kr_module *module)
{
struct engine *engine = module->data;
memset(&kr_cookies_control, 0, sizeof(kr_cookies_control));
kr_cookies_control.enabled = false;
kr_cookies_control.current_cs = &dflt_cs;
// cookies_cache_init(&kr_cookies_control.cache, engine);
module->data = NULL;
return kr_ok();
......@@ -272,7 +281,7 @@ int cookies_control_deinit(struct kr_module *module)
}
kr_cookies_control.current_cs = &dflt_cs;
kr_cache_close(&kr_cookies_control.cache);
// kr_cache_close(&kr_cookies_control.cache);
return kr_ok();
}
......
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