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