The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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  */
25 RCSIDH(dict_ext_h, "$Id: 46cd3c5e429a747fd89c6c658516a323ec15a0a1 $")
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
34 extern "C" {
35 #endif
36 
37 extern fr_ext_t const fr_dict_attr_ext_def;
38 extern fr_ext_t const fr_dict_enum_ext_def;
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  */
52 typedef 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 
57 DIAG_OFF(attributes)
58 typedef 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_UNRESOLVED = 0x10 //!< This flag is combined with the other states to indicate
66  ///< that the reference is unresolved.
68 DIAG_ON(attributes)
69 
70 #define fr_dict_attr_ref_is_unresolved(_type) ((_type) & FR_DICT_ATTR_REF_UNRESOLVED)
71 #define fr_dict_attr_ref_type(_type) ((_type) & ~FR_DICT_ATTR_REF_UNRESOLVED)
72 
73 /** Attribute extension - Holds a reference to an attribute in another dictionary
74  *
75  */
76 typedef struct {
77  fr_dict_attr_ref_type_t type; //!< The state of the reference.
78  union {
79  fr_dict_attr_t const *ref; //!< A resolved pointer to the referenced attribute.
80  char *unresolved; //!< An unresolved reference (will need resolving later).
81  };
83 
84 /** Attribute extension - Cached vendor pointer
85  *
86  */
87 typedef struct {
88  fr_dict_attr_t const *vendor; //!< ancestor which has type #FR_TYPE_VENDOR
90 
91 /** Attribute extension - Stack of dictionary attributes that describe the path back to the root of the dictionary
92  *
93  */
94 typedef struct {
95  bool unused; //!< Zero length arrays are apparently GNU extensions
96  ///< and we're not allowed to have structs with a
97  ///< single variable array as its member.
98  ///< We'll likely want to store something else here
99  ///< at some point, so we just have a dummy field to
100  ///< avoid changing all the code.
101  fr_dict_attr_t const *da_stack[]; //!< Stack of dictionary attributes
103 
104 /** Attribute extension - Holds enumeration values
105  *
106  */
107 typedef struct {
108  size_t max_name_len; //!< maximum length of a name
109  fr_hash_table_t *value_by_name; //!< Lookup an enumeration value by name
110  fr_hash_table_t *name_by_value; //!< Lookup a name by value
112 
113 /** Attribute extension - Holds a hash table with the names of all children of this attribute
114  *
115  */
116 typedef struct {
117  fr_hash_table_t *namespace; //!< Lookup a child by name
119 
120 /** Enum extension - Sub-struct or union pointer
121  *
122  */
123 typedef struct {
124  fr_dict_attr_t const *union_ref; //!< The union da this value points into.
126 
127 /** @name Add extension structures to attributes
128  *
129  * @{
130  */
131 
132 /* Retrieve an extension structure for a dictionary attribute
133  *
134  * @param[in] da to retrieve structure from.
135  * @param[in] ext to retrieve.
136  * @return
137  * - NULL if the extension wasn't found.
138  * - A pointer to the start of the extension.
139  */
140 static inline void *fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
141 {
142  if (!da->ext[ext]) return NULL;
143 
144  return fr_ext_ptr(da, da->ext[ext], fr_dict_attr_ext_def.info[ext].has_hdr);
145 }
146 
147 /** Return whether a da has a given extension or not
148  *
149  * @param[in] da to check for extensions.
150  * @param[in] ext to check.
151  * @return
152  * - true if the da has the specified extension.
153  * - false if the da does not have the specified extension
154  */
155 static inline bool fr_dict_attr_has_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
156 {
157  return (da->ext[ext] > 0);
158 }
159 
160 /** Return the cached da stack (if any) associated with an attribute
161  *
162  * @param[in] da to return cached da stack for.
163  * @return
164  * - NULL if no da stack available.
165  * - The cached da stack on success.
166  */
167 static inline fr_dict_attr_t const **fr_dict_attr_da_stack(fr_dict_attr_t const *da)
168 {
170 
172  if (!ext) return NULL;
173 
174  return ext->da_stack;
175 }
176 
177 /** Return the reference associated with a group type attribute
178  *
179  * @param[in] da to return the reference for.
180  * @return
181  * - NULL if no reference available.
182  * - A pointer to the attribute being referenced.
183  */
184 static inline fr_dict_attr_t const *fr_dict_attr_ref(fr_dict_attr_t const *da)
185 {
187 
189  if (!ext) return NULL;
190 
191  /*
192  * Unresolve refs aren't valid refs...
193  */
194  if (fr_dict_attr_ref_is_unresolved(ext->type)) return NULL;
195 
196  /*
197  * Temporary backwards compatibility...
198  */
199  if (ext->type != FR_DICT_ATTR_REF_ALIAS) return NULL;
200 
201  return ext->ref;
202 }
203 
204 /** Return the vendor number for an attribute
205  *
206  * @param[in] da The dictionary attribute to find the
207  * vendor for.
208  * @return
209  * - 0 this isn't a vendor specific attribute.
210  * - The vendor PEN.
211  */
213 {
215 
216  if (da->type == FR_TYPE_VENDOR) return da->attr;
217 
219  if (!ext || !ext->vendor) return 0;
220 
221  return ext->vendor->attr;
222 }
223 
224 /** Return the vendor da for an attribute
225  *
226  * @param[in] da The dictionary attribute to find the
227  * vendor for.
228  * @return
229  * - 0 this isn't a vendor specific attribute.
230  * - The vendor PEN.
231  */
233 {
235 
236  if (da->type == FR_TYPE_VENDOR) return da;
237 
239  if (!ext) return NULL;
240 
241  return ext->vendor;
242 }
243 
244 /** @} */
245 
246 void fr_dict_attr_ext_debug(fr_dict_attr_t const *da);
247 
248 #ifdef __cplusplus
249 }
250 #endif
#define DIAG_ON(_x)
Definition: build.h:456
#define RCSIDH(h, id)
Definition: build.h:482
#define DIAG_OFF(_x)
Definition: build.h:455
fr_dict_attr_ext_t
Extension identifier.
Definition: dict.h:161
@ FR_DICT_ATTR_EXT_DA_STACK
Cached da stack.
Definition: dict.h:167
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition: dict.h:164
@ FR_DICT_ATTR_EXT_VENDOR
Cached vendor pointer.
Definition: dict.h:166
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:232
fr_ext_t const fr_dict_attr_ext_def
Holds additional information about extension structures.
Definition: dict_ext.c:213
size_t max_name_len
maximum length of a name
Definition: dict_ext.h:108
fr_ext_t const fr_dict_enum_ext_def
Holds additional information about extension structures.
Definition: dict_ext.c:275
fr_dict_attr_t const * vendor
ancestor which has type FR_TYPE_VENDOR
Definition: dict_ext.h:88
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition: dict_ext.h:140
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:167
fr_dict_attr_ref_type_t type
The state of the reference.
Definition: dict_ext.h:77
fr_hash_table_t * name_by_value
Lookup a name by value.
Definition: dict_ext.h:110
fr_dict_attr_t const ** children
Children of this attribute.
Definition: dict_ext.h:54
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:184
#define fr_dict_attr_ref_is_unresolved(_type)
Definition: dict_ext.h:70
fr_hash_table_t * value_by_name
Lookup an enumeration value by name.
Definition: dict_ext.h:109
void fr_dict_attr_ext_debug(fr_dict_attr_t const *da)
Definition: dict_ext.c:267
fr_dict_attr_t const * da_stack[]
Stack of dictionary attributes.
Definition: dict_ext.h:101
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:95
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:155
fr_dict_attr_t const * union_ref
The union da this value points into.
Definition: dict_ext.h:124
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_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:65
@ 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:212
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:94
Attribute extension - Holds enumeration values.
Definition: dict_ext.h:107
Attribute extension - Holds a hash table with the names of all children of this attribute.
Definition: dict_ext.h:116
Attribute extension - Holds a reference to an attribute in another dictionary.
Definition: dict_ext.h:76
Attribute extension - Cached vendor pointer.
Definition: dict_ext.h:87
Enum extension - Sub-struct or union pointer.
Definition: dict_ext.h:123
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
bool has_hdr
Additional metadata should be allocated before the extension data to record the exact length of the e...
Definition: ext.h:113
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.
Definition: merged_model.c:122
unsigned int uint32_t
Definition: merged_model.c:33