libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
ipv6_prefix.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* asprintf, strdup */
16 #include <sys/cdefs.h>
17 
18 #include "plugins_types.h"
19 
20 #include <arpa/inet.h>
21 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #endif
25 #include <assert.h>
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "libyang.h"
32 
33 #include "common.h"
34 #include "compat.h"
35 
46 static void lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
47 
58 static LY_ERR
59 ipv6prefix_str2ip(const char *value, size_t value_len, struct in6_addr *addr, uint8_t *prefix, struct ly_err_item **err)
60 {
61  LY_ERR ret = LY_SUCCESS;
62  const char *pref_str;
63  char *mask_str = NULL;
64 
65  /* it passed the pattern validation */
66  pref_str = ly_strnchr(value, '/', value_len);
67  ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
68 
69  /* get just the network prefix */
70  mask_str = strndup(value, pref_str - value);
71  LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
72 
73  /* convert it to netword-byte order */
74  if (inet_pton(AF_INET6, mask_str, addr) != 1) {
75  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", mask_str);
76  goto cleanup;
77  }
78 
79 cleanup:
80  free(mask_str);
81  return ret;
82 }
83 
90 static void
91 ipv6prefix_zero_host(struct in6_addr *addr, uint8_t prefix)
92 {
93  uint32_t i, j, mask;
94 
95  /* zero host bits */
96  for (i = 0; i < 4; ++i) {
97  mask = 0;
98  for (j = 0; j < 32; ++j) {
99  mask <<= 1;
100  if (prefix > (i * 32) + j) {
101  mask |= 1;
102  }
103  }
104  mask = htonl(mask);
105  ((uint32_t *)addr->s6_addr)[i] &= mask;
106  }
107 }
108 
112 static LY_ERR
113 lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
114  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
115  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
116  struct ly_err_item **err)
117 {
118  LY_ERR ret = LY_SUCCESS;
119  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
120  struct lyd_value_ipv6_prefix *val;
121 
122  /* init storage */
123  memset(storage, 0, sizeof *storage);
124  storage->realtype = type;
125 
126  if (format == LY_VALUE_LYB) {
127  /* validation */
128  if (value_len != 17) {
129  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix value size %zu (expected 17).",
130  value_len);
131  goto cleanup;
132  }
133  if (((uint8_t *)value)[16] > 128) {
134  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix prefix length %" PRIu8 ".",
135  ((uint8_t *)value)[16]);
136  goto cleanup;
137  }
138 
139  /* store/allocate value */
140  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
141  storage->dyn_mem = (void *)value;
142  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
143 
144  LYD_VALUE_GET(storage, val);
145  } else {
146  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
147  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
148 
149  memcpy(val, value, value_len);
150  }
151 
152  /* zero host */
153  ipv6prefix_zero_host(&val->addr, val->prefix);
154 
155  /* success */
156  goto cleanup;
157  }
158 
159  /* allocate value */
160  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
161  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
162 
163  /* check hints */
164  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
165  LY_CHECK_GOTO(ret, cleanup);
166 
167  /* length restriction of the string */
168  if (type_str->length) {
169  /* value_len is in bytes, but we need number of characters here */
170  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
171  LY_CHECK_GOTO(ret, cleanup);
172  }
173 
174  /* pattern restrictions */
175  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
176  LY_CHECK_GOTO(ret, cleanup);
177 
178  /* get the mask in network-byte order */
179  ret = ipv6prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
180  LY_CHECK_GOTO(ret, cleanup);
181 
182  /* zero host */
183  ipv6prefix_zero_host(&val->addr, val->prefix);
184 
185  if (format == LY_VALUE_CANON) {
186  /* store canonical value */
187  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
188  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
189  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
190  LY_CHECK_GOTO(ret, cleanup);
191  } else {
192  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
193  LY_CHECK_GOTO(ret, cleanup);
194  }
195  }
196 
197 cleanup:
198  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
199  free((void *)value);
200  }
201 
202  if (ret) {
203  lyplg_type_free_ipv6_prefix(ctx, storage);
204  }
205  return ret;
206 }
207 
211 static LY_ERR
212 lyplg_type_compare_ipv6_prefix(const struct lyd_value *val1, const struct lyd_value *val2)
213 {
214  struct lyd_value_ipv6_prefix *v1, *v2;
215 
216  if (val1->realtype != val2->realtype) {
217  return LY_ENOT;
218  }
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 
232 static const void *
233 lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
234  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
235 {
236  struct lyd_value_ipv6_prefix *val;
237  char *ret;
238 
239  LYD_VALUE_GET(value, val);
240 
241  if (format == LY_VALUE_LYB) {
242  *dynamic = 0;
243  if (value_len) {
244  *value_len = sizeof *val;
245  }
246  return val;
247  }
248 
249  /* generate canonical value if not already */
250  if (!value->_canonical) {
251  /* IPv6 mask + '/' + prefix */
252  ret = malloc(INET6_ADDRSTRLEN + 4);
253  LY_CHECK_RET(!ret, NULL);
254 
255  /* convert back to string */
256  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
257  free(ret);
258  return NULL;
259  }
260 
261  /* add the prefix */
262  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
263 
264  /* store it */
265  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
266  LOGMEM(ctx);
267  return NULL;
268  }
269  }
270 
271  /* use the cached canonical value */
272  if (dynamic) {
273  *dynamic = 0;
274  }
275  if (value_len) {
276  *value_len = strlen(value->_canonical);
277  }
278  return value->_canonical;
279 }
280 
284 static LY_ERR
285 lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
286 {
287  LY_ERR ret;
288  struct lyd_value_ipv6_prefix *orig_val, *dup_val;
289 
290  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
291  LY_CHECK_RET(ret);
292 
293  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
294  if (!dup_val) {
295  lydict_remove(ctx, dup->_canonical);
296  return LY_EMEM;
297  }
298 
299  LYD_VALUE_GET(original, orig_val);
300  memcpy(dup_val, orig_val, sizeof *orig_val);
301 
302  dup->realtype = original->realtype;
303  return LY_SUCCESS;
304 }
305 
309 static void
310 lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
311 {
312  struct lyd_value_ipv6_prefix *val;
313 
314  lydict_remove(ctx, value->_canonical);
315  LYD_VALUE_GET(value, val);
317 }
318 
326 const struct lyplg_type_record plugins_ipv6_prefix[] = {
327  {
328  .module = "ietf-inet-types",
329  .revision = "2013-07-15",
330  .name = "ipv6-prefix",
331 
332  .plugin.id = "libyang 2 - ipv6-prefix, version 1",
333  .plugin.store = lyplg_type_store_ipv6_prefix,
334  .plugin.validate = NULL,
335  .plugin.compare = lyplg_type_compare_ipv6_prefix,
336  .plugin.sort = NULL,
337  .plugin.print = lyplg_type_print_ipv6_prefix,
338  .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
339  .plugin.free = lyplg_type_free_ipv6_prefix
340  },
341  {0}
342 };
libyang context handler.
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 ...
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 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...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_EMEM
Definition: log.h:244
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
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.
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's hints (if any) in the specified format.
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
struct lysc_pattern ** patterns
Definition: tree_schema.h:1558
struct lysc_range * length
Definition: tree_schema.h:1557
Compiled YANG data node.
Definition: tree_schema.h:1644
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
@ LY_TYPE_STRING
Definition: tree.h:210
@ LY_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
const struct lyplg_type_record plugins_ipv6_prefix[]
Plugin information for ipv6-prefix type implementation.
Definition: ipv6_prefix.c:326
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:535
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:573
struct in6_addr addr
Definition: tree_data.h:659
const char * _canonical
Definition: tree_data.h:532
YANG data representation.
Definition: tree_data.h:531
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition: tree_data.h:658
#define LOGMEM(CTX)
Definition: tree_edit.h:25