libyang  4.0.0
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 
50 static void
51 lyplg_type_lyb_size_ipv4_address_no_zone(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
52  uint32_t *fixed_size_bits)
53 {
54  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
55  *fixed_size_bits = 32;
56 }
57 
61 static LY_ERR
62 lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
63  uint32_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
64  const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
65  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
66 {
67  LY_ERR ret = LY_SUCCESS;
69  uint32_t value_size;
70 
71  /* init storage */
72  memset(storage, 0, sizeof *storage);
73  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
74  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
75  storage->realtype = type;
76 
77  /* check value length */
78  ret = lyplg_type_check_value_size("ipv4-address-no-zone", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 32,
79  &value_size, err);
80  LY_CHECK_GOTO(ret, cleanup);
81 
82  if (format == LY_VALUE_LYB) {
83  /* store IP address */
84  memcpy(&val->addr, value, 4);
85 
86  /* success */
87  goto cleanup;
88  }
89 
90  /* check hints */
91  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
92  LY_CHECK_GOTO(ret, cleanup);
93 
94  /* we always need a dynamic value */
95  if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
96  value = strndup(value, value_size);
97  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
98 
99  options |= LYPLG_TYPE_STORE_DYNAMIC;
100  }
101 
102  /* get the network-byte order address */
103  if (!inet_pton(AF_INET, value, &val->addr)) {
104  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv4 address \"%s\".", (char *)value);
105  goto cleanup;
106  }
107 
108  /* store canonical value */
109  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
110  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
111  LY_CHECK_GOTO(ret, cleanup);
112 
113 cleanup:
114  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
115  free((void *)value);
116  }
117 
118  if (ret) {
119  lyplg_type_free_simple(ctx, storage);
120  }
121  return ret;
122 }
123 
127 static LY_ERR
128 lyplg_type_compare_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
129  const struct lyd_value *val2)
130 {
131  struct lyd_value_ipv4_address_no_zone *v1, *v2;
132 
133  LYD_VALUE_GET(val1, v1);
134  LYD_VALUE_GET(val2, v2);
135 
136  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
137  return LY_ENOT;
138  }
139  return LY_SUCCESS;
140 }
141 
145 static int
146 lyplg_type_sort_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
147  const struct lyd_value *val2)
148 {
149  struct lyd_value_ipv4_address_no_zone *v1, *v2;
150 
151  LYD_VALUE_GET(val1, v1);
152  LYD_VALUE_GET(val2, v2);
153 
154  return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
155 }
156 
160 static const void *
161 lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
162  void *UNUSED(prefix_data), ly_bool *dynamic, uint32_t *value_size_bits)
163 {
164  struct lyd_value_ipv4_address_no_zone *val;
165  char *ret;
166 
167  LYD_VALUE_GET(value, val);
168 
169  if (format == LY_VALUE_LYB) {
170  *dynamic = 0;
171  if (value_size_bits) {
172  *value_size_bits = 32;
173  }
174  return &val->addr;
175  }
176 
177  /* generate canonical value if not already (loaded from LYB) */
178  if (!value->_canonical) {
179  ret = malloc(INET_ADDRSTRLEN);
180  LY_CHECK_RET(!ret, NULL);
181 
182  /* get the address in string */
183  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
184  free(ret);
185  LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
186  return NULL;
187  }
188 
189  /* store it */
190  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
191  LOGMEM(ctx);
192  return NULL;
193  }
194  }
195 
196  /* use the cached canonical value */
197  if (dynamic) {
198  *dynamic = 0;
199  }
200  if (value_size_bits) {
201  *value_size_bits = strlen(value->_canonical) * 8;
202  }
203  return value->_canonical;
204 }
205 
214  {
215  .module = "ietf-inet-types",
216  .revision = "2013-07-15",
217  .name = "ipv4-address-no-zone",
218 
219  .plugin.id = "ly2 ipv4-address-no-zone",
220  .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_no_zone,
221  .plugin.store = lyplg_type_store_ipv4_address_no_zone,
222  .plugin.validate_value = NULL,
223  .plugin.validate_tree = NULL,
224  .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
225  .plugin.sort = lyplg_type_sort_ipv4_address_no_zone,
226  .plugin.print = lyplg_type_print_ipv4_address_no_zone,
227  .plugin.duplicate = lyplg_type_dup_simple,
228  .plugin.free = lyplg_type_free_simple,
229  },
230  {0}
231 };
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
YANG extension compiled instance.
Definition: plugins_exts.h:436
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
Definition: log.h:254
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition: tree_data.h:654
#define LOGMEM(CTX)
Definition: tree_edit.h:22
Definition: log.h:243
struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
Definition: log.h:242
The main libyang public header.
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
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:606
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.
Definition: log.h:248
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
#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:1300
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.
return memcmp(v1->data, v2->data, bitmap_size)
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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.
API for (user) types plugins.
libyang context handler.