The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
dict_ext.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/** Multi-protocol AVP dictionary API
19 *
20 * @file src/lib/util/dict_ext.h
21 *
22 * @copyright 2020 The FreeRADIUS server project
23 * @copyright 2020,2024 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
24 */
25RCSIDH(dict_ext_h, "$Id: f4bf0dd8a8dc6ec734a62e99df4c9d681fd6bd6d $")
26
27#include <freeradius-devel/util/dict.h>
28#include <freeradius-devel/util/ext.h>
29#include <freeradius-devel/util/hash.h>
30
31#include <limits.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
39
40/** Attribute extension - Holds children for an attribute
41 *
42 * Children are possible for:
43 *
44 * #FR_TYPE_TLV, #FR_TYPE_VENDOR, #FR_TYPE_VSA, #FR_TYPE_STRUCT
45 *
46 * *or* where the parent->parent->type is
47 * #FR_TYPE_STRUCT, and "parent" is a "key"
48 * field. Note that these attributes therefore
49 * cannot have VALUEs, as the child defines their
50 * VALUE. See dict_attr_can_have_children() for details.
51 */
52typedef struct {
53 fr_hash_table_t *child_by_name; //!< Namespace at this level in the hierarchy.
54 fr_dict_attr_t const **children; //!< Children of this attribute.
56
57DIAG_OFF(attributes)
58typedef enum CC_HINT(flag_enum) {
59 FR_DICT_ATTR_REF_NONE = 0x00, //!< No ref set.
60 FR_DICT_ATTR_REF_ALIAS = 0x01, //!< The attribute is an alias for another attribute.
61 ///< Either a straight ALIAS, or a reference into another
62 ///< dictionary.
63 FR_DICT_ATTR_REF_CLONE = 0x02, //!< The attribute is a "copy" of another attribute.
64 FR_DICT_ATTR_REF_ENUM = 0x04, //!< The attribute is an enumeration value.
65 FR_DICT_ATTR_REF_KEY = 0x08, //!< it is a UNION which has a ref to a key, and children.
66 FR_DICT_ATTR_REF_UNRESOLVED = 0x10 //!< This flag is combined with the other states to indicate
67 ///< that the reference is unresolved.
69DIAG_ON(attributes)
70
71#define fr_dict_attr_ref_is_unresolved(_type) ((_type) & FR_DICT_ATTR_REF_UNRESOLVED)
72#define fr_dict_attr_ref_type(_type) ((_type) & ~FR_DICT_ATTR_REF_UNRESOLVED)
73
74/** Attribute extension - Holds a reference to an attribute in another dictionary
75 *
76 */
77typedef struct {
78 fr_dict_attr_ref_type_t type; //!< The state of the reference.
79 union {
80 fr_dict_attr_t const *ref; //!< A resolved pointer to the referenced attribute.
81 char *unresolved; //!< An unresolved reference (will need resolving later).
82 };
84
85/** Attribute extension - Cached vendor pointer
86 *
87 */
88typedef struct {
89 fr_dict_attr_t const *vendor; //!< ancestor which has type #FR_TYPE_VENDOR
91
92/** Attribute extension - Stack of dictionary attributes that describe the path back to the root of the dictionary
93 *
94 */
95typedef struct {
96 bool unused; //!< Zero length arrays are apparently GNU extensions
97 ///< and we're not allowed to have structs with a
98 ///< single variable array as its member.
99 ///< We'll likely want to store something else here
100 ///< at some point, so we just have a dummy field to
101 ///< avoid changing all the code.
102 fr_dict_attr_t const *da_stack[]; //!< Stack of dictionary attributes
104
105/** Attribute extension - Holds enumeration values
106 *
107 */
108typedef struct {
109 size_t max_name_len; //!< maximum length of a name
110 fr_hash_table_t *value_by_name; //!< Lookup an enumeration value by name
111 fr_hash_table_t *name_by_value; //!< Lookup a name by value
113
114/** Attribute extension - Holds a hash table with the names of all children of this attribute
115 *
116 */
117typedef struct {
118 fr_hash_table_t *namespace; //!< Lookup a child by name
120
121/** @name Add extension structures to attributes
122 *
123 * @{
124 */
125
126/* Retrieve an extension structure for a dictionary attribute
127 *
128 * @param[in] da to retrieve structure from.
129 * @param[in] ext to retrieve.
130 * @return
131 * - NULL if the extension wasn't found.
132 * - A pointer to the start of the extension.
133 */
134static inline void *fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
135{
136 if (!da->ext[ext]) return NULL;
137
138 return fr_ext_ptr(da, da->ext[ext], fr_dict_attr_ext_def.info[ext].has_hdr);
139}
140
141/** Return whether a da has a given extension or not
142 *
143 * @param[in] da to check for extensions.
144 * @param[in] ext to check.
145 * @return
146 * - true if the da has the specified extension.
147 * - false if the da does not have the specified extension
148 */
150{
151 return (da->ext[ext] > 0);
152}
153
154/** Return the cached da stack (if any) associated with an attribute
155 *
156 * @param[in] da to return cached da stack for.
157 * @return
158 * - NULL if no da stack available.
159 * - The cached da stack on success.
160 */
162{
164
166 if (!ext) return NULL;
167
168 return ext->da_stack;
169}
170
171/** Return the reference associated with a group type attribute
172 *
173 * @param[in] da to return the reference for.
174 * @return
175 * - NULL if no reference available.
176 * - A pointer to the attribute being referenced.
177 */
178static inline fr_dict_attr_t const *fr_dict_attr_ref(fr_dict_attr_t const *da)
179{
181
183 if (!ext) return NULL;
184
185 /*
186 * Unresolve refs aren't valid refs...
187 */
188 if (fr_dict_attr_ref_is_unresolved(ext->type)) return NULL;
189
190 /*
191 * Temporary backwards compatibility...
192 */
193 if (ext->type != FR_DICT_ATTR_REF_ALIAS) return NULL;
194
195 return ext->ref;
196}
197
198/** Return the vendor number for an attribute
199 *
200 * @param[in] da The dictionary attribute to find the
201 * vendor for.
202 * @return
203 * - 0 this isn't a vendor specific attribute.
204 * - The vendor PEN.
205 */
207{
209
210 if (da->type == FR_TYPE_VENDOR) return da->attr;
211
213 if (!ext || !ext->vendor) return 0;
214
215 return ext->vendor->attr;
216}
217
218/** Return the vendor da for an attribute
219 *
220 * @param[in] da The dictionary attribute to find the
221 * vendor for.
222 * @return
223 * - 0 this isn't a vendor specific attribute.
224 * - The vendor PEN.
225 */
227{
229
230 if (da->type == FR_TYPE_VENDOR) return da;
231
233 if (!ext) return NULL;
234
235 return ext->vendor;
236}
237
238/* Retrieve an extension structure for a dictionary enum
239 *
240 * @param[in] enumv to retrieve structure from.
241 * @param[in] ext to retrieve.
242 * @return
243 * - NULL if the extension wasn't found.
244 * - A pointer to the start of the extension.
245 */
246static inline void *fr_dict_enum_ext(fr_dict_enum_value_t const *enumv, fr_dict_enum_ext_t ext)
247{
248 if (!enumv->ext[ext]) return NULL;
249
250 return fr_ext_ptr(enumv, enumv->ext[ext], fr_dict_enum_ext_def.info[ext].has_hdr);
251}
252
253/** Return the attribute reference associated with an enum
254 *
255 * @param[in] enumv to return the reference for.
256 * @return
257 * - NULL if no reference available.
258 * - A pointer to the attribute being referenced.
259 */
261{
263
265 if (!ref) return NULL;
266
267 return ref->da;
268}
269
270
271/** @} */
272
274
275#ifdef __cplusplus
276}
277#endif
#define DIAG_ON(_x)
Definition build.h:460
#define RCSIDH(h, id)
Definition build.h:486
#define DIAG_OFF(_x)
Definition build.h:459
fr_dict_enum_ext_t
Extension identifier.
Definition dict.h:238
@ FR_DICT_ENUM_EXT_ATTR_REF
Reference to a child attribute associated with this key value.
Definition dict.h:239
uint8_t ext[FR_DICT_ENUM_EXT_MAX]
Extensions to the dictionary attribute.
Definition dict.h:260
fr_dict_attr_ext_t
Extension identifier.
Definition dict.h:181
@ FR_DICT_ATTR_EXT_DA_STACK
Cached da stack.
Definition dict.h:188
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition dict.h:184
@ FR_DICT_ATTR_EXT_VENDOR
Cached vendor pointer.
Definition dict.h:187
fr_dict_attr_t const * da
the child structure referenced by this value of key
Definition dict.h:247
Enum extension - Sub-struct or union pointer.
Definition dict.h:246
Value of an enumerated attribute.
Definition dict.h:254
static void * fr_dict_enum_ext(fr_dict_enum_value_t const *enumv, fr_dict_enum_ext_t ext)
Definition dict_ext.h:246
fr_ext_t const fr_dict_attr_ext_def
Holds additional information about extension structures.
Definition dict_ext.c:262
size_t max_name_len
maximum length of a name
Definition dict_ext.h:109
fr_ext_t const fr_dict_enum_ext_def
Holds additional information about extension structures.
Definition dict_ext.c:349
fr_dict_attr_t const * vendor
ancestor which has type FR_TYPE_VENDOR
Definition dict_ext.h:89
fr_dict_attr_ref_type_t type
The state of the reference.
Definition dict_ext.h:78
fr_hash_table_t * name_by_value
Lookup a name by value.
Definition dict_ext.h:111
fr_dict_attr_t const ** children
Children of this attribute.
Definition dict_ext.h:54
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:134
#define fr_dict_attr_ref_is_unresolved(_type)
Definition dict_ext.h:71
fr_hash_table_t * value_by_name
Lookup an enumeration value by name.
Definition dict_ext.h:110
void fr_dict_attr_ext_debug(fr_dict_attr_t const *da)
Definition dict_ext.c:341
fr_dict_attr_t const * da_stack[]
Stack of dictionary attributes.
Definition dict_ext.h:102
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:178
fr_hash_table_t * child_by_name
Namespace at this level in the hierarchy.
Definition dict_ext.h:53
bool unused
Zero length arrays are apparently GNU extensions and we're not allowed to have structs with a single ...
Definition dict_ext.h:96
static bool fr_dict_attr_has_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Return whether a da has a given extension or not.
Definition dict_ext.h:149
static fr_dict_attr_t const * fr_dict_enum_attr_ref(fr_dict_enum_value_t const *enumv)
Return the attribute reference associated with an enum.
Definition dict_ext.h:260
static fr_dict_attr_t const ** fr_dict_attr_da_stack(fr_dict_attr_t const *da)
Return the cached da stack (if any) associated with an attribute.
Definition dict_ext.h:161
static fr_dict_attr_t const * fr_dict_vendor_da_by_da(fr_dict_attr_t const *da)
Return the vendor da for an attribute.
Definition dict_ext.h:226
fr_dict_attr_ref_type_t
Definition dict_ext.h:58
@ FR_DICT_ATTR_REF_ENUM
The attribute is an enumeration value.
Definition dict_ext.h:64
@ FR_DICT_ATTR_REF_KEY
it is a UNION which has a ref to a key, and children.
Definition dict_ext.h:65
@ FR_DICT_ATTR_REF_ALIAS
The attribute is an alias for another attribute.
Definition dict_ext.h:60
@ FR_DICT_ATTR_REF_CLONE
The attribute is a "copy" of another attribute.
Definition dict_ext.h:63
@ FR_DICT_ATTR_REF_UNRESOLVED
This flag is combined with the other states to indicate that the reference is unresolved.
Definition dict_ext.h:66
@ FR_DICT_ATTR_REF_NONE
No ref set.
Definition dict_ext.h:59
static uint32_t fr_dict_vendor_num_by_da(fr_dict_attr_t const *da)
Return the vendor number for an attribute.
Definition dict_ext.h:206
Attribute extension - Holds children for an attribute.
Definition dict_ext.h:52
Attribute extension - Stack of dictionary attributes that describe the path back to the root of the d...
Definition dict_ext.h:95
Attribute extension - Holds enumeration values.
Definition dict_ext.h:108
Attribute extension - Holds a hash table with the names of all children of this attribute.
Definition dict_ext.h:117
Attribute extension - Holds a reference to an attribute in another dictionary.
Definition dict_ext.h:77
Attribute extension - Cached vendor pointer.
Definition dict_ext.h:88
bool has_hdr
Additional metadata should be allocated before the extension data to record the exact length of the e...
Definition ext.h:113
static void * fr_ext_ptr(TALLOC_CTX const *chunk, size_t offset, bool has_hdr)
Return a pointer to an extension in a chunk.
Definition ext.h:150
fr_ext_info_t const * info
Additional information about each extension.
Definition ext.h:131
Structure to define a set of extensions.
Definition ext.h:126
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
unsigned int uint32_t