libyang  2.1.30
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 #ifdef _WIN32
20 # include <winsock2.h>
21 # include <ws2tcpip.h>
22 #else
23 # include <arpa/inet.h>
24 # if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
25 # include <netinet/in.h>
26 # include <sys/socket.h>
27 # endif
28 #endif
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "libyang.h"
34 
35 #include "common.h"
36 #include "compat.h"
37 
48 #define LYB_VALUE_LEN 17
49 
50 static void lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
51 
62 static LY_ERR
63 ipv6prefix_str2ip(const char *value, size_t value_len, struct in6_addr *addr, uint8_t *prefix, 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), 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_EMEM, cleanup);
76 
77  /* convert it to netword-byte order */
78  if (inet_pton(AF_INET6, mask_str, addr) != 1) {
79  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert 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 
116 static LY_ERR
117 lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
118  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
119  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
120  struct ly_err_item **err)
121 {
122  LY_ERR ret = LY_SUCCESS;
123  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
124  struct lyd_value_ipv6_prefix *val;
125 
126  /* init storage */
127  memset(storage, 0, sizeof *storage);
128  storage->realtype = type;
129 
130  if (format == LY_VALUE_LYB) {
131  /* validation */
132  if (value_len != LYB_VALUE_LEN) {
133  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix value size %zu (expected %d).",
134  value_len, LYB_VALUE_LEN);
135  goto cleanup;
136  }
137  if (((uint8_t *)value)[16] > 128) {
138  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-prefix prefix length %" PRIu8 ".",
139  ((uint8_t *)value)[16]);
140  goto cleanup;
141  }
142 
143  /* store/allocate value */
144  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
145  storage->dyn_mem = (void *)value;
146  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
147 
148  LYD_VALUE_GET(storage, val);
149  } else {
150  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
151  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
152 
153  memcpy(val, value, value_len);
154  }
155 
156  /* zero host */
157  ipv6prefix_zero_host(&val->addr, val->prefix);
158 
159  /* success */
160  goto cleanup;
161  }
162 
163  /* allocate value */
164  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
165  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
166 
167  /* check hints */
168  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
169  LY_CHECK_GOTO(ret, cleanup);
170 
171  /* length restriction of the string */
172  if (type_str->length) {
173  /* value_len is in bytes, but we need number of characters here */
174  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
175  LY_CHECK_GOTO(ret, cleanup);
176  }
177 
178  /* pattern restrictions */
179  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
180  LY_CHECK_GOTO(ret, cleanup);
181 
182  /* get the mask in network-byte order */
183  ret = ipv6prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
184  LY_CHECK_GOTO(ret, cleanup);
185 
186  /* zero host */
187  ipv6prefix_zero_host(&val->addr, val->prefix);
188 
189  if (format == LY_VALUE_CANON) {
190  /* store canonical value */
191  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
192  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
193  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
194  LY_CHECK_GOTO(ret, cleanup);
195  } else {
196  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
197  LY_CHECK_GOTO(ret, cleanup);
198  }
199  }
200 
201 cleanup:
202  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
203  free((void *)value);
204  }
205 
206  if (ret) {
207  lyplg_type_free_ipv6_prefix(ctx, storage);
208  }
209  return ret;
210 }
211 
215 static LY_ERR
216 lyplg_type_compare_ipv6_prefix(const struct lyd_value *val1, const struct lyd_value *val2)
217 {
218  struct lyd_value_ipv6_prefix *v1, *v2;
219 
220  if (val1->realtype != val2->realtype) {
221  return LY_ENOT;
222  }
223 
224  LYD_VALUE_GET(val1, v1);
225  LYD_VALUE_GET(val2, v2);
226 
227  if (memcmp(v1, v2, sizeof *v1)) {
228  return LY_ENOT;
229  }
230  return LY_SUCCESS;
231 }
232 
236 static const void *
237 lyplg_type_print_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
238  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
239 {
240  struct lyd_value_ipv6_prefix *val;
241  char *ret;
242 
243  LYD_VALUE_GET(value, val);
244 
245  if (format == LY_VALUE_LYB) {
246  *dynamic = 0;
247  if (value_len) {
248  *value_len = LYB_VALUE_LEN;
249  }
250  return val;
251  }
252 
253  /* generate canonical value if not already */
254  if (!value->_canonical) {
255  /* IPv6 mask + '/' + prefix */
256  ret = malloc(INET6_ADDRSTRLEN + 4);
257  LY_CHECK_RET(!ret, NULL);
258 
259  /* convert back to string */
260  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
261  free(ret);
262  return NULL;
263  }
264 
265  /* add the prefix */
266  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
267 
268  /* store it */
269  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
270  LOGMEM(ctx);
271  return NULL;
272  }
273  }
274 
275  /* use the cached canonical value */
276  if (dynamic) {
277  *dynamic = 0;
278  }
279  if (value_len) {
280  *value_len = strlen(value->_canonical);
281  }
282  return value->_canonical;
283 }
284 
288 static LY_ERR
289 lyplg_type_dup_ipv6_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
290 {
291  LY_ERR ret;
292  struct lyd_value_ipv6_prefix *orig_val, *dup_val;
293 
294  memset(dup, 0, sizeof *dup);
295 
296  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
297  LY_CHECK_GOTO(ret, error);
298 
299  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
300  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
301 
302  LYD_VALUE_GET(original, orig_val);
303  memcpy(dup_val, orig_val, sizeof *orig_val);
304 
305  dup->realtype = original->realtype;
306  return LY_SUCCESS;
307 
308 error:
309  lyplg_type_free_ipv6_prefix(ctx, dup);
310  return ret;
311 }
312 
316 static void
317 lyplg_type_free_ipv6_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
318 {
319  struct lyd_value_ipv6_prefix *val;
320 
321  lydict_remove(ctx, value->_canonical);
322  value->_canonical = NULL;
323  LYD_VALUE_GET(value, val);
325 }
326 
335  {
336  .module = "ietf-inet-types",
337  .revision = "2013-07-15",
338  .name = "ipv6-prefix",
339 
340  .plugin.id = "libyang 2 - ipv6-prefix, version 1",
341  .plugin.store = lyplg_type_store_ipv6_prefix,
342  .plugin.validate = NULL,
343  .plugin.compare = lyplg_type_compare_ipv6_prefix,
344  .plugin.sort = NULL,
345  .plugin.print = lyplg_type_print_ipv6_prefix,
346  .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
347  .plugin.free = lyplg_type_free_ipv6_prefix,
348  .plugin.lyb_data_len = LYB_VALUE_LEN,
349  },
350  {0}
351 };
struct lysc_type * realtype
Definition: tree_data.h:564
Compiled YANG data node.
Definition: tree_schema.h:1414
LIBYANG_API_DECL 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:256
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
struct in6_addr addr
Definition: tree_data.h:689
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1327
struct lysc_range * length
Definition: tree_schema.h:1326
YANG data representation.
Definition: tree_data.h:560
const char * _canonical
Definition: tree_data.h:561
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:299
Definition: log.h:291
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:603
Definition: log.h:262
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:334
const char * module
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
Definition: log.h:268
Special lyd_value structure for ietf-inet-types ipv6-prefix values.
Definition: tree_data.h:688
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.
#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:234
#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:1299
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:254
#define LYB_VALUE_LEN
Definition: ipv6_prefix.c:48
API for (user) types plugins.
libyang context handler.
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.