libyang 6.4.3
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 "compat.h"
37#include "dict.h"
38#include "ly_common.h"
48static void
49lyplg_type_lyb_size_ipv4_address_no_zone(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
50 uint64_t *fixed_size_bits)
51{
52 *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
53 *fixed_size_bits = 32;
54}
55
59static LY_ERR
60lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
61 uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
62 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
63 struct ly_err_item **err)
64{
65 LY_ERR ret = LY_SUCCESS;
67 uint32_t value_size;
68
69 /* init storage */
70 memset(storage, 0, sizeof *storage);
72 LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
73 storage->realtype = type;
74
75 /* check value length */
76 ret = lyplg_type_check_value_size("ipv4-address-no-zone", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 32,
77 &value_size, err);
78 LY_CHECK_GOTO(ret, cleanup);
79
80 if (format == LY_VALUE_LYB) {
81 /* store IP address */
82 memcpy(&val->addr, value, 4);
83
84 /* success */
85 goto cleanup;
86 }
87
88 /* check hints */
89 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
90 LY_CHECK_GOTO(ret, cleanup);
91
92 /* we always need a dynamic value */
93 if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
94 value = strndup(value, value_size);
95 LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
96
97 options |= LYPLG_TYPE_STORE_DYNAMIC;
98 }
99
100 /* get the network-byte order address */
101 if (!inet_pton(AF_INET, value, &val->addr)) {
102 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to store IPv4 address \"%s\".", (char *)value);
103 goto cleanup;
104 }
105
106 /* store canonical value */
107 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
108 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
109 LY_CHECK_GOTO(ret, cleanup);
110
111cleanup:
112 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
113 free((void *)value);
114 }
115
116 if (ret) {
117 lyplg_type_free_simple(ctx, storage);
118 }
119 return ret;
120}
121
125static LY_ERR
126lyplg_type_compare_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
127 const struct lyd_value *val2)
128{
129 struct lyd_value_ipv4_address_no_zone *v1, *v2;
130
131 LYD_VALUE_GET(val1, v1);
132 LYD_VALUE_GET(val2, v2);
133
134 if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
135 return LY_ENOT;
136 }
137 return LY_SUCCESS;
138}
139
143static int
144lyplg_type_sort_ipv4_address_no_zone(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
145 const struct lyd_value *val2)
146{
147 struct lyd_value_ipv4_address_no_zone *v1, *v2;
148
149 LYD_VALUE_GET(val1, v1);
150 LYD_VALUE_GET(val2, v2);
151
152 return memcmp(&v1->addr, &v2->addr, sizeof v1->addr);
153}
154
158static const void *
159lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
160 void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
161{
163 char *ret;
164
165 LYD_VALUE_GET(value, val);
166
167 if (format == LY_VALUE_LYB) {
168 *dynamic = 0;
169 if (value_size_bits) {
170 *value_size_bits = 32;
171 }
172 return &val->addr;
173 }
174
175 /* generate canonical value if not already (loaded from LYB) */
176 if (!value->_canonical) {
177 ret = malloc(INET_ADDRSTRLEN);
178 LY_CHECK_RET(!ret, NULL);
179
180 /* get the address in string */
181 if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
182 free(ret);
183 LOGERR(ctx, LY_ESYS, "Failed to get IPv4 address in string (%s).", strerror(errno));
184 return NULL;
185 }
186
187 /* store it */
188 if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
189 LOGMEM(ctx);
190 return NULL;
191 }
192 }
193
194 /* use the cached canonical value */
195 if (dynamic) {
196 *dynamic = 0;
197 }
198 if (value_size_bits) {
199 *value_size_bits = strlen(value->_canonical) * 8;
200 }
201 return value->_canonical;
202}
203
212 {
213 .module = "ietf-inet-types",
214 .revision = NULL,
215 .name = "ipv4-address-no-zone",
216
217 .plugin.id = "ly2 ipv4-address-no-zone",
218 .plugin.lyb_size = lyplg_type_lyb_size_ipv4_address_no_zone,
219 .plugin.store = lyplg_type_store_ipv4_address_no_zone,
220 .plugin.validate_value = NULL,
221 .plugin.validate_tree = NULL,
222 .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
223 .plugin.sort = lyplg_type_sort_ipv4_address_no_zone,
224 .plugin.print = lyplg_type_print_ipv4_address_no_zone,
225 .plugin.duplicate = lyplg_type_dup_simple,
226 .plugin.free = lyplg_type_free_simple,
227 },
228 {0}
229};
libyang dictionary
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:252
@ LYVE_DATA
Definition log.h:290
@ LY_ESYS
Definition log.h:255
@ LY_EMEM
Definition log.h:254
@ LY_ENOT
Definition log.h:266
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:298
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.
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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 lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
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.
@ LYPLG_LYB_SIZE_FIXED_BITS
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.
const struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:614
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition tree_data.h:653
const char * _canonical
Definition tree_data.h:611
YANG data representation.
Definition tree_data.h:610
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition tree_data.h:700
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition utils.h:93
@ LY_VALUE_LYB
Definition utils.h:100