20 # include <winsock2.h>
21 # include <ws2tcpip.h>
23 # include <arpa/inet.h>
24 # if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
25 # include <netinet/in.h>
26 # include <sys/socket.h>
48 #define LYB_VALUE_LEN 17
50 static void lyplg_type_free_ipv6_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value);
63 ipv6prefix_str2ip(
const char *value,
size_t value_len,
struct in6_addr *addr, uint8_t *prefix,
struct ly_err_item **err)
67 char *mask_str = NULL;
70 pref_str = ly_strnchr(value,
'/', value_len);
71 ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
74 mask_str = strndup(value, pref_str - value);
75 LY_CHECK_ERR_GOTO(!mask_str, ret =
LY_EMEM, cleanup);
78 if (inet_pton(AF_INET6, mask_str, addr) != 1) {
95 ipv6prefix_zero_host(
struct in6_addr *addr, uint8_t prefix)
100 for (i = 0; i < 4; ++i) {
102 for (j = 0; j < 32; ++j) {
104 if (prefix > (i * 32) + j) {
109 ((uint32_t *)addr->s6_addr)[i] &= mask;
117 lyplg_type_store_ipv6_prefix(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
118 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
119 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
127 memset(storage, 0,
sizeof *storage);
137 if (((uint8_t *)value)[16] > 128) {
139 ((uint8_t *)value)[16]);
145 storage->dyn_mem = (
void *)value;
151 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
153 memcpy(val, value, value_len);
157 ipv6prefix_zero_host(&val->
addr, val->
prefix);
165 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
169 LY_CHECK_GOTO(ret, cleanup);
175 LY_CHECK_GOTO(ret, cleanup);
180 LY_CHECK_GOTO(ret, cleanup);
183 ret = ipv6prefix_str2ip(value, value_len, &val->
addr, &val->
prefix, err);
184 LY_CHECK_GOTO(ret, cleanup);
187 ipv6prefix_zero_host(&val->
addr, val->
prefix);
194 LY_CHECK_GOTO(ret, cleanup);
197 LY_CHECK_GOTO(ret, cleanup);
207 lyplg_type_free_ipv6_prefix(ctx, storage);
216 lyplg_type_compare_ipv6_prefix(
const struct lyd_value *val1,
const struct lyd_value *val2)
227 if (memcmp(v1, v2,
sizeof *v1)) {
238 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
256 ret = malloc(INET6_ADDRSTRLEN + 4);
257 LY_CHECK_RET(!ret, NULL);
260 if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
266 sprintf(ret + strlen(ret),
"/%" PRIu8, val->
prefix);
294 memset(dup, 0,
sizeof *dup);
297 LY_CHECK_GOTO(ret, error);
300 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
303 memcpy(dup_val, orig_val,
sizeof *orig_val);
309 lyplg_type_free_ipv6_prefix(ctx, dup);
336 .
module =
"ietf-inet-types",
337 .revision =
"2013-07-15",
338 .name =
"ipv6-prefix",
340 .plugin.id =
"libyang 2 - ipv6-prefix, version 1",
341 .plugin.store = lyplg_type_store_ipv6_prefix,
342 .plugin.validate = NULL,
343 .plugin.compare = lyplg_type_compare_ipv6_prefix,
345 .plugin.print = lyplg_type_print_ipv6_prefix,
346 .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
347 .plugin.free = lyplg_type_free_ipv6_prefix,