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.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 <ctype.h>
30#include <errno.h>
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "libyang.h"
36
37#include "common.h"
38#include "compat.h"
39
50static void lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value);
51
64static LY_ERR
65ipv6address_str2ip(const char *value, size_t value_len, uint32_t options, const struct ly_ctx *ctx,
66 struct in6_addr *addr, const char **zone, struct ly_err_item **err)
67{
68 LY_ERR ret = LY_SUCCESS;
69 const char *addr_no_zone;
70 char *zone_ptr = NULL, *addr_dyn = NULL;
71 size_t zone_len;
72
73 /* store zone and get the string IPv6 address without it */
74 if ((zone_ptr = ly_strnchr(value, '%', value_len))) {
75 /* there is a zone index */
76 zone_len = value_len - (zone_ptr - value) - 1;
77 ret = lydict_insert(ctx, zone_ptr + 1, zone_len, zone);
78 LY_CHECK_GOTO(ret, cleanup);
79
80 /* get the IP without it */
81 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
82 *zone_ptr = '\0';
83 addr_no_zone = value;
84 } else {
85 addr_dyn = strndup(value, zone_ptr - value);
86 addr_no_zone = addr_dyn;
87 }
88 } else {
89 /* no zone */
90 *zone = NULL;
91
92 /* get the IP terminated with zero */
93 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
94 /* we can use the value directly */
95 addr_no_zone = value;
96 } else {
97 addr_dyn = strndup(value, value_len);
98 addr_no_zone = addr_dyn;
99 }
100 }
101
102 /* store the IPv6 address in network-byte order */
103 if (!inet_pton(AF_INET6, addr_no_zone, addr)) {
104 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv6 address \"%s\".", addr_no_zone);
105 goto cleanup;
106 }
107
108 /* restore the value */
109 if ((options & LYPLG_TYPE_STORE_DYNAMIC) && zone_ptr) {
110 *zone_ptr = '%';
111 }
112
113cleanup:
114 free(addr_dyn);
115 return ret;
116}
117
121static LY_ERR
122lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
123 uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
124 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
125 struct ly_err_item **err)
126{
127 LY_ERR ret = LY_SUCCESS;
128 const char *value_str = value;
129 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
130 struct lyd_value_ipv6_address *val;
131 size_t i;
132
133 /* init storage */
134 memset(storage, 0, sizeof *storage);
135 LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
136 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
137 storage->realtype = type;
138
139 if (format == LY_VALUE_LYB) {
140 /* validation */
141 if (value_len < 16) {
142 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address value size %zu "
143 "(expected at least 16).", value_len);
144 goto cleanup;
145 }
146 for (i = 16; i < value_len; ++i) {
147 if (!isalnum(value_str[i])) {
148 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv6-address zone character 0x%x.",
149 value_str[i]);
150 goto cleanup;
151 }
152 }
153
154 /* store IP address */
155 memcpy(&val->addr, value, sizeof val->addr);
156
157 /* store zone, if any */
158 if (value_len > 16) {
159 ret = lydict_insert(ctx, value_str + 16, value_len - 16, &val->zone);
160 LY_CHECK_GOTO(ret, cleanup);
161 } else {
162 val->zone = NULL;
163 }
164
165 /* success */
166 goto cleanup;
167 }
168
169 /* check hints */
170 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
171 LY_CHECK_GOTO(ret, cleanup);
172
173 /* length restriction of the string */
174 if (type_str->length) {
175 /* value_len is in bytes, but we need number of characters here */
176 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
177 LY_CHECK_GOTO(ret, cleanup);
178 }
179
180 /* pattern restrictions */
181 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
182 LY_CHECK_GOTO(ret, cleanup);
183
184 /* get the network-byte order address */
185 ret = ipv6address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
186 LY_CHECK_GOTO(ret, cleanup);
187
188 if (format == LY_VALUE_CANON) {
189 /* store canonical value */
190 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
191 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
192 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
193 LY_CHECK_GOTO(ret, cleanup);
194 } else {
195 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
196 LY_CHECK_GOTO(ret, cleanup);
197 }
198 }
199
200cleanup:
201 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
202 free((void *)value);
203 }
204
205 if (ret) {
206 lyplg_type_free_ipv6_address(ctx, storage);
207 }
208 return ret;
209}
210
214static LY_ERR
215lyplg_type_compare_ipv6_address(const struct lyd_value *val1, const struct lyd_value *val2)
216{
217 struct lyd_value_ipv6_address *v1, *v2;
218
219 LYD_VALUE_GET(val1, v1);
220 LYD_VALUE_GET(val2, v2);
221
222 /* zones are NULL or in the dictionary */
223 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
224 return LY_ENOT;
225 }
226 return LY_SUCCESS;
227}
228
232static const void *
233lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
234 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
235{
236 struct lyd_value_ipv6_address *val;
237 size_t zone_len;
238 char *ret;
239
240 LYD_VALUE_GET(value, val);
241
242 if (format == LY_VALUE_LYB) {
243 if (!val->zone) {
244 /* address-only, const */
245 *dynamic = 0;
246 if (value_len) {
247 *value_len = sizeof val->addr;
248 }
249 return &val->addr;
250 }
251
252 /* dynamic */
253 zone_len = strlen(val->zone);
254 ret = malloc(sizeof val->addr + zone_len);
255 LY_CHECK_RET(!ret, NULL);
256
257 memcpy(ret, &val->addr, sizeof val->addr);
258 memcpy(ret + sizeof val->addr, val->zone, zone_len);
259
260 *dynamic = 1;
261 if (value_len) {
262 *value_len = sizeof val->addr + zone_len;
263 }
264 return ret;
265 }
266
267 /* generate canonical value if not already */
268 if (!value->_canonical) {
269 /* '%' + zone */
270 zone_len = val->zone ? strlen(val->zone) + 1 : 0;
271 ret = malloc(INET6_ADDRSTRLEN + zone_len);
272 LY_CHECK_RET(!ret, NULL);
273
274 /* get the address in string */
275 if (!inet_ntop(AF_INET6, &val->addr, ret, INET6_ADDRSTRLEN)) {
276 free(ret);
277 LOGERR(ctx, LY_EVALID, "Failed to get IPv6 address in string (%s).", strerror(errno));
278 return NULL;
279 }
280
281 /* add zone */
282 if (zone_len) {
283 sprintf(ret + strlen(ret), "%%%s", val->zone);
284 }
285
286 /* store it */
287 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
288 LOGMEM(ctx);
289 return NULL;
290 }
291 }
292
293 /* use the cached canonical value */
294 if (dynamic) {
295 *dynamic = 0;
296 }
297 if (value_len) {
298 *value_len = strlen(value->_canonical);
299 }
300 return value->_canonical;
301}
302
306static LY_ERR
307lyplg_type_dup_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
308{
309 LY_ERR ret;
310 struct lyd_value_ipv6_address *orig_val, *dup_val;
311
312 memset(dup, 0, sizeof *dup);
313
314 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
315 LY_CHECK_GOTO(ret, error);
316
317 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
318 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
319
320 LYD_VALUE_GET(original, orig_val);
321 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
322 ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
323 LY_CHECK_GOTO(ret, error);
324
325 dup->realtype = original->realtype;
326 return LY_SUCCESS;
327
328error:
329 lyplg_type_free_ipv6_address(ctx, dup);
330 return ret;
331}
332
336static void
337lyplg_type_free_ipv6_address(const struct ly_ctx *ctx, struct lyd_value *value)
338{
339 struct lyd_value_ipv6_address *val;
340
341 lydict_remove(ctx, value->_canonical);
342 value->_canonical = NULL;
343 LYD_VALUE_GET(value, val);
344 if (val) {
345 lydict_remove(ctx, val->zone);
347 }
348}
349
358 {
359 .module = "ietf-inet-types",
360 .revision = "2013-07-15",
361 .name = "ipv6-address",
362
363 .plugin.id = "libyang 2 - ipv6-address, version 1",
364 .plugin.store = lyplg_type_store_ipv6_address,
365 .plugin.validate = NULL,
366 .plugin.compare = lyplg_type_compare_ipv6_address,
367 .plugin.sort = NULL,
368 .plugin.print = lyplg_type_print_ipv6_address,
369 .plugin.duplicate = lyplg_type_dup_ipv6_address,
370 .plugin.free = lyplg_type_free_ipv6_address,
371 .plugin.lyb_data_len = -1,
372 },
373 {0}
374};
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.
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[]
Plugin information for ipv6-address 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
struct in6_addr addr
Definition tree_data.h:681
#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 values.
Definition tree_data.h:680
#define LOGMEM(CTX)
Definition tree_edit.h:22