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
ipv4_address.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 <ctype.h>
25 #include <errno.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "libyang.h"
31 
32 #include "common.h"
33 #include "compat.h"
34 
45 static void lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value);
46 
59 static LY_ERR
60 ipv4address_str2ip(const char *value, size_t value_len, uint32_t options, const struct ly_ctx *ctx,
61  struct in_addr *addr, const char **zone, struct ly_err_item **err)
62 {
63  LY_ERR ret = LY_SUCCESS;
64  const char *addr_no_zone;
65  char *zone_ptr = NULL, *addr_dyn = NULL;
66  size_t zone_len;
67 
68  /* store zone and get the string IPv4 address without it */
69  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
70  /* there is a zone index */
71  zone_len = value_len - (zone_ptr - value) - 1;
72  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
73  LY_CHECK_GOTO(ret, cleanup);
74 
75  /* get the IP without it */
76  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
77  *zone_ptr = '\0';
78  addr_no_zone = value;
79  } else {
80  addr_dyn = strndup(value, zone_ptr - value);
81  addr_no_zone = addr_dyn;
82  }
83  } else {
84  /* no zone */
85  *zone = NULL;
86 
87  /* get the IP terminated with zero */
88  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
89  /* we can use the value directly */
90  addr_no_zone = value;
91  } else {
92  addr_dyn = strndup(value, value_len);
93  addr_no_zone = addr_dyn;
94  }
95  }
96 
97  /* store the IPv4 address in network-byte order */
98  if (!inet_pton(AF_INET, addr_no_zone, addr)) {
99  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", addr_no_zone);
100  goto cleanup;
101  }
102 
103  /* restore the value */
104  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
105  *zone_ptr = '%';
106  }
107 
108 cleanup:
109  free(addr_dyn);
110  return ret;
111 }
112 
116 static LY_ERR
117 lyplg_type_store_ipv4_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
118  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
119  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
120  struct ly_err_item **err)
121 {
122  LY_ERR ret = LY_SUCCESS;
123  const char *value_str = value;
124  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
125  struct lyd_value_ipv4_address *val;
126  size_t i;
127 
128  /* init storage */
129  memset(storage, 0, sizeof *storage);
130  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
131  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
132  storage->realtype = type;
133 
134  if (format == LY_VALUE_LYB) {
135  /* validation */
136  if (value_len < 4) {
137  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %zu "
138  "(expected at least 4).", value_len);
139  goto cleanup;
140  }
141  for (i = 4; i < value_len; ++i) {
142  if (!isalnum(value_str[i])) {
143  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address zone character 0x%x.",
144  value_str[i]);
145  goto cleanup;
146  }
147  }
148 
149  /* store IP address */
150  memcpy(&val->addr, value, sizeof val->addr);
151 
152  /* store zone, if any */
153  if (value_len > 4) {
154  ret = lydict_insert(ctx, value_str + 4, value_len - 4, &val->zone);
155  LY_CHECK_GOTO(ret, cleanup);
156  } else {
157  val->zone = NULL;
158  }
159 
160  /* success */
161  goto cleanup;
162  }
163 
164  /* check hints */
165  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
166  LY_CHECK_GOTO(ret, cleanup);
167 
168  /* length restriction of the string */
169  if (type_str->length) {
170  /* value_len is in bytes, but we need number of characters here */
171  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
172  LY_CHECK_GOTO(ret, cleanup);
173  }
174 
175  /* pattern restrictions */
176  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
177  LY_CHECK_GOTO(ret, cleanup);
178 
179  /* get the network-byte order address */
180  ret = ipv4address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
181  LY_CHECK_GOTO(ret, cleanup);
182 
183  /* store canonical value */
184  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
185  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
186  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
187  LY_CHECK_GOTO(ret, cleanup);
188  } else {
189  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
190  LY_CHECK_GOTO(ret, cleanup);
191  }
192 
193 cleanup:
194  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
195  free((void *)value);
196  }
197 
198  if (ret) {
199  lyplg_type_free_ipv4_address(ctx, storage);
200  }
201  return ret;
202 }
203 
207 static LY_ERR
208 lyplg_type_compare_ipv4_address(const struct lyd_value *val1, const struct lyd_value *val2)
209 {
210  struct lyd_value_ipv4_address *v1, *v2;
211 
212  if (val1->realtype != val2->realtype) {
213  return LY_ENOT;
214  }
215 
216  LYD_VALUE_GET(val1, v1);
217  LYD_VALUE_GET(val2, v2);
218 
219  /* zones are NULL or in the dictionary */
220  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
221  return LY_ENOT;
222  }
223  return LY_SUCCESS;
224 }
225 
229 static const void *
230 lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
231  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
232 {
233  struct lyd_value_ipv4_address *val;
234  size_t zone_len;
235  char *ret;
236 
237  LYD_VALUE_GET(value, val);
238 
239  if (format == LY_VALUE_LYB) {
240  if (!val->zone) {
241  /* address-only, const */
242  *dynamic = 0;
243  if (value_len) {
244  *value_len = sizeof val->addr;
245  }
246  return &val->addr;
247  }
248 
249  /* dynamic */
250  zone_len = strlen(val->zone);
251  ret = malloc(sizeof val->addr + zone_len);
252  LY_CHECK_RET(!ret, NULL);
253 
254  memcpy(ret, &val->addr, sizeof val->addr);
255  memcpy(ret + sizeof val->addr, val->zone, zone_len);
256 
257  *dynamic = 1;
258  if (value_len) {
259  *value_len = sizeof val->addr + zone_len;
260  }
261  return ret;
262  }
263 
264  /* generate canonical value if not already */
265  if (!value->_canonical) {
266  /* '%' + zone */
267  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
268  ret = malloc(INET_ADDRSTRLEN + zone_len);
269  LY_CHECK_RET(!ret, NULL);
270 
271  /* get the address in string */
272  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
273  free(ret);
274  LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
275  return NULL;
276  }
277 
278  /* add zone */
279  if (zone_len) {
280  sprintf(ret + strlen(ret), "%%%s", val->zone);
281  }
282 
283  /* store it */
284  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
285  LOGMEM(ctx);
286  return NULL;
287  }
288  }
289 
290  /* use the cached canonical value */
291  if (dynamic) {
292  *dynamic = 0;
293  }
294  if (value_len) {
295  *value_len = strlen(value->_canonical);
296  }
297  return value->_canonical;
298 }
299 
303 static LY_ERR
304 lyplg_type_dup_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
305 {
306  LY_ERR ret;
307  struct lyd_value_ipv4_address *orig_val, *dup_val;
308 
309  memset(dup, 0, sizeof *dup);
310 
311  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
312  LY_CHECK_GOTO(ret, error);
313 
314  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
315  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
316 
317  LYD_VALUE_GET(original, orig_val);
318 
319  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
320  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
321  LY_CHECK_GOTO(ret, error);
322 
323  dup->realtype = original->realtype;
324  return LY_SUCCESS;
325 
326 error:
327  lyplg_type_free_ipv4_address(ctx, dup);
328  return ret;
329 }
330 
334 static void
335 lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value)
336 {
337  struct lyd_value_ipv4_address *val;
338 
339  lydict_remove(ctx, value->_canonical);
340  value->_canonical = NULL;
341  LYD_VALUE_GET(value, val);
342  if (val) {
343  lydict_remove(ctx, val->zone);
345  }
346 }
347 
356  {
357  .module = "ietf-inet-types",
358  .revision = "2013-07-15",
359  .name = "ipv4-address",
360 
361  .plugin.id = "libyang 2 - ipv4-address, version 1",
362  .plugin.store = lyplg_type_store_ipv4_address,
363  .plugin.validate = NULL,
364  .plugin.compare = lyplg_type_compare_ipv4_address,
365  .plugin.sort = NULL,
366  .plugin.print = lyplg_type_print_ipv4_address,
367  .plugin.duplicate = lyplg_type_dup_ipv4_address,
368  .plugin.free = lyplg_type_free_ipv4_address,
369  .plugin.lyb_data_len = -1,
370  },
371  {0}
372 };
struct lysc_type * realtype
Definition: tree_data.h:539
Compiled YANG data node.
Definition: tree_schema.h:1666
struct in_addr addr
Definition: tree_data.h:633
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.
const char * zone
Definition: tree_data.h:634
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 ...
struct lyplg_type_record plugins_ipv4_address[]
Plugin information for ipv4-address type implementation.
Definition: ipv4_address.c:355
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address values.
Definition: tree_data.h:632
#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
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
#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.