libyang  4.0.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_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_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
53 
54 static void
55 lyplg_type_lyb_size_ipv4_prefix(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
56  uint32_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_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
126  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
127  const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
128  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
129 {
130  LY_ERR ret = LY_SUCCESS;
131  struct lyd_value_ipv4_prefix *val;
132  uint32_t value_size;
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  /* check value length */
141  ret = lyplg_type_check_value_size("ipv4-prefix", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS,
142  LYPLG_IPV4PREF_LYB_VALUE_SIZE, &value_size, err);
143  LY_CHECK_GOTO(ret, cleanup);
144 
145  if (format == LY_VALUE_LYB) {
146  /* validation */
147  if (((uint8_t *)value)[4] > 32) {
148  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix prefix length %" PRIu8 ".",
149  ((uint8_t *)value)[4]);
150  goto cleanup;
151  }
152 
153  /* store addr + prefix */
154  memcpy(val, value, value_size);
155 
156  /* zero host */
157  ipv4prefix_zero_host(&val->addr, val->prefix);
158 
159  /* success */
160  goto cleanup;
161  }
162 
163  /* check hints */
164  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
165  LY_CHECK_GOTO(ret, cleanup);
166 
167  /* get the mask in network-byte order */
168  ret = ipv4prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
169  LY_CHECK_GOTO(ret, cleanup);
170 
171  /* zero host */
172  ipv4prefix_zero_host(&val->addr, val->prefix);
173 
174  if (format == LY_VALUE_CANON) {
175  /* store canonical value */
176  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
177  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
178  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
179  LY_CHECK_GOTO(ret, cleanup);
180  } else {
181  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
182  LY_CHECK_GOTO(ret, cleanup);
183  }
184  }
185 
186 cleanup:
187  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
188  free((void *)value);
189  }
190 
191  if (ret) {
192  lyplg_type_free_ipv4_prefix(ctx, storage);
193  }
194  return ret;
195 }
196 
200 static LY_ERR
201 lyplg_type_compare_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
202  const struct lyd_value *val2)
203 {
204  struct lyd_value_ipv4_prefix *v1, *v2;
205 
206  LYD_VALUE_GET(val1, v1);
207  LYD_VALUE_GET(val2, v2);
208 
209  if (memcmp(v1, v2, sizeof *v1)) {
210  return LY_ENOT;
211  }
212  return LY_SUCCESS;
213 }
214 
218 static int
219 lyplg_type_sort_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
220  const struct lyd_value *val2)
221 {
222  struct lyd_value_ipv4_prefix *v1, *v2;
223 
224  LYD_VALUE_GET(val1, v1);
225  LYD_VALUE_GET(val2, v2);
226 
227  return memcmp(v1, v2, sizeof *v1);
228 }
229 
233 static const void *
234 lyplg_type_print_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
235  void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
236 {
237  struct lyd_value_ipv4_prefix *val;
238  char *ret;
239 
240  LYD_VALUE_GET(value, val);
241 
242  if (format == LY_VALUE_LYB) {
243  *dynamic = 0;
244  if (value_size_bits) {
245  *value_size_bits = LYPLG_IPV4PREF_LYB_VALUE_SIZE;
246  }
247  return val;
248  }
249 
250  /* generate canonical value if not already */
251  if (!value->_canonical) {
252  /* IPv4 mask + '/' + prefix */
253  ret = malloc(INET_ADDRSTRLEN + 3);
254  LY_CHECK_RET(!ret, NULL);
255 
256  /* convert back to string */
257  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
258  free(ret);
259  return NULL;
260  }
261 
262  /* add the prefix */
263  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
264 
265  /* store it */
266  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
267  LOGMEM(ctx);
268  return NULL;
269  }
270  }
271 
272  /* use the cached canonical value */
273  if (dynamic) {
274  *dynamic = 0;
275  }
276  if (value_size_bits) {
277  *value_size_bits = strlen(value->_canonical) * 8;
278  }
279  return value->_canonical;
280 }
281 
285 static LY_ERR
286 lyplg_type_dup_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
287 {
288  LY_ERR ret;
289  struct lyd_value_ipv4_prefix *orig_val, *dup_val;
290 
291  memset(dup, 0, sizeof *dup);
292 
293  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
294  LY_CHECK_GOTO(ret, error);
295 
296  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
297  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
298 
299  LYD_VALUE_GET(original, orig_val);
300  memcpy(dup_val, orig_val, sizeof *orig_val);
301 
302  dup->realtype = original->realtype;
303  return LY_SUCCESS;
304 
305 error:
306  lyplg_type_free_ipv4_prefix(ctx, dup);
307  return ret;
308 }
309 
313 static void
314 lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
315 {
316  struct lyd_value_ipv4_prefix *val;
317 
318  lydict_remove(ctx, value->_canonical);
319  value->_canonical = NULL;
320  LYD_VALUE_GET(value, val);
322 }
323 
332  {
333  .module = "ietf-inet-types",
334  .revision = "2013-07-15",
335  .name = "ipv4-prefix",
336 
337  .plugin.id = "ly2 ipv4-prefix",
338  .plugin.lyb_size = lyplg_type_lyb_size_ipv4_prefix,
339  .plugin.store = lyplg_type_store_ipv4_prefix,
340  .plugin.validate_value = NULL,
341  .plugin.validate_tree = NULL,
342  .plugin.compare = lyplg_type_compare_ipv4_prefix,
343  .plugin.sort = lyplg_type_sort_ipv4_prefix,
344  .plugin.print = lyplg_type_print_ipv4_prefix,
345  .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
346  .plugin.free = lyplg_type_free_ipv4_prefix,
347  },
348  {0}
349 };
struct in_addr addr
Definition: tree_data.h:670
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
struct lyplg_type_record plugins_ipv4_prefix[]
Plugin information for ipv4-prefix type implementation.
Definition: ipv4_prefix.c:331
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
YANG extension compiled instance.
Definition: plugins_exts.h:436
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:254
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
Definition: log.h:242
The main libyang public header.
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
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:606
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:669
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:248
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:1300
return memcmp(v1->data, v2->data, bitmap_size)
#define LYPLG_IPV4PREF_LYB_VALUE_SIZE
Definition: ipv4_prefix.c:50
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.