libyang 3.1.0
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_internal.h"
18#include "plugins_types.h"
19
20#ifdef _WIN32
21# include <winsock2.h>
22# include <ws2tcpip.h>
23#else
24# include <arpa/inet.h>
25# if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
26# include <netinet/in.h>
27# include <sys/socket.h>
28# endif
29#endif
30#include <assert.h>
31#include <errno.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "libyang.h"
37
38#include "compat.h"
39#include "ly_common.h"
40
50static void lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value);
51
62static LY_ERR
63ipv6addressnozone_str2ip(const char *value, size_t value_len, uint32_t options, struct in6_addr *addr, struct ly_err_item **err)
64{
65 LY_ERR ret = LY_SUCCESS;
66 const char *addr_str;
67 char *addr_dyn = NULL;
68
69 /* get the IP terminated with zero */
70 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
71 /* we can use the value directly */
72 addr_str = value;
73 } else {
74 addr_dyn = strndup(value, value_len);
75 addr_str = addr_dyn;
76 }
77
78 /* store the IPv6 address in network-byte order */
79 if (!inet_pton(AF_INET6, addr_str, addr)) {
80 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_str);
81 goto cleanup;
82 }
83
84cleanup:
85 free(addr_dyn);
86 return ret;
87}
88
92static LY_ERR
93lyplg_type_store_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
94 size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
95 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
96 struct ly_err_item **err)
97{
98 LY_ERR ret = LY_SUCCESS;
99 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
101
102 /* init storage */
103 memset(storage, 0, sizeof *storage);
104 storage->realtype = type;
105
106 if (format == LY_VALUE_LYB) {
107 /* validation */
108 if (value_len != 16) {
109 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address-no-zone value size %zu "
110 "(expected 16).", value_len);
111 goto cleanup;
112 }
113
114 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
115 /* use the value directly */
116 storage->dyn_mem = (void *)value;
117 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
118 } else {
119 /* allocate value */
120 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
121 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
122
123 /* store IP address */
124 memcpy(&val->addr, value, sizeof val->addr);
125 }
126
127 /* success */
128 goto cleanup;
129 }
130
131 /* allocate value */
132 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
133 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
134
135 /* check hints */
136 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
137 LY_CHECK_GOTO(ret, cleanup);
138
139 /* length restriction of the string */
140 if (type_str->length) {
141 /* value_len is in bytes, but we need number of characters here */
142 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
143 LY_CHECK_GOTO(ret, cleanup);
144 }
145
146 /* pattern restrictions */
147 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
148 LY_CHECK_GOTO(ret, cleanup);
149
150 /* get the network-byte order address */
151 ret = ipv6addressnozone_str2ip(value, value_len, options, &val->addr, err);
152 LY_CHECK_GOTO(ret, cleanup);
153
154 if (format == LY_VALUE_CANON) {
155 /* store canonical value */
156 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
157 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
158 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
159 LY_CHECK_GOTO(ret, cleanup);
160 } else {
161 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
162 LY_CHECK_GOTO(ret, cleanup);
163 }
164 }
165
166cleanup:
167 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
168 free((void *)value);
169 }
170
171 if (ret) {
172 lyplg_type_free_ipv6_address_no_zone(ctx, storage);
173 }
174 return ret;
175}
176
180static LY_ERR
181lyplg_type_compare_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
182 const struct lyd_value *val2)
183{
184 struct lyd_value_ipv6_address_no_zone *v1, *v2;
185
186 LYD_VALUE_GET(val1, v1);
187 LYD_VALUE_GET(val2, v2);
188
189 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
190 return LY_ENOT;
191 }
192 return LY_SUCCESS;
193}
194
198static int
199lyplg_type_sort_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
200 const struct lyd_value *val2)
201{
202 struct lyd_value_ipv6_address_no_zone *v1, *v2;
203
204 LYD_VALUE_GET(val1, v1);
205 LYD_VALUE_GET(val2, v2);
206
207 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
208}
209
213static const void *
214lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
215 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
216{
218 char *ret;
219
220 LYD_VALUE_GET(value, val);
221
222 if (format == LY_VALUE_LYB) {
223 *dynamic = 0;
224 if (value_len) {
225 *value_len = sizeof val->addr;
226 }
227 return &val->addr;
228 }
229
230 /* generate canonical value if not already */
231 if (!value->_canonical) {
232 /* '%' + zone */
233 ret = malloc(INET6_ADDRSTRLEN);
234 LY_CHECK_RET(!ret, NULL);
235
236 /* get the address in string */
237 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
238 free(ret);
239 LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
240 return NULL;
241 }
242
243 /* store it */
244 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
245 LOGMEM(ctx);
246 return NULL;
247 }
248 }
249
250 /* use the cached canonical value */
251 if (dynamic) {
252 *dynamic = 0;
253 }
254 if (value_len) {
255 *value_len = strlen(value->_canonical);
256 }
257 return value->_canonical;
258}
259
263static LY_ERR
264lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
265{
266 LY_ERR ret;
267 struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
268
269 memset(dup, 0, sizeof *dup);
270
271 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
272 LY_CHECK_GOTO(ret, error);
273
274 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
275 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
276
277 LYD_VALUE_GET(original, orig_val);
278 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
279
280 dup->realtype = original->realtype;
281 return LY_SUCCESS;
282
283error:
284 lyplg_type_free_ipv6_address_no_zone(ctx, dup);
285 return ret;
286}
287
291static void
292lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
293{
295
296 lydict_remove(ctx, value->_canonical);
297 value->_canonical = NULL;
298 LYD_VALUE_GET(value, val);
300}
301
310 {
311 .module = "ietf-inet-types",
312 .revision = "2013-07-15",
313 .name = "ipv6-address-no-zone",
314
315 .plugin.id = "libyang 2 - ipv6-address-no-zone, version 1",
316 .plugin.store = lyplg_type_store_ipv6_address_no_zone,
317 .plugin.validate = NULL,
318 .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
319 .plugin.sort = lyplg_type_sort_ipv6_address_no_zone,
320 .plugin.print = lyplg_type_print_ipv6_address_no_zone,
321 .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
322 .plugin.free = lyplg_type_free_ipv6_address_no_zone,
323 .plugin.lyb_data_len = 16,
324 },
325 {0}
326};
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:237
@ LYVE_DATA
Definition log.h:274
@ LY_ESYS
Definition log.h:240
@ LY_EMEM
Definition log.h:239
@ LY_ENOT
Definition log.h:251
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
const char *const char * revision
#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.
#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.
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.
#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:575
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:614
const char * _canonical
Definition tree_data.h:572
YANG data representation.
Definition tree_data.h:571
Special lyd_value structure for ietf-inet-types ipv6-address-no-zone values.
Definition tree_data.h:684
#define LOGMEM(CTX)
Definition tree_edit.h:22