From: Arran Cudbard-Bell Date: Thu, 15 Jun 2023 15:16:46 +0000 (-0400) Subject: xlat: Add module find function X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09e63b1023bae39f820d403221527cda0ae5460a;p=thirdparty%2Ffreeradius-server.git xlat: Add module find function --- diff --git a/src/lib/unlang/xlat_func.c b/src/lib/unlang/xlat_func.c index c5b24c589d1..225778563f4 100644 --- a/src/lib/unlang/xlat_func.c +++ b/src/lib/unlang/xlat_func.c @@ -156,6 +156,32 @@ static int xlat_arg_cmp_list_no_escape(xlat_arg_parser_t const a[], xlat_arg_par } #endif +xlat_t *xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name) +{ + char inst_name[256]; + + fr_assert(xlat_root); + + if (!*name) { + ERROR("%s: Invalid xlat name", __FUNCTION__); + return NULL; + } + + /* + * Name xlats other than those which are just the module instance + * as . + */ + if (mctx && name != mctx->inst->name) { + snprintf(inst_name, sizeof(inst_name), "%s.%s", mctx->inst->name, name); + name = inst_name; + } + + /* + * If it already exists, replace the instance. + */ + return fr_rb_find(xlat_root, &(xlat_t){ .name = name }); +} + /** Register an xlat function for a module * * @param[in] ctx Used to automate deregistration of the xlat fnction. @@ -169,7 +195,7 @@ static int xlat_arg_cmp_list_no_escape(xlat_arg_parser_t const a[], xlat_arg_par * - NULL on failure. */ xlat_t *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, - char const *name, xlat_func_t func, fr_type_t return_type) + char const *name, xlat_func_t func, fr_type_t return_type) { xlat_t *c; module_inst_ctx_t *our_mctx = NULL; diff --git a/src/lib/unlang/xlat_func.h b/src/lib/unlang/xlat_func.h index 7d23b1f20b0..ef664d76494 100644 --- a/src/lib/unlang/xlat_func.h +++ b/src/lib/unlang/xlat_func.h @@ -52,6 +52,8 @@ typedef int (*xlat_resolve_t)(xlat_exp_t *xlat, void *inst, xlat_res_rules_t con */ typedef int (*xlat_purify_t)(xlat_exp_t *xlat, void *inst, request_t *request); +xlat_t *xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name); + xlat_t *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx, char const *name, xlat_func_t func, fr_type_t return_type); xlat_t *xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type) CC_HINT(nonnull(2));