libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
ipv4_address_no_zone.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 <ctype.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include "libyang.h"
33 
34 #include "common.h"
35 #include "compat.h"
36 
49 static LY_ERR
50 lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
51  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
52  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
53  struct ly_err_item **err)
54 {
55  LY_ERR ret = LY_SUCCESS;
56  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
58 
59  /* init storage */
60  memset(storage, 0, sizeof *storage);
61  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
62  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
63  storage->realtype = type;
64 
65  if (format == LY_VALUE_LYB) {
66  /* validation */
67  if (value_len != 4) {
68  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
69  "(expected 4).", value_len);
70  goto cleanup;
71  }
72 
73  /* store IP address */
74  memcpy(&val->addr, value, 4);
75 
76  /* success */
77  goto cleanup;
78  }
79 
80  /* check hints */
81  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
82  LY_CHECK_GOTO(ret, cleanup);
83 
84  /* length restriction of the string */
85  if (type_str->length) {
86  /* value_len is in bytes, but we need number of characters here */
87  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
88  LY_CHECK_GOTO(ret, cleanup);
89  }
90 
91  /* pattern restrictions */
92  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
93  LY_CHECK_GOTO(ret, cleanup);
94 
95  /* we always need a dynamic value */
96  if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
97  value = strndup(value, value_len);
98  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
99 
100  options |= LYPLG_TYPE_STORE_DYNAMIC;
101  }
102 
103  /* get the network-byte order address */
104  if (!inet_pton(AF_INET, value, &val->addr)) {
105  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
106  goto cleanup;
107  }
108 
109  /* store canonical value */
110  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
111  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
112  LY_CHECK_GOTO(ret, cleanup);
113 
114 cleanup:
115  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
116  free((void *)value);
117  }
118 
119  if (ret) {
120  lyplg_type_free_simple(ctx, storage);
121  }
122  return ret;
123 }
124 
128 static LY_ERR
129 lyplg_type_compare_ipv4_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
130 {
131  struct lyd_value_ipv4_address_no_zone *v1, *v2;
132 
133  if (val1->realtype != val2->realtype) {
134  return LY_ENOT;
135  }
136 
137  LYD_VALUE_GET(val1, v1);
138  LYD_VALUE_GET(val2, v2);
139 
140  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
141  return LY_ENOT;
142  }
143  return LY_SUCCESS;
144 }
145 
149 static const void *
150 lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
151  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
152 {
153  struct lyd_value_ipv4_address_no_zone *val;
154  char *ret;
155 
156  LYD_VALUE_GET(value, val);
157 
158  if (format == LY_VALUE_LYB) {
159  *dynamic = 0;
160  if (value_len) {
161  *value_len = 4;
162  }
163  return &val->addr;
164  }
165 
166  /* generate canonical value if not already (loaded from LYB) */
167  if (!value->_canonical) {
168  ret = malloc(INET_ADDRSTRLEN);
169  LY_CHECK_RET(!ret, NULL);
170 
171  /* get the address in string */
172  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
173  free(ret);
174  LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
175  return NULL;
176  }
177 
178  /* store it */
179  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
180  LOGMEM(ctx);
181  return NULL;
182  }
183  }
184 
185  /* use the cached canonical value */
186  if (dynamic) {
187  *dynamic = 0;
188  }
189  if (value_len) {
190  *value_len = strlen(value->_canonical);
191  }
192  return value->_canonical;
193 }
194 
203  {
204  .module = "ietf-inet-types",
205  .revision = "2013-07-15",
206  .name = "ipv4-address-no-zone",
207 
208  .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
209  .plugin.store = lyplg_type_store_ipv4_address_no_zone,
210  .plugin.validate = NULL,
211  .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
212  .plugin.sort = NULL,
213  .plugin.print = lyplg_type_print_ipv4_address_no_zone,
214  .plugin.duplicate = lyplg_type_dup_simple,
215  .plugin.free = lyplg_type_free_simple
216  },
217  {0}
218 };
libyang context handler.
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
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.
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.
LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
#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_LYB
Definition: tree.h:241
const struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
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
#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-address-no-zone values.
Definition: tree_data.h:620
#define LOGMEM(CTX)
Definition: tree_edit.h:25