libyang  2.0.112
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 #include <arpa/inet.h>
20 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
21 #include <netinet/in.h>
22 #include <sys/socket.h>
23 #endif
24 #include <errno.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "libyang.h"
30 
31 #include "common.h"
32 #include "compat.h"
33 
46 static LY_ERR
47 lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
48  size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
49  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
50  struct ly_err_item **err)
51 {
52  LY_ERR ret = LY_SUCCESS;
53  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
55 
56  /* init storage */
57  memset(storage, 0, sizeof *storage);
58  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
59  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
60  storage->realtype = type;
61 
62  if (format == LY_VALUE_LYB) {
63  /* validation */
64  if (value_len != 4) {
65  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
66  "(expected 4).", value_len);
67  goto cleanup;
68  }
69 
70  /* store IP address */
71  memcpy(&val->addr, value, 4);
72 
73  /* success */
74  goto cleanup;
75  }
76 
77  /* check hints */
78  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
79  LY_CHECK_GOTO(ret, cleanup);
80 
81  /* length restriction of the string */
82  if (type_str->length) {
83  /* value_len is in bytes, but we need number of characters here */
84  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
85  LY_CHECK_GOTO(ret, cleanup);
86  }
87 
88  /* pattern restrictions */
89  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, 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_len);
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 convert 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 
111 cleanup:
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 
125 static LY_ERR
126 lyplg_type_compare_ipv4_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
127 {
128  struct lyd_value_ipv4_address_no_zone *v1, *v2;
129 
130  if (val1->realtype != val2->realtype) {
131  return LY_ENOT;
132  }
133 
134  LYD_VALUE_GET(val1, v1);
135  LYD_VALUE_GET(val2, v2);
136 
137  if (memcmp(&v1->addr, &v2->addr, sizeof v1->addr)) {
138  return LY_ENOT;
139  }
140  return LY_SUCCESS;
141 }
142 
146 static const void *
147 lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
148  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
149 {
150  struct lyd_value_ipv4_address_no_zone *val;
151  char *ret;
152 
153  LYD_VALUE_GET(value, val);
154 
155  if (format == LY_VALUE_LYB) {
156  *dynamic = 0;
157  if (value_len) {
158  *value_len = 4;
159  }
160  return &val->addr;
161  }
162 
163  /* generate canonical value if not already (loaded from LYB) */
164  if (!value->_canonical) {
165  ret = malloc(INET_ADDRSTRLEN);
166  LY_CHECK_RET(!ret, NULL);
167 
168  /* get the address in string */
169  if (!inet_ntop(AF_INET, &val->addr, ret, INET_ADDRSTRLEN)) {
170  free(ret);
171  LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
172  return NULL;
173  }
174 
175  /* store it */
176  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
177  LOGMEM(ctx);
178  return NULL;
179  }
180  }
181 
182  /* use the cached canonical value */
183  if (dynamic) {
184  *dynamic = 0;
185  }
186  if (value_len) {
187  *value_len = strlen(value->_canonical);
188  }
189  return value->_canonical;
190 }
191 
200  {
201  .module = "ietf-inet-types",
202  .revision = "2013-07-15",
203  .name = "ipv4-address-no-zone",
204 
205  .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
206  .plugin.store = lyplg_type_store_ipv4_address_no_zone,
207  .plugin.validate = NULL,
208  .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
209  .plugin.sort = NULL,
210  .plugin.print = lyplg_type_print_ipv4_address_no_zone,
211  .plugin.duplicate = lyplg_type_dup_simple,
212  .plugin.free = lyplg_type_free_simple,
213  .plugin.lyb_data_len = 4,
214  },
215  {0}
216 };
struct lysc_type * realtype
Definition: tree_data.h:539
Compiled YANG data node.
Definition: tree_schema.h:1666
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 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:248
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
#define LYPLG_TYPE_STORE_DYNAMIC
Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
Definition: tree_data.h:625
#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:1580
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.
struct lysc_range * length
Definition: tree_schema.h:1579
YANG data representation.
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:536
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.
Libyang full error structure.
Definition: log.h:291
Definition: log.h:283
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:578
Definition: log.h:254
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.
const char * module
Definition: log.h:260
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
#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:1552
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:245
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.
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.
API for (user) types plugins.
libyang context handler.