libyang  3.4.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, size_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  size_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 convert 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, size_t value_len,
125  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
126  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
127  struct ly_err_item **err)
128 {
129  LY_ERR ret = LY_SUCCESS;
130  const char *value_str = value;
131  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
132  struct lyd_value_ipv6_address *val;
133  size_t i;
134 
135  /* init storage */
136  memset(storage, 0, sizeof *storage);
137  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
138  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
139  storage->realtype = type;
140 
141  if (format == LY_VALUE_LYB) {
142  /* validation */
143  if (value_len < 16) {
144  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %zu "
145  "(expected at least 16).", value_len);
146  goto cleanup;
147  }
148  for (i = 16; i < value_len; ++i) {
149  if (!isalnum(value_str[i])) {
150  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
151  value_str[i]);
152  goto cleanup;
153  }
154  }
155 
156  /* store IP address */
157  memcpy(&val->addr, value, sizeof val->addr);
158 
159  /* store zone, if any */
160  if (value_len > 16) {
161  ret = lydict_insert(ctx, value_str + 16, value_len - 16, &val->zone);
162  LY_CHECK_GOTO(ret, cleanup);
163  } else {
164  val->zone = NULL;
165  }
166 
167  /* success */
168  goto cleanup;
169  }
170 
171  /* check hints */
172  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
173  LY_CHECK_GOTO(ret, cleanup);
174 
175  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
176  /* length restriction of the string */
177  if (type_str->length) {
178  /* value_len is in bytes, but we need number of characters here */
179  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
180  LY_CHECK_GOTO(ret, cleanup);
181  }
182 
183  /* pattern restrictions */
184  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
185  LY_CHECK_GOTO(ret, cleanup);
186  }
187 
188  /* get the network-byte order address */
189  ret = ipv6address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
190  LY_CHECK_GOTO(ret, cleanup);
191 
192  if (format == LY_VALUE_CANON) {
193  /* store canonical value */
194  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
195  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
196  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
197  LY_CHECK_GOTO(ret, cleanup);
198  } else {
199  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
200  LY_CHECK_GOTO(ret, cleanup);
201  }
202  }
203 
204 cleanup:
205  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
206  free((void *)value);
207  }
208 
209  if (ret) {
210  lyplg_type_free_ipv6_address(ctx, storage);
211  }
212  return ret;
213 }
214 
218 static LY_ERR
219 lyplg_type_compare_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
220  const struct lyd_value *val2)
221 {
222  struct lyd_value_ipv6_address *v1, *v2;
223 
224  LYD_VALUE_GET(val1, v1);
225  LYD_VALUE_GET(val2, v2);
226 
227  /* zones are NULL or in the dictionary */
228  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
229  return LY_ENOT;
230  }
231  return LY_SUCCESS;
232 }
233 
237 static int
238 lyplg_type_sort_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
239  const struct lyd_value *val2)
240 {
241  struct lyd_value_ipv6_address *v1, *v2;
242  int cmp;
243 
244  LYD_VALUE_GET(val1, v1);
245  LYD_VALUE_GET(val2, v2);
246 
247  cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
248  if (cmp != 0) {
249  return cmp;
250  }
251 
252  if (!v1->zone && v2->zone) {
253  return -1;
254  } else if (v1->zone && !v2->zone) {
255  return 1;
256  } else if (v1->zone && v2->zone) {
257  return strcmp(v1->zone, v2->zone);
258  }
259 
260  return 0;
261 }
262 
266 static const void *
267 lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
268  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
269 {
270  struct lyd_value_ipv6_address *val;
271  size_t zone_len;
272  char *ret;
273 
274  LYD_VALUE_GET(value, val);
275 
276  if (format == LY_VALUE_LYB) {
277  if (!val->zone) {
278  /* address-only, const */
279  *dynamic = 0;
280  if (value_len) {
281  *value_len = sizeof val->addr;
282  }
283  return &val->addr;
284  }
285 
286  /* dynamic */
287  zone_len = strlen(val->zone);
288  ret = malloc(sizeof val->addr + zone_len);
289  LY_CHECK_RET(!ret, NULL);
290 
291  memcpy(ret, &val->addr, sizeof val->addr);
292  memcpy(ret + sizeof val->addr, val->zone, zone_len);
293 
294  *dynamic = 1;
295  if (value_len) {
296  *value_len = sizeof val->addr + zone_len;
297  }
298  return ret;
299  }
300 
301  /* generate canonical value if not already */
302  if (!value->_canonical) {
303  /* '%' + zone */
304  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
305  ret = malloc(INET6_ADDRSTRLEN + zone_len);
306  LY_CHECK_RET(!ret, NULL);
307 
308  /* get the address in string */
309  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
310  free(ret);
311  LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
312  return NULL;
313  }
314 
315  /* add zone */
316  if (zone_len) {
317  sprintf(ret + strlen(ret), "%%%s", val->zone);
318  }
319 
320  /* store it */
321  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
322  LOGMEM(ctx);
323  return NULL;
324  }
325  }
326 
327  /* use the cached canonical value */
328  if (dynamic) {
329  *dynamic = 0;
330  }
331  if (value_len) {
332  *value_len = strlen(value->_canonical);
333  }
334  return value->_canonical;
335 }
336 
340 static LY_ERR
341 lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
342 {
343  LY_ERR ret;
344  struct lyd_value_ipv6_address *orig_val, *dup_val;
345 
346  memset(dup, 0, sizeof *dup);
347 
348  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
349  LY_CHECK_GOTO(ret, error);
350 
351  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
352  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
353 
354  LYD_VALUE_GET(original, orig_val);
355  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
356  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
357  LY_CHECK_GOTO(ret, error);
358 
359  dup->realtype = original->realtype;
360  return LY_SUCCESS;
361 
362 error:
363  lyplg_type_free_ipv6_address(ctx, dup);
364  return ret;
365 }
366 
370 static void
371 lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
372 {
373  struct lyd_value_ipv6_address *val;
374 
375  lydict_remove(ctx, value->_canonical);
376  value->_canonical = NULL;
377  LYD_VALUE_GET(value, val);
378  if (val) {
379  lydict_remove(ctx, val->zone);
381  }
382 }
383 
392  {
393  .module = "ietf-inet-types",
394  .revision = "2013-07-15",
395  .name = "ipv6-address",
396 
397  .plugin.id = "libyang 2 - ipv6-address, version 1",
398  .plugin.store = lyplg_type_store_ipv6_address,
399  .plugin.validate = NULL,
400  .plugin.compare = lyplg_type_compare_ipv6_address,
401  .plugin.sort = lyplg_type_sort_ipv6_address,
402  .plugin.print = lyplg_type_print_ipv6_address,
403  .plugin.duplicate = lyplg_type_dup_ipv6_address,
404  .plugin.free = lyplg_type_free_ipv6_address,
405  .plugin.lyb_data_len = -1,
406  },
407  {0}
408 };
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)
const char * zone
Definition: tree_data.h:693
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
int cmp
Definition: binary.c:359
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1339
Special lyd_value structure for ietf-inet-types ipv6-address values.
Definition: tree_data.h:691
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
Definition: log.h:243
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:692
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.
const char * module
struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address type implementation.
Definition: ipv6_address.c:391
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
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
#define LYPLG_TYPE_STORE_ONLY
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.