libyang  6.4.41
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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 "compat.h"
38 #include "dict.h"
39 #include "ly_common.h"
50 static void lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value);
51 
63 static LY_ERR
64 ipv6address_str2ip(const char *value, uint32_t value_len, uint32_t options, const struct ly_ctx *ctx,
65  struct lyd_value_ipv6_address *val, struct ly_err_item **err)
66 {
67  LY_ERR ret = LY_SUCCESS;
68  const char *addr_no_zone;
69  char *zone_ptr = NULL, *addr_dyn = NULL;
70  uint32_t zone_len;
71 
72  /* store zone and get the string IPv6 address without it */
73  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
74  /* there is a zone index */
75  zone_len = value_len - (zone_ptr - value) - 1;
76  if (!zone_len) {
77  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Missing zone following '%%' in IPv6 address \"%.*s\".",
78  (int)value_len, value);
79  goto cleanup;
80  }
81  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, &val->zone);
82  LY_CHECK_GOTO(ret, cleanup);
83 
84  /* get the IP without it */
85  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
86  *zone_ptr = '\0';
87  addr_no_zone = value;
88  } else {
89  addr_dyn = strndup(value, zone_ptr - value);
90  addr_no_zone = addr_dyn;
91  }
92  } else {
93  /* no zone */
94  val->zone = NULL;
95 
96  /* get the IP terminated with zero */
97  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
98  /* we can use the value directly */
99  addr_no_zone = value;
100  } else {
101  addr_dyn = strndup(value, value_len);
102  addr_no_zone = addr_dyn;
103  }
104  }
105 
106  /* store the IPv6 address in network-byte order */
107  if (!inet_pton(AF_INET6, addr_no_zone, &val->addr)) {
108  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", addr_no_zone);
109  goto cleanup;
110  }
111 
112  /* restore the value */
113  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
114  *zone_ptr = '%';
115  }
116 
117 cleanup:
118  free(addr_dyn);
119  return ret;
120 }
121 
125 static LY_ERR
126 lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
127  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
128  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
129  struct ly_err_item **err)
130 {
131  LY_ERR ret = LY_SUCCESS;
132  struct lyd_value_ipv6_address *val;
133  const char *value_str = value;
134  uint32_t value_size, i;
135 
136  /* init storage */
137  memset(storage, 0, sizeof *storage);
138  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
139  LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
140  storage->realtype = type;
141 
142  value_size = LYPLG_BITS2BYTES(value_size_bits);
143 
144  if (format == LY_VALUE_LYB) {
145  /* validation */
146  if (value_size_bits < 128) {
147  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %" PRIu64
148  " b (expected at least 128 b).", value_size_bits);
149  goto cleanup;
150  }
151  for (i = 16; i < value_size; ++i) {
152  if (!isalnum(value_str[i])) {
153  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
154  value_str[i]);
155  goto cleanup;
156  }
157  }
158 
159  /* store IP address */
160  memcpy(&val->addr, value, sizeof val->addr);
161 
162  /* store zone, if any */
163  if (value_size > 16) {
164  ret = lydict_insert(ctx, value_str + 16, value_size - 16, &val->zone);
165  LY_CHECK_GOTO(ret, cleanup);
166  } else {
167  val->zone = NULL;
168  }
169 
170  /* success */
171  goto cleanup;
172  }
173 
174  /* check hints */
175  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
176  LY_CHECK_GOTO(ret, cleanup);
177 
178  /* get the network-byte order address */
179  ret = ipv6address_str2ip(value, value_size, options, ctx, val, err);
180  LY_CHECK_GOTO(ret, cleanup);
181 
182  if (format == LY_VALUE_CANON) {
183  /* store canonical value */
184  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
185  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
186  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
187  LY_CHECK_GOTO(ret, cleanup);
188  } else {
189  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
190  LY_CHECK_GOTO(ret, cleanup);
191  }
192  }
193 
194 cleanup:
195  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
196  free((void *)value);
197  }
198 
199  if (ret) {
200  lyplg_type_free_ipv6_address(ctx, storage);
201  }
202  return ret;
203 }
204 
208 static LY_ERR
209 lyplg_type_compare_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
210  const struct lyd_value *val2)
211 {
212  struct lyd_value_ipv6_address *v1, *v2;
213 
214  LYD_VALUE_GET(val1, v1);
215  LYD_VALUE_GET(val2, v2);
216 
217  /* zones are NULL or in the dictionary */
218  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
219  return LY_ENOT;
220  }
221  return LY_SUCCESS;
222 }
223 
227 static int
228 lyplg_type_sort_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
229  const struct lyd_value *val2)
230 {
231  struct lyd_value_ipv6_address *v1, *v2;
232  int cmp;
233 
234  LYD_VALUE_GET(val1, v1);
235  LYD_VALUE_GET(val2, v2);
236 
237  cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
238  if (cmp != 0) {
239  return cmp;
240  }
241 
242  if (!v1->zone && v2->zone) {
243  return -1;
244  } else if (v1->zone && !v2->zone) {
245  return 1;
246  } else if (v1->zone && v2->zone) {
247  return strcmp(v1->zone, v2->zone);
248  }
249 
250  return 0;
251 }
252 
256 static const void *
257 lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
258  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
259 {
260  struct lyd_value_ipv6_address *val;
261  uint32_t zone_len, ret_size, ret_len;
262  char *ret;
263 
264  LYD_VALUE_GET(value, val);
265 
266  if (format == LY_VALUE_LYB) {
267  if (!val->zone) {
268  /* address-only, const */
269  *dynamic = 0;
270  if (value_size_bits) {
271  *value_size_bits = sizeof val->addr * 8;
272  }
273  return &val->addr;
274  }
275 
276  /* dynamic */
277  zone_len = strlen(val->zone);
278  ret = malloc(sizeof val->addr + zone_len);
279  LY_CHECK_RET(!ret, NULL);
280 
281  memcpy(ret, &val->addr, sizeof val->addr);
282  memcpy(ret + sizeof val->addr, val->zone, zone_len);
283 
284  *dynamic = 1;
285  if (value_size_bits) {
286  *value_size_bits = sizeof val->addr * 8 + zone_len * 8;
287  }
288  return ret;
289  }
290 
291  /* generate canonical value if not already */
292  if (!value->_canonical) {
293  /* '%' + zone */
294  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
295  ret_size = INET6_ADDRSTRLEN + zone_len;
296  ret = malloc(ret_size);
297  LY_CHECK_RET(!ret, NULL);
298 
299  /* get the address in string */
300  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
301  free(ret);
302  LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
303  return NULL;
304  }
305 
306  /* add zone */
307  if (zone_len) {
308  ret_len = strlen(ret);
309  snprintf(ret + ret_len, ret_size - ret_len, "%%%s", val->zone);
310  }
311 
312  /* store it */
313  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
314  LOGMEM(ctx);
315  return NULL;
316  }
317  }
318 
319  /* use the cached canonical value */
320  if (dynamic) {
321  *dynamic = 0;
322  }
323  if (value_size_bits) {
324  *value_size_bits = strlen(value->_canonical) * 8;
325  }
326  return value->_canonical;
327 }
328 
332 static LY_ERR
333 lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
334 {
335  LY_ERR ret;
336  struct lyd_value_ipv6_address *orig_val, *dup_val;
337 
338  memset(dup, 0, sizeof *dup);
339 
340  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
341  LY_CHECK_GOTO(ret, error);
342 
343  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
344  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
345 
346  LYD_VALUE_GET(original, orig_val);
347  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
348  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
349  LY_CHECK_GOTO(ret, error);
350 
351  dup->realtype = original->realtype;
352  return LY_SUCCESS;
353 
354 error:
355  lyplg_type_free_ipv6_address(ctx, dup);
356  return ret;
357 }
358 
362 static void
363 lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
364 {
365  struct lyd_value_ipv6_address *val;
366 
367  lydict_remove(ctx, value->_canonical);
368  value->_canonical = NULL;
369  LYD_VALUE_GET(value, val);
370  if (val) {
371  lydict_remove(ctx, val->zone);
373  }
374 }
375 
383 const struct lyplg_type_record plugins_ipv6_address[] = {
384  {
385  .module = "ietf-inet-types",
386  .revision = NULL,
387  .name = "ipv6-address",
388 
389  .plugin.id = "ly2 ipv6-address",
390  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
391  .plugin.store = lyplg_type_store_ipv6_address,
392  .plugin.validate_value = NULL,
393  .plugin.validate_tree = NULL,
394  .plugin.compare = lyplg_type_compare_ipv6_address,
395  .plugin.sort = lyplg_type_sort_ipv6_address,
396  .plugin.print = lyplg_type_print_ipv6_address,
397  .plugin.duplicate = lyplg_type_dup_ipv6_address,
398  .plugin.free = lyplg_type_free_ipv6_address,
399  },
400  {
401  .module = "ietf-inet-types",
402  .revision = NULL,
403  .name = "ipv6-address-link-local",
404 
405  .plugin.id = "ly2 ipv6-address",
406  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
407  .plugin.store = lyplg_type_store_ipv6_address,
408  .plugin.validate_value = NULL,
409  .plugin.validate_tree = NULL,
410  .plugin.compare = lyplg_type_compare_ipv6_address,
411  .plugin.sort = lyplg_type_sort_ipv6_address,
412  .plugin.print = lyplg_type_print_ipv6_address,
413  .plugin.duplicate = lyplg_type_dup_ipv6_address,
414  .plugin.free = lyplg_type_free_ipv6_address,
415  },
416  {0}
417 };
libyang dictionary
libyang context handler.
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 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_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:252
@ LYVE_DATA
Definition: log.h:290
@ LY_ESYS
Definition: log.h:255
@ LY_EMEM
Definition: log.h:254
@ LY_ENOT
Definition: log.h:266
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
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's hints (if any) in the specified format.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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 an error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
Compiled YANG data node.
Definition: tree_schema.h:1412
const struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address and ipv6-address-link-local type implementation.
Definition: ipv6_address.c:383
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
struct in6_addr addr
Definition: tree_data.h:731
const char * zone
Definition: tree_data.h:732
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:653
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
Special lyd_value structure for ietf-inet-types ipv6-address values.
Definition: tree_data.h:730
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_LYB
Definition: utils.h:100