The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dict_ext_priv.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 /** Extensions for dictionary definitions
19  *
20  * @file src/lib/util/dict_ext_priv.h
21  *
22  * @copyright 2020 The FreeRADIUS server project
23  * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
24  */
25 RCSIDH(dict_ext_priv_h, "$Id: aa8d3b59c68c7034866feb6b405f98f3b9b78370 $")
26 
27 #include <freeradius-devel/util/dict.h>
28 #include <limits.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /** @name Add extension structures to attributes
35  *
36  * @{
37  */
38 
39 /** Allocate an attribute extension of a particular size
40  *
41  */
42 static inline void *dict_attr_ext_alloc_size(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext, size_t ext_len)
43 {
44  if (!(*da_p)->flags.is_unknown && unlikely((*da_p)->dict && fr_dict_is_read_only((*da_p)->dict))) {
45  fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root((*da_p)->dict)->name);
46  return NULL;
47  }
48 
49  return fr_ext_alloc_size(&fr_dict_attr_ext_def, (void **)da_p, ext, ext_len);
50 }
51 
52 /** Allocate an attribute extension
53  *
54  */
55 static inline void *dict_attr_ext_alloc(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext)
56 {
57  if (!(*da_p)->flags.is_unknown && unlikely((*da_p)->dict && fr_dict_is_read_only((*da_p)->dict))) {
58  fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root((*da_p)->dict)->name);
59  return NULL;
60  }
61 
62  return fr_ext_alloc_size(&fr_dict_attr_ext_def, (void **)da_p, ext, fr_dict_attr_ext_def.info[ext].min);
63 }
64 
65 /** Return the length of an attribute extension
66  *
67  */
68 static inline size_t dict_attr_ext_len(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
69 {
70  return fr_ext_len(&fr_dict_attr_ext_def, (void const *)da, ext);
71 }
72 
73 /** Copy a single attribute extension from one attribute to another
74  *
75  */
76 static inline void *dict_attr_ext_copy(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in, fr_dict_attr_ext_t ext)
77 {
78  if (unlikely((*da_out_p)->dict && fr_dict_is_read_only((*da_out_p)->dict))) {
79  fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root((*da_out_p)->dict)->name);
80  return NULL;
81  }
82 
83  return fr_ext_copy(&fr_dict_attr_ext_def, (void **)da_out_p, (void const *)da_in, ext);
84 }
85 
86 /** Copy all attribute extensions from one attribute to another
87  *
88  */
89 static inline int dict_attr_ext_copy_all(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in)
90 {
91  if (unlikely((*da_out_p)->dict && fr_dict_is_read_only((*da_out_p)->dict))) {
92  fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root((*da_out_p)->dict)->name);
93  return -1;
94  }
95 
96  return fr_ext_copy_all(&fr_dict_attr_ext_def, (void **)da_out_p, (void const *)da_in);
97 }
98 
99 /** Print extension debug information for attributes
100  *
101  */
102 static inline void dict_attr_ext_debug(char const *name, fr_dict_attr_t const *da)
103 {
105 }
106 /** @} */
107 
108 /** @name Convenience functions for populating attribute extensions
109  *
110  * @{
111  */
112 static inline int dict_attr_ref_set(fr_dict_attr_t const *da, fr_dict_attr_t const *ref)
113 {
115 
117  if (unlikely(!ext)) {
118  fr_strerror_printf("%s (%s) contains no 'ref' extension", da->name,
119  fr_type_to_str(da->type));
120  return -1;
121  }
122  ext->ref = ref;
123 
124  return 0;
125 }
126 
127 static inline int dict_attr_children_set(fr_dict_attr_t const *da, fr_dict_attr_t const **children)
128 {
130 
132  if (unlikely(!ext)) {
133  fr_strerror_printf("%s (%s) contains no 'children' extension", da->name,
134  fr_type_to_str(da->type));
135  return -1;
136  }
137  ext->children = children;
138 
139  return 0;
140 }
141 
142 static inline fr_dict_attr_t const **dict_attr_children(fr_dict_attr_t const *da)
143 {
145 
147  if (unlikely(!ext)) {
148  fr_strerror_printf("%s (%s) contains no 'children' extension", da->name,
149  fr_type_to_str(da->type));
150  return NULL;
151  }
152  return ext->children;
153 }
154 
155 /** Return the namespace hash table associated with the attribute
156  *
157  * @param[in] da to return the reference for.
158  * @return
159  * - NULL if no namespace available.
160  * - A pointer to the namespace hash table
161  */
163 {
164  fr_dict_attr_t const *ref;
166 
167  ref = fr_dict_attr_ref(da);
168  if (unlikely(ref != NULL)) return NULL;
169 
171  if (!ext) return NULL;
172 
173  return ext->namespace;
174 }
175 /** @} */
176 
177 #ifdef __cplusplus
178 }
179 #endif
#define RCSIDH(h, id)
Definition: build.h:445
#define unlikely(_x)
Definition: build.h:378
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition: dict_util.c:1997
bool fr_dict_is_read_only(fr_dict_t const *dict)
Definition: dict_util.c:2002
fr_dict_attr_ext_t
Extension identifier.
Definition: dict.h:159
@ FR_DICT_ATTR_EXT_NAMESPACE
Attribute has its own namespace.
Definition: dict.h:167
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition: dict.h:162
@ FR_DICT_ATTR_EXT_CHILDREN
Attribute has children.
Definition: dict.h:161
fr_ext_t const fr_dict_attr_ext_def
Holds additional information about extension structures.
Definition: dict_ext.c:160
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition: dict_ext.h:120
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:164
fr_dict_attr_t const * ref
reference, only for FR_TYPE_GROUP
Definition: dict_ext.h:61
Attribute extension - Holds children for an attribute.
Definition: dict_ext.h:52
Attribute extension - Holds a hash table with the names of all children of this attribute.
Definition: dict_ext.h:96
Attribute extension - Holds a reference to an attribute in another dictionary.
Definition: dict_ext.h:60
static int dict_attr_ref_set(fr_dict_attr_t const *da, fr_dict_attr_t const *ref)
static int dict_attr_children_set(fr_dict_attr_t const *da, fr_dict_attr_t const **children)
static void * dict_attr_ext_alloc_size(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext, size_t ext_len)
Allocate an attribute extension of a particular size.
Definition: dict_ext_priv.h:42
static void * dict_attr_ext_alloc(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext)
Allocate an attribute extension.
Definition: dict_ext_priv.h:55
static fr_hash_table_t * dict_attr_namespace(fr_dict_attr_t const *da)
Return the namespace hash table associated with the attribute.
static size_t dict_attr_ext_len(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Return the length of an attribute extension.
Definition: dict_ext_priv.h:68
static int dict_attr_ext_copy_all(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in)
Copy all attribute extensions from one attribute to another.
Definition: dict_ext_priv.h:89
static void * dict_attr_ext_copy(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in, fr_dict_attr_ext_t ext)
Copy a single attribute extension from one attribute to another.
Definition: dict_ext_priv.h:76
static fr_dict_attr_t const ** dict_attr_children(fr_dict_attr_t const *da)
static void dict_attr_ext_debug(char const *name, fr_dict_attr_t const *da)
Print extension debug information for attributes.
void * fr_ext_alloc_size(fr_ext_t const *def, void **chunk_p, int ext, size_t ext_len)
Add a variable length extension to a talloc chunk.
Definition: ext.c:55
size_t fr_ext_len(fr_ext_t const *def, TALLOC_CTX const *chunk, int ext)
Return the length of an extension.
Definition: ext.c:128
void fr_ext_debug(fr_ext_t const *def, char const *name, void const *chunk)
Print out all extensions and hexdump their contents.
Definition: ext.c:336
void * fr_ext_copy(fr_ext_t const *def, TALLOC_CTX **chunk_dst, TALLOC_CTX const *chunk_src, int ext)
Copy extension data from one attribute to another.
Definition: ext.c:160
int fr_ext_copy_all(fr_ext_t const *def, TALLOC_CTX **chunk_dst, TALLOC_CTX const *chunk_src)
Copy all the extensions from one attribute to another.
Definition: ext.c:245
size_t min
Minimum size of extension.
Definition: ext.h:112
fr_ext_info_t const * info
Additional information about each extension.
Definition: ext.h:131
static char const * name
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition: strerror.h:64
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition: types.h:433