libyang 3.4.2
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;
100
101 /* init storage */
102 memset(storage, 0, sizeof *storage);
103 storage->realtype = type;
104
105 if (format == LY_VALUE_LYB) {
106 /* validation */
107 if (value_len != 16) {
108 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address-no-zone value size %zu "
109 "(expected 16).", value_len);
110 goto cleanup;
111 }
112
113 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && LYPLG_TYPE_VAL_IS_DYN(val)) {
114 /* use the value directly */
115 storage->dyn_mem = (void *)value;
116 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
117 } else {
118 /* allocate value */
119 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
120 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
121
122 /* store IP address */
123 memcpy(&val->addr, value, sizeof val->addr);
124 }
125
126 /* success */
127 goto cleanup;
128 }
129
130 /* allocate value */
131 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
132 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
133
134 /* check hints */
135 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
136 LY_CHECK_GOTO(ret, cleanup);
137
138 /* get the network-byte order address, validates the value */
139 ret = ipv6addressnozone_str2ip(value, value_len, options, &val->addr, err);
140 LY_CHECK_GOTO(ret, cleanup);
141
142 if (format == LY_VALUE_CANON) {
143 /* store canonical value */
144 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
145 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
146 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
147 LY_CHECK_GOTO(ret, cleanup);
148 } else {
149 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
150 LY_CHECK_GOTO(ret, cleanup);
151 }
152 }
153
154cleanup:
155 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
156 free((void *)value);
157 }
158
159 if (ret) {
160 lyplg_type_free_ipv6_address_no_zone(ctx, storage);
161 }
162 return ret;
163}
164
168static LY_ERR
169lyplg_type_compare_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
170 const struct lyd_value *val2)
171{
172 struct lyd_value_ipv6_address_no_zone *v1, *v2;
173
174 LYD_VALUE_GET(val1, v1);
175 LYD_VALUE_GET(val2, v2);
176
177 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
178 return LY_ENOT;
179 }
180 return LY_SUCCESS;
181}
182
186static int
187lyplg_type_sort_ipv6_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
188 const struct lyd_value *val2)
189{
190 struct lyd_value_ipv6_address_no_zone *v1, *v2;
191
192 LYD_VALUE_GET(val1, v1);
193 LYD_VALUE_GET(val2, v2);
194
195 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
196}
197
201static const void *
202lyplg_type_print_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
203 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
204{
206 char *ret;
207
208 LYD_VALUE_GET(value, val);
209
210 if (format == LY_VALUE_LYB) {
211 *dynamic = 0;
212 if (value_len) {
213 *value_len = sizeof val->addr;
214 }
215 return &val->addr;
216 }
217
218 /* generate canonical value if not already */
219 if (!value->_canonical) {
220 /* '%' + zone */
221 ret = malloc(INET6_ADDRSTRLEN);
222 LY_CHECK_RET(!ret, NULL);
223
224 /* get the address in string */
225 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
226 free(ret);
227 LOGERR(ctx, LY_ESYS, "Failed to get IPv6 address in string (%s).", strerror(errno));
228 return NULL;
229 }
230
231 /* store it */
232 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
233 LOGMEM(ctx);
234 return NULL;
235 }
236 }
237
238 /* use the cached canonical value */
239 if (dynamic) {
240 *dynamic = 0;
241 }
242 if (value_len) {
243 *value_len = strlen(value->_canonical);
244 }
245 return value->_canonical;
246}
247
251static LY_ERR
252lyplg_type_dup_ipv6_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
253{
254 LY_ERR ret;
255 struct lyd_value_ipv6_address_no_zone *orig_val, *dup_val;
256
257 memset(dup, 0, sizeof *dup);
258
259 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
260 LY_CHECK_GOTO(ret, error);
261
262 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
263 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
264
265 LYD_VALUE_GET(original, orig_val);
266 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
267
268 dup->realtype = original->realtype;
269 return LY_SUCCESS;
270
271error:
272 lyplg_type_free_ipv6_address_no_zone(ctx, dup);
273 return ret;
274}
275
279static void
280lyplg_type_free_ipv6_address_no_zone(const struct ly_ctx *ctx, struct lyd_value *value)
281{
283
284 lydict_remove(ctx, value->_canonical);
285 value->_canonical = NULL;
286 LYD_VALUE_GET(value, val);
288}
289
298 {
299 .module = "ietf-inet-types",
300 .revision = "2013-07-15",
301 .name = "ipv6-address-no-zone",
302
303 .plugin.id = "libyang 2 - ipv6-address-no-zone, version 1",
304 .plugin.store = lyplg_type_store_ipv6_address_no_zone,
305 .plugin.validate = NULL,
306 .plugin.compare = lyplg_type_compare_ipv6_address_no_zone,
307 .plugin.sort = lyplg_type_sort_ipv6_address_no_zone,
308 .plugin.print = lyplg_type_print_ipv6_address_no_zone,
309 .plugin.duplicate = lyplg_type_dup_ipv6_address_no_zone,
310 .plugin.free = lyplg_type_free_ipv6_address_no_zone,
311 .plugin.lyb_data_len = 16,
312 },
313 {0}
314};
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.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
#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
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ 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