16 #include <sys/cdefs.h>
20 #include <arpa/inet.h>
21 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
22 #include <netinet/in.h>
23 #include <sys/socket.h>
46 static void lyplg_type_free_ipv6_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value);
59 ipv6prefix_str2ip(
const char *value,
size_t value_len,
struct in6_addr *addr, uint8_t *prefix,
struct ly_err_item **err)
63 char *mask_str = NULL;
66 pref_str = ly_strnchr(value,
'/', value_len);
67 ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
70 mask_str = strndup(value, pref_str - value);
71 LY_CHECK_ERR_GOTO(!mask_str, ret =
LY_EMEM, cleanup);
74 if (inet_pton(AF_INET6, mask_str, addr) != 1) {
91 ipv6prefix_zero_host(
struct in6_addr *addr, uint8_t prefix)
96 for (i = 0; i < 4; ++i) {
98 for (j = 0; j < 32; ++j) {
100 if (prefix > (i * 32) + j) {
105 ((uint32_t *)addr->s6_addr)[i] &= mask;
113 lyplg_type_store_ipv6_prefix(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
114 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
115 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
123 memset(storage, 0,
sizeof *storage);
128 if (value_len != 17) {
133 if (((uint8_t *)value)[16] > 128) {
135 ((uint8_t *)value)[16]);
141 storage->dyn_mem = (
void *)value;
147 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
149 memcpy(val, value, value_len);
153 ipv6prefix_zero_host(&val->
addr, val->
prefix);
161 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
165 LY_CHECK_GOTO(ret, cleanup);
171 LY_CHECK_GOTO(ret, cleanup);
176 LY_CHECK_GOTO(ret, cleanup);
179 ret = ipv6prefix_str2ip(value, value_len, &val->
addr, &val->
prefix, err);
180 LY_CHECK_GOTO(ret, cleanup);
183 ipv6prefix_zero_host(&val->
addr, val->
prefix);
190 LY_CHECK_GOTO(ret, cleanup);
193 LY_CHECK_GOTO(ret, cleanup);
203 lyplg_type_free_ipv6_prefix(ctx, storage);
212 lyplg_type_compare_ipv6_prefix(
const struct lyd_value *val1,
const struct lyd_value *val2)
223 if (memcmp(v1, v2,
sizeof *v1)) {
234 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
244 *value_len =
sizeof *val;
252 ret = malloc(INET6_ADDRSTRLEN + 4);
253 LY_CHECK_RET(!ret, NULL);
256 if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
262 sprintf(ret + strlen(ret),
"/%" PRIu8, val->
prefix);
300 memcpy(dup_val, orig_val,
sizeof *orig_val);
328 .
module =
"ietf-inet-types",
329 .revision =
"2013-07-15",
330 .name =
"ipv6-prefix",
332 .plugin.id =
"libyang 2 - ipv6-prefix, version 1",
333 .plugin.store = lyplg_type_store_ipv6_prefix,
334 .plugin.validate = NULL,
335 .plugin.compare = lyplg_type_compare_ipv6_prefix,
337 .plugin.print = lyplg_type_print_ipv6_prefix,
338 .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
339 .plugin.free = lyplg_type_free_ipv6_prefix
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 ...
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 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...
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.
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.
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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.
#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_prefix[]
Plugin information for ipv6-prefix 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-prefix values.