libyang  3.6.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_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 LYB_VALUE_LEN 5
51 
52 static void lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
53 
64 static LY_ERR
65 ipv4prefix_str2ip(const char *value, size_t value_len, struct in_addr *addr, uint8_t *prefix, struct ly_err_item **err)
66 {
67  LY_ERR ret = LY_SUCCESS;
68  const char *pref_str;
69  char *mask_str = NULL;
70 
71  /* it passed the pattern validation */
72  pref_str = ly_strnchr(value, '/', value_len);
73  ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
74 
75  /* get just the network prefix */
76  mask_str = strndup(value, pref_str - value);
77  LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
78 
79  /* convert it to netword-byte order */
80  if (inet_pton(AF_INET, mask_str, addr) != 1) {
81  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", mask_str);
82  goto cleanup;
83  }
84 
85 cleanup:
86  free(mask_str);
87  return ret;
88 }
89 
96 static void
97 ipv4prefix_zero_host(struct in_addr *addr, uint8_t prefix)
98 {
99  uint32_t i, mask;
100 
101  /* zero host bits */
102  mask = 0;
103  for (i = 0; i < 32; ++i) {
104  mask <<= 1;
105  if (prefix > i) {
106  mask |= 1;
107  }
108  }
109  mask = htonl(mask);
110  addr->s_addr &= mask;
111 }
112 
116 static LY_ERR
117 lyplg_type_store_ipv4_prefix(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  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
124  struct lyd_value_ipv4_prefix *val;
125 
126  /* init storage */
127  memset(storage, 0, sizeof *storage);
128  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
129  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
130  storage->realtype = type;
131 
132  if (format == LY_VALUE_LYB) {
133  /* validation */
134  if (value_len != LYB_VALUE_LEN) {
135  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix value size %zu (expected %d).",
136  value_len, LYB_VALUE_LEN);
137  goto cleanup;
138  }
139  if (((uint8_t *)value)[4] > 32) {
140  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix prefix length %" PRIu8 ".",
141  ((uint8_t *)value)[4]);
142  goto cleanup;
143  }
144 
145  /* store addr + prefix */
146  memcpy(val, value, value_len);
147 
148  /* zero host */
149  ipv4prefix_zero_host(&val->addr, val->prefix);
150 
151  /* success */
152  goto cleanup;
153  }
154 
155  /* check hints */
156  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
157  LY_CHECK_GOTO(ret, cleanup);
158 
159  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
160  /* length restriction of the string */
161  if (type_str->length) {
162  /* value_len is in bytes, but we need number of characters here */
163  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
164  LY_CHECK_GOTO(ret, cleanup);
165  }
166 
167  /* pattern restrictions */
168  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
169  LY_CHECK_GOTO(ret, cleanup);
170  }
171 
172  /* get the mask in network-byte order */
173  ret = ipv4prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
174  LY_CHECK_GOTO(ret, cleanup);
175 
176  /* zero host */
177  ipv4prefix_zero_host(&val->addr, val->prefix);
178 
179  if (format == LY_VALUE_CANON) {
180  /* store canonical value */
181  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
182  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
183  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
184  LY_CHECK_GOTO(ret, cleanup);
185  } else {
186  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
187  LY_CHECK_GOTO(ret, cleanup);
188  }
189  }
190 
191 cleanup:
192  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
193  free((void *)value);
194  }
195 
196  if (ret) {
197  lyplg_type_free_ipv4_prefix(ctx, storage);
198  }
199  return ret;
200 }
201 
205 static LY_ERR
206 lyplg_type_compare_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
207  const struct lyd_value *val2)
208 {
209  struct lyd_value_ipv4_prefix *v1, *v2;
210 
211  LYD_VALUE_GET(val1, v1);
212  LYD_VALUE_GET(val2, v2);
213 
214  if (memcmp(v1, v2, sizeof *v1)) {
215  return LY_ENOT;
216  }
217  return LY_SUCCESS;
218 }
219 
223 static int
224 lyplg_type_sort_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
225  const struct lyd_value *val2)
226 {
227  struct lyd_value_ipv4_prefix *v1, *v2;
228 
229  LYD_VALUE_GET(val1, v1);
230  LYD_VALUE_GET(val2, v2);
231 
232  return memcmp(v1, v2, sizeof *v1);
233 }
234 
238 static const void *
239 lyplg_type_print_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
240  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
241 {
242  struct lyd_value_ipv4_prefix *val;
243  char *ret;
244 
245  LYD_VALUE_GET(value, val);
246 
247  if (format == LY_VALUE_LYB) {
248  *dynamic = 0;
249  if (value_len) {
250  *value_len = LYB_VALUE_LEN;
251  }
252  return val;
253  }
254 
255  /* generate canonical value if not already */
256  if (!value->_canonical) {
257  /* IPv4 mask + '/' + prefix */
258  ret = malloc(INET_ADDRSTRLEN + 3);
259  LY_CHECK_RET(!ret, NULL);
260 
261  /* convert back to string */
262  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
263  free(ret);
264  return NULL;
265  }
266 
267  /* add the prefix */
268  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
269 
270  /* store it */
271  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
272  LOGMEM(ctx);
273  return NULL;
274  }
275  }
276 
277  /* use the cached canonical value */
278  if (dynamic) {
279  *dynamic = 0;
280  }
281  if (value_len) {
282  *value_len = strlen(value->_canonical);
283  }
284  return value->_canonical;
285 }
286 
290 static LY_ERR
291 lyplg_type_dup_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
292 {
293  LY_ERR ret;
294  struct lyd_value_ipv4_prefix *orig_val, *dup_val;
295 
296  memset(dup, 0, sizeof *dup);
297 
298  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
299  LY_CHECK_GOTO(ret, error);
300 
301  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
302  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
303 
304  LYD_VALUE_GET(original, orig_val);
305  memcpy(dup_val, orig_val, sizeof *orig_val);
306 
307  dup->realtype = original->realtype;
308  return LY_SUCCESS;
309 
310 error:
311  lyplg_type_free_ipv4_prefix(ctx, dup);
312  return ret;
313 }
314 
318 static void
319 lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
320 {
321  struct lyd_value_ipv4_prefix *val;
322 
323  lydict_remove(ctx, value->_canonical);
324  value->_canonical = NULL;
325  LYD_VALUE_GET(value, val);
327 }
328 
337  {
338  .module = "ietf-inet-types",
339  .revision = "2013-07-15",
340  .name = "ipv4-prefix",
341 
342  .plugin.id = "libyang 2 - ipv4-prefix, version 1",
343  .plugin.store = lyplg_type_store_ipv4_prefix,
344  .plugin.validate = NULL,
345  .plugin.compare = lyplg_type_compare_ipv4_prefix,
346  .plugin.sort = lyplg_type_sort_ipv4_prefix,
347  .plugin.print = lyplg_type_print_ipv4_prefix,
348  .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
349  .plugin.free = lyplg_type_free_ipv4_prefix,
350  .plugin.lyb_data_len = LYB_VALUE_LEN,
351  },
352  {0}
353 };
struct in_addr addr
Definition: tree_data.h:681
struct lysc_type * realtype
Definition: tree_data.h:579
Compiled YANG data node.
Definition: tree_schema.h:1439
#define LYB_VALUE_LEN
Definition: ipv4_prefix.c:50
struct lyplg_type_record plugins_ipv4_prefix[]
Plugin information for ipv4-prefix type implementation.
Definition: ipv4_prefix.c:336
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
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
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:680
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.