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.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 <ctype.h>
32#include <errno.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "libyang.h"
38
39#include "compat.h"
40#include "ly_common.h"
41
51
52static void lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value);
53
65static LY_ERR
66ipv6address_str2ip(const char *value, uint32_t value_len, uint32_t options, const struct ly_ctx *ctx,
67 struct lyd_value_ipv6_address *val, struct ly_err_item **err)
68{
69 LY_ERR ret = LY_SUCCESS;
70 const char *addr_no_zone;
71 char *zone_ptr = NULL, *addr_dyn = NULL;
72 uint32_t zone_len;
73
74 /* store zone and get the string IPv6 address without it */
75 if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
76 /* there is a zone index */
77 zone_len = value_len - (zone_ptr - value) - 1;
78 ret = lydict_insert(ctx, zone_ptr + 1, zone_len, &val->zone);
79 LY_CHECK_GOTO(ret, cleanup);
80
81 /* get the IP without it */
82 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
83 *zone_ptr = '\0';
84 addr_no_zone = value;
85 } else {
86 addr_dyn = strndup(value, zone_ptr - value);
87 addr_no_zone = addr_dyn;
88 }
89 } else {
90 /* no zone */
91 val->zone = NULL;
92
93 /* get the IP terminated with zero */
94 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
95 /* we can use the value directly */
96 addr_no_zone = value;
97 } else {
98 addr_dyn = strndup(value, value_len);
99 addr_no_zone = addr_dyn;
100 }
101 }
102
103 /* store the IPv6 address in network-byte order */
104 if (!inet_pton(AF_INET6, addr_no_zone, &val->addr)) {
105 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv6 address \"%s\".", addr_no_zone);
106 goto cleanup;
107 }
108
109 /* restore the value */
110 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
111 *zone_ptr = '%';
112 }
113
114cleanup:
115 free(addr_dyn);
116 return ret;
117}
118
122static LY_ERR
123lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
124 uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
125 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
126 struct ly_err_item **err)
127{
128 LY_ERR ret = LY_SUCCESS;
129 struct lyd_value_ipv6_address *val;
130 const char *value_str = value;
131 uint32_t value_size, i;
132
133 /* init storage */
134 memset(storage, 0, sizeof *storage);
135 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
136 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
137 storage->realtype = type;
138
139 value_size = LYPLG_BITS2BYTES(value_size_bits);
140
141 if (format == LY_VALUE_LYB) {
142 /* validation */
143 if (value_size_bits < 128) {
144 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %" PRIu64
145 " b (expected at least 128 b).", value_size_bits);
146 goto cleanup;
147 }
148 for (i = 16; i < value_size; ++i) {
149 if (!isalnum(value_str[i])) {
150 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
151 value_str[i]);
152 goto cleanup;
153 }
154 }
155
156 /* store IP address */
157 memcpy(&val->addr, value, sizeof val->addr);
158
159 /* store zone, if any */
160 if (value_size > 16) {
161 ret = lydict_insert(ctx, value_str + 16, value_size - 16, &val->zone);
162 LY_CHECK_GOTO(ret, cleanup);
163 } else {
164 val->zone = NULL;
165 }
166
167 /* success */
168 goto cleanup;
169 }
170
171 /* check hints */
172 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
173 LY_CHECK_GOTO(ret, cleanup);
174
175 /* get the network-byte order address */
176 ret = ipv6address_str2ip(value, value_size, options, ctx, val, err);
177 LY_CHECK_GOTO(ret, cleanup);
178
179 if (format == LY_VALUE_CANON) {
180 /* store canonical value */
181 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
182 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
183 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
184 LY_CHECK_GOTO(ret, cleanup);
185 } else {
186 ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
187 LY_CHECK_GOTO(ret, cleanup);
188 }
189 }
190
191cleanup:
192 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
193 free((void *)value);
194 }
195
196 if (ret) {
197 lyplg_type_free_ipv6_address(ctx, storage);
198 }
199 return ret;
200}
201
205static LY_ERR
206lyplg_type_compare_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
207 const struct lyd_value *val2)
208{
209 struct lyd_value_ipv6_address *v1, *v2;
210
211 LYD_VALUE_GET(val1, v1);
212 LYD_VALUE_GET(val2, v2);
213
214 /* zones are NULL or in the dictionary */
215 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
216 return LY_ENOT;
217 }
218 return LY_SUCCESS;
219}
220
224static int
225lyplg_type_sort_ipv6_address(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
226 const struct lyd_value *val2)
227{
228 struct lyd_value_ipv6_address *v1, *v2;
229 int cmp;
230
231 LYD_VALUE_GET(val1, v1);
232 LYD_VALUE_GET(val2, v2);
233
234 cmp = memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
235 if (cmp != 0) {
236 return cmp;
237 }
238
239 if (!v1->zone && v2->zone) {
240 return -1;
241 } else if (v1->zone && !v2->zone) {
242 return 1;
243 } else if (v1->zone && v2->zone) {
244 return strcmp(v1->zone, v2->zone);
245 }
246
247 return 0;
248}
249
253static const void *
254lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
255 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
256{
257 struct lyd_value_ipv6_address *val;
258 uint32_t zone_len;
259 char *ret;
260
261 LYD_VALUE_GET(value, val);
262
263 if (format == LY_VALUE_LYB) {
264 if (!val->zone) {
265 /* address-only, const */
266 *dynamic = 0;
267 if (value_size_bits) {
268 *value_size_bits = sizeof val->addr * 8;
269 }
270 return &val->addr;
271 }
272
273 /* dynamic */
274 zone_len = strlen(val->zone);
275 ret = malloc(sizeof val->addr + zone_len);
276 LY_CHECK_RET(!ret, NULL);
277
278 memcpy(ret, &val->addr, sizeof val->addr);
279 memcpy(ret + sizeof val->addr, val->zone, zone_len);
280
281 *dynamic = 1;
282 if (value_size_bits) {
283 *value_size_bits = sizeof val->addr * 8 + zone_len * 8;
284 }
285 return ret;
286 }
287
288 /* generate canonical value if not already */
289 if (!value->_canonical) {
290 /* '%' + zone */
291 zone_len = val->zone ? strlen(val->zone) + 1 : 0;
292 ret = malloc(INET6_ADDRSTRLEN + zone_len);
293 LY_CHECK_RET(!ret, NULL);
294
295 /* get the address in string */
296 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
297 free(ret);
298 LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
299 return NULL;
300 }
301
302 /* add zone */
303 if (zone_len) {
304 sprintf(ret + strlen(ret), "%%%s", val->zone);
305 }
306
307 /* store it */
308 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
309 LOGMEM(ctx);
310 return NULL;
311 }
312 }
313
314 /* use the cached canonical value */
315 if (dynamic) {
316 *dynamic = 0;
317 }
318 if (value_size_bits) {
319 *value_size_bits = strlen(value->_canonical) * 8;
320 }
321 return value->_canonical;
322}
323
327static LY_ERR
328lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
329{
330 LY_ERR ret;
331 struct lyd_value_ipv6_address *orig_val, *dup_val;
332
333 memset(dup, 0, sizeof *dup);
334
335 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
336 LY_CHECK_GOTO(ret, error);
337
338 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
339 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
340
341 LYD_VALUE_GET(original, orig_val);
342 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
343 ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
344 LY_CHECK_GOTO(ret, error);
345
346 dup->realtype = original->realtype;
347 return LY_SUCCESS;
348
349error:
350 lyplg_type_free_ipv6_address(ctx, dup);
351 return ret;
352}
353
357static void
358lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
359{
360 struct lyd_value_ipv6_address *val;
361
362 lydict_remove(ctx, value->_canonical);
363 value->_canonical = NULL;
364 LYD_VALUE_GET(value, val);
365 if (val) {
366 lydict_remove(ctx, val->zone);
368 }
369}
370
379 {
380 .module = "ietf-inet-types",
381 .revision = NULL,
382 .name = "ipv6-address",
383
384 .plugin.id = "ly2 ipv6-address",
385 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
386 .plugin.store = lyplg_type_store_ipv6_address,
387 .plugin.validate_value = NULL,
388 .plugin.validate_tree = NULL,
389 .plugin.compare = lyplg_type_compare_ipv6_address,
390 .plugin.sort = lyplg_type_sort_ipv6_address,
391 .plugin.print = lyplg_type_print_ipv6_address,
392 .plugin.duplicate = lyplg_type_dup_ipv6_address,
393 .plugin.free = lyplg_type_free_ipv6_address,
394 },
395 {
396 .module = "ietf-inet-types",
397 .revision = NULL,
398 .name = "ipv6-address-link-local",
399
400 .plugin.id = "ly2 ipv6-address",
401 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
402 .plugin.store = lyplg_type_store_ipv6_address,
403 .plugin.validate_value = NULL,
404 .plugin.validate_tree = NULL,
405 .plugin.compare = lyplg_type_compare_ipv6_address,
406 .plugin.sort = lyplg_type_sort_ipv6_address,
407 .plugin.print = lyplg_type_print_ipv6_address,
408 .plugin.duplicate = lyplg_type_dup_ipv6_address,
409 .plugin.free = lyplg_type_free_ipv6_address,
410 },
411 {0}
412};
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.
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 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.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#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[]
Plugin information for ipv6-address and ipv6-address-link-local 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
struct in6_addr addr
Definition tree_data.h:668
#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 values.
Definition tree_data.h:667
#define LOGMEM(CTX)
Definition tree_edit.h:22