libyang 4.3.4
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv6_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_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_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
117 uint32_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_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
128 uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
129 const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
130 struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
131{
132 LY_ERR ret = LY_SUCCESS;
133 struct lyd_value_ipv6_prefix *val;
134 uint32_t value_size;
135
136 /* init storage */
137 memset(storage, 0, sizeof *storage);
138 storage->realtype = type;
139
140 /* check value length */
141 ret = lyplg_type_check_value_size("ipv6-prefix", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
142 LYPLG_IPV6PREF_LYB_VALUE_SIZE, &value_size, err);
143 LY_CHECK_GOTO(ret, cleanup);
144
145 if (format == LY_VALUE_LYB) {
146 /* validation */
147 if (((uint8_t *)value)[16] > 128) {
148 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix prefix length %" PRIu8 ".",
149 ((uint8_t *)value)[16]);
150 goto cleanup;
151 }
152
153 /* store/allocate value */
154 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
155 storage->dyn_mem = (void *)value;
156 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
157
158 LYD_VALUE_GET(storage, val);
159 } else {
160 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
161 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
162
163 memcpy(val, value, value_size);
164 }
165
166 /* zero host */
167 ipv6prefix_zero_host(&val->addr, val->prefix);
168
169 /* success */
170 goto cleanup;
171 }
172
173 /* allocate value */
174 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
175 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
176
177 /* check hints */
178 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
179 LY_CHECK_GOTO(ret, cleanup);
180
181 /* get the mask in network-byte order */
182 ret = ipv6prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
183 LY_CHECK_GOTO(ret, cleanup);
184
185 /* zero host */
186 ipv6prefix_zero_host(&val->addr, val->prefix);
187
188 if (format == LY_VALUE_CANON) {
189 /* store canonical value */
190 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
191 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
192 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
193 LY_CHECK_GOTO(ret, cleanup);
194 } else {
195 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
196 LY_CHECK_GOTO(ret, cleanup);
197 }
198 }
199
200cleanup:
201 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
202 free((void *)value);
203 }
204
205 if (ret) {
206 lyplg_type_free_ipv6_prefix(ctx, storage);
207 }
208 return ret;
209}
210
214static LY_ERR
215lyplg_type_compare_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
216 const struct lyd_value *val2)
217{
218 struct lyd_value_ipv6_prefix *v1, *v2;
219
220 LYD_VALUE_GET(val1, v1);
221 LYD_VALUE_GET(val2, v2);
222
223 if (memcmp(v1, v2, sizeof *v1)) {
224 return LY_ENOT;
225 }
226 return LY_SUCCESS;
227}
228
232static int
233lyplg_type_sort_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
234 const struct lyd_value *val2)
235{
236 struct lyd_value_ipv6_prefix *v1, *v2;
237
238 LYD_VALUE_GET(val1, v1);
239 LYD_VALUE_GET(val2, v2);
240
241 return memcmp(v1, v2, sizeof *v1);
242}
243
247static const void *
248lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
249 void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
250{
251 struct lyd_value_ipv6_prefix *val;
252 char *ret;
253
254 LYD_VALUE_GET(value, val);
255
256 if (format == LY_VALUE_LYB) {
257 *dynamic = 0;
258 if (value_size_bits) {
259 *value_size_bits = LYPLG_IPV6PREF_LYB_VALUE_SIZE;
260 }
261 return val;
262 }
263
264 /* generate canonical value if not already */
265 if (!value->_canonical) {
266 /* IPv6 mask + '/' + prefix */
267 ret = malloc(INET6_ADDRSTRLEN + 4);
268 LY_CHECK_RET(!ret, NULL);
269
270 /* convert back to string */
271 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
272 free(ret);
273 return NULL;
274 }
275
276 /* add the prefix */
277 sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
278
279 /* store it */
280 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
281 LOGMEM(ctx);
282 return NULL;
283 }
284 }
285
286 /* use the cached canonical value */
287 if (dynamic) {
288 *dynamic = 0;
289 }
290 if (value_size_bits) {
291 *value_size_bits = strlen(value->_canonical) * 8;
292 }
293 return value->_canonical;
294}
295
299static LY_ERR
300lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
301{
302 LY_ERR ret;
303 struct lyd_value_ipv6_prefix *orig_val, *dup_val;
304
305 memset(dup, 0, sizeof *dup);
306
307 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
308 LY_CHECK_GOTO(ret, error);
309
310 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
311 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
312
313 LYD_VALUE_GET(original, orig_val);
314 memcpy(dup_val, orig_val, sizeof *orig_val);
315
316 dup->realtype = original->realtype;
317 return LY_SUCCESS;
318
319error:
320 lyplg_type_free_ipv6_prefix(ctx, dup);
321 return ret;
322}
323
327static void
328lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
329{
330 struct lyd_value_ipv6_prefix *val;
331
332 lydict_remove(ctx, value->_canonical);
333 value->_canonical = NULL;
334 LYD_VALUE_GET(value, val);
336}
337
346 {
347 .module = "ietf-inet-types",
348 .revision = "2013-07-15",
349 .name = "ipv6-prefix",
350
351 .plugin.id = "ly2 ipv6-prefix",
352 .plugin.lyb_size = lyplg_type_lyb_size_ipv6_prefix,
353 .plugin.store = lyplg_type_store_ipv6_prefix,
354 .plugin.validate_value = NULL,
355 .plugin.validate_tree = NULL,
356 .plugin.compare = lyplg_type_compare_ipv6_prefix,
357 .plugin.sort = lyplg_type_sort_ipv6_prefix,
358 .plugin.print = lyplg_type_print_ipv6_prefix,
359 .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
360 .plugin.free = lyplg_type_free_ipv6_prefix,
361 },
362 {0}
363};
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:237
@ LYVE_DATA
Definition log.h:274
@ LY_EMEM
Definition log.h:239
@ LY_ENOT
Definition log.h:251
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
YANG extension compiled instance.
#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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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
LY_DATA_TYPE basetype
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv6_prefix[]
Plugin information for ipv6-prefix type implementation.
#define LYPLG_IPV6PREF_LYB_VALUE_SIZE
Definition ipv6_prefix.c:50
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:567
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:606
struct in6_addr addr
Definition tree_data.h:693
const char * _canonical
Definition tree_data.h:564
YANG data representation.
Definition tree_data.h:563
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition tree_data.h:692
#define LOGMEM(CTX)
Definition tree_edit.h:22