libyang  3.13.5
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_no_zone.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* strndup */
16 
17 #include "plugins_internal.h"
18 #include "plugins_types.h"
19 
20 #ifdef _WIN32
21 # include <winsock2.h>
22 # include <ws2tcpip.h>
23 #else
24 # include <arpa/inet.h>
25 # if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
26 # include <netinet/in.h>
27 # include <sys/socket.h>
28 # endif
29 #endif
30 #include <assert.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "libyang.h"
37 
38 #include "compat.h"
39 #include "ly_common.h"
40 
53 static LY_ERR
54 lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
55  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
56  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
57  struct ly_err_item **err)
58 {
59  LY_ERR ret = LY_SUCCESS;
60  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
62 
63  /* init storage */
64  memset(storage, 0, sizeof *storage);
65  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
66  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
67  storage->realtype = type;
68 
69  if (format == LY_VALUE_LYB) {
70  /* validation */
71  if (value_len != 4) {
72  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
73  "(expected 4).", value_len);
74  goto cleanup;
75  }
76 
77  /* store IP address */
78  memcpy(&val->addr, value, 4);
79 
80  /* success */
81  goto cleanup;
82  }
83 
84  /* check hints */
85  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
86  LY_CHECK_GOTO(ret, cleanup);
87 
88  /* length restriction of the string */
89  if (type_str->length) {
90  /* value_len is in bytes, but we need number of characters here */
91  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
92  LY_CHECK_GOTO(ret, cleanup);
93  }
94 
95  /* pattern restrictions */
96  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
97  LY_CHECK_GOTO(ret, cleanup);
98 
99  /* we always need a dynamic value */
100  if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
101  value = strndup(value, value_len);
102  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
103 
104  options |= LYPLG_TYPE_STORE_DYNAMIC;
105  }
106 
107  /* get the network-byte order address */
108  if (!inet_pton(AF_INET, value, &val->addr)) {
109  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
110  goto cleanup;
111  }
112 
113  /* store canonical value */
114  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
115  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
116  LY_CHECK_GOTO(ret, cleanup);
117 
118 cleanup:
119  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
120  free((void *)value);
121  }
122 
123  if (ret) {
124  lyplg_type_free_simple(ctx, storage);
125  }
126  return ret;
127 }
128 
132 static LY_ERR
133 lyplg_type_compare_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
134  const struct lyd_value *val2)
135 {
136  struct lyd_value_ipv4_address_no_zone *v1, *v2;
137 
138  LYD_VALUE_GET(val1, v1);
139  LYD_VALUE_GET(val2, v2);
140 
141  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
142  return LY_ENOT;
143  }
144  return LY_SUCCESS;
145 }
146 
150 static int
151 lyplg_type_sort_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
152  const struct lyd_value *val2)
153 {
154  struct lyd_value_ipv4_address_no_zone *v1, *v2;
155 
156  LYD_VALUE_GET(val1, v1);
157  LYD_VALUE_GET(val2, v2);
158 
159  return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
160 }
161 
165 static const void *
166 lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
167  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
168 {
169  struct lyd_value_ipv4_address_no_zone *val;
170  char *ret;
171 
172  LYD_VALUE_GET(value, val);
173 
174  if (format == LY_VALUE_LYB) {
175  *dynamic = 0;
176  if (value_len) {
177  *value_len = 4;
178  }
179  return &val->addr;
180  }
181 
182  /* generate canonical value if not already (loaded from LYB) */
183  if (!value->_canonical) {
184  ret = malloc(INET_ADDRSTRLEN);
185  LY_CHECK_RET(!ret, NULL);
186 
187  /* get the address in string */
188  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
189  free(ret);
190  LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
191  return NULL;
192  }
193 
194  /* store it */
195  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
196  LOGMEM(ctx);
197  return NULL;
198  }
199  }
200 
201  /* use the cached canonical value */
202  if (dynamic) {
203  *dynamic = 0;
204  }
205  if (value_len) {
206  *value_len = strlen(value->_canonical);
207  }
208  return value->_canonical;
209 }
210 
219  {
220  .module = "ietf-inet-types",
221  .revision = "2013-07-15",
222  .name = "ipv4-address-no-zone",
223 
224  .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
225  .plugin.store = lyplg_type_store_ipv4_address_no_zone,
226  .plugin.validate = NULL,
227  .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
228  .plugin.sort = lyplg_type_sort_ipv4_address_no_zone,
229  .plugin.print = lyplg_type_print_ipv4_address_no_zone,
230  .plugin.duplicate = lyplg_type_dup_simple,
231  .plugin.free = lyplg_type_free_simple,
232  .plugin.lyb_data_len = 4,
233  },
234  {0}
235 };
struct lysc_type * realtype
Definition: tree_data.h:571
Compiled YANG data node.
Definition: tree_schema.h:1439
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:242
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition: tree_data.h:657
#define LOGMEM(CTX)
Definition: tree_edit.h:22
struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1339
struct lysc_range * length
Definition: tree_schema.h:1338
YANG data representation.
Definition: tree_data.h:567
const char * _canonical
Definition: tree_data.h:568
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:285
Definition: log.h:277
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:610
Definition: log.h:243
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
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
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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
Definition: log.h:248
#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:1305
LIBYANG_API_DECL 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.
LIBYANG_API_DECL 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.
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
libyang context handler.
Definition: log.h:254
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.