aboutsummaryrefslogtreecommitdiffstats
path: root/lib/includes/hicn/name.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/includes/hicn/name.h')
-rw-r--r--lib/includes/hicn/name.h246
1 files changed, 140 insertions, 106 deletions
diff --git a/lib/includes/hicn/name.h b/lib/includes/hicn/name.h
index d5202068b..a4e8e02ad 100644
--- a/lib/includes/hicn/name.h
+++ b/lib/includes/hicn/name.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2021-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -24,119 +24,109 @@
#ifndef HICN_NAME_H
#define HICN_NAME_H
+#include <assert.h>
#include <stdbool.h>
+#include <stddef.h>
#ifndef _WIN32
-#include <netinet/in.h> // struct sockadd
+#include <netinet/in.h> // struct sockadd
#endif
+#include <hicn/common.h>
#include <hicn/util/ip_address.h>
-#include "common.h"
/******************************************************************************
* hICN names
******************************************************************************/
-#define TCP_SEQNO_LEN 4 /* bytes */
+#define TCP_SEQNO_LEN 4 /* bytes */
#define HICN_V4_PREFIX_LEN IPV4_ADDR_LEN
#define HICN_V6_PREFIX_LEN IPV6_ADDR_LEN
+
+#if 0
#define HICN_SEGMENT_LEN TCP_SEQNO_LEN
-#define HICN_V6_NAME_LEN (HICN_V6_PREFIX_LEN + HICN_SEGMENT_LEN) /* 20 bytes */
-#define HICN_V4_NAME_LEN (HICN_V4_PREFIX_LEN + HICN_SEGMENT_LEN) /* 8 bytes */
+
+#define HICN_V6_NAME_LEN \
+ (HICN_V6_PREFIX_LEN + HICN_SEGMENT_LEN) /* 20 bytes \
+ */
+#define HICN_V4_NAME_LEN \
+ (HICN_V4_PREFIX_LEN + HICN_SEGMENT_LEN) /* 8 bytes \
+ */
+#endif
+
+#define HICN_INVALID_SUFFIX ((uint32_t) (~0))
/* Prefix */
-typedef u32 hicn_name_suffix_t;
+#define HICN_PREFIX_MAX_LEN IP_ADDRESS_MAX_LEN
typedef struct
{
- ip46_address_t name;
+ hicn_ip_address_t name;
u8 len;
} hicn_prefix_t;
+#define HICN_PREFIX_EMPTY \
+ (hicn_prefix_t) { .name = IP_ADDRESS_EMPTY, .len = 0 }
+
+static inline const hicn_ip_address_t *
+hicn_prefix_get_ip_address (const hicn_prefix_t *prefix)
+{
+ return &prefix->name;
+}
+
+static inline u8
+hicn_prefix_get_len (const hicn_prefix_t *prefix)
+{
+ return prefix->len;
+}
+
+int hicn_prefix_get_ip_prefix (const hicn_prefix_t *prefix,
+ hicn_ip_prefix_t *ip_prefix);
/*
* Name
*
* A name is a prefix + a segment name (suffix)
*/
-typedef union
-{
- struct
- {
- union
- {
- u32 prefix;
- u8 prefix_as_u8[4];
- ip4_address_t prefix_as_ip4;
- };
- hicn_name_suffix_t suffix;
- };
- u8 buffer[HICN_V4_NAME_LEN];
-} hicn_v4_name_t;
-
-typedef union
-{
- struct
- {
- union
- {
- u64 prefix[2];
- u8 prefix_as_u8[16];
- ip6_address_t prefix_as_ip6;
- };
- hicn_name_suffix_t suffix;
- };
- u8 buffer[HICN_V6_NAME_LEN];
-} hicn_v6_name_t;
-
-#ifndef HICN_VPP_PLUGIN
-#define HICN_NAME_COMPONENT_SIZE 2
+typedef hicn_ip_address_t hicn_name_prefix_t;
+typedef uint32_t hicn_name_suffix_t;
+
+#define hicn_name_prefix_cmp hicn_ip_address_cmp
+#define hicn_name_prefix_equals hicn_ip_address_equals
+#define hicn_name_prefix_get_len_bits hicn_ip_address_get_len_bits
+#define hicn_name_prefix_get_hash hicn_ip_address_get_hash
+#define hicn_name_prefix_snprintf hicn_ip_address_snprintf
+#define HICN_NAME_PREFIX_EMPTY IP_ADDRESS_EMPTY
typedef struct
{
- struct iovec buffers[HICN_NAME_COMPONENT_SIZE];
-} hicn_iov_name_t;
+ hicn_name_prefix_t prefix;
+ hicn_name_suffix_t suffix;
+} hicn_name_t;
-#define UNSPEC 1 << 0
-#define HNT_CONTIGUOUS 1 << 1
-#define HNT_IOV 1 << 2
-#define HNT_INET 1 << 3
-#define HNT_INET6 1 << 4
+static_assert (offsetof (hicn_name_t, prefix) == 0, "");
+static_assert (offsetof (hicn_name_t, suffix) == 16, "");
+static_assert (sizeof (hicn_name_t) == 20, "");
-typedef enum
+#define HICN_NAME_EMPTY \
+ (hicn_name_t) { .prefix = HICN_NAME_PREFIX_EMPTY, .suffix = 0, }
+
+static inline const hicn_name_prefix_t *
+hicn_name_get_prefix (const hicn_name_t *name)
{
- HNT_UNSPEC = UNSPEC,
- HNT_CONTIGUOUS_V4 = HNT_CONTIGUOUS | HNT_INET,
- HNT_CONTIGUOUS_V6 = HNT_CONTIGUOUS | HNT_INET6,
- HNT_IOV_V4 = HNT_IOV | HNT_INET,
- HNT_IOV_V6 = HNT_IOV | HNT_INET6,
-} hicn_name_type_t;
-#endif /* HICN_VPP_PLUGIN */
+ return &name->prefix;
+}
-typedef struct
+static inline const hicn_name_suffix_t
+hicn_name_get_suffix (const hicn_name_t *name)
{
-#ifndef HICN_VPP_PLUGIN
- hicn_name_type_t type;
- u8 len;
-#endif /* HICN_VPP_PLUGIN */
- union
- {
- hicn_v4_name_t ip4;
- hicn_v6_name_t ip6;
- ip46_address_t ip46;
-#ifndef HICN_VPP_PLUGIN
- hicn_iov_name_t iov;
- u8 buffer[HICN_V6_NAME_LEN];
-#endif /* HICN_VPP_PLUGIN */
- };
-} hicn_name_t;
+ return name->suffix;
+}
-#ifndef HICN_VPP_PLUGIN
-#define _is_unspec(name) ((name->type & UNSPEC))
-#define _is_contiguous(name) ((name->type & HNT_CONTIGUOUS) >> 1)
-#define _is_iov(name) ((name->type & HNT_IOV) >> 2)
-#define _is_inet4(name) ((name->type & HNT_INET) >> 3)
-#define _is_inet6(name) ((name->type & HNT_INET6) >> 4)
-#endif /* HICN_VPP_PLUGIN */
+#define _is_unspec(name) \
+ (((name)->prefix.pad[0] | (name)->prefix.pad[1] | (name)->prefix.pad[2] | \
+ (name)->prefix.v4.as_u32) == 0)
+#define _is_inet4(name) (hicn_ip_address_is_v4 (&name->prefix))
+#define _is_inet6(name) (!_is_inet4 (name))
/**
* @brief Create an hICN name from IP address in presentation format
@@ -145,37 +135,40 @@ typedef struct
* @param [out] Resulting hICN name
* @return hICN error code
*/
-int hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name);
+int hicn_name_create (const char *ip_address, u32 id, hicn_name_t *name);
/**
* @brief Create an hICN name from IP address
* @param [in] ip_address - IP address
- * @param [in] id Segment - identifier
+ * @param [in] suffix - Name suffix
* @param [out] Resulting - hICN name
* @return hICN error code
*/
-int hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id,
- hicn_name_t * name);
+int hicn_name_create_from_ip_address (const hicn_ip_address_t ip_address,
+ u32 suffix, hicn_name_t *name);
/**
- * @brief Returns the length of an hICN name
- * @param [in] name - hICN name
- * @return Name length
+ * @brief Create an hICN name from IP prefix
+ * @param [in] prefix - IP prefix
+ * @param [in] suffix - Name suffix
+ * @param [out] Resulting - hICN name
+ * @return hICN error code
*/
-u8 hicn_name_get_length (const hicn_name_t * name);
+int hicn_name_create_from_ip_prefix (const hicn_ip_prefix_t *prefix, u32 id,
+ hicn_name_t *name);
/**
* @brief Compare two hICN names
* @param [in] name_1 - First name to compare
* @param [in] name_2 - Second name to compare
- * @param [in] consider_segment - Flag indicating whether the segment part has to be
- * considered
+ * @param [in] consider_segment - Flag indicating whether the segment part has
+ * to be considered
* @return An integer less than, equal to, or greater than zero if name_1 is
* found, respectively, to be lest than, to match, or be greater than name_2
* based on numeric order.
*/
-int hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2,
- bool consider_segment);
+int hicn_name_compare (const hicn_name_t *name_1, const hicn_name_t *name_2,
+ bool consider_segment);
/**
* @brief Provides a 32-bit hash of an hICN name
@@ -184,7 +177,10 @@ int hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2,
* @param [in] consider_suffix - Consider the suffix in the hash computation
* @return hICN error code
*/
-int hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix);
+uint32_t _hicn_name_get_hash (const hicn_name_t *name, bool consider_suffix);
+
+#define hicn_name_get_hash(NAME) _hicn_name_get_hash (NAME, true)
+#define hicn_name_get_prefix_hash(NAME) _hicn_name_get_hash (NAME, false)
/**
* @brief Test whether an hICN name is empty
@@ -192,7 +188,7 @@ int hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix);
* @return 0 if the name is empty, any other value otherwise (implementation
* returns 1)
*/
-int hicn_name_empty (hicn_name_t * name);
+int hicn_name_empty (hicn_name_t *name);
/**
* @brief Copy an hICN name
@@ -200,7 +196,7 @@ int hicn_name_empty (hicn_name_t * name);
* @param [in] src - Source name to copy
* @return hICN error code
*/
-int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src);
+int hicn_name_copy (hicn_name_t *dst, const hicn_name_t *src);
/**
* @brief Copy an hICN name to a buffer
@@ -209,8 +205,7 @@ int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src);
* @param [in] copy_suffix - Flag indicating whether the suffix has to be
* considered
*/
-int hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src,
- bool copy_suffix);
+int hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *src);
/**
* @brief Sets the segment part of an hICN name
@@ -218,7 +213,7 @@ int hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src,
* @param [in] seq_number - Segment identifier
* @return hICN error code
*/
-int hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number);
+int hicn_name_set_suffix (hicn_name_t *name, hicn_name_suffix_t suffix);
/**
* @brief Retrieves the segment part of an hICN name
@@ -226,7 +221,7 @@ int hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number);
* @param [in] seq_number - Segment identifier
* @return hICN error code
*/
-int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number);
+int hicn_name_get_seq_number (const hicn_name_t *name, u32 *seq_number);
/**
* @brief Convert an hICN name to a socket address
@@ -234,8 +229,8 @@ int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number);
* @param [out] ip_address - Resulting socket address
* @return hICN error code
*/
-int hicn_name_to_sockaddr_address (const hicn_name_t * name,
- struct sockaddr *ip_address);
+int hicn_name_to_sockaddr_address (const hicn_name_t *name,
+ struct sockaddr *ip_address);
/**
* @brief Convert an hICN name to an IP address
@@ -243,8 +238,8 @@ int hicn_name_to_sockaddr_address (const hicn_name_t * name,
* @param [out] ip_address - Resulting IP address
* @return hICN error code
*/
-int hicn_name_to_ip_prefix (const hicn_name_t * name,
- ip_prefix_t * ip_prefix);
+int hicn_name_to_hicn_ip_prefix (const hicn_name_t *name,
+ hicn_ip_prefix_t *hicn_ip_prefix);
/**
* @brief Convert an hICN name to presentation format
@@ -253,7 +248,7 @@ int hicn_name_to_ip_prefix (const hicn_name_t * name,
* @param [in] len - Number of bytes available in the buffer
* @return hICN error code
*/
-int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len);
+int hicn_name_ntop (const hicn_name_t *src, char *dst, size_t len);
/**
* @brief Convert an hICN name from presentation format
@@ -261,7 +256,7 @@ int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len);
* @param [out] dst - Resulting name
* @return hICN error code
*/
-int hicn_name_pton (const char *src, hicn_name_t * dst);
+int hicn_name_pton (const char *src, hicn_name_t *dst);
/**
* @brief Returns the IP address family of an hICN name
@@ -269,7 +264,19 @@ int hicn_name_pton (const char *src, hicn_name_t * dst);
* @param [out] family - Resulting IP address family (AF_INET or AF_INET6)
* @return hICN error code
*/
-int hicn_name_get_family (const hicn_name_t * name, int *family);
+int hicn_name_get_family (const hicn_name_t *name, int *family);
+
+bool hicn_name_is_v4 (const hicn_name_t *name);
+
+int hicn_name_snprintf (char *s, size_t size, const hicn_name_t *name);
+
+int hicn_name_no_suffix_snprintf (char *s, size_t size,
+ const hicn_name_t *name);
+
+int hicn_name_cmp (const hicn_name_t *n1, const hicn_name_t *n2);
+bool hicn_name_equals (const hicn_name_t *n1, const hicn_name_t *n2);
+
+#define MAXSZ_HICN_NAME MAXSZ_IP_ADDRESS
/**
* @brief Creates an hICN prefix from an IP address
@@ -277,8 +284,35 @@ int hicn_name_get_family (const hicn_name_t * name, int *family);
* @param [out] prefix - Resulting prefix
* @return hICN error code
*/
-int hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix,
- hicn_prefix_t * prefix);
+int hicn_prefix_create_from_ip_prefix (const hicn_ip_prefix_t *hicn_ip_prefix,
+ hicn_prefix_t *prefix);
+
+int
+hicn_prefix_create_from_ip_address_len (const hicn_ip_address_t *ip_address,
+ uint8_t len, hicn_prefix_t *prefix);
+
+hicn_prefix_t *hicn_prefix_dup (const hicn_prefix_t *prefix);
+
+int hicn_prefix_copy (hicn_prefix_t *dst, const hicn_prefix_t *src);
+
+bool hicn_prefix_is_v4 (const hicn_prefix_t *prefix);
+
+uint32_t hicn_prefix_lpm (const hicn_prefix_t *p1, const hicn_prefix_t *p2);
+
+void hicn_prefix_clear (hicn_prefix_t *prefix, uint8_t start_from);
+
+void hicn_prefix_truncate (hicn_prefix_t *prefix, uint8_t len);
+
+int hicn_prefix_cmp (const hicn_prefix_t *p1, const hicn_prefix_t *p2);
+
+bool hicn_prefix_equals (const hicn_prefix_t *p1, const hicn_prefix_t *p2);
+
+int hicn_prefix_snprintf (char *s, size_t size, const hicn_prefix_t *prefix);
+
+uint8_t _hicn_prefix_get_bit (const hicn_prefix_t *prefix, uint8_t pos);
+uint8_t hicn_prefix_get_bit (const hicn_prefix_t *prefix, uint8_t pos);
+
+#define MAXSZ_HICN_PREFIX MAXSZ_IP_PREFIX
#endif /* HICN_NAME_H */