libyang  2.1.30
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ipv4_address_no_zone.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 <errno.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "libyang.h"
35 
36 #include "common.h"
37 #include "compat.h"
38 
51 static LY_ERR
52 lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
53  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
54  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
55  struct ly_err_item **err)
56 {
57  LY_ERR ret = LY_SUCCESS;
58  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
60 
61  /* init storage */
62  memset(storage, 0, sizeof *storage);
63  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
64  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
65  storage->realtype = type;
66 
67  if (format == LY_VALUE_LYB) {
68  /* validation */
69  if (value_len != 4) {
70  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
71  "(expected 4).", value_len);
72  goto cleanup;
73  }
74 
75  /* store IP address */
76  memcpy(&val->addr, value, 4);
77 
78  /* success */
79  goto cleanup;
80  }
81 
82  /* check hints */
83  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
84  LY_CHECK_GOTO(ret, cleanup);
85 
86  /* length restriction of the string */
87  if (type_str->length) {
88  /* value_len is in bytes, but we need number of characters here */
89  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
90  LY_CHECK_GOTO(ret, cleanup);
91  }
92 
93  /* pattern restrictions */
94  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
95  LY_CHECK_GOTO(ret, cleanup);
96 
97  /* we always need a dynamic value */
98  if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
99  value = strndup(value, value_len);
100  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
101 
102  options |= LYPLG_TYPE_STORE_DYNAMIC;
103  }
104 
105  /* get the network-byte order address */
106  if (!inet_pton(AF_INET, value, &val->addr)) {
107  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
108  goto cleanup;
109  }
110 
111  /* store canonical value */
112  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
113  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
114  LY_CHECK_GOTO(ret, cleanup);
115 
116 cleanup:
117  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
118  free((void *)value);
119  }
120 
121  if (ret) {
122  lyplg_type_free_simple(ctx, storage);
123  }
124  return ret;
125 }
126 
130 static LY_ERR
131 lyplg_type_compare_ipv4_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
132 {
133  struct lyd_value_ipv4_address_no_zone *v1, *v2;
134 
135  if (val1->realtype != val2->realtype) {
136  return LY_ENOT;
137  }
138 
139  LYD_VALUE_GET(val1, v1);
140  LYD_VALUE_GET(val2, v2);
141 
142  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
143  return LY_ENOT;
144  }
145  return LY_SUCCESS;
146 }
147 
151 static const void *
152 lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
153  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
154 {
155  struct lyd_value_ipv4_address_no_zone *val;
156  char *ret;
157 
158  LYD_VALUE_GET(value, val);
159 
160  if (format == LY_VALUE_LYB) {
161  *dynamic = 0;
162  if (value_len) {
163  *value_len = 4;
164  }
165  return &val->addr;
166  }
167 
168  /* generate canonical value if not already (loaded from LYB) */
169  if (!value->_canonical) {
170  ret = malloc(INET_ADDRSTRLEN);
171  LY_CHECK_RET(!ret, NULL);
172 
173  /* get the address in string */
174  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
175  free(ret);
176  LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
177  return NULL;
178  }
179 
180  /* store it */
181  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
182  LOGMEM(ctx);
183  return NULL;
184  }
185  }
186 
187  /* use the cached canonical value */
188  if (dynamic) {
189  *dynamic = 0;
190  }
191  if (value_len) {
192  *value_len = strlen(value->_canonical);
193  }
194  return value->_canonical;
195 }
196 
205  {
206  .module = "ietf-inet-types",
207  .revision = "2013-07-15",
208  .name = "ipv4-address-no-zone",
209 
210  .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
211  .plugin.store = lyplg_type_store_ipv4_address_no_zone,
212  .plugin.validate = NULL,
213  .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
214  .plugin.sort = NULL,
215  .plugin.print = lyplg_type_print_ipv4_address_no_zone,
216  .plugin.duplicate = lyplg_type_dup_simple,
217  .plugin.free = lyplg_type_free_simple,
218  .plugin.lyb_data_len = 4,
219  },
220  {0}
221 };
struct lysc_type * realtype
Definition: tree_data.h:564
Compiled YANG data node.
Definition: tree_schema.h:1414
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.
Definition: log.h:256
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition: tree_data.h:650
#define LOGMEM(CTX)
Definition: tree_edit.h:22
struct lyplg_type_record plugins_ipv4_address_no_zone[]
Plugin information for ipv4-address-no-zone type implementation.
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1327
struct lysc_range * length
Definition: tree_schema.h:1326
YANG data representation.
Definition: tree_data.h:560
const char * _canonical
Definition: tree_data.h:561
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 full error structure.
Definition: log.h:299
Definition: log.h:291
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:603
Definition: log.h:262
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&#39;s hints (if any) in the specified format.
const char * module
Definition: log.h:268
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1299
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.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:254
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.
API for (user) types plugins.
libyang context handler.
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.