summaryrefslogtreecommitdiffstats
path: root/lib/src/name.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/name.c')
-rw-r--r--lib/src/name.c435
1 files changed, 59 insertions, 376 deletions
diff --git a/lib/src/name.c b/lib/src/name.c
index 9388c35e7..1aa54ad40 100644
--- a/lib/src/name.c
+++ b/lib/src/name.c
@@ -30,72 +30,32 @@
#include <hicn/error.h>
#include <hicn/name.h>
-#if ! HICN_VPP_PLUGIN
int
hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name)
{
int af, rc;
+ u8 *dst;
af = get_addr_family (ip_address);
switch (af)
{
case AF_INET:
- if (name->type == HNT_UNSPEC)
- {
- name->type = HNT_CONTIGUOUS_V4;
- }
- name->len = IPV4_ADDR_LEN;
+ dst = (u8*)(&name->prefix.ip4);
break;
case AF_INET6:
- if (name->type == HNT_UNSPEC)
- {
- name->type = HNT_CONTIGUOUS_V6;
- }
- name->len = IPV6_ADDR_LEN;
+ dst = (u8*)(&name->prefix.ip6.as_u8);
break;
default:
return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
}
- if ((name->type != HNT_CONTIGUOUS_V4) && (name->type != HNT_CONTIGUOUS_V6))
- {
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
- rc = inet_pton (af, ip_address, name->buffer);
+ rc = inet_pton (af, ip_address, dst);
if (rc <= 0)
{
return HICN_LIB_ERROR_UNKNOWN_ADDRESS;
}
- *(u32 *) (name->buffer + name->len) = id;
-
- return HICN_LIB_ERROR_NONE;
-}
-
-int
-hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id,
- hicn_name_t * name)
-{
- switch (prefix->family)
- {
- case AF_INET:
- name->type = HNT_CONTIGUOUS_V4;
- memcpy (name->buffer, prefix->address.v4.buffer,
- ip_address_len(prefix->family));
- name->len = IPV4_ADDR_LEN;
- break;
- case AF_INET6:
- name->type = HNT_CONTIGUOUS_V6;
- memcpy (name->buffer, prefix->address.v6.buffer,
- ip_address_len(prefix->family));
- name->len = IPV6_ADDR_LEN;
- break;
- default:
- return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
- }
-
- *(u32 *) (name->buffer + name->len) = id;
+ name->suffix = id;
return HICN_LIB_ERROR_NONE;
}
@@ -103,170 +63,25 @@ hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id,
u8
hicn_name_get_length (const hicn_name_t * name)
{
- return name->len;
+ return hicn_name_is_ip4(name) ? HICN_V4_NAME_LEN : HICN_V4_NAME_LEN;
}
-int
-hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2,
- bool consider_segment)
-{
- hicn_name_t *name1 = (hicn_name_t *) name_1;
- hicn_name_t *name2 = (hicn_name_t *) name_2;
-
- if ((name1->type == HNT_CONTIGUOUS_V4 && name2->type == HNT_CONTIGUOUS_V6)
- || (name1->type == HNT_CONTIGUOUS_V6
- && name2->type == HNT_CONTIGUOUS_V4))
- {
- return -1;
- }
-
- if ((name1->type == HNT_IOV_V4 && name2->type == HNT_IOV_V6) ||
- (name1->type == HNT_IOV_V6 && name2->type == HNT_IOV_V4))
- {
- return -1;
- }
-
- if ((name1->type == HNT_IOV_V4 && name2->type == HNT_CONTIGUOUS_V6) ||
- (name1->type == HNT_IOV_V6 && name2->type == HNT_CONTIGUOUS_V4))
- {
- return -1;
- }
-
- if (name1->type == HNT_UNSPEC || name2->type == HNT_UNSPEC)
- {
- return -1;
- }
-
- size_t len1 = 0, len2 = 0;
-
- u8 *buffer11, *buffer12, *buffer21, *buffer22;
-
- switch (name1->type)
- {
- case HNT_CONTIGUOUS_V4:
- buffer11 = name1->buffer;
- buffer12 = name1->buffer + IPV4_ADDR_LEN;
- len1 = IPV4_ADDR_LEN;
- break;
- case HNT_CONTIGUOUS_V6:
- buffer11 = name1->buffer;
- buffer12 = name1->buffer + IPV6_ADDR_LEN;
- len1 = IPV6_ADDR_LEN;
- break;
- case HNT_IOV_V4:
- buffer11 = name1->iov.buffers[0].iov_base;
- buffer12 = name1->iov.buffers[1].iov_base;
- len1 = IPV4_ADDR_LEN;
- break;
- case HNT_IOV_V6:
- buffer11 = name1->iov.buffers[0].iov_base;
- buffer12 = name1->iov.buffers[1].iov_base;
- len1 = IPV6_ADDR_LEN;
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
- switch (name2->type)
- {
- case HNT_CONTIGUOUS_V4:
- buffer21 = name2->buffer;
- buffer22 = name2->buffer + IPV4_ADDR_LEN;
- len2 = IPV4_ADDR_LEN;
- break;
- case HNT_CONTIGUOUS_V6:
- buffer21 = name2->buffer;
- buffer22 = name2->buffer + IPV6_ADDR_LEN;
- len2 = IPV6_ADDR_LEN;
- break;
- case HNT_IOV_V4:
- buffer21 = name2->iov.buffers[0].iov_base;
- buffer22 = name2->iov.buffers[1].iov_base;
- len2 = IPV4_ADDR_LEN;
- break;
- case HNT_IOV_V6:
- buffer21 = name2->iov.buffers[0].iov_base;
- buffer22 = name2->iov.buffers[1].iov_base;
- len2 = IPV6_ADDR_LEN;
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
- // Sanity check
- if (len1 != len2)
- {
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
- int ret1 = memcmp ((u8 *) buffer11, (u8 *) buffer21, len1);
-
- if (!consider_segment)
- {
- return ret1;
- }
-
- int ret2 = memcmp ((u8 *) buffer12, (u8 *) buffer22, HICN_SEGMENT_LEN);
-
- return ret1 || ret2;
-}
+#if ! HICN_VPP_PLUGIN
int
hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix)
{
- switch (name->type)
- {
- case HNT_CONTIGUOUS_V4:
- *hash = hash32 (name->buffer, consider_suffix ? HICN_V4_NAME_LEN : HICN_V4_PREFIX_LEN);
- break;
- case HNT_CONTIGUOUS_V6:
- *hash = hash32 (name->buffer, consider_suffix ? HICN_V6_NAME_LEN : HICN_V6_PREFIX_LEN);
- break;
- case HNT_IOV_V4:
- case HNT_IOV_V6:
- *hash =
- hash32 (name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len);
- if (consider_suffix)
- {
- *hash = cumulative_hash32 (name->iov.buffers[1].iov_base,
- name->iov.buffers[1].iov_len, *hash);
- }
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
+ size_t size = (u8)consider_suffix * sizeof(hicn_name_suffix_t) + IPV6_ADDR_LEN;
+ *hash = hash32 (name->buffer, size);
return HICN_LIB_ERROR_NONE;
}
-int
-hicn_name_empty (hicn_name_t * name)
-{
- return name->type == HNT_UNSPEC ? HICN_LIB_ERROR_NONE : 1;
-}
+#endif /* ! HICN_VPP_PLUGIN */
int
hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src)
{
- switch (src->type)
- {
- case HNT_CONTIGUOUS_V4:
- case HNT_CONTIGUOUS_V6:
- *dst = *src;
- break;
- case HNT_IOV_V4:
- case HNT_IOV_V6:
- dst->type =
- src->type == HNT_IOV_V4 ? HNT_CONTIGUOUS_V4 : HNT_CONTIGUOUS_V6;
- memcpy (dst->buffer, src->iov.buffers[0].iov_base,
- src->iov.buffers[0].iov_len);
- memcpy (dst->buffer + src->iov.buffers[0].iov_len,
- src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
+ memcpy (dst, src, sizeof(*dst));
return HICN_LIB_ERROR_NONE;
}
@@ -274,78 +89,18 @@ int
hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src,
bool copy_suffix)
{
- size_t length;
-
- switch (src->type)
- {
- case HNT_CONTIGUOUS_V4:
- if (copy_suffix)
- {
- length = HICN_V4_NAME_LEN;
- }
- else
- {
- length = IPV4_ADDR_LEN;
- }
- memcpy (dst, src->buffer, length);
- break;
- case HNT_CONTIGUOUS_V6:
- if (copy_suffix)
- {
- length = HICN_V6_NAME_LEN;
- }
- else
- {
- length = IPV6_ADDR_LEN;
- }
- memcpy (dst, src->buffer, length);
- break;
- case HNT_IOV_V4:
- case HNT_IOV_V6:
- memcpy (dst, src->iov.buffers[0].iov_base, src->iov.buffers[0].iov_len);
- if (copy_suffix)
- {
- memcpy (dst + src->iov.buffers[0].iov_len,
- src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len);
- }
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
-
+ u8 is_ip4 = hicn_name_is_ip4(src);
+ size_t size = is_ip4 * IPV4_ADDR_LEN + (1 - is_ip4) * IPV6_ADDR_LEN;
+ size += (u8)copy_suffix * sizeof (hicn_name_suffix_t);
+ void *_src = (void *)(is_ip4 * (u64)(&src->prefix.ip4) + (1 - is_ip4) * (u64)(&src->prefix.ip6));
+ memcpy (dst, _src, size);
return HICN_LIB_ERROR_NONE;
}
int
hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number)
{
- u8 *sequence_number = NULL;
-
- switch (name->type)
- {
- case HNT_CONTIGUOUS_V6:
- sequence_number = name->buffer + IPV6_ADDR_LEN;
- break;
- case HNT_CONTIGUOUS_V4:
- sequence_number = name->buffer + IPV4_ADDR_LEN;
- break;
- case HNT_IOV_V6:
- case HNT_IOV_V4:
- sequence_number = name->iov.buffers[1].iov_base;
- break;
- case HNT_UNSPEC:
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
- if (sequence_number)
- {
- *(u32 *) sequence_number = seq_number;
- }
- else
- {
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
+ name->suffix = seq_number;
return HICN_LIB_ERROR_NONE;
}
@@ -356,34 +111,20 @@ hicn_name_to_sockaddr_address (const hicn_name_t * name,
struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) ip_address;
struct sockaddr_in *tmp4 = (struct sockaddr_in *) ip_address;
- switch (name->type)
+ u8 is_ip4 = hicn_name_is_ip4 (name);
+ ip_address->sa_family = AF_INET * is_ip4 + AF_INET6 * (1 - is_ip4);
+
+ if (is_ip4)
{
- case HNT_CONTIGUOUS_V6:
- tmp6->sin6_family = AF_INET6;
- tmp6->sin6_scope_id = 0;
- tmp6->sin6_port = DUMMY_PORT;
- memcpy (&tmp6->sin6_addr, name->buffer, IPV6_ADDR_LEN);
- break;
- case HNT_IOV_V6:
- tmp6->sin6_family = AF_INET6;
- tmp6->sin6_scope_id = 0;
- tmp6->sin6_port = DUMMY_PORT;
- memcpy (&tmp6->sin6_addr, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
- break;
- case HNT_CONTIGUOUS_V4:
tmp4->sin_family = AF_INET;
tmp4->sin_port = DUMMY_PORT;
- memcpy (&tmp4->sin_addr, name->buffer, IPV4_ADDR_LEN);
- break;
- case HNT_IOV_V4:
- tmp4->sin_family = AF_INET;
- tmp4->sin_port = DUMMY_PORT;
- memcpy (&tmp4->sin_addr, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
- break;
- default:
- return HICN_LIB_ERROR_UNEXPECTED;
+ memcpy (&tmp4->sin_addr, name->prefix.ip4.as_u8, IPV4_ADDR_LEN);
+ }
+ else
+ {
+ tmp6->sin6_family = AF_INET6;
+ tmp6->sin6_port = DUMMY_PORT;
+ memcpy (&tmp6->sin6_addr, name->prefix.ip6.as_u8, IPV6_ADDR_LEN);
}
return HICN_LIB_ERROR_NONE;
@@ -392,63 +133,14 @@ hicn_name_to_sockaddr_address (const hicn_name_t * name,
int
hicn_name_to_ip_prefix (const hicn_name_t * name, ip_prefix_t * prefix)
{
- switch (name->type)
- {
- case HNT_CONTIGUOUS_V6:
- memcpy (&prefix->address.v6.buffer, name->buffer, IPV6_ADDR_LEN);
- prefix->family = AF_INET6;
- break;
- case HNT_IOV_V6:
- memcpy (&prefix->address.v6.buffer, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
- prefix->family = AF_INET6;
- break;
- case HNT_CONTIGUOUS_V4:
- memcpy (&prefix->address.v4.buffer, name->buffer, IPV4_ADDR_LEN);
- prefix->family = AF_INET;
- break;
- case HNT_IOV_V4:
- memcpy (&prefix->address.v4.buffer, name->iov.buffers[0].iov_base,
- name->iov.buffers[0].iov_len);
- prefix->family = AF_INET;
- break;
- default:
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
+ memcpy (prefix, &name->prefix, sizeof(*prefix));
return HICN_LIB_ERROR_NONE;
}
int
hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number)
{
- const u8 *sequence_number = NULL;
-
- switch (name->type)
- {
- case HNT_CONTIGUOUS_V6:
- sequence_number = name->buffer + IPV6_ADDR_LEN;
- break;
- case HNT_CONTIGUOUS_V4:
- sequence_number = name->buffer + IPV4_ADDR_LEN;
- break;
- case HNT_IOV_V6:
- case HNT_IOV_V4:
- sequence_number = name->iov.buffers[1].iov_base;
- break;
- default:
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
- if (sequence_number)
- {
- *seq_number = *(u32 *) sequence_number;
- }
- else
- {
- return HICN_LIB_ERROR_UNEXPECTED;
- }
-
+ *seq_number = name->suffix;
return HICN_LIB_ERROR_NONE;
}
@@ -457,29 +149,12 @@ hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len)
{
int offset;
const char *rc;
- void *seg_number = NULL;
+ u8 is_ip4 = hicn_name_is_ip4 (src);
- switch (src->type)
- {
- case HNT_CONTIGUOUS_V6:
- rc = inet_ntop (AF_INET6, src->buffer, dst, (socklen_t)len);
- seg_number = (u8 *) src->buffer + IPV6_ADDR_LEN;
- break;
- case HNT_CONTIGUOUS_V4:
- rc = inet_ntop (AF_INET, src->buffer, dst, (socklen_t)len);
- seg_number = (u8 *) src->buffer + IPV4_ADDR_LEN;
- break;
- case HNT_IOV_V6:
- rc = inet_ntop (AF_INET6, src->iov.buffers[0].iov_base, dst, (socklen_t)len);
- seg_number = src->iov.buffers[1].iov_base;
- break;
- case HNT_IOV_V4:
- rc = inet_ntop (AF_INET, src->iov.buffers[0].iov_base, dst, (socklen_t)len);
- seg_number = src->iov.buffers[1].iov_base;
- break;
- default:
- return HICN_LIB_ERROR_NOT_IMPLEMENTED;
- }
+ if (is_ip4)
+ rc = inet_ntop (AF_INET, (struct in_addr*)(src->prefix.ip4.as_u8), dst, (socklen_t)len);
+ else
+ rc = inet_ntop (AF_INET6, (struct in6_addr*)(src->prefix.ip6.as_u8), dst, (socklen_t)len);
if (!rc)
{
@@ -489,8 +164,7 @@ hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len)
offset = (int) strlen (dst);
dst[offset] = '|';
- sprintf (dst + offset + 1, "%lu", (unsigned long) (*(u32 *) seg_number));
-
+ sprintf (dst + offset + 1, "%lu", (unsigned long) src->suffix);
return HICN_LIB_ERROR_NONE;
ERR:
@@ -506,19 +180,8 @@ hicn_name_pton (const char *src, hicn_name_t * dst)
int
hicn_name_get_family (const hicn_name_t * name, int *family)
{
- switch (name->type)
- {
- case HNT_CONTIGUOUS_V6:
- case HNT_IOV_V6:
- *family = AF_INET6;
- break;
- case HNT_CONTIGUOUS_V4:
- case HNT_IOV_V4:
- *family = AF_INET;
- break;
- default:
- return HICN_LIB_ERROR_UNEXPECTED;
- }
+ u8 is_ip4 = hicn_name_is_ip4 (name);
+ *family = AF_INET * is_ip4 + (1 - is_ip4) * AF_INET6;
return HICN_LIB_ERROR_NONE;
}
@@ -539,12 +202,32 @@ hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix,
default:
return HICN_LIB_ERROR_INVALID_IP_ADDRESS;
}
+
prefix->len = (u8) (ip_prefix->len);
return HICN_LIB_ERROR_NONE;
}
-#endif /* ! HICN_VPP_PLUGIN */
+int
+hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id,
+ hicn_name_t * name)
+{
+ int i;
+
+ for (i = 0; i < 2; i++)
+ name->prefix.ip6.as_u64[i] = prefix->address.v6.as_u64[i];
+ name->suffix = id;
+
+ return HICN_LIB_ERROR_NONE;
+}
+
+int
+hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2,
+ bool consider_segment)
+{
+ size_t size = (u8)consider_segment * sizeof(hicn_name_suffix_t) + IPV6_ADDR_LEN;
+ return memcmp (name_1, name_2, size);
+}
/*