libyang  4.0.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_address.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 <ctype.h>
32 #include <errno.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "libyang.h"
38 
39 #include "compat.h"
40 #include "ly_common.h"
41 
52 static void lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value);
53 
66 static LY_ERR
67 ipv6address_str2ip(const char *value, uint32_t value_len, uint32_t options, const struct ly_ctx *ctx,
68  struct in6_addr *addr, const char **zone, struct ly_err_item **err)
69 {
70  LY_ERR ret = LY_SUCCESS;
71  const char *addr_no_zone;
72  char *zone_ptr = NULL, *addr_dyn = NULL;
73  uint32_t zone_len;
74 
75  /* store zone and get the string IPv6 address without it */
76  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
77  /* there is a zone index */
78  zone_len = value_len - (zone_ptr - value) - 1;
79  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
80  LY_CHECK_GOTO(ret, cleanup);
81 
82  /* get the IP without it */
83  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
84  *zone_ptr = '\0';
85  addr_no_zone = value;
86  } else {
87  addr_dyn = strndup(value, zone_ptr - value);
88  addr_no_zone = addr_dyn;
89  }
90  } else {
91  /* no zone */
92  *zone = NULL;
93 
94  /* get the IP terminated with zero */
95  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
96  /* we can use the value directly */
97  addr_no_zone = value;
98  } else {
99  addr_dyn = strndup(value, value_len);
100  addr_no_zone = addr_dyn;
101  }
102  }
103 
104  /* store the IPv6 address in network-byte order */
105  if (!inet_pton(AF_INET6, addr_no_zone, addr)) {
106  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", addr_no_zone);
107  goto cleanup;
108  }
109 
110  /* restore the value */
111  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
112  *zone_ptr = '%';
113  }
114 
115 cleanup:
116  free(addr_dyn);
117  return ret;
118 }
119 
123 static LY_ERR
124 lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
125  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
126  const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
127  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
128 {
129  LY_ERR ret = LY_SUCCESS;
130  struct lyd_value_ipv6_address *val;
131  const char *value_str = value;
132  uint32_t value_size, i;
133 
134  /* init storage */
135  memset(storage, 0, sizeof *storage);
136  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
137  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
138  storage->realtype = type;
139 
140  value_size = LYPLG_BITS2BYTES(value_size_bits);
141 
142  if (format == LY_VALUE_LYB) {
143  /* validation */
144  if (value_size_bits < 128) {
145  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %" PRIu32
146  " b (expected at least 128 b).", value_size_bits);
147  goto cleanup;
148  }
149  for (i = 16; i < value_size; ++i) {
150  if (!isalnum(value_str[i])) {
151  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
152  value_str[i]);
153  goto cleanup;
154  }
155  }
156 
157  /* store IP address */
158  memcpy(&val->addr, value, sizeof val->addr);
159 
160  /* store zone, if any */
161  if (value_size > 16) {
162  ret = lydict_insert(ctx, value_str + 16, value_size - 16, &val->zone);
163  LY_CHECK_GOTO(ret, cleanup);
164  } else {
165  val->zone = NULL;
166  }
167 
168  /* success */
169  goto cleanup;
170  }
171 
172  /* check hints */
173  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
174  LY_CHECK_GOTO(ret, cleanup);
175 
176  /* get the network-byte order address */
177  ret = ipv6address_str2ip(value, value_size, options, ctx, &val->addr, &val->zone, err);
178  LY_CHECK_GOTO(ret, cleanup);
179 
180  if (format == LY_VALUE_CANON) {
181  /* store canonical value */
182  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
183  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
184  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
185  LY_CHECK_GOTO(ret, cleanup);
186  } else {
187  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
188  LY_CHECK_GOTO(ret, cleanup);
189  }
190  }
191 
192 cleanup:
193  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
194  free((void *)value);
195  }
196 
197  if (ret) {
198  lyplg_type_free_ipv6_address(ctx, storage);
199  }
200  return ret;
201 }
202 
206 static LY_ERR
207 lyplg_type_compare_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
208  const struct lyd_value *val2)
209 {
210  struct lyd_value_ipv6_address *v1, *v2;
211 
212  LYD_VALUE_GET(val1, v1);
213  LYD_VALUE_GET(val2, v2);
214 
215  /* zones are NULL or in the dictionary */
216  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
217  return LY_ENOT;
218  }
219  return LY_SUCCESS;
220 }
221 
225 static int
226 lyplg_type_sort_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
227  const struct lyd_value *val2)
228 {
229  struct lyd_value_ipv6_address *v1, *v2;
230  int cmp;
231 
232  LYD_VALUE_GET(val1, v1);
233  LYD_VALUE_GET(val2, v2);
234 
235  cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
236  if (cmp != 0) {
237  return cmp;
238  }
239 
240  if (!v1->zone && v2->zone) {
241  return -1;
242  } else if (v1->zone && !v2->zone) {
243  return 1;
244  } else if (v1->zone && v2->zone) {
245  return strcmp(v1->zone, v2->zone);
246  }
247 
248  return 0;
249 }
250 
254 static const void *
255 lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
256  void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
257 {
258  struct lyd_value_ipv6_address *val;
259  uint32_t zone_len;
260  char *ret;
261 
262  LYD_VALUE_GET(value, val);
263 
264  if (format == LY_VALUE_LYB) {
265  if (!val->zone) {
266  /* address-only, const */
267  *dynamic = 0;
268  if (value_size_bits) {
269  *value_size_bits = sizeof val->addr * 8;
270  }
271  return &val->addr;
272  }
273 
274  /* dynamic */
275  zone_len = strlen(val->zone);
276  ret = malloc(sizeof val->addr + zone_len);
277  LY_CHECK_RET(!ret, NULL);
278 
279  memcpy(ret, &val->addr, sizeof val->addr);
280  memcpy(ret + sizeof val->addr, val->zone, zone_len);
281 
282  *dynamic = 1;
283  if (value_size_bits) {
284  *value_size_bits = sizeof val->addr * 8 + zone_len * 8;
285  }
286  return ret;
287  }
288 
289  /* generate canonical value if not already */
290  if (!value->_canonical) {
291  /* '%' + zone */
292  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
293  ret = malloc(INET6_ADDRSTRLEN + zone_len);
294  LY_CHECK_RET(!ret, NULL);
295 
296  /* get the address in string */
297  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
298  free(ret);
299  LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
300  return NULL;
301  }
302 
303  /* add zone */
304  if (zone_len) {
305  sprintf(ret + strlen(ret), "%%%s", val->zone);
306  }
307 
308  /* store it */
309  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
310  LOGMEM(ctx);
311  return NULL;
312  }
313  }
314 
315  /* use the cached canonical value */
316  if (dynamic) {
317  *dynamic = 0;
318  }
319  if (value_size_bits) {
320  *value_size_bits = strlen(value->_canonical) * 8;
321  }
322  return value->_canonical;
323 }
324 
328 static LY_ERR
329 lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
330 {
331  LY_ERR ret;
332  struct lyd_value_ipv6_address *orig_val, *dup_val;
333 
334  memset(dup, 0, sizeof *dup);
335 
336  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
337  LY_CHECK_GOTO(ret, error);
338 
339  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
340  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
341 
342  LYD_VALUE_GET(original, orig_val);
343  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
344  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
345  LY_CHECK_GOTO(ret, error);
346 
347  dup->realtype = original->realtype;
348  return LY_SUCCESS;
349 
350 error:
351  lyplg_type_free_ipv6_address(ctx, dup);
352  return ret;
353 }
354 
358 static void
359 lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
360 {
361  struct lyd_value_ipv6_address *val;
362 
363  lydict_remove(ctx, value->_canonical);
364  value->_canonical = NULL;
365  LYD_VALUE_GET(value, val);
366  if (val) {
367  lydict_remove(ctx, val->zone);
369  }
370 }
371 
380  {
381  .module = "ietf-inet-types",
382  .revision = "2013-07-15",
383  .name = "ipv6-address",
384 
385  .plugin.id = "ly2 ipv6-address",
386  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
387  .plugin.store = lyplg_type_store_ipv6_address,
388  .plugin.validate_value = NULL,
389  .plugin.validate_tree = NULL,
390  .plugin.compare = lyplg_type_compare_ipv6_address,
391  .plugin.sort = lyplg_type_sort_ipv6_address,
392  .plugin.print = lyplg_type_print_ipv6_address,
393  .plugin.duplicate = lyplg_type_dup_ipv6_address,
394  .plugin.free = lyplg_type_free_ipv6_address,
395  },
396  {0}
397 };
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
const char * zone
Definition: tree_data.h:686
YANG extension compiled instance.
Definition: plugins_exts.h:436
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:254
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
Definition: log.h:243
int cmp
Definition: binary.c:394
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:242
The main libyang public header.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
Special lyd_value structure for ietf-inet-types ipv6-address values.
Definition: tree_data.h:684
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
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:606
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...
struct in6_addr addr
Definition: tree_data.h:685
Definition: log.h:248
const char * module
struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address type implementation.
Definition: ipv6_address.c:379
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
#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:1300
return memcmp(v1->data, v2->data, bitmap_size)
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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.
API for (user) types plugins.
libyang context handler.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.