From: Arran Cudbard-Bell Date: Thu, 21 Mar 2024 04:24:27 +0000 (-0400) Subject: Add ttl.get function for cache module X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7ec5fb8778174d183c4e48cc9244f72b92b0392;p=thirdparty%2Ffreeradius-server.git Add ttl.get function for cache module --- diff --git a/src/lib/unlang/xlat_func.c b/src/lib/unlang/xlat_func.c index 44cb6748489..2ef35b08dee 100644 --- a/src/lib/unlang/xlat_func.c +++ b/src/lib/unlang/xlat_func.c @@ -294,10 +294,8 @@ xlat_t *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx c = xlat_func_register(ctx, name, func, return_type); if (!c) return NULL; - if (mctx) { - MEM(our_mctx = talloc_zero(c, module_inst_ctx_t)); /* Original won't stick around */ - memcpy(our_mctx, mctx, sizeof(*our_mctx)); - } + MEM(our_mctx = talloc_zero(c, module_inst_ctx_t)); /* Original won't stick around */ + memcpy(our_mctx, mctx, sizeof(*our_mctx)); c->mctx = our_mctx; return c; diff --git a/src/modules/rlm_cache/rlm_cache.c b/src/modules/rlm_cache/rlm_cache.c index b2ea7c8ab31..c0578f3bca9 100644 --- a/src/modules/rlm_cache/rlm_cache.c +++ b/src/modules/rlm_cache/rlm_cache.c @@ -888,6 +888,43 @@ xlat_action_t cache_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, return XLAT_ACTION_DONE; } +static xlat_action_t cache_ttl_get_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, + xlat_ctx_t const *xctx, + request_t *request, UNUSED fr_value_box_list_t *in) +{ + + rlm_cache_entry_t *c = NULL; + rlm_cache_t *inst = talloc_get_type_abort(xctx->mctx->inst->data, rlm_cache_t); + cache_call_env_t *env = talloc_get_type_abort(xctx->env_data, cache_call_env_t); + rlm_cache_handle_t *handle = NULL; + + rlm_rcode_t rcode = RLM_MODULE_NOOP; + + fr_value_box_t *vb; + + if (cache_acquire(&handle, inst, request) < 0) { + return XLAT_ACTION_FAIL; + } + + cache_find(&rcode, &c, inst, request, &handle, env->key); + switch (rcode) { + case RLM_MODULE_OK: /* found */ + break; + + default: + cache_release(inst, request, &handle); + return XLAT_ACTION_DONE; + } + + MEM(vb = fr_value_box_alloc_null(ctx)); + vb->vb_int64 = fr_time_delta_unwrap(fr_unix_time_sub(c->expires, fr_time_to_unix_time(request->packet->timestamp))); + fr_dcursor_append(out, vb); + + cache_release(inst, request, &handle); + + return XLAT_ACTION_DONE; +} + /** Release the allocated resources and cleanup the avps */ static void cache_unref(request_t *request, rlm_cache_t const *inst, rlm_cache_entry_t *entry, @@ -1384,6 +1421,9 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx) xlat_func_args_set(xlat, cache_xlat_args); xlat_func_call_env_set(xlat, &cache_method_env); + xlat = xlat_func_register_module(inst, mctx, "ttl.get", cache_ttl_get_xlat, FR_TYPE_VOID); + xlat_func_call_env_set(xlat, &cache_method_env); + return 0; } diff --git a/src/tests/modules/cache_rbtree/cache-xlat.unlang b/src/tests/modules/cache_rbtree/cache-xlat.unlang index 721af1db30f..eae4648a1f9 100644 --- a/src/tests/modules/cache_rbtree/cache-xlat.unlang +++ b/src/tests/modules/cache_rbtree/cache-xlat.unlang @@ -9,7 +9,12 @@ if (!ok) { test_fail } -&request.Login-LAT-Service := %cache(request.Callback-Id) +# Check the cache TTL function works +if (%cache.ttl.get() < 4) { + test_fail +} + +&request.Login-LAT-Service := %cache('request.Callback-Id') if (&Login-LAT-Service != &control.Callback-Id) { test_fail