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