libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
ipv6_address_no_zone.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 
46 static void lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value);
47 
58 static LY_ERR
59 ipv6addressnozone_str2ip(const char *value, size_t value_len, uint32_t options, struct in6_addr *addr, struct ly_err_item **err)
60 {
61  LY_ERR ret = LY_SUCCESS;
62  const char *addr_str;
63  char *addr_dyn = NULL;
64 
65  /* get the IP terminated with zero */
66  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
67  /* we can use the value directly */
68  addr_str = value;
69  } else {
70  addr_dyn = strndup(value, value_len);
71  addr_str = addr_dyn;
72  }
73 
74  /* store the IPv6 address in network-byte order */
75  if (!inet_pton(AF_INET6, addr_str, addr)) {
76  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_str);
77  goto cleanup;
78  }
79 
80 cleanup:
81  free(addr_dyn);
82  return ret;
83 }
84 
88 static LY_ERR
89 lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
90  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
91  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
92  struct ly_err_item **err)
93 {
94  LY_ERR ret = LY_SUCCESS;
95  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
97 
98  /* init storage */
99  memset(storage, 0, sizeof *storage);
100  storage->realtype = type;
101 
102  if (format == LY_VALUE_LYB) {
103  /* validation */
104  if (value_len != 16) {
105  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address-no-zone value size %zu "
106  "(expected 16).", value_len);
107  goto cleanup;
108  }
109 
110  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
111  /* use the value directly */
112  storage->dyn_mem = (void *)value;
113  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
114  } else {
115  /* allocate value */
116  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
117  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
118 
119  /* store IP address */
120  memcpy(&val->addr, value, sizeof val->addr);
121  }
122 
123  /* success */
124  goto cleanup;
125  }
126 
127  /* allocate value */
128  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
129  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
130 
131  /* check hints */
132  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
133  LY_CHECK_GOTO(ret, cleanup);
134 
135  /* length restriction of the string */
136  if (type_str->length) {
137  /* value_len is in bytes, but we need number of characters here */
138  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
139  LY_CHECK_GOTO(ret, cleanup);
140  }
141 
142  /* pattern restrictions */
143  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
144  LY_CHECK_GOTO(ret, cleanup);
145 
146  /* get the network-byte order address */
147  ret = ipv6addressnozone_str2ip(value, value_len, options, &val->addr, err);
148  LY_CHECK_GOTO(ret, cleanup);
149 
150  if (format == LY_VALUE_CANON) {
151  /* store canonical value */
152  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
153  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
154  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
155  LY_CHECK_GOTO(ret, cleanup);
156  } else {
157  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
158  LY_CHECK_GOTO(ret, cleanup);
159  }
160  }
161 
162 cleanup:
163  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
164  free((void *)value);
165  }
166 
167  if (ret) {
168  lyplg_type_free_ipv6_address_no_zone(ctx, storage);
169  }
170  return ret;
171 }
172 
176 static LY_ERR
177 lyplg_type_compare_ipv6_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
178 {
179  struct lyd_value_ipv6_address_no_zone *v1, *v2;
180 
181  if (val1->realtype != val2->realtype) {
182  return LY_ENOT;
183  }
184 
185  LYD_VALUE_GET(val1, v1);
186  LYD_VALUE_GET(val2, v2);
187 
188  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
189  return LY_ENOT;
190  }
191  return LY_SUCCESS;
192 }
193 
197 static const void *
198 lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
199  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
200 {
201  struct lyd_value_ipv6_address_no_zone *val;
202  char *ret;
203 
204  LYD_VALUE_GET(value, val);
205 
206  if (format == LY_VALUE_LYB) {
207  *dynamic = 0;
208  if (value_len) {
209  *value_len = sizeof val->addr;
210  }
211  return &val->addr;
212  }
213 
214  /* generate canonical value if not already */
215  if (!value->_canonical) {
216  /* '%' + zone */
217  ret = malloc(INET6_ADDRSTRLEN);
218  LY_CHECK_RET(!ret, NULL);
219 
220  /* get the address in string */
221  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
222  free(ret);
223  LOGERR(ctx, LY_EVALID, "Failed to get IPv6 address in string (%s).", strerror(errno));
224  return NULL;
225  }
226 
227  /* store it */
228  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
229  LOGMEM(ctx);
230  return NULL;
231  }
232  }
233 
234  /* use the cached canonical value */
235  if (dynamic) {
236  *dynamic = 0;
237  }
238  if (value_len) {
239  *value_len = strlen(value->_canonical);
240  }
241  return value->_canonical;
242 }
243 
247 static LY_ERR
248 lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
249 {
250  LY_ERR ret;
251  struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
252 
253  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
254  LY_CHECK_RET(ret);
255 
256  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
257  if (!dup_val) {
258  lydict_remove(ctx, dup->_canonical);
259  return LY_EMEM;
260  }
261 
262  LYD_VALUE_GET(original, orig_val);
263  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
264 
265  dup->realtype = original->realtype;
266  return LY_SUCCESS;
267 }
268 
272 static void
273 lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
274 {
275  struct lyd_value_ipv6_address_no_zone *val;
276 
277  lydict_remove(ctx, value->_canonical);
278  LYD_VALUE_GET(value, val);
280 }
281 
290  {
291  .module = "ietf-inet-types",
292  .revision = "2013-07-15",
293  .name = "ipv6-address-no-zone",
294 
295  .plugin.id = "libyang 2 - ipv6-address-no-zone, version 1",
296  .plugin.store = lyplg_type_store_ipv6_address_no_zone,
297  .plugin.validate = NULL,
298  .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
299  .plugin.sort = NULL,
300  .plugin.print = lyplg_type_print_ipv6_address_no_zone,
301  .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
302  .plugin.free = lyplg_type_free_ipv6_address_no_zone
303  },
304  {0}
305 };
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.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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_no_zone[]
Plugin information for ipv6-address-no-zone type implementation.
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
#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-no-zone values.
Definition: tree_data.h:643
#define LOGMEM(CTX)
Definition: tree_edit.h:25