libyang  3.4.2
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ipv6_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 "libyang.h"
36 
37 #include "compat.h"
38 #include "ly_common.h"
39 
50 #define LYB_VALUE_LEN 17
51 
52 static void lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
53 
64 static LY_ERR
65 ipv6prefix_str2ip(const char *value, size_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 convert IPv6 address \"%s\".", mask_str);
82  goto cleanup;
83  }
84 
85 cleanup:
86  free(mask_str);
87  return ret;
88 }
89 
96 static void
97 ipv6prefix_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 
118 static LY_ERR
119 lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
120  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
121  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
122  struct ly_err_item **err)
123 {
124  LY_ERR ret = LY_SUCCESS;
125  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
126  struct lyd_value_ipv6_prefix *val;
127 
128  /* init storage */
129  memset(storage, 0, sizeof *storage);
130  storage->realtype = type;
131 
132  if (format == LY_VALUE_LYB) {
133  /* validation */
134  if (value_len != LYB_VALUE_LEN) {
135  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix value size %zu (expected %d).",
136  value_len, LYB_VALUE_LEN);
137  goto cleanup;
138  }
139  if (((uint8_t *)value)[16] > 128) {
140  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix prefix length %" PRIu8 ".",
141  ((uint8_t *)value)[16]);
142  goto cleanup;
143  }
144 
145  /* store/allocate value */
146  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
147  storage->dyn_mem = (void *)value;
148  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
149 
150  LYD_VALUE_GET(storage, val);
151  } else {
152  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
153  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
154 
155  memcpy(val, value, value_len);
156  }
157 
158  /* zero host */
159  ipv6prefix_zero_host(&val->addr, val->prefix);
160 
161  /* success */
162  goto cleanup;
163  }
164 
165  /* allocate value */
166  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
167  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
168 
169  /* check hints */
170  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
171  LY_CHECK_GOTO(ret, cleanup);
172 
173  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
174  /* length restriction of the string */
175  if (type_str->length) {
176  /* value_len is in bytes, but we need number of characters here */
177  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
178  LY_CHECK_GOTO(ret, cleanup);
179  }
180 
181  /* pattern restrictions */
182  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
183  LY_CHECK_GOTO(ret, cleanup);
184  }
185 
186  /* get the mask in network-byte order */
187  ret = ipv6prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
188  LY_CHECK_GOTO(ret, cleanup);
189 
190  /* zero host */
191  ipv6prefix_zero_host(&val->addr, val->prefix);
192 
193  if (format == LY_VALUE_CANON) {
194  /* store canonical value */
195  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
196  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
197  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
198  LY_CHECK_GOTO(ret, cleanup);
199  } else {
200  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
201  LY_CHECK_GOTO(ret, cleanup);
202  }
203  }
204 
205 cleanup:
206  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
207  free((void *)value);
208  }
209 
210  if (ret) {
211  lyplg_type_free_ipv6_prefix(ctx, storage);
212  }
213  return ret;
214 }
215 
219 static LY_ERR
220 lyplg_type_compare_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
221  const struct lyd_value *val2)
222 {
223  struct lyd_value_ipv6_prefix *v1, *v2;
224 
225  LYD_VALUE_GET(val1, v1);
226  LYD_VALUE_GET(val2, v2);
227 
228  if (memcmp(v1, v2, sizeof *v1)) {
229  return LY_ENOT;
230  }
231  return LY_SUCCESS;
232 }
233 
237 static int
238 lyplg_type_sort_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
239  const struct lyd_value *val2)
240 {
241  struct lyd_value_ipv6_prefix *v1, *v2;
242 
243  LYD_VALUE_GET(val1, v1);
244  LYD_VALUE_GET(val2, v2);
245 
246  return memcmp(v1, v2, sizeof *v1);
247 }
248 
252 static const void *
253 lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
254  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
255 {
256  struct lyd_value_ipv6_prefix *val;
257  char *ret;
258 
259  LYD_VALUE_GET(value, val);
260 
261  if (format == LY_VALUE_LYB) {
262  *dynamic = 0;
263  if (value_len) {
264  *value_len = LYB_VALUE_LEN;
265  }
266  return val;
267  }
268 
269  /* generate canonical value if not already */
270  if (!value->_canonical) {
271  /* IPv6 mask + '/' + prefix */
272  ret = malloc(INET6_ADDRSTRLEN + 4);
273  LY_CHECK_RET(!ret, NULL);
274 
275  /* convert back to string */
276  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
277  free(ret);
278  return NULL;
279  }
280 
281  /* add the prefix */
282  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
283 
284  /* store it */
285  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
286  LOGMEM(ctx);
287  return NULL;
288  }
289  }
290 
291  /* use the cached canonical value */
292  if (dynamic) {
293  *dynamic = 0;
294  }
295  if (value_len) {
296  *value_len = strlen(value->_canonical);
297  }
298  return value->_canonical;
299 }
300 
304 static LY_ERR
305 lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
306 {
307  LY_ERR ret;
308  struct lyd_value_ipv6_prefix *orig_val, *dup_val;
309 
310  memset(dup, 0, sizeof *dup);
311 
312  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
313  LY_CHECK_GOTO(ret, error);
314 
315  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
316  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
317 
318  LYD_VALUE_GET(original, orig_val);
319  memcpy(dup_val, orig_val, sizeof *orig_val);
320 
321  dup->realtype = original->realtype;
322  return LY_SUCCESS;
323 
324 error:
325  lyplg_type_free_ipv6_prefix(ctx, dup);
326  return ret;
327 }
328 
332 static void
333 lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
334 {
335  struct lyd_value_ipv6_prefix *val;
336 
337  lydict_remove(ctx, value->_canonical);
338  value->_canonical = NULL;
339  LYD_VALUE_GET(value, val);
341 }
342 
351  {
352  .module = "ietf-inet-types",
353  .revision = "2013-07-15",
354  .name = "ipv6-prefix",
355 
356  .plugin.id = "libyang 2 - ipv6-prefix, version 1",
357  .plugin.store = lyplg_type_store_ipv6_prefix,
358  .plugin.validate = NULL,
359  .plugin.compare = lyplg_type_compare_ipv6_prefix,
360  .plugin.sort = lyplg_type_sort_ipv6_prefix,
361  .plugin.print = lyplg_type_print_ipv6_prefix,
362  .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
363  .plugin.free = lyplg_type_free_ipv6_prefix,
364  .plugin.lyb_data_len = LYB_VALUE_LEN,
365  },
366  {0}
367 };
struct lysc_type * realtype
Definition: tree_data.h:575
Compiled YANG data node.
Definition: tree_schema.h:1439
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
struct in6_addr addr
Definition: tree_data.h:700
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1339
struct lysc_range * length
Definition: tree_schema.h:1338
YANG data representation.
Definition: tree_data.h:571
const char * _canonical
Definition: tree_data.h:572
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
Libyang full error structure.
Definition: log.h:285
Definition: log.h:277
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:614
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.
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(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 lyplg_type_check_hints(uint32_t hints, const char *value, size_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser&#39;s hints (if any) in the specified format.
struct lyplg_type_record plugins_ipv6_prefix[]
Plugin information for ipv6-prefix type implementation.
Definition: ipv6_prefix.c:350
const char * module
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition: tree_data.h:699
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
Definition: log.h:248
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1305
#define LYB_VALUE_LEN
Definition: ipv6_prefix.c:50
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
#define LYPLG_TYPE_STORE_ONLY
Definition: log.h:254
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.