The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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 /** 'compositing' using talloc structures
19  *
20  * These allow multiple variable length memory areas to be appended to
21  * talloced structures. Extensions can either contain a header in which
22  * case the exact length is recorded, or they can be of a fixed size.
23  *
24  * The structure being extended must be padded to a multiple of FR_EXT_ALIGNMENT.
25  * i.e. CC_HINT(aligned(FR_EXT_ALIGNMENT)).
26  *
27  * It is strongly recommended that extended structures are allocated in a
28  * talloc_pool() to avoid the overhead of multiple reallocs.
29  *
30  * @file src/lib/util/ext.h
31  *
32  * @copyright 2020 The FreeRADIUS server project
33  * @copyright 2020 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
34  */
35 RCSIDH(ext_h, "$Id: 8f485ee92039de1ea8fb6df5deaf0d960e8544e1 $")
36 
37 #include <freeradius-devel/util/table.h>
38 #include <limits.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /** The alignment of object extension structures
45  *
46  */
47 #ifdef __WORD_SIZE
48 # if __WORD_SIZE < 4
49 # define FR_EXT_ALIGNMENT sizeof(uint32_t)
50 # else
51 # define FR_EXT_ALIGNMENT __WORD_SIZE /* From limits.h */
52 # endif
53 #else
54 # define FR_EXT_ALIGNMENT sizeof(uint64_t)
55 #endif
56 
57 typedef struct fr_ext_s fr_ext_t;
58 
59 /** Function for pre-allocating extension memory for extensions before they're copied
60  *
61  * @param[in] def Extension definitions.
62  * @param[in,out] dst_chunk_p to add extensions to.
63  * @param[in] ext that's being copied.
64  * @param[in] src_ext_ptr Pointer for the src extension.
65  * @param[in] src_ext_len Length of the src extension.
66  * @return
67  * - NULL on error.
68  * - Pointer to the new extension on success.
69  */
70 typedef void *(* fr_ext_alloc_t)(fr_ext_t const *def, TALLOC_CTX **dst_chunk_p,
71  int ext, void *src_ext_ptr, size_t src_ext_len);
72 
73 /** Function for re-populating extensions after they're copied
74  *
75  * @param[in] ext that's being copied.
76  * @param[in] dst_chunk Talloc chunk we're copying to.
77  * @param[in] dst_ext_ptr Pointer to the dst extension to populate.
78  * @param[in] dst_ext_len The length of the dst extension.
79  * @param[in] src_chunk Talloc chunk we're copying from.
80  * @param[in] src_ext_ptr Pointer for the src extension.
81  * @param[in] src_ext_len Length of the src extension.
82  * @return
83  * - NULL on error.
84  * - Pointer to the new extension on success.
85  */
86 typedef int (* fr_ext_copy_t)(int ext,
87  TALLOC_CTX *dst_chunk,
88  void *dst_ext_ptr, size_t dst_ext_len,
89  TALLOC_CTX const *src_chunk,
90  void *src_ext_ptr, size_t src_ext_len);
91 
92 /** Function for re-establishing internal consistency on realloc
93  *
94  * In some cases the chunk may cache a pointer to an extension.
95  * On realloc this pointer may be invalidated. This provides a
96  * callback to fixup consistency issues after a realloc.
97  *
98  * @param[in] ext that's being copied.
99  * @param[in] chunk Talloc chunk.
100  * @param[in] ext_ptr Pointer to the extension to fixup.
101  * @param[in] ext_len The length of the extension to fixup.
102  * @return
103  * - NULL on error.
104  * - Pointer to the new extension on success.
105  */
106 typedef int (* fr_ext_fixup_t)(int ext, TALLOC_CTX *chunk,
107  void *ext_ptr, size_t ext_len);
108 
109 /** Additional information for a given extension
110  */
111 typedef struct {
112  size_t min; //!< Minimum size of extension.
113  bool has_hdr; //!< Additional metadata should be allocated before
114  ///< the extension data to record the exact length
115  ///< of the extension.
116  bool can_copy; //!< Copying this extension between structs is allowed.
117 
118  fr_ext_alloc_t alloc; //!< Override the normal alloc operation with a callback.
119  fr_ext_copy_t copy; //!< Override the normal copy operation with a callback.
120  fr_ext_fixup_t fixup; //!< Callback for fixing up internal consistency issues.
121 } fr_ext_info_t;
122 
123 /** Structure to define a set of extensions
124  *
125  */
126 struct fr_ext_s {
127  size_t offset_of_exts; //!< Where in the extended struct the extensions array starts.
128  fr_table_num_ordered_t const *name_table; //!< String identifiers for the extensions.
129  size_t *name_table_len; //!< How many extensions there are in the table.
130  int max; //!< The highest extension value.
131  fr_ext_info_t const *info; //!< Additional information about each extension.
132 };
133 
134 /** Optional extension header struct
135  *
136  */
137 typedef struct {
138  size_t len; //!< Length of extension data.
139  uint8_t data[]; //!< Extension data
140 } CC_HINT(aligned(FR_EXT_ALIGNMENT)) fr_ext_hdr_t;
141 
142 static inline CC_HINT(always_inline) uint8_t *fr_ext_offsets(fr_ext_t const *def, TALLOC_CTX const *chunk)
143 {
144  return (uint8_t *)(((uintptr_t)chunk) + def->offset_of_exts);
145 }
146 
147 /** Return a pointer to an extension in a chunk
148  *
149  */
150 static inline CC_HINT(always_inline) void *fr_ext_ptr(TALLOC_CTX const *chunk, size_t offset, bool has_hdr)
151 {
152  uintptr_t out;
153 
154  out = (uintptr_t)chunk; /* chunk start */
155  out += offset * FR_EXT_ALIGNMENT; /* offset described by the extension */
156  out += sizeof(fr_ext_hdr_t) * (has_hdr == true); /* data field offset by length header */
157 
158  return (void *)out;
159 }
160 
161 void *fr_ext_alloc_size(fr_ext_t const *def, TALLOC_CTX **chunk_p, int ext, size_t ext_len);
162 
163 size_t fr_ext_len(fr_ext_t const *def, TALLOC_CTX const *chunk_in, int ext);
164 
165 void *fr_ext_copy(fr_ext_t const *def, TALLOC_CTX **chunk_out, TALLOC_CTX const *chunk_in, int ext);
166 
167 int fr_ext_copy_all(fr_ext_t const *def, TALLOC_CTX **chunk_out, TALLOC_CTX const *chunk_in);
168 
169 void fr_ext_debug(fr_ext_t const *def, char const *name, TALLOC_CTX const *chunk);
170 
171 #ifdef __cplusplus
172 }
173 #endif
#define RCSIDH(h, id)
Definition: build.h:482
bool can_copy
Copying this extension between structs is allowed.
Definition: ext.h:116
size_t offset_of_exts
Where in the extended struct the extensions array starts.
Definition: ext.h:127
void * fr_ext_alloc_size(fr_ext_t const *def, TALLOC_CTX **chunk_p, int ext, size_t ext_len)
void *(* fr_ext_alloc_t)(fr_ext_t const *def, TALLOC_CTX **dst_chunk_p, int ext, void *src_ext_ptr, size_t src_ext_len)
Function for pre-allocating extension memory for extensions before they're copied.
Definition: ext.h:70
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
int max
The highest extension value.
Definition: ext.h:130
size_t fr_ext_len(fr_ext_t const *def, TALLOC_CTX const *chunk_in, int ext)
Return the length of an extension.
Definition: ext.c:128
int fr_ext_copy_all(fr_ext_t const *def, TALLOC_CTX **chunk_out, TALLOC_CTX const *chunk_in)
Copy all the extensions from one attribute to another.
Definition: ext.c:245
bool has_hdr
Additional metadata should be allocated before the extension data to record the exact length of the e...
Definition: ext.h:113
int(* fr_ext_copy_t)(int ext, TALLOC_CTX *dst_chunk, void *dst_ext_ptr, size_t dst_ext_len, TALLOC_CTX const *src_chunk, void *src_ext_ptr, size_t src_ext_len)
Function for re-populating extensions after they're copied.
Definition: ext.h:86
size_t * name_table_len
How many extensions there are in the table.
Definition: ext.h:129
fr_table_num_ordered_t const * name_table
String identifiers for the extensions.
Definition: ext.h:128
size_t len
Length of extension data.
Definition: ext.h:138
fr_ext_copy_t copy
Override the normal copy operation with a callback.
Definition: ext.h:119
fr_ext_alloc_t alloc
Override the normal alloc operation with a callback.
Definition: ext.h:118
void * fr_ext_copy(fr_ext_t const *def, TALLOC_CTX **chunk_out, TALLOC_CTX const *chunk_in, int ext)
Copy extension data from one attribute to another.
Definition: ext.c:160
fr_ext_fixup_t fixup
Callback for fixing up internal consistency issues.
Definition: ext.h:120
int(* fr_ext_fixup_t)(int ext, TALLOC_CTX *chunk, void *ext_ptr, size_t ext_len)
Function for re-establishing internal consistency on realloc.
Definition: ext.h:106
#define FR_EXT_ALIGNMENT
The alignment of object extension structures.
Definition: ext.h:54
size_t min
Minimum size of extension.
Definition: ext.h:112
static uint8_t * fr_ext_offsets(fr_ext_t const *def, TALLOC_CTX const *chunk)
Definition: ext.h:142
void fr_ext_debug(fr_ext_t const *def, char const *name, TALLOC_CTX const *chunk)
fr_ext_info_t const * info
Additional information about each extension.
Definition: ext.h:131
Optional extension header struct.
Definition: ext.h:137
Additional information for a given extension.
Definition: ext.h:111
Structure to define a set of extensions.
Definition: ext.h:126
unsigned char uint8_t
Definition: merged_model.c:30
static char const * name
An element in an arbitrarily ordered array of name to num mappings.
Definition: table.h:57
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997