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> 
   38 #include "ly_common.h" 
   50 #define LYPLG_IPV6PREF_LYB_VALUE_SIZE 136 
   52 static void lyplg_type_free_ipv6_prefix(
const struct ly_ctx *ctx, 
struct lyd_value *value);
 
   65 ipv6prefix_str2ip(
const char *value, uint32_t value_len, 
struct in6_addr *addr, uint8_t *prefix, 
struct ly_err_item **err)
 
   69     char *mask_str = NULL;
 
   72     pref_str = ly_strnchr(value, 
'/', value_len);
 
   73     ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
 
   76     mask_str = strndup(value, pref_str - value);
 
   77     LY_CHECK_ERR_GOTO(!mask_str, ret = 
LY_EMEM, cleanup);
 
   80     if (inet_pton(AF_INET6, mask_str, addr) != 1) {
 
   97 ipv6prefix_zero_host(
struct in6_addr *addr, uint8_t prefix)
 
  102     for (i = 0; i < 4; ++i) {
 
  104         for (j = 0; j < 32; ++j) {
 
  106             if (prefix > (i * 32) + j) {
 
  111         ((uint32_t *)addr->s6_addr)[i] &= mask;
 
  117         uint32_t *fixed_size_bits)
 
  127 lyplg_type_store_ipv6_prefix(
const struct ly_ctx *ctx, 
const struct lysc_type *type, 
const void *value, uint32_t value_size_bits,
 
  128         uint32_t options, 
LY_VALUE_FORMAT format, 
void *UNUSED(prefix_data), uint32_t hints,
 
  137     memset(storage, 0, 
sizeof *storage);
 
  143     LY_CHECK_GOTO(ret, cleanup);
 
  147         if (((uint8_t *)value)[16] > 128) {
 
  149                     ((uint8_t *)value)[16]);
 
  155             storage->dyn_mem = (
void *)value;
 
  156             options &= ~LYPLG_TYPE_STORE_DYNAMIC;
 
  161             LY_CHECK_ERR_GOTO(!val, ret = 
LY_EMEM, cleanup);
 
  163             memcpy(val, value, value_size);
 
  167         ipv6prefix_zero_host(&val->
addr, val->
prefix);
 
  175     LY_CHECK_ERR_GOTO(!val, ret = 
LY_EMEM, cleanup);
 
  179     LY_CHECK_GOTO(ret, cleanup);
 
  182     ret = ipv6prefix_str2ip(value, value_size, &val->
addr, &val->
prefix, err);
 
  183     LY_CHECK_GOTO(ret, cleanup);
 
  186     ipv6prefix_zero_host(&val->
addr, val->
prefix);
 
  190         if (options & LYPLG_TYPE_STORE_DYNAMIC) {
 
  192             options &= ~LYPLG_TYPE_STORE_DYNAMIC;
 
  193             LY_CHECK_GOTO(ret, cleanup);
 
  196             LY_CHECK_GOTO(ret, cleanup);
 
  201     if (options & LYPLG_TYPE_STORE_DYNAMIC) {
 
  206         lyplg_type_free_ipv6_prefix(ctx, storage);
 
  215 lyplg_type_compare_ipv6_prefix(
const struct ly_ctx *UNUSED(ctx), 
const struct lyd_value *val1,
 
  223     if (
memcmp(v1, v2, 
sizeof *v1)) {
 
  233 lyplg_type_sort_ipv6_prefix(
const struct ly_ctx *UNUSED(ctx), 
const struct lyd_value *val1,
 
  241     return memcmp(v1, v2, 
sizeof *v1);
 
  249         void *UNUSED(prefix_data), 
ly_bool *dynamic, uint32_t *value_size_bits)
 
  258         if (value_size_bits) {
 
  267         ret = malloc(INET6_ADDRSTRLEN + 4);
 
  268         LY_CHECK_RET(!ret, NULL);
 
  271         if (!inet_ntop(AF_INET6, &val->
addr, ret, INET6_ADDRSTRLEN)) {
 
  277         sprintf(ret + strlen(ret), 
"/%" PRIu8, val->
prefix);
 
  290     if (value_size_bits) {
 
  291         *value_size_bits = strlen(value->
_canonical) * 8;
 
  305     memset(dup, 0, 
sizeof *dup);
 
  308     LY_CHECK_GOTO(ret, error);
 
  311     LY_CHECK_ERR_GOTO(!dup_val, ret = 
LY_EMEM, error);
 
  314     memcpy(dup_val, orig_val, 
sizeof *orig_val);
 
  320     lyplg_type_free_ipv6_prefix(ctx, dup);
 
  328 lyplg_type_free_ipv6_prefix(
const struct ly_ctx *ctx, 
struct lyd_value *value)
 
  347         .
module = 
"ietf-inet-types",
 
  348         .revision = 
"2013-07-15",
 
  349         .name = 
"ipv6-prefix",
 
  351         .plugin.id = 
"ly2 ipv6-prefix",
 
  352         .plugin.lyb_size = lyplg_type_lyb_size_ipv6_prefix,
 
  353         .plugin.store = lyplg_type_store_ipv6_prefix,
 
  354         .plugin.validate_value = NULL,
 
  355         .plugin.validate_tree = NULL,
 
  356         .plugin.compare = lyplg_type_compare_ipv6_prefix,
 
  357         .plugin.sort = lyplg_type_sort_ipv6_prefix,
 
  358         .plugin.print = lyplg_type_print_ipv6_prefix,
 
  359         .plugin.duplicate = lyplg_type_dup_ipv6_prefix,
 
  360         .plugin.free = lyplg_type_free_ipv6_prefix,
 
struct lysc_type * realtype
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected. 
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type. 
YANG extension compiled instance. 
LY_ERR
libyang's error codes returned by the libyang functions. 
uint8_t ly_bool
Type to indicate boolean value. 
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_IPV6PREF_LYB_VALUE_SIZE
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header. 
YANG data representation. 
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...
struct lyplg_type_record plugins_ipv6_prefix[]
Plugin information for ipv6-prefix type implementation. 
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically. 
Special lyd_value structure for ietf-inet-types ipv6-prefix values. 
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. 
return memcmp(v1->data, v2->data, bitmap_size)
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. 
API for (user) types plugins.