plugins types FEATURE specific ipv4-address-no-zone plugin
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 925ac39..3281590 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -103,6 +103,7 @@
src/plugins_types/string.c
src/plugins_types/union.c
src/plugins_types/ipv4_address.c
+ src/plugins_types/ipv4_address_no_zone.c
src/plugins_types/ipv6_address.c
src/plugins_types/ipv4_prefix.c
src/plugins_types/ipv6_prefix.c
diff --git a/src/plugins.c b/src/plugins.c
index 57f5b1c..87a7260 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -55,6 +55,7 @@
* ietf-inet-types
*/
extern const struct lyplg_type_record plugins_ipv4_address[];
+extern const struct lyplg_type_record plugins_ipv4_address_no_zone[];
extern const struct lyplg_type_record plugins_ipv6_address[];
extern const struct lyplg_type_record plugins_ipv4_prefix[];
extern const struct lyplg_type_record plugins_ipv6_prefix[];
@@ -422,6 +423,7 @@
/* ietf-inet-types */
LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address), error);
+ LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address_no_zone), error);
LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_address), error);
LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_prefix), error);
LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_prefix), error);
diff --git a/src/plugins_types/ipv4_address.c b/src/plugins_types/ipv4_address.c
index 3c359b6..77f5e32 100644
--- a/src/plugins_types/ipv4_address.c
+++ b/src/plugins_types/ipv4_address.c
@@ -36,7 +36,7 @@
/**
* @page howtoDataLYB LYB Binary Format
- * @subsection howtoDataLYBTypesIPv4Address ipv4-address(-no-zone) (ietf-inet-types)
+ * @subsection howtoDataLYBTypesIPv4Address ipv4-address (ietf-inet-types)
*
* | Size (B) | Mandatory | Type | Meaning |
* | :------ | :-------: | :--: | :-----: |
@@ -140,25 +140,17 @@
if (format == LY_VALUE_LYB) {
/* validation */
- if (!strcmp(type->plugin->id, "libyang 2 - ipv4-address-no-zone, version 1")) {
- if (value_len != 4) {
- ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
- "(expected 4).", value_len);
+ if (value_len < 4) {
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %zu "
+ "(expected at least 4).", value_len);
+ goto cleanup;
+ }
+ for (i = 4; i < value_len; ++i) {
+ if (!isalnum(value_str[i])) {
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address zone character 0x%x.",
+ value_str[i]);
goto cleanup;
}
- } else {
- if (value_len < 4) {
- ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address value size %zu "
- "(expected at least 4).", value_len);
- goto cleanup;
- }
- for (i = 4; i < value_len; ++i) {
- if (!isalnum(value_str[i])) {
- ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address zone character 0x%x.",
- value_str[i]);
- goto cleanup;
- }
- }
}
/* allocate the value */
@@ -213,16 +205,14 @@
ret = ipv4address_str2ip(value, value_len, options, ctx, &val->addr, &val->zone, err);
LY_CHECK_GOTO(ret, cleanup);
- if (format == LY_VALUE_CANON) {
- /* store canonical value */
- if (options & LYPLG_TYPE_STORE_DYNAMIC) {
- ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
- options &= ~LYPLG_TYPE_STORE_DYNAMIC;
- LY_CHECK_GOTO(ret, cleanup);
- } else {
- ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
- LY_CHECK_GOTO(ret, cleanup);
- }
+ /* store canonical value */
+ if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+ ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
+ options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+ LY_CHECK_GOTO(ret, cleanup);
+ } else {
+ ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
+ LY_CHECK_GOTO(ret, cleanup);
}
cleanup:
@@ -383,7 +373,7 @@
}
/**
- * @brief Plugin information for ip-address type implementation.
+ * @brief Plugin information for ipv4-address type implementation.
*
* Note that external plugins are supposed to use:
*
@@ -404,19 +394,5 @@
.plugin.duplicate = lyplg_type_dup_ipv4_address,
.plugin.free = lyplg_type_free_ipv4_address
},
- {
- .module = "ietf-inet-types",
- .revision = "2013-07-15",
- .name = "ipv4-address-no-zone",
-
- .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
- .plugin.store = lyplg_type_store_ipv4_address,
- .plugin.validate = NULL,
- .plugin.compare = lyplg_type_compare_ipv4_address,
- .plugin.print = lyplg_type_print_ipv4_address,
- .plugin.hash = lyplg_type_hash_ipv4_address,
- .plugin.duplicate = lyplg_type_dup_ipv4_address,
- .plugin.free = lyplg_type_free_ipv4_address
- },
{0}
};
diff --git a/src/plugins_types/ipv4_address_no_zone.c b/src/plugins_types/ipv4_address_no_zone.c
new file mode 100644
index 0000000..fe7057a
--- /dev/null
+++ b/src/plugins_types/ipv4_address_no_zone.c
@@ -0,0 +1,233 @@
+/**
+ * @file ipv4_address_no_zone.c
+ * @author Michal Vasko <mvasko@cesnet.cz>
+ * @brief ietf-inet-types ipv4-address-no-zone type plugin.
+ *
+ * Copyright (c) 2019-2021 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#define _GNU_SOURCE /* asprintf, strdup */
+#include <sys/cdefs.h>
+
+#include "plugins_types.h"
+
+#include <arpa/inet.h>
+#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
+#include <netinet/in.h>
+#include <sys/socket.h>
+#endif
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libyang.h"
+
+#include "common.h"
+#include "compat.h"
+
+/**
+ * @page howtoDataLYB LYB Binary Format
+ * @subsection howtoDataLYBTypesIPv4AddressNoZone ipv4-address-no-zone (ietf-inet-types)
+ *
+ * | Size (B) | Mandatory | Type | Meaning |
+ * | :------ | :-------: | :--: | :-----: |
+ * | 4 | yes | `struct in_addr *` | IPv4 address in network-byte order |
+ */
+
+/**
+ * @brief Stored value structure for ipv4-address
+ */
+struct lyd_value_ipv4_address_no_zone {
+ struct in_addr addr;
+};
+
+/**
+ * @brief Implementation of ::lyplg_type_store_clb for the ipv4-address-no-zone ietf-inet-types type.
+ */
+static LY_ERR
+lyplg_type_store_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value,
+ size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
+ const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
+ struct ly_err_item **err)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lysc_type_str *type_str = (struct lysc_type_str *)type;
+
+ /* zero storage so we can always free it */
+ memset(storage, 0, sizeof *storage);
+
+ if (format == LY_VALUE_LYB) {
+ /* validation */
+ if (value_len != 4) {
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB ipv4-address-no-zone value size %zu "
+ "(expected 4).", value_len);
+ goto cleanup;
+ }
+
+ /* init storage */
+ storage->_canonical = NULL;
+ storage->ptr = NULL;
+ storage->realtype = type;
+
+ /* store IP address as uint32_t value */
+ memcpy(&storage->uint32, value, 4);
+
+ /* success */
+ goto cleanup;
+ }
+
+ /* check hints */
+ ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* length restriction of the string */
+ if (type_str->length) {
+ /* value_len is in bytes, but we need number of characters here */
+ ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
+ LY_CHECK_GOTO(ret, cleanup);
+ }
+
+ /* pattern restrictions */
+ ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* init storage */
+ storage->_canonical = NULL;
+ storage->ptr = NULL;
+ storage->realtype = type;
+
+ /* we always need a dynamic value */
+ if (!(options & LYPLG_TYPE_STORE_DYNAMIC)) {
+ value = strndup(value, value_len);
+ LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
+
+ options |= LYPLG_TYPE_STORE_DYNAMIC;
+ }
+
+ /* get the network-byte order address */
+ if (!inet_pton(AF_INET, value, &storage->uint32)) {
+ ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to convert IPv4 address \"%s\".", (char *)value);
+ goto cleanup;
+ }
+
+ /* store canonical value */
+ ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
+ options &= ~LYPLG_TYPE_STORE_DYNAMIC;
+ LY_CHECK_GOTO(ret, cleanup);
+
+cleanup:
+ if (options & LYPLG_TYPE_STORE_DYNAMIC) {
+ free((void *)value);
+ }
+
+ if (ret) {
+ lyplg_type_free_simple(ctx, storage);
+ }
+ return ret;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_compare_clb for the ipv4-address-no-zone ietf-inet-types type.
+ */
+static LY_ERR
+lyplg_type_compare_ipv4_address_no_zone(const struct lyd_value *val1, const struct lyd_value *val2)
+{
+ if (val1->realtype != val2->realtype) {
+ return LY_ENOT;
+ }
+
+ if (val1->uint32 != val2->uint32) {
+ return LY_ENOT;
+ }
+ return LY_SUCCESS;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_print_clb for the ipv4-address-no-zone ietf-inet-types type.
+ */
+static const void *
+lyplg_type_print_ipv4_address_no_zone(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
+ void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
+{
+ char *ret;
+
+ if (format == LY_VALUE_LYB) {
+ *dynamic = 0;
+ if (value_len) {
+ *value_len = 4;
+ }
+ return &value->uint32;
+ }
+
+ /* generate canonical value if not already (loaded from LYB) */
+ if (!value->_canonical) {
+ ret = malloc(INET_ADDRSTRLEN);
+ LY_CHECK_RET(!ret, NULL);
+
+ /* get the address in string */
+ if (!inet_ntop(AF_INET, &value->uint32, ret, INET_ADDRSTRLEN)) {
+ free(ret);
+ LOGERR(ctx, LY_EVALID, "Failed to get IPv4 address in string (%s).", strerror(errno));
+ return NULL;
+ }
+
+ /* store it */
+ if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
+ LOGMEM(ctx);
+ return NULL;
+ }
+ }
+
+ /* use the cached canonical value */
+ if (dynamic) {
+ *dynamic = 0;
+ }
+ if (value_len) {
+ *value_len = strlen(value->_canonical);
+ }
+ return value->_canonical;
+}
+
+/**
+ * @brief Implementation of ::lyplg_type_hash_clb for the ipv4-address-no-zone ietf-inet-types type.
+ */
+static const void *
+lyplg_type_hash_ipv4_address_no_zone(const struct lyd_value *value, ly_bool *dynamic, size_t *key_len)
+{
+ /* simply use the LYB value */
+ return lyplg_type_print_ipv4_address_no_zone(NULL, value, LY_VALUE_LYB, NULL, dynamic, key_len);
+}
+
+/**
+ * @brief Plugin information for ipv4-address-no-zone type implementation.
+ *
+ * Note that external plugins are supposed to use:
+ *
+ * LYPLG_TYPES = {
+ */
+const struct lyplg_type_record plugins_ipv4_address_no_zone[] = {
+ {
+ .module = "ietf-inet-types",
+ .revision = "2013-07-15",
+ .name = "ipv4-address-no-zone",
+
+ .plugin.id = "libyang 2 - ipv4-address-no-zone, version 1",
+ .plugin.store = lyplg_type_store_ipv4_address_no_zone,
+ .plugin.validate = NULL,
+ .plugin.compare = lyplg_type_compare_ipv4_address_no_zone,
+ .plugin.print = lyplg_type_print_ipv4_address_no_zone,
+ .plugin.hash = lyplg_type_hash_ipv4_address_no_zone,
+ .plugin.duplicate = lyplg_type_dup_simple,
+ .plugin.free = lyplg_type_free_simple
+ },
+ {0}
+};