libyang  2.0.112
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_no_zone.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* strndup */
16 
17 #include "plugins_types.h"
18 
19 #include <arpa/inet.h>
20 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #endif
24 #include <errno.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "libyang.h"
30 
31 #include "common.h"
32 #include "compat.h"
33 
43 static void lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value);
44 
55 static LY_ERR
56 ipv6addressnozone_str2ip(const char *value, size_t value_len, uint32_t options, struct in6_addr *addr, struct ly_err_item **err)
57 {
58  LY_ERR ret = LY_SUCCESS;
59  const char *addr_str;
60  char *addr_dyn = NULL;
61 
62  /* get the IP terminated with zero */
63  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
64  /* we can use the value directly */
65  addr_str = value;
66  } else {
67  addr_dyn = strndup(value, value_len);
68  addr_str = addr_dyn;
69  }
70 
71  /* store the IPv6 address in network-byte order */
72  if (!inet_pton(AF_INET6, addr_str, addr)) {
73  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_str);
74  goto cleanup;
75  }
76 
77 cleanup:
78  free(addr_dyn);
79  return ret;
80 }
81 
85 static LY_ERR
86 lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
87  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
88  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
89  struct ly_err_item **err)
90 {
91  LY_ERR ret = LY_SUCCESS;
92  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
94 
95  /* init storage */
96  memset(storage, 0, sizeof *storage);
97  storage->realtype = type;
98 
99  if (format == LY_VALUE_LYB) {
100  /* validation */
101  if (value_len != 16) {
102  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address-no-zone value size %zu "
103  "(expected 16).", value_len);
104  goto cleanup;
105  }
106 
107  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
108  /* use the value directly */
109  storage->dyn_mem = (void *)value;
110  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
111  } else {
112  /* allocate value */
113  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
114  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
115 
116  /* store IP address */
117  memcpy(&val->addr, value, sizeof val->addr);
118  }
119 
120  /* success */
121  goto cleanup;
122  }
123 
124  /* allocate value */
125  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
126  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
127 
128  /* check hints */
129  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
130  LY_CHECK_GOTO(ret, cleanup);
131 
132  /* length restriction of the string */
133  if (type_str->length) {
134  /* value_len is in bytes, but we need number of characters here */
135  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
136  LY_CHECK_GOTO(ret, cleanup);
137  }
138 
139  /* pattern restrictions */
140  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
141  LY_CHECK_GOTO(ret, cleanup);
142 
143  /* get the network-byte order address */
144  ret = ipv6addressnozone_str2ip(value, value_len, options, &val->addr, err);
145  LY_CHECK_GOTO(ret, cleanup);
146 
147  if (format == LY_VALUE_CANON) {
148  /* store canonical value */
149  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
150  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
151  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
152  LY_CHECK_GOTO(ret, cleanup);
153  } else {
154  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
155  LY_CHECK_GOTO(ret, cleanup);
156  }
157  }
158 
159 cleanup:
160  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
161  free((void *)value);
162  }
163 
164  if (ret) {
165  lyplg_type_free_ipv6_address_no_zone(ctx, storage);
166  }
167  return ret;
168 }
169 
173 static LY_ERR
174 lyplg_type_compare_ipv6_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
175 {
176  struct lyd_value_ipv6_address_no_zone *v1, *v2;
177 
178  if (val1->realtype != val2->realtype) {
179  return LY_ENOT;
180  }
181 
182  LYD_VALUE_GET(val1, v1);
183  LYD_VALUE_GET(val2, v2);
184 
185  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
186  return LY_ENOT;
187  }
188  return LY_SUCCESS;
189 }
190 
194 static const void *
195 lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
196  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
197 {
198  struct lyd_value_ipv6_address_no_zone *val;
199  char *ret;
200 
201  LYD_VALUE_GET(value, val);
202 
203  if (format == LY_VALUE_LYB) {
204  *dynamic = 0;
205  if (value_len) {
206  *value_len = sizeof val->addr;
207  }
208  return &val->addr;
209  }
210 
211  /* generate canonical value if not already */
212  if (!value->_canonical) {
213  /* '%' + zone */
214  ret = malloc(INET6_ADDRSTRLEN);
215  LY_CHECK_RET(!ret, NULL);
216 
217  /* get the address in string */
218  if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
219  free(ret);
220  LOGERR(ctx, LY_EVALID, "Failed to get IPv6 address in string (%s).", strerror(errno));
221  return NULL;
222  }
223 
224  /* store it */
225  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
226  LOGMEM(ctx);
227  return NULL;
228  }
229  }
230 
231  /* use the cached canonical value */
232  if (dynamic) {
233  *dynamic = 0;
234  }
235  if (value_len) {
236  *value_len = strlen(value->_canonical);
237  }
238  return value->_canonical;
239 }
240 
244 static LY_ERR
245 lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
246 {
247  LY_ERR ret;
248  struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
249 
250  memset(dup, 0, sizeof *dup);
251 
252  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
253  LY_CHECK_GOTO(ret, error);
254 
255  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
256  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
257 
258  LYD_VALUE_GET(original, orig_val);
259  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
260 
261  dup->realtype = original->realtype;
262  return LY_SUCCESS;
263 
264 error:
265  lyplg_type_free_ipv6_address_no_zone(ctx, dup);
266  return ret;
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  value->_canonical = NULL;
279  LYD_VALUE_GET(value, val);
281 }
282 
291  {
292  .module = "ietf-inet-types",
293  .revision = "2013-07-15",
294  .name = "ipv6-address-no-zone",
295 
296  .plugin.id = "libyang 2 - ipv6-address-no-zone, version 1",
297  .plugin.store = lyplg_type_store_ipv6_address_no_zone,
298  .plugin.validate = NULL,
299  .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
300  .plugin.sort = NULL,
301  .plugin.print = lyplg_type_print_ipv6_address_no_zone,
302  .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
303  .plugin.free = lyplg_type_free_ipv6_address_no_zone,
304  .plugin.lyb_data_len = 16,
305  },
306  {0}
307 };
struct lysc_type * realtype
Definition: tree_data.h:539
Compiled YANG data node.
Definition: tree_schema.h:1666
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:248
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
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 ...
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv6-address-no-zone values.
Definition: tree_data.h:648
#define LOGMEM(CTX)
Definition: tree_edit.h:22
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...
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1580
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.
struct lysc_range * length
Definition: tree_schema.h:1579
YANG data representation.
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:536
Libyang full error structure.
Definition: log.h:291
Definition: log.h:283
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:578
Definition: log.h:254
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.
const char * module
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
Definition: log.h:260
#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:235
struct lyplg_type_record plugins_ipv6_address_no_zone[]
Plugin information for ipv6-address-no-zone type implementation.
#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:1552
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:245
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.
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.
API for (user) types plugins.
libyang context handler.