libyang 6.4.3
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv4_address_prefix.c
Go to the documentation of this file.
1
15#define _GNU_SOURCE /* strndup */
16
17#include "plugins_internal.h"
18#include "plugins_types.h"
19
20#ifdef _WIN32
21# include <winsock2.h>
22# include <ws2tcpip.h>
23#else
24# include <arpa/inet.h>
25# if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
26# include <netinet/in.h>
27# include <sys/socket.h>
28# endif
29#endif
30#include <assert.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "compat.h"
36#include "dict.h"
37#include "ly_common.h"
48#define LYPLG_IPV4PREF_LYB_VALUE_SIZE 40
49
50static void lyplg_type_free_ipv4_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
51
52static void
53lyplg_type_lyb_size_ipv4_address_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
54 uint64_t *fixed_size_bits)
55{
56 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
57 *fixed_size_bits = LYPLG_IPV4PREF_LYB_VALUE_SIZE;
58}
59
70static LY_ERR
71ipv4prefix_str2ip(const char *value, uint32_t value_len, struct in_addr *addr, uint8_t *prefix, struct ly_err_item **err)
72{
73 LY_ERR ret = LY_SUCCESS;
74 const char *pref_str;
75 char *mask_str = NULL;
76
77 /* it passed the pattern validation */
78 pref_str = ly_strnchr(value, '/', value_len);
79 ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
80
81 /* get just the network prefix */
82 mask_str = strndup(value, pref_str - value);
83 LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
84
85 /* convert it to netword-byte order */
86 if (inet_pton(AF_INET, mask_str, addr) != 1) {
87 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv4 address \"%s\".", mask_str);
88 goto cleanup;
89 }
90
91cleanup:
92 free(mask_str);
93 return ret;
94}
95
102static void
103ipv4prefix_zero_host(struct in_addr *addr, uint8_t prefix)
104{
105 uint32_t i, mask;
106
107 /* zero host bits */
108 mask = 0;
109 for (i = 0; i < 32; ++i) {
110 mask <<= 1;
111 if (prefix > i) {
112 mask |= 1;
113 }
114 }
115 mask = htonl(mask);
116 addr->s_addr &= mask;
117}
118
122static LY_ERR
123lyplg_type_store_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
124 uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
125 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
126 struct ly_err_item **err)
127{
128 LY_ERR ret = LY_SUCCESS;
129 const struct lysc_type_str *type_str = (struct lysc_type_str *)type;
130 struct lyd_value_ipv4_prefix *val;
131 uint32_t value_size;
132
133 /* init storage */
134 memset(storage, 0, sizeof *storage);
135 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
136 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
137 storage->realtype = type;
138
139 /* check value length */
140 ret = lyplg_type_check_value_size(type->name, format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
141 LYPLG_IPV4PREF_LYB_VALUE_SIZE, &value_size, err);
142 LY_CHECK_GOTO(ret, cleanup);
143
144 if (format == LY_VALUE_LYB) {
145 /* validation */
146 if (((uint8_t *)value)[4] > 32) {
147 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s prefix length %" PRIu8 ".",
148 type->name, ((uint8_t *)value)[4]);
149 goto cleanup;
150 }
151
152 /* store addr + prefix */
153 memcpy(val, value, value_size);
154
155 if (!strcmp(type->name, "ipv4-prefix")) {
156 /* zero host */
157 ipv4prefix_zero_host(&val->addr, val->prefix);
158 }
159
160 /* success */
161 goto cleanup;
162 }
163
164 /* check hints */
165 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
166 LY_CHECK_GOTO(ret, cleanup);
167
168 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
169 /* length restriction of the string */
170 if (type_str->length) {
171 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, value_size, value, value_size, err);
172 LY_CHECK_GOTO(ret, cleanup);
173 }
174
175 /* validate patterns */
176 ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
177 LY_CHECK_GOTO(ret, cleanup);
178 }
179
180 /* get the mask in network-byte order */
181 ret = ipv4prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
182 LY_CHECK_GOTO(ret, cleanup);
183
184 if (!strcmp(type->name, "ipv4-prefix")) {
185 /* zero host */
186 ipv4prefix_zero_host(&val->addr, val->prefix);
187 }
188
189 if (format == LY_VALUE_CANON) {
190 /* store canonical value */
191 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
192 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
193 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
194 LY_CHECK_GOTO(ret, cleanup);
195 } else {
196 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
197 LY_CHECK_GOTO(ret, cleanup);
198 }
199 }
200
201cleanup:
202 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
203 free((void *)value);
204 }
205
206 if (ret) {
207 lyplg_type_free_ipv4_address_prefix(ctx, storage);
208 }
209 return ret;
210}
211
215static LY_ERR
216lyplg_type_compare_ipv4_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
217 const struct lyd_value *val2)
218{
219 struct lyd_value_ipv4_prefix *v1, *v2;
220
221 LYD_VALUE_GET(val1, v1);
222 LYD_VALUE_GET(val2, v2);
223
224 if (memcmp(v1, v2, sizeof *v1)) {
225 return LY_ENOT;
226 }
227 return LY_SUCCESS;
228}
229
233static int
234lyplg_type_sort_ipv4_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
235 const struct lyd_value *val2)
236{
237 struct lyd_value_ipv4_prefix *v1, *v2;
238
239 LYD_VALUE_GET(val1, v1);
240 LYD_VALUE_GET(val2, v2);
241
242 return memcmp(v1, v2, sizeof *v1);
243}
244
248static const void *
249lyplg_type_print_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
250 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
251{
252 struct lyd_value_ipv4_prefix *val;
253 char *ret;
254
255 LYD_VALUE_GET(value, val);
256
257 if (format == LY_VALUE_LYB) {
258 *dynamic = 0;
259 if (value_size_bits) {
260 *value_size_bits = LYPLG_IPV4PREF_LYB_VALUE_SIZE;
261 }
262 return val;
263 }
264
265 /* generate canonical value if not already */
266 if (!value->_canonical) {
267 /* IPv4 mask + '/' + prefix */
268 ret = malloc(INET_ADDRSTRLEN + 3);
269 LY_CHECK_RET(!ret, NULL);
270
271 /* convert back to string */
272 if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
273 free(ret);
274 return NULL;
275 }
276
277 /* add the prefix */
278 sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
279
280 /* store it */
281 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
282 LOGMEM(ctx);
283 return NULL;
284 }
285 }
286
287 /* use the cached canonical value */
288 if (dynamic) {
289 *dynamic = 0;
290 }
291 if (value_size_bits) {
292 *value_size_bits = strlen(value->_canonical) * 8;
293 }
294 return value->_canonical;
295}
296
300static LY_ERR
301lyplg_type_dup_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
302{
303 LY_ERR ret;
304 struct lyd_value_ipv4_prefix *orig_val, *dup_val;
305
306 memset(dup, 0, sizeof *dup);
307
308 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
309 LY_CHECK_GOTO(ret, error);
310
311 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
312 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
313
314 LYD_VALUE_GET(original, orig_val);
315 memcpy(dup_val, orig_val, sizeof *orig_val);
316
317 dup->realtype = original->realtype;
318 return LY_SUCCESS;
319
320error:
321 lyplg_type_free_ipv4_address_prefix(ctx, dup);
322 return ret;
323}
324
328static void
329lyplg_type_free_ipv4_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
330{
331 struct lyd_value_ipv4_prefix *val;
332
333 lydict_remove(ctx, value->_canonical);
334 value->_canonical = NULL;
335 LYD_VALUE_GET(value, val);
337}
338
347 {
348 .module = "ietf-inet-types",
349 .revision = NULL,
350 .name = "ipv4-prefix",
351
352 .plugin.id = "ly2 ipv4-address-prefix",
353 .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_prefix,
354 .plugin.store = lyplg_type_store_ipv4_address_prefix,
355 .plugin.validate_value = NULL,
356 .plugin.validate_tree = NULL,
357 .plugin.compare = lyplg_type_compare_ipv4_address_prefix,
358 .plugin.sort = lyplg_type_sort_ipv4_address_prefix,
359 .plugin.print = lyplg_type_print_ipv4_address_prefix,
360 .plugin.duplicate = lyplg_type_dup_ipv4_address_prefix,
361 .plugin.free = lyplg_type_free_ipv4_address_prefix,
362 },
363 {
364 .module = "ietf-inet-types",
365 .revision = NULL,
366 .name = "ipv4-address-and-prefix",
367
368 .plugin.id = "ly2 ipv4-address-prefix",
369 .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_prefix,
370 .plugin.store = lyplg_type_store_ipv4_address_prefix,
371 .plugin.validate_value = NULL,
372 .plugin.validate_tree = NULL,
373 .plugin.compare = lyplg_type_compare_ipv4_address_prefix,
374 .plugin.sort = lyplg_type_sort_ipv4_address_prefix,
375 .plugin.print = lyplg_type_print_ipv4_address_prefix,
376 .plugin.duplicate = lyplg_type_dup_ipv4_address_prefix,
377 .plugin.free = lyplg_type_free_ipv4_address_prefix,
378 },
379 {0}
380};
libyang dictionary
libyang context handler.
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:252
@ LYVE_DATA
Definition log.h:290
@ LY_EMEM
Definition log.h:254
@ LY_ENOT
Definition log.h:266
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:298
const char *const char * revision
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **patterns, const char *str, uint32_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, uint32_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_ONLY
LY_DATA_TYPE basetype
const char * name
struct lysc_pattern ** patterns
struct lysc_range * length
Compiled YANG data node.
#define LYPLG_IPV4PREF_LYB_VALUE_SIZE
const struct lyplg_type_record plugins_ipv4_address_prefix[]
Plugin information for ipv4-prefix and ipv4-address-and-prefix type implementation.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:614
struct in_addr addr
Definition tree_data.h:716
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:653
const char * _canonical
Definition tree_data.h:611
YANG data representation.
Definition tree_data.h:610
Special lyd_value structure for ietf-inet-types ipv4-prefix values.
Definition tree_data.h:715
@ LY_TYPE_STRING
Definition utils.h:68
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition utils.h:93
@ LY_VALUE_CANON
Definition utils.h:94
@ LY_VALUE_LYB
Definition utils.h:100