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 5
50 static void lyplg_type_free_ipv4_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value);
63 ipv4prefix_str2ip(
const char *value,
size_t value_len,
struct in_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_INET, mask_str, addr) != 1) {
95 ipv4prefix_zero_host(
struct in_addr *addr, uint8_t prefix)
101 for (i = 0; i < 32; ++i) {
108 addr->s_addr &= mask;
115 lyplg_type_store_ipv4_prefix(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
116 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
117 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
125 memset(storage, 0,
sizeof *storage);
127 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
137 if (((uint8_t *)value)[4] > 32) {
139 ((uint8_t *)value)[4]);
144 memcpy(val, value, value_len);
147 ipv4prefix_zero_host(&val->
addr, val->
prefix);
155 LY_CHECK_GOTO(ret, cleanup);
161 LY_CHECK_GOTO(ret, cleanup);
166 LY_CHECK_GOTO(ret, cleanup);
169 ret = ipv4prefix_str2ip(value, value_len, &val->
addr, &val->
prefix, err);
170 LY_CHECK_GOTO(ret, cleanup);
173 ipv4prefix_zero_host(&val->
addr, val->
prefix);
179 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
180 LY_CHECK_GOTO(ret, cleanup);
183 LY_CHECK_GOTO(ret, cleanup);
188 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
193 lyplg_type_free_ipv4_prefix(ctx, storage);
202 lyplg_type_compare_ipv4_prefix(
const struct lyd_value *val1,
const struct lyd_value *val2)
213 if (memcmp(v1, v2,
sizeof *v1)) {
224 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
242 ret = malloc(INET_ADDRSTRLEN + 3);
243 LY_CHECK_RET(!ret, NULL);
246 if (!inet_ntop(AF_INET, &val->
addr, ret, INET_ADDRSTRLEN)) {
252 sprintf(ret + strlen(ret),
"/%" PRIu8, val->
prefix);
280 memset(dup, 0,
sizeof *dup);
283 LY_CHECK_GOTO(ret, error);
286 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
289 memcpy(dup_val, orig_val,
sizeof *orig_val);
295 lyplg_type_free_ipv4_prefix(ctx, dup);
303 lyplg_type_free_ipv4_prefix(
const struct ly_ctx *ctx,
struct lyd_value *value)
322 .
module =
"ietf-inet-types",
323 .revision =
"2013-07-15",
324 .name =
"ipv4-prefix",
326 .plugin.id =
"libyang 2 - ipv4-prefix, version 1",
327 .plugin.store = lyplg_type_store_ipv4_prefix,
328 .plugin.validate = NULL,
329 .plugin.compare = lyplg_type_compare_ipv4_prefix,
331 .plugin.print = lyplg_type_print_ipv4_prefix,
332 .plugin.duplicate = lyplg_type_dup_ipv4_prefix,
333 .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.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
uint8_t ly_bool
Type to indicate boolean value.
#define LYPLG_TYPE_STORE_DYNAMIC
The main libyang public header.
struct lysc_pattern ** patterns
struct lysc_range * length
YANG data representation.
LIBYANG_API_DECL 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.
Libyang full error structure.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
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 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.
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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.
LY_ERR
libyang's error codes returned by the libyang functions.
API for (user) types plugins.
LIBYANG_API_DECL 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.