The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
module_ctx.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: aa05f838015e12e458fef82fe4840662891057b2 $
20  *
21  * @file lib/server/module_ctx.h
22  * @brief Temporary argument structures for module calls.
23  *
24  * These get used in various places where we may not want to include
25  * the full module.h.
26  *
27  * @copyright 2021 Arran Cudbard-bell <a.cudbardb@freeradius.org>
28  */
29 RCSIDH(module_ctx_h, "$Id: aa05f838015e12e458fef82fe4840662891057b2 $")
30 
31 #include <freeradius-devel/server/dl_module.h>
32 #include <freeradius-devel/util/event.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /** Temporary structure to hold arguments for module calls
39  *
40  */
41 typedef struct {
42  dl_module_inst_t const *inst; //!< Dynamic loader API handle for the module.
43  void *thread; //!< Thread specific instance data.
44  void *env_data; //!< Per call environment data.
45  void *rctx; //!< Resume ctx that a module previously set.
46 } module_ctx_t;
47 
48 /** Temporary structure to hold arguments for instantiation calls
49  *
50  */
51 typedef struct {
52  dl_module_inst_t const *inst; //!< Dynamic loader API handle for the module.
54 
55 /** Temporary structure to hold arguments for thread_instantiation calls
56  *
57  */
58 typedef struct {
59  dl_module_inst_t const *inst; //!< Dynamic loader API handle for the module.
60  ///< Must come first to allow cast between
61  ///< module_inst_ctx.
62  void *thread; //!< Thread instance data.
63  fr_event_list_t *el; //!< Event list to register any IO handlers
64  ///< and timers against.
66 
67 DIAG_OFF(unused-function)
68 /** Allocate a module calling ctx on the heap based on an instance ctx
69  *
70  */
71 static module_ctx_t *module_ctx_from_inst(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx)
72 {
73  module_ctx_t *nmctx;
74 
75  nmctx = talloc_zero(ctx, module_ctx_t);
76  if (unlikely(!nmctx)) return NULL;
77  nmctx->inst = mctx->inst;
78 
79  return nmctx;
80 }
81 
82 /** Allocate a module calling ctx on the heap based on an instance ctx
83  *
84  */
86 {
87  module_ctx_t *nmctx;
88 
89  nmctx = talloc_zero(ctx, module_ctx_t);
90  if (unlikely(!nmctx)) return NULL;
91  nmctx->inst = mctx->inst;
92  nmctx->thread = mctx->thread;
93 
94  return nmctx;
95 }
96 
97 /** Duplicate a stack based module_ctx_t on the heap
98  *
99  */
100 static module_ctx_t *module_ctx_dup(TALLOC_CTX *ctx, module_ctx_t const *mctx)
101 {
102  module_ctx_t *nmctx;
103 
104  nmctx = talloc_zero(ctx, module_ctx_t);
105  if (unlikely(!nmctx)) return NULL;
106  memcpy(nmctx, mctx, sizeof(*nmctx));
107 
108  return nmctx;
109 }
110 DIAG_ON(unused-function)
111 
112 /** Wrapper to create a module_ctx_t as a compound literal
113  *
114  * This is used so that the compiler will flag any uses of (module_ctx_t)
115  * which don't set the required fields. Additional arguments should be added
116  * to this macro whenever the module_ctx_t fields are altered.
117  *
118  * @param[in] _dl_inst of the module being called.
119  * @param[in] _thread instance of the module being called.
120  * @param[in] _env_data Call environment data.
121  * @param[in] _rctx Resume ctx (if any).
122  */
123 #define MODULE_CTX(_dl_inst, _thread, _env_data, _rctx) &(module_ctx_t){ .inst = _dl_inst, .thread = _thread, .env_data = _env_data, .rctx = _rctx }
124 
125 /** Wrapper to create a module_ctx_t as a compound literal from a module_inst_ctx_t
126  *
127  * This is used so that the compiler will flag any uses of (module_ctx_t)
128  * which don't set the required fields. Additional arguments should be added
129  * to this macro whenever the module_ctx_t fields are altered.
130  *
131  * @param[in] _mctx to copy fields from.
132  */
133 #define MODULE_CTX_FROM_INST(_mctx) &(module_ctx_t){ .inst = (_mctx)->inst }
134 
135 /** Wrapper to create a module_ctx_t as a compound literal from a module_inst_ctx_t
136  *
137  * This is used so that the compiler will flag any uses of (module_ctx_t)
138  * which don't set the required fields. Additional arguments should be added
139  * to this macro whenever the module_ctx_t fields are altered.
140  *
141  * @param[in] _mctx to copy fields from.
142  */
143 #define MODULE_CTX_FROM_THREAD_INST(_mctx) &(module_ctx_t){ .inst = (_mctx)->inst, .thread = (_mctx)->thread, .env_data = (_mctx)->env_data }
144 
145 /** Wrapper to create a module_inst_ctx_t as a compound literal
146  *
147  * This is used so that the compiler will flag any uses of (module_inst_ctx_t)
148  * which don't set the required fields. Additional arguments should be added
149  * to this macro whenever the module_inst_ctx_t fields are altered.
150  *
151  * @param[in] _dl_inst of the module being called..
152  */
153 #define MODULE_INST_CTX(_dl_inst) &(module_inst_ctx_t){ .inst = _dl_inst }
154 
155 /** Wrapper to create a module_thread_inst_ctx_t as a compound literal
156  *
157  * This is used so that the compiler will flag any uses of (module_thread_inst_ctx_t)
158  * which don't set the required fields. Additional arguments should be added
159  * to this macro whenever the module_thread_inst_ctx_t fields are altered.
160  *
161  * @param[in] _dl_inst of the module being called.
162  * @param[in] _thread instance of the module being called.
163  * @param[in] _el Thread specific event list.
164  */
165 #define MODULE_THREAD_INST_CTX(_dl_inst, _thread, _el) &(module_thread_inst_ctx_t){ .inst = _dl_inst, .thread = _thread, .el = _el }
166 
167 /** Wrapper to create a module_inst_ctx_t as a comound listeral from a module_thread_ctx_t
168  *
169  * Extract the dl_module_inst_t from a module_thread_inst_ctx_t.
170  *
171  * @param[in] _mctx to extract module_thread_inst_ctx_t from.
172  */
173 #define MODULE_THREAD_INST_CTX_FROM_INST_CTX(_mctx) &(module_ctx_t){ .inst = (_mctx)->inst }
174 
175 #ifdef __cplusplus
176 }
177 #endif
#define DIAG_ON(_x)
Definition: build.h:419
#define RCSIDH(h, id)
Definition: build.h:445
#define unlikely(_x)
Definition: build.h:378
#define DIAG_OFF(_x)
Definition: build.h:418
A module/inst tuple.
Definition: dl_module.h:162
Stores all information relating to an event list.
Definition: event.c:411
void * env_data
Per call environment data.
Definition: module_ctx.h:44
void * thread
Thread specific instance data.
Definition: module_ctx.h:43
static module_ctx_t * module_ctx_from_inst(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx)
Allocate a module calling ctx on the heap based on an instance ctx.
Definition: module_ctx.h:71
static module_ctx_t * module_ctx_from_thread_inst(TALLOC_CTX *ctx, module_thread_inst_ctx_t const *mctx)
Allocate a module calling ctx on the heap based on an instance ctx.
Definition: module_ctx.h:85
void * rctx
Resume ctx that a module previously set.
Definition: module_ctx.h:45
fr_event_list_t * el
Event list to register any IO handlers and timers against.
Definition: module_ctx.h:63
static module_ctx_t * module_ctx_dup(TALLOC_CTX *ctx, module_ctx_t const *mctx)
Duplicate a stack based module_ctx_t on the heap.
Definition: module_ctx.h:100
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:52
void * thread
Thread instance data.
Definition: module_ctx.h:62
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:42
dl_module_inst_t const * inst
Dynamic loader API handle for the module.
Definition: module_ctx.h:59
Temporary structure to hold arguments for module calls.
Definition: module_ctx.h:41
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:51
Temporary structure to hold arguments for thread_instantiation calls.
Definition: module_ctx.h:58