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: d409cbf1a830e224856b508c4a34211e9a5e6aa9 $")
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_dict_attr_t const **children; //!< Children of this attribute.
55
56DIAG_OFF(attributes)
57typedef enum CC_HINT(flag_enum) {
58 FR_DICT_ATTR_REF_NONE = 0x00, //!< No ref set.
59 FR_DICT_ATTR_REF_ALIAS = 0x01, //!< The attribute is an alias for another attribute.
60 ///< Either a straight ALIAS, or a pointer from FR_TYPE_GROUP
61 ///< into another dictionary.
62 FR_DICT_ATTR_REF_CLONE = 0x02, //!< The attribute is a "copy" of another attribute.
63 FR_DICT_ATTR_REF_ENUM = 0x04, //!< The attribute is an enumeration value.
64 FR_DICT_ATTR_REF_KEY = 0x08, //!< it is a UNION which has a ref to a key, and children.
65 FR_DICT_ATTR_REF_ROOT = 0x10, //!< only for FR_TYPE_ATTR, point to the default root for enums
66 FR_DICT_ATTR_REF_UNRESOLVED = 0x8000 //!< 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 - Holds enumeration values
93 *
94 */
95typedef struct {
96 size_t max_name_len; //!< maximum length of a name
97 fr_hash_table_t *value_by_name; //!< Lookup an enumeration value by name
98 fr_hash_table_t *name_by_value; //!< Lookup a name by value
100
101/** Attribute extension - Holds a hash table with the names of all children of this attribute
102 *
103 */
104typedef struct {
105 fr_hash_table_t *namespace; //!< Lookup a child by name
107
108/** @name Add extension structures to attributes
109 *
110 * @{
111 */
112
113/* Retrieve an extension structure for a dictionary attribute
114 *
115 * @param[in] da to retrieve structure from.
116 * @param[in] ext to retrieve.
117 * @return
118 * - NULL if the extension wasn't found.
119 * - A pointer to the start of the extension.
120 */
121static inline void *fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
122{
123 if (!da->ext[ext]) return NULL;
124
125 return fr_ext_ptr(da, da->ext[ext], fr_dict_attr_ext_def.info[ext].has_hdr);
126}
127
128/** Return whether a da has a given extension or not
129 *
130 * @param[in] da to check for extensions.
131 * @param[in] ext to check.
132 * @return
133 * - true if the da has the specified extension.
134 * - false if the da does not have the specified extension
135 */
137{
138 return (da->ext[ext] > 0);
139}
140
141/** Return the reference associated with a group type attribute
142 *
143 * @param[in] da to return the reference for.
144 * @return
145 * - NULL if no reference available.
146 * - A pointer to the attribute being referenced.
147 */
148static inline fr_dict_attr_t const *fr_dict_attr_ref(fr_dict_attr_t const *da)
149{
151
153 if (!ext) return NULL;
154
155 /*
156 * Unresolve refs aren't valid refs...
157 */
158 if (fr_dict_attr_ref_is_unresolved(ext->type)) return NULL;
159
160 /*
161 * Temporary backwards compatibility...
162 */
163 if (ext->type != FR_DICT_ATTR_REF_ALIAS) return NULL;
164
165 return ext->ref;
166}
167
168/** Return the vendor number for an attribute
169 *
170 * @param[in] da The dictionary attribute to find the
171 * vendor for.
172 * @return
173 * - 0 this isn't a vendor specific attribute.
174 * - The vendor PEN.
175 */
177{
179
180 if (da->type == FR_TYPE_VENDOR) return da->attr;
181
183 if (!ext || !ext->vendor) return 0;
184
185 return ext->vendor->attr;
186}
187
188/** Return the vendor da for an attribute
189 *
190 * @param[in] da The dictionary attribute to find the
191 * vendor for.
192 * @return
193 * - 0 this isn't a vendor specific attribute.
194 * - The vendor PEN.
195 */
197{
199
200 if (da->type == FR_TYPE_VENDOR) return da;
201
203 if (!ext) return NULL;
204
205 return ext->vendor;
206}
207
208/* Retrieve an extension structure for a dictionary enum
209 *
210 * @param[in] enumv to retrieve structure from.
211 * @param[in] ext to retrieve.
212 * @return
213 * - NULL if the extension wasn't found.
214 * - A pointer to the start of the extension.
215 */
216static inline void *fr_dict_enum_ext(fr_dict_enum_value_t const *enumv, fr_dict_enum_ext_t ext)
217{
218 if (!enumv->ext[ext]) return NULL;
219
220 return fr_ext_ptr(enumv, enumv->ext[ext], fr_dict_enum_ext_def.info[ext].has_hdr);
221}
222
223/** Return the attribute reference associated with an enum
224 *
225 * @param[in] enumv to return the reference for.
226 * @return
227 * - NULL if no reference available.
228 * - A pointer to the attribute being referenced.
229 */
231{
233
235 if (!ref) return NULL;
236
237 return ref->da;
238}
239
240
241/** @} */
242
244
245#ifdef __cplusplus
246}
247#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:239
@ FR_DICT_ENUM_EXT_ATTR_REF
Reference to a child attribute associated with this key value.
Definition dict.h:240
uint8_t ext[FR_DICT_ENUM_EXT_MAX]
Extensions to the dictionary attribute.
Definition dict.h:261
fr_dict_attr_ext_t
Extension identifier.
Definition dict.h:183
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition dict.h:186
@ FR_DICT_ATTR_EXT_VENDOR
Cached vendor pointer.
Definition dict.h:189
fr_dict_attr_t const * da
the child structure referenced by this value of key
Definition dict.h:248
Enum extension - Sub-struct or union pointer.
Definition dict.h:247
Value of an enumerated attribute.
Definition dict.h:255
static void * fr_dict_enum_ext(fr_dict_enum_value_t const *enumv, fr_dict_enum_ext_t ext)
Definition dict_ext.h:216
fr_ext_t const fr_dict_attr_ext_def
Holds additional information about extension structures.
Definition dict_ext.c:246
size_t max_name_len
maximum length of a name
Definition dict_ext.h:96
fr_ext_t const fr_dict_enum_ext_def
Holds additional information about extension structures.
Definition dict_ext.c:328
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:98
fr_dict_attr_t const ** children
Children of this attribute.
Definition dict_ext.h:53
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:121
#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:97
void fr_dict_attr_ext_debug(fr_dict_attr_t const *da)
Definition dict_ext.c:320
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:148
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:136
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:230
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:196
fr_dict_attr_ref_type_t
Definition dict_ext.h:57
@ FR_DICT_ATTR_REF_ENUM
The attribute is an enumeration value.
Definition dict_ext.h:63
@ FR_DICT_ATTR_REF_ROOT
only for FR_TYPE_ATTR, point to the default root for enums
Definition dict_ext.h:65
@ FR_DICT_ATTR_REF_KEY
it is a UNION which has a ref to a key, and children.
Definition dict_ext.h:64
@ FR_DICT_ATTR_REF_ALIAS
The attribute is an alias for another attribute.
Definition dict_ext.h:59
@ FR_DICT_ATTR_REF_CLONE
The attribute is a "copy" of another attribute.
Definition dict_ext.h:62
@ 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:58
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:176
Attribute extension - Holds children for an attribute.
Definition dict_ext.h:52
Attribute extension - Holds enumeration values.
Definition dict_ext.h:95
Attribute extension - Holds a hash table with the names of all children of this attribute.
Definition dict_ext.h:104
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