libyang  3.1.0
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  /* length restriction of the string */
174  if (type_str->length) {
175  /* value_len is in bytes, but we need number of characters here */
176  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
177  LY_CHECK_GOTO(ret, cleanup);
178  }
179 
180  /* pattern restrictions */
181  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
182  LY_CHECK_GOTO(ret, cleanup);
183 
184  /* get the mask in network-byte order */
185  ret = ipv6prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
186  LY_CHECK_GOTO(ret, cleanup);
187 
188  /* zero host */
189  ipv6prefix_zero_host(&val->addr, val->prefix);
190 
191  if (format == LY_VALUE_CANON) {
192  /* store canonical value */
193  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
194  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
195  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
196  LY_CHECK_GOTO(ret, cleanup);
197  } else {
198  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
199  LY_CHECK_GOTO(ret, cleanup);
200  }
201  }
202 
203 cleanup:
204  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
205  free((void *)value);
206  }
207 
208  if (ret) {
209  lyplg_type_free_ipv6_prefix(ctx, storage);
210  }
211  return ret;
212 }
213 
217 static LY_ERR
218 lyplg_type_compare_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
219  const struct lyd_value *val2)
220 {
221  struct lyd_value_ipv6_prefix *v1, *v2;
222 
223  LYD_VALUE_GET(val1, v1);
224  LYD_VALUE_GET(val2, v2);
225 
226  if (memcmp(v1, v2, sizeof *v1)) {
227  return LY_ENOT;
228  }
229  return LY_SUCCESS;
230 }
231 
235 static int
236 lyplg_type_sort_ipv6_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
237  const struct lyd_value *val2)
238 {
239  struct lyd_value_ipv6_prefix *v1, *v2;
240 
241  LYD_VALUE_GET(val1, v1);
242  LYD_VALUE_GET(val2, v2);
243 
244  return memcmp(v1, v2, sizeof *v1);
245 }
246 
250 static const void *
251 lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
252  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
253 {
254  struct lyd_value_ipv6_prefix *val;
255  char *ret;
256 
257  LYD_VALUE_GET(value, val);
258 
259  if (format == LY_VALUE_LYB) {
260  *dynamic = 0;
261  if (value_len) {
262  *value_len = LYB_VALUE_LEN;
263  }
264  return val;
265  }
266 
267  /* generate canonical value if not already */
268  if (!value->_canonical) {
269  /* IPv6 mask + '/' + prefix */
270  ret = malloc(INET6_ADDRSTRLEN + 4);
271  LY_CHECK_RET(!ret, NULL);
272 
273  /* convert back to string */
274  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
275  free(ret);
276  return NULL;
277  }
278 
279  /* add the prefix */
280  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
281 
282  /* store it */
283  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
284  LOGMEM(ctx);
285  return NULL;
286  }
287  }
288 
289  /* use the cached canonical value */
290  if (dynamic) {
291  *dynamic = 0;
292  }
293  if (value_len) {
294  *value_len = strlen(value->_canonical);
295  }
296  return value->_canonical;
297 }
298 
302 static LY_ERR
303 lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
304 {
305  LY_ERR ret;
306  struct lyd_value_ipv6_prefix *orig_val, *dup_val;
307 
308  memset(dup, 0, sizeof *dup);
309 
310  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
311  LY_CHECK_GOTO(ret, error);
312 
313  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
314  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
315 
316  LYD_VALUE_GET(original, orig_val);
317  memcpy(dup_val, orig_val, sizeof *orig_val);
318 
319  dup->realtype = original->realtype;
320  return LY_SUCCESS;
321 
322 error:
323  lyplg_type_free_ipv6_prefix(ctx, dup);
324  return ret;
325 }
326 
330 static void
331 lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
332 {
333  struct lyd_value_ipv6_prefix *val;
334 
335  lydict_remove(ctx, value->_canonical);
336  value->_canonical = NULL;
337  LYD_VALUE_GET(value, val);
339 }
340 
349  {
350  .module = "ietf-inet-types",
351  .revision = "2013-07-15",
352  .name = "ipv6-prefix",
353 
354  .plugin.id = "libyang 2 - ipv6-prefix, version 1",
355  .plugin.store = lyplg_type_store_ipv6_prefix,
356  .plugin.validate = NULL,
357  .plugin.compare = lyplg_type_compare_ipv6_prefix,
358  .plugin.sort = lyplg_type_sort_ipv6_prefix,
359  .plugin.print = lyplg_type_print_ipv6_prefix,
360  .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
361  .plugin.free = lyplg_type_free_ipv6_prefix,
362  .plugin.lyb_data_len = LYB_VALUE_LEN,
363  },
364  {0}
365 };
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:348
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.
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.