libyang  5.7.2
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_prefix.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 <assert.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "libyang.h"
36 
37 #include "compat.h"
38 #include "ly_common.h"
39 
50 #define LYPLG_IPV4PREF_LYB_VALUE_SIZE 40
51 
52 static void lyplg_type_free_ipv4_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
53 
54 static void
55 lyplg_type_lyb_size_ipv4_address_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
56  uint64_t *fixed_size_bits)
57 {
58  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
59  *fixed_size_bits = LYPLG_IPV4PREF_LYB_VALUE_SIZE;
60 }
61 
72 static LY_ERR
73 ipv4prefix_str2ip(const char *value, uint32_t value_len, struct in_addr *addr, uint8_t *prefix, struct ly_err_item **err)
74 {
75  LY_ERR ret = LY_SUCCESS;
76  const char *pref_str;
77  char *mask_str = NULL;
78 
79  /* it passed the pattern validation */
80  pref_str = ly_strnchr(value, '/', value_len);
81  ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
82 
83  /* get just the network prefix */
84  mask_str = strndup(value, pref_str - value);
85  LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
86 
87  /* convert it to netword-byte order */
88  if (inet_pton(AF_INET, mask_str, addr) != 1) {
89  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv4 address \"%s\".", mask_str);
90  goto cleanup;
91  }
92 
93 cleanup:
94  free(mask_str);
95  return ret;
96 }
97 
104 static void
105 ipv4prefix_zero_host(struct in_addr *addr, uint8_t prefix)
106 {
107  uint32_t i, mask;
108 
109  /* zero host bits */
110  mask = 0;
111  for (i = 0; i < 32; ++i) {
112  mask <<= 1;
113  if (prefix > i) {
114  mask |= 1;
115  }
116  }
117  mask = htonl(mask);
118  addr->s_addr &= mask;
119 }
120 
124 static LY_ERR
125 lyplg_type_store_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
126  uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
127  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
128  struct ly_err_item **err)
129 {
130  LY_ERR ret = LY_SUCCESS;
131  const struct lysc_type_str *type_str = (struct lysc_type_str *)type;
132  struct lyd_value_ipv4_prefix *val;
133  uint32_t value_size;
134 
135  /* init storage */
136  memset(storage, 0, sizeof *storage);
137  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
138  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
139  storage->realtype = type;
140 
141  /* check value length */
142  ret = lyplg_type_check_value_size(type->name, format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
143  LYPLG_IPV4PREF_LYB_VALUE_SIZE, &value_size, err);
144  LY_CHECK_GOTO(ret, cleanup);
145 
146  if (format == LY_VALUE_LYB) {
147  /* validation */
148  if (((uint8_t *)value)[4] > 32) {
149  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s prefix length %" PRIu8 ".",
150  type->name, ((uint8_t *)value)[4]);
151  goto cleanup;
152  }
153 
154  /* store addr + prefix */
155  memcpy(val, value, value_size);
156 
157  if (!strcmp(type->name, "ipv4-prefix")) {
158  /* zero host */
159  ipv4prefix_zero_host(&val->addr, val->prefix);
160  }
161 
162  /* success */
163  goto cleanup;
164  }
165 
166  /* check hints */
167  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
168  LY_CHECK_GOTO(ret, cleanup);
169 
170  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
171  /* length restriction of the string */
172  if (type_str->length) {
173  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, value_size, value, value_size, err);
174  LY_CHECK_GOTO(ret, cleanup);
175  }
176 
177  /* validate patterns */
178  ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
179  LY_CHECK_GOTO(ret, cleanup);
180  }
181 
182  /* get the mask in network-byte order */
183  ret = ipv4prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
184  LY_CHECK_GOTO(ret, cleanup);
185 
186  if (!strcmp(type->name, "ipv4-prefix")) {
187  /* zero host */
188  ipv4prefix_zero_host(&val->addr, val->prefix);
189  }
190 
191  if (format == LY_VALUE_CANON) {
192  /* store canonical value */
193  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
194  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
195  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
196  LY_CHECK_GOTO(ret, cleanup);
197  } else {
198  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
199  LY_CHECK_GOTO(ret, cleanup);
200  }
201  }
202 
203 cleanup:
204  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
205  free((void *)value);
206  }
207 
208  if (ret) {
209  lyplg_type_free_ipv4_address_prefix(ctx, storage);
210  }
211  return ret;
212 }
213 
217 static LY_ERR
218 lyplg_type_compare_ipv4_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
219  const struct lyd_value *val2)
220 {
221  struct lyd_value_ipv4_prefix *v1, *v2;
222 
223  LYD_VALUE_GET(val1, v1);
224  LYD_VALUE_GET(val2, v2);
225 
226  if (memcmp(v1, v2, sizeof *v1)) {
227  return LY_ENOT;
228  }
229  return LY_SUCCESS;
230 }
231 
235 static int
236 lyplg_type_sort_ipv4_address_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
237  const struct lyd_value *val2)
238 {
239  struct lyd_value_ipv4_prefix *v1, *v2;
240 
241  LYD_VALUE_GET(val1, v1);
242  LYD_VALUE_GET(val2, v2);
243 
244  return memcmp(v1, v2, sizeof *v1);
245 }
246 
250 static const void *
251 lyplg_type_print_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
252  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
253 {
254  struct lyd_value_ipv4_prefix *val;
255  char *ret;
256 
257  LYD_VALUE_GET(value, val);
258 
259  if (format == LY_VALUE_LYB) {
260  *dynamic = 0;
261  if (value_size_bits) {
262  *value_size_bits = LYPLG_IPV4PREF_LYB_VALUE_SIZE;
263  }
264  return val;
265  }
266 
267  /* generate canonical value if not already */
268  if (!value->_canonical) {
269  /* IPv4 mask + '/' + prefix */
270  ret = malloc(INET_ADDRSTRLEN + 3);
271  LY_CHECK_RET(!ret, NULL);
272 
273  /* convert back to string */
274  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
275  free(ret);
276  return NULL;
277  }
278 
279  /* add the prefix */
280  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
281 
282  /* store it */
283  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
284  LOGMEM(ctx);
285  return NULL;
286  }
287  }
288 
289  /* use the cached canonical value */
290  if (dynamic) {
291  *dynamic = 0;
292  }
293  if (value_size_bits) {
294  *value_size_bits = strlen(value->_canonical) * 8;
295  }
296  return value->_canonical;
297 }
298 
302 static LY_ERR
303 lyplg_type_dup_ipv4_address_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
304 {
305  LY_ERR ret;
306  struct lyd_value_ipv4_prefix *orig_val, *dup_val;
307 
308  memset(dup, 0, sizeof *dup);
309 
310  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
311  LY_CHECK_GOTO(ret, error);
312 
313  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
314  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
315 
316  LYD_VALUE_GET(original, orig_val);
317  memcpy(dup_val, orig_val, sizeof *orig_val);
318 
319  dup->realtype = original->realtype;
320  return LY_SUCCESS;
321 
322 error:
323  lyplg_type_free_ipv4_address_prefix(ctx, dup);
324  return ret;
325 }
326 
330 static void
331 lyplg_type_free_ipv4_address_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
332 {
333  struct lyd_value_ipv4_prefix *val;
334 
335  lydict_remove(ctx, value->_canonical);
336  value->_canonical = NULL;
337  LYD_VALUE_GET(value, val);
339 }
340 
349  {
350  .module = "ietf-inet-types",
351  .revision = NULL,
352  .name = "ipv4-prefix",
353 
354  .plugin.id = "ly2 ipv4-address-prefix",
355  .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_prefix,
356  .plugin.store = lyplg_type_store_ipv4_address_prefix,
357  .plugin.validate_value = NULL,
358  .plugin.validate_tree = NULL,
359  .plugin.compare = lyplg_type_compare_ipv4_address_prefix,
360  .plugin.sort = lyplg_type_sort_ipv4_address_prefix,
361  .plugin.print = lyplg_type_print_ipv4_address_prefix,
362  .plugin.duplicate = lyplg_type_dup_ipv4_address_prefix,
363  .plugin.free = lyplg_type_free_ipv4_address_prefix,
364  },
365  {
366  .module = "ietf-inet-types",
367  .revision = NULL,
368  .name = "ipv4-address-and-prefix",
369 
370  .plugin.id = "ly2 ipv4-address-prefix",
371  .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_prefix,
372  .plugin.store = lyplg_type_store_ipv4_address_prefix,
373  .plugin.validate_value = NULL,
374  .plugin.validate_tree = NULL,
375  .plugin.compare = lyplg_type_compare_ipv4_address_prefix,
376  .plugin.sort = lyplg_type_sort_ipv4_address_prefix,
377  .plugin.print = lyplg_type_print_ipv4_address_prefix,
378  .plugin.duplicate = lyplg_type_dup_ipv4_address_prefix,
379  .plugin.free = lyplg_type_free_ipv4_address_prefix,
380  },
381  {0}
382 };
struct in_addr addr
Definition: tree_data.h:629
struct lysc_type * realtype
Definition: tree_data.h:527
Compiled YANG data node.
Definition: tree_schema.h:1430
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
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
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
Definition: log.h:269
#define LYPLG_TYPE_STORE_DYNAMIC
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **patterns, const char *str, uint32_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
#define LOGMEM(CTX)
Definition: tree_edit.h:22
struct lyplg_type_record plugins_ipv4_address_prefix[]
Plugin information for ipv4-prefix and ipv4-address-and-prefix type implementation.
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:257
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1330
struct lysc_range * length
Definition: tree_schema.h:1329
YANG data representation.
Definition: tree_data.h:523
const char * _canonical
Definition: tree_data.h:524
Libyang full error structure.
Definition: log.h:300
Definition: log.h:292
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:566
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.
Special lyd_value structure for ietf-inet-types ipv4-prefix values.
Definition: tree_data.h:628
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...
const char * name
Definition: tree_schema.h:1293
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: tree.h:234
#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.
#define LYPLG_IPV4PREF_LYB_VALUE_SIZE
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, uint32_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
#define LYPLG_TYPE_STORE_ONLY