From 1fc9a18f95fd2ff491679e4c8de519afe49d8c47 Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 20 Apr 2020 12:55:28 +0200 Subject: [HICN-598] [HICN-599] Fix hicn_name_t definition conflicts. Change-Id: Ica8db44e27c3a4911ea869e91f96b781809373d8 Signed-off-by: Mauro Sardara --- hicn-plugin/src/pg.c | 12 +- lib/includes/hicn/common.h | 1 + lib/includes/hicn/name.h | 82 +--- lib/src/compat.c | 5 - lib/src/name.c | 435 +++------------------- lib/src/protocol/ipv4.c | 24 +- lib/src/protocol/ipv6.c | 24 +- libtransport/includes/hicn/transport/core/name.h | 9 +- libtransport/src/core/name.cc | 41 +- libtransport/src/implementation/socket_producer.h | 8 +- 10 files changed, 107 insertions(+), 534 deletions(-) diff --git a/hicn-plugin/src/pg.c b/hicn-plugin/src/pg.c index 9938e85ba..eb833030d 100644 --- a/hicn-plugin/src/pg.c +++ b/hicn-plugin/src/pg.c @@ -404,12 +404,12 @@ hicn_rewrite_interestv4 (vlib_main_t * vm, vlib_buffer_t * b0, u32 seq_number, .ip4 = hicnpg_main.pgen_clt_src_addr.ip4, }; hicn_name_t dst_name = { - .ip4.prefix_as_ip4 = hicnpg_main.pgen_clt_hicn_name->fp_addr.ip4, - .ip4.suffix = seq_number, + .prefix.ip4 = hicnpg_main.pgen_clt_hicn_name->fp_addr.ip4, + .suffix = seq_number, }; src_addr.ip4.as_u32 += clib_host_to_net_u32 (iface); - dst_name.ip4.prefix_as_ip4.as_u32 += clib_net_to_host_u32 (next_flow); + dst_name.prefix.ip4.as_u32 += clib_net_to_host_u32 (next_flow); /* Update locator and name */ hicn_type_t type = hicn_get_buffer (b0)->type; @@ -450,11 +450,11 @@ hicn_rewrite_interestv6 (vlib_main_t * vm, vlib_buffer_t * b0, u32 seq_number, .ip6 = hicnpg_main.pgen_clt_src_addr.ip6, }; hicn_name_t dst_name = { - .ip6.prefix_as_ip6 = hicnpg_main.pgen_clt_hicn_name->fp_addr.ip6, - .ip6.suffix = seq_number, + .prefix.ip6 = hicnpg_main.pgen_clt_hicn_name->fp_addr.ip6, + .suffix = seq_number, }; src_addr.ip6.as_u32[3] += clib_host_to_net_u32 (iface); - dst_name.ip6.prefix_as_ip6.as_u32[3] += clib_net_to_host_u32 (next_flow); + dst_name.prefix.ip6.as_u32[3] += clib_net_to_host_u32 (next_flow); /* Update locator and name */ hicn_type_t type = hicn_get_buffer (b0)->type; diff --git a/lib/includes/hicn/common.h b/lib/includes/hicn/common.h index 30f370241..3385b4d19 100644 --- a/lib/includes/hicn/common.h +++ b/lib/includes/hicn/common.h @@ -154,6 +154,7 @@ struct iovec typedef union { + u8 as_u8[4]; u32 as_u32; struct in_addr as_inaddr; } ip4_address_t; diff --git a/lib/includes/hicn/name.h b/lib/includes/hicn/name.h index d5202068b..9cee0bc1d 100644 --- a/lib/includes/hicn/name.h +++ b/lib/includes/hicn/name.h @@ -62,81 +62,18 @@ 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; - }; + ip46_address_t prefix; 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 struct -{ - struct iovec buffers[HICN_NAME_COMPONENT_SIZE]; -} hicn_iov_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 - -typedef enum -{ - 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 */ - -typedef struct -{ -#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; -#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 */ +always_inline +int hicn_name_is_ip4 (const hicn_name_t * name) +{ + const ip46_address_t *ip46 = &name->prefix; + return (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0); +} /** * @brief Create an hICN name from IP address in presentation format @@ -192,7 +129,10 @@ 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); +always_inline int hicn_name_empty (hicn_name_t * name) +{ + return ((name->prefix.ip6.as_u64[0] | name->prefix.ip6.as_u64[1] | (u64)name->suffix) == 0); +} /** * @brief Copy an hICN name diff --git a/lib/src/compat.c b/lib/src/compat.c index 615175e3b..db2f40e37 100644 --- a/lib/src/compat.c +++ b/lib/src/compat.c @@ -255,11 +255,6 @@ hicn_packet_set_name (hicn_format_t format, hicn_header_t * h, { hicn_type_t type = hicn_format_to_type (format); -#ifndef HICN_VPP_PLUGIN - if (name->type & HNT_IOV) - return HICN_LIB_ERROR_NOT_IMPLEMENTED; -#endif /* HICN_VPP_PLUGIN */ - if (is_interest) return hicn_ops_vft[type.l1]->set_interest_name (type, &h->protocol, name); 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 #include -#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); +} /* diff --git a/lib/src/protocol/ipv4.c b/lib/src/protocol/ipv4.c index 781907231..5ad4ddbfa 100644 --- a/lib/src/protocol/ipv4.c +++ b/lib/src/protocol/ipv4.c @@ -78,20 +78,16 @@ int ipv4_get_interest_name (hicn_type_t type, const hicn_protocol_t * h, hicn_name_t * name) { - name->ip4.prefix_as_ip4 = h->ipv4.daddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V4; - name->len = HICN_V4_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_interest_name_suffix, type, h, &(name->ip4.suffix)); + name->prefix.ip4.as_u32 = h->ipv4.daddr.as_u32; + return CHILD_OPS (get_interest_name_suffix, type, h, &(name->suffix)); } int ipv4_set_interest_name (hicn_type_t type, hicn_protocol_t * h, const hicn_name_t * name) { - h->ipv4.daddr = name->ip4.prefix_as_ip4; - return CHILD_OPS (set_interest_name_suffix, type, h, &(name->ip4.suffix)); + h->ipv4.daddr.as_u32 = name->prefix.ip4.as_u32; + return CHILD_OPS (set_interest_name_suffix, type, h, &(name->suffix)); } int @@ -149,20 +145,16 @@ int ipv4_get_data_name (hicn_type_t type, const hicn_protocol_t * h, hicn_name_t * name) { - name->ip4.prefix_as_ip4 = h->ipv4.saddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V4; - name->len = HICN_V4_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_data_name_suffix, type, h, &(name->ip4.suffix)); + name->prefix.ip4.as_u32 = h->ipv4.saddr.as_u32; + return CHILD_OPS (get_data_name_suffix, type, h, &(name->suffix)); } int ipv4_set_data_name (hicn_type_t type, hicn_protocol_t * h, const hicn_name_t * name) { - h->ipv4.saddr = name->ip4.prefix_as_ip4; - return CHILD_OPS (set_data_name_suffix, type, h, &(name->ip4.suffix)); + h->ipv4.saddr.as_u32 = name->prefix.ip4.as_u32; + return CHILD_OPS (set_data_name_suffix, type, h, &(name->suffix)); } int diff --git a/lib/src/protocol/ipv6.c b/lib/src/protocol/ipv6.c index f23b01cd8..9db3e3a17 100644 --- a/lib/src/protocol/ipv6.c +++ b/lib/src/protocol/ipv6.c @@ -68,20 +68,16 @@ int ipv6_get_interest_name (hicn_type_t type, const hicn_protocol_t * h, hicn_name_t * name) { - name->ip6.prefix_as_ip6 = h->ipv6.daddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V6; - name->len = HICN_V6_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_interest_name_suffix, type, h, &(name->ip6.suffix)); + name->prefix.ip6 = h->ipv6.daddr; + return CHILD_OPS (get_interest_name_suffix, type, h, &(name->suffix)); } int ipv6_set_interest_name (hicn_type_t type, hicn_protocol_t * h, const hicn_name_t * name) { - h->ipv6.daddr = name->ip6.prefix_as_ip6; - return CHILD_OPS (set_interest_name_suffix, type, h, &(name->ip6.suffix)); + h->ipv6.daddr = name->prefix.ip6; + return CHILD_OPS (set_interest_name_suffix, type, h, &(name->suffix)); } int @@ -139,20 +135,16 @@ int ipv6_get_data_name (hicn_type_t type, const hicn_protocol_t * h, hicn_name_t * name) { - name->ip6.prefix_as_ip6 = h->ipv6.saddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V6; - name->len = HICN_V6_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_data_name_suffix, type, h, &(name->ip6.suffix)); + name->prefix.ip6 = h->ipv6.saddr; + return CHILD_OPS (get_data_name_suffix, type, h, &(name->suffix)); } int ipv6_set_data_name (hicn_type_t type, hicn_protocol_t * h, const hicn_name_t * name) { - h->ipv6.saddr = name->ip6.prefix_as_ip6; - return CHILD_OPS (set_data_name_suffix, type, h, &(name->ip6.suffix)); + h->ipv6.saddr = name->prefix.ip6; + return CHILD_OPS (set_data_name_suffix, type, h, &(name->suffix)); } int diff --git a/libtransport/includes/hicn/transport/core/name.h b/libtransport/includes/hicn/transport/core/name.h index ea72797ad..46bbd107d 100644 --- a/libtransport/includes/hicn/transport/core/name.h +++ b/libtransport/includes/hicn/transport/core/name.h @@ -51,7 +51,6 @@ class Name { public: using NameStruct = hicn_name_t; - using Type = hicn_name_type_t; Name(); @@ -81,12 +80,12 @@ class Name { bool equals(const Name &name, bool consider_segment = true) const; + TRANSPORT_ALWAYS_INLINE bool isIp4() { return hicn_name_is_ip4(&name_); } + uint32_t getHash32(bool consider_suffix = true) const; void clear(); - Type getType() const; - uint32_t getSuffix() const; std::shared_ptr getAddress() const; @@ -125,14 +124,14 @@ struct compare2 {}; template <> struct compare2 { - size_t operator()(const transport::core::Name &name1, const transport::core::Name &name2) const; + size_t operator()(const transport::core::Name &name1, + const transport::core::Name &name2) const; }; } // end namespace core } // end namespace transport - namespace std { template <> struct hash { diff --git a/libtransport/src/core/name.cc b/libtransport/src/core/name.cc index 811e93b87..3455460fc 100644 --- a/libtransport/src/core/name.cc +++ b/libtransport/src/core/name.cc @@ -13,14 +13,13 @@ * limitations under the License. */ +#include #include #include #include #include #include -#include - namespace transport { namespace core { @@ -29,28 +28,19 @@ Name::Name() { name_ = {}; } Name::Name(int family, const uint8_t *ip_address, std::uint32_t suffix) : name_({}) { - name_.type = HNT_UNSPEC; - std::size_t length; - uint8_t *dst = NULL; - if (family == AF_INET) { - dst = name_.ip4.prefix_as_u8; - length = IPV4_ADDR_LEN; - name_.type = HNT_CONTIGUOUS_V4; + name_.prefix.ip4.as_u32 = *(u32 *)(ip_address); } else if (family == AF_INET6) { - dst = name_.ip6.prefix_as_u8; - length = IPV6_ADDR_LEN; - name_.type = HNT_CONTIGUOUS_V6; + std::memcpy(&name_.prefix.ip6.as_u64[0], ip_address, IPV6_ADDR_LEN); } else { throw errors::RuntimeException("Specified name family does not exist."); } - std::memcpy(dst, ip_address, length); - *reinterpret_cast(dst + length) = suffix; + name_.suffix = suffix; } Name::Name(const char *name, uint32_t segment) { - name_.type = HNT_UNSPEC; + name_ = {}; if (hicn_name_create(name, segment, &name_) < 0) { throw errors::InvalidIpAddressException(); } @@ -60,7 +50,7 @@ Name::Name(const std::string &uri, uint32_t segment) : Name(uri.c_str(), segment) {} Name::Name(const std::string &uri) { - name_.type = HNT_UNSPEC; + name_ = {}; utils::StringTokenizer tokenizer(uri, "|"); std::string ip_address; std::string seq_number; @@ -125,9 +115,7 @@ uint32_t Name::getHash32(bool consider_suffix) const { return hash; } -void Name::clear() { name_.type = HNT_UNSPEC; }; - -Name::Type Name::getType() const { return name_.type; } +void Name::clear() { name_ = {}; }; uint32_t Name::getSuffix() const { uint32_t ret = 0; @@ -148,20 +136,7 @@ Name &Name::setSuffix(uint32_t seq_number) { } std::shared_ptr Name::getAddress() const { - Sockaddr *ret = nullptr; - - switch (name_.type) { - case HNT_CONTIGUOUS_V4: - case HNT_IOV_V4: - ret = (Sockaddr *)new Sockaddr4; - break; - case HNT_CONTIGUOUS_V6: - case HNT_IOV_V6: - ret = (Sockaddr *)new Sockaddr6; - break; - default: - throw errors::MalformedNameException(); - } + Sockaddr *ret = (Sockaddr *)(new sockaddr_storage()); if (hicn_name_to_sockaddr_address((hicn_name_t *)&name_, ret) < 0) { throw errors::MalformedNameException(); diff --git a/libtransport/src/implementation/socket_producer.h b/libtransport/src/implementation/socket_producer.h index a6f0f969e..574723607 100644 --- a/libtransport/src/implementation/socket_producer.h +++ b/libtransport/src/implementation/socket_producer.h @@ -134,16 +134,12 @@ class ProducerSocket : public Socket, core::Packet::Format hf_format = core::Packet::Format::HF_UNSPEC; core::Packet::Format hf_format_ah = core::Packet::Format::HF_UNSPEC; - if (content_name.getType() == HNT_CONTIGUOUS_V4 || - content_name.getType() == HNT_IOV_V4) { + if (content_name.isIp4()) { hf_format = core::Packet::Format::HF_INET_TCP; hf_format_ah = core::Packet::Format::HF_INET_TCP_AH; - } else if (content_name.getType() == HNT_CONTIGUOUS_V6 || - content_name.getType() == HNT_IOV_V6) { + } else { hf_format = core::Packet::Format::HF_INET6_TCP; hf_format_ah = core::Packet::Format::HF_INET6_TCP_AH; - } else { - throw errors::RuntimeException("Unknown name format."); } format = hf_format; -- cgit 1.2.3-korg