libyang 5.7.1
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv6_address_no_zone.c
Go to the documentation of this file.
1
14
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
49
50static void lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value);
51
62static LY_ERR
63ipv6addressnozone_str2ip(const char *value, uint32_t value_len, uint32_t options, struct in6_addr *addr, struct ly_err_item **err)
64{
65 LY_ERR ret = LY_SUCCESS;
66 const char *addr_str;
67 char *addr_dyn = NULL;
68
69 /* get the IP terminated with zero */
70 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
71 /* we can use the value directly */
72 addr_str = value;
73 } else {
74 addr_dyn = strndup(value, value_len);
75 addr_str = addr_dyn;
76 }
77
78 /* store the IPv6 address in network-byte order */
79 if (!inet_pton(AF_INET6, addr_str, addr)) {
80 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", addr_str);
81 goto cleanup;
82 }
83
84cleanup:
85 free(addr_dyn);
86 return ret;
87}
88
89static void
90lyplg_type_lyb_size_ipv6_address_no_zone(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
91 uint64_t *fixed_size_bits)
92{
93 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
94 *fixed_size_bits = 128;
95}
96
100static LY_ERR
101lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
102 uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
103 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
104 struct ly_err_item **err)
105{
106 LY_ERR ret = LY_SUCCESS;
108 uint32_t value_size;
109
110 /* init storage */
111 memset(storage, 0, sizeof *storage);
112 storage->realtype = type;
113
114 /* check value length */
115 ret = lyplg_type_check_value_size("ipv6-address-no-zone", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 128,
116 &value_size, err);
117 LY_CHECK_GOTO(ret, cleanup);
118
119 if (format == LY_VALUE_LYB) {
120 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
121 /* use the value directly */
122 storage->dyn_mem = (void *)value;
123 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
124 } else {
125 /* allocate value */
126 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
127 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
128
129 /* store IP address */
130 memcpy(&val->addr, value, sizeof val->addr);
131 }
132
133 /* success */
134 goto cleanup;
135 }
136
137 /* allocate value */
138 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
139 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
140
141 /* check hints */
142 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
143 LY_CHECK_GOTO(ret, cleanup);
144
145 /* get the network-byte order address, validates the value */
146 ret = ipv6addressnozone_str2ip(value, value_size, options, &val->addr, err);
147 LY_CHECK_GOTO(ret, cleanup);
148
149 if (format == LY_VALUE_CANON) {
150 /* store canonical value */
151 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
152 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
153 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
154 LY_CHECK_GOTO(ret, cleanup);
155 } else {
156 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
157 LY_CHECK_GOTO(ret, cleanup);
158 }
159 }
160
161cleanup:
162 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
163 free((void *)value);
164 }
165
166 if (ret) {
167 lyplg_type_free_ipv6_address_no_zone(ctx, storage);
168 }
169 return ret;
170}
171
175static LY_ERR
176lyplg_type_compare_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
177 const struct lyd_value *val2)
178{
179 struct lyd_value_ipv6_address_no_zone *v1, *v2;
180
181 LYD_VALUE_GET(val1, v1);
182 LYD_VALUE_GET(val2, v2);
183
184 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
185 return LY_ENOT;
186 }
187 return LY_SUCCESS;
188}
189
193static int
194lyplg_type_sort_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
195 const struct lyd_value *val2)
196{
197 struct lyd_value_ipv6_address_no_zone *v1, *v2;
198
199 LYD_VALUE_GET(val1, v1);
200 LYD_VALUE_GET(val2, v2);
201
202 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
203}
204
208static const void *
209lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
210 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
211{
213 char *ret;
214
215 LYD_VALUE_GET(value, val);
216
217 if (format == LY_VALUE_LYB) {
218 *dynamic = 0;
219 if (value_size_bits) {
220 *value_size_bits = sizeof val->addr * 8;
221 }
222 return &val->addr;
223 }
224
225 /* generate canonical value if not already */
226 if (!value->_canonical) {
227 /* '%' + zone */
228 ret = malloc(INET6_ADDRSTRLEN);
229 LY_CHECK_RET(!ret, NULL);
230
231 /* get the address in string */
232 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
233 free(ret);
234 LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
235 return NULL;
236 }
237
238 /* store it */
239 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
240 LOGMEM(ctx);
241 return NULL;
242 }
243 }
244
245 /* use the cached canonical value */
246 if (dynamic) {
247 *dynamic = 0;
248 }
249 if (value_size_bits) {
250 *value_size_bits = strlen(value->_canonical) * 8;
251 }
252 return value->_canonical;
253}
254
258static LY_ERR
259lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
260{
261 LY_ERR ret;
262 struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
263
264 memset(dup, 0, sizeof *dup);
265
266 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
267 LY_CHECK_GOTO(ret, error);
268
269 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
270 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
271
272 LYD_VALUE_GET(original, orig_val);
273 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
274
275 dup->realtype = original->realtype;
276 return LY_SUCCESS;
277
278error:
279 lyplg_type_free_ipv6_address_no_zone(ctx, dup);
280 return ret;
281}
282
286static void
287lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
288{
290
291 lydict_remove(ctx, value->_canonical);
292 value->_canonical = NULL;
293 LYD_VALUE_GET(value, val);
295}
296
305 {
306 .module = "ietf-inet-types",
307 .revision = NULL,
308 .name = "ipv6-address-no-zone",
309
310 .plugin.id = "ly2 ipv6-address-no-zone",
311 .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_no_zone,
312 .plugin.store = lyplg_type_store_ipv6_address_no_zone,
313 .plugin.validate_value = NULL,
314 .plugin.validate_tree = NULL,
315 .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
316 .plugin.sort = lyplg_type_sort_ipv6_address_no_zone,
317 .plugin.print = lyplg_type_print_ipv6_address_no_zone,
318 .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
319 .plugin.free = lyplg_type_free_ipv6_address_no_zone,
320 },
321 {0}
322};
libyang context handler.
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 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_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:252
@ LYVE_DATA
Definition log.h:289
@ LY_ESYS
Definition log.h:255
@ LY_EMEM
Definition log.h:254
@ LY_ENOT
Definition log.h:266
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:297
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
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's hints (if any) in the specified format.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_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.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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.
@ LYPLG_LYB_SIZE_FIXED_BITS
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv6_address_no_zone[]
Plugin information for ipv6-address-no-zone type implementation.
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:527
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:566
const char * _canonical
Definition tree_data.h:524
YANG data representation.
Definition tree_data.h:523
Special lyd_value structure for ietf-inet-types ipv6-address-no-zone values.
Definition tree_data.h:636
#define LOGMEM(CTX)
Definition tree_edit.h:22