17 #include "plugins_internal.h"
21 # include <winsock2.h>
22 # include <ws2tcpip.h>
24 # include <arpa/inet.h>
25 # if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
26 # include <netinet/in.h>
27 # include <sys/socket.h>
40 #include "ly_common.h"
52 static void lyplg_type_free_ipv6_address(
const struct ly_ctx *ctx,
struct lyd_value *value);
67 ipv6address_str2ip(
const char *value,
size_t value_len, uint32_t options,
const struct ly_ctx *ctx,
68 struct in6_addr *addr,
const char **zone,
struct ly_err_item **err)
71 const char *addr_no_zone;
72 char *zone_ptr = NULL, *addr_dyn = NULL;
76 if ((zone_ptr = ly_strnchr(value,
'%', value_len))) {
78 zone_len = value_len - (zone_ptr - value) - 1;
80 LY_CHECK_GOTO(ret, cleanup);
87 addr_dyn = strndup(value, zone_ptr - value);
88 addr_no_zone = addr_dyn;
99 addr_dyn = strndup(value, value_len);
100 addr_no_zone = addr_dyn;
105 if (!inet_pton(AF_INET6, addr_no_zone, addr)) {
124 lyplg_type_store_ipv6_address(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
125 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
126 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
130 const char *value_str = value;
136 memset(storage, 0,
sizeof *storage);
138 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
143 if (value_len < 16) {
145 "(expected at least 16).", value_len);
148 for (i = 16; i < value_len; ++i) {
149 if (!isalnum(value_str[i])) {
157 memcpy(&val->
addr, value,
sizeof val->
addr);
160 if (value_len > 16) {
162 LY_CHECK_GOTO(ret, cleanup);
173 LY_CHECK_GOTO(ret, cleanup);
180 LY_CHECK_GOTO(ret, cleanup);
185 LY_CHECK_GOTO(ret, cleanup);
189 ret = ipv6address_str2ip(value, value_len, options, ctx, &val->
addr, &val->
zone, err);
190 LY_CHECK_GOTO(ret, cleanup);
196 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
197 LY_CHECK_GOTO(ret, cleanup);
200 LY_CHECK_GOTO(ret, cleanup);
205 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
210 lyplg_type_free_ipv6_address(ctx, storage);
219 lyplg_type_compare_ipv6_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
238 lyplg_type_sort_ipv6_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
268 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
281 *value_len =
sizeof val->
addr;
287 zone_len = strlen(val->
zone);
288 ret = malloc(
sizeof val->
addr + zone_len);
289 LY_CHECK_RET(!ret, NULL);
291 memcpy(ret, &val->
addr,
sizeof val->
addr);
292 memcpy(ret +
sizeof val->
addr, val->
zone, zone_len);
296 *value_len =
sizeof val->
addr + zone_len;
304 zone_len = val->
zone ? strlen(val->
zone) + 1 : 0;
305 ret = malloc(INET6_ADDRSTRLEN + zone_len);
306 LY_CHECK_RET(!ret, NULL);
309 if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
311 LOGERR(ctx,
LY_ESYS,
"Failed to get IPv6 address in string (%s).", strerror(errno));
317 sprintf(ret + strlen(ret),
"%%%s", val->
zone);
346 memset(dup, 0,
sizeof *dup);
349 LY_CHECK_GOTO(ret, error);
352 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
355 memcpy(&dup_val->
addr, &orig_val->
addr,
sizeof orig_val->
addr);
357 LY_CHECK_GOTO(ret, error);
363 lyplg_type_free_ipv6_address(ctx, dup);
371 lyplg_type_free_ipv6_address(
const struct ly_ctx *ctx,
struct lyd_value *value)
393 .
module =
"ietf-inet-types",
394 .revision =
"2013-07-15",
395 .name =
"ipv6-address",
397 .plugin.id =
"libyang 2 - ipv6-address, version 1",
398 .plugin.store = lyplg_type_store_ipv6_address,
399 .plugin.validate = NULL,
400 .plugin.compare = lyplg_type_compare_ipv6_address,
401 .plugin.sort = lyplg_type_sort_ipv6_address,
402 .plugin.print = lyplg_type_print_ipv6_address,
403 .plugin.duplicate = lyplg_type_dup_ipv6_address,
404 .plugin.free = lyplg_type_free_ipv6_address,
405 .plugin.lyb_data_len = -1,
struct lysc_type * realtype
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
uint8_t ly_bool
Type to indicate boolean value.
#define LYPLG_TYPE_STORE_DYNAMIC
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
struct lysc_pattern ** patterns
Special lyd_value structure for ietf-inet-types ipv6-address values.
struct lysc_range * length
YANG data representation.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
Libyang full error structure.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
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.
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(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 lyplg_type_check_hints(uint32_t hints, const char *value, size_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.
struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address type implementation.
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.
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
API for (user) types plugins.
LY_ERR
libyang's error codes returned by the libyang functions.
#define LYPLG_TYPE_STORE_ONLY
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.