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