The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
limit.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: 670851dd2fd7433033b9f341c8ab759f6772e746 $
19  *
20  * @file unlang/limit.c
21  * @brief Unlang "limit" keyword evaluation.
22  *
23  * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24  */
25 RCSID("$Id: 670851dd2fd7433033b9f341c8ab759f6772e746 $")
26 
27 #include "group_priv.h"
28 #include "limit_priv.h"
29 
30 typedef struct {
33 
34 typedef struct {
38 
39  fr_value_box_list_t result;
41 
42 /** Send a signal (usually stop) to a request
43  *
44  * @param[in] request The current request.
45  * @param[in] frame current stack frame.
46  * @param[in] action to signal.
47  */
49 {
50  unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
51 
52  if (action == FR_SIGNAL_CANCEL) {
53  state->thread->active_callers--;
54  }
55 }
56 
58 {
59  unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
60 
61  state->thread->active_callers--;
62 
64 }
65 
67 {
68  unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
69  unlang_action_t action;
70 
71  state->thread = unlang_thread_instance(frame->instruction);
72  fr_assert(state->thread != NULL);
73 
74  if (state->thread->active_callers >= state->limit) return UNLANG_ACTION_FAIL;
75 
77 
78  action = unlang_interpret_push_children(p_result, request, frame->result, UNLANG_NEXT_STOP);
79 
80  state->thread->active_callers += (action == UNLANG_ACTION_PUSHED_CHILD);
81 
82  return action;
83 }
84 
86 {
87  unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
88  fr_value_box_t *box = fr_value_box_list_head(&state->result);
89 
90  /*
91  * compile_limit() ensures that the tmpl is cast to uint32, so we don't have to do any more work here.
92  */
93  state->limit = box->vb_uint32;
94 
95  return unlang_limit_enforce(p_result, request, frame);
96 }
97 
99 {
100  unlang_group_t *g;
101  unlang_limit_t *gext;
102  unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
103 
105  gext = unlang_group_to_limit(g);
106 
107  state->request = request;
108 
109  if (!gext->vpt) {
110  state->limit = gext->limit;
111  return unlang_limit_enforce(p_result, request, frame);
112  }
113 
114  fr_value_box_list_init(&state->result);
115 
116  if (unlang_tmpl_push(state, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
117 
119 
121 }
122 
123 
125 {
127  &(unlang_op_t){
128  .name = "limit",
129  .interpret = unlang_limit,
130  .signal = unlang_limit_signal,
131  .debug_braces = true,
132  .frame_state_size = sizeof(unlang_frame_state_limit_t),
133  .frame_state_type = "unlang_frame_state_limit_t",
134 
135  .thread_inst_size = sizeof(unlang_thread_limit_t),
136  .thread_inst_type = "unlang_thread_limit_t",
137  });
138 }
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition: action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition: action.h:39
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition: action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition: action.h:37
#define RCSID(id)
Definition: build.h:444
#define UNUSED
Definition: build.h:313
void * unlang_thread_instance(unlang_t const *instruction)
Get the thread-instance data for an instruction.
Definition: compile.c:5011
unlang_action_t unlang_interpret_push_children(rlm_rcode_t *p_result, request_t *request, rlm_rcode_t default_rcode, bool do_next_sibling)
Push the children of the current frame onto a new frame onto the stack.
Definition: interpret.c:243
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition: base.c:65
void unlang_limit_init(void)
Definition: limit.c:124
static unlang_action_t unlang_limit_resume_done(UNUSED rlm_rcode_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition: limit.c:57
static unlang_action_t unlang_limit_enforce(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: limit.c:66
request_t * request
Definition: limit.c:37
unlang_thread_limit_t * thread
Definition: limit.c:35
static void unlang_limit_signal(UNUSED request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition: limit.c:48
fr_value_box_list_t result
Definition: limit.c:39
static unlang_action_t unlang_limit_xlat_done(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: limit.c:85
uint32_t active_callers
Definition: limit.c:31
static unlang_action_t unlang_limit(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: limit.c:98
uint32_t limit
Definition: limit_priv.h:34
static unlang_limit_t * unlang_group_to_limit(unlang_group_t *g)
Cast a group structure to the limit keyword extension.
Definition: limit_priv.h:40
tmpl_t * vpt
Definition: limit_priv.h:33
unsigned int uint32_t
Definition: merged_model.c:33
rlm_rcode_t
Return codes indicating the result of the module call.
Definition: rcode.h:40
fr_signal_t
Definition: signal.h:48
fr_assert(0)
int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
Definition: tmpl.c:259
void * state
Stack frame specialisations.
Definition: unlang_priv.h:304
#define UNLANG_NEXT_STOP
Definition: unlang_priv.h:102
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
Definition: unlang_priv.h:539
@ UNLANG_TYPE_LIMIT
limit number of requests in a section
Definition: unlang_priv.h:79
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
Definition: unlang_priv.h:526
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:289
rlm_rcode_t result
The result from executing the instruction.
Definition: unlang_priv.h:308
Generic representation of a grouping.
Definition: unlang_priv.h:155
An unlang operation.
Definition: unlang_priv.h:214
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:288