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.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_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value);
51
64static LY_ERR
65ipv4address_str2ip(const char *value, size_t value_len, uint32_t options, const struct ly_ctx *ctx,
66 struct in_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 IPv4 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 IPv4 address in network-byte order */
103 if (!inet_pton(AF_INET, addr_no_zone, addr)) {
104 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 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_ipv4_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_ipv4_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 < 4) {
142 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %zu "
143 "(expected at least 4).", value_len);
144 goto cleanup;
145 }
146 for (i = 4; i < value_len; ++i) {
147 if (!isalnum(value_str[i])) {
148 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-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 > 4) {
159 ret = lydict_insert(ctx, value_str + 4, value_len - 4, &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 = ipv4address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
186 LY_CHECK_GOTO(ret, cleanup);
187
188 /* store canonical value */
189 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
190 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
191 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
192 LY_CHECK_GOTO(ret, cleanup);
193 } else {
194 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
195 LY_CHECK_GOTO(ret, cleanup);
196 }
197
198cleanup:
199 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
200 free((void *)value);
201 }
202
203 if (ret) {
204 lyplg_type_free_ipv4_address(ctx, storage);
205 }
206 return ret;
207}
208
212static LY_ERR
213lyplg_type_compare_ipv4_address(const struct lyd_value *val1, const struct lyd_value *val2)
214{
215 struct lyd_value_ipv4_address *v1, *v2;
216
217 LYD_VALUE_GET(val1, v1);
218 LYD_VALUE_GET(val2, v2);
219
220 /* zones are NULL or in the dictionary */
221 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr) || (v1->zone != v2->zone)) {
222 return LY_ENOT;
223 }
224 return LY_SUCCESS;
225}
226
230static const void *
231lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
232 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
233{
234 struct lyd_value_ipv4_address *val;
235 size_t zone_len;
236 char *ret;
237
238 LYD_VALUE_GET(value, val);
239
240 if (format == LY_VALUE_LYB) {
241 if (!val->zone) {
242 /* address-only, const */
243 *dynamic = 0;
244 if (value_len) {
245 *value_len = sizeof val->addr;
246 }
247 return &val->addr;
248 }
249
250 /* dynamic */
251 zone_len = strlen(val->zone);
252 ret = malloc(sizeof val->addr + zone_len);
253 LY_CHECK_RET(!ret, NULL);
254
255 memcpy(ret, &val->addr, sizeof val->addr);
256 memcpy(ret + sizeof val->addr, val->zone, zone_len);
257
258 *dynamic = 1;
259 if (value_len) {
260 *value_len = sizeof val->addr + zone_len;
261 }
262 return ret;
263 }
264
265 /* generate canonical value if not already */
266 if (!value->_canonical) {
267 /* '%' + zone */
268 zone_len = val->zone ? strlen(val->zone) + 1 : 0;
269 ret = malloc(INET_ADDRSTRLEN + zone_len);
270 LY_CHECK_RET(!ret, NULL);
271
272 /* get the address in string */
273 if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
274 free(ret);
275 LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
276 return NULL;
277 }
278
279 /* add zone */
280 if (zone_len) {
281 sprintf(ret + strlen(ret), "%%%s", val->zone);
282 }
283
284 /* store it */
285 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
286 LOGMEM(ctx);
287 return NULL;
288 }
289 }
290
291 /* use the cached canonical value */
292 if (dynamic) {
293 *dynamic = 0;
294 }
295 if (value_len) {
296 *value_len = strlen(value->_canonical);
297 }
298 return value->_canonical;
299}
300
304static LY_ERR
305lyplg_type_dup_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
306{
307 LY_ERR ret;
308 struct lyd_value_ipv4_address *orig_val, *dup_val;
309
310 memset(dup, 0, sizeof *dup);
311
312 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
313 LY_CHECK_GOTO(ret, error);
314
315 LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
316 LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
317
318 LYD_VALUE_GET(original, orig_val);
319
320 memcpy(&dup_val->addr, &orig_val->addr, sizeof orig_val->addr);
321 ret = lydict_insert(ctx, orig_val->zone, 0, &dup_val->zone);
322 LY_CHECK_GOTO(ret, error);
323
324 dup->realtype = original->realtype;
325 return LY_SUCCESS;
326
327error:
328 lyplg_type_free_ipv4_address(ctx, dup);
329 return ret;
330}
331
335static void
336lyplg_type_free_ipv4_address(const struct ly_ctx *ctx, struct lyd_value *value)
337{
338 struct lyd_value_ipv4_address *val;
339
340 lydict_remove(ctx, value->_canonical);
341 value->_canonical = NULL;
342 LYD_VALUE_GET(value, val);
343 if (val) {
344 lydict_remove(ctx, val->zone);
346 }
347}
348
357 {
358 .module = "ietf-inet-types",
359 .revision = "2013-07-15",
360 .name = "ipv4-address",
361
362 .plugin.id = "libyang 2 - ipv4-address, version 1",
363 .plugin.store = lyplg_type_store_ipv4_address,
364 .plugin.validate = NULL,
365 .plugin.compare = lyplg_type_compare_ipv4_address,
366 .plugin.sort = NULL,
367 .plugin.print = lyplg_type_print_ipv4_address,
368 .plugin.duplicate = lyplg_type_dup_ipv4_address,
369 .plugin.free = lyplg_type_free_ipv4_address,
370 .plugin.lyb_data_len = -1,
371 },
372 {0}
373};
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_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv4_address[]
Plugin information for ipv4-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.
struct in_addr addr
Definition tree_data.h:658
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 values.
Definition tree_data.h:657
#define LOGMEM(CTX)
Definition tree_edit.h:22