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>
39 #include "ly_common.h"
51 static void lyplg_type_free_ipv4_address(
const struct ly_ctx *ctx,
struct lyd_value *value);
66 ipv4address_str2ip(
const char *value, uint32_t value_len, uint32_t options,
const struct ly_ctx *ctx,
67 struct in_addr *addr,
const char **zone,
struct ly_err_item **err)
70 const char *addr_no_zone;
71 char *zone_ptr = NULL, *addr_dyn = NULL;
75 if ((zone_ptr = ly_strnchr(value,
'%', value_len))) {
77 zone_len = value_len - (zone_ptr - value) - 1;
79 LY_CHECK_GOTO(ret, cleanup);
86 addr_dyn = strndup(value, zone_ptr - value);
87 addr_no_zone = addr_dyn;
98 addr_dyn = strndup(value, value_len);
99 addr_no_zone = addr_dyn;
104 if (!inet_pton(AF_INET, addr_no_zone, addr)) {
123 lyplg_type_store_ipv4_address(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
124 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
130 const char *value_str = value;
131 uint32_t value_size, i;
134 memset(storage, 0,
sizeof *storage);
136 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
143 if (value_size_bits < 32) {
145 " b (expected at least 32 b).", value_size_bits);
148 for (i = 4; i < value_size; ++i) {
149 if (!isalnum(value_str[i])) {
157 memcpy(&val->
addr, value,
sizeof val->
addr);
160 if (value_size > 4) {
162 LY_CHECK_GOTO(ret, cleanup);
173 LY_CHECK_GOTO(ret, cleanup);
176 ret = ipv4address_str2ip(value, value_size, options, ctx, &val->
addr, &val->
zone, err);
177 LY_CHECK_GOTO(ret, cleanup);
182 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
183 LY_CHECK_GOTO(ret, cleanup);
186 LY_CHECK_GOTO(ret, cleanup);
190 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
195 lyplg_type_free_ipv4_address(ctx, storage);
204 lyplg_type_compare_ipv4_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
223 lyplg_type_sort_ipv4_address(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
253 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
265 if (value_size_bits) {
266 *value_size_bits =
sizeof val->
addr * 8;
272 zone_len = strlen(val->
zone);
273 ret = malloc(
sizeof val->
addr + zone_len);
274 LY_CHECK_RET(!ret, NULL);
276 memcpy(ret, &val->
addr,
sizeof val->
addr);
277 memcpy(ret +
sizeof val->
addr, val->
zone, zone_len);
280 if (value_size_bits) {
281 *value_size_bits =
sizeof val->
addr * 8 + zone_len * 8;
289 zone_len = val->
zone ? strlen(val->
zone) + 1 : 0;
290 ret = malloc(INET_ADDRSTRLEN + zone_len);
291 LY_CHECK_RET(!ret, NULL);
294 if (!inet_ntop(AF_INET, &val->
addr, ret, INET_ADDRSTRLEN)) {
296 LOGERR(ctx,
LY_ESYS,
"Failed to get IPv4 address in string (%s).", strerror(errno));
302 sprintf(ret + strlen(ret),
"%%%s", val->
zone);
316 if (value_size_bits) {
317 *value_size_bits = strlen(value->
_canonical) * 8;
331 memset(dup, 0,
sizeof *dup);
334 LY_CHECK_GOTO(ret, error);
337 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
341 memcpy(&dup_val->
addr, &orig_val->
addr,
sizeof orig_val->
addr);
343 LY_CHECK_GOTO(ret, error);
349 lyplg_type_free_ipv4_address(ctx, dup);
357 lyplg_type_free_ipv4_address(
const struct ly_ctx *ctx,
struct lyd_value *value)
379 .
module =
"ietf-inet-types",
380 .revision =
"2013-07-15",
381 .name =
"ipv4-address",
383 .plugin.id =
"ly2 ipv4-address",
385 .plugin.store = lyplg_type_store_ipv4_address,
386 .plugin.validate_value = NULL,
387 .plugin.validate_tree = NULL,
388 .plugin.compare = lyplg_type_compare_ipv4_address,
389 .plugin.sort = lyplg_type_sort_ipv4_address,
390 .plugin.print = lyplg_type_print_ipv4_address,
391 .plugin.duplicate = lyplg_type_dup_ipv4_address,
392 .plugin.free = lyplg_type_free_ipv4_address,
struct lysc_type * realtype
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
YANG extension compiled instance.
LY_ERR
libyang's error codes returned by the libyang functions.
uint8_t ly_bool
Type to indicate boolean value.
struct lyplg_type_record plugins_ipv4_address[]
Plugin information for ipv4-address type implementation.
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address values.
LYPLG_TYPE_VAL_INLINE_DESTROY(val)
The main libyang public header.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
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...
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.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.