libyang 2.1.148
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv4_address_no_zone.c
Go to the documentation of this file.
1
15#define _GNU_SOURCE /* strndup */
16
17#include "plugins_types.h"
18
19#ifdef _WIN32
20# include <winsock2.h>
21# include <ws2tcpip.h>
22#else
23# include <arpa/inet.h>
24# if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
25# include <netinet/in.h>
26# include <sys/socket.h>
27# endif
28#endif
29#include <errno.h>
30#include <stdint.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "libyang.h"
35
36#include "common.h"
37#include "compat.h"
38
51static LY_ERR
52lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
53 size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
54 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
55 struct ly_err_item **err)
56{
57 LY_ERR ret = LY_SUCCESS;
58 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
60
61 /* init storage */
62 memset(storage, 0, sizeof *storage);
64 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
65 storage->realtype = type;
66
67 if (format == LY_VALUE_LYB) {
68 /* validation */
69 if (value_len != 4) {
70 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
71 "(expected 4).", value_len);
72 goto cleanup;
73 }
74
75 /* store IP address */
76 memcpy(&val->addr, value, 4);
77
78 /* success */
79 goto cleanup;
80 }
81
82 /* check hints */
83 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
84 LY_CHECK_GOTO(ret, cleanup);
85
86 /* length restriction of the string */
87 if (type_str->length) {
88 /* value_len is in bytes, but we need number of characters here */
89 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
90 LY_CHECK_GOTO(ret, cleanup);
91 }
92
93 /* pattern restrictions */
94 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
95 LY_CHECK_GOTO(ret, cleanup);
96
97 /* we always need a dynamic value */
98 if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
99 value = strndup(value, value_len);
100 LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
101
102 options |= LYPLG_TYPE_STORE_DYNAMIC;
103 }
104
105 /* get the network-byte order address */
106 if (!inet_pton(AF_INET, value, &val->addr)) {
107 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
108 goto cleanup;
109 }
110
111 /* store canonical value */
112 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
113 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
114 LY_CHECK_GOTO(ret, cleanup);
115
116cleanup:
117 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
118 free((void *)value);
119 }
120
121 if (ret) {
122 lyplg_type_free_simple(ctx, storage);
123 }
124 return ret;
125}
126
130static LY_ERR
131lyplg_type_compare_ipv4_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
132{
133 struct lyd_value_ipv4_address_no_zone *v1, *v2;
134
135 LYD_VALUE_GET(val1, v1);
136 LYD_VALUE_GET(val2, v2);
137
138 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
139 return LY_ENOT;
140 }
141 return LY_SUCCESS;
142}
143
147static const void *
148lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
149 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
150{
152 char *ret;
153
154 LYD_VALUE_GET(value, val);
155
156 if (format == LY_VALUE_LYB) {
157 *dynamic = 0;
158 if (value_len) {
159 *value_len = 4;
160 }
161 return &val->addr;
162 }
163
164 /* generate canonical value if not already (loaded from LYB) */
165 if (!value->_canonical) {
166 ret = malloc(INET_ADDRSTRLEN);
167 LY_CHECK_RET(!ret, NULL);
168
169 /* get the address in string */
170 if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
171 free(ret);
172 LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
173 return NULL;
174 }
175
176 /* store it */
177 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
178 LOGMEM(ctx);
179 return NULL;
180 }
181 }
182
183 /* use the cached canonical value */
184 if (dynamic) {
185 *dynamic = 0;
186 }
187 if (value_len) {
188 *value_len = strlen(value->_canonical);
189 }
190 return value->_canonical;
191}
192
201 {
202 .module = "ietf-inet-types",
203 .revision = "2013-07-15",
204 .name = "ipv4-address-no-zone",
205
206 .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
207 .plugin.store = lyplg_type_store_ipv4_address_no_zone,
208 .plugin.validate = NULL,
209 .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
210 .plugin.sort = NULL,
211 .plugin.print = lyplg_type_print_ipv4_address_no_zone,
212 .plugin.duplicate = lyplg_type_dup_simple,
213 .plugin.free = lyplg_type_free_simple,
214 .plugin.lyb_data_len = 4,
215 },
216 {0}
217};
libyang context handler.
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
libyang's error codes returned by the libyang functions.
Definition log.h:248
@ LYVE_DATA
Definition log.h:285
@ LY_EMEM
Definition log.h:250
@ LY_ENOT
Definition log.h:262
@ LY_EVALID
Definition log.h:256
@ LY_SUCCESS
Definition log.h:249
Libyang full error structure.
Definition log.h:293
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
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_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.
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.
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 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.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
struct lysc_pattern ** patterns
struct lysc_range * length
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_TYPE_STRING
Definition tree.h:209
@ LY_VALUE_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:564
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:603
const char * _canonical
Definition tree_data.h:561
YANG data representation.
Definition tree_data.h:560
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition tree_data.h:650
#define LOGMEM(CTX)
Definition tree_edit.h:22