The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
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: b0a45de71cb86083cababf938a5029bd0bcddbf2 $
19 *
20 * @file unlang/limit.c
21 * @brief Unlang "limit" keyword evaluation.
22 *
23 * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: b0a45de71cb86083cababf938a5029bd0bcddbf2 $")
26
27#include <freeradius-devel/server/rcode.h>
28#include "group_priv.h"
29#include "limit_priv.h"
30
34
42
43/** Send a signal (usually stop) to a request
44 *
45 * @param[in] request The current request.
46 * @param[in] frame current stack frame.
47 * @param[in] action to signal.
48 */
50{
51 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
52
53 if (action == FR_SIGNAL_CANCEL) {
54 state->thread->active_callers--;
55 }
56}
57
66
68{
69 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
70 unlang_action_t action;
71
73 fr_assert(state->thread != NULL);
74
75 if (state->thread->active_callers >= state->limit) return UNLANG_ACTION_FAIL;
76
78
80
82
83 return action;
84}
85
87{
88 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
89 fr_value_box_t *box = fr_value_box_list_head(&state->result);
90
91 /*
92 * compile_limit() ensures that the tmpl is cast to uint32, so we don't have to do any more work here.
93 */
94 state->limit = box->vb_uint32;
95
96 return unlang_limit_enforce(p_result, request, frame);
97}
98
100{
102 unlang_limit_t *gext;
103 unlang_frame_state_limit_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_limit_t);
104
106 gext = unlang_group_to_limit(g);
107
108 state->request = request;
109
110 if (!gext->vpt) {
111 state->limit = gext->limit;
112 return unlang_limit_enforce(p_result, request, frame);
113 }
114
115 fr_value_box_list_init(&state->result);
116
117 if (unlang_tmpl_push(state, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
118
120
122}
123
124
126{
128 &(unlang_op_t){
129 .name = "limit",
130 .interpret = unlang_limit,
131 .signal = unlang_limit_signal,
133 .frame_state_size = sizeof(unlang_frame_state_limit_t),
134 .frame_state_type = "unlang_frame_state_limit_t",
135
136 .thread_inst_size = sizeof(unlang_thread_limit_t),
137 .thread_inst_type = "unlang_thread_limit_t",
138 });
139}
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:485
#define UNUSED
Definition build.h:317
void * unlang_thread_instance(unlang_t const *instruction)
Get the thread-instance data for an instruction.
Definition compile.c:5276
Declarations for the "group" keyword.
unlang_action_t unlang_interpret_push_children(unlang_result_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:389
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
static unlang_action_t unlang_limit_enforce(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:67
static unlang_action_t unlang_limit_xlat_done(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:86
void unlang_limit_init(void)
Definition limit.c:125
static unlang_action_t unlang_limit_resume_done(UNUSED unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:58
request_t * request
Definition limit.c:38
unlang_thread_limit_t * thread
Definition limit.c:36
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:49
fr_value_box_list_t result
Definition limit.c:40
static unlang_action_t unlang_limit(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition limit.c:99
uint32_t active_callers
Definition limit.c:32
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
#define fr_assert(_expr)
Definition rad_assert.h:38
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:52
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
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:254
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:97
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_LIMIT
limit number of requests in a section
Definition unlang_priv.h:73
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.
unlang_t const * instruction
The unlang node we're evaluating.
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
Generic representation of a grouping.
An unlang operation.
Our interpreter stack, as distinct from the C stack.