Loading...
Searching...
No Matches
ipv4_address_no_zone.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strndup */
#include "plugins_internal.h"
#include "plugins_types.h"
#ifdef _WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
#else
# include <arpa/inet.h>
# if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
# include <netinet/in.h>
# include <sys/socket.h>
# endif
#endif
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libyang.h"
#include "compat.h"
#include "ly_common.h"
static LY_ERR
lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_str *type_str = (struct lysc_type_str *)type;
struct lyd_value_ipv4_address_no_zone *val;
/* init storage */
memset(storage, 0, sizeof *storage);
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
storage->realtype = type;
if (format == LY_VALUE_LYB) {
/* validation */
if (value_len != 4) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
"(expected 4).", value_len);
goto cleanup;
}
/* store IP address */
memcpy(&val->addr, value, 4);
/* success */
goto cleanup;
}
/* check hints */
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
LY_CHECK_GOTO(ret, cleanup);
/* length restriction of the string */
if (type_str->length) {
/* value_len is in bytes, but we need number of characters here */
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
}
/* pattern restrictions */
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
/* we always need a dynamic value */
if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
value = strndup(value, value_len);
LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
options |= LYPLG_TYPE_STORE_DYNAMIC;
}
/* get the network-byte order address */
if (!inet_pton(AF_INET, value, &val->addr)) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
goto cleanup;
}
/* store canonical value */
ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
LY_CHECK_GOTO(ret, cleanup);
cleanup:
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
free((void *)value);
}
if (ret) {
lyplg_type_free_simple(ctx, storage);
}
return ret;
}
static LY_ERR
lyplg_type_compare_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
const struct lyd_value *val2)
{
struct lyd_value_ipv4_address_no_zone *v1, *v2;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
return LY_ENOT;
}
return LY_SUCCESS;
}
static int
lyplg_type_sort_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
const struct lyd_value *val2)
{
struct lyd_value_ipv4_address_no_zone *v1, *v2;
LYD_VALUE_GET(val1, v1);
LYD_VALUE_GET(val2, v2);
return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
}
static const void *
lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
{
struct lyd_value_ipv4_address_no_zone *val;
char *ret;
LYD_VALUE_GET(value, val);
if (format == LY_VALUE_LYB) {
*dynamic = 0;
if (value_len) {
*value_len = 4;
}
return &val->addr;
}
/* generate canonical value if not already (loaded from LYB) */
if (!value->_canonical) {
ret = malloc(INET_ADDRSTRLEN);
LY_CHECK_RET(!ret, NULL);
/* get the address in string */
if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
free(ret);
LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
return NULL;
}
/* store it */
if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
LOGMEM(ctx);
return NULL;
}
}
/* use the cached canonical value */
if (dynamic) {
*dynamic = 0;
}
if (value_len) {
*value_len = strlen(value->_canonical);
}
return value->_canonical;
}
const struct lyplg_type_record plugins_ipv4_address_no_zone[] = {
{
.module = "ietf-inet-types",
.revision = "2013-07-15",
.name = "ipv4-address-no-zone",
.plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
.plugin.store = lyplg_type_store_ipv4_address_no_zone,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
.plugin.sort = lyplg_type_sort_ipv4_address_no_zone,
.plugin.print = lyplg_type_print_ipv4_address_no_zone,
.plugin.duplicate = lyplg_type_dup_simple,
.plugin.free = lyplg_type_free_simple,
.plugin.lyb_data_len = 4,
},
{0}
};
ly_ctx
libyang context handler.
lydict_insert_zc
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,...
LY_ERR
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_ESYS
@ LY_ESYS
Definition log.h:240
LY_EMEM
@ LY_EMEM
Definition log.h:239
LY_ENOT
@ LY_ENOT
Definition log.h:251
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
ly_err_item
Libyang full error structure.
Definition log.h:282
lyplg_type_record::revision
const char *const char * revision
Definition plugins_types.h:638
LYPLG_TYPE_VAL_INLINE_PREPARE
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
Definition plugins_types.h:194
lyplg_type_validate_patterns
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.
lyplg_type_validate_range
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.
lyplg_type_check_hints
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.
ly_err_new
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.
lyplg_type_record
Definition plugins_types.h:635
lyplg_type_dup_simple
LIBYANG_API_DECL LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
lyplg_type_free_simple
LIBYANG_API_DECL void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_type_str::patterns
struct lysc_pattern ** patterns
Definition tree_schema.h:1321
lysc_type_str::length
struct lysc_range * length
Definition tree_schema.h:1320
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_str
Definition tree_schema.h:1313
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
LY_TYPE_STRING
@ LY_TYPE_STRING
Definition tree.h:209
LY_VALUE_LYB
@ LY_VALUE_LYB
Definition tree.h:240
plugins_ipv4_address_no_zone
const struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
Definition ipv4_address_no_zone.c:218
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value_ipv4_address_no_zone::addr
struct in_addr addr
Definition tree_data.h:658
LYD_VALUE_GET
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:610
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_value
YANG data representation.
Definition tree_data.h:567
lyd_value_ipv4_address_no_zone
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition tree_data.h:657
LOGMEM
#define LOGMEM(CTX)
Definition tree_edit.h:22