libyang 2.1.148
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv6_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
48static void lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value);
49
60static LY_ERR
61ipv6addressnozone_str2ip(const char *value, size_t value_len, uint32_t options, struct in6_addr *addr, struct ly_err_item **err)
62{
63 LY_ERR ret = LY_SUCCESS;
64 const char *addr_str;
65 char *addr_dyn = NULL;
66
67 /* get the IP terminated with zero */
68 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
69 /* we can use the value directly */
70 addr_str = value;
71 } else {
72 addr_dyn = strndup(value, value_len);
73 addr_str = addr_dyn;
74 }
75
76 /* store the IPv6 address in network-byte order */
77 if (!inet_pton(AF_INET6, addr_str, addr)) {
78 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_str);
79 goto cleanup;
80 }
81
82cleanup:
83 free(addr_dyn);
84 return ret;
85}
86
90static LY_ERR
91lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
92 size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
93 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
94 struct ly_err_item **err)
95{
96 LY_ERR ret = LY_SUCCESS;
97 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
99
100 /* init storage */
101 memset(storage, 0, sizeof *storage);
102 storage->realtype = type;
103
104 if (format == LY_VALUE_LYB) {
105 /* validation */
106 if (value_len != 16) {
107 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address-no-zone value size %zu "
108 "(expected 16).", value_len);
109 goto cleanup;
110 }
111
112 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
113 /* use the value directly */
114 storage->dyn_mem = (void *)value;
115 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
116 } else {
117 /* allocate value */
118 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
119 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
120
121 /* store IP address */
122 memcpy(&val->addr, value, sizeof val->addr);
123 }
124
125 /* success */
126 goto cleanup;
127 }
128
129 /* allocate value */
130 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
131 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
132
133 /* check hints */
134 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
135 LY_CHECK_GOTO(ret, cleanup);
136
137 /* length restriction of the string */
138 if (type_str->length) {
139 /* value_len is in bytes, but we need number of characters here */
140 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
141 LY_CHECK_GOTO(ret, cleanup);
142 }
143
144 /* pattern restrictions */
145 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
146 LY_CHECK_GOTO(ret, cleanup);
147
148 /* get the network-byte order address */
149 ret = ipv6addressnozone_str2ip(value, value_len, options, &val->addr, err);
150 LY_CHECK_GOTO(ret, cleanup);
151
152 if (format == LY_VALUE_CANON) {
153 /* store canonical value */
154 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
155 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
156 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
157 LY_CHECK_GOTO(ret, cleanup);
158 } else {
159 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
160 LY_CHECK_GOTO(ret, cleanup);
161 }
162 }
163
164cleanup:
165 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
166 free((void *)value);
167 }
168
169 if (ret) {
170 lyplg_type_free_ipv6_address_no_zone(ctx, storage);
171 }
172 return ret;
173}
174
178static LY_ERR
179lyplg_type_compare_ipv6_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
180{
181 struct lyd_value_ipv6_address_no_zone *v1, *v2;
182
183 LYD_VALUE_GET(val1, v1);
184 LYD_VALUE_GET(val2, v2);
185
186 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
187 return LY_ENOT;
188 }
189 return LY_SUCCESS;
190}
191
195static const void *
196lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
197 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
198{
200 char *ret;
201
202 LYD_VALUE_GET(value, val);
203
204 if (format == LY_VALUE_LYB) {
205 *dynamic = 0;
206 if (value_len) {
207 *value_len = sizeof val->addr;
208 }
209 return &val->addr;
210 }
211
212 /* generate canonical value if not already */
213 if (!value->_canonical) {
214 /* '%' + zone */
215 ret = malloc(INET6_ADDRSTRLEN);
216 LY_CHECK_RET(!ret, NULL);
217
218 /* get the address in string */
219 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
220 free(ret);
221 LOGERR(ctx, LY_EVALID, "Failed to get IPv6 address in string (%s).", strerror(errno));
222 return NULL;
223 }
224
225 /* store it */
226 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
227 LOGMEM(ctx);
228 return NULL;
229 }
230 }
231
232 /* use the cached canonical value */
233 if (dynamic) {
234 *dynamic = 0;
235 }
236 if (value_len) {
237 *value_len = strlen(value->_canonical);
238 }
239 return value->_canonical;
240}
241
245static LY_ERR
246lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
247{
248 LY_ERR ret;
249 struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
250
251 memset(dup, 0, sizeof *dup);
252
253 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
254 LY_CHECK_GOTO(ret, error);
255
256 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
257 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
258
259 LYD_VALUE_GET(original, orig_val);
260 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
261
262 dup->realtype = original->realtype;
263 return LY_SUCCESS;
264
265error:
266 lyplg_type_free_ipv6_address_no_zone(ctx, dup);
267 return ret;
268}
269
273static void
274lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
275{
277
278 lydict_remove(ctx, value->_canonical);
279 value->_canonical = NULL;
280 LYD_VALUE_GET(value, val);
282}
283
292 {
293 .module = "ietf-inet-types",
294 .revision = "2013-07-15",
295 .name = "ipv6-address-no-zone",
296
297 .plugin.id = "libyang 2 - ipv6-address-no-zone, version 1",
298 .plugin.store = lyplg_type_store_ipv6_address_no_zone,
299 .plugin.validate = NULL,
300 .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
301 .plugin.sort = NULL,
302 .plugin.print = lyplg_type_print_ipv6_address_no_zone,
303 .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
304 .plugin.free = lyplg_type_free_ipv6_address_no_zone,
305 .plugin.lyb_data_len = 16,
306 },
307 {0}
308};
libyang context handler.
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_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_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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
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.
#define LYPLG_TYPE_VAL_IS_DYN(type_val)
Check whether specific type value needs to be allocated dynamically.
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.
#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_CANON
Definition tree.h:235
@ LY_VALUE_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv6_address_no_zone[]
Plugin information for ipv6-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 ipv6-address-no-zone values.
Definition tree_data.h:673
#define LOGMEM(CTX)
Definition tree_edit.h:22