The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
switch.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: 68430d1154f56ba1226abd6f27ffe098cd33f291 $
19 *
20 * @file unlang/switch.c
21 * @brief Unlang "switch" keyword evaluation.
22 *
23 * @copyright 2006-2019 The FreeRADIUS server project
24 */
25RCSID("$Id: 68430d1154f56ba1226abd6f27ffe098cd33f291 $")
26
27#include <freeradius-devel/server/rcode.h>
28#include "group_priv.h"
29#include "switch_priv.h"
30
32{
33 unlang_t *found;
34
35 unlang_group_t *switch_g;
36 unlang_switch_t *switch_gext;
37
38 fr_value_box_t const *box = NULL;
39
41
42 /*
43 * Mock up an unlang_cast_t. Note that these on-stack
44 * buffers are the reason why case_cmp(), case_hash(),
45 * and case_to_key() use direct casts, and not the
46 * "generic to x" functions.
47 */
48 tmpl_t case_vpt = (tmpl_t) {
49 .type = TMPL_TYPE_DATA,
50 };
51 unlang_case_t my_case = (unlang_case_t) {
53 .self = (unlang_t) {
55 },
56 },
57 .vpt = &case_vpt,
58 };
59
60 switch_g = unlang_generic_to_group(frame->instruction);
61 switch_gext = unlang_group_to_switch(switch_g);
62
63 found = NULL;
64
65 /*
66 * The attribute doesn't exist. We can skip
67 * directly to the default 'case' statement.
68 */
69 if (tmpl_is_attr(switch_gext->vpt)) {
70 if (tmpl_find_vp(&vp, request, switch_gext->vpt) < 0) {
71 found = switch_gext->default_case;
72 goto do_null_case;
73 } else {
74 box = &vp->data;
75 }
76
77 /*
78 * Expand the template if necessary, so that it
79 * is evaluated once instead of for each 'case'
80 * statement.
81 */
82 } else if (tmpl_is_xlat(switch_gext->vpt) ||
83 tmpl_is_exec(switch_gext->vpt)) {
84 ssize_t slen;
85
87 request, switch_gext->vpt);
88 if (slen < 0) goto find_null_case;
89 } else if (!fr_cond_assert_msg(0, "Invalid tmpl type %s", tmpl_type_to_str(switch_gext->vpt->type))) {
90 return UNLANG_ACTION_FAIL;
91 }
92
93 /*
94 * case_gext->vpt.data.literal is an in-line box, so we
95 * have to make a shallow copy of its contents.
96 *
97 * Note: We do not pass a ctx here as we don't want to
98 * create a reference.
99 */
100 fr_value_box_copy_shallow(NULL, &case_vpt.data.literal, box);
101 found = fr_htrie_find(switch_gext->ht, &my_case);
102 if (!found) {
103 find_null_case:
104 found = switch_gext->default_case;
105 }
106
107do_null_case:
108 /*
109 * Nothing found. Just continue, and ignore the "switch"
110 * statement.
111 */
112 if (!found) return UNLANG_ACTION_EXECUTE_NEXT;
113
116 }
117
119}
120
121
123{
125
127
128 return unlang_group(p_result, request, frame);
129}
130
132{
134 &(unlang_op_t){
135 .name = "switch",
136 .interpret = unlang_switch,
138 });
139
141 &(unlang_op_t){
142 .name = "case",
143 .interpret = unlang_case,
145 });
146}
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_EXECUTE_NEXT
Execute the next unlang_t.
Definition action.h:38
@ UNLANG_ACTION_STOP_PROCESSING
Break out of processing the current request (unwind).
Definition action.h:42
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
#define RCSID(id)
Definition build.h:485
#define UNUSED
Definition build.h:317
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:156
unlang_action_t unlang_group(UNUSED unlang_result_t *p_result, request_t *request, UNUSED unlang_stack_frame_t *frame)
Definition group.c:31
Declarations for the "group" keyword.
static void * fr_htrie_find(fr_htrie_t *ht, void const *data)
Find data in a htrie.
Definition htrie.h:104
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1661
int unlang_interpret_push(unlang_result_t *result_p, request_t *request, unlang_t const *instruction, unlang_frame_conf_t const *conf, bool do_next_sibling)
Push a new frame onto the stack.
Definition interpret.c:283
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:153
#define UNLANG_SUB_FRAME
Definition interpret.h:37
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
@ FR_TYPE_VALUE_BOX
A boxed value.
long int ssize_t
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:52
#define RETURN_UNLANG_NOOP
Definition rcode.h:63
int tmpl_find_vp(fr_pair_t **out, request_t *request, tmpl_t const *vpt))
Returns the first VP matching a tmpl_t.
Definition tmpl_eval.c:768
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:634
#define tmpl_is_xlat(vpt)
Definition tmpl.h:210
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define tmpl_is_exec(vpt)
Definition tmpl.h:211
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:138
#define tmpl_aexpand_type(_ctx, _out, _type, _request, _vpt)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
Definition tmpl.h:1071
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
static unlang_action_t unlang_case(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition switch.c:122
void unlang_switch_init(void)
Definition switch.c:131
static unlang_action_t unlang_switch(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition switch.c:31
fr_htrie_t * ht
Definition switch_priv.h:36
unlang_group_t group
Definition switch_priv.h:56
static unlang_switch_t * unlang_group_to_switch(unlang_group_t *g)
Cast a group structure to the switch keyword extension.
Definition switch_priv.h:42
unlang_t * default_case
Definition switch_priv.h:34
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:97
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_SWITCH
Switch section.
Definition unlang_priv.h:58
@ UNLANG_TYPE_CASE
Case section (within a UNLANG_TYPE_SWITCH).
Definition unlang_priv.h:59
unlang_t const * instruction
The unlang node we're evaluating.
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
@ UNLANG_OP_FLAG_BREAK_POINT
Break point.
struct unlang_s unlang_t
unlang_type_t type
The specialisation of this node.
unlang_t * children
Children beneath this group.
Generic representation of a grouping.
An unlang operation.
A node in a graph of unlang_op_t (s) that we execute.
Our interpreter stack, as distinct from the C stack.
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition value.c:4065