libyang 5.7.1
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv6_address_prefix.c
Go to the documentation of this file.
1
14
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 "libyang.h"
36
37#include "compat.h"
38#include "ly_common.h"
39
49
50#define LYPLG_IPV6PREF_LYB_VALUE_SIZE 136
51
52static void lyplg_type_free_ipv6_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
53
64static LY_ERR
65ipv6prefix_str2ip(const char *value, uint32_t value_len, struct in6_addr *addr, uint8_t *prefix, struct ly_err_item **err)
66{
67 LY_ERR ret = LY_SUCCESS;
68 const char *pref_str;
69 char *mask_str = NULL;
70
71 /* it passed the pattern validation */
72 pref_str = ly_strnchr(value, '/', value_len);
73 ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
74
75 /* get just the network prefix */
76 mask_str = strndup(value, pref_str - value);
77 LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
78
79 /* convert it to netword-byte order */
80 if (inet_pton(AF_INET6, mask_str, addr) != 1) {
81 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", mask_str);
82 goto cleanup;
83 }
84
85cleanup:
86 free(mask_str);
87 return ret;
88}
89
96static void
97ipv6prefix_zero_host(struct in6_addr *addr, uint8_t prefix)
98{
99 uint32_t i, j, mask;
100
101 /* zero host bits */
102 for (i = 0; i < 4; ++i) {
103 mask = 0;
104 for (j = 0; j < 32; ++j) {
105 mask <<= 1;
106 if (prefix > (i * 32) + j) {
107 mask |= 1;
108 }
109 }
110 mask = htonl(mask);
111 ((uint32_t *)addr->s6_addr)[i] &= mask;
112 }
113}
114
115static void
116lyplg_type_lyb_size_ipv6_address_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
117 uint64_t *fixed_size_bits)
118{
119 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
120 *fixed_size_bits = LYPLG_IPV6PREF_LYB_VALUE_SIZE;
121}
122
126static LY_ERR
127lyplg_type_store_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
128 uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
129 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
130 struct ly_err_item **err)
131{
132 LY_ERR ret = LY_SUCCESS;
133 const struct lysc_type_str *type_str = (struct lysc_type_str *)type;
134 struct lyd_value_ipv6_prefix *val;
135 uint32_t value_size;
136
137 /* init storage */
138 memset(storage, 0, sizeof *storage);
139 storage->realtype = type;
140
141 /* check value length */
142 ret = lyplg_type_check_value_size(type->name, format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
143 LYPLG_IPV6PREF_LYB_VALUE_SIZE, &value_size, err);
144 LY_CHECK_GOTO(ret, cleanup);
145
146 if (format == LY_VALUE_LYB) {
147 /* validation */
148 if (((uint8_t *)value)[16] > 128) {
149 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s prefix length %" PRIu8 ".",
150 type->name, ((uint8_t *)value)[16]);
151 goto cleanup;
152 }
153
154 /* store/allocate value */
155 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
156 storage->dyn_mem = (void *)value;
157 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
158
159 LYD_VALUE_GET(storage, val);
160 } else {
161 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
162 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
163
164 memcpy(val, value, value_size);
165 }
166
167 if (!strcmp(type->name, "ipv6-prefix")) {
168 /* zero host */
169 ipv6prefix_zero_host(&val->addr, val->prefix);
170 }
171
172 /* success */
173 goto cleanup;
174 }
175
176 /* allocate value */
177 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
178 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
179
180 /* check hints */
181 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
182 LY_CHECK_GOTO(ret, cleanup);
183
184 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
185 /* length restriction of the string */
186 if (type_str->length) {
187 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, value_size, value, value_size, err);
188 LY_CHECK_GOTO(ret, cleanup);
189 }
190
191 /* validate patterns */
192 ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
193 LY_CHECK_GOTO(ret, cleanup);
194 }
195
196 /* get the mask in network-byte order */
197 ret = ipv6prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
198 LY_CHECK_GOTO(ret, cleanup);
199
200 if (!strcmp(type->name, "ipv6-prefix")) {
201 /* zero host */
202 ipv6prefix_zero_host(&val->addr, val->prefix);
203 }
204
205 if (format == LY_VALUE_CANON) {
206 /* store canonical value */
207 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
208 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
209 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
210 LY_CHECK_GOTO(ret, cleanup);
211 } else {
212 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
213 LY_CHECK_GOTO(ret, cleanup);
214 }
215 }
216
217cleanup:
218 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
219 free((void *)value);
220 }
221
222 if (ret) {
223 lyplg_type_free_ipv6_address_prefix(ctx, storage);
224 }
225 return ret;
226}
227
231static LY_ERR
232lyplg_type_compare_ipv6_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
233 const struct lyd_value *val2)
234{
235 struct lyd_value_ipv6_prefix *v1, *v2;
236
237 LYD_VALUE_GET(val1, v1);
238 LYD_VALUE_GET(val2, v2);
239
240 if (memcmp(v1, v2, sizeof *v1)) {
241 return LY_ENOT;
242 }
243 return LY_SUCCESS;
244}
245
249static int
250lyplg_type_sort_ipv6_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
251 const struct lyd_value *val2)
252{
253 struct lyd_value_ipv6_prefix *v1, *v2;
254
255 LYD_VALUE_GET(val1, v1);
256 LYD_VALUE_GET(val2, v2);
257
258 return memcmp(v1, v2, sizeof *v1);
259}
260
264static const void *
265lyplg_type_print_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
266 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
267{
268 struct lyd_value_ipv6_prefix *val;
269 char *ret;
270
271 LYD_VALUE_GET(value, val);
272
273 if (format == LY_VALUE_LYB) {
274 *dynamic = 0;
275 if (value_size_bits) {
276 *value_size_bits = LYPLG_IPV6PREF_LYB_VALUE_SIZE;
277 }
278 return val;
279 }
280
281 /* generate canonical value if not already */
282 if (!value->_canonical) {
283 /* IPv6 mask + '/' + prefix */
284 ret = malloc(INET6_ADDRSTRLEN + 4);
285 LY_CHECK_RET(!ret, NULL);
286
287 /* convert back to string */
288 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
289 free(ret);
290 return NULL;
291 }
292
293 /* add the prefix */
294 sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
295
296 /* store it */
297 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
298 LOGMEM(ctx);
299 return NULL;
300 }
301 }
302
303 /* use the cached canonical value */
304 if (dynamic) {
305 *dynamic = 0;
306 }
307 if (value_size_bits) {
308 *value_size_bits = strlen(value->_canonical) * 8;
309 }
310 return value->_canonical;
311}
312
316static LY_ERR
317lyplg_type_dup_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
318{
319 LY_ERR ret;
320 struct lyd_value_ipv6_prefix *orig_val, *dup_val;
321
322 memset(dup, 0, sizeof *dup);
323
324 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
325 LY_CHECK_GOTO(ret, error);
326
327 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
328 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
329
330 LYD_VALUE_GET(original, orig_val);
331 memcpy(dup_val, orig_val, sizeof *orig_val);
332
333 dup->realtype = original->realtype;
334 return LY_SUCCESS;
335
336error:
337 lyplg_type_free_ipv6_address_prefix(ctx, dup);
338 return ret;
339}
340
344static void
345lyplg_type_free_ipv6_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
346{
347 struct lyd_value_ipv6_prefix *val;
348
349 lydict_remove(ctx, value->_canonical);
350 value->_canonical = NULL;
351 LYD_VALUE_GET(value, val);
353}
354
363 {
364 .module = "ietf-inet-types",
365 .revision = NULL,
366 .name = "ipv6-prefix",
367
368 .plugin.id = "ly2 ipv6-address-prefix",
369 .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_prefix,
370 .plugin.store = lyplg_type_store_ipv6_address_prefix,
371 .plugin.validate_value = NULL,
372 .plugin.validate_tree = NULL,
373 .plugin.compare = lyplg_type_compare_ipv6_address_prefix,
374 .plugin.sort = lyplg_type_sort_ipv6_address_prefix,
375 .plugin.print = lyplg_type_print_ipv6_address_prefix,
376 .plugin.duplicate = lyplg_type_dup_ipv6_address_prefix,
377 .plugin.free = lyplg_type_free_ipv6_address_prefix,
378 },
379 {
380 .module = "ietf-inet-types",
381 .revision = NULL,
382 .name = "ipv6-address-and-prefix",
383
384 .plugin.id = "ly2 ipv6-address-prefix",
385 .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_prefix,
386 .plugin.store = lyplg_type_store_ipv6_address_prefix,
387 .plugin.validate_value = NULL,
388 .plugin.validate_tree = NULL,
389 .plugin.compare = lyplg_type_compare_ipv6_address_prefix,
390 .plugin.sort = lyplg_type_sort_ipv6_address_prefix,
391 .plugin.print = lyplg_type_print_ipv6_address_prefix,
392 .plugin.duplicate = lyplg_type_dup_ipv6_address_prefix,
393 .plugin.free = lyplg_type_free_ipv6_address_prefix,
394 },
395 {0}
396};
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:289
@ 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:297
#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.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_TYPE_STRING
Definition tree.h:209
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv6_address_prefix[]
Plugin information for ipv6-prefix and ipv6-address-and-prefix type implementation.
#define LYPLG_IPV6PREF_LYB_VALUE_SIZE
The main libyang public header.
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:527
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:566
struct in6_addr addr
Definition tree_data.h:652
const char * _canonical
Definition tree_data.h:524
YANG data representation.
Definition tree_data.h:523
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition tree_data.h:651
#define LOGMEM(CTX)
Definition tree_edit.h:22