libyang 3.4.2
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
ipv4_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
53static LY_ERR
54lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
55 size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
56 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
57 struct ly_err_item **err)
58{
59 LY_ERR ret = LY_SUCCESS;
61
62 /* init storage */
63 memset(storage, 0, sizeof *storage);
65 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
66 storage->realtype = type;
67
68 if (format == LY_VALUE_LYB) {
69 /* validation */
70 if (value_len != 4) {
71 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
72 "(expected 4).", value_len);
73 goto cleanup;
74 }
75
76 /* store IP address */
77 memcpy(&val->addr, value, 4);
78
79 /* success */
80 goto cleanup;
81 }
82
83 /* check hints */
84 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
85 LY_CHECK_GOTO(ret, cleanup);
86
87 /* we always need a dynamic value */
88 if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
89 value = strndup(value, value_len);
90 LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
91
92 options |= LYPLG_TYPE_STORE_DYNAMIC;
93 }
94
95 /* get the network-byte order address, validates the value */
96 if (!inet_pton(AF_INET, value, &val->addr)) {
97 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
98 goto cleanup;
99 }
100
101 /* store canonical value */
102 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
103 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
104 LY_CHECK_GOTO(ret, cleanup);
105
106cleanup:
107 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
108 free((void *)value);
109 }
110
111 if (ret) {
112 lyplg_type_free_simple(ctx, storage);
113 }
114 return ret;
115}
116
120static LY_ERR
121lyplg_type_compare_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
122 const struct lyd_value *val2)
123{
124 struct lyd_value_ipv4_address_no_zone *v1, *v2;
125
126 LYD_VALUE_GET(val1, v1);
127 LYD_VALUE_GET(val2, v2);
128
129 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
130 return LY_ENOT;
131 }
132 return LY_SUCCESS;
133}
134
138static int
139lyplg_type_sort_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
140 const struct lyd_value *val2)
141{
142 struct lyd_value_ipv4_address_no_zone *v1, *v2;
143
144 LYD_VALUE_GET(val1, v1);
145 LYD_VALUE_GET(val2, v2);
146
147 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
148}
149
153static const void *
154lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
155 void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
156{
158 char *ret;
159
160 LYD_VALUE_GET(value, val);
161
162 if (format == LY_VALUE_LYB) {
163 *dynamic = 0;
164 if (value_len) {
165 *value_len = 4;
166 }
167 return &val->addr;
168 }
169
170 /* generate canonical value if not already (loaded from LYB) */
171 if (!value->_canonical) {
172 ret = malloc(INET_ADDRSTRLEN);
173 LY_CHECK_RET(!ret, NULL);
174
175 /* get the address in string */
176 if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
177 free(ret);
178 LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
179 return NULL;
180 }
181
182 /* store it */
183 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
184 LOGMEM(ctx);
185 return NULL;
186 }
187 }
188
189 /* use the cached canonical value */
190 if (dynamic) {
191 *dynamic = 0;
192 }
193 if (value_len) {
194 *value_len = strlen(value->_canonical);
195 }
196 return value->_canonical;
197}
198
207 {
208 .module = "ietf-inet-types",
209 .revision = "2013-07-15",
210 .name = "ipv4-address-no-zone",
211
212 .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
213 .plugin.store = lyplg_type_store_ipv4_address_no_zone,
214 .plugin.validate = NULL,
215 .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
216 .plugin.sort = lyplg_type_sort_ipv4_address_no_zone,
217 .plugin.print = lyplg_type_print_ipv4_address_no_zone,
218 .plugin.duplicate = lyplg_type_dup_simple,
219 .plugin.free = lyplg_type_free_simple,
220 .plugin.lyb_data_len = 4,
221 },
222 {0}
223};
libyang context handler.
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_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.
LIBYANG_API_DECL LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
LIBYANG_API_DECL void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
#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_LYB
Definition tree.h:240
const struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-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 ipv4-address-no-zone values.
Definition tree_data.h:661
#define LOGMEM(CTX)
Definition tree_edit.h:22