libyang  2.1.111
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_address.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 <ctype.h>
30 #include <errno.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "libyang.h"
36 
37 #include "common.h"
38 #include "compat.h"
39 
50 static void lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value);
51 
64 static LY_ERR
65 ipv4address_str2ip(const char *value, size_t value_len, uint32_t options, const struct ly_ctx *ctx,
66  struct in_addr *addr, const char **zone, struct ly_err_item **err)
67 {
68  LY_ERR ret = LY_SUCCESS;
69  const char *addr_no_zone;
70  char *zone_ptr = NULL, *addr_dyn = NULL;
71  size_t zone_len;
72 
73  /* store zone and get the string IPv4 address without it */
74  if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
75  /* there is a zone index */
76  zone_len = value_len - (zone_ptr - value) - 1;
77  ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
78  LY_CHECK_GOTO(ret, cleanup);
79 
80  /* get the IP without it */
81  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
82  *zone_ptr = '\0';
83  addr_no_zone = value;
84  } else {
85  addr_dyn = strndup(value, zone_ptr - value);
86  addr_no_zone = addr_dyn;
87  }
88  } else {
89  /* no zone */
90  *zone = NULL;
91 
92  /* get the IP terminated with zero */
93  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
94  /* we can use the value directly */
95  addr_no_zone = value;
96  } else {
97  addr_dyn = strndup(value, value_len);
98  addr_no_zone = addr_dyn;
99  }
100  }
101 
102  /* store the IPv4 address in network-byte order */
103  if (!inet_pton(AF_INET, addr_no_zone, addr)) {
104  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", addr_no_zone);
105  goto cleanup;
106  }
107 
108  /* restore the value */
109  if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
110  *zone_ptr = '%';
111  }
112 
113 cleanup:
114  free(addr_dyn);
115  return ret;
116 }
117 
121 static LY_ERR
122 lyplg_type_store_ipv4_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
123  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
124  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
125  struct ly_err_item **err)
126 {
127  LY_ERR ret = LY_SUCCESS;
128  const char *value_str = value;
129  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
130  struct lyd_value_ipv4_address *val;
131  size_t i;
132 
133  /* init storage */
134  memset(storage, 0, sizeof *storage);
135  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
136  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
137  storage->realtype = type;
138 
139  if (format == LY_VALUE_LYB) {
140  /* validation */
141  if (value_len < 4) {
142  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %zu "
143  "(expected at least 4).", value_len);
144  goto cleanup;
145  }
146  for (i = 4; i < value_len; ++i) {
147  if (!isalnum(value_str[i])) {
148  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address zone character 0x%x.",
149  value_str[i]);
150  goto cleanup;
151  }
152  }
153 
154  /* store IP address */
155  memcpy(&val->addr, value, sizeof val->addr);
156 
157  /* store zone, if any */
158  if (value_len > 4) {
159  ret = lydict_insert(ctx, value_str + 4, value_len - 4, &val->zone);
160  LY_CHECK_GOTO(ret, cleanup);
161  } else {
162  val->zone = NULL;
163  }
164 
165  /* success */
166  goto cleanup;
167  }
168 
169  /* check hints */
170  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
171  LY_CHECK_GOTO(ret, cleanup);
172 
173  /* length restriction of the string */
174  if (type_str->length) {
175  /* value_len is in bytes, but we need number of characters here */
176  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
177  LY_CHECK_GOTO(ret, cleanup);
178  }
179 
180  /* pattern restrictions */
181  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
182  LY_CHECK_GOTO(ret, cleanup);
183 
184  /* get the network-byte order address */
185  ret = ipv4address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
186  LY_CHECK_GOTO(ret, cleanup);
187 
188  /* store canonical value */
189  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
190  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
191  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
192  LY_CHECK_GOTO(ret, cleanup);
193  } else {
194  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
195  LY_CHECK_GOTO(ret, cleanup);
196  }
197 
198 cleanup:
199  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
200  free((void *)value);
201  }
202 
203  if (ret) {
204  lyplg_type_free_ipv4_address(ctx, storage);
205  }
206  return ret;
207 }
208 
212 static LY_ERR
213 lyplg_type_compare_ipv4_address(const struct lyd_value *val1, const struct lyd_value *val2)
214 {
215  struct lyd_value_ipv4_address *v1, *v2;
216 
217  if (val1->realtype != val2->realtype) {
218  return LY_ENOT;
219  }
220 
221  LYD_VALUE_GET(val1, v1);
222  LYD_VALUE_GET(val2, v2);
223 
224  /* zones are NULL or in the dictionary */
225  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
226  return LY_ENOT;
227  }
228  return LY_SUCCESS;
229 }
230 
234 static const void *
235 lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
236  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
237 {
238  struct lyd_value_ipv4_address *val;
239  size_t zone_len;
240  char *ret;
241 
242  LYD_VALUE_GET(value, val);
243 
244  if (format == LY_VALUE_LYB) {
245  if (!val->zone) {
246  /* address-only, const */
247  *dynamic = 0;
248  if (value_len) {
249  *value_len = sizeof val->addr;
250  }
251  return &val->addr;
252  }
253 
254  /* dynamic */
255  zone_len = strlen(val->zone);
256  ret = malloc(sizeof val->addr + zone_len);
257  LY_CHECK_RET(!ret, NULL);
258 
259  memcpy(ret, &val->addr, sizeof val->addr);
260  memcpy(ret + sizeof val->addr, val->zone, zone_len);
261 
262  *dynamic = 1;
263  if (value_len) {
264  *value_len = sizeof val->addr + zone_len;
265  }
266  return ret;
267  }
268 
269  /* generate canonical value if not already */
270  if (!value->_canonical) {
271  /* '%' + zone */
272  zone_len = val->zone ? strlen(val->zone) + 1 : 0;
273  ret = malloc(INET_ADDRSTRLEN + zone_len);
274  LY_CHECK_RET(!ret, NULL);
275 
276  /* get the address in string */
277  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
278  free(ret);
279  LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
280  return NULL;
281  }
282 
283  /* add zone */
284  if (zone_len) {
285  sprintf(ret + strlen(ret), "%%%s", val->zone);
286  }
287 
288  /* store it */
289  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
290  LOGMEM(ctx);
291  return NULL;
292  }
293  }
294 
295  /* use the cached canonical value */
296  if (dynamic) {
297  *dynamic = 0;
298  }
299  if (value_len) {
300  *value_len = strlen(value->_canonical);
301  }
302  return value->_canonical;
303 }
304 
308 static LY_ERR
309 lyplg_type_dup_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
310 {
311  LY_ERR ret;
312  struct lyd_value_ipv4_address *orig_val, *dup_val;
313 
314  memset(dup, 0, sizeof *dup);
315 
316  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
317  LY_CHECK_GOTO(ret, error);
318 
319  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
320  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
321 
322  LYD_VALUE_GET(original, orig_val);
323 
324  memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
325  ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
326  LY_CHECK_GOTO(ret, error);
327 
328  dup->realtype = original->realtype;
329  return LY_SUCCESS;
330 
331 error:
332  lyplg_type_free_ipv4_address(ctx, dup);
333  return ret;
334 }
335 
339 static void
340 lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value)
341 {
342  struct lyd_value_ipv4_address *val;
343 
344  lydict_remove(ctx, value->_canonical);
345  value->_canonical = NULL;
346  LYD_VALUE_GET(value, val);
347  if (val) {
348  lydict_remove(ctx, val->zone);
350  }
351 }
352 
361  {
362  .module = "ietf-inet-types",
363  .revision = "2013-07-15",
364  .name = "ipv4-address",
365 
366  .plugin.id = "libyang 2 - ipv4-address, version 1",
367  .plugin.store = lyplg_type_store_ipv4_address,
368  .plugin.validate = NULL,
369  .plugin.compare = lyplg_type_compare_ipv4_address,
370  .plugin.sort = NULL,
371  .plugin.print = lyplg_type_print_ipv4_address,
372  .plugin.duplicate = lyplg_type_dup_ipv4_address,
373  .plugin.free = lyplg_type_free_ipv4_address,
374  .plugin.lyb_data_len = -1,
375  },
376  {0}
377 };
struct lysc_type * realtype
Definition: tree_data.h:564
Compiled YANG data node.
Definition: tree_schema.h:1414
struct in_addr addr
Definition: tree_data.h:658
const char * zone
Definition: tree_data.h:659
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
struct lyplg_type_record plugins_ipv4_address[]
Plugin information for ipv4-address type implementation.
Definition: ipv4_address.c:360
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address values.
Definition: tree_data.h:657
#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
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.