/src/freeradius-server/src/lib/util/encode.c
Line | Count | Source |
1 | | /* |
2 | | * This library is free software; you can redistribute it and/or |
3 | | * modify it under the terms of the GNU Lesser General Public |
4 | | * License as published by the Free Software Foundation; either |
5 | | * version 2.1 of the License, or (at your option) any later version. |
6 | | * |
7 | | * This library 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 GNU |
10 | | * Lesser General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU Lesser General Public |
13 | | * License along with this library; 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: 033483b1b3f5bea8f237bd84c195616bc952af1c $ |
19 | | * |
20 | | * @file src/lib/util/encode.c |
21 | | * @brief Generic functions for decoding protocols. |
22 | | * |
23 | | * @copyright 2022 Network RADIUS SAS (legal@networkradius.com) |
24 | | */ |
25 | | #include <freeradius-devel/io/test_point.h> |
26 | | #include <freeradius-devel/util/proto.h> |
27 | | #include <freeradius-devel/util/encode.h> |
28 | | |
29 | | /** Encode an array of values from the network |
30 | | * |
31 | | * @param[out] dbuff buffer to write the TLV to. |
32 | | * @param[in] da_stack Describing nesting of options. |
33 | | * @param[in] depth in the da_stack. |
34 | | * @param[in,out] cursor Current attribute we're encoding. |
35 | | * @param[in] encode_ctx Containing DHCPv4 dictionary. |
36 | | * @param[in] encode_value Function to perform encoding of a single value. |
37 | | * @return |
38 | | * - >0 length of data encoded. |
39 | | * - 0 if we ran out of space. |
40 | | * - < 0 on error. |
41 | | */ |
42 | | ssize_t fr_pair_array_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, |
43 | | fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value) |
44 | 0 | { |
45 | 0 | ssize_t slen; |
46 | 0 | fr_dbuff_t work_dbuff = FR_DBUFF(dbuff); |
47 | 0 | fr_pair_t *vp; |
48 | 0 | fr_dict_attr_t const *da = da_stack->da[depth]; |
49 | |
|
50 | 0 | FR_PROTO_STACK_PRINT(da_stack, depth); |
51 | |
|
52 | 0 | if (!fr_cond_assert_msg(da->flags.array, |
53 | 0 | "%s: Internal sanity check failed, attribute \"%s\" does not have array bit set", |
54 | 0 | __FUNCTION__, da->name)) return PAIR_ENCODE_FATAL_ERROR; |
55 | | |
56 | 0 | while (fr_dbuff_extend(&work_dbuff)) { |
57 | 0 | fr_dbuff_t element_dbuff = FR_DBUFF(&work_dbuff); |
58 | |
|
59 | 0 | slen = encode_value(&element_dbuff, da_stack, depth, cursor, encode_ctx); |
60 | 0 | if (slen < 0) return slen; |
61 | | |
62 | 0 | fr_dbuff_set(&work_dbuff, &element_dbuff); |
63 | |
|
64 | 0 | vp = fr_dcursor_current(cursor); |
65 | 0 | if (!vp || (vp->da != da)) break; /* Stop if we have an attribute of a different type */ |
66 | 0 | } |
67 | | |
68 | 0 | return fr_dbuff_set(dbuff, &work_dbuff); |
69 | 0 | } |
70 | | |
71 | | ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, |
72 | | fr_da_stack_t *da_stack, unsigned int depth, |
73 | | fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair) |
74 | 0 | { |
75 | 0 | fr_dbuff_t work_dbuff = FR_DBUFF(dbuff); |
76 | 0 | fr_pair_t const *vp; |
77 | 0 | ssize_t len; |
78 | |
|
79 | 0 | while (true) { |
80 | 0 | FR_PROTO_STACK_PRINT(da_stack, depth); |
81 | |
|
82 | 0 | vp = fr_dcursor_current(cursor); |
83 | 0 | fr_assert(!vp->da->flags.internal); |
84 | |
|
85 | 0 | len = encode_pair(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx); |
86 | 0 | if (len < 0) return len; |
87 | | |
88 | | /* |
89 | | * If nothing updated the attribute, stop |
90 | | */ |
91 | 0 | if (!fr_dcursor_current(cursor) || (vp == fr_dcursor_current(cursor))) break; |
92 | | |
93 | 0 | vp = fr_dcursor_current(cursor); |
94 | 0 | if (!vp) break; |
95 | | |
96 | 0 | fr_proto_da_stack_build(da_stack, vp->da); |
97 | 0 | } |
98 | | |
99 | 0 | FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done cursor"); |
100 | |
|
101 | 0 | return fr_dbuff_set(dbuff, &work_dbuff); |
102 | 0 | } |
103 | | |
104 | | /** Encode a foreign reference to the network |
105 | | * |
106 | | * @param[out] dbuff buffer to write the TLV to. |
107 | | * @param[in] da_stack Describing nesting of options. |
108 | | * @param[in] depth in the da_stack. |
109 | | * @param[in,out] cursor Current attribute we're encoding. |
110 | | * @return |
111 | | * - >0 length of data encoded. |
112 | | * - 0 if we ran out of space. |
113 | | * - < 0 on error. |
114 | | */ |
115 | | ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, |
116 | | fr_dcursor_t *cursor) |
117 | 0 | { |
118 | 0 | ssize_t slen; |
119 | 0 | fr_dict_attr_t const *da; |
120 | 0 | fr_pair_t const *vp = fr_dcursor_current(cursor); |
121 | 0 | fr_dbuff_t work_dbuff = FR_DBUFF(dbuff); |
122 | |
|
123 | 0 | fr_dict_attr_t const *ref; |
124 | 0 | fr_dict_protocol_t const *proto; |
125 | |
|
126 | 0 | FR_PROTO_STACK_PRINT(da_stack, depth); |
127 | |
|
128 | 0 | da = da_stack->da[depth]; |
129 | 0 | fr_assert(da->type == FR_TYPE_GROUP); |
130 | |
|
131 | 0 | ref = fr_dict_attr_ref(da); |
132 | 0 | if (!ref) { |
133 | 0 | fr_strerror_printf("Invalid attribute reference for %s", da->name); |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | 0 | proto = fr_dict_protocol(ref->dict); |
138 | 0 | fr_assert(proto != NULL); |
139 | |
|
140 | 0 | if (!proto->encode) { |
141 | 0 | fr_strerror_printf("Attribute %s -> %s does not have an encoder", da->name, ref->name); |
142 | 0 | return 0; |
143 | 0 | } |
144 | | |
145 | | /* |
146 | | * The foreign functions don't take a cursor, so we have to update the cursor ourselves. |
147 | | */ |
148 | 0 | slen = proto->encode(&work_dbuff, &vp->vp_group); |
149 | 0 | if (slen < 0) return slen; |
150 | | |
151 | 0 | FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "group ref"); |
152 | |
|
153 | 0 | vp = fr_dcursor_next(cursor); |
154 | 0 | fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL); |
155 | |
|
156 | 0 | return fr_dbuff_set(dbuff, &work_dbuff); |
157 | 0 | } |
158 | | |
159 | | /** Generic encode value. |
160 | | * |
161 | | */ |
162 | | ssize_t fr_pair_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth, |
163 | | fr_dcursor_t *cursor, UNUSED void *encode_ctx) |
164 | 0 | { |
165 | 0 | fr_pair_t const *vp = fr_dcursor_current(cursor); |
166 | 0 | fr_dbuff_t work_dbuff = FR_DBUFF(dbuff); |
167 | |
|
168 | 0 | if (!fr_type_is_leaf(vp->vp_type)) { |
169 | 0 | FR_PROTO_TRACE("Cannot use generic encoder for data type %s", fr_type_to_str(vp->vp_type)); |
170 | 0 | fr_strerror_printf("Cannot encode data type %s", fr_type_to_str(vp->vp_type)); |
171 | 0 | return PAIR_ENCODE_FATAL_ERROR; |
172 | 0 | } |
173 | | |
174 | 0 | if (fr_value_box_to_network(&work_dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR; |
175 | | |
176 | 0 | (void) fr_dcursor_next(cursor); |
177 | |
|
178 | 0 | return fr_dbuff_set(dbuff, &work_dbuff); |
179 | 0 | } |