23# include <arpa/inet.h>
24# if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
25# include <netinet/in.h>
26# include <sys/socket.h>
50static void lyplg_type_free_ipv6_address(
const struct ly_ctx *ctx,
struct lyd_value *value);
65ipv6address_str2ip(
const char *value,
size_t value_len, uint32_t options,
const struct ly_ctx *ctx,
66 struct in6_addr *addr,
const char **zone,
struct ly_err_item **err)
69 const char *addr_no_zone;
70 char *zone_ptr = NULL, *addr_dyn = NULL;
74 if ((zone_ptr = ly_strnchr(value,
'%', value_len))) {
76 zone_len = value_len - (zone_ptr - value) - 1;
78 LY_CHECK_GOTO(ret, cleanup);
85 addr_dyn = strndup(value, zone_ptr - value);
86 addr_no_zone = addr_dyn;
97 addr_dyn = strndup(value, value_len);
98 addr_no_zone = addr_dyn;
103 if (!inet_pton(AF_INET6, addr_no_zone, addr)) {
122lyplg_type_store_ipv6_address(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
123 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
124 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
128 const char *value_str = value;
134 memset(storage, 0,
sizeof *storage);
136 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
141 if (value_len < 16) {
143 "(expected at least 16).", value_len);
146 for (i = 16; i < value_len; ++i) {
147 if (!isalnum(value_str[i])) {
155 memcpy(&val->
addr, value,
sizeof val->
addr);
158 if (value_len > 16) {
160 LY_CHECK_GOTO(ret, cleanup);
171 LY_CHECK_GOTO(ret, cleanup);
177 LY_CHECK_GOTO(ret, cleanup);
182 LY_CHECK_GOTO(ret, cleanup);
185 ret = ipv6address_str2ip(value, value_len, options, ctx, &val->
addr, &val->
zone, err);
186 LY_CHECK_GOTO(ret, cleanup);
192 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
193 LY_CHECK_GOTO(ret, cleanup);
196 LY_CHECK_GOTO(ret, cleanup);
206 lyplg_type_free_ipv6_address(ctx, storage);
215lyplg_type_compare_ipv6_address(
const struct lyd_value *val1,
const struct lyd_value *val2)
234 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
247 *value_len =
sizeof val->
addr;
253 zone_len = strlen(val->
zone);
254 ret = malloc(
sizeof val->
addr + zone_len);
255 LY_CHECK_RET(!ret, NULL);
257 memcpy(ret, &val->
addr,
sizeof val->
addr);
258 memcpy(ret +
sizeof val->
addr, val->
zone, zone_len);
262 *value_len =
sizeof val->
addr + zone_len;
270 zone_len = val->
zone ? strlen(val->
zone) + 1 : 0;
271 ret = malloc(INET6_ADDRSTRLEN + zone_len);
272 LY_CHECK_RET(!ret, NULL);
275 if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
277 LOGERR(ctx,
LY_EVALID,
"Failed to get IPv6 address in string (%s).", strerror(errno));
283 sprintf(ret + strlen(ret),
"%%%s", val->
zone);
312 memset(dup, 0,
sizeof *dup);
315 LY_CHECK_GOTO(ret, error);
318 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
321 memcpy(&dup_val->
addr, &orig_val->
addr,
sizeof orig_val->
addr);
323 LY_CHECK_GOTO(ret, error);
329 lyplg_type_free_ipv6_address(ctx, dup);
337lyplg_type_free_ipv6_address(
const struct ly_ctx *ctx,
struct lyd_value *value)
359 .module =
"ietf-inet-types",
360 .revision =
"2013-07-15",
361 .name =
"ipv6-address",
363 .plugin.id =
"libyang 2 - ipv6-address, version 1",
364 .plugin.store = lyplg_type_store_ipv6_address,
365 .plugin.validate = NULL,
366 .plugin.compare = lyplg_type_compare_ipv6_address,
368 .plugin.print = lyplg_type_print_ipv6_address,
369 .plugin.duplicate = lyplg_type_dup_ipv6_address,
370 .plugin.free = lyplg_type_free_ipv6_address,
371 .plugin.lyb_data_len = -1,
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.
Libyang full error structure.
#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_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_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
struct lysc_pattern ** patterns
struct lysc_range * length
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
const struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address type implementation.
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
API for (user) types plugins.
const struct lysc_type * realtype
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
YANG data representation.
Special lyd_value structure for ietf-inet-types ipv6-address values.