libyang  2.0.7
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 /* asprintf, strdup */
16 #include <sys/cdefs.h>
17 
18 #include "plugins_types.h"
19 
20 #include <arpa/inet.h>
21 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #endif
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "libyang.h"
33 
34 #include "common.h"
35 #include "compat.h"
36 
47 static void lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value);
48 
61 static LY_ERR
62 ipv6address_str2ip(const char *value, size_t value_len, uint32_t options, const struct ly_ctx *ctx,
63  struct in6_addr *addr, const char **zone, struct ly_err_item **err)
64 {
65  LY_ERR ret = LY_SUCCESS;
66  const char *addr_no_zone;
67  char *zone_ptr = NULL, *addr_dyn = NULL;
68  size_t zone_len;
69 
70  /* store zone and get the string IPv6 address without it */
71  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
72  /* there is a zone index */
73  zone_len = value_len - (zone_ptr - value) - 1;
74  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
75  LY_CHECK_GOTO(ret, cleanup);
76 
77  /* get the IP without it */
78  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
79  *zone_ptr = '\0';
80  addr_no_zone = value;
81  } else {
82  addr_dyn = strndup(value, zone_ptr - value);
83  addr_no_zone = addr_dyn;
84  }
85  } else {
86  /* no zone */
87  *zone = NULL;
88 
89  /* get the IP terminated with zero */
90  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
91  /* we can use the value directly */
92  addr_no_zone = value;
93  } else {
94  addr_dyn = strndup(value, value_len);
95  addr_no_zone = addr_dyn;
96  }
97  }
98 
99  /* store the IPv6 address in network-byte order */
100  if (!inet_pton(AF_INET6, addr_no_zone, addr)) {
101  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_no_zone);
102  goto cleanup;
103  }
104 
105  /* restore the value */
106  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
107  *zone_ptr = '%';
108  }
109 
110 cleanup:
111  free(addr_dyn);
112  return ret;
113 }
114 
118 static LY_ERR
119 lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
120  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
121  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
122  struct ly_err_item **err)
123 {
124  LY_ERR ret = LY_SUCCESS;
125  const char *value_str = value;
126  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
127  struct lyd_value_ipv6_address *val;
128  size_t i;
129 
130  /* init storage */
131  memset(storage, 0, sizeof *storage);
132  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
133  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
134  storage->realtype = type;
135 
136  if (format == LY_VALUE_LYB) {
137  /* validation */
138  if (value_len < 16) {
139  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %zu "
140  "(expected at least 16).", value_len);
141  goto cleanup;
142  }
143  for (i = 16; i < value_len; ++i) {
144  if (!isalnum(value_str[i])) {
145  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
146  value_str[i]);
147  goto cleanup;
148  }
149  }
150 
151  /* store IP address */
152  memcpy(&val->addr, value, sizeof val->addr);
153 
154  /* store zone, if any */
155  if (value_len > 16) {
156  ret = lydict_insert(ctx, value + 16, value_len - 16, &val->zone);
157  LY_CHECK_GOTO(ret, cleanup);
158  } else {
159  val->zone = NULL;
160  }
161 
162  /* success */
163  goto cleanup;
164  }
165 
166  /* check hints */
167  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
168  LY_CHECK_GOTO(ret, cleanup);
169 
170  /* length restriction of the string */
171  if (type_str->length) {
172  /* value_len is in bytes, but we need number of characters here */
173  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
174  LY_CHECK_GOTO(ret, cleanup);
175  }
176 
177  /* pattern restrictions */
178  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
179  LY_CHECK_GOTO(ret, cleanup);
180 
181  /* get the network-byte order address */
182  ret = ipv6address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
183  LY_CHECK_GOTO(ret, cleanup);
184 
185  if (format == LY_VALUE_CANON) {
186  /* store canonical value */
187  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
188  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
189  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
190  LY_CHECK_GOTO(ret, cleanup);
191  } else {
192  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
193  LY_CHECK_GOTO(ret, cleanup);
194  }
195  }
196 
197 cleanup:
198  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
199  free((void *)value);
200  }
201 
202  if (ret) {
203  lyplg_type_free_ipv6_address(ctx, storage);
204  }
205  return ret;
206 }
207 
211 static LY_ERR
212 lyplg_type_compare_ipv6_address(const struct lyd_value *val1, const struct lyd_value *val2)
213 {
214  struct lyd_value_ipv6_address *v1, *v2;
215 
216  if (val1->realtype != val2->realtype) {
217  return LY_ENOT;
218  }
219 
220  LYD_VALUE_GET(val1, v1);
221  LYD_VALUE_GET(val2, v2);
222 
223  /* zones are NULL or in the dictionary */
224  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
225  return LY_ENOT;
226  }
227  return LY_SUCCESS;
228 }
229 
233 static const void *
234 lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
235  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
236 {
237  struct lyd_value_ipv6_address *val;
238  size_t zone_len;
239  char *ret;
240 
241  LYD_VALUE_GET(value, val);
242 
243  if (format == LY_VALUE_LYB) {
244  if (!val->zone) {
245  /* address-only, const */
246  *dynamic = 0;
247  if (value_len) {
248  *value_len = sizeof val->addr;
249  }
250  return &val->addr;
251  }
252 
253  /* dynamic */
254  zone_len = strlen(val->zone);
255  ret = malloc(sizeof val->addr + zone_len);
256  LY_CHECK_RET(!ret, NULL);
257 
258  memcpy(ret, &val->addr, sizeof val->addr);
259  memcpy(ret + sizeof val->addr, val->zone, zone_len);
260 
261  *dynamic = 1;
262  if (value_len) {
263  *value_len = sizeof val->addr + zone_len;
264  }
265  return ret;
266  }
267 
268  /* generate canonical value if not already */
269  if (!value->_canonical) {
270  /* '%' + zone */
271  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
272  ret = malloc(INET6_ADDRSTRLEN + zone_len);
273  LY_CHECK_RET(!ret, NULL);
274 
275  /* get the address in string */
276  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
277  free(ret);
278  LOGERR(ctx, LY_EVALID, "Failed to get IPv6 address in string (%s).", strerror(errno));
279  return NULL;
280  }
281 
282  /* add zone */
283  if (zone_len) {
284  sprintf(ret + strlen(ret), "%%%s", val->zone);
285  }
286 
287  /* store it */
288  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
289  LOGMEM(ctx);
290  return NULL;
291  }
292  }
293 
294  /* use the cached canonical value */
295  if (dynamic) {
296  *dynamic = 0;
297  }
298  if (value_len) {
299  *value_len = strlen(value->_canonical);
300  }
301  return value->_canonical;
302 }
303 
307 static LY_ERR
308 lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
309 {
310  LY_ERR ret;
311  struct lyd_value_ipv6_address *orig_val, *dup_val;
312 
313  memset(dup, 0, sizeof *dup);
314 
315  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
316  LY_CHECK_RET(ret);
317 
318  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
319  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
320 
321  LYD_VALUE_GET(original, orig_val);
322  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
323  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
324  LY_CHECK_GOTO(ret, error);
325 
326  dup->realtype = original->realtype;
327  return LY_SUCCESS;
328 
329 error:
330  lyplg_type_free_ipv6_address(ctx, dup);
331  return ret;
332 }
333 
337 static void
338 lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
339 {
340  struct lyd_value_ipv6_address *val;
341 
342  lydict_remove(ctx, value->_canonical);
343  LYD_VALUE_GET(value, val);
344  if (val) {
345  lydict_remove(ctx, val->zone);
347  }
348 }
349 
357 const struct lyplg_type_record plugins_ipv6_address[] = {
358  {
359  .module = "ietf-inet-types",
360  .revision = "2013-07-15",
361  .name = "ipv6-address",
362 
363  .plugin.id = "libyang 2 - ipv6-address, version 1",
364  .plugin.store = lyplg_type_store_ipv6_address,
365  .plugin.validate = NULL,
366  .plugin.compare = lyplg_type_compare_ipv6_address,
367  .plugin.sort = NULL,
368  .plugin.print = lyplg_type_print_ipv6_address,
369  .plugin.duplicate = lyplg_type_dup_ipv6_address,
370  .plugin.free = lyplg_type_free_ipv6_address
371  },
372  {0}
373 };
libyang context handler.
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 ...
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 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...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_EMEM
Definition: log.h:244
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
Libyang full error structure.
Definition: log.h:287
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.
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's hints (if any) in the specified format.
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
struct lysc_pattern ** patterns
Definition: tree_schema.h:1558
struct lysc_range * length
Definition: tree_schema.h:1557
Compiled YANG data node.
Definition: tree_schema.h:1644
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
@ LY_TYPE_STRING
Definition: tree.h:210
@ LY_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
const struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address type implementation.
Definition: ipv6_address.c:357
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:535
struct in6_addr addr
Definition: tree_data.h:651
const char * zone
Definition: tree_data.h:652
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:573
const char * _canonical
Definition: tree_data.h:532
YANG data representation.
Definition: tree_data.h:531
Special lyd_value structure for ietf-inet-types ipv6-address values.
Definition: tree_data.h:650
#define LOGMEM(CTX)
Definition: tree_edit.h:25