libyang  2.0.112
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_types.h"
18 
19 #include <arpa/inet.h>
20 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #endif
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "libyang.h"
29 
30 #include "common.h"
31 #include "compat.h"
32 
43 #define LYB_VALUE_LEN 5
44 
45 static void lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value);
46 
57 static LY_ERR
58 ipv4prefix_str2ip(const char *value, size_t value_len, struct in_addr *addr, uint8_t *prefix, struct ly_err_item **err)
59 {
60  LY_ERR ret = LY_SUCCESS;
61  const char *pref_str;
62  char *mask_str = NULL;
63 
64  /* it passed the pattern validation */
65  pref_str = ly_strnchr(value, '/', value_len);
66  ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
67 
68  /* get just the network prefix */
69  mask_str = strndup(value, pref_str - value);
70  LY_CHECK_ERR_GOTO(!mask_str, ret = LY_EMEM, cleanup);
71 
72  /* convert it to netword-byte order */
73  if (inet_pton(AF_INET, mask_str, addr) != 1) {
74  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", mask_str);
75  goto cleanup;
76  }
77 
78 cleanup:
79  free(mask_str);
80  return ret;
81 }
82 
89 static void
90 ipv4prefix_zero_host(struct in_addr *addr, uint8_t prefix)
91 {
92  uint32_t i, mask;
93 
94  /* zero host bits */
95  mask = 0;
96  for (i = 0; i < 32; ++i) {
97  mask <<= 1;
98  if (prefix > i) {
99  mask |= 1;
100  }
101  }
102  mask = htonl(mask);
103  addr->s_addr &= mask;
104 }
105 
109 static LY_ERR
110 lyplg_type_store_ipv4_prefix(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
111  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
112  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
113  struct ly_err_item **err)
114 {
115  LY_ERR ret = LY_SUCCESS;
116  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
117  struct lyd_value_ipv4_prefix *val;
118 
119  /* init storage */
120  memset(storage, 0, sizeof *storage);
121  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
122  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
123  storage->realtype = type;
124 
125  if (format == LY_VALUE_LYB) {
126  /* validation */
127  if (value_len != LYB_VALUE_LEN) {
128  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix value size %zu (expected %d).",
129  value_len, LYB_VALUE_LEN);
130  goto cleanup;
131  }
132  if (((uint8_t *)value)[4] > 32) {
133  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-prefix prefix length %" PRIu8 ".",
134  ((uint8_t *)value)[4]);
135  goto cleanup;
136  }
137 
138  /* store addr + prefix */
139  memcpy(val, value, value_len);
140 
141  /* zero host */
142  ipv4prefix_zero_host(&val->addr, val->prefix);
143 
144  /* success */
145  goto cleanup;
146  }
147 
148  /* check hints */
149  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
150  LY_CHECK_GOTO(ret, cleanup);
151 
152  /* length restriction of the string */
153  if (type_str->length) {
154  /* value_len is in bytes, but we need number of characters here */
155  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
156  LY_CHECK_GOTO(ret, cleanup);
157  }
158 
159  /* pattern restrictions */
160  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
161  LY_CHECK_GOTO(ret, cleanup);
162 
163  /* get the mask in network-byte order */
164  ret = ipv4prefix_str2ip(value, value_len, &val->addr, &val->prefix, err);
165  LY_CHECK_GOTO(ret, cleanup);
166 
167  /* zero host */
168  ipv4prefix_zero_host(&val->addr, val->prefix);
169 
170  if (format == LY_VALUE_CANON) {
171  /* store canonical value */
172  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
173  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
174  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
175  LY_CHECK_GOTO(ret, cleanup);
176  } else {
177  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
178  LY_CHECK_GOTO(ret, cleanup);
179  }
180  }
181 
182 cleanup:
183  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
184  free((void *)value);
185  }
186 
187  if (ret) {
188  lyplg_type_free_ipv4_prefix(ctx, storage);
189  }
190  return ret;
191 }
192 
196 static LY_ERR
197 lyplg_type_compare_ipv4_prefix(const struct lyd_value *val1, const struct lyd_value *val2)
198 {
199  struct lyd_value_ipv4_prefix *v1, *v2;
200 
201  if (val1->realtype != val2->realtype) {
202  return LY_ENOT;
203  }
204 
205  LYD_VALUE_GET(val1, v1);
206  LYD_VALUE_GET(val2, v2);
207 
208  if (memcmp(v1, v2, sizeof *v1)) {
209  return LY_ENOT;
210  }
211  return LY_SUCCESS;
212 }
213 
217 static const void *
218 lyplg_type_print_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
219  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
220 {
221  struct lyd_value_ipv4_prefix *val;
222  char *ret;
223 
224  LYD_VALUE_GET(value, val);
225 
226  if (format == LY_VALUE_LYB) {
227  *dynamic = 0;
228  if (value_len) {
229  *value_len = LYB_VALUE_LEN;
230  }
231  return val;
232  }
233 
234  /* generate canonical value if not already */
235  if (!value->_canonical) {
236  /* IPv4 mask + '/' + prefix */
237  ret = malloc(INET_ADDRSTRLEN + 3);
238  LY_CHECK_RET(!ret, NULL);
239 
240  /* convert back to string */
241  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
242  free(ret);
243  return NULL;
244  }
245 
246  /* add the prefix */
247  sprintf(ret + strlen(ret), "/%" PRIu8, val->prefix);
248 
249  /* store it */
250  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
251  LOGMEM(ctx);
252  return NULL;
253  }
254  }
255 
256  /* use the cached canonical value */
257  if (dynamic) {
258  *dynamic = 0;
259  }
260  if (value_len) {
261  *value_len = strlen(value->_canonical);
262  }
263  return value->_canonical;
264 }
265 
269 static LY_ERR
270 lyplg_type_dup_ipv4_prefix(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
271 {
272  LY_ERR ret;
273  struct lyd_value_ipv4_prefix *orig_val, *dup_val;
274 
275  memset(dup, 0, sizeof *dup);
276 
277  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
278  LY_CHECK_GOTO(ret, error);
279 
280  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
281  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
282 
283  LYD_VALUE_GET(original, orig_val);
284  memcpy(dup_val, orig_val, sizeof *orig_val);
285 
286  dup->realtype = original->realtype;
287  return LY_SUCCESS;
288 
289 error:
290  lyplg_type_free_ipv4_prefix(ctx, dup);
291  return ret;
292 }
293 
297 static void
298 lyplg_type_free_ipv4_prefix(const struct ly_ctx *ctx, struct lyd_value *value)
299 {
300  struct lyd_value_ipv4_prefix *val;
301 
302  lydict_remove(ctx, value->_canonical);
303  value->_canonical = NULL;
304  LYD_VALUE_GET(value, val);
306 }
307 
316  {
317  .module = "ietf-inet-types",
318  .revision = "2013-07-15",
319  .name = "ipv4-prefix",
320 
321  .plugin.id = "libyang 2 - ipv4-prefix, version 1",
322  .plugin.store = lyplg_type_store_ipv4_prefix,
323  .plugin.validate = NULL,
324  .plugin.compare = lyplg_type_compare_ipv4_prefix,
325  .plugin.sort = NULL,
326  .plugin.print = lyplg_type_print_ipv4_prefix,
327  .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
328  .plugin.free = lyplg_type_free_ipv4_prefix,
329  .plugin.lyb_data_len = LYB_VALUE_LEN,
330  },
331  {0}
332 };
struct in_addr addr
Definition: tree_data.h:641
struct lysc_type * realtype
Definition: tree_data.h:539
Compiled YANG data node.
Definition: tree_schema.h:1666
#define LYB_VALUE_LEN
Definition: ipv4_prefix.c:43
struct lyplg_type_record plugins_ipv4_prefix[]
Plugin information for ipv4-prefix type implementation.
Definition: ipv4_prefix.c:315
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
Definition: log.h:248
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
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 ...
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
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...
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1580
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.
struct lysc_range * length
Definition: tree_schema.h:1579
YANG data representation.
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:536
Libyang full error structure.
Definition: log.h:291
Definition: log.h:283
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:578
Definition: log.h:254
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.
Special lyd_value structure for ietf-inet-types ipv4-prefix values.
Definition: tree_data.h:640
const char * module
Definition: log.h:260
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
#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:1552
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:245
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.
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.
API for (user) types plugins.
libyang context handler.