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