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>
39 #include "ly_common.h"
50 static void lyplg_type_free_ipv6_address(
const struct ly_ctx *ctx,
struct lyd_value *value);
64 ipv6address_str2ip(
const char *value, uint32_t value_len, uint32_t options,
const struct ly_ctx *ctx,
68 const char *addr_no_zone;
69 char *zone_ptr = NULL, *addr_dyn = NULL;
73 if ((zone_ptr = ly_strnchr(value,
'%', value_len))) {
75 zone_len = value_len - (zone_ptr - value) - 1;
78 (
int)value_len, value);
82 LY_CHECK_GOTO(ret, cleanup);
89 addr_dyn = strndup(value, zone_ptr - value);
90 addr_no_zone = addr_dyn;
101 addr_dyn = strndup(value, value_len);
102 addr_no_zone = addr_dyn;
107 if (!inet_pton(AF_INET6, addr_no_zone, &val->
addr)) {
126 lyplg_type_store_ipv6_address(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
127 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
128 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
133 const char *value_str = value;
134 uint32_t value_size, i;
137 memset(storage, 0,
sizeof *storage);
146 if (value_size_bits < 128) {
148 " b (expected at least 128 b).", value_size_bits);
151 for (i = 16; i < value_size; ++i) {
152 if (!isalnum(value_str[i])) {
160 memcpy(&val->
addr, value,
sizeof val->
addr);
163 if (value_size > 16) {
165 LY_CHECK_GOTO(ret, cleanup);
176 LY_CHECK_GOTO(ret, cleanup);
179 ret = ipv6address_str2ip(value, value_size, options, ctx, val, err);
180 LY_CHECK_GOTO(ret, cleanup);
187 LY_CHECK_GOTO(ret, cleanup);
190 LY_CHECK_GOTO(ret, cleanup);
200 lyplg_type_free_ipv6_address(ctx, storage);
209 lyplg_type_compare_ipv6_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
228 lyplg_type_sort_ipv6_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
258 void *UNUSED(prefix_data),
ly_bool *dynamic, uint64_t *value_size_bits)
261 uint32_t zone_len, ret_size, ret_len;
270 if (value_size_bits) {
271 *value_size_bits =
sizeof val->
addr * 8;
277 zone_len = strlen(val->
zone);
278 ret = malloc(
sizeof val->
addr + zone_len);
279 LY_CHECK_RET(!ret, NULL);
281 memcpy(ret, &val->
addr,
sizeof val->
addr);
282 memcpy(ret +
sizeof val->
addr, val->
zone, zone_len);
285 if (value_size_bits) {
286 *value_size_bits =
sizeof val->
addr * 8 + zone_len * 8;
294 zone_len = val->
zone ? strlen(val->
zone) + 1 : 0;
295 ret_size = INET6_ADDRSTRLEN + zone_len;
296 ret = malloc(ret_size);
297 LY_CHECK_RET(!ret, NULL);
300 if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
302 LOGERR(ctx,
LY_ESYS,
"Failed to get IPv6 address in string (%s).", strerror(errno));
308 ret_len = strlen(ret);
309 snprintf(ret + ret_len, ret_size - ret_len,
"%%%s", val->
zone);
323 if (value_size_bits) {
324 *value_size_bits = strlen(value->
_canonical) * 8;
338 memset(dup, 0,
sizeof *dup);
341 LY_CHECK_GOTO(ret, error);
344 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
347 memcpy(&dup_val->
addr, &orig_val->
addr,
sizeof orig_val->
addr);
349 LY_CHECK_GOTO(ret, error);
355 lyplg_type_free_ipv6_address(ctx, dup);
385 .
module =
"ietf-inet-types",
387 .name =
"ipv6-address",
389 .plugin.id =
"ly2 ipv6-address",
391 .plugin.store = lyplg_type_store_ipv6_address,
392 .plugin.validate_value = NULL,
393 .plugin.validate_tree = NULL,
394 .plugin.compare = lyplg_type_compare_ipv6_address,
395 .plugin.sort = lyplg_type_sort_ipv6_address,
396 .plugin.print = lyplg_type_print_ipv6_address,
397 .plugin.duplicate = lyplg_type_dup_ipv6_address,
398 .plugin.free = lyplg_type_free_ipv6_address,
401 .module =
"ietf-inet-types",
403 .name =
"ipv6-address-link-local",
405 .plugin.id =
"ly2 ipv6-address",
407 .plugin.store = lyplg_type_store_ipv6_address,
408 .plugin.validate_value = NULL,
409 .plugin.validate_tree = NULL,
410 .plugin.compare = lyplg_type_compare_ipv6_address,
411 .plugin.sort = lyplg_type_sort_ipv6_address,
412 .plugin.print = lyplg_type_print_ipv6_address,
413 .plugin.duplicate = lyplg_type_dup_ipv6_address,
414 .plugin.free = lyplg_type_free_ipv6_address,
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_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 an error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
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
const struct lyplg_type_record plugins_ipv6_address[]
Plugin information for ipv6-address and ipv6-address-link-local type implementation.
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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.