libyang  2.2.8
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  /* length restriction of the string */
176  if (type_str->length) {
177  /* value_len is in bytes, but we need number of characters here */
178  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
179  LY_CHECK_GOTO(ret, cleanup);
180  }
181 
182  /* pattern restrictions */
183  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
184  LY_CHECK_GOTO(ret, cleanup);
185 
186  /* get the network-byte order address */
187  ret = ipv6address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
188  LY_CHECK_GOTO(ret, cleanup);
189 
190  if (format == LY_VALUE_CANON) {
191  /* store canonical value */
192  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
193  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
194  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
195  LY_CHECK_GOTO(ret, cleanup);
196  } else {
197  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
198  LY_CHECK_GOTO(ret, cleanup);
199  }
200  }
201 
202 cleanup:
203  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
204  free((void *)value);
205  }
206 
207  if (ret) {
208  lyplg_type_free_ipv6_address(ctx, storage);
209  }
210  return ret;
211 }
212 
216 static LY_ERR
217 lyplg_type_compare_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
218  const struct lyd_value *val2)
219 {
220  struct lyd_value_ipv6_address *v1, *v2;
221 
222  LYD_VALUE_GET(val1, v1);
223  LYD_VALUE_GET(val2, v2);
224 
225  /* zones are NULL or in the dictionary */
226  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
227  return LY_ENOT;
228  }
229  return LY_SUCCESS;
230 }
231 
235 static int
236 lyplg_type_sort_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
237  const struct lyd_value *val2)
238 {
239  struct lyd_value_ipv6_address *v1, *v2;
240  int cmp;
241 
242  LYD_VALUE_GET(val1, v1);
243  LYD_VALUE_GET(val2, v2);
244 
245  cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
246  if (cmp != 0) {
247  return cmp;
248  }
249 
250  if (!v1->zone && v2->zone) {
251  return -1;
252  } else if (v1->zone && !v2->zone) {
253  return 1;
254  } else if (v1->zone && v2->zone) {
255  return strcmp(v1->zone, v2->zone);
256  }
257 
258  return 0;
259 }
260 
264 static const void *
265 lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
266  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
267 {
268  struct lyd_value_ipv6_address *val;
269  size_t zone_len;
270  char *ret;
271 
272  LYD_VALUE_GET(value, val);
273 
274  if (format == LY_VALUE_LYB) {
275  if (!val->zone) {
276  /* address-only, const */
277  *dynamic = 0;
278  if (value_len) {
279  *value_len = sizeof val->addr;
280  }
281  return &val->addr;
282  }
283 
284  /* dynamic */
285  zone_len = strlen(val->zone);
286  ret = malloc(sizeof val->addr + zone_len);
287  LY_CHECK_RET(!ret, NULL);
288 
289  memcpy(ret, &val->addr, sizeof val->addr);
290  memcpy(ret + sizeof val->addr, val->zone, zone_len);
291 
292  *dynamic = 1;
293  if (value_len) {
294  *value_len = sizeof val->addr + zone_len;
295  }
296  return ret;
297  }
298 
299  /* generate canonical value if not already */
300  if (!value->_canonical) {
301  /* '%' + zone */
302  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
303  ret = malloc(INET6_ADDRSTRLEN + zone_len);
304  LY_CHECK_RET(!ret, NULL);
305 
306  /* get the address in string */
307  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
308  free(ret);
309  LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
310  return NULL;
311  }
312 
313  /* add zone */
314  if (zone_len) {
315  sprintf(ret + strlen(ret), "%%%s", val->zone);
316  }
317 
318  /* store it */
319  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
320  LOGMEM(ctx);
321  return NULL;
322  }
323  }
324 
325  /* use the cached canonical value */
326  if (dynamic) {
327  *dynamic = 0;
328  }
329  if (value_len) {
330  *value_len = strlen(value->_canonical);
331  }
332  return value->_canonical;
333 }
334 
338 static LY_ERR
339 lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
340 {
341  LY_ERR ret;
342  struct lyd_value_ipv6_address *orig_val, *dup_val;
343 
344  memset(dup, 0, sizeof *dup);
345 
346  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
347  LY_CHECK_GOTO(ret, error);
348 
349  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
350  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
351 
352  LYD_VALUE_GET(original, orig_val);
353  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
354  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
355  LY_CHECK_GOTO(ret, error);
356 
357  dup->realtype = original->realtype;
358  return LY_SUCCESS;
359 
360 error:
361  lyplg_type_free_ipv6_address(ctx, dup);
362  return ret;
363 }
364 
368 static void
369 lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
370 {
371  struct lyd_value_ipv6_address *val;
372 
373  lydict_remove(ctx, value->_canonical);
374  value->_canonical = NULL;
375  LYD_VALUE_GET(value, val);
376  if (val) {
377  lydict_remove(ctx, val->zone);
379  }
380 }
381 
390  {
391  .module = "ietf-inet-types",
392  .revision = "2013-07-15",
393  .name = "ipv6-address",
394 
395  .plugin.id = "libyang 2 - ipv6-address, version 1",
396  .plugin.store = lyplg_type_store_ipv6_address,
397  .plugin.validate = NULL,
398  .plugin.compare = lyplg_type_compare_ipv6_address,
399  .plugin.sort = lyplg_type_sort_ipv6_address,
400  .plugin.print = lyplg_type_print_ipv6_address,
401  .plugin.duplicate = lyplg_type_dup_ipv6_address,
402  .plugin.free = lyplg_type_free_ipv6_address,
403  .plugin.lyb_data_len = -1,
404  },
405  {0}
406 };
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:382
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:389
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.
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.