libyang 5.8.6
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,
64 struct lyd_value_ipv6_address_no_zone *val, struct ly_err_item **err)
65{
66 LY_ERR ret = LY_SUCCESS;
67 const char *addr_str;
68 char *addr_dyn = NULL;
69
70 /* get the IP terminated with zero */
71 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
72 /* we can use the value directly */
73 addr_str = value;
74 } else {
75 addr_dyn = strndup(value, value_len);
76 addr_str = addr_dyn;
77 }
78
79 /* store the IPv6 address in network-byte order */
80 if (!inet_pton(AF_INET6, addr_str, &val->addr)) {
81 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", addr_str);
82 goto cleanup;
83 }
84
85cleanup:
86 free(addr_dyn);
87 return ret;
88}
89
90static void
91lyplg_type_lyb_size_ipv6_address_no_zone(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
92 uint64_t *fixed_size_bits)
93{
94 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
95 *fixed_size_bits = 128;
96}
97
101static LY_ERR
102lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
103 uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
104 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
105 struct ly_err_item **err)
106{
107 LY_ERR ret = LY_SUCCESS;
109 uint32_t value_size;
110
111 /* init storage */
112 memset(storage, 0, sizeof *storage);
113 storage->realtype = type;
114
115 /* check value length */
116 ret = lyplg_type_check_value_size("ipv6-address-no-zone", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 128,
117 &value_size, err);
118 LY_CHECK_GOTO(ret, cleanup);
119
120 if (format == LY_VALUE_LYB) {
121 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
122 /* use the value directly */
123 storage->dyn_mem = (void *)value;
124 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
125 } else {
126 /* allocate value */
127 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
128 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
129
130 /* store IP address */
131 memcpy(&val->addr, value, sizeof val->addr);
132 }
133
134 /* success */
135 goto cleanup;
136 }
137
138 /* allocate value */
139 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
140 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
141
142 /* check hints */
143 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
144 LY_CHECK_GOTO(ret, cleanup);
145
146 /* get the network-byte order address, validates the value */
147 ret = ipv6addressnozone_str2ip(value, value_size, options, val, err);
148 LY_CHECK_GOTO(ret, cleanup);
149
150 if (format == LY_VALUE_CANON) {
151 /* store canonical value */
152 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
153 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
154 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
155 LY_CHECK_GOTO(ret, cleanup);
156 } else {
157 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
158 LY_CHECK_GOTO(ret, cleanup);
159 }
160 }
161
162cleanup:
163 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
164 free((void *)value);
165 }
166
167 if (ret) {
168 lyplg_type_free_ipv6_address_no_zone(ctx, storage);
169 }
170 return ret;
171}
172
176static LY_ERR
177lyplg_type_compare_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
178 const struct lyd_value *val2)
179{
180 struct lyd_value_ipv6_address_no_zone *v1, *v2;
181
182 LYD_VALUE_GET(val1, v1);
183 LYD_VALUE_GET(val2, v2);
184
185 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
186 return LY_ENOT;
187 }
188 return LY_SUCCESS;
189}
190
194static int
195lyplg_type_sort_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
196 const struct lyd_value *val2)
197{
198 struct lyd_value_ipv6_address_no_zone *v1, *v2;
199
200 LYD_VALUE_GET(val1, v1);
201 LYD_VALUE_GET(val2, v2);
202
203 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
204}
205
209static const void *
210lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
211 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
212{
214 char *ret;
215
216 LYD_VALUE_GET(value, val);
217
218 if (format == LY_VALUE_LYB) {
219 *dynamic = 0;
220 if (value_size_bits) {
221 *value_size_bits = sizeof val->addr * 8;
222 }
223 return &val->addr;
224 }
225
226 /* generate canonical value if not already */
227 if (!value->_canonical) {
228 /* '%' + zone */
229 ret = malloc(INET6_ADDRSTRLEN);
230 LY_CHECK_RET(!ret, NULL);
231
232 /* get the address in string */
233 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
234 free(ret);
235 LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
236 return NULL;
237 }
238
239 /* store it */
240 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
241 LOGMEM(ctx);
242 return NULL;
243 }
244 }
245
246 /* use the cached canonical value */
247 if (dynamic) {
248 *dynamic = 0;
249 }
250 if (value_size_bits) {
251 *value_size_bits = strlen(value->_canonical) * 8;
252 }
253 return value->_canonical;
254}
255
259static LY_ERR
260lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
261{
262 LY_ERR ret;
263 struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
264
265 memset(dup, 0, sizeof *dup);
266
267 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
268 LY_CHECK_GOTO(ret, error);
269
270 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
271 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
272
273 LYD_VALUE_GET(original, orig_val);
274 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
275
276 dup->realtype = original->realtype;
277 return LY_SUCCESS;
278
279error:
280 lyplg_type_free_ipv6_address_no_zone(ctx, dup);
281 return ret;
282}
283
287static void
288lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
289{
291
292 lydict_remove(ctx, value->_canonical);
293 value->_canonical = NULL;
294 LYD_VALUE_GET(value, val);
296}
297
306 {
307 .module = "ietf-inet-types",
308 .revision = NULL,
309 .name = "ipv6-address-no-zone",
310
311 .plugin.id = "ly2 ipv6-address-no-zone",
312 .plugin.lyb_size = lyplg_type_lyb_size_ipv6_address_no_zone,
313 .plugin.store = lyplg_type_store_ipv6_address_no_zone,
314 .plugin.validate_value = NULL,
315 .plugin.validate_tree = NULL,
316 .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
317 .plugin.sort = lyplg_type_sort_ipv6_address_no_zone,
318 .plugin.print = lyplg_type_print_ipv6_address_no_zone,
319 .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
320 .plugin.free = lyplg_type_free_ipv6_address_no_zone,
321 },
322 {0}
323};
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:551
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:590
const char * _canonical
Definition tree_data.h:548
YANG data representation.
Definition tree_data.h:547
Special lyd_value structure for ietf-inet-types ipv6-address-no-zone values.
Definition tree_data.h:660
#define LOGMEM(CTX)
Definition tree_edit.h:22