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_IPV4PREF_LYB_VALUE_SIZE 40
52 static void lyplg_type_free_ipv4_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value);
56 uint32_t *fixed_size_bits)
73 ipv4prefix_str2ip(
const char *value, uint32_t value_len,
struct in_addr *addr, uint8_t *prefix,
struct ly_err_item **err)
77 char *mask_str = NULL;
80 pref_str = ly_strnchr(value,
'/', value_len);
81 ly_strntou8(pref_str + 1, value_len - (pref_str + 1 - value), prefix);
84 mask_str = strndup(value, pref_str - value);
85 LY_CHECK_ERR_GOTO(!mask_str, ret =
LY_EMEM, cleanup);
88 if (inet_pton(AF_INET, mask_str, addr) != 1) {
105 ipv4prefix_zero_host(
struct in_addr *addr, uint8_t prefix)
111 for (i = 0; i < 32; ++i) {
118 addr->s_addr &= mask;
125 lyplg_type_store_ipv4_prefix(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
126 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
135 memset(storage, 0,
sizeof *storage);
137 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
143 LY_CHECK_GOTO(ret, cleanup);
147 if (((uint8_t *)value)[4] > 32) {
149 ((uint8_t *)value)[4]);
154 memcpy(val, value, value_size);
157 ipv4prefix_zero_host(&val->
addr, val->
prefix);
165 LY_CHECK_GOTO(ret, cleanup);
168 ret = ipv4prefix_str2ip(value, value_size, &val->
addr, &val->
prefix, err);
169 LY_CHECK_GOTO(ret, cleanup);
172 ipv4prefix_zero_host(&val->
addr, val->
prefix);
178 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
179 LY_CHECK_GOTO(ret, cleanup);
182 LY_CHECK_GOTO(ret, cleanup);
187 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
192 lyplg_type_free_ipv4_prefix(ctx, storage);
201 lyplg_type_compare_ipv4_prefix(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
209 if (
memcmp(v1, v2,
sizeof *v1)) {
219 lyplg_type_sort_ipv4_prefix(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
227 return memcmp(v1, v2,
sizeof *v1);
235 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
244 if (value_size_bits) {
253 ret = malloc(INET_ADDRSTRLEN + 3);
254 LY_CHECK_RET(!ret, NULL);
257 if (!inet_ntop(AF_INET, &val->
addr, ret, INET_ADDRSTRLEN)) {
263 sprintf(ret + strlen(ret),
"/%" PRIu8, val->
prefix);
276 if (value_size_bits) {
277 *value_size_bits = strlen(value->
_canonical) * 8;
291 memset(dup, 0,
sizeof *dup);
294 LY_CHECK_GOTO(ret, error);
297 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
300 memcpy(dup_val, orig_val,
sizeof *orig_val);
306 lyplg_type_free_ipv4_prefix(ctx, dup);
314 lyplg_type_free_ipv4_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value)
333 .
module =
"ietf-inet-types",
334 .revision =
"2013-07-15",
335 .name =
"ipv4-prefix",
337 .plugin.id =
"ly2 ipv4-prefix",
338 .plugin.lyb_size = lyplg_type_lyb_size_ipv4_prefix,
339 .plugin.store = lyplg_type_store_ipv4_prefix,
340 .plugin.validate_value = NULL,
341 .plugin.validate_tree = NULL,
342 .plugin.compare = lyplg_type_compare_ipv4_prefix,
343 .plugin.sort = lyplg_type_sort_ipv4_prefix,
344 .plugin.print = lyplg_type_print_ipv4_prefix,
345 .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
346 .plugin.free = lyplg_type_free_ipv4_prefix,
struct lysc_type * realtype
struct lyplg_type_record plugins_ipv4_prefix[]
Plugin information for ipv4-prefix type implementation.
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
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.
Special lyd_value structure for ietf-inet-types ipv4-prefix values.
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...
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)
#define LYPLG_IPV4PREF_LYB_VALUE_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.