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