libyang  6.3.1
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 "compat.h"
37 #include "dict.h"
38 #include "ly_common.h"
49 static void lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value);
50 
63 static LY_ERR
64 ipv4address_str2ip(const char *value, uint32_t value_len, uint32_t options, const struct ly_ctx *ctx,
65  struct in_addr *addr, const char **zone, struct ly_err_item **err)
66 {
67  LY_ERR ret = LY_SUCCESS;
68  const char *addr_no_zone;
69  char *zone_ptr = NULL, *addr_dyn = NULL;
70  uint32_t zone_len;
71 
72  /* store zone and get the string IPv4 address without it */
73  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
74  /* there is a zone index */
75  zone_len = value_len - (zone_ptr - value) - 1;
76  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
77  LY_CHECK_GOTO(ret, cleanup);
78 
79  /* get the IP without it */
80  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
81  *zone_ptr = '\0';
82  addr_no_zone = value;
83  } else {
84  addr_dyn = strndup(value, zone_ptr - value);
85  addr_no_zone = addr_dyn;
86  }
87  } else {
88  /* no zone */
89  *zone = NULL;
90 
91  /* get the IP terminated with zero */
92  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
93  /* we can use the value directly */
94  addr_no_zone = value;
95  } else {
96  addr_dyn = strndup(value, value_len);
97  addr_no_zone = addr_dyn;
98  }
99  }
100 
101  /* store the IPv4 address in network-byte order */
102  if (!inet_pton(AF_INET, addr_no_zone, addr)) {
103  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv4 address \"%s\".", addr_no_zone);
104  goto cleanup;
105  }
106 
107  /* restore the value */
108  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
109  *zone_ptr = '%';
110  }
111 
112 cleanup:
113  free(addr_dyn);
114  return ret;
115 }
116 
120 static LY_ERR
121 lyplg_type_store_ipv4_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
122  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
123  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
124  struct ly_err_item **err)
125 {
126  LY_ERR ret = LY_SUCCESS;
127  struct lyd_value_ipv4_address *val;
128  const char *value_str = value;
129  uint32_t value_size, i;
130 
131  /* init storage */
132  memset(storage, 0, sizeof *storage);
133  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
134  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
135  storage->realtype = type;
136 
137  value_size = LYPLG_BITS2BYTES(value_size_bits);
138 
139  if (format == LY_VALUE_LYB) {
140  /* validation */
141  if (value_size_bits < 32) {
142  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %" PRIu64
143  " b (expected at least 32 b).", value_size_bits);
144  goto cleanup;
145  }
146  for (i = 4; i < value_size; ++i) {
147  if (!isalnum(value_str[i])) {
148  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address zone character 0x%x.",
149  value_str[i]);
150  goto cleanup;
151  }
152  }
153 
154  /* store IP address */
155  memcpy(&val->addr, value, sizeof val->addr);
156 
157  /* store zone, if any */
158  if (value_size > 4) {
159  ret = lydict_insert(ctx, value_str + 4, value_size - 4, &val->zone);
160  LY_CHECK_GOTO(ret, cleanup);
161  } else {
162  val->zone = NULL;
163  }
164 
165  /* success */
166  goto cleanup;
167  }
168 
169  /* check hints */
170  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
171  LY_CHECK_GOTO(ret, cleanup);
172 
173  /* get the network-byte order address */
174  ret = ipv4address_str2ip(value, value_size, options, ctx, &val->addr, &val->zone, err);
175  LY_CHECK_GOTO(ret, cleanup);
176 
177  /* store canonical value */
178  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
179  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
180  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
181  LY_CHECK_GOTO(ret, cleanup);
182  } else {
183  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
184  LY_CHECK_GOTO(ret, cleanup);
185  }
186 
187 cleanup:
188  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
189  free((void *)value);
190  }
191 
192  if (ret) {
193  lyplg_type_free_ipv4_address(ctx, storage);
194  }
195  return ret;
196 }
197 
201 static LY_ERR
202 lyplg_type_compare_ipv4_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
203  const struct lyd_value *val2)
204 {
205  struct lyd_value_ipv4_address *v1, *v2;
206 
207  LYD_VALUE_GET(val1, v1);
208  LYD_VALUE_GET(val2, v2);
209 
210  /* zones are NULL or in the dictionary */
211  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
212  return LY_ENOT;
213  }
214  return LY_SUCCESS;
215 }
216 
220 static int
221 lyplg_type_sort_ipv4_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
222  const struct lyd_value *val2)
223 {
224  struct lyd_value_ipv4_address *v1, *v2;
225  int cmp;
226 
227  LYD_VALUE_GET(val1, v1);
228  LYD_VALUE_GET(val2, v2);
229 
230  cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
231  if (cmp != 0) {
232  return cmp;
233  }
234 
235  if (!v1->zone && v2->zone) {
236  return -1;
237  } else if (v1->zone && !v2->zone) {
238  return 1;
239  } else if (v1->zone && v2->zone) {
240  return strcmp(v1->zone, v2->zone);
241  }
242 
243  return 0;
244 }
245 
249 static const void *
250 lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
251  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
252 {
253  struct lyd_value_ipv4_address *val;
254  uint32_t zone_len;
255  char *ret;
256 
257  LYD_VALUE_GET(value, val);
258 
259  if (format == LY_VALUE_LYB) {
260  if (!val->zone) {
261  /* address-only, const */
262  *dynamic = 0;
263  if (value_size_bits) {
264  *value_size_bits = sizeof val->addr * 8;
265  }
266  return &val->addr;
267  }
268 
269  /* dynamic */
270  zone_len = strlen(val->zone);
271  ret = malloc(sizeof val->addr + zone_len);
272  LY_CHECK_RET(!ret, NULL);
273 
274  memcpy(ret, &val->addr, sizeof val->addr);
275  memcpy(ret + sizeof val->addr, val->zone, zone_len);
276 
277  *dynamic = 1;
278  if (value_size_bits) {
279  *value_size_bits = sizeof val->addr * 8 + zone_len * 8;
280  }
281  return ret;
282  }
283 
284  /* generate canonical value if not already */
285  if (!value->_canonical) {
286  /* '%' + zone */
287  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
288  ret = malloc(INET_ADDRSTRLEN + zone_len);
289  LY_CHECK_RET(!ret, NULL);
290 
291  /* get the address in string */
292  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
293  free(ret);
294  LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
295  return NULL;
296  }
297 
298  /* add zone */
299  if (zone_len) {
300  sprintf(ret + strlen(ret), "%%%s", val->zone);
301  }
302 
303  /* store it */
304  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
305  LOGMEM(ctx);
306  return NULL;
307  }
308  }
309 
310  /* use the cached canonical value */
311  if (dynamic) {
312  *dynamic = 0;
313  }
314  if (value_size_bits) {
315  *value_size_bits = strlen(value->_canonical) * 8;
316  }
317  return value->_canonical;
318 }
319 
323 static LY_ERR
324 lyplg_type_dup_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
325 {
326  LY_ERR ret;
327  struct lyd_value_ipv4_address *orig_val, *dup_val;
328 
329  memset(dup, 0, sizeof *dup);
330 
331  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
332  LY_CHECK_GOTO(ret, error);
333 
334  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
335  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
336 
337  LYD_VALUE_GET(original, orig_val);
338 
339  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
340  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
341  LY_CHECK_GOTO(ret, error);
342 
343  dup->realtype = original->realtype;
344  return LY_SUCCESS;
345 
346 error:
347  lyplg_type_free_ipv4_address(ctx, dup);
348  return ret;
349 }
350 
354 static void
355 lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value)
356 {
357  struct lyd_value_ipv4_address *val;
358 
359  lydict_remove(ctx, value->_canonical);
360  value->_canonical = NULL;
361  LYD_VALUE_GET(value, val);
362  if (val) {
363  lydict_remove(ctx, val->zone);
365  }
366 }
367 
376  {
377  .module = "ietf-inet-types",
378  .revision = NULL,
379  .name = "ipv4-address",
380 
381  .plugin.id = "ly2 ipv4-address",
382  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
383  .plugin.store = lyplg_type_store_ipv4_address,
384  .plugin.validate_value = NULL,
385  .plugin.validate_tree = NULL,
386  .plugin.compare = lyplg_type_compare_ipv4_address,
387  .plugin.sort = lyplg_type_sort_ipv4_address,
388  .plugin.print = lyplg_type_print_ipv4_address,
389  .plugin.duplicate = lyplg_type_dup_ipv4_address,
390  .plugin.free = lyplg_type_free_ipv4_address,
391  },
392  {
393  .module = "ietf-inet-types",
394  .revision = NULL,
395  .name = "ipv4-address-link-local",
396 
397  .plugin.id = "ly2 ipv4-address",
398  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
399  .plugin.store = lyplg_type_store_ipv4_address,
400  .plugin.validate_value = NULL,
401  .plugin.validate_tree = NULL,
402  .plugin.compare = lyplg_type_compare_ipv4_address,
403  .plugin.sort = lyplg_type_sort_ipv4_address,
404  .plugin.print = lyplg_type_print_ipv4_address,
405  .plugin.duplicate = lyplg_type_dup_ipv4_address,
406  .plugin.free = lyplg_type_free_ipv4_address,
407  },
408  {0}
409 };
struct lysc_type * realtype
Definition: tree_data.h:615
Compiled YANG data node.
Definition: tree_schema.h:1430
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
struct in_addr addr
Definition: tree_data.h:709
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
const char * zone
Definition: tree_data.h:710
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:255
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
struct lyplg_type_record plugins_ipv4_address[]
Plugin information for ipv4-address and ipv4-address-link-local type implementation.
Definition: ipv4_address.c:375
libyang dictionary
Definition: log.h:269
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address values.
Definition: tree_data.h:708
Definition: log.h:258
int cmp
Definition: binary.c:392
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:257
YANG data representation.
Definition: tree_data.h:611
const char * _canonical
Definition: tree_data.h:612
Libyang full error structure.
Definition: log.h:301
Definition: log.h:293
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:654
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...
Definition: log.h:263
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: utils.h:93
#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:1296
return memcmp(v1->data, v2->data, bitmap_size)
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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.
API for (user) types plugins.
libyang context handler.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.