libyang  2.2.8
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  /* length restriction of the string */
160  if (type_str->length) {
161  /* value_len is in bytes, but we need number of characters here */
162  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
163  LY_CHECK_GOTO(ret, cleanup);
164  }
165 
166  /* pattern restrictions */
167  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
168  LY_CHECK_GOTO(ret, cleanup);
169 
170  /* get the mask in network-byte order */
171  ret = ipv4prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
172  LY_CHECK_GOTO(ret, cleanup);
173 
174  /* zero host */
175  ipv4prefix_zero_host(&val->addr, val->prefix);
176 
177  if (format == LY_VALUE_CANON) {
178  /* store canonical value */
179  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
180  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
181  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
182  LY_CHECK_GOTO(ret, cleanup);
183  } else {
184  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
185  LY_CHECK_GOTO(ret, cleanup);
186  }
187  }
188 
189 cleanup:
190  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
191  free((void *)value);
192  }
193 
194  if (ret) {
195  lyplg_type_free_ipv4_prefix(ctx, storage);
196  }
197  return ret;
198 }
199 
203 static LY_ERR
204 lyplg_type_compare_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
205  const struct lyd_value *val2)
206 {
207  struct lyd_value_ipv4_prefix *v1, *v2;
208 
209  LYD_VALUE_GET(val1, v1);
210  LYD_VALUE_GET(val2, v2);
211 
212  if (memcmp(v1, v2, sizeof *v1)) {
213  return LY_ENOT;
214  }
215  return LY_SUCCESS;
216 }
217 
221 static int
222 lyplg_type_sort_ipv4_prefix(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
223  const struct lyd_value *val2)
224 {
225  struct lyd_value_ipv4_prefix *v1, *v2;
226 
227  LYD_VALUE_GET(val1, v1);
228  LYD_VALUE_GET(val2, v2);
229 
230  return memcmp(v1, v2, sizeof *v1);
231 }
232 
236 static const void *
237 lyplg_type_print_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
238  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
239 {
240  struct lyd_value_ipv4_prefix *val;
241  char *ret;
242 
243  LYD_VALUE_GET(value, val);
244 
245  if (format == LY_VALUE_LYB) {
246  *dynamic = 0;
247  if (value_len) {
248  *value_len = LYB_VALUE_LEN;
249  }
250  return val;
251  }
252 
253  /* generate canonical value if not already */
254  if (!value->_canonical) {
255  /* IPv4 mask + '/' + prefix */
256  ret = malloc(INET_ADDRSTRLEN + 3);
257  LY_CHECK_RET(!ret, NULL);
258 
259  /* convert back to string */
260  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
261  free(ret);
262  return NULL;
263  }
264 
265  /* add the prefix */
266  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
267 
268  /* store it */
269  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
270  LOGMEM(ctx);
271  return NULL;
272  }
273  }
274 
275  /* use the cached canonical value */
276  if (dynamic) {
277  *dynamic = 0;
278  }
279  if (value_len) {
280  *value_len = strlen(value->_canonical);
281  }
282  return value->_canonical;
283 }
284 
288 static LY_ERR
289 lyplg_type_dup_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
290 {
291  LY_ERR ret;
292  struct lyd_value_ipv4_prefix *orig_val, *dup_val;
293 
294  memset(dup, 0, sizeof *dup);
295 
296  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
297  LY_CHECK_GOTO(ret, error);
298 
299  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
300  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
301 
302  LYD_VALUE_GET(original, orig_val);
303  memcpy(dup_val, orig_val, sizeof *orig_val);
304 
305  dup->realtype = original->realtype;
306  return LY_SUCCESS;
307 
308 error:
309  lyplg_type_free_ipv4_prefix(ctx, dup);
310  return ret;
311 }
312 
316 static void
317 lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
318 {
319  struct lyd_value_ipv4_prefix *val;
320 
321  lydict_remove(ctx, value->_canonical);
322  value->_canonical = NULL;
323  LYD_VALUE_GET(value, val);
325 }
326 
335  {
336  .module = "ietf-inet-types",
337  .revision = "2013-07-15",
338  .name = "ipv4-prefix",
339 
340  .plugin.id = "libyang 2 - ipv4-prefix, version 1",
341  .plugin.store = lyplg_type_store_ipv4_prefix,
342  .plugin.validate = NULL,
343  .plugin.compare = lyplg_type_compare_ipv4_prefix,
344  .plugin.sort = lyplg_type_sort_ipv4_prefix,
345  .plugin.print = lyplg_type_print_ipv4_prefix,
346  .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
347  .plugin.free = lyplg_type_free_ipv4_prefix,
348  .plugin.lyb_data_len = LYB_VALUE_LEN,
349  },
350  {0}
351 };
struct in_addr addr
Definition: tree_data.h:677
struct lysc_type * realtype
Definition: tree_data.h:575
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:334
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:571
const char * _canonical
Definition: tree_data.h:572
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:614
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:676
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.
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.