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>
50 static void lyplg_type_free_ipv4_address(
const struct ly_ctx *ctx,
struct lyd_value *value);
65 ipv4address_str2ip(
const char *value,
size_t value_len, uint32_t options,
const struct ly_ctx *ctx,
66 struct in_addr *addr,
const char **zone,
struct ly_err_item **err)
69 const char *addr_no_zone;
70 char *zone_ptr = NULL, *addr_dyn = NULL;
74 if ((zone_ptr = ly_strnchr(value,
'%', value_len))) {
76 zone_len = value_len - (zone_ptr - value) - 1;
78 LY_CHECK_GOTO(ret, cleanup);
85 addr_dyn = strndup(value, zone_ptr - value);
86 addr_no_zone = addr_dyn;
97 addr_dyn = strndup(value, value_len);
98 addr_no_zone = addr_dyn;
103 if (!inet_pton(AF_INET, addr_no_zone, addr)) {
122 lyplg_type_store_ipv4_address(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value,
size_t value_len,
123 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
124 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
128 const char *value_str = value;
134 memset(storage, 0,
sizeof *storage);
136 LY_CHECK_ERR_GOTO(!val, ret =
LY_EMEM, cleanup);
143 "(expected at least 4).", value_len);
146 for (i = 4; i < value_len; ++i) {
147 if (!isalnum(value_str[i])) {
155 memcpy(&val->
addr, value,
sizeof val->
addr);
160 LY_CHECK_GOTO(ret, cleanup);
171 LY_CHECK_GOTO(ret, cleanup);
177 LY_CHECK_GOTO(ret, cleanup);
182 LY_CHECK_GOTO(ret, cleanup);
185 ret = ipv4address_str2ip(value, value_len, options, ctx, &val->
addr, &val->
zone, err);
186 LY_CHECK_GOTO(ret, cleanup);
191 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
192 LY_CHECK_GOTO(ret, cleanup);
195 LY_CHECK_GOTO(ret, cleanup);
199 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
204 lyplg_type_free_ipv4_address(ctx, storage);
213 lyplg_type_compare_ipv4_address(
const struct lyd_value *val1,
const struct lyd_value *val2)
236 void *UNUSED(prefix_data),
ly_bool *dynamic,
size_t *value_len)
249 *value_len =
sizeof val->
addr;
255 zone_len = strlen(val->
zone);
256 ret = malloc(
sizeof val->
addr + zone_len);
257 LY_CHECK_RET(!ret, NULL);
259 memcpy(ret, &val->
addr,
sizeof val->
addr);
260 memcpy(ret +
sizeof val->
addr, val->
zone, zone_len);
264 *value_len =
sizeof val->
addr + zone_len;
272 zone_len = val->
zone ? strlen(val->
zone) + 1 : 0;
273 ret = malloc(INET_ADDRSTRLEN + zone_len);
274 LY_CHECK_RET(!ret, NULL);
277 if (!inet_ntop(AF_INET, &val->
addr, ret, INET_ADDRSTRLEN)) {
279 LOGERR(ctx,
LY_EVALID,
"Failed to get IPv4 address in string (%s).", strerror(errno));
285 sprintf(ret + strlen(ret),
"%%%s", val->
zone);
314 memset(dup, 0,
sizeof *dup);
317 LY_CHECK_GOTO(ret, error);
320 LY_CHECK_ERR_GOTO(!dup_val, ret =
LY_EMEM, error);
324 memcpy(&dup_val->
addr, &orig_val->
addr,
sizeof orig_val->
addr);
326 LY_CHECK_GOTO(ret, error);
332 lyplg_type_free_ipv4_address(ctx, dup);
340 lyplg_type_free_ipv4_address(
const struct ly_ctx *ctx,
struct lyd_value *value)
362 .
module =
"ietf-inet-types",
363 .revision =
"2013-07-15",
364 .name =
"ipv4-address",
366 .plugin.id =
"libyang 2 - ipv4-address, version 1",
367 .plugin.store = lyplg_type_store_ipv4_address,
368 .plugin.validate = NULL,
369 .plugin.compare = lyplg_type_compare_ipv4_address,
371 .plugin.print = lyplg_type_print_ipv4_address,
372 .plugin.duplicate = lyplg_type_dup_ipv4_address,
373 .plugin.free = lyplg_type_free_ipv4_address,
374 .plugin.lyb_data_len = -1,
struct lysc_type * realtype
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.
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.
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.
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.