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