The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
xlat_func.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 6d64d87b17c118971ad984eab390f5a557882302 $
20  *
21  * @file lib/unlang/xlat_func.h
22  * @brief Registration API for xlat functions.
23  *
24  * @copyright 2023 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25  */
26 RCSIDH(xlat_register_h, "$Id: 6d64d87b17c118971ad984eab390f5a557882302 $")
27 
28 #include <freeradius-devel/unlang/xlat.h>
29 
30 /*
31  * GCC doesn't support flag_enum (yet)
32  *
33  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81665
34  */
35 DIAG_OFF(attributes)
36 typedef enum CC_HINT(flag_enum) {
41 DIAG_ON(attributes)
42 
43 /** Custom function to print xlat debug
44  */
45 typedef fr_slen_t (*xlat_print_t)(fr_sbuff_t *in, xlat_exp_t const *self, void *inst, fr_sbuff_escape_rules_t const *e_rules);
46 
47 /** Custom function to perform resolution of arguments
48  */
49 typedef int (*xlat_resolve_t)(xlat_exp_t *xlat, void *inst, xlat_res_rules_t const *xr_rules);
50 
51 /** Custom function purify the result of an xlat function
52  */
53 typedef int (*xlat_purify_t)(xlat_exp_t *xlat, void *inst, request_t *request);
54 
55 int8_t xlat_func_cmp(void const *one, void const *two);
56 
57 xlat_t *xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name);
58 
59 xlat_t *xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type) CC_HINT(nonnull(2));
60 
61 void xlat_mctx_set(xlat_t *x, module_inst_ctx_t const *mctx);
62 
63 int xlat_func_args_set(xlat_t *xlat, xlat_arg_parser_t const args[]) CC_HINT(nonnull);
64 
65 void xlat_func_call_env_set(xlat_t *x, call_env_method_t const *env) CC_HINT(nonnull);
66 
67 void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags) CC_HINT(nonnull);
68 
69 void xlat_func_print_set(xlat_t *xlat, xlat_print_t func);
70 
72 
73 void xlat_purify_func_set(xlat_t *xlat, xlat_purify_t func);
74 
75 /** Set the escaped values for output boxes
76  *
77  * Any boxes output by the xlat function will have their values marked as safe for something.
78  *
79  * @param[in] _xlat function to set the escaped value for (as returned by xlat_register).
80  * @param[in] _escaped escaped value to write to output boxes.
81  */
82 #define xlat_func_safe_for_set(_xlat, _escaped) _xlat_func_safe_for_set(_xlat, (uintptr_t) (_escaped))
83 void _xlat_func_safe_for_set(xlat_t *xlat, uintptr_t escaped);
84 
85 /** Set a callback for global instantiation of xlat functions
86  *
87  * @param[in] _xlat function to set the callback for (as returned by xlat_register).
88  * @param[in] _instantiate A instantiation callback.
89  * @param[in] _inst_struct The instance struct to pre-allocate.
90  * @param[in] _detach A destructor callback.
91  * @param[in] _uctx to pass to _instantiate and _detach callbacks.
92  */
93 #define xlat_func_instantiate_set(_xlat, _instantiate, _inst_struct, _detach, _uctx) \
94  _xlat_func_instantiate_set(_xlat, _instantiate, #_inst_struct, sizeof(_inst_struct), _detach, _uctx)
95 void _xlat_func_instantiate_set(xlat_t const *xlat,
96  xlat_instantiate_t instantiate, char const *inst_type, size_t inst_size,
97  xlat_detach_t detach,
98  void *uctx);
99 
100 /** Set a callback for thread-specific instantiation of xlat functions
101  *
102  * @param[in] _xlat function to set the callback for (as returned by xlat_register).
103  * @param[in] _instantiate A instantiation callback.
104  * @param[in] _inst_struct The instance struct to pre-allocate.
105  * @param[in] _detach A destructor callback.
106  * @param[in] _uctx to pass to _instantiate and _detach callbacks.
107  */
108 #define xlat_func_thread_instantiate_set(_xlat, _instantiate, _inst_struct, _detach, _uctx) \
109  _xlat_func_thread_instantiate_set(_xlat, _instantiate, #_inst_struct, sizeof(_inst_struct), _detach, _uctx)
112  char const *thread_inst_type, size_t thread_inst_size,
114  void *uctx);
115 
116 void xlat_func_unregister(char const *name);
118 /** @hidecallgraph */
119 
120 int xlat_func_init(void);
121 void xlat_func_free(void);
va_list args
Definition: acutest.h:770
#define DIAG_ON(_x)
Definition: build.h:456
#define RCSIDH(h, id)
Definition: build.h:482
#define DIAG_OFF(_x)
Definition: build.h:455
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
static fr_slen_t in
Definition: dict.h:821
fr_type_t
Definition: merged_model.c:80
ssize_t fr_slen_t
Definition: merged_model.c:35
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:50
static void thread_detach(UNUSED void *uctx)
Explicitly cleanup module/xlat resources.
Definition: radiusd.c:148
static int thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el, UNUSED void *uctx)
Create module and xlat per-thread instances.
Definition: radiusd.c:131
static char const * name
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
Module instance data.
Definition: module.h:265
eap_aka_sim_process_conf_t * inst
int(* xlat_thread_detach_t)(xlat_thread_inst_ctx_t const *xctx)
xlat thread detach callback
Definition: xlat.h:273
int(* xlat_detach_t)(xlat_inst_ctx_t const *xctx)
xlat detach callback
Definition: xlat.h:258
xlat_action_t(* xlat_func_t)(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
xlat callback function
Definition: xlat.h:214
int(* xlat_thread_instantiate_t)(xlat_thread_inst_ctx_t const *xctx)
Allocate new thread instance data for an xlat instance.
Definition: xlat.h:244
int(* xlat_instantiate_t)(xlat_inst_ctx_t const *xctx)
Allocate new instance data for an xlat instance.
Definition: xlat.h:235
Definition for a single argument consumend by an xlat function.
Definition: xlat.h:145
int nonnull(2, 5))
void xlat_purify_func_set(xlat_t *xlat, xlat_purify_t func)
Set a resolve routine for an xlat function.
Definition: xlat_func.c:434
void _xlat_func_safe_for_set(xlat_t *xlat, uintptr_t escaped)
Set the escaped values for output boxes.
Definition: xlat_func.c:444
void xlat_func_free(void)
Definition: xlat_func.c:566
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type))
Register an xlat function.
Definition: xlat_func.c:218
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition: xlat_func.c:402
void xlat_func_call_env_set(xlat_t *x, call_env_method_t const *env)
Register call environment of an xlat.
Definition: xlat_func.c:392
void xlat_func_resolve_set(xlat_t *xlat, xlat_resolve_t func)
Set a resolve routine for an xlat function.
Definition: xlat_func.c:424
int(* xlat_purify_t)(xlat_exp_t *xlat, void *inst, request_t *request)
Custom function purify the result of an xlat function.
Definition: xlat_func.h:53
int(* xlat_resolve_t)(xlat_exp_t *xlat, void *inst, xlat_res_rules_t const *xr_rules)
Custom function to perform resolution of arguments.
Definition: xlat_func.h:49
fr_slen_t(* xlat_print_t)(fr_sbuff_t *in, xlat_exp_t const *self, void *inst, fr_sbuff_escape_rules_t const *e_rules)
Custom function to print xlat debug.
Definition: xlat_func.h:45
xlat_func_flags_t
Definition: xlat_func.h:36
@ XLAT_FUNC_FLAG_NONE
Definition: xlat_func.h:37
@ XLAT_FUNC_FLAG_PURE
Definition: xlat_func.h:38
@ XLAT_FUNC_FLAG_INTERNAL
Definition: xlat_func.h:39
int xlat_func_args_set(xlat_t *xlat, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition: xlat_func.c:365
void xlat_func_unregister_module(module_instance_t const *inst)
Definition: xlat_func.c:533
int xlat_func_init(void)
Definition: xlat_func.c:550
void xlat_func_print_set(xlat_t *xlat, xlat_print_t func)
Set a print routine for an xlat function.
Definition: xlat_func.c:414
int8_t xlat_func_cmp(void const *one, void const *two)
Compare two xlat_t by the underlying function.
Definition: xlat_func.c:69
void xlat_mctx_set(xlat_t *x, module_inst_ctx_t const *mctx)
Associate a module calling ctx with the xlat.
Definition: xlat_func.c:299
void xlat_func_unregister(char const *name)
Unregister an xlat function.
Definition: xlat_func.c:519
void _xlat_func_thread_instantiate_set(xlat_t const *xlat, xlat_thread_instantiate_t thread_instantiate, char const *thread_inst_type, size_t thread_inst_size, xlat_thread_detach_t thread_detach, void *uctx)
Register an async xlat.
Definition: xlat_func.c:489
void _xlat_func_instantiate_set(xlat_t const *xlat, xlat_instantiate_t instantiate, char const *inst_type, size_t inst_size, xlat_detach_t detach, void *uctx)
Set global instantiation/detach callbacks.
Definition: xlat_func.c:461
xlat_t * xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name)
Definition: xlat_func.c:182
An xlat expansion node.
Definition: xlat_priv.h:151