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