libyang  6.4.13
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
ipv6_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_IPV6PREF_LYB_VALUE_SIZE 136
49 
50 static void lyplg_type_free_ipv6_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
51 
61 static LY_ERR
62 ipv6prefix_str2ip(const char *value, uint32_t value_len, struct lyd_value_ipv6_prefix *val,
63  struct ly_err_item **err)
64 {
65  LY_ERR ret = LY_SUCCESS;
66  const char *pref_str;
67  char *mask_str = NULL;
68 
69  /* it passed the pattern validation */
70  pref_str = ly_strnchr(value, '/', value_len);
71  ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), &val->prefix);
72 
73  /* get just the network prefix */
74  mask_str = strndup(value, pref_str - value);
75  LY_CHECK_ERR_GOTO(!mask_str, ret = ly_err_new_no_mem(err), cleanup);
76 
77  /* convert it to netword-byte order */
78  if (inet_pton(AF_INET6, mask_str, &val->addr) != 1) {
79  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", mask_str);
80  goto cleanup;
81  }
82 
83 cleanup:
84  free(mask_str);
85  return ret;
86 }
87 
94 static void
95 ipv6prefix_zero_host(struct in6_addr *addr, uint8_t prefix)
96 {
97  uint32_t i, j, mask;
98 
99  /* zero host bits */
100  for (i = 0; i < 4; ++i) {
101  mask = 0;
102  for (j = 0; j < 32; ++j) {
103  mask <<= 1;
104  if (prefix > (i * 32) + j) {
105  mask |= 1;
106  }
107  }
108  mask = htonl(mask);
109  ((uint32_t *)addr->s6_addr)[i] &= mask;
110  }
111 }
112 
113 static void
114 lyplg_type_lyb_size_ipv6_address_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
115  uint64_t *fixed_size_bits)
116 {
117  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
118  *fixed_size_bits = LYPLG_IPV6PREF_LYB_VALUE_SIZE;
119 }
120 
124 static LY_ERR
125 lyplg_type_store_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
126  uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
127  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
128  struct ly_err_item **err)
129 {
130  LY_ERR ret = LY_SUCCESS;
131  const struct lysc_type_str *type_str = (struct lysc_type_str *)type;
132  struct lyd_value_ipv6_prefix *val;
133  uint32_t value_size;
134 
135  /* init storage */
136  memset(storage, 0, sizeof *storage);
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_IPV6PREF_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)[16] > 128) {
147  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s prefix length %" PRIu8 ".",
148  type->name, ((uint8_t *)value)[16]);
149  goto cleanup;
150  }
151 
152  /* store/allocate value */
153  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
154  storage->dyn_mem = (void *)value;
155  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
156 
157  LYD_VALUE_GET(storage, val);
158  } else {
159  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
160  LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
161 
162  memcpy(val, value, value_size);
163  }
164 
165  if (!strcmp(type->name, "ipv6-prefix")) {
166  /* zero host */
167  ipv6prefix_zero_host(&val->addr, val->prefix);
168  }
169 
170  /* success */
171  goto cleanup;
172  }
173 
174  /* allocate value */
175  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
176  LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
177 
178  /* check hints */
179  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
180  LY_CHECK_GOTO(ret, cleanup);
181 
182  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
183  /* length restriction of the string */
184  if (type_str->length) {
185  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, value_size, value, value_size, err);
186  LY_CHECK_GOTO(ret, cleanup);
187  }
188 
189  /* validate patterns */
190  ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
191  LY_CHECK_GOTO(ret, cleanup);
192  }
193 
194  /* get the mask in network-byte order */
195  ret = ipv6prefix_str2ip(value, value_size, val, err);
196  LY_CHECK_GOTO(ret, cleanup);
197 
198  if (!strcmp(type->name, "ipv6-prefix")) {
199  /* zero host */
200  ipv6prefix_zero_host(&val->addr, val->prefix);
201  }
202 
203  if (format == LY_VALUE_CANON) {
204  /* store canonical value */
205  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
206  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
207  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
208  LY_CHECK_GOTO(ret, cleanup);
209  } else {
210  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
211  LY_CHECK_GOTO(ret, cleanup);
212  }
213  }
214 
215 cleanup:
216  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
217  free((void *)value);
218  }
219 
220  if (ret) {
221  lyplg_type_free_ipv6_address_prefix(ctx, storage);
222  }
223  return ret;
224 }
225 
229 static LY_ERR
230 lyplg_type_compare_ipv6_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
231  const struct lyd_value *val2)
232 {
233  struct lyd_value_ipv6_prefix *v1, *v2;
234 
235  LYD_VALUE_GET(val1, v1);
236  LYD_VALUE_GET(val2, v2);
237 
238  if (memcmp(v1, v2, sizeof *v1)) {
239  return LY_ENOT;
240  }
241  return LY_SUCCESS;
242 }
243 
247 static int
248 lyplg_type_sort_ipv6_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
249  const struct lyd_value *val2)
250 {
251  struct lyd_value_ipv6_prefix *v1, *v2;
252 
253  LYD_VALUE_GET(val1, v1);
254  LYD_VALUE_GET(val2, v2);
255 
256  return memcmp(v1, v2, sizeof *v1);
257 }
258 
262 static const void *
263 lyplg_type_print_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
264  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
265 {
266  struct lyd_value_ipv6_prefix *val;
267  char *ret;
268 
269  LYD_VALUE_GET(value, val);
270 
271  if (format == LY_VALUE_LYB) {
272  *dynamic = 0;
273  if (value_size_bits) {
274  *value_size_bits = LYPLG_IPV6PREF_LYB_VALUE_SIZE;
275  }
276  return val;
277  }
278 
279  /* generate canonical value if not already */
280  if (!value->_canonical) {
281  /* IPv6 mask + '/' + prefix */
282  ret = malloc(INET6_ADDRSTRLEN + 4);
283  LY_CHECK_RET(!ret, NULL);
284 
285  /* convert back to string */
286  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
287  free(ret);
288  return NULL;
289  }
290 
291  /* add the prefix */
292  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
293 
294  /* store it */
295  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
296  LOGMEM(ctx);
297  return NULL;
298  }
299  }
300 
301  /* use the cached canonical value */
302  if (dynamic) {
303  *dynamic = 0;
304  }
305  if (value_size_bits) {
306  *value_size_bits = strlen(value->_canonical) * 8;
307  }
308  return value->_canonical;
309 }
310 
314 static LY_ERR
315 lyplg_type_dup_ipv6_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
316 {
317  LY_ERR ret;
318  struct lyd_value_ipv6_prefix *orig_val, *dup_val;
319 
320  memset(dup, 0, sizeof *dup);
321 
322  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
323  LY_CHECK_GOTO(ret, error);
324 
325  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
326  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
327 
328  LYD_VALUE_GET(original, orig_val);
329  memcpy(dup_val, orig_val, sizeof *orig_val);
330 
331  dup->realtype = original->realtype;
332  return LY_SUCCESS;
333 
334 error:
335  lyplg_type_free_ipv6_address_prefix(ctx, dup);
336  return ret;
337 }
338 
342 static void
343 lyplg_type_free_ipv6_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
344 {
345  struct lyd_value_ipv6_prefix *val;
346 
347  lydict_remove(ctx, value->_canonical);
348  value->_canonical = NULL;
349  LYD_VALUE_GET(value, val);
351 }
352 
361  {
362  .module = "ietf-inet-types",
363  .revision = NULL,
364  .name = "ipv6-prefix",
365 
366  .plugin.id = "ly2 ipv6-address-prefix",
367  .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_prefix,
368  .plugin.store = lyplg_type_store_ipv6_address_prefix,
369  .plugin.validate_value = NULL,
370  .plugin.validate_tree = NULL,
371  .plugin.compare = lyplg_type_compare_ipv6_address_prefix,
372  .plugin.sort = lyplg_type_sort_ipv6_address_prefix,
373  .plugin.print = lyplg_type_print_ipv6_address_prefix,
374  .plugin.duplicate = lyplg_type_dup_ipv6_address_prefix,
375  .plugin.free = lyplg_type_free_ipv6_address_prefix,
376  },
377  {
378  .module = "ietf-inet-types",
379  .revision = NULL,
380  .name = "ipv6-address-and-prefix",
381 
382  .plugin.id = "ly2 ipv6-address-prefix",
383  .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_prefix,
384  .plugin.store = lyplg_type_store_ipv6_address_prefix,
385  .plugin.validate_value = NULL,
386  .plugin.validate_tree = NULL,
387  .plugin.compare = lyplg_type_compare_ipv6_address_prefix,
388  .plugin.sort = lyplg_type_sort_ipv6_address_prefix,
389  .plugin.print = lyplg_type_print_ipv6_address_prefix,
390  .plugin.duplicate = lyplg_type_dup_ipv6_address_prefix,
391  .plugin.free = lyplg_type_free_ipv6_address_prefix,
392  },
393  {0}
394 };
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 * module
#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 an error structure.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_ONLY
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
const char * name
Definition: tree_schema.h:1275
struct lysc_pattern ** patterns
Definition: tree_schema.h:1312
struct lysc_range * length
Definition: tree_schema.h:1311
Compiled YANG data node.
Definition: tree_schema.h:1412
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
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:653
struct in6_addr addr
Definition: tree_data.h:739
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition: tree_data.h:738
@ 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