diff options
Diffstat (limited to 'lib/src')
52 files changed, 9260 insertions, 2813 deletions
diff --git a/lib/src/CMakeLists.txt b/lib/src/CMakeLists.txt index 2588bde8b..27446a493 100644 --- a/lib/src/CMakeLists.txt +++ b/lib/src/CMakeLists.txt @@ -1,4 +1,4 @@ -# 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: @@ -11,53 +11,124 @@ # See the License for the specific language governing permissions and # limitations under the License. +############################################################## +# Source and Header files +############################################################## list(APPEND LIBHICN_SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/common.c - ${CMAKE_CURRENT_SOURCE_DIR}/compat.c - ${CMAKE_CURRENT_SOURCE_DIR}/error.c - ${CMAKE_CURRENT_SOURCE_DIR}/mapme.c - ${CMAKE_CURRENT_SOURCE_DIR}/name.c - ${CMAKE_CURRENT_SOURCE_DIR}/ops.c - ${CMAKE_CURRENT_SOURCE_DIR}/policy.c - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ah.c - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/icmp.c - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ipv4.c - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ipv6.c - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/tcp.c - ${CMAKE_CURRENT_SOURCE_DIR}/util/ip_address.c - ${CMAKE_CURRENT_SOURCE_DIR}/util/log.c + base.c + common.c + error.c + face.c + mapme.c + name.c + ops.c + packet.c + policy.c + strategy.c + protocol/ah.c + protocol/icmp.c + protocol/ipv4.c + protocol/ipv6.c + protocol/tcp.c + protocol/udp.c + protocol/new.c + util/ip_address.c + util/log.c + util/pool.c + util/ring.c + util/slab.c + util/types.c + util/vector.c ) if (WIN32) list(APPEND LIBHICN_SOURCE_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/util/windows/dlfcn.c + util/windows/dlfcn.c ) endif () -set (COMPILER_DEFINITIONS "-DWITH_MAPME") + +############################################################## +# Compiler definitions +############################################################## +set (COMPILER_DEFINITIONS + PUBLIC "-DWITH_MAPME" + PUBLIC "-DWITH_POLICY" +) include(BuildMacros) include(WindowsMacros) -if (DISABLE_SHARED_LIBRARIES) - build_library(${LIBHICN} - STATIC - SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} - COMPONENT lib${LIBHICN} - INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../includes - DEFINITIONS ${COMPILER_DEFINITIONS} - HEADER_ROOT_DIR hicn - INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} - LINK_LIBRARIES ${WSOCK32_LIBRARY} ${WS2_32_LIBRARY} +############################################################## +# Build type +############################################################## +set (BUILD_TYPES "STATIC") + +if (NOT DISABLE_SHARED_LIBRARIES) + list(APPEND BUILD_TYPES + "SHARED" ) -else () - build_library(${LIBHICN} - SHARED STATIC - SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} - COMPONENT lib${LIBHICN} - INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../includes - DEFINITIONS ${COMPILER_DEFINITIONS} - HEADER_ROOT_DIR hicn - INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} - LINK_LIBRARIES ${WSOCK32_LIBRARY} ${WS2_32_LIBRARY} +endif() + + +############################################################## +# Compiler Options +############################################################## +set(COMPILER_OPTIONS + ${DEFAULT_COMPILER_OPTIONS} + PRIVATE "-Wno-address-of-packed-member" +) + + +############################################################## +# Link libraries +############################################################## +if (WIN32) + list(APPEND LIBRARIES + PRIVATE ${WSOCK32_LIBRARY} + PRIVATE ${WS2_32_LIBRARY} ) endif () + + +############################################################## +# Build library +############################################################## +build_library(${LIBHICN} + ${BUILD_TYPES} + SOURCES ${LIBHICN_SOURCE_FILES} + COMPONENT ${LIBHICN_COMPONENT} + INCLUDE_DIRS + PUBLIC + $<BUILD_INTERFACE:${Libhicn_INCLUDE_DIRS}> + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> + $<BUILD_INTERFACE:${THIRD_PARTY_INCLUDE_DIRS}> + DEFINITIONS ${COMPILER_DEFINITIONS} + INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} + DEPENDS ${THIRD_PARTY_DEPENDENCIES} + LINK_LIBRARIES + PRIVATE ${WSOCK32_LIBRARY} + PRIVATE ${WS2_32_LIBRARY} + PUBLIC ${THIRD_PARTY_LIBRARIES} + VERSION ${CURRENT_VERSION} + EXPORT_NAME ${LIBHICN_COMPONENT} + COMPILE_OPTIONS ${COMPILER_OPTIONS} +) + +############################################################## +# Unit tests +############################################################## +if (${BUILD_TESTS}) + add_subdirectory(test) +endif() + + +############################################################## +# Cmake config files +############################################################## +create_cmake_config ( + ${LIBHICN_COMPONENT} + INCLUDE_DIRS ${HICN_INCLUDE_DIRS} + VERSION ${CURRENT_VERSION} + COMPONENT ${LIBHICN_COMPONENT} + NAMESPACE hicn +) diff --git a/lib/src/base.c b/lib/src/base.c new file mode 100644 index 000000000..1852666a6 --- /dev/null +++ b/lib/src/base.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file base.c + * @brief Implementation of base hICN definitions. + */ + +#include <hicn/base.h> +#include "ops.h" + +const char *_hicn_packet_type_str[] = { +#define _(x) [HICN_PACKET_TYPE_##x] = #x, + foreach_packet_type +#undef _ +}; + +int +hicn_packet_format_snprintf (char *s, size_t size, hicn_packet_format_t format) +{ + char *cur = s; + int rc; + + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + if (i > 0) + { + rc = snprintf (cur, size - (cur - s), "%s", "/"); + if (rc < 0 || rc >= size - (cur - s)) + return rc; + cur += rc; + } + + rc = snprintf (cur, size - (cur - s), "%s", hicn_ops_vft[protocol]->name); + if (rc < 0 || rc >= size - (cur - s)) + return rc; + cur += rc; + }); + return (int) (cur - s); +} diff --git a/lib/src/common.c b/lib/src/common.c index 562771e09..362034942 100644 --- a/lib/src/common.c +++ b/lib/src/common.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -20,8 +20,8 @@ */ #include <stdlib.h> -#include <string.h> // memset -#include <sys/types.h> // getaddrinfo +#include <string.h> // memset +#include <sys/types.h> // getaddrinfo #ifndef _WIN32 #include <sys/socket.h> #include <netdb.h> @@ -31,8 +31,6 @@ #include <hicn/common.h> #include <hicn/util/log.h> - - int get_addr_family (const char *ip_address) { @@ -56,61 +54,48 @@ get_addr_family (const char *ip_address) /* hashes */ -u32 -cumulative_hash32 (const void *data, size_t len, u32 lastValue) -{ - // Standard FNV 32-bit prime: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param - const u32 fnv1a_prime = 0x01000193; - u32 hash = lastValue; - size_t i; +// FNV-1a 32-bit http://www.isthe.com/chongo/tech/comp/fnv/ +typedef u_int32_t Fnv32_t; +#define FNV_32_PRIME ((Fnv32_t) 0x01000193) +#define FNV1_32_INIT ((Fnv32_t) 0x811c9dc5) +#define FNV1_32A_INIT FNV1_32_INIT - const char *chardata = data; +Fnv32_t +cumulative_hash32 (const void *buf, size_t len, Fnv32_t hval) +{ + unsigned char *bp = (unsigned char *) buf; /* start of buffer */ + unsigned char *be = bp + len; /* beyond end of buffer */ - for (i = 0; i < len; i++) + /* + * FNV-1a hash each octet in the buffer + */ + while (bp < be) { - hash = hash ^ chardata[i]; - hash = hash * fnv1a_prime; - } - - return hash; -} - -u32 -hash32 (const void *data, size_t len) -{ - // Standard FNV 32-bit offset: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param - const u32 fnv1a_offset = 0x811C9DC5; - return cumulative_hash32 (data, len, fnv1a_offset); -} -u64 -cumulative_hash64 (const void *data, size_t len, u64 lastValue) -{ - // Standard FNV 64-bit prime: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param - const u64 fnv1a_prime = 0x00000100000001B3ULL; - u64 hash = lastValue; - const char *chardata = data; - size_t i; + /* xor the bottom with the current octet */ + hval ^= (Fnv32_t) *bp++; - for (i = 0; i < len; i++) - { - hash = hash ^ chardata[i]; - hash = hash * fnv1a_prime; + /* multiply by the 32 bit FNV magic prime mod 2^32 */ +#if defined(NO_FNV_GCC_OPTIMIZATION) + hval *= FNV_32_PRIME; +#else + hval += + (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); +#endif } - return hash; + /* return our new hash value */ + return hval; } -u64 -hash64 (const void *data, size_t len) +u32 +hash32 (const void *data, size_t len) { - // Standard FNV 64-bit offset: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param - const u64 fnv1a_offset = 0xCBF29CE484222325ULL; - return cumulative_hash64 (data, len, fnv1a_offset); + return cumulative_hash32 (data, len, FNV1_32A_INIT); } void -hicn_packet_dump (const uint8_t * buffer, size_t len) +hicn_packet_dump (const uint8_t *buffer, size_t len) { int i; unsigned char buff[17]; diff --git a/lib/src/compat.c b/lib/src/compat.c deleted file mode 100644 index 932c11d9e..000000000 --- a/lib/src/compat.c +++ /dev/null @@ -1,1273 +0,0 @@ -/* - * Copyright (c) 2017-2019 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: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file compat.c - * @brief Implementation of the compatibility layer. - */ -#ifndef _WIN32 -#include <netinet/in.h> -#endif -#include <string.h> // memset -#include <stddef.h> // offsetof - -#include <hicn/common.h> -#include <hicn/compat.h> -#include <hicn/error.h> -#include <hicn/header.h> -#include <hicn/name.h> -#include <hicn/ops.h> - -#define member_size(type, member) sizeof (((type *) 0)->member) -#define ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a))) - -#define HICN_NAME_COMPONENT_SIZE 2 - -int -hicn_packet_get_format (const hicn_header_t *h, hicn_format_t *format) -{ - *format = HF_UNSPEC; - - switch (HICN_IP_VERSION (h)) - { - case 4: - switch (h->v4.ip.protocol) - { - case IPPROTO_TCP: - if (h->v4.tcp.flags & AH_FLAG) - *format = HF_INET_TCP_AH; - else - *format = HF_INET_TCP; - break; - case IPPROTO_ICMP: - *format = HF_INET_ICMP; - break; - default: - return HICN_LIB_ERROR_NOT_HICN; - } - break; - case 6: - switch (h->v6.ip.nxt) - { - case IPPROTO_TCP: - if (h->v6.tcp.flags & AH_FLAG) - *format = HF_INET6_TCP_AH; - else - *format = HF_INET6_TCP; - break; - case IPPROTO_ICMPV6: - *format = HF_INET6_ICMP; - break; - default: - return HICN_LIB_ERROR_NOT_HICN; - } - break; - default: - return HICN_LIB_ERROR_NOT_HICN; - } - - return HICN_LIB_ERROR_NONE; -} - -/** - * @brief Convert (former) hICN format into (newer) hICN type - * @param [in] format - hICN format - * @return hICN type, all zero'ed if type is unknown - */ -hicn_type_t -hicn_format_to_type (hicn_format_t format) -{ - switch (format) - { - case HF_INET_TCP: - return HICN_TYPE_IPV4_TCP; - case HF_INET6_TCP: - return HICN_TYPE_IPV6_TCP; - case HF_INET_ICMP: - return HICN_TYPE_IPV4_ICMP; - case HF_INET6_ICMP: - return HICN_TYPE_IPV6_ICMP; - case HF_INET_TCP_AH: - return HICN_TYPE_IPV4_TCP_AH; - case HF_INET6_TCP_AH: - return HICN_TYPE_IPV6_TCP_AH; - case HF_INET_ICMP_AH: - return HICN_TYPE_IPV4_ICMP_AH; - case HF_INET6_ICMP_AH: - return HICN_TYPE_IPV6_ICMP_AH; - default: - break; - } - return HICN_TYPE_NONE; -} - -/** - * @brief Parse hICN header and return hICN type - * @param [in] h - hICN header - * @param [out] format - hICN type - * @return hICN error code - * - * This function is used to wrap old API calls to new ones - */ -hicn_type_t -hicn_header_to_type (const hicn_header_t *h) -{ - hicn_format_t format; - hicn_packet_get_format (h, &format); - return hicn_format_to_type (format); -} - -int -hicn_packet_init_header (hicn_format_t format, hicn_header_t *packet) -{ - hicn_type_t type = hicn_format_to_type (format); - - if (hicn_type_is_none (type)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - return hicn_ops_vft[type.l1]->init_packet_header (type, &packet->protocol); -} - -int -hicn_packet_compute_checksum (hicn_format_t format, hicn_header_t *h) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->update_checksums (type, &h->protocol, 0, ~0); -} - -int -hicn_packet_compute_header_checksum (hicn_format_t format, hicn_header_t *h, - u16 init_sum) -{ - hicn_type_t type = hicn_format_to_type (format); - /* payload_length == 0: ignore payload */ - return hicn_ops_vft[type.l1]->update_checksums (type, &h->protocol, init_sum, - 0); -} - -int -hicn_packet_check_integrity_no_payload (hicn_format_t format, hicn_header_t *h, - u16 init_sum) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->verify_checksums (type, &h->protocol, init_sum, - 0); -} - -int -hicn_packet_get_header_length_from_format (hicn_format_t format, - size_t *header_length) -{ - *header_length = _is_ipv4 (format) * IPV4_HDRLEN; - *header_length += _is_ipv6 (format) * IPV6_HDRLEN; - *header_length += _is_icmp (format) * ICMP_HDRLEN; - *header_length += _is_tcp (format) * TCP_HDRLEN; - *header_length += _is_ah (format) * AH_HDRLEN; - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_header_length (hicn_format_t format, const hicn_header_t *h, - size_t *header_length) -{ - hicn_packet_get_header_length_from_format (format, header_length); - int is_ah = _is_ah (format); - int is_ipv4 = _is_ipv4 (format); - int is_ipv6 = _is_ipv6 (format); - // The signature payload is expressed as number of 32 bits words - if (is_ah && is_ipv4) - *header_length += (h->v4ah.ah.payloadlen) << 2; - else if (is_ah && is_ipv6) - *header_length += (h->v6ah.ah.payloadlen) << 2; - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_payload_length (hicn_format_t format, const hicn_header_t *h, - size_t *payload_length) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_payload_length (type, &h->protocol, - payload_length); -} - -int -hicn_packet_set_payload_length (hicn_format_t format, hicn_header_t *h, - const size_t payload_length) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_payload_length (type, &h->protocol, - payload_length); -} - -int -hicn_packet_compare (const hicn_header_t *packet1, - const hicn_header_t *packet2) -{ - hicn_type_t type1 = hicn_header_to_type (packet1); - hicn_type_t type2 = hicn_header_to_type (packet2); - - size_t len1, len2; - int rc; - - if (type1.as_u32 != type2.as_u32) - return HICN_LIB_ERROR_UNEXPECTED; - - rc = hicn_ops_vft[type1.l1]->get_length (type1, &packet1->protocol, &len1); - if (PREDICT_FALSE (rc < 0)) - return HICN_LIB_ERROR_UNEXPECTED; - - rc = hicn_ops_vft[type2.l1]->get_length (type2, &packet2->protocol, &len2); - if (PREDICT_FALSE (rc < 0)) - return HICN_LIB_ERROR_UNEXPECTED; - - if (len1 != len2) - return HICN_LIB_ERROR_UNEXPECTED; - - return memcmp ((u8 *) packet1, (u8 *) packet2, len1); -} - -int -hicn_packet_get_name (hicn_format_t format, const hicn_header_t *h, - hicn_name_t *name, u8 is_interest) -{ - hicn_type_t type = hicn_format_to_type (format); - - if (is_interest) - return hicn_ops_vft[type.l1]->get_interest_name (type, &h->protocol, name); - else - return hicn_ops_vft[type.l1]->get_data_name (type, &h->protocol, name); -} - -int -hicn_packet_set_name (hicn_format_t format, hicn_header_t *h, - const hicn_name_t *name, u8 is_interest) -{ - 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); - else - return hicn_ops_vft[type.l1]->set_data_name (type, &h->protocol, name); -} - -int -hicn_packet_set_payload (hicn_format_t format, hicn_header_t *h, - const u8 *payload, u16 payload_length) -{ - hicn_type_t type = hicn_format_to_type (format); - size_t header_length; - int rc; - - rc = hicn_ops_vft[type.l1]->get_header_length (type, &h->protocol, - &header_length); - if (rc < 0) - return rc; - - memcpy ((u8 *) h + header_length, payload, payload_length); - - return hicn_ops_vft[type.l1]->set_payload_length (type, &h->protocol, - payload_length); -} - -int -hicn_packet_get_payload (hicn_format_t format, const hicn_header_t *h, - u8 **payload, size_t *payload_size, bool hard_copy) -{ - size_t header_length, payload_length; - int rc; - hicn_type_t type = hicn_format_to_type (format); - - rc = hicn_ops_vft[type.l1]->get_header_length (type, &h->protocol, - &header_length); - if (rc < 0) - return rc; - - rc = hicn_ops_vft[type.l1]->get_payload_length (type, &h->protocol, - &payload_length); - if (rc < 0) - return rc; - - if (hard_copy) - { - memcpy (payload, (u8 *) h + header_length, payload_length); - } - else - { - *payload = (u8 *) h + header_length; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_locator (hicn_format_t format, const hicn_header_t *h, - ip_address_t *address, bool is_interest) -{ - int is_ipv4 = (format & HFO_INET); - int is_ipv6 = (format & HFO_INET6) >> 1; - - if (is_ipv4) - { - address->v4.as_inaddr = - is_interest ? h->v4.ip.saddr.as_inaddr : h->v4.ip.daddr.as_inaddr; - } - else if (is_ipv6) - { - address->v6.as_in6addr = - is_interest ? h->v6.ip.saddr.as_in6addr : h->v6.ip.daddr.as_in6addr; - } - else - { - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_locator (hicn_format_t format, hicn_header_t *h, - const ip_address_t *address, bool is_interest) -{ - int is_ipv4 = (format & HFO_INET); - int is_ipv6 = (format & HFO_INET6) >> 1; - - if (is_ipv6) - { - if (is_interest) - h->v6.ip.saddr.as_in6addr = address->v6.as_in6addr; - else - h->v6.ip.daddr.as_in6addr = address->v6.as_in6addr; - } - else if (is_ipv4) - { - if (is_interest) - h->v4.ip.saddr.as_inaddr = address->v4.as_inaddr; - else - h->v4.ip.daddr.as_inaddr = address->v4.as_inaddr; - } - else - { - return HICN_LIB_ERROR_INVALID_PARAMETER; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_signature_size (hicn_format_t format, const hicn_header_t *h, - size_t *bytes) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_signature_size (type, &h->protocol, bytes); -} - -int -hicn_packet_set_signature_size (hicn_format_t format, hicn_header_t *h, - size_t bytes) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_signature_size (type, &h->protocol, bytes); -} - -int -hicn_packet_get_signature_gap (hicn_format_t format, const hicn_header_t *h, - uint8_t *bytes) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_signature_gap (type, &h->protocol, bytes); -} - -int -hicn_packet_set_signature_gap (hicn_format_t format, hicn_header_t *h, - uint8_t bytes) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_signature_gap (type, &h->protocol, bytes); -} - -int -hicn_packet_set_signature_timestamp (hicn_format_t format, hicn_header_t *h, - uint64_t signature_timestamp) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_signature_timestamp (type, &h->protocol, - signature_timestamp); -} - -int -hicn_packet_get_signature_timestamp (hicn_format_t format, - const hicn_header_t *h, - uint64_t *signature_timestamp) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_signature_timestamp (type, &h->protocol, - signature_timestamp); -} - -int -hicn_packet_set_validation_algorithm (hicn_format_t format, hicn_header_t *h, - uint8_t validation_algorithm) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_validation_algorithm ( - type, &h->protocol, validation_algorithm); -} - -int -hicn_packet_get_validation_algorithm (hicn_format_t format, - const hicn_header_t *h, - uint8_t *validation_algorithm) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_validation_algorithm ( - type, &h->protocol, validation_algorithm); -} - -int -hicn_packet_set_key_id (hicn_format_t format, hicn_header_t *h, - uint8_t *key_id) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->set_key_id (type, &h->protocol, key_id); -} - -int -hicn_packet_get_key_id (hicn_format_t format, hicn_header_t *h, - uint8_t **key_id, uint8_t *key_id_length) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_key_id (type, &h->protocol, key_id, - key_id_length); -} - -int -hicn_packet_get_hoplimit (const hicn_header_t *h, u8 *hops) -{ - switch (HICN_IP_VERSION (h)) - { - case 6: - *hops = h->v6.ip.hlim; - break; - case 4: - *hops = h->v4.ip.ttl; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_hoplimit (hicn_header_t *h, u8 hops) -{ - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.ip.hlim = hops; - break; - case 4: - h->v4.ip.ttl = hops; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_lifetime (const hicn_header_t *h, u32 *lifetime) -{ - hicn_type_t type = hicn_header_to_type (h); - return hicn_ops_vft[type.l1]->get_lifetime (type, &h->protocol, - (hicn_lifetime_t *) lifetime); -} - -int -hicn_packet_set_lifetime (hicn_header_t *h, u32 lifetime) -{ - hicn_type_t type = hicn_header_to_type (h); - return hicn_ops_vft[type.l1]->set_lifetime (type, &h->protocol, - (hicn_lifetime_t) lifetime); -} - -int -hicn_packet_get_reserved_bits (const hicn_header_t *h, u8 *reserved_bits) -{ - switch (HICN_IP_VERSION (h)) - { - case 6: - *reserved_bits = (u8) (h->v6.tcp.reserved); - break; - case 4: - *reserved_bits = (u8) (h->v4.tcp.reserved); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_reserved_bits (hicn_header_t *h, const u8 reserved_bits) -{ - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.reserved = reserved_bits; - break; - case 4: - h->v4.tcp.reserved = reserved_bits; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_payload_type (const hicn_header_t *h, - hicn_payload_type_t *payload_type) -{ - switch (HICN_IP_VERSION (h)) - { - case 6: - *payload_type = - ((h->v6.tcp.flags & HICN_TCP_FLAG_URG) == HICN_TCP_FLAG_URG); - break; - case 4: - *payload_type = - ((h->v4.tcp.flags & HICN_TCP_FLAG_URG) == HICN_TCP_FLAG_URG); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - if (*payload_type == HPT_UNSPEC) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_payload_type (hicn_header_t *h, - hicn_payload_type_t payload_type) -{ - if (payload_type != HPT_DATA && payload_type != HPT_MANIFEST) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - if (payload_type) - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_URG; - else - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_URG; - break; - case 4: - if (payload_type) - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_URG; - else - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_URG; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_syn (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_SYN; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_SYN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_reset_syn (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_SYN; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_SYN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_test_syn (hicn_format_t format, const hicn_header_t *h, bool *flag) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *flag = h->v6.tcp.flags & HICN_TCP_FLAG_SYN; - break; - case 4: - *flag = h->v4.tcp.flags & HICN_TCP_FLAG_SYN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_ack (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_ACK; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_ACK; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_reset_ack (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_ACK; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_ACK; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_test_ack (hicn_format_t format, const hicn_header_t *h, bool *flag) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *flag = h->v6.tcp.flags & HICN_TCP_FLAG_ACK; - break; - case 4: - *flag = h->v4.tcp.flags & HICN_TCP_FLAG_ACK; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_rst (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_RST; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_RST; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_reset_rst (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_RST; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_RST; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_test_rst (hicn_format_t format, const hicn_header_t *h, bool *flag) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *flag = h->v6.tcp.flags & HICN_TCP_FLAG_RST; - break; - case 4: - *flag = h->v4.tcp.flags & HICN_TCP_FLAG_RST; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_fin (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_FIN; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_FIN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_reset_fin (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_FIN; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_FIN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_test_fin (hicn_format_t format, const hicn_header_t *h, bool *flag) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *flag = h->v6.tcp.flags & HICN_TCP_FLAG_FIN; - break; - case 4: - *flag = h->v4.tcp.flags & HICN_TCP_FLAG_FIN; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_ece (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags | HICN_TCP_FLAG_ECE; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags | HICN_TCP_FLAG_ECE; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_reset_ece (hicn_format_t format, hicn_header_t *h) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.flags = h->v6.tcp.flags & ~HICN_TCP_FLAG_ECE; - break; - case 4: - h->v4.tcp.flags = h->v4.tcp.flags & ~HICN_TCP_FLAG_ECE; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_test_ece (hicn_format_t format, const hicn_header_t *h, bool *flag) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *flag = h->v6.tcp.flags & HICN_TCP_FLAG_ECE; - break; - case 4: - *flag = h->v4.tcp.flags & HICN_TCP_FLAG_ECE; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_src_port (hicn_format_t format, hicn_header_t *h, u16 src_port) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.sport = htons (src_port); - break; - case 4: - h->v4.tcp.sport = htons (src_port); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_src_port (hicn_format_t format, const hicn_header_t *h, - u16 *src_port) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *src_port = ntohs (h->v6.tcp.sport); - break; - case 4: - *src_port = ntohs (h->v4.tcp.sport); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_set_dst_port (hicn_format_t format, hicn_header_t *h, u16 dst_port) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - h->v6.tcp.dport = htons (dst_port); - break; - case 4: - h->v4.tcp.dport = htons (dst_port); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_get_dst_port (hicn_format_t format, const hicn_header_t *h, - u16 *dst_port) -{ - if (!_is_tcp (format)) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - switch (HICN_IP_VERSION (h)) - { - case 6: - *dst_port = ntohs (h->v6.tcp.dport); - break; - case 4: - *dst_port = ntohs (h->v4.tcp.dport); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - return HICN_LIB_ERROR_NONE; -} - -int -hicn_packet_copy_header (hicn_format_t format, const hicn_header_t *packet, - hicn_header_t *destination, bool copy_ah) -{ - size_t header_length = _is_ipv4 (format) * IPV4_HDRLEN; - header_length += _is_ipv6 (format) * IPV6_HDRLEN; - header_length += _is_icmp (format) * ICMP_HDRLEN; - header_length += _is_tcp (format) * TCP_HDRLEN; - header_length += _is_ah (format) * copy_ah * AH_HDRLEN; - - memcpy (destination, packet, header_length); - - return HICN_LIB_ERROR_NONE; -} - -#define _INTEREST 1 -#define _DATA 0 - -/* Interest */ - -int -hicn_interest_get_name (hicn_format_t format, const hicn_header_t *interest, - hicn_name_t *name) -{ - return hicn_packet_get_name (format, interest, name, _INTEREST); -} - -int -hicn_interest_set_name (hicn_format_t format, hicn_header_t *interest, - const hicn_name_t *name) -{ - int ret_err = hicn_packet_reset_ece ( - format, interest); // interest packet -> ece flag unset - if (ret_err < 0) - return HICN_LIB_ERROR_UNEXPECTED; - return hicn_packet_set_name (format, interest, name, _INTEREST); -} - -int -hicn_interest_get_locator (hicn_format_t format, const hicn_header_t *interest, - ip_address_t *address) -{ - return hicn_packet_get_locator (format, interest, address, _INTEREST); -} - -int -hicn_interest_set_locator (hicn_format_t format, hicn_header_t *interest, - const ip_address_t *address) -{ - return hicn_packet_set_locator (format, interest, address, _INTEREST); -} - -int -hicn_interest_compare (const hicn_header_t *interest_1, - const hicn_header_t *interest_2) -{ - return hicn_packet_compare (interest_1, interest_2); -} - -int -hicn_interest_get_lifetime (const hicn_header_t *interest, u32 *lifetime) -{ - return hicn_packet_get_lifetime (interest, lifetime); -} - -int -hicn_interest_set_lifetime (hicn_header_t *interest, u32 lifetime) -{ - return hicn_packet_set_lifetime (interest, lifetime); -} - -int -hicn_interest_get_header_length (hicn_format_t format, - const hicn_header_t *interest, - size_t *header_length) -{ - return hicn_packet_get_header_length (format, interest, header_length); -} - -int -hicn_interest_get_payload_length (hicn_format_t format, - const hicn_header_t *interest, - size_t *payload_length) -{ - return hicn_packet_get_payload_length (format, interest, payload_length); -} - -int -hicn_interest_get_payload (hicn_format_t format, const hicn_header_t *interest, - u8 **payload, size_t *payload_size, bool hard_copy) -{ - return hicn_packet_get_payload (format, interest, payload, payload_size, - hard_copy); -} - -int -hicn_interest_set_payload (hicn_format_t format, hicn_header_t *interest, - const u8 *payload, size_t payload_length) -{ - return hicn_packet_set_payload (format, interest, payload, - (u16) payload_length); -} - -int -hicn_interest_reset_for_hash (hicn_format_t format, hicn_header_t *packet) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->reset_interest_for_hash (type, - &packet->protocol); -} - -/* Data */ - -int -hicn_data_get_name (hicn_format_t format, const hicn_header_t *data, - hicn_name_t *name) -{ - return hicn_packet_get_name (format, data, name, _DATA); -} - -int -hicn_data_set_name (hicn_format_t format, hicn_header_t *data, - const hicn_name_t *name) -{ - int ret_err = - hicn_packet_set_ece (format, data); // data packet -> ece flag set - if (ret_err < 0) - return HICN_LIB_ERROR_UNEXPECTED; - return hicn_packet_set_name (format, data, name, _DATA); -} - -int -hicn_data_get_locator (hicn_format_t format, const hicn_header_t *data, - ip_address_t *address) -{ - return hicn_packet_get_locator (format, data, address, _DATA); -} - -int -hicn_data_set_locator (hicn_format_t format, hicn_header_t *data, - const ip_address_t *address) -{ - return hicn_packet_set_locator (format, data, address, _DATA); -} - -int -hicn_data_compare (const hicn_header_t *data_1, const hicn_header_t *data_2) -{ - return hicn_packet_compare (data_1, data_2); -} - -int -hicn_data_get_expiry_time (const hicn_header_t *data, u32 *expiry_time) -{ - return hicn_packet_get_lifetime (data, expiry_time); -} - -int -hicn_data_set_expiry_time (hicn_header_t *data, u32 expiry_time) -{ - return hicn_packet_set_lifetime (data, (hicn_lifetime_t) expiry_time); -} - -int -hicn_data_get_header_length (hicn_format_t format, hicn_header_t *data, - size_t *header_length) -{ - return hicn_packet_get_header_length (format, data, header_length); -} - -int -hicn_data_get_payload_length (hicn_format_t format, const hicn_header_t *data, - size_t *payload_length) -{ - return hicn_packet_get_payload_length (format, data, payload_length); -} - -int -hicn_data_set_payload_type (hicn_header_t *data, - hicn_payload_type_t payload_type) -{ - return hicn_packet_set_payload_type (data, payload_type); -} - -int -hicn_data_get_payload_type (const hicn_header_t *data, - hicn_payload_type_t *payload_type) -{ - return hicn_packet_get_payload_type (data, payload_type); -} - -int -hicn_data_get_path_label (const hicn_header_t *data, u32 *path_label) -{ - hicn_type_t type = hicn_header_to_type (data); - return hicn_ops_vft[type.l1]->get_data_pathlabel (type, &data->protocol, - path_label); -} - -int -hicn_data_set_path_label (hicn_header_t *data, u32 path_label) -{ - hicn_type_t type = hicn_header_to_type (data); - return hicn_ops_vft[type.l1]->set_data_pathlabel (type, &data->protocol, - path_label); -} - -int -hicn_data_set_payload (hicn_format_t format, hicn_header_t *data, - const u8 *payload, size_t payload_length) -{ - return hicn_packet_set_payload (format, data, payload, (u16) payload_length); -} - -int -hicn_data_get_payload (hicn_format_t format, const hicn_header_t *data, - u8 **payload, size_t *payload_size, bool hard_copy) -{ - return hicn_packet_get_payload (format, data, payload, payload_size, - hard_copy); -} - -int -hicn_data_reset_for_hash (hicn_format_t format, hicn_header_t *packet) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->reset_data_for_hash (type, &packet->protocol); -} - -int -hicn_packet_get_signature (hicn_format_t format, hicn_header_t *packet, - uint8_t **sign_buf) -{ - hicn_type_t type = hicn_format_to_type (format); - return hicn_ops_vft[type.l1]->get_signature (type, &packet->protocol, - sign_buf); -} - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/lib/src/error.c b/lib/src/error.c index ab5d6e27d..0d7c80d9b 100644 --- a/lib/src/error.c +++ b/lib/src/error.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -21,7 +21,7 @@ #include <hicn/error.h> const char *HICN_LIB_ERROR_STRING[] = { -#define _(a,b,c) [b] = c, +#define _(a, b, c) [b] = c, foreach_libhicn_error #undef _ }; diff --git a/lib/src/face.c b/lib/src/face.c new file mode 100644 index 000000000..81395c4b1 --- /dev/null +++ b/lib/src/face.c @@ -0,0 +1,455 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file face.c + * \brief Implementation of face abstraction + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <hicn/face.h> +#include <hicn/util/token.h> + +#define member_size(type, member) sizeof (((type *) 0)->member) + +/* Netdevice */ + +const char *_netdevice_type_str[] = { +#define _(x) [NETDEVICE_TYPE_##x] = STRINGIZE (x), + foreach_netdevice_type +#undef _ +}; + +netdevice_t * +netdevice_create_from_index (u32 index) +{ + netdevice_t *netdevice = malloc (sizeof (netdevice_t)); + if (!netdevice) + goto ERR_MALLOC; + + int rc = netdevice_set_index (netdevice, index); + if (rc < 0) + goto ERR_INIT; + + return netdevice; + +ERR_INIT: + free (netdevice); +ERR_MALLOC: + return NULL; +} + +netdevice_t * +netdevice_create_from_name (const char *name) +{ + netdevice_t *netdevice = malloc (sizeof (netdevice_t)); + if (!netdevice) + goto ERR_MALLOC; + + int rc = netdevice_set_name (netdevice, name); + if (rc < 0) + goto ERR_INIT; + + return netdevice; + +ERR_INIT: + free (netdevice); +ERR_MALLOC: + return NULL; +} + +/** + * \brief Update the index of the netdevice based on the name + */ +int +netdevice_update_index (netdevice_t *netdevice) +{ + netdevice->index = if_nametoindex (netdevice->name); + if (netdevice->index == 0) + return -1; + return 0; +} + +int +netdevice_update_name (netdevice_t *netdevice) +{ + if (!if_indextoname (netdevice->index, netdevice->name)) + return -1; + return 0; +} + +void +netdevice_free (netdevice_t *netdevice) +{ + free (netdevice); +} + +int +netdevice_get_index (const netdevice_t *netdevice, u32 *index) +{ + if (netdevice->index == 0) + return -1; + *index = netdevice->index; + return 0; +} + +int +netdevice_set_index (netdevice_t *netdevice, u32 index) +{ + netdevice->index = index; + return netdevice_update_name (netdevice); +} + +int +netdevice_get_name (const netdevice_t *netdevice, const char **name) +{ + if (netdevice->name[0] == '\0') + return -1; + *name = netdevice->name; + return 0; +} + +int +netdevice_set_name (netdevice_t *netdevice, const char *name) +{ + memset (netdevice->name, 0, sizeof (netdevice->name)); + int rc = snprintf (netdevice->name, IFNAMSIZ, "%s", name); + if (rc < 0) + return -1; + if (rc >= IFNAMSIZ) + return -2; /* truncated */ + return netdevice_update_index (netdevice); +} + +int +netdevice_cmp (const netdevice_t *nd1, const netdevice_t *nd2) +{ + return (nd1->index - nd2->index); +} + +bool +netdevice_is_empty (const netdevice_t *netdevice) +{ + return (netdevice->index != 0) || (netdevice->name[0] != '\0'); +} + +/* Face state */ + +const char *_face_state_str[] = { +#define _(x) [FACE_STATE_##x] = STRINGIZE (x), + foreach_face_state +#undef _ +}; + +/* Face type */ + +const char *_face_type_str[] = { +#define _(x) [FACE_TYPE_##x] = STRINGIZE (x), + foreach_face_type +#undef _ +}; + +face_type_t +face_type_from_str (const char *str) +{ +#define _(x) \ + if (strcasecmp (str, STRINGIZE (x)) == 0) \ + return FACE_TYPE_##x; \ + else + foreach_face_type +#undef _ + return FACE_TYPE_UNDEFINED; +} + +/* Face */ + +int +face_initialize (face_t *face) +{ + memset (face, 0, sizeof (face_t)); /* 0'ed for hash */ + return 1; +} + +int +face_initialize_udp (face_t *face, const char *interface_name, + const hicn_ip_address_t *local_addr, u16 local_port, + const hicn_ip_address_t *remote_addr, u16 remote_port, + int family) +{ + if (!local_addr) + return -1; + + *face = (face_t){ + .type = FACE_TYPE_UDP, + .family = family, + .local_addr = *local_addr, + .local_port = local_port, + .remote_addr = remote_addr ? *remote_addr : IP_ADDRESS_EMPTY, + .remote_port = remote_port, + }; + + snprintf (face->netdevice.name, IFNAMSIZ, "%s", interface_name); + + return 1; +} + +int +face_initialize_udp_sa (face_t *face, const char *interface_name, + const struct sockaddr *local_addr, + const struct sockaddr *remote_addr) +{ + if (!local_addr) + return -1; + + if (remote_addr && (local_addr->sa_family != remote_addr->sa_family)) + return -1; + + switch (local_addr->sa_family) + { + case AF_INET: + { + struct sockaddr_in *lsai = (struct sockaddr_in *) local_addr; + struct sockaddr_in *rsai = (struct sockaddr_in *) remote_addr; + *face = (face_t){ + .type = FACE_TYPE_UDP, + .family = AF_INET, + .local_addr.v4.as_inaddr = lsai->sin_addr, + .local_port = lsai ? ntohs (lsai->sin_port) : 0, + .remote_addr = IP_ADDRESS_EMPTY, + .remote_port = rsai ? ntohs (rsai->sin_port) : 0, + }; + if (rsai) + face->remote_addr.v4.as_inaddr = rsai->sin_addr; + } + break; + case AF_INET6: + { + struct sockaddr_in6 *lsai = (struct sockaddr_in6 *) local_addr; + struct sockaddr_in6 *rsai = (struct sockaddr_in6 *) remote_addr; + *face = (face_t){ + .type = FACE_TYPE_UDP, + .family = AF_INET6, + .local_addr.v6.as_in6addr = lsai->sin6_addr, + .local_port = lsai ? ntohs (lsai->sin6_port) : 0, + .remote_addr = IP_ADDRESS_EMPTY, + .remote_port = rsai ? ntohs (rsai->sin6_port) : 0, + }; + if (rsai) + face->remote_addr.v6.as_in6addr = rsai->sin6_addr; + } + break; + default: + return -1; + } + + snprintf (face->netdevice.name, IFNAMSIZ, "%s", interface_name); + + return 1; +} + +face_t * +face_create () +{ + face_t *face = calloc (1, sizeof (face_t)); /* 0'ed for hash */ + return face; +} + +face_t * +face_create_udp (const char *interface_name, + const hicn_ip_address_t *local_addr, u16 local_port, + const hicn_ip_address_t *remote_addr, u16 remote_port, + int family) +{ + face_t *face = face_create (); + if (face_initialize_udp (face, interface_name, local_addr, local_port, + remote_addr, remote_port, family) < 0) + goto ERR_INIT; + return face; + +ERR_INIT: + free (face); + return NULL; +} + +face_t * +face_create_udp_sa (const char *interface_name, + const struct sockaddr *local_addr, + const struct sockaddr *remote_addr) +{ + face_t *face = face_create (); + if (face_initialize_udp_sa (face, interface_name, local_addr, remote_addr) < + 0) + goto ERR_INIT; + return face; + +ERR_INIT: + free (face); + return NULL; +} + +void +face_free (face_t *face) +{ + free (face); +} + +/** + * \brief Compare two faces + * \param [in] f1 - First face + * \param [in] f2 - Second face + * \return whether faces are equal, ie both their types are parameters are + * equal. + * + * NOTE: this function implements a partial order. + */ +int +face_cmp (const face_t *f1, const face_t *f2) +{ + + int ret = f1->type - f2->type; + if (ret != 0) + return ret; + + ret = f1->family - f2->family; + if (ret != 0) + return ret; + + /* + * FIXME As hicn-light API might not return the netdevice, we can discard the + * comparison when one of the two is not set for now... + */ + if ((f1->netdevice.index != 0) && (f2->netdevice.index != 0)) + { + ret = netdevice_cmp (&f1->netdevice, &f2->netdevice); + if (ret != 0) + return ret; + } + + switch (f1->type) + { + case FACE_TYPE_HICN: + ret = hicn_ip_address_cmp (&f1->local_addr, &f2->local_addr); + if (ret != 0) + return ret; + + ret = hicn_ip_address_cmp (&f1->remote_addr, &f2->remote_addr); + if (ret != 0) + return ret; + + break; + + case FACE_TYPE_TCP: + case FACE_TYPE_UDP: + ret = hicn_ip_address_cmp (&f1->local_addr, &f2->local_addr); + if (ret != 0) + return ret; + + ret = f1->local_port - f2->local_port; + if (ret != 0) + return ret; + + ret = hicn_ip_address_cmp (&f1->remote_addr, &f2->remote_addr); + if (ret != 0) + return ret; + + ret = f1->remote_port - f2->remote_port; + if (ret != 0) + return ret; + + break; + default: + break; + } + + return 0; +} + +/* /!\ Please update constants in header file upon changes */ +size_t +face_snprintf (char *s, size_t size, const face_t *face) +{ + char local[MAXSZ_IP_ADDRESS]; + char remote[MAXSZ_IP_ADDRESS]; + char tags[MAXSZ_POLICY_TAGS]; + + if (!hicn_ip_address_match_family (&face->local_addr, face->family)) + return 0; + if (!hicn_ip_address_match_family (&face->remote_addr, face->family)) + return 0; + + hicn_ip_address_snprintf (local, MAXSZ_IP_ADDRESS, &face->local_addr); + hicn_ip_address_snprintf (remote, MAXSZ_IP_ADDRESS, &face->remote_addr); + policy_tags_snprintf (tags, MAXSZ_POLICY_TAGS, face->tags); + + switch (face->type) + { + case FACE_TYPE_HICN: + { + + return snprintf (s, size, "%s [%s -> %s] [%s]", + face_type_str (face->type), local, remote, tags); + } + case FACE_TYPE_UNDEFINED: + case FACE_TYPE_TCP: + case FACE_TYPE_UDP: + { + return snprintf (s, size, "%s [%s:%d -> %s:%d] [%s]", + face_type_str (face->type), local, face->local_port, + remote, face->remote_port, tags); + } + default: + return -1; + } +} + +policy_tags_t +face_get_tags (const face_t *face) +{ + return face->tags; +} + +int +face_set_tags (face_t *face, policy_tags_t tags) +{ + face->tags = tags; + return 1; +} + +face_protocol_t +get_protocol (face_type_t face_type) +{ + switch (face_type) + { + case FACE_TYPE_HICN: + case FACE_TYPE_HICN_LISTENER: + return FACE_PROTOCOL_HICN; + + case FACE_TYPE_TCP: + case FACE_TYPE_TCP_LISTENER: + return FACE_PROTOCOL_TCP; + + case FACE_TYPE_UDP: + case FACE_TYPE_UDP_LISTENER: + return FACE_PROTOCOL_UDP; + + case FACE_TYPE_UNDEFINED: + case FACE_TYPE_N: + break; + } + return FACE_PROTOCOL_UNKNOWN; +} diff --git a/lib/src/libhicn-config.cmake.in b/lib/src/libhicn-config.cmake.in new file mode 100644 index 000000000..1ae219280 --- /dev/null +++ b/lib/src/libhicn-config.cmake.in @@ -0,0 +1,12 @@ +set(Libhicn_VERSION "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@") + +@PACKAGE_INIT@ + +set(Libhicn_VERSION_MAJOR "@VERSION_MAJOR@") +set(Libhicn_VERSION_MINOR "@VERSION_MINOR@") +set(Libhicn_VERSION_PATCH "@VERSION_PATCH@") + +set_and_check(Libhicn_INCLUDE_DIRS "@PACKAGE_Libhicn_INCLUDE_DIRS@") +include("${CMAKE_CURRENT_LIST_DIR}/libhicn-targets.cmake") + +check_required_components(Libhicn) diff --git a/lib/src/mapme.c b/lib/src/mapme.c index 3e7e8bc80..3f864e768 100644 --- a/lib/src/mapme.c +++ b/lib/src/mapme.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -22,12 +22,48 @@ #include <hicn/common.h> #include <hicn/error.h> -#include <hicn/protocol/ipv4.h> -#include <hicn/protocol/ipv6.h> +#include "protocol/ipv4.h" +#include "protocol/ipv6.h" +#include "protocol/icmprd.h" + +/** @brief MAP-Me packet header for IPv4 */ +typedef struct +{ + _ipv4_header_t ip; + _icmprd4_header_t icmp_rd; + seq_t seq; + u8 len; + u8 _pad[3]; +} hicn_mapme_v4_header_t; + +/** @brief MAP-Me packet header for IPv6 */ +typedef struct +{ + _ipv6_header_t ip; + _icmprd_header_t icmp_rd; + seq_t seq; + u8 len; + u8 _pad[3]; +} hicn_mapme_v6_header_t; + +/** @brief MAP-Me packet header (IP version agnostic) */ +typedef union +{ + hicn_mapme_v4_header_t v4; + hicn_mapme_v6_header_t v6; +} hicn_mapme_header_t; + +#define HICN_MAPME_V4_HDRLEN sizeof (hicn_mapme_v4_header_t) +#define HICN_MAPME_V6_HDRLEN sizeof (hicn_mapme_v6_header_t) + +static_assert (EXPECTED_MAPME_V4_HDRLEN == HICN_MAPME_V4_HDRLEN, + "Size of MAPME_V4 struct does not match its expected size."); +static_assert (EXPECTED_MAPME_V6_HDRLEN == HICN_MAPME_V6_HDRLEN, + "Size of MAPME_V6 struct does not match its expected size."); size_t -hicn_mapme_v4_create_packet (u8 * buf, const hicn_prefix_t * prefix, - const mapme_params_t * params) +hicn_mapme_v4_create_packet (u8 *buf, const hicn_prefix_t *prefix, + const mapme_params_t *params) { hicn_mapme_v4_header_t *mh = (hicn_mapme_v4_header_t *) buf; /* *INDENT-OFF* */ @@ -41,14 +77,14 @@ hicn_mapme_v4_create_packet (u8 * buf, const hicn_prefix_t * prefix, .ttl = HICN_MAPME_TTL, .protocol = IPPROTO_ICMP, .csum = 0, - .saddr.as_u32 = 0, - .daddr = prefix->name.ip4, + .saddr.as_u32 = IPV4_LOOPBACK.v4.as_u32, + .daddr = prefix->name.v4, }, .icmp_rd = { .type = ((params->type == UPDATE) || (params->type == NOTIFICATION)) ? HICN_MAPME_ICMP_TYPE_IPV4 : HICN_MAPME_ICMP_TYPE_ACK_IPV4, .code = HICN_MAPME_ICMP_CODE, .csum = 0, - .ip = prefix->name.ip4, + .ip = prefix->name.v4, }, .seq = htonl(params->seq), .len = prefix->len, @@ -59,15 +95,15 @@ hicn_mapme_v4_create_packet (u8 * buf, const hicn_prefix_t * prefix, } size_t -hicn_mapme_v6_create_packet (u8 * buf, const hicn_prefix_t * prefix, - const mapme_params_t * params) +hicn_mapme_v6_create_packet (u8 *buf, const hicn_prefix_t *prefix, + const mapme_params_t *params) { hicn_mapme_v6_header_t *mh = (hicn_mapme_v6_header_t *) buf; /* *INDENT-OFF* */ *mh = (hicn_mapme_v6_header_t) { .ip = { - .saddr = {{0}}, - .daddr = prefix->name.ip6, + .saddr = IPV6_LOOPBACK.v6, + .daddr = prefix->name.v6, .version_class_flow = htonl( (IPV6_DEFAULT_VERSION << 28) | (IPV6_DEFAULT_TRAFFIC_CLASS << 20) | @@ -81,8 +117,8 @@ hicn_mapme_v6_create_packet (u8 * buf, const hicn_prefix_t * prefix, .code = HICN_MAPME_ICMP_CODE, .csum = 0, .res = 0, - .tgt = prefix->name.ip6, - .dst = prefix->name.ip6, + .tgt = prefix->name.v6, + .dst = prefix->name.v6, }, .seq = htonl(params->seq), .len = prefix->len, @@ -92,20 +128,20 @@ hicn_mapme_v6_create_packet (u8 * buf, const hicn_prefix_t * prefix, } size_t -hicn_mapme_create_packet (u8 * buf, const hicn_prefix_t * prefix, - const mapme_params_t * params) +hicn_mapme_create_packet (u8 *buf, const hicn_prefix_t *prefix, + const mapme_params_t *params) { /* We currently ignore subsequent protocol definitions */ - if (PREDICT_TRUE (params->protocol == IPPROTO_IPV6)) + if (HICN_EXPECT_TRUE (params->protocol == IPPROTO_IPV6)) return hicn_mapme_v6_create_packet (buf, prefix, params); else return hicn_mapme_v4_create_packet (buf, prefix, params); } size_t -hicn_mapme_v4_create_ack (u8 * buf, const mapme_params_t * params) +hicn_mapme_v4_create_ack (u8 *buf, const mapme_params_t *params) { - ip4_address_t tmp; // tmp storage for swapping IP addresses for ACK + ipv4_address_t tmp; // tmp storage for swapping IP addresses for ACK hicn_mapme_v4_header_t *mh = (hicn_mapme_v4_header_t *) buf; tmp = mh->ip.daddr; @@ -119,9 +155,9 @@ hicn_mapme_v4_create_ack (u8 * buf, const mapme_params_t * params) } size_t -hicn_mapme_v6_create_ack (u8 * buf, const mapme_params_t * params) +hicn_mapme_v6_create_ack (u8 *buf, const mapme_params_t *params) { - ip6_address_t tmp; // tmp storage for swapping IP addresses for ACK + ipv6_address_t tmp; // tmp storage for swapping IP addresses for ACK hicn_mapme_v6_header_t *mh = (hicn_mapme_v6_header_t *) buf; tmp = mh->ip.daddr; @@ -135,32 +171,33 @@ hicn_mapme_v6_create_ack (u8 * buf, const mapme_params_t * params) } size_t -hicn_mapme_create_ack (u8 * buf, const mapme_params_t * params) +hicn_mapme_create_ack (u8 *buf, const mapme_params_t *params) { /* We currently ignore subsequent protocol definitions */ - if (PREDICT_TRUE (params->protocol == IPPROTO_IPV6)) + if (HICN_EXPECT_TRUE (params->protocol == IPPROTO_IPV6)) return hicn_mapme_v6_create_ack (buf, params); else return hicn_mapme_v4_create_ack (buf, params); } int -hicn_mapme_v4_parse_packet (const u8 * packet, hicn_prefix_t * prefix, - mapme_params_t * params) +hicn_mapme_v4_parse_packet (const u8 *packet, hicn_prefix_t *prefix, + mapme_params_t *params) { hicn_mapme_v4_header_t *mh = (hicn_mapme_v4_header_t *) packet; /* *INDENT-OFF* */ *prefix = (hicn_prefix_t) { .name = { - .ip4 = HICN_MAPME_TYPE_IS_IU (mh->icmp_rd.type) ? mh->ip.daddr : mh->ip.saddr, + .v4 = HICN_MAPME_TYPE_IS_IU (mh->icmp_rd.type) ? mh->ip.daddr : mh->ip.saddr, }, .len = mh->len, }; - *params = (mapme_params_t) { + *params = (mapme_params_t){ .protocol = IPPROTO_IP, - .type = (mh->icmp_rd.type == HICN_MAPME_ICMP_TYPE_IPV4) ? UPDATE : UPDATE_ACK, + .type = + (mh->icmp_rd.type == HICN_MAPME_ICMP_TYPE_IPV4) ? UPDATE : UPDATE_ACK, .seq = ntohl (mh->seq), }; /* *INDENT-ON* */ @@ -169,22 +206,23 @@ hicn_mapme_v4_parse_packet (const u8 * packet, hicn_prefix_t * prefix, } int -hicn_mapme_v6_parse_packet (const u8 * packet, hicn_prefix_t * prefix, - mapme_params_t * params) +hicn_mapme_v6_parse_packet (const u8 *packet, hicn_prefix_t *prefix, + mapme_params_t *params) { hicn_mapme_v6_header_t *mh = (hicn_mapme_v6_header_t *) packet; /* *INDENT-OFF* */ *prefix = (hicn_prefix_t) { .name = { - .ip6 = HICN_MAPME_TYPE_IS_IU (mh->icmp_rd.type) ? mh->ip.daddr : mh->ip.saddr, + .v6 = HICN_MAPME_TYPE_IS_IU (mh->icmp_rd.type) ? mh->ip.daddr : mh->ip.saddr, }, .len = mh->len, }; - *params = (mapme_params_t) { + *params = (mapme_params_t){ .protocol = IPPROTO_IPV6, - .type = (mh->icmp_rd.type == HICN_MAPME_ICMP_TYPE_IPV6) ? UPDATE : UPDATE_ACK, + .type = + (mh->icmp_rd.type == HICN_MAPME_ICMP_TYPE_IPV6) ? UPDATE : UPDATE_ACK, .seq = ntohl (mh->seq), }; /* *INDENT-ON* */ @@ -193,8 +231,8 @@ hicn_mapme_v6_parse_packet (const u8 * packet, hicn_prefix_t * prefix, } int -hicn_mapme_parse_packet (const u8 * packet, hicn_prefix_t * prefix, - mapme_params_t * params) +hicn_mapme_parse_packet (const u8 *packet, hicn_prefix_t *prefix, + mapme_params_t *params) { switch (HICN_IP_VERSION (packet)) { @@ -203,8 +241,9 @@ hicn_mapme_parse_packet (const u8 * packet, hicn_prefix_t * prefix, case 6: return hicn_mapme_v6_parse_packet (packet, prefix, params); default: - return HICN_LIB_ERROR_UNEXPECTED; + break; } + return HICN_LIB_ERROR_UNEXPECTED; } /* diff --git a/lib/src/name.c b/lib/src/name.c index 54b2a76aa..f363eea90 100644 --- a/lib/src/name.c +++ b/lib/src/name.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -19,533 +19,470 @@ */ #ifndef _WIN32 -#include <arpa/inet.h> // inet_ptin +#include <arpa/inet.h> // inet_ptin #endif #include <errno.h> #include <stdio.h> -#include <stdlib.h> // strtoul -#include <string.h> // memcpy +#include <stdlib.h> // strtoul +#include <string.h> // memcpy #include <hicn/common.h> #include <hicn/error.h> #include <hicn/name.h> +#include <hicn/util/sstrncpy.h> + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#if ! HICN_VPP_PLUGIN int -hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name) +hicn_name_create (const char *ip_address, u32 id, hicn_name_t *name) { int af, rc; - 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; - break; - case AF_INET6: - if (name->type == HNT_UNSPEC) - { - name->type = HNT_CONTIGUOUS_V6; - } - name->len = IPV6_ADDR_LEN; - break; - default: - return HICN_LIB_ERROR_INVALID_IP_ADDRESS; - } + memset (name, 0, sizeof (hicn_name_t)); - if ((name->type != HNT_CONTIGUOUS_V4) && (name->type != HNT_CONTIGUOUS_V6)) - { - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } + af = get_addr_family (ip_address); + int v4 = (af == AF_INET); + u8 *dst = (u8 *) (v4 * (intptr_t) (name->prefix.v4.as_u8) + + (1 - v4) * (intptr_t) (name->prefix.v6.as_u8)); + rc = inet_pton (af, ip_address, dst); - rc = inet_pton (af, ip_address, name->buffer); - if (rc <= 0) + if (rc != 1) { return HICN_LIB_ERROR_UNKNOWN_ADDRESS; } - *(u32 *) (name->buffer + name->len) = id; + + name->suffix = id; return HICN_LIB_ERROR_NONE; } int -hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id, - hicn_name_t * name) +hicn_name_create_from_ip_address (const hicn_ip_address_t ip_address, + u32 suffix, 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->prefix = ip_address; + name->suffix = suffix; return HICN_LIB_ERROR_NONE; } -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) { - return name->len; + name->prefix = prefix->address; + 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) +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; + int ret; + ret = memcmp (&name_1->prefix.v6.buffer, &name_2->prefix.v6.buffer, + sizeof (name_1->prefix.v6)); - if ((name1->type == HNT_CONTIGUOUS_V4 && name2->type == HNT_CONTIGUOUS_V6) - || (name1->type == HNT_CONTIGUOUS_V6 - && name2->type == HNT_CONTIGUOUS_V4)) - { - return -1; - } + if (consider_segment) + ret |= !(name_1->suffix == name_2->suffix); - if ((name1->type == HNT_IOV_V4 && name2->type == HNT_IOV_V6) || - (name1->type == HNT_IOV_V6 && name2->type == HNT_IOV_V4)) - { - return -1; - } + return ret; +} - if ((name1->type == HNT_IOV_V4 && name2->type == HNT_CONTIGUOUS_V6) || - (name1->type == HNT_IOV_V6 && name2->type == HNT_CONTIGUOUS_V4)) - { - return -1; - } +uint32_t +_hicn_name_get_hash (const hicn_name_t *name, bool consider_suffix) +{ + uint32_t hash = hash32 (&name->prefix, sizeof (name->prefix)); - if (name1->type == HNT_UNSPEC || name2->type == HNT_UNSPEC) - { - return -1; - } + if (consider_suffix) + hash = cumulative_hash32 (&name->suffix, sizeof (name->suffix), hash); - size_t len1 = 0, len2 = 0; + return hash; +} - u8 *buffer11, *buffer12, *buffer21, *buffer22; +int +hicn_name_empty (hicn_name_t *name) +{ + return _is_unspec (name); +} - 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; - } +int +hicn_name_copy (hicn_name_t *dst, const hicn_name_t *src) +{ + *dst = *src; + return HICN_LIB_ERROR_NONE; +} - 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; - } +int +hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *name) +{ + int v4 = _is_inet4 (name); + const u8 *src = (u8 *) (v4 * (intptr_t) (name->prefix.v4.as_u8) + + (1 - v4) * (intptr_t) (name->prefix.v6.as_u8)); + size_t size = v4 * IPV4_ADDR_LEN + (1 - v4) * IPV6_ADDR_LEN; + memcpy (dst, src, size); - // Sanity check - if (len1 != len2) - { - return HICN_LIB_ERROR_UNEXPECTED; - } + return HICN_LIB_ERROR_NONE; +} - int ret1 = memcmp ((u8 *) buffer11, (u8 *) buffer21, len1); +int +hicn_name_set_suffix (hicn_name_t *name, hicn_name_suffix_t suffix) +{ + name->suffix = suffix; + return HICN_LIB_ERROR_NONE; +} - if (!consider_segment) +int +hicn_name_to_sockaddr_address (const hicn_name_t *name, + struct sockaddr *ip_address) +{ + struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) ip_address; + struct sockaddr_in *tmp4 = (struct sockaddr_in *) ip_address; + + assert (!_is_unspec (name)); + + if (_is_inet4 (name)) + { + tmp4->sin_family = AF_INET; + tmp4->sin_port = DUMMY_PORT; + memcpy (&tmp4->sin_addr, &name->prefix.v4, IPV4_ADDR_LEN); + } + else { - return ret1; + tmp6->sin6_family = AF_INET6; + tmp6->sin6_scope_id = 0; + tmp6->sin6_port = DUMMY_PORT; + memcpy (&tmp6->sin6_addr, &name->prefix.v6, IPV6_ADDR_LEN); } - int ret2 = memcmp ((u8 *) buffer12, (u8 *) buffer22, HICN_SEGMENT_LEN); - - return ret1 || ret2; + return HICN_LIB_ERROR_NONE; } int -hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix) +hicn_name_to_hicn_ip_prefix (const hicn_name_t *name, hicn_ip_prefix_t *prefix) { - 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; - } - + int family, rc; + rc = hicn_name_get_family (name, &family); + if (rc) + return rc; + + prefix->family = family; + prefix->address.v6.as_u64[0] = name->prefix.v6.as_u64[0]; + prefix->address.v6.as_u64[1] = name->prefix.v6.as_u64[1]; + prefix->len = 128; return HICN_LIB_ERROR_NONE; } int -hicn_name_empty (hicn_name_t * name) +hicn_name_get_seq_number (const hicn_name_t *name, u32 *seq_number) { - return name->type == HNT_UNSPEC ? 1 : 0; + *seq_number = name->suffix; + return HICN_LIB_ERROR_NONE; } int -hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src) +hicn_name_ntop (const hicn_name_t *src, char *dst, size_t len) { - switch (src->type) + int offset; + const char *rc = NULL; + int v4 = _is_inet4 (src); + const u8 *addr = (u8 *) (v4 * (intptr_t) src->prefix.v4.as_u8 + + (1 - v4) * (intptr_t) src->prefix.v6.as_u8); + int family = v4 * AF_INET + (1 - v4) * AF_INET6; + rc = inet_ntop (family, addr, dst, (socklen_t) len); + + if (!rc) { - 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; + return HICN_LIB_ERROR_UNSPECIFIED; } + offset = (int) strnlen_s (dst, len); + dst[offset] = '|'; + + snprintf (dst + offset + 1, len - offset - 1, "%lu", + (unsigned long) (src->suffix)); + return HICN_LIB_ERROR_NONE; } int -hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src, - bool copy_suffix) +hicn_name_pton (const char *src, hicn_name_t *dst) { - size_t length; + return HICN_LIB_ERROR_NOT_IMPLEMENTED; +} - 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; - } +int +hicn_name_get_family (const hicn_name_t *name, int *family) +{ + assert (!_is_unspec (name)); + int v4 = _is_inet4 (name); + *family = v4 * AF_INET + (1 - v4) * AF_INET6; return HICN_LIB_ERROR_NONE; } +bool +hicn_name_is_v4 (const hicn_name_t *name) +{ + return _is_inet4 (name); +} + int -hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number) +hicn_name_snprintf (char *s, size_t size, const hicn_name_t *name) { - u8 *sequence_number = NULL; + int n, rc; + n = hicn_ip_address_snprintf (s, size, &name->prefix); + if (n < 0 || n >= size) + return n; + rc = snprintf (s + n, size - n, "|%d", name->suffix); + if (rc < 0) + return rc; + return rc + n; +} - 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; - } +int +hicn_name_no_suffix_snprintf (char *s, size_t size, const hicn_name_t *name) +{ + return hicn_ip_address_snprintf (s, size, &name->prefix); +} - if (sequence_number) - { - *(u32 *) sequence_number = seq_number; - } - else - { - return HICN_LIB_ERROR_UNEXPECTED; - } +int +hicn_prefix_create_from_ip_prefix (const hicn_ip_prefix_t *hicn_ip_prefix, + hicn_prefix_t *prefix) +{ + if (hicn_ip_prefix->family != AF_INET && hicn_ip_prefix->family != AF_INET6) + return HICN_LIB_ERROR_INVALID_IP_ADDRESS; + prefix->name = hicn_ip_prefix->address; + prefix->len = (u8) (hicn_ip_prefix->len); return HICN_LIB_ERROR_NONE; } int -hicn_name_to_sockaddr_address (const hicn_name_t * name, - struct sockaddr *ip_address) +hicn_name_cmp (const hicn_name_t *n1, const hicn_name_t *n2) { - struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) ip_address; - struct sockaddr_in *tmp4 = (struct sockaddr_in *) ip_address; - - switch (name->type) - { - 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; - } + int rc = hicn_ip_address_cmp (&n1->prefix, &n2->prefix); + if (rc != 0) + return rc; + return n2->suffix - n1->suffix; +} - return HICN_LIB_ERROR_NONE; +bool +hicn_name_equals (const hicn_name_t *n1, const hicn_name_t *n2) +{ + return (hicn_name_cmp (n1, n2) == 0); } int -hicn_name_to_ip_prefix (const hicn_name_t * name, ip_prefix_t * prefix) +hicn_prefix_create_from_ip_address_len (const hicn_ip_address_t *ip_address, + uint8_t len, hicn_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; - } + prefix->name = *ip_address; + prefix->len = len; return HICN_LIB_ERROR_NONE; } +hicn_prefix_t * +hicn_prefix_dup (const hicn_prefix_t *prefix) +{ + hicn_prefix_t *copy = malloc (sizeof (hicn_prefix_t)); + if (!copy) + goto ERR_MALLOC; + if (hicn_prefix_copy (copy, prefix) < 0) + goto ERR_COPY; + return copy; + +ERR_COPY: + free (copy); +ERR_MALLOC: + return NULL; +} + int -hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number) +hicn_prefix_copy (hicn_prefix_t *dst, const hicn_prefix_t *src) { - const u8 *sequence_number = NULL; + dst->name = src->name; + dst->len = src->len; + return 0; +} - switch (name->type) +bool +hicn_prefix_is_v4 (const hicn_prefix_t *prefix) +{ + return hicn_ip_address_is_v4 (&prefix->name); +} + +/* + * The ip address is in network byte order (big endian, msb last) in + * hicn_{prefix,name}_t, as in ip_address_t which builds upon struct in*_addr, + * But the bits are in host order... so we cannot use builtin functions to get + * the position of the first 1 unless we swap bytes as was done previously, + * which is costly and non-essential. + * + * + * Example: + * + * bits | 127 .. 120 | ... | 8 9 10 11 12 13 14 15 | 0 .. 7 | + * diff | | ... | 1 0 1 0 0 0 0 0 | | + * ^ + * bit of interest ----------------+ + */ + +uint64_t +_log2_nbo (uint64_t val) +{ + assert (val != 0); /* There is at least 1 bit set (network byte order) */ + + uint64_t result = 0; + + /* Search for the first 1, starting from left */ + + /* The first 32 bits of val are 0 */ + if (!(val & 0x00000000FFFFFFFF)) { - 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; + val = val >> 32; + result = result | 32; } - if (sequence_number) + if (!(val & 0x0000FFFF)) { - *seq_number = *(u32 *) sequence_number; + val = val >> 16; + result = result | 16; } - else + + if (!(val & 0x00FF)) { - return HICN_LIB_ERROR_UNEXPECTED; + val = val >> 8; + result = result | 8; } - return HICN_LIB_ERROR_NONE; + /* Val now contains the byte with at last 1 bit set (host bit order) */ + if (val & 0xF0) + val = val >> 4; + else + result = result | 4; + + if (val & 0xC) + val = val >> 2; + else + result = result | 2; + + if (val & 0x2) + val = val >> 1; + else + result = result | 1; + + return result; } -int -hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len) +uint32_t +hicn_prefix_lpm (const hicn_prefix_t *p1, const hicn_prefix_t *p2) { - int offset; - const char *rc; - void *seg_number = NULL; - - switch (src->type) + uint32_t prefix_len = 0; + /* Test each block of 64 bits as a whole */ + for (unsigned i = 0; i < 2; i++) { - 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 (!rc) - { - goto ERR; + /* Check for differences in the two u64 */ + uint64_t diff = p1->name.v6.as_u64[i] ^ p2->name.v6.as_u64[i]; + if (diff) + { + /* + * As the ip_address_t mimics in*_addr and has network byte order + * (and host bit order, we cannot directly use 64-bit operations: + */ + prefix_len += _log2_nbo (diff); + break; + } + prefix_len += 8 * sizeof (uint64_t); } - offset = (int) strlen (dst); - dst[offset] = '|'; - - sprintf (dst + offset + 1, "%lu", (unsigned long) (*(u32 *) seg_number)); - - return HICN_LIB_ERROR_NONE; + /* Bound the returned prefix length by the length of all input */ + return MIN (prefix_len, + MIN (hicn_prefix_get_len (p1), hicn_prefix_get_len (p2))); +} -ERR: - return HICN_LIB_ERROR_UNSPECIFIED; +void +hicn_prefix_clear (hicn_prefix_t *prefix, uint8_t start_from) +{ + uint8_t *buffer = prefix->name.v6.as_u8; + + /* Compute the offset of the byte from which to clear the name... */ + uint8_t offset = start_from / 8; + if (hicn_prefix_is_v4 (prefix)) + offset += IP_ADDRESS_V4_OFFSET_LEN; /* Ignore padding */ + /* ... and the position of the first bit to clear */ + uint8_t pos = start_from % 8; + + /* Mask to clear specific bits at offset... + * pos 7 6 5 4 3 2 1 0 (eg. start_from = 19, pos = 3) + * mask 0 0 0 0 0 1 1 1 (= 1<<pos - 1) + * */ + uint8_t mask = 0xFF ^ ((1 << (8 - pos)) - 1); + buffer[offset] &= mask; + + /* ... then fully clear remaining bytes */ + for (uint8_t i = offset + 1; i < HICN_PREFIX_MAX_LEN; i++) + buffer[i] = 0; } -int -hicn_name_pton (const char *src, hicn_name_t * dst) +void +hicn_prefix_truncate (hicn_prefix_t *prefix, uint8_t len) { - return HICN_LIB_ERROR_NOT_IMPLEMENTED; + hicn_prefix_clear (prefix, len); + prefix->len = len; } int -hicn_name_get_family (const hicn_name_t * name, int *family) +hicn_prefix_cmp (const hicn_prefix_t *p1, const hicn_prefix_t *p2) { - 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; - } + if (p1->len != p2->len) + return p2->len - p1->len; + return hicn_ip_address_cmp (&p1->name, &p2->name); +} - return HICN_LIB_ERROR_NONE; +bool +hicn_prefix_equals (const hicn_prefix_t *p1, const hicn_prefix_t *p2) +{ + return hicn_prefix_cmp (p1, p2) == 0; } int -hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, - hicn_prefix_t * prefix) +hicn_prefix_snprintf (char *s, size_t size, const hicn_prefix_t *prefix) { - switch (ip_prefix->family) - { - case AF_INET: - prefix->name.ip4.as_u32 = ip_prefix->address.v4.as_u32; - break; - case AF_INET6: - prefix->name.ip6.as_u64[0] = ip_prefix->address.v6.as_u64[0]; - prefix->name.ip6.as_u64[1] = ip_prefix->address.v6.as_u64[1]; - break; - default: - return HICN_LIB_ERROR_INVALID_IP_ADDRESS; - } - prefix->len = (u8) (ip_prefix->len); + hicn_ip_prefix_t ip_prefix = { .family = + hicn_ip_address_get_family (&prefix->name), + .address = prefix->name, + .len = prefix->len }; + return hicn_ip_prefix_snprintf (s, size, &ip_prefix); +} - return HICN_LIB_ERROR_NONE; +uint8_t +_hicn_prefix_get_bit (const hicn_prefix_t *prefix, uint8_t pos) +{ + const hicn_ip_address_t *address = hicn_prefix_get_ip_address (prefix); + return hicn_ip_address_get_bit (address, pos); } -#endif /* ! HICN_VPP_PLUGIN */ +uint8_t +hicn_prefix_get_bit (const hicn_prefix_t *prefix, uint8_t pos) +{ + assert (pos <= hicn_prefix_get_len (prefix)); + return _hicn_prefix_get_bit (prefix, pos); +} +int +hicn_prefix_get_ip_prefix (const hicn_prefix_t *prefix, + hicn_ip_prefix_t *ip_prefix) +{ + *ip_prefix = + (hicn_ip_prefix_t){ .family = hicn_ip_address_get_family (&prefix->name), + .address = prefix->name, + .len = prefix->len }; + return HICN_LIB_ERROR_NONE; +} /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/ops.c b/lib/src/ops.c index ef12e9bf0..2c38b5374 100644 --- a/lib/src/ops.c +++ b/lib/src/ops.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -22,25 +22,35 @@ #include <netinet/in.h> #endif #include <stdlib.h> -#include <hicn/ops.h> -#include <hicn/header.h> + +#include "ops.h" +#include "protocol.h" extern const hicn_ops_t hicn_ops_ipv4; extern const hicn_ops_t hicn_ops_icmp; +extern const hicn_ops_t hicn_ops_udp; extern const hicn_ops_t hicn_ops_tcp; extern const hicn_ops_t hicn_ops_ipv6; +extern const hicn_ops_t hicn_ops_new; extern const hicn_ops_t hicn_ops_ah; /* Declare empty operations (terminates recursion on protocol layers) */ -DECLARE_init_packet_header (none, NONE); + +int +none_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + pkbuf->payload = pkbuf->len; + return HICN_LIB_ERROR_NONE; +} + DECLARE_get_interest_locator (none, NONE); DECLARE_set_interest_locator (none, NONE); DECLARE_get_interest_name (none, NONE); DECLARE_set_interest_name (none, NONE); DECLARE_get_interest_name_suffix (none, NONE); DECLARE_set_interest_name_suffix (none, NONE); -DECLARE_mark_packet_as_interest (none, NONE); -DECLARE_mark_packet_as_data (none, NONE); +DECLARE_get_type (none, NONE); +DECLARE_set_type (none, NONE); DECLARE_reset_interest_for_hash (none, NONE); DECLARE_get_data_locator (none, NONE); DECLARE_set_data_locator (none, NONE); @@ -48,21 +58,20 @@ DECLARE_get_data_name (none, NONE); DECLARE_set_data_name (none, NONE); DECLARE_get_data_name_suffix (none, NONE); DECLARE_set_data_name_suffix (none, NONE); -DECLARE_get_data_pathlabel (none, NONE); -DECLARE_set_data_pathlabel (none, NONE); -DECLARE_update_data_pathlabel (none, NONE); +DECLARE_get_data_path_label (none, NONE); +DECLARE_set_data_path_label (none, NONE); +DECLARE_update_data_path_label (none, NONE); DECLARE_reset_data_for_hash (none, NONE); DECLARE_get_lifetime (none, NONE); DECLARE_set_lifetime (none, NONE); DECLARE_update_checksums (none, NONE); +DECLARE_update_checksums_incremental (none, NONE); DECLARE_verify_checksums (none, NONE); DECLARE_rewrite_interest (none, NONE); DECLARE_rewrite_data (none, NONE); -DECLARE_get_length (none, NONE); -DECLARE_get_header_length (none, NONE); -DECLARE_get_current_header_length (none, NONE); -DECLARE_get_payload_length (none, NONE); -DECLARE_set_payload_length (none, NONE); +DECLARE_set_payload_len (none, NONE); +DECLARE_get_payload_type (none, NONE); +DECLARE_set_payload_type (none, NONE); DECLARE_get_signature_size (none, NONE); DECLARE_set_signature_size (none, NONE); DECLARE_set_signature_timestamp (none, NONE); @@ -72,9 +81,12 @@ DECLARE_get_validation_algorithm (none, NONE); DECLARE_set_key_id (none, NONE); DECLARE_get_key_id (none, NONE); DECLARE_get_signature (none, NONE); -DECLARE_get_signature_gap (none, NONE); -DECLARE_set_signature_gap (none, NONE); -DECLARE_HICN_OPS (none); +DECLARE_has_signature (none, NONE); +DECLARE_get_signature_padding (none, NONE); +DECLARE_set_signature_padding (none, NONE); +DECLARE_is_last_data (none, NONE); +DECLARE_set_last_data (none, NONE); +DECLARE_HICN_OPS (none, 0); /** * @brief Virtual function table for packet operations @@ -84,10 +96,12 @@ const hicn_ops_t *const hicn_ops_vft[] = { /* 0 */ [IPPROTO_IP] = &hicn_ops_ipv4, /* 1 */[IPPROTO_ICMP] = &hicn_ops_icmp, /* 6 */[IPPROTO_TCP] = &hicn_ops_tcp, + /* 6 */[IPPROTO_UDP] = &hicn_ops_udp, /* 41 */[IPPROTO_IPV6] = &hicn_ops_ipv6, /* 51 */[IPPROTO_AH] = &hicn_ops_ah, /* 58 */[IPPROTO_ICMPV6] = &hicn_ops_icmp, - [IPPROTO_NONE] = &hicn_ops_none, + /* 59 */[IPPROTO_NONE] = &hicn_ops_none, + /* 98 */[IPPROTO_ENCAP] = &hicn_ops_new, }; /* diff --git a/lib/src/ops.h b/lib/src/ops.h new file mode 100644 index 000000000..843945690 --- /dev/null +++ b/lib/src/ops.h @@ -0,0 +1,979 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file base.h + * @brief Protocol-independent packet operations + */ + +#ifndef HICN_OPS_H +#define HICN_OPS_H + +#include <assert.h> +#include <stdlib.h> + +#include <hicn/base.h> +#include <hicn/error.h> +#include <hicn/name.h> +#include <hicn/packet.h> // HICN_OPAQUE_LEN +#include <hicn/util/ip_address.h> + +#include "protocol.h" + +/* + * In order to provide fast lookup and accelerate packet operations, we allow + * ourselves to use a header cache structure under the responsibility of the + * caller, and that can be associated to each packet. This structure is exposed + * as a opaque pointer. + */ + +/* + * hICN operations on packets + * + * All prototypes take an hicn_type_t parameter as their first argument, as + * this decides the sequence of protocols that are being used by the different + * operations. + */ + +typedef struct hicn_ops_s +{ + const char *name; + + size_t header_len; + + /** + * @brief Initialize the headers of the hicn packet + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + */ + int (*init_packet_header) (hicn_packet_buffer_t *pkbuf, size_t pos); + + /** + * @brief Retrieves an Interest locator + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] ip_address - Retrieved locator + * @return hICN error code + */ + int (*get_interest_locator) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, + hicn_ip_address_t *ip_address); + + /** + * @brief Sets an Interest locator + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] ip_address - Locator to set + * @return hICN error code + */ + int (*set_interest_locator) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address); + + /** + * @brief Retrieves an Interest name + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] name - Retrieved name + * @return hICN error code + */ + int (*get_interest_name) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, hicn_name_t *name); + + /** + * @brief Sets an Interest name + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] name - Name to set + * @return hICN error code + */ + int (*set_interest_name) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_t *name); + + /** + * @brief Retrieves an Interest name suffix + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] suffix - Retrieved name suffix + * @return hICN error code + */ + int (*get_interest_name_suffix) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, + hicn_name_suffix_t *suffix); + + /** + * @brief Sets an Interest name suffix + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] suffix - Name suffix to set + * @return hICN error code + */ + int (*set_interest_name_suffix) (const hicn_packet_buffer_t *pkbuf, + size_t pos, + const hicn_name_suffix_t *suffix); + + /** + * @brief Get packet type + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Position within the network layers + * @param [out] type - Packet type + * @return hICN error code + */ + int (*get_type) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type); + + /** + * @brief Set flag to mark current packet as interest + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Position within the network layers + * @param [in] type - Packet type + * @return hICN error code + */ + int (*set_type) (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type); + + /** + * @brief Clear the necessary Interest fields in order to hash it + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @return hICN error code + */ + int (*reset_interest_for_hash) (hicn_packet_buffer_t *pkbuf, size_t pos); + + /** + * @brief Retrieves a Data locator + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] ip_address - Retrieved locator + * @return hICN error code + */ + int (*get_data_locator) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_ip_address_t *ip_address); + + /** + * @brief Sets a Data locator + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] ip_address - Locator to set + * @return hICN error code + */ + int (*set_data_locator) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address); + + /** + * @brief Retrieves a Data name + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] name - Retrieved name + * @return hICN error code + */ + int (*get_data_name) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_name_t *name); + + /** + * @brief Sets a Data name + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] name - Name to set + * @return hICN error code + */ + int (*set_data_name) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_t *name); + + /** + * @brief Retrieves a Data name suffix + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] suffix - Retrieved name suffix + * @return hICN error code + */ + int (*get_data_name_suffix) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, hicn_name_suffix_t *suffix); + + /** + * @brief Sets a Data name suffix + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] suffix - Name suffix to set + * @return hICN error code + */ + int (*set_data_name_suffix) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix); + + /** + * @brief Retrieves a Data path_label + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [out] path_label - Retrieved path_label + * @return hICN error code + */ + int (*get_data_path_label) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, hicn_path_label_t *path_label); + + /** + * @brief Sets a Data path_label + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] path_label - Pathlabel to set + * @return hICN error code + */ + int (*set_data_path_label) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_path_label_t path_label); + + /** + * @brief Update a Data path_label with a new face identifier + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] path_label - Face identifier used to update path_label + * @return hICN error code + */ + int (*update_data_path_label) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id); + + /** + * @brief Clear the necessary Data fields in order to hash it + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @return hICN error code + */ + int (*reset_data_for_hash) (hicn_packet_buffer_t *pkbuf, size_t pos); + + /** + * @brief Retrieves an Interest or Data lifetime + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Current position in the sequence of headers while + * executing command + * @param [out] path_label - Retrieved lifetime + * @return hICN error code + */ + int (*get_lifetime) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_lifetime_t *lifetime); + + /** + * @brief Sets an Interest or Data lifetime + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] path_label - Lifetime to set + * @return hICN error code + */ + int (*set_lifetime) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_lifetime_t lifetime); + + /** + * @brief Update all checksums in packet headers + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] partial_csum - Partial checksum (set to 0, used internally to + * carry intermediate values from IP pseudo-header) + * @param [in] payload_len - Payload len (can be set to ~0, retrieved + * and used internally to carry payload len across protocol headers) + * @return hICN error code + * + * Payload len is initialized during the initial steps (eg. IP) if not + * provided (value is ~0), and not ignored (value is 0). + */ + int (*update_checksums) (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len); + + /** + * @brief Update all checksums in packet headers + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] old_val - Pointer to the old value + * @param [in] new_val - Pointer to the new value + * @param [in] size - Size of the changed value + * @param [in] skip_first - Skip the first protocol (ignore IP checksum) + * @return hICN error code + */ + int (*update_checksums_incremental) (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first); + + /** + * @brief Validate all checksums in packet headers + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] partial_csum - Partial checksum, or zero if no partial + * checksum available + * @param [in] payload_len - Payload len (can be set to ~0, retrieved + * and used internally to carry payload len across protocol headers) + * @return hICN error code + */ + int (*verify_checksums) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + u16 partial_csum, size_t payload_len); + + /** + * @brief Rewrite an Interest packet header (locator) + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] addr_new - New locator + * @param [in] addr_old - Old locator (set to NULL, used internally to + * compute incremental checksums) + * @return hICN error code + */ + int (*rewrite_interest) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old); + + /** + * @brief Rewrite a Data packet header (locator + path_label) + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @param [in] addr_new - New locator + * @param [in] addr_old - Old locator (set to NULL, used internally to + * compute incremental checksums) + * @param [in] face_id - Face identifier used to update path_label + * @param [in] reset_pl - If not zero, reset the current path_label + * before update it + * @return hICN error code + */ + int (*rewrite_data) (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, + const hicn_faceid_t face_id, u8 reset_pl); + + /** + * @brief Return the packet len + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @parma [out] len - Returned packet len + * @return hICN error code + */ + int (*get_len) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + size_t *len); + + /** + * @brief Return the current packet header len + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @parma [out] header_len - Returned packet current header len + * @return hICN error code + */ + int (*get_current_header_len) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, size_t *header_len); + + /** + * @brief Sets the packet paylaod len + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Depth in the header stack + * @parma [out] payload_len - Payload len to set + * @return hICN error code + */ + int (*set_payload_len) (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t payload_len); + + /** + * @brief Retrieves an Interest or Data signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Current position in the sequence of headers while + * executing command + * @param [out] signature_size - Retrieved signature size + * @return hICN error code + */ + int (*get_signature_size) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, size_t *signature_size); + + /** + * @brief Sets an Interest or Data signature size + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] signature_size - Signature size to set + * @return hICN error code + */ + int (*set_signature_size) (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size); + + /** + * @brief Sets an Interest or Data signature padding between maximum size and + * real size + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] signature_size - Signature size to set + * @return hICN error code + */ + int (*set_signature_padding) (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_padding); + + /** + * @brief gets an Interest or Data signature padding between maximum size and + * real size + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] signature_size - retrieve the padding between maximum size and + * real size + * @return hICN error code + */ + int (*get_signature_padding) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, size_t *signature_padding); + + /** + * @brief Gets the signature timestamp + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [out] signature_timestamp - Retrieved signature timestamp + * @return hICN error code + */ + int (*get_signature_timestamp) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, + uint64_t *signature_timestamp); + + /** + * @brief Sets the signature timestamp + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] signature_timestamp - Signature timestamp to set + * @return hICN error code + */ + int (*set_signature_timestamp) (const hicn_packet_buffer_t *pkbuf, + size_t pos, uint64_t signature_timestamp); + + /** + * @brief Gets the signature validation algorithm + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [out] validation_algorithm - Retrieved validation_algorithm + * @return hICN error code + */ + int (*get_validation_algorithm) (const hicn_packet_buffer_t *pkbuf, + const size_t pos, + uint8_t *validation_algorithm); + + /** + * @brief Sets the signature validation algorithm + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] validation_algorithm - Validation algorithm enumeration + * @return hICN error code + */ + int (*set_validation_algorithm) (const hicn_packet_buffer_t *pkbuf, + size_t pos, uint8_t validation_algorithm); + + /** + * @brief Gets the key id + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [out] key_id - Retrieved key id first byte address + * @return hICN error code + */ + int (*get_key_id) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + uint8_t **key_id, uint8_t *key_id_size); + + /** + * @brief Sets the key id + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] key_id - Key id first byte address + * @return hICN error code + */ + int (*set_key_id) (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *key_id, size_t size); + + /** + * @brief Get a pointer to the signature field in the packet + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [out] signature - Pointer to the memory region holding the + * signature + * @return hICN error code + */ + int (*get_signature) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + uint8_t **signature); + + /** + * @brief Returns whether the packet holds a signature. + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [out] flag - Boolean indicating whether the packet has a signature. + * @return hICN error code + */ + int (*has_signature) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + bool *flag); + + /** + * @brief Set payload type of the packet + * @param [in] pkbuf - hICN packet buffer + * @param [in,out] pos - Current position in the sequence of headers while + * executing command + * @param [in] payload_type - The payload type of this packet + * @return hICN error code + */ + int (*set_payload_type) (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type); + + /** + * @brief Get payload type from the packet + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Current position in the sequence of headers while + * executing command + * @param [out] payload_type - The payload type of this packet + * @return hICN error code + */ + int (*get_payload_type) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_payload_type_t *payload_type); + + /** + * @brief Check if data packet is last one. + * @param [in] pkbuf - hICN packet buffer + * @param [in] pos - Current position in the sequence of headers while + * executing command + * @param [out] is_last - 1 if last data, 0 otherwise + * @return hICN error code + */ + int (*is_last_data) (const hicn_packet_buffer_t *pkbuf, const size_t pos, + int *is_last); + + /** + * @brief Mark data packet as last + * @param [in] pkbuf - hICN packet buffer + * @param [in, out] pos - Current position in the sequence of headers while + * executing command + * @return hICN error code + */ + int (*set_last_data) (const hicn_packet_buffer_t *pkbuf, size_t pos); + +} hicn_ops_t; + +#define DECLARE_HICN_OPS(protocol, len) \ + const hicn_ops_t hicn_ops_##protocol = { \ + ATTR_INIT (name, #protocol), \ + ATTR_INIT (header_len, len), \ + ATTR_INIT (init_packet_header, protocol##_init_packet_header), \ + ATTR_INIT (get_interest_locator, protocol##_get_interest_locator), \ + ATTR_INIT (set_interest_locator, protocol##_set_interest_locator), \ + ATTR_INIT (get_interest_name, protocol##_get_interest_name), \ + ATTR_INIT (set_interest_name, protocol##_set_interest_name), \ + ATTR_INIT (get_interest_name_suffix, \ + protocol##_get_interest_name_suffix), \ + ATTR_INIT (set_interest_name_suffix, \ + protocol##_set_interest_name_suffix), \ + ATTR_INIT (get_type, protocol##_get_type), \ + ATTR_INIT (set_type, protocol##_set_type), \ + ATTR_INIT (reset_interest_for_hash, protocol##_reset_interest_for_hash), \ + ATTR_INIT (get_data_locator, protocol##_get_data_locator), \ + ATTR_INIT (set_data_locator, protocol##_set_data_locator), \ + ATTR_INIT (get_data_name, protocol##_get_data_name), \ + ATTR_INIT (set_data_name, protocol##_set_data_name), \ + ATTR_INIT (get_data_name_suffix, protocol##_get_data_name_suffix), \ + ATTR_INIT (set_data_name_suffix, protocol##_set_data_name_suffix), \ + ATTR_INIT (get_data_path_label, protocol##_get_data_path_label), \ + ATTR_INIT (set_data_path_label, protocol##_set_data_path_label), \ + ATTR_INIT (update_data_path_label, protocol##_update_data_path_label), \ + ATTR_INIT (reset_data_for_hash, protocol##_reset_data_for_hash), \ + ATTR_INIT (get_lifetime, protocol##_get_lifetime), \ + ATTR_INIT (set_lifetime, protocol##_set_lifetime), \ + ATTR_INIT (update_checksums, protocol##_update_checksums), \ + ATTR_INIT (update_checksums_incremental, \ + protocol##_update_checksums_incremental), \ + ATTR_INIT (verify_checksums, protocol##_verify_checksums), \ + ATTR_INIT (rewrite_interest, protocol##_rewrite_interest), \ + ATTR_INIT (rewrite_data, protocol##_rewrite_data), \ + ATTR_INIT (set_payload_len, protocol##_set_payload_len), \ + ATTR_INIT (get_payload_type, protocol##_get_payload_type), \ + ATTR_INIT (set_payload_type, protocol##_set_payload_type), \ + ATTR_INIT (get_signature_size, protocol##_get_signature_size), \ + ATTR_INIT (get_signature_timestamp, protocol##_get_signature_timestamp), \ + ATTR_INIT (set_signature_timestamp, protocol##_set_signature_timestamp), \ + ATTR_INIT (get_validation_algorithm, \ + protocol##_get_validation_algorithm), \ + ATTR_INIT (set_validation_algorithm, \ + protocol##_set_validation_algorithm), \ + ATTR_INIT (get_key_id, protocol##_get_key_id), \ + ATTR_INIT (set_key_id, protocol##_set_key_id), \ + ATTR_INIT (get_signature, protocol##_get_signature), \ + ATTR_INIT (has_signature, protocol##_has_signature), \ + ATTR_INIT (set_signature_padding, protocol##_set_signature_padding), \ + ATTR_INIT (set_signature_size, protocol##_set_signature_size), \ + ATTR_INIT (get_signature_padding, protocol##_get_signature_padding), \ + ATTR_INIT (is_last_data, protocol##_is_last_data), \ + } + +/** + * @brief Protocol-independent packet operations VFT + * NOTE: The following declarations should be kept in order + */ +extern const hicn_ops_t *const hicn_ops_vft[]; + +static inline uint8_t +PROT (const hicn_packet_buffer_t *pkbuf, int pos) +{ + if (pos < -1 || pos >= HICN_PACKET_FORMAT_SIZE - 1) + return IPPROTO_NONE; + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + return HICN_PACKET_FORMAT_GET (format, pos + 1); +} + +#define CALL_CHILD(method, pkbuf, pos, ...) \ + hicn_ops_vft[PROT (pkbuf, (pos))]->method (pkbuf, (pos) + 1, ##__VA_ARGS__); + +#define CALL(method, pkbuf, ...) CALL_CHILD (method, pkbuf, -1, ##__VA_ARGS__) + +/** Shortcuts to entry points in VFT */ +#define HICN_OPS4 hicn_ops_vft[IPPROTO_IP] +#define HICN_OPS6 hicn_ops_vft[IPPROTO_IPV6] + +/* Helpers for simple declarations */ + +#define DECLARE_init_packet_header(protocol, error) \ + int protocol##_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_interest_locator(protocol, error) \ + int protocol##_get_interest_locator (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_ip_address_t *ip_address) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_interest_locator(protocol, error) \ + int protocol##_set_interest_locator (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_ip_address_t *ip_address) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_interest_name(protocol, error) \ + int protocol##_get_interest_name (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, hicn_name_t *name) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_interest_name(protocol, error) \ + int protocol##_set_interest_name (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, const hicn_name_t *name) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_interest_name_suffix(protocol, error) \ + int protocol##_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_name_suffix_t *suffix) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_interest_name_suffix(protocol, error) \ + int protocol##_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_name_suffix_t *suffix) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_type(protocol, error) \ + int protocol##_get_type (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, hicn_packet_type_t *type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_type(protocol, error) \ + int protocol##_set_type (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, hicn_packet_type_t type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_reset_interest_for_hash(protocol, error) \ + int protocol##_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, \ + size_t pos) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_data_locator(protocol, error) \ + int protocol##_get_data_locator (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_ip_address_t *ip_address) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_data_locator(protocol, error) \ + int protocol##_set_data_locator (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_ip_address_t *ip_address) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_data_name(protocol, error) \ + int protocol##_get_data_name (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, hicn_name_t *name) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_data_name(protocol, error) \ + int protocol##_set_data_name (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, const hicn_name_t *name) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_data_name_suffix(protocol, error) \ + int protocol##_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_name_suffix_t *suffix) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_data_name_suffix(protocol, error) \ + int protocol##_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_name_suffix_t *suffix) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_data_path_label(protocol, error) \ + int protocol##_get_data_path_label (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_path_label_t *path_label) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_data_path_label(protocol, error) \ + int protocol##_set_data_path_label (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_path_label_t path_label) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_update_data_path_label(protocol, error) \ + int protocol##_update_data_path_label (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + const hicn_faceid_t face_id) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_reset_data_for_hash(protocol, error) \ + int protocol##_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, \ + size_t pos) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_lifetime(protocol, error) \ + int protocol##_get_lifetime (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, hicn_lifetime_t *lifetime) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_lifetime(protocol, error) \ + int protocol##_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, \ + const hicn_lifetime_t lifetime) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_update_checksums(protocol, error) \ + int protocol##_update_checksums (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, u16 partial_csum, \ + size_t payload_len) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_update_checksums_incremental(protocol, error) \ + int protocol##_update_checksums_incremental ( \ + const hicn_packet_buffer_t *pkbuf, size_t pos, u16 *old_val, \ + u16 *new_val, u8 size, bool skip_first) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_verify_checksums(protocol, error) \ + int protocol##_verify_checksums (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, u16 partial_csum, \ + size_t payload_len) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_rewrite_interest(protocol, error) \ + int protocol##_rewrite_interest ( \ + const hicn_packet_buffer_t *pkbuf, size_t pos, \ + const hicn_ip_address_t *addr_new, hicn_ip_address_t *addr_old) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_rewrite_data(protocol, error) \ + int protocol##_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, \ + const hicn_ip_address_t *addr_new, \ + hicn_ip_address_t *addr_old, \ + const hicn_faceid_t face_id, u8 reset_pl) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_payload_len(protocol, error) \ + int protocol##_set_payload_len (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, size_t payload_len) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_payload_type(protocol, error) \ + int protocol##_get_payload_type (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + hicn_payload_type_t *payload_type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_payload_type(protocol, error) \ + int protocol##_set_payload_type (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + hicn_payload_type_t payload_type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_signature_size(protocol, error) \ + int protocol##_get_signature_size (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + size_t *signature_size) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_signature_size(protocol, error) \ + int protocol##_set_signature_size (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, size_t signature_size) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_signature_padding(protocol, error) \ + int protocol##_set_signature_padding (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, size_t padding) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_signature_padding(protocol, error) \ + int protocol##_get_signature_padding (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, size_t *padding) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_signature_timestamp(protocol, error) \ + int protocol##_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + uint64_t signature_timestamp) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_signature_timestamp(protocol, error) \ + int protocol##_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + uint64_t *signature_timestamp) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_validation_algorithm(protocol, error) \ + int protocol##_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, \ + size_t pos, \ + uint8_t validation_algorithm) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_validation_algorithm(protocol, error) \ + int protocol##_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, \ + uint8_t *validation_algorithm) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_key_id(protocol, error) \ + int protocol##_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, \ + uint8_t *key_id, size_t size) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_key_id(protocol, error) \ + int protocol##_get_key_id (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, uint8_t **key_id, \ + uint8_t *key_id_size) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_get_signature(protocol, error) \ + int protocol##_get_signature (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, uint8_t **signature) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_has_signature(protocol, error) \ + int protocol##_has_signature (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, bool *flag) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_is_last_data(protocol, error) \ + int protocol##_is_last_data (const hicn_packet_buffer_t *pkbuf, \ + const size_t pos, int *is_last) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_last_data(protocol, error) \ + int protocol##_set_last_data (const hicn_packet_buffer_t *pkbuf, \ + size_t pos) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#endif /* HICN_OPS_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/packet.c b/lib/src/packet.c new file mode 100644 index 000000000..bebfad23e --- /dev/null +++ b/lib/src/packet.c @@ -0,0 +1,806 @@ +/* + * Copyright (c) 2017-2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file packet.c + * @brief Implementation of the compatibility layer. + */ +#ifndef _WIN32 +#include <netinet/in.h> +#endif +#include <string.h> // memset +#include <stddef.h> // offsetof + +#include <hicn/common.h> +#include <hicn/packet.h> +#include <hicn/error.h> +#include <hicn/name.h> +#include <hicn/util/log.h> +#include "ops.h" + +#define member_size(type, member) sizeof (((type *) 0)->member) +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a))) + +#define HICN_NAME_COMPONENT_SIZE 2 + +hicn_packet_format_t +hicn_packet_get_format (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->format; +} + +void +hicn_packet_set_format (hicn_packet_buffer_t *pkbuf, + hicn_packet_format_t format) +{ + pkbuf->format = format; +} + +hicn_packet_type_t +hicn_packet_get_type (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->type; +} + +void +hicn_packet_initialize_type (hicn_packet_buffer_t *pkbuf, + hicn_packet_type_t type) +{ + assert (pkbuf->format != HICN_PACKET_FORMAT_NONE); + pkbuf->type = type; + CALL (set_type, pkbuf, type); +} + +void +hicn_packet_set_type (hicn_packet_buffer_t *pkbuf, hicn_packet_type_t type) +{ + pkbuf->type = type; +} + +bool +hicn_packet_is_interest (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->type == HICN_PACKET_TYPE_INTEREST; +} + +bool +hicn_packet_is_data (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->type == HICN_PACKET_TYPE_DATA; +} + +bool +hicn_packet_is_undefined (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->type == HICN_PACKET_TYPE_UNDEFINED; +} + +int +hicn_packet_init_header (hicn_packet_buffer_t *pkbuf, + size_t additional_header_size) +{ + if (hicn_packet_is_undefined (pkbuf)) + return HICN_LIB_ERROR_UNEXPECTED; + if (hicn_packet_get_format (pkbuf) == HICN_PACKET_FORMAT_NONE) + return HICN_LIB_ERROR_UNEXPECTED; + if (!pkbuf_get_header (pkbuf)) + return HICN_LIB_ERROR_UNEXPECTED; + pkbuf->len = 0; + pkbuf->payload = 0; + + int rc = CALL (init_packet_header, pkbuf); + + /* + * Additional header size is there for the signature, and assumes the AH + * header is always located at the end... + */ + pkbuf->len += additional_header_size; + pkbuf->payload += additional_header_size; + + return rc; +} + +int +hicn_packet_reset (hicn_packet_buffer_t *pkbuf) +{ + memset (pkbuf, 0, sizeof (hicn_packet_buffer_t)); + hicn_packet_set_format (pkbuf, HICN_PACKET_FORMAT_NONE); + hicn_packet_set_type (pkbuf, HICN_PACKET_TYPE_UNDEFINED); + hicn_packet_set_len (pkbuf, 0); + + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_analyze (hicn_packet_buffer_t *pkbuf) +{ + u8 *header = pkbuf_get_header (pkbuf); + u8 protocol; + u16 offset = 0; + bool has_signature; + size_t signature_size; + int rc; + + /* Bootstrap: assume IP packet, and get version from header */ + switch (HICN_IP_VERSION (pkbuf_get_header (pkbuf))) + { + case 4: + protocol = IPPROTO_IP; + break; + case 6: + protocol = IPPROTO_IPV6; + break; + case 9: + protocol = IPPROTO_ENCAP; // new + break; + default: + goto ERR; + } + + hicn_packet_format_t *format = &pkbuf->format; + for (unsigned i = 0; i < HICN_PACKET_FORMAT_SIZE; i++) + { + HICN_PACKET_FORMAT_SET (*format, i, protocol); + + /* Next protocol + increment offset */ + switch (protocol) + { + + /* + * All packets either start with IPv4 or IPv6, so we take the + * opportunity to update packet length there + */ + case IPPROTO_IP: + { + if (i > 0) + goto ERR; +#ifdef OPAQUE_IP + pkbuf->ipv4 = offset; +#else + assert (offset == 0); +#endif /* OPAQUE_IP */ + _ipv4_header_t *ipv4 = (_ipv4_header_t *) (header + offset); + protocol = ipv4->protocol; + offset += IPV4_HDRLEN; + + // hicn_packet_set_len (pkbuf, htons (ipv4->len)); + if (hicn_packet_get_len (pkbuf) != htons (ipv4->len)) + { + ERROR ("Invalid packet size in IPv4 header %d != %d", + htons (ipv4->len), hicn_packet_get_len (pkbuf)); + goto ERR; + } + break; + } + case IPPROTO_IPV6: + { + if (i > 0) + goto ERR; +#ifdef OPAQUE_IP + pkbuf->ipv6 = offset; +#else + assert (offset == 0); +#endif /* OPAQUE_IP */ + _ipv6_header_t *ipv6 = (_ipv6_header_t *) (header + offset); + protocol = ipv6->nxt; + offset += IPV6_HDRLEN; + // hicn_packet_set_len (pkbuf, IPV6_HDRLEN + htons (ipv6->len)); + if (hicn_packet_get_len (pkbuf) != IPV6_HDRLEN + htons (ipv6->len)) + { + ERROR ("Invalid packet size in IPv6 header %d != %d", + IPV6_HDRLEN + htons (ipv6->len), + hicn_packet_get_len (pkbuf)); + goto ERR; + } + break; + } + case IPPROTO_TCP: + pkbuf->tcp = offset; + /* After TCP, we might eventually have a AH header */ + rc = CALL_CHILD (has_signature, pkbuf, i - 1, &has_signature); + if (rc < 0) + goto ERR; + protocol = has_signature ? IPPROTO_AH : IPPROTO_NONE; + offset += TCP_HDRLEN; + break; + case IPPROTO_UDP: + pkbuf->udp = offset; + protocol = IPPROTO_ENCAP; + offset += UDP_HDRLEN; + break; + case IPPROTO_ICMP: + case IPPROTO_ICMPV6: + pkbuf->icmp = offset; + /* After ICMP, we might eventually have a AH header */ + // CALL_CHILD (has_signature, pkbuf, i - 1, &has_signature); + protocol = /* has_signature ? IPPROTO_AH : */ IPPROTO_NONE; + offset += ICMP_HDRLEN; + break; + + case IPPROTO_ENCAP: + pkbuf->newhdr = offset; + /* After ENCAP, we might eventually have a AH header */ + rc = CALL_CHILD (has_signature, pkbuf, i - 1, &has_signature); + if (rc < 0) + goto ERR; + protocol = has_signature ? IPPROTO_AH : IPPROTO_NONE; + offset += NEW_HDRLEN; + break; + + case IPPROTO_AH: + pkbuf->ah = offset; + protocol = IPPROTO_NONE; + offset += AH_HDRLEN; + + rc = CALL_CHILD (get_signature_size, pkbuf, i - 1, &signature_size); + if (rc < 0) + goto ERR; + offset += signature_size; + break; + + case IPPROTO_NONE: + /* NONE until we terminate the list of protocols */ + break; + + default: + goto ERR; + } + } + pkbuf->payload = offset; + + rc = CALL (get_type, pkbuf, (hicn_packet_type_t *) (&pkbuf->type)); + if (rc < 0) + goto ERR; + + return HICN_LIB_ERROR_NONE; + +ERR: + pkbuf->format = HICN_PACKET_FORMAT_NONE; + pkbuf->type = HICN_PACKET_TYPE_UNDEFINED; + return HICN_LIB_ERROR_UNEXPECTED; +} + +int +hicn_packet_set_buffer (hicn_packet_buffer_t *pkbuf, u8 *buffer, + uint16_t buffer_size, uint16_t len) +{ + pkbuf_set_header (pkbuf, buffer); + pkbuf->buffer_size = buffer_size; + pkbuf->len = len; + + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_get_buffer (const hicn_packet_buffer_t *pkbuf, u8 **buffer, + uint16_t *buffer_size, uint16_t *len) +{ + *buffer = pkbuf_get_header (pkbuf); + *buffer_size = pkbuf->buffer_size; + *len = pkbuf->len; + return HICN_LIB_ERROR_NONE; +} + +size_t +hicn_packet_get_len (const hicn_packet_buffer_t *pkbuf) +{ + return pkbuf->len; +} + +int +hicn_packet_set_len (hicn_packet_buffer_t *pkbuf, size_t len) +{ + pkbuf->len = len; + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_get_header_len (const hicn_packet_buffer_t *pkbuf, size_t *len) +{ + *len = pkbuf->payload; + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_get_payload_len (const hicn_packet_buffer_t *pkbuf, size_t *len) +{ + *len = hicn_packet_get_len (pkbuf) - pkbuf->payload; + return HICN_LIB_ERROR_NONE; +} + +// XXX this fails with chained membufs in libtransport +int +hicn_packet_set_payload (const hicn_packet_buffer_t *pkbuf, const u8 *payload, + u16 payload_len) +{ + memcpy (pkbuf_get_header (pkbuf) + pkbuf->payload, payload, payload_len); + + return CALL (set_payload_len, pkbuf, payload_len); +} + +int +hicn_packet_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy) +{ + *payload_size = hicn_packet_get_len (pkbuf) - pkbuf->payload; + + if (hard_copy) + { + memcpy (payload, pkbuf_get_header (pkbuf) + pkbuf->payload, + *payload_size); + } + else + { + *payload = pkbuf_get_header (pkbuf) + pkbuf->payload; + } + + return HICN_LIB_ERROR_NONE; +} + +/* Header fields manipulation */ + +int +hicn_packet_get_header_length_from_format (hicn_packet_format_t format, + size_t *header_length) +{ + *header_length = 0; + HICN_PACKET_FORMAT_ENUMERATE (format, i, protocol, { + *header_length += hicn_ops_vft[protocol]->header_len; + }); + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_compute_checksum (const hicn_packet_buffer_t *pkbuf) +{ + return CALL (update_checksums, pkbuf, 0, ~0); +} + +int +hicn_packet_compute_header_checksum (const hicn_packet_buffer_t *pkbuf, + u16 init_sum) +{ + /* payload_len == 0: ignore payload */ + return CALL (update_checksums, pkbuf, init_sum, 0); +} + +int +hicn_packet_check_integrity_no_payload (const hicn_packet_buffer_t *pkbuf, + u16 init_sum) +{ + return CALL (verify_checksums, pkbuf, init_sum, 0); +} + +int +hicn_packet_set_payload_length (const hicn_packet_buffer_t *pkbuf, + const size_t payload_len) +{ + return CALL (set_payload_len, pkbuf, payload_len); +} + +int +hicn_packet_compare (const hicn_packet_buffer_t *pkbuf1, + const hicn_packet_buffer_t *pkbuf2) +{ + hicn_packet_format_t format1 = hicn_packet_get_format (pkbuf1); + hicn_packet_format_t format2 = hicn_packet_get_format (pkbuf2); + + if (format1 != format2) + return HICN_LIB_ERROR_UNEXPECTED; + + size_t len1 = hicn_packet_get_len (pkbuf1); + size_t len2 = hicn_packet_get_len (pkbuf2); + + if (len1 != len2) + return HICN_LIB_ERROR_UNEXPECTED; + + return memcmp (pkbuf_get_header (pkbuf1), pkbuf_get_header (pkbuf2), len1); +} + +int +hicn_packet_get_name (const hicn_packet_buffer_t *pkbuf, hicn_name_t *name) +{ + switch (pkbuf->type) + { + case HICN_PACKET_TYPE_INTEREST: + return hicn_interest_get_name (pkbuf, name); + case HICN_PACKET_TYPE_DATA: + return hicn_data_get_name (pkbuf, name); + default: + return HICN_LIB_ERROR_UNEXPECTED; + } +} + +int +hicn_packet_set_name (const hicn_packet_buffer_t *pkbuf, + const hicn_name_t *name) +{ + switch (pkbuf->type) + { + case HICN_PACKET_TYPE_INTEREST: + return hicn_interest_set_name (pkbuf, name); + case HICN_PACKET_TYPE_DATA: + return hicn_data_set_name (pkbuf, name); + default: + return HICN_LIB_ERROR_UNEXPECTED; + } +} + +int +hicn_packet_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *address) +{ + switch (pkbuf->type) + { + case HICN_PACKET_TYPE_INTEREST: + return hicn_interest_get_locator (pkbuf, address); + case HICN_PACKET_TYPE_DATA: + return hicn_data_get_locator (pkbuf, address); + default: + return HICN_LIB_ERROR_UNEXPECTED; + } +} + +int +hicn_packet_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *address) +{ + switch (pkbuf->type) + { + case HICN_PACKET_TYPE_INTEREST: + return hicn_interest_set_locator (pkbuf, address); + case HICN_PACKET_TYPE_DATA: + return hicn_data_set_locator (pkbuf, address); + default: + return HICN_LIB_ERROR_UNEXPECTED; + } +} + +int +hicn_packet_get_signature_size (const hicn_packet_buffer_t *pkbuf, + size_t *bytes) +{ + return CALL (get_signature_size, pkbuf, bytes); +} + +int +hicn_packet_set_signature_size (const hicn_packet_buffer_t *pkbuf, + size_t bytes) +{ + return CALL (set_signature_size, pkbuf, bytes); +} + +int +hicn_packet_get_signature_padding (const hicn_packet_buffer_t *pkbuf, + size_t *bytes) +{ + return CALL (get_signature_padding, pkbuf, bytes); +} + +int +hicn_packet_set_signature_padding (const hicn_packet_buffer_t *pkbuf, + size_t bytes) +{ + return CALL (set_signature_padding, pkbuf, bytes); +} + +int +hicn_packet_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, + uint64_t signature_timestamp) +{ + return CALL (set_signature_timestamp, pkbuf, signature_timestamp); +} + +int +hicn_packet_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, + uint64_t *signature_timestamp) +{ + return CALL (get_signature_timestamp, pkbuf, signature_timestamp); +} + +int +hicn_packet_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, + uint8_t validation_algorithm) +{ + return CALL (set_validation_algorithm, pkbuf, validation_algorithm); +} + +int +hicn_packet_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, + uint8_t *validation_algorithm) +{ + return CALL (get_validation_algorithm, pkbuf, validation_algorithm); +} + +int +hicn_packet_set_key_id (const hicn_packet_buffer_t *pkbuf, uint8_t *key_id, + size_t key_len) +{ + return CALL (set_key_id, pkbuf, key_id, key_len); +} + +int +hicn_packet_get_key_id (const hicn_packet_buffer_t *pkbuf, uint8_t **key_id, + uint8_t *key_id_len) +{ + return CALL (get_key_id, pkbuf, key_id, key_id_len); +} + +int +hicn_packet_get_lifetime (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t *lifetime) +{ + return CALL (get_lifetime, pkbuf, lifetime); +} + +int +hicn_packet_set_lifetime (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t lifetime) +{ + return CALL (set_lifetime, pkbuf, lifetime); +} + +int +hicn_packet_get_payload_type (const hicn_packet_buffer_t *pkbuf, + hicn_payload_type_t *payload_type) +{ + return CALL (get_payload_type, pkbuf, payload_type); +} + +int +hicn_packet_set_payload_type (const hicn_packet_buffer_t *pkbuf, + hicn_payload_type_t payload_type) +{ + return CALL (set_payload_type, pkbuf, payload_type); +} + +int +hicn_packet_save_header (const hicn_packet_buffer_t *pkbuf, u8 *header, + size_t *header_len, bool copy_ah) +{ + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + if (copy_ah || !HICN_PACKET_FORMAT_IS_AH (format)) + { + int rc = hicn_packet_get_header_len (pkbuf, header_len); + if (HICN_LIB_IS_ERROR (rc)) + return rc; + } + else + { + /* Copy up until the ah header (which we assume is last) */ + *header_len = pkbuf->ah; + } + + memcpy (header, pkbuf_get_header (pkbuf), *header_len); + + return HICN_LIB_ERROR_NONE; +} + +int +hicn_packet_load_header (const hicn_packet_buffer_t *pkbuf, const u8 *header, + size_t header_len) +{ + memcpy (pkbuf_get_header (pkbuf), header, header_len); + + return HICN_LIB_ERROR_NONE; +} + +/* Interest */ + +int +hicn_interest_get_name (const hicn_packet_buffer_t *pkbuf, hicn_name_t *name) +{ + return CALL (get_interest_name, pkbuf, name); +} + +int +hicn_interest_set_name (const hicn_packet_buffer_t *pkbuf, + const hicn_name_t *name) +{ + return CALL (set_interest_name, pkbuf, name); +} + +int +hicn_interest_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *address) +{ + return CALL (get_interest_locator, pkbuf, address); +} + +int +hicn_interest_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *address) +{ + return CALL (set_interest_locator, pkbuf, address); +} + +int +hicn_interest_compare (const hicn_packet_buffer_t *pkbuf1, + const hicn_packet_buffer_t *pkbuf2) +{ + return hicn_packet_compare (pkbuf1, pkbuf2); +} + +int +hicn_interest_get_lifetime (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t *lifetime) +{ + return hicn_packet_get_lifetime (pkbuf, lifetime); +} + +int +hicn_interest_set_lifetime (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t lifetime) +{ + return hicn_packet_set_lifetime (pkbuf, lifetime); +} + +int +hicn_interest_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy) +{ + return hicn_packet_get_payload (pkbuf, payload, payload_size, hard_copy); +} + +int +hicn_interest_set_payload (const hicn_packet_buffer_t *pkbuf, + const u8 *payload, size_t payload_len) +{ + return hicn_packet_set_payload (pkbuf, payload, (u16) payload_len); +} + +int +hicn_interest_reset_for_hash (hicn_packet_buffer_t *pkbuf) +{ + return CALL (reset_interest_for_hash, pkbuf); +} + +/* Data */ + +int +hicn_data_get_name (const hicn_packet_buffer_t *pkbuf, hicn_name_t *name) +{ + return CALL (get_data_name, pkbuf, name); +} + +int +hicn_data_set_name (const hicn_packet_buffer_t *pkbuf, const hicn_name_t *name) +{ + return CALL (set_data_name, pkbuf, name); +} + +int +hicn_data_get_locator (const hicn_packet_buffer_t *pkbuf, + hicn_ip_address_t *address) +{ + return CALL (get_data_locator, pkbuf, address); +} + +int +hicn_data_set_locator (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *address) +{ + return CALL (set_data_locator, pkbuf, address); +} + +int +hicn_data_compare (const hicn_packet_buffer_t *pkbuf1, + const hicn_packet_buffer_t *pkbuf2) +{ + return hicn_packet_compare (pkbuf1, pkbuf2); +} + +int +hicn_data_get_expiry_time (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t *expiry_time) +{ + return hicn_packet_get_lifetime (pkbuf, expiry_time); +} + +int +hicn_data_set_expiry_time (const hicn_packet_buffer_t *pkbuf, + hicn_lifetime_t expiry_time) +{ + return hicn_packet_set_lifetime (pkbuf, expiry_time); +} + +/* Path label */ + +int +hicn_data_get_path_label (const hicn_packet_buffer_t *pkbuf, + hicn_path_label_t *path_label) +{ + return CALL (get_data_path_label, pkbuf, path_label); +} + +int +hicn_get_path_label (const hicn_packet_buffer_t *pkbuf, + hicn_path_label_t *path_label) +{ + if (!hicn_packet_is_data (pkbuf)) + return INVALID_PATH_LABEL; + return hicn_data_get_path_label (pkbuf, path_label); +} + +int +hicn_data_set_path_label (const hicn_packet_buffer_t *pkbuf, + hicn_path_label_t path_label) +{ + return CALL (set_data_path_label, pkbuf, path_label); +} + +int +hicn_data_set_payload (const hicn_packet_buffer_t *pkbuf, const u8 *payload, + size_t payload_len) +{ + return hicn_packet_set_payload (pkbuf, payload, (u16) payload_len); +} + +int +hicn_data_get_payload (const hicn_packet_buffer_t *pkbuf, u8 **payload, + size_t *payload_size, bool hard_copy) +{ + return hicn_packet_get_payload (pkbuf, payload, payload_size, hard_copy); +} + +int +hicn_data_reset_for_hash (hicn_packet_buffer_t *pkbuf) +{ + return CALL (reset_data_for_hash, pkbuf); +} + +int +hicn_data_is_last (const hicn_packet_buffer_t *pkbuf, int *is_last) +{ + return CALL (is_last_data, pkbuf, is_last); +} + +int +hicn_data_set_last (const hicn_packet_buffer_t *pkbuf) +{ + return CALL (set_last_data, pkbuf); +} + +int +hicn_packet_get_signature (const hicn_packet_buffer_t *pkbuf, + uint8_t **sign_buf) +{ + return CALL (get_signature, pkbuf, sign_buf); +} + +int +hicn_interest_rewrite (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) +{ + return CALL (rewrite_interest, pkbuf, addr_new, addr_old); +} + +int +hicn_data_rewrite (const hicn_packet_buffer_t *pkbuf, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) +{ + return CALL (rewrite_data, pkbuf, addr_new, addr_old, face_id, reset_pl); +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/policy.c b/lib/src/policy.c index 694f0ea5e..4a4b9fa89 100644 --- a/lib/src/policy.c +++ b/lib/src/policy.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -21,41 +21,53 @@ #include <stdio.h> #include <hicn/policy.h> -const char * policy_tag_str[] = { - #define _(x, y) [POLICY_TAG_ ## x] = STRINGIZE(x), - foreach_policy_tag - #undef _ +const char *policy_tag_str[] = { +#define _(x, y) [POLICY_TAG_##x] = STRINGIZE (x), + foreach_policy_tag +#undef _ }; const char policy_tag_short_str[] = { - #define _(x, y) [POLICY_TAG_ ## x] = y, - foreach_policy_tag - #undef _ +#define _(x, y) [POLICY_TAG_##x] = y, + foreach_policy_tag +#undef _ }; -const char * policy_state_str[] = { - #define _(x) [POLICY_STATE_ ## x] = STRINGIZE(x), - foreach_policy_state - #undef _ +const char *policy_state_str[] = { +#define _(x) [POLICY_STATE_##x] = STRINGIZE (x), + foreach_policy_state +#undef _ }; +policy_state_t +policy_state_from_str (const char *str) +{ +#define _(x) \ + if (strcasecmp (str, #x) == 0) \ + return POLICY_STATE_##x; \ + else + foreach_policy_state +#undef _ + return POLICY_STATE_N; +} + int -policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state) +policy_tag_state_snprintf (char *s, size_t size, + const policy_tag_state_t *tag_state) { - char *cur = s; - int rc; - - if (tag_state->disabled > 1) - return -1; - - rc = snprintf(cur, s + size - cur, "%s%s", (tag_state->disabled == 1) ? "!" : "", policy_state_str[tag_state->state]); - if (rc >= (int)(s + size - cur)) - return (int)(s + size - cur); - if (rc < 0) - return rc; - cur += rc; - if (size != 0 && cur >= s + size) - return (int)(cur - s); - - return (int)(cur - s); + char *cur = s; + int rc; + + if (tag_state->disabled > 1) + return -1; + + rc = snprintf (cur, s + size - cur, "%s%s", + (tag_state->disabled == 1) ? "!" : "", + policy_state_str[tag_state->state]); + if (rc >= (int) (s + size - cur)) + return (int) (s + size - cur); + if (rc < 0) + return rc; + cur += rc; + return (int) (cur - s); } diff --git a/lib/src/protocol.h b/lib/src/protocol.h new file mode 100644 index 000000000..58bbb2ac2 --- /dev/null +++ b/lib/src/protocol.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol.h + * @brief Protocol header definitions + */ +#ifndef HICN_PROTOCOL_H +#define HICN_PROTOCOL_H + +#include "protocol/ah.h" +#include "protocol/ipv4.h" +#include "protocol/ipv6.h" +#include "protocol/icmp.h" +#include "protocol/icmprd.h" +#include "protocol/tcp.h" +#include "protocol/udp.h" +#include "protocol/new.h" + +#endif /* HICN_PROTOCOL_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/ah.c b/lib/src/protocol/ah.c index 13340eae1..ea028d559 100644 --- a/lib/src/protocol/ah.c +++ b/lib/src/protocol/ah.c @@ -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: @@ -18,12 +18,12 @@ * @brief hICN operations for AH header */ -#include <string.h> // memcpy #include <hicn/common.h> #include <hicn/error.h> -#include <hicn/header.h> -#include <hicn/ops.h> -#include <hicn/protocol/ah.h> +#include <string.h> // memcpy + +#include "../ops.h" +#include "ah.h" DECLARE_get_interest_locator (ah, UNEXPECTED); DECLARE_set_interest_locator (ah, UNEXPECTED); @@ -31,206 +31,242 @@ DECLARE_get_interest_name (ah, UNEXPECTED); DECLARE_set_interest_name (ah, UNEXPECTED); DECLARE_get_interest_name_suffix (ah, UNEXPECTED); DECLARE_set_interest_name_suffix (ah, UNEXPECTED); -DECLARE_mark_packet_as_interest (ah, UNEXPECTED); -DECLARE_mark_packet_as_data (ah, UNEXPECTED); +DECLARE_get_type (ah, UNEXPECTED); +DECLARE_set_type (ah, UNEXPECTED); DECLARE_get_data_locator (ah, UNEXPECTED); DECLARE_set_data_locator (ah, UNEXPECTED); DECLARE_get_data_name (ah, UNEXPECTED); DECLARE_set_data_name (ah, UNEXPECTED); DECLARE_get_data_name_suffix (ah, UNEXPECTED); DECLARE_set_data_name_suffix (ah, UNEXPECTED); -DECLARE_get_data_pathlabel (ah, UNEXPECTED); -DECLARE_set_data_pathlabel (ah, UNEXPECTED); -DECLARE_update_data_pathlabel (ah, UNEXPECTED); +DECLARE_get_data_path_label (ah, UNEXPECTED); +DECLARE_set_data_path_label (ah, UNEXPECTED); +DECLARE_update_data_path_label (ah, UNEXPECTED); DECLARE_get_lifetime (ah, UNEXPECTED); DECLARE_set_lifetime (ah, UNEXPECTED); -DECLARE_get_payload_length (ah, UNEXPECTED); -DECLARE_set_payload_length (ah, UNEXPECTED); +// DECLARE_get_payload_len (ah, UNEXPECTED); +DECLARE_set_payload_len (ah, UNEXPECTED); +DECLARE_get_payload_type (ah, UNEXPECTED); +DECLARE_set_payload_type (ah, UNEXPECTED); +DECLARE_is_last_data (ah, UNEXPECTED); +DECLARE_set_last_data (ah, UNEXPECTED); int -ah_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +ah_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { - /* *INDENT-OFF* */ - h->ah = (_ah_header_t){ + pkbuf->ah = pkbuf->len; + if (AH_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += AH_HDRLEN; + + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + /* clang-format off */ + *ah = (_ah_header_t){ .nh = (u8) 0, .payloadlen = (u8) 0, .reserved = (u16) 0, }; - /* *INDENT-ON* */ - return CHILD_OPS (init_packet_header, type, h); + /* clang-format on */ + return CALL_CHILD (init_packet_header, pkbuf, pos); } int -ah_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +ah_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + size_t signature_size; - int rc = - hicn_ops_vft[type.l1]->get_signature_size (type, h, &signature_size); + int rc = CALL (get_signature_size, pkbuf, &signature_size); if (rc < 0) return rc; - memset (&(h->ah.validationPayload), 0, signature_size); - return CHILD_OPS (reset_interest_for_hash, type, h); + memset (&(ah->validationPayload), 0, signature_size); + ah->signaturePadding = 0; + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -ah_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +ah_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + size_t signature_size; - int rc = - hicn_ops_vft[type.l1]->get_signature_size (type, h, &signature_size); + int rc = CALL (get_signature_size, pkbuf, &signature_size); if (rc < 0) return rc; - memset (&(h->ah.validationPayload), 0, signature_size); - return CHILD_OPS (reset_interest_for_hash, type, h); + memset (&(ah->validationPayload), 0, signature_size); + ah->signaturePadding = 0; + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -ah_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ah_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { /* Nothing to do as there is no checksum in AH */ return HICN_LIB_ERROR_NONE; } int -ah_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ah_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 *old_val, u16 *new_val, u8 size, + bool skip_first) { - /* Nothing to do as there is no checksum in AH */ - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (update_checksums_incremental, pkbuf, pos, old_val, + new_val, size, false); } int -ah_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old) +ah_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { - /* Nothing to do on signature */ + /* Nothing to do as there is no checksum in AH */ return HICN_LIB_ERROR_NONE; } int -ah_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl) +ah_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) { /* Nothing to do on signature */ return HICN_LIB_ERROR_NONE; } int -ah_get_length (hicn_type_t type, const hicn_protocol_t *h, size_t *length) -{ - return HICN_LIB_ERROR_NOT_IMPLEMENTED; -} - -int -ah_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ah_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) { - *header_length = AH_HDRLEN + (h->ah.payloadlen << 2); + /* Nothing to do on signature */ return HICN_LIB_ERROR_NONE; } int -ah_get_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ah_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) { - size_t child_header_length = 0; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - *header_length = AH_HDRLEN + (h->ah.payloadlen << 2) + child_header_length; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + *signature = ah->validationPayload; return HICN_LIB_ERROR_NONE; } int -ah_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +ah_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) { - *signature = h->ah.validationPayload; + *flag = true; return HICN_LIB_ERROR_NONE; } int -ah_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, +ah_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, size_t *signature_size) { - *signature_size = h->ah.payloadlen << 2; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + *signature_size = ah->payloadlen << 2; return HICN_LIB_ERROR_NONE; } int -ah_set_signature_size (hicn_type_t type, hicn_protocol_t *h, - const size_t signature_size) +ah_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) { - h->ah.payloadlen = (u8) (signature_size >> 2); + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + ah->payloadlen = signature_size >> 2; return HICN_LIB_ERROR_NONE; } int -ah_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, +ah_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, uint64_t signature_timestamp) { - uint64_t netwok_order_timestamp = htonll (signature_timestamp); - memcpy (h->ah.timestamp_as_u8, &netwok_order_timestamp, sizeof (uint64_t)); + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + uint64_t netwok_order_timestamp = hicn_net_to_host_64 (signature_timestamp); + memcpy (ah->timestamp_as_u8, &netwok_order_timestamp, sizeof (uint64_t)); return HICN_LIB_ERROR_NONE; } int -ah_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, +ah_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, uint64_t *signature_timestamp) { - memcpy (signature_timestamp, h->ah.timestamp_as_u8, sizeof (uint64_t)); - *signature_timestamp = ntohll (*signature_timestamp); + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + memcpy (signature_timestamp, ah->timestamp_as_u8, sizeof (uint64_t)); + *signature_timestamp = hicn_host_to_net_64 (*signature_timestamp); return HICN_LIB_ERROR_NONE; } int -ah_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, +ah_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t validation_algorithm) { - h->ah.validationAlgorithm = validation_algorithm; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + ah->validationAlgorithm = validation_algorithm; return HICN_LIB_ERROR_NONE; } int -ah_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, +ah_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *validation_algorithm) { - *validation_algorithm = h->ah.validationAlgorithm; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + *validation_algorithm = ah->validationAlgorithm; return HICN_LIB_ERROR_NONE; } int -ah_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ah_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) { - h->ah.signatureGap = gap; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + ah->signaturePadding = padding; return HICN_LIB_ERROR_NONE; } int -ah_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, uint8_t *gap) +ah_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) { - *gap = h->ah.signatureGap; + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + *padding = ah->signaturePadding; return HICN_LIB_ERROR_NONE; } int -ah_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +ah_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *key_id, + size_t size) { - memcpy (h->ah.keyId, key_id, sizeof (h->ah.keyId)); + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + if (size != sizeof (ah->keyId)) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + memcpy (ah->keyId, key_id, sizeof (ah->keyId)); return HICN_LIB_ERROR_NONE; } int -ah_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, +ah_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t **key_id, uint8_t *key_id_size) { - *key_id = h->ah.keyId; - *key_id_size = sizeof (h->ah.keyId); + _ah_header_t *ah = pkbuf_get_ah (pkbuf); + + *key_id = ah->keyId; + *key_id_size = sizeof (ah->keyId); return HICN_LIB_ERROR_NONE; } -DECLARE_HICN_OPS (ah); +DECLARE_HICN_OPS (ah, AH_HDRLEN); /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/protocol/ah.h b/lib/src/protocol/ah.h new file mode 100644 index 000000000..ee124f92a --- /dev/null +++ b/lib/src/protocol/ah.h @@ -0,0 +1,83 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol/ah.h + * @brief AH packet header + */ +#ifndef HICN_PROTOCOL_AH_H +#define HICN_PROTOCOL_AH_H + +#include <hicn/common.h> + +/* + * The TCP PSH flag is set to indicate TCP payload in fact contains a AH header + * with signature information for the packet + */ +#define AH_FLAG 0x10 + +/* + * The length of the AH struct must be 44 bytes. + */ +#define EXPECTED_AH_HDRLEN 44 + +typedef struct +{ + u8 nh; // To match with reserved in IPSEC AH + // Length of the signature field. Note that the signature might be smaller + // than the field: the actual size is computed from the field size and + // signaturePadding. + u8 payloadlen; + union + { + u16 reserved; + + struct + { + u8 validationAlgorithm; + u8 signaturePadding; + }; + }; + union + { + struct + { + u32 spi; + u32 seq; + }; + // Unix timestamp indicating when the signature has been calculated + u8 timestamp_as_u8[8]; + u16 timestamp_as_u16[4]; + u32 timestamp_as_u32[2]; + }; + // ICV would follow + u8 keyId[32]; // Hash of pub key + /* 44 B + validationPayload */ + u8 validationPayload[0]; // Holds the signature +} _ah_header_t; + +#define AH_HDRLEN sizeof (_ah_header_t) +static_assert (EXPECTED_AH_HDRLEN == AH_HDRLEN, + "Size of AH Struct does not match its expected size."); + +#endif /* HICN_PROTOCOL_AH_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/icmp.c b/lib/src/protocol/icmp.c index 5783cf52c..add871bcf 100644 --- a/lib/src/protocol/icmp.c +++ b/lib/src/protocol/icmp.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021-2023 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: @@ -13,10 +13,11 @@ * limitations under the License. */ -#include <string.h> -#include <hicn/protocol/icmp.h> #include <hicn/error.h> -#include <hicn/ops.h> +#include <string.h> + +#include "icmp.h" +#include "../ops.h" DECLARE_get_interest_locator (icmp, UNEXPECTED); DECLARE_set_interest_locator (icmp, UNEXPECTED); @@ -24,81 +25,104 @@ DECLARE_get_interest_name (icmp, UNEXPECTED); DECLARE_set_interest_name (icmp, UNEXPECTED); DECLARE_get_interest_name_suffix (icmp, UNEXPECTED); DECLARE_set_interest_name_suffix (icmp, UNEXPECTED); -DECLARE_mark_packet_as_interest (icmp, UNEXPECTED); -DECLARE_mark_packet_as_data (icmp, UNEXPECTED); DECLARE_get_data_locator (icmp, UNEXPECTED); DECLARE_set_data_locator (icmp, UNEXPECTED); DECLARE_get_data_name (icmp, UNEXPECTED); DECLARE_set_data_name (icmp, UNEXPECTED); DECLARE_get_data_name_suffix (icmp, UNEXPECTED); DECLARE_set_data_name_suffix (icmp, UNEXPECTED); -DECLARE_get_data_pathlabel (icmp, UNEXPECTED); -DECLARE_set_data_pathlabel (icmp, UNEXPECTED); -DECLARE_update_data_pathlabel (icmp, UNEXPECTED); +DECLARE_get_data_path_label (icmp, UNEXPECTED); +DECLARE_set_data_path_label (icmp, UNEXPECTED); +DECLARE_update_data_path_label (icmp, UNEXPECTED); DECLARE_get_lifetime (icmp, UNEXPECTED); DECLARE_set_lifetime (icmp, UNEXPECTED); -DECLARE_get_length (icmp, UNEXPECTED); -DECLARE_get_payload_length (icmp, UNEXPECTED); -DECLARE_set_payload_length (icmp, UNEXPECTED); +// DECLARE_get_payload_len (icmp, UNEXPECTED); +DECLARE_set_payload_len (icmp, UNEXPECTED); +DECLARE_get_payload_type (icmp, UNEXPECTED); +DECLARE_set_payload_type (icmp, UNEXPECTED); DECLARE_get_signature (icmp, UNEXPECTED); +DECLARE_has_signature (icmp, UNEXPECTED); +DECLARE_is_last_data (icmp, UNEXPECTED); +DECLARE_set_last_data (icmp, UNEXPECTED); int -icmp_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +icmp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { - h->icmp = (_icmp_header_t){ + pkbuf->icmp = pkbuf->len; + if (ICMP_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += ICMP_HDRLEN; + + _icmp_header_t *icmp = pkbuf_get_icmp (pkbuf); + + *icmp = (_icmp_header_t){ .type = 0, .code = 0, .csum = 0, }; - return HICN_LIB_ERROR_NONE; // CHILD_OPS(init_packet_header, type, h->icmp); + return CALL_CHILD (init_packet_header, pkbuf, pos); } int -icmp_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +icmp_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { - h->icmp.csum = 0; + _icmp_header_t *icmp = pkbuf_get_icmp (pkbuf); + + icmp->csum = 0; - return CHILD_OPS (reset_interest_for_hash, type, h); + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -icmp_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +icmp_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { - h->icmp.csum = 0; + _icmp_header_t *icmp = pkbuf_get_icmp (pkbuf); + + icmp->csum = 0; - return CHILD_OPS (reset_data_for_hash, type, h); + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); } int -icmp_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +icmp_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; - // h->icmp.csum = 0; - // h->icmp.csum = csum(h->bytes, TCP_HDRLEN + payload_length, + // icmp->csum = 0; + // icmp->csum = csum(h->bytes, TCP_HDRLEN + payload_len, // ~partial_csum); // - // return CHILD_OPS(update_checksums, type, h->icmp, 0, payload_length); + // return CALL_CHILD(update_checksums, pkbuf, pos->icmp, 0, + // payload_len); } int -icmp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +icmp_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; - // if (csum(h->bytes, TCP_HDRLEN + payload_length, ~partial_csum) != 0) +} + +int +icmp_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + return HICN_LIB_ERROR_NOT_IMPLEMENTED; + // if (csum(h->bytes, TCP_HDRLEN + payload_len, ~partial_csum) != 0) // return HICN_LIB_ERROR_CORRUPTED_PACKET; - // return CHILD_OPS(verify_checksums, type, h->icmp, 0, payload_length); + // return CALL_CHILD(verify_checksums, pkbuf, pos->icmp, 0, + // payload_len); } int -icmp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) +icmp_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; - // u16 *icmp_checksum = &(h->icmp.csum); + // u16 *icmp_checksum = &(icmp->csum); // // /* // * Padding fields are set to zero so we can apply checksum on the @@ -119,12 +143,13 @@ icmp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, } int -icmp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl) +icmp_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; - // u16 *icmp_checksum = &(h->icmp.csum); + // u16 *icmp_checksum = &(icmp->csum); // // /* // * Padding fields are set to zero so we can apply checksum on the @@ -139,9 +164,9 @@ icmp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, // csum = ip_csum_add_even (csum, addr_new->ip6.as_u64[0]); // csum = ip_csum_add_even (csum, addr_new->ip6.as_u64[1]); // - // csum = ip_csum_sub_even (csum, h->icmp.pathlabel); - // icmp_update_data_pathlabel(type, h, face_id); - // csum = ip_csum_add_even (csum, h->icmp.pathlabel); + // csum = ip_csum_sub_even (csum, icmp->path_label); + // icmp_update_data_path_label(pkbuf, pos, face_id); + // csum = ip_csum_add_even (csum, icmp->path_label); // // *icmp_checksum = ip_csum_fold (csum); // @@ -149,95 +174,93 @@ icmp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, } int -icmp_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +icmp_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) { - *header_length = ICMP_HDRLEN; + *type = HICN_PACKET_TYPE_MAPME; return HICN_LIB_ERROR_NONE; } int -icmp_get_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +icmp_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) { - size_t child_header_length = 0; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - - *header_length = ICMP_HDRLEN + child_header_length; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_type, pkbuf, pos, type); } int -icmp_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, +icmp_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, size_t *signature_size) { - return CHILD_OPS (get_signature_size, type, h, signature_size); + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); } int -icmp_set_signature_size (hicn_type_t type, hicn_protocol_t *h, +icmp_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, size_t signature_size) { - return CHILD_OPS (set_signature_size, type, h, signature_size); + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); } int -icmp_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +icmp_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); } int -icmp_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +icmp_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); } int -icmp_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, +icmp_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, uint64_t signature_timestamp) { - return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); } int -icmp_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, +icmp_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, uint64_t *signature_timestamp) { - return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); } int -icmp_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, +icmp_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t validation_algorithm) { - return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -icmp_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, +icmp_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *validation_algorithm) { - return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -icmp_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +icmp_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *key_id, size_t key_len) { - return CHILD_OPS (set_key_id, type, h, key_id); + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); } int -icmp_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, - uint8_t *key_id_size) +icmp_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) { - return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); } -DECLARE_HICN_OPS (icmp); +DECLARE_HICN_OPS (icmp, ICMP_HDRLEN); /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/protocol/icmp.h b/lib/src/protocol/icmp.h new file mode 100644 index 000000000..6cbba6398 --- /dev/null +++ b/lib/src/protocol/icmp.h @@ -0,0 +1,84 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol/icmp.h + * @brief ICMP packet header + */ +#ifndef HICN_PROTOCOL_ICMP_H +#define HICN_PROTOCOL_ICMP_H + +#include <hicn/common.h> + +/* + * The length of the ICMP header struct must be 4 bytes. + */ +#define EXPECTED_ICMP_HDRLEN 4 + +typedef struct +{ + u8 type; + u8 code; + u16 csum; +} _icmp_header_t; + +#define ICMP_HDRLEN sizeof (_icmp_header_t) +static_assert (EXPECTED_ICMP_HDRLEN == ICMP_HDRLEN, + "Size of ICMP struct does not match its expected size."); + +/* + * The length of the ICMPWLDR header struct must be 4 bytes. + */ +#define EXPECTED_ICMPWLDR_HDRLEN 8 + +typedef struct +{ + u8 type; + u8 code; + u16 csum; + union + { + struct + { + u16 id; + u16 sequence; + } echo; /* echo datagram */ + u32 gateway; /* gateway address */ + struct + { + u16 _unused; + u16 mtu; + } frag; /* path mtu discovery */ + struct + { + u16 expected_lbl; + u16 received_lbl; + } wldr_notification_lbl; + }; +} _icmp_wldr_header_t; + +#define ICMPWLDR_HDRLEN sizeof (_icmp_wldr_header_t) +static_assert (EXPECTED_ICMPWLDR_HDRLEN == ICMPWLDR_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); + +#endif /* HICN_PROTOCOL_ICMP_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/icmprd.h b/lib/src/protocol/icmprd.h new file mode 100644 index 000000000..7edff8111 --- /dev/null +++ b/lib/src/protocol/icmprd.h @@ -0,0 +1,72 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol/icmp-rd.c + * @brief hICN operations for ICMP Redirect header + */ +#ifndef HICN_PROTOCOL_ICMPRD_H +#define HICN_PROTOCOL_ICMPRD_H + +#include <hicn/common.h> +#include "ipv4.h" + +/* + * The length of the ICMPRD4 header struct must be 92 bytes. + */ +#define EXPECTED_ICMPRD4_HDRLEN 92 + +typedef struct +{ + u8 type; + u8 code; + u16 csum; + ipv4_address_t ip; + _ipv4_header_t iph; + u8 data[64]; +} _icmprd4_header_t; + +#define ICMPRD4_HDRLEN sizeof (_icmprd4_header_t) +static_assert (EXPECTED_ICMPRD4_HDRLEN == ICMPRD4_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); + +/* + * The length of the ICMPRD header struct must be 40 bytes. + */ +#define EXPECTED_ICMPRD_HDRLEN 40 + +typedef struct +{ + u8 type; + u8 code; + u16 csum; + u32 res; + ipv6_address_t tgt; + ipv6_address_t dst; +} _icmprd_header_t; + +#define ICMPRD_HDRLEN sizeof (_icmprd_header_t) +static_assert (EXPECTED_ICMPRD_HDRLEN == ICMPRD_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); + +#endif /* HICN_PROTOCOL_ICMPRD_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/ipv4.c b/lib/src/protocol/ipv4.c index cf50f9996..daa5a706a 100644 --- a/lib/src/protocol/ipv4.c +++ b/lib/src/protocol/ipv4.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -14,7 +14,7 @@ */ /** - * @file protocol/ipv4.c + * @file protocol/ipv4->c * @brief hICN operations for IPv4 header * * NOTE: IPv4 options (affecting the header size) are currently not supported. @@ -28,241 +28,262 @@ #include <string.h> #include <hicn/error.h> -#include <hicn/ops.h> #include <hicn/common.h> -#include <hicn/header.h> -#include <hicn/protocol/ipv4.h> -typedef unsigned short u_short; -int ipv4_get_payload_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *payload_length); +#include "../ops.h" +#include "ipv4.h" + +#define UINT16_T_MASK 0x0000ffff // 1111 1111 1111 1111 + +#define ipv4_get_payload_len(pkbuf, ipv4) htons (ipv4->len - pkbuf->payload) int -ipv4_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +ipv4_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { - size_t total_header_length; - int rc = - hicn_ops_vft[type.l1]->get_header_length (type, h, &total_header_length); - if (rc < 0) - return rc; + assert (pkbuf->len == 0); + if (IPV4_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += IPV4_HDRLEN; - h->ipv4 = (_ipv4_header_t){ + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + + size_t header_len; + hicn_packet_get_header_length_from_format (pkbuf->format, &header_len); + + /* We initialize the len considering an empty payload */ + *ipv4 = (_ipv4_header_t){ .version_ihl = (IPV4_DEFAULT_VERSION << 4) | (0x0f & IPV4_DEFAULT_IHL), .tos = IPV4_DEFAULT_TOS, - .len = htons ((u16) total_header_length), + .len = htons (header_len), .id = htons (IPV4_DEFAULT_ID), .frag_off = htons (IPV4_DEFAULT_FRAG_OFF), .ttl = HICN_DEFAULT_TTL, - .protocol = type.l2, + .protocol = HICN_PACKET_FORMAT_GET (format, pos + 1), .csum = 0, .saddr.as_u32 = 0, .daddr.as_u32 = 0, }; - return CHILD_OPS (init_packet_header, type, h); + return CALL_CHILD (init_packet_header, pkbuf, pos); } int -ipv4_get_interest_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) +ipv4_get_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) { - ip_address->ip4 = h->ipv4.saddr; + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ip_address->v4 = ipv4->saddr; return HICN_LIB_ERROR_NONE; } int -ipv4_set_interest_locator (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address) +ipv4_set_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) { - h->ipv4.saddr = ip_address->ip4; + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ipv4->saddr = ip_address->v4; return HICN_LIB_ERROR_NONE; } int -ipv4_get_interest_name (hicn_type_t type, const hicn_protocol_t *h, +ipv4_get_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + name->prefix.v4 = ipv4->daddr; + return CALL_CHILD (get_interest_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv4_set_interest_name (hicn_type_t type, hicn_protocol_t *h, +ipv4_set_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ipv4->daddr = name->prefix.v4; + return CALL_CHILD (set_interest_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv4_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, +ipv4_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_name_suffix_t *suffix) { - return CHILD_OPS (get_interest_name_suffix, type, h, suffix); + return CALL_CHILD (get_interest_name_suffix, pkbuf, pos, suffix); } int -ipv4_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, +ipv4_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_suffix_t *suffix) { - return CHILD_OPS (set_interest_name_suffix, type, h, suffix); + return CALL_CHILD (set_interest_name_suffix, pkbuf, pos, suffix); } int -ipv4_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +ipv4_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) { - return CHILD_OPS (mark_packet_as_interest, type, h); + return CALL_CHILD (get_type, pkbuf, pos, type); } int -ipv4_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) +ipv4_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) { - return CHILD_OPS (mark_packet_as_data, type, h); + return CALL_CHILD (set_type, pkbuf, pos, type); } int -ipv4_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +ipv4_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + /* Sets everything to 0 up to IP destination address */ - memset (&(h->ipv4), 0, 16); + memset (ipv4, 0, 16); - return CHILD_OPS (reset_interest_for_hash, type, h); + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -ipv4_get_data_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) +ipv4_get_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) { - ip_address->ip4 = h->ipv4.daddr; + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ip_address->v4 = ipv4->daddr; return HICN_LIB_ERROR_NONE; } int -ipv4_set_data_locator (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address) +ipv4_set_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) { - h->ipv4.daddr = ip_address->ip4; + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ipv4->daddr = ip_address->v4; return HICN_LIB_ERROR_NONE; } int -ipv4_get_data_name (hicn_type_t type, const hicn_protocol_t *h, +ipv4_get_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + name->prefix.v4 = ipv4->saddr; + return CALL_CHILD (get_data_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv4_set_data_name (hicn_type_t type, hicn_protocol_t *h, +ipv4_set_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + ipv4->saddr = name->prefix.v4; + return CALL_CHILD (set_data_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv4_get_data_name_suffix (hicn_type_t type, const hicn_protocol_t *h, +ipv4_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_name_suffix_t *suffix) { - return CHILD_OPS (get_data_name_suffix, type, h, suffix); + return CALL_CHILD (get_data_name_suffix, pkbuf, pos, suffix); } int -ipv4_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, +ipv4_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_suffix_t *suffix) { - return CHILD_OPS (set_data_name_suffix, type, h, suffix); + return CALL_CHILD (set_data_name_suffix, pkbuf, pos, suffix); } int -ipv4_get_data_pathlabel (hicn_type_t type, const hicn_protocol_t *h, - u32 *pathlabel) +ipv4_get_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t *path_label) { - return CHILD_OPS (get_data_pathlabel, type, h, pathlabel); + return CALL_CHILD (get_data_path_label, pkbuf, pos, path_label); } int -ipv4_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const u32 pathlabel) +ipv4_set_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t path_label) { - return CHILD_OPS (set_data_pathlabel, type, h, pathlabel); + return CALL_CHILD (set_data_path_label, pkbuf, pos, path_label); } int -ipv4_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const hicn_faceid_t face_id) +ipv4_update_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id) { - return CHILD_OPS (update_data_pathlabel, type, h, face_id); + return CALL_CHILD (update_data_path_label, pkbuf, pos, face_id); } int -ipv4_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +ipv4_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + /* Sets everything to 0 up to source address */ - memset (&h->ipv4, 0, 12); + memset (ipv4, 0, 12); /* Clears destination address */ - memset (&(h->ipv4.daddr), 0, 4); + memset (&(ipv4->daddr), 0, 4); - return CHILD_OPS (reset_data_for_hash, type, h); + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); } int -ipv4_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, +ipv4_get_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_lifetime_t *lifetime) { - return CHILD_OPS (get_lifetime, type, h, lifetime); + return CALL_CHILD (get_lifetime, pkbuf, pos, lifetime); } int -ipv4_set_lifetime (hicn_type_t type, hicn_protocol_t *h, +ipv4_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_lifetime_t lifetime) { - return CHILD_OPS (set_lifetime, type, h, lifetime); + return CALL_CHILD (set_lifetime, pkbuf, pos, lifetime); } int -ipv4_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ipv4_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + /* * Checksum field is not accounted for in lower layers, so we can compute * them in any order. Note that it is only a header checksum. */ - h->ipv4.csum = 0; - h->ipv4.csum = csum (h, IPV4_HDRLEN, 0); + ipv4->csum = 0; + ipv4->csum = csum (pkbuf_get_header (pkbuf), IPV4_HDRLEN, 0); - /* Retrieve payload length if not specified, as it is not available later */ - if (payload_length == 0) + /* Retrieve payload len if not specified, as it is not available later */ + if (payload_len == 0) { - int rc = ipv4_get_payload_length (type, h, &payload_length); - if (rc < 0) - return rc; + payload_len = ipv4_get_payload_len (pkbuf, ipv4); } - /* Ignore the payload if payload_length = ~0 */ - if (payload_length == ~0) + /* Ignore the payload if payload_len = ~0 */ + if (payload_len == ~0) { - payload_length = 0; + payload_len = 0; } /* Build pseudo-header */ ipv4_pseudo_header_t psh; - psh.ip_src = h->ipv4.saddr; - psh.ip_dst = h->ipv4.daddr; + psh.ip_src = ipv4->saddr; + psh.ip_dst = ipv4->daddr; /* Size is u32 and not u16, we cannot copy and need to care about endianness */ - psh.size = htons (ntohs (h->ipv4.len) - (u16) IPV4_HDRLEN); + psh.size = htons (ntohs (ipv4->len) - (u16) IPV4_HDRLEN); psh.zero = 0; - psh.protocol = (u8) h->ipv4.protocol; + psh.protocol = (u8) ipv4->protocol; /* Compute partial checksum based on pseudo-header */ if (partial_csum != 0) @@ -271,217 +292,245 @@ ipv4_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, } partial_csum = csum (&psh, IPV4_PSHDRLEN, partial_csum); - return CHILD_OPS (update_checksums, type, h, partial_csum, payload_length); + return CALL_CHILD (update_checksums, pkbuf, pos, partial_csum, payload_len); +} + +int +ipv4_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) +{ + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + /* We update the child first */ + int rc = CALL_CHILD (update_checksums_incremental, pkbuf, pos, old_val, + new_val, size, false); + if (rc < 0) + return rc; + + if (!skip_first) + { + for (uint8_t i = 0; i < size; i++) + { + uint16_t old_csum = ~ipv4->csum; + uint16_t not_old_val = ~(*old_val); + uint32_t sum = (uint32_t) old_csum + not_old_val + *new_val; + + while (sum >> 16) + { + sum = (sum >> 16) + (sum & UINT16_T_MASK); + } + + ipv4->csum = ~sum; + ++old_val; + ++new_val; + } + } + + return HICN_LIB_ERROR_NONE; } int -ipv4_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ipv4_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + /* * Checksum field is not accounted for in lower layers, so we can compute * them in any order. Note that it is only a header checksum. */ - if (csum (h, IPV4_HDRLEN, 0) != 0) + if (csum (pkbuf_get_header (pkbuf), IPV4_HDRLEN, 0) != 0) return HICN_LIB_ERROR_CORRUPTED_PACKET; - /* Retrieve payload length if not specified, as it is not available later */ - if (payload_length == 0) + /* Retrieve payload len if not specified, as it is not available later */ + if (payload_len == 0) { - int rc = ipv4_get_payload_length (type, h, &payload_length); - if (rc < 0) - return rc; + payload_len = ipv4_get_payload_len (pkbuf, ipv4); } /* Build pseudo-header */ ipv4_pseudo_header_t psh; - psh.ip_src = h->ipv4.saddr; - psh.ip_dst = h->ipv4.daddr; + psh.ip_src = ipv4->saddr; + psh.ip_dst = ipv4->daddr; /* Size is u32 and not u16, we cannot copy and need to care about endianness */ - psh.size = htons (ntohs (h->ipv4.len) - (u16) IPV4_HDRLEN); + psh.size = htons (ntohs (ipv4->len) - (u16) IPV4_HDRLEN); psh.zero = 0; - psh.protocol = (u8) h->ipv4.protocol; + psh.protocol = (u8) ipv4->protocol; /* Compute partial checksum based on pseudo-header */ partial_csum = csum (&psh, IPV4_PSHDRLEN, 0); - return CHILD_OPS (update_checksums, type, h, partial_csum, payload_length); + return CALL_CHILD (verify_checksums, pkbuf, pos, partial_csum, payload_len); } int -ipv4_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) +ipv4_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + // ASSERT(addr_old == NULL); - addr_old->ip4 = h->ipv4.saddr; + addr_old->v4 = ipv4->saddr; addr_old->pad[0] = 0; addr_old->pad[1] = 0; addr_old->pad[2] = 0; - h->ipv4.saddr = addr_new->ip4; - h->ipv4.csum = 0; - h->ipv4.csum = csum (&h->ipv4, IPV4_HDRLEN, 0); + ipv4->saddr = addr_new->v4; + ipv4->csum = 0; + ipv4->csum = csum (&ipv4, IPV4_HDRLEN, 0); - return CHILD_OPS (rewrite_interest, type, h, addr_new, addr_old); + return CALL_CHILD (rewrite_interest, pkbuf, pos, addr_new, addr_old); } int -ipv4_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl) +ipv4_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) { + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + // ASSERT(addr_old == NULL); - addr_old->ip4 = h->ipv4.daddr; + addr_old->v4 = ipv4->daddr; addr_old->pad[0] = 0; addr_old->pad[1] = 0; addr_old->pad[2] = 0; - h->ipv4.daddr = addr_new->ip4; - h->ipv4.csum = 0; - h->ipv4.csum = csum (&h->ipv4, IPV4_HDRLEN, 0); + ipv4->daddr = addr_new->v4; + ipv4->csum = 0; + ipv4->csum = csum (&ipv4, IPV4_HDRLEN, 0); - return CHILD_OPS (rewrite_data, type, h, addr_new, addr_old, face_id, - reset_pl); + return CALL_CHILD (rewrite_data, pkbuf, pos, addr_new, addr_old, face_id, + reset_pl); } int -ipv4_get_current_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv4_set_payload_len (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t payload_len) { - *header_length = IPV4_HDRLEN; + _ipv4_header_t *ipv4 = pkbuf_get_ipv4 (pkbuf); + + size_t child_header_len = hicn_ops_vft[pos + 1]->header_len; + + ipv4->len = htons ((u16) (payload_len + IPV4_HDRLEN + child_header_len)); return HICN_LIB_ERROR_NONE; } int -ipv4_get_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv4_get_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t *payload_type) { - *header_length = h->ipv4.len; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (get_payload_type, pkbuf, pos, payload_type); } int -ipv4_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv4_set_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type) { - *header_length = IPV4_HDRLEN; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_payload_type, pkbuf, pos, payload_type); } int -ipv4_get_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv4_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *signature_size) { - size_t child_header_length = 0; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - *header_length = IPV4_HDRLEN + child_header_length; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); } int -ipv4_get_payload_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *payload_length) +ipv4_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) { - size_t child_header_length; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - *payload_length = htons (h->ipv4.len) - IPV4_HDRLEN - child_header_length; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); } int -ipv4_set_payload_length (hicn_type_t type, hicn_protocol_t *h, - size_t payload_length) +ipv4_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) { - size_t child_header_length; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - h->ipv4.len = - htons ((u_short) (payload_length + IPV4_HDRLEN + child_header_length)); - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); } int -ipv4_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, - size_t *signature_size) +ipv4_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) { - return CHILD_OPS (get_signature_size, type, h, signature_size); + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); } int -ipv4_set_signature_size (hicn_type_t type, hicn_protocol_t *h, - size_t signature_size) +ipv4_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t signature_timestamp) { - return CHILD_OPS (set_signature_size, type, h, signature_size); + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); } int -ipv4_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ipv4_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t *signature_timestamp) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); } int -ipv4_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +ipv4_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t validation_algorithm) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -ipv4_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, - uint64_t signature_timestamp) +ipv4_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *validation_algorithm) { - return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -ipv4_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, - uint64_t *signature_timestamp) +ipv4_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *key_id, size_t key_len) { - return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); } int -ipv4_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, - uint8_t validation_algorithm) +ipv4_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) { - return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); } int -ipv4_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *validation_algorithm) +ipv4_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) { - return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_signature, pkbuf, pos, signature); } int -ipv4_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +ipv4_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) { - return CHILD_OPS (set_key_id, type, h, key_id); + return CALL_CHILD (has_signature, pkbuf, pos, flag); } int -ipv4_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, - uint8_t *key_id_size) +ipv4_is_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos, int *is_last) { - return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); + return CALL_CHILD (is_last_data, pkbuf, pos, is_last); } int -ipv4_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +ipv4_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) { - return CHILD_OPS (get_signature, type, h, signature); + return CALL_CHILD (set_last_data, pkbuf, pos); } -DECLARE_HICN_OPS (ipv4); +DECLARE_HICN_OPS (ipv4, IPV4_HDRLEN); /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/protocol/ipv4.h b/lib/src/protocol/ipv4.h new file mode 100644 index 000000000..5d729b955 --- /dev/null +++ b/lib/src/protocol/ipv4.h @@ -0,0 +1,112 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HICN_PROTOCOL_IPV4 +#define HICN_PROTOCOL_IPV4 + +#include <hicn/util/ip_address.h> + +#include <hicn/base.h> +#include <hicn/common.h> + +/* Headers were adapted from linux' definitions in netinet/ip.h */ + +/* + * The length of the IPV4 header struct must be 20 bytes. + */ +#define EXPECTED_IPV4_HDRLEN 20 + +typedef struct +{ + union + { + struct + { +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + u8 ihl : 4; + u8 version : 4; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + u8 version : 4; + u8 ihl : 4; +#else +#error "Unsupported endianness" +#endif + }; + + u8 version_ihl; + }; + u8 tos; + + /* + * This is the total length of the IP packet, including the IP header. + * NOTE: vriable len header size is currently not supported by the lib. + */ + u16 len; + + u16 id; + u16 frag_off; + u8 ttl; + u8 protocol; + u16 csum; + ipv4_address_t saddr; + ipv4_address_t daddr; +} _ipv4_header_t; + +#define ipv4_header_bytes(ipv4_header) \ + (sizeof (u32) * (ipv4_header->version_ihl & 0xf)) + +#define IPV4_HDRLEN sizeof (_ipv4_header_t) +static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN, + "Size of IPV4 struct does not match its expected size."); + +/* + * The length of the IPV4 pseudo header struct must be 12 bytes. + */ +#define EXPECTED_IPV4_PSHDRLEN 12 + +typedef struct +{ + ipv4_address_t ip_src; + ipv4_address_t ip_dst; + u8 zero; + u8 protocol; + u16 size; +} ipv4_pseudo_header_t; + +#define IPV4_PSHDRLEN sizeof (ipv4_pseudo_header_t) +static_assert (EXPECTED_IPV4_PSHDRLEN == IPV4_PSHDRLEN, + "Size of IPV4_PSHDR struct does not match its expected size."); + +/* Default field values */ +#define IPV4_DEFAULT_VERSION 4 +#define IPV4_DEFAULT_IHL 5 +#define IPV4_DEFAULT_TOS 0 +#define IPV4_DEFAULT_PAYLOAD_LENGTH 0 +#define IPV4_DEFAULT_ID 300 +#define IPV4_DEFAULT_FRAG_OFF 0x000 +#define IPV4_DEFAULT_TTL 64 +#define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP +#define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0 +#define IPV4_DEFAULT_DST_IP 0, 0, 0, 0 + +#endif /* HICN_PROTOCOL_IPV4 */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/ipv6.c b/lib/src/protocol/ipv6.c index 7ac55b2be..c5fa643aa 100644 --- a/lib/src/protocol/ipv6.c +++ b/lib/src/protocol/ipv6.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -17,224 +17,252 @@ #include <string.h> #include <hicn/common.h> #include <hicn/error.h> -#include <hicn/ops.h> + +#include "ipv6.h" +#include "../ops.h" typedef unsigned short u_short; -int ipv6_get_payload_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *payload_length); + +#ifdef OPAQUE_IP +#define GET_IPV6_HEADER(pkbuf) \ + (_ipv6_header_t *) ((pkbuf)->header + (pkbuf)->ipv6) +#else +#define GET_IPV6_HEADER(pkbuf) (_ipv6_header_t *) ((pkbuf)->header) +#endif /* OPAQUE_IP */ int -ipv6_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +ipv6_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { - size_t total_header_length; - int rc = CHILD_OPS (get_header_length, type, h, &total_header_length); - if (rc < 0) - return rc; + assert (pkbuf->len == 0); + if (IPV6_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += IPV6_HDRLEN; + + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); - /* *INDENT-OFF* */ - h->ipv6 = (_ipv6_header_t){ - .saddr = { { 0 } }, - .daddr = { { 0 } }, + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + + size_t header_len; + hicn_packet_get_header_length_from_format (pkbuf->format, &header_len); + + /* clang-format off */ + *ipv6 = (_ipv6_header_t){ + .saddr = IP6_ADDRESS_EMPTY, + .daddr = IP6_ADDRESS_EMPTY, .version_class_flow = htonl ((IPV6_DEFAULT_VERSION << 28) | (IPV6_DEFAULT_TRAFFIC_CLASS << 20) | (IPV6_DEFAULT_FLOW_LABEL & 0xfffff)), - .len = htons ((u16) total_header_length), - .nxt = type.l2, + .len = htons(header_len - IPV6_HDRLEN), + .nxt = HICN_PACKET_FORMAT_GET(format, pos+1), .hlim = HICN_DEFAULT_TTL, }; - /* *INDENT-ON* */ - return CHILD_OPS (init_packet_header, type, h); + /* clang-format on */ + return CALL_CHILD (init_packet_header, pkbuf, pos); } int -ipv6_get_interest_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) +ipv6_get_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) { - ip_address->ip6 = h->ipv6.saddr; + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ip_address->v6 = ipv6->saddr; return HICN_LIB_ERROR_NONE; } int -ipv6_set_interest_locator (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address) +ipv6_set_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) { - h->ipv6.saddr = ip_address->ip6; + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ipv6->saddr = ip_address->v6; return HICN_LIB_ERROR_NONE; } int -ipv6_get_interest_name (hicn_type_t type, const hicn_protocol_t *h, +ipv6_get_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + name->prefix.v6 = ipv6->daddr; + return CALL_CHILD (get_interest_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv6_set_interest_name (hicn_type_t type, hicn_protocol_t *h, +ipv6_set_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ipv6->daddr = name->prefix.v6; + return CALL_CHILD (set_interest_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv6_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, +ipv6_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_name_suffix_t *suffix) { - return CHILD_OPS (get_interest_name_suffix, type, h, suffix); + return CALL_CHILD (get_interest_name_suffix, pkbuf, pos, suffix); } int -ipv6_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, +ipv6_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_suffix_t *suffix) { - return CHILD_OPS (set_interest_name_suffix, type, h, suffix); + return CALL_CHILD (set_interest_name_suffix, pkbuf, pos, suffix); } int -ipv6_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +ipv6_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) { - return CHILD_OPS (mark_packet_as_interest, type, h); + return CALL_CHILD (get_type, pkbuf, pos, type); } int -ipv6_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) +ipv6_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) { - return CHILD_OPS (mark_packet_as_data, type, h); + return CALL_CHILD (set_type, pkbuf, pos, type); } int -ipv6_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +ipv6_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + /* Sets everything to 0 up to IP destination address */ - memset (&(h->ipv6), 0, 24); + memset (ipv6, 0, 24); - return CHILD_OPS (reset_interest_for_hash, type, h); + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -ipv6_get_data_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) +ipv6_get_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) { - ip_address->ip6 = h->ipv6.daddr; + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ip_address->v6 = ipv6->daddr; return HICN_LIB_ERROR_NONE; } int -ipv6_set_data_locator (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address) +ipv6_set_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) { - h->ipv6.daddr = ip_address->ip6; + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ipv6->daddr = ip_address->v6; return HICN_LIB_ERROR_NONE; } int -ipv6_get_data_name (hicn_type_t type, const hicn_protocol_t *h, +ipv6_get_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + name->prefix.v6 = ipv6->saddr; + return CALL_CHILD (get_data_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv6_set_data_name (hicn_type_t type, hicn_protocol_t *h, +ipv6_set_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, 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)); + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ipv6->saddr = name->prefix.v6; + return CALL_CHILD (set_data_name_suffix, pkbuf, pos, &(name->suffix)); } int -ipv6_get_data_name_suffix (hicn_type_t type, const hicn_protocol_t *h, +ipv6_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_name_suffix_t *suffix) { - return CHILD_OPS (get_data_name_suffix, type, h, suffix); + return CALL_CHILD (get_data_name_suffix, pkbuf, pos, suffix); } int -ipv6_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, +ipv6_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_suffix_t *suffix) { - return CHILD_OPS (set_data_name_suffix, type, h, suffix); + return CALL_CHILD (set_data_name_suffix, pkbuf, pos, suffix); } int -ipv6_get_data_pathlabel (hicn_type_t type, const hicn_protocol_t *h, - u32 *pathlabel) +ipv6_get_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t *path_label) { - return CHILD_OPS (get_data_pathlabel, type, h, pathlabel); + return CALL_CHILD (get_data_path_label, pkbuf, pos, path_label); } int -ipv6_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const u32 pathlabel) +ipv6_set_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t path_label) { - return CHILD_OPS (set_data_pathlabel, type, h, pathlabel); + return CALL_CHILD (set_data_path_label, pkbuf, pos, path_label); } int -ipv6_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const hicn_faceid_t face_id) +ipv6_update_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id) { - return CHILD_OPS (update_data_pathlabel, type, h, face_id); + return CALL_CHILD (update_data_path_label, pkbuf, pos, face_id); } int -ipv6_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +ipv6_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + /* IP: Set everithing to 0 up to destination address */ - memset (&h->ipv6, 0, 8); + memset (ipv6, 0, 8); /* Clears destination address */ - memset (&(h->ipv6.daddr), 0, 16); + memset (&(ipv6->daddr), 0, 16); - return CHILD_OPS (reset_data_for_hash, type, h); + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); } int -ipv6_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, +ipv6_get_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_lifetime_t *lifetime) { - return CHILD_OPS (get_lifetime, type, h, lifetime); + return CALL_CHILD (get_lifetime, pkbuf, pos, lifetime); } int -ipv6_set_lifetime (hicn_type_t type, hicn_protocol_t *h, +ipv6_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_lifetime_t lifetime) { - return CHILD_OPS (set_lifetime, type, h, lifetime); + return CALL_CHILD (set_lifetime, pkbuf, pos, lifetime); } int -ipv6_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ipv6_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { - /* Retrieve payload length if not specified */ - if (payload_length == ~0) + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + /* Retrieve payload len if not specified */ + if (payload_len == ~0) { - int rc = ipv6_get_payload_length (type, h, &payload_length); - if (rc < 0) - return rc; + payload_len = hicn_packet_get_len (pkbuf) - pkbuf->payload; } /* Build pseudo-header */ ipv6_pseudo_header_t psh; - psh.ip_src = h->ipv6.saddr; - psh.ip_dst = h->ipv6.daddr; + psh.ip_src = ipv6->saddr; + psh.ip_dst = ipv6->daddr; /* Size is u32 and not u16, we cannot copy and need to care about endianness */ - psh.size = htonl (ntohs (h->ipv6.len)); + psh.size = htonl (ntohs (ipv6->len)); psh.zeros = 0; psh.zero = 0; - psh.protocol = h->ipv6.nxt; + psh.protocol = ipv6->nxt; /* Compute partial checksum based on pseudo-header */ if (partial_csum != 0) @@ -243,31 +271,41 @@ ipv6_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, } partial_csum = csum (&psh, IPV6_PSHDRLEN, partial_csum); - return CHILD_OPS (update_checksums, type, h, partial_csum, payload_length); + return CALL_CHILD (update_checksums, pkbuf, pos, partial_csum, payload_len); } int -ipv6_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +ipv6_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) { - /* Retrieve payload length if not specified */ - if (payload_length == ~0) + /* We update the child only */ + return CALL_CHILD (update_checksums_incremental, pkbuf, pos, old_val, + new_val, size, false); +} + +int +ipv6_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + /* Retrieve payload len if not specified */ + if (payload_len == ~0) { - int rc = ipv6_get_payload_length (type, h, &payload_length); - if (rc < 0) - return rc; + payload_len = hicn_packet_get_len (pkbuf) - pkbuf->payload; } /* Build pseudo-header */ ipv6_pseudo_header_t pseudo; - pseudo.ip_src = h->ipv6.saddr; - pseudo.ip_dst = h->ipv6.daddr; + pseudo.ip_src = ipv6->saddr; + pseudo.ip_dst = ipv6->daddr; /* Size is u32 and not u16, we cannot copy and need to care about endianness */ - pseudo.size = htonl (ntohs (h->ipv6.len)); + pseudo.size = htonl (ntohs (ipv6->len)); pseudo.zeros = 0; pseudo.zero = 0; - pseudo.protocol = h->ipv6.nxt; + pseudo.protocol = ipv6->nxt; /* Compute partial checksum based on pseudo-header */ if (partial_csum != 0) @@ -276,161 +314,161 @@ ipv6_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, } partial_csum = csum (&pseudo, IPV6_PSHDRLEN, partial_csum); - return CHILD_OPS (verify_checksums, type, h, partial_csum, payload_length); + return CALL_CHILD (verify_checksums, pkbuf, pos, partial_csum, payload_len); } int -ipv6_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) +ipv6_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) { + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + // ASSERT(addr_old == NULL); - addr_old->ip6 = h->ipv6.saddr; - h->ipv6.saddr = addr_new->ip6; + addr_old->v6 = ipv6->saddr; + ipv6->saddr = addr_new->v6; - return CHILD_OPS (rewrite_interest, type, h, addr_new, addr_old); + return CALL_CHILD (rewrite_interest, pkbuf, pos, addr_new, addr_old); } int -ipv6_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl) +ipv6_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) { + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + // ASSERT(addr_old == NULL); - addr_old->ip6 = h->ipv6.daddr; - h->ipv6.daddr = addr_new->ip6; + addr_old->v6 = ipv6->daddr; + ipv6->daddr = addr_new->v6; - return CHILD_OPS (rewrite_data, type, h, addr_new, addr_old, face_id, - reset_pl); + return CALL_CHILD (rewrite_data, pkbuf, pos, addr_new, addr_old, face_id, + reset_pl); } int -ipv6_get_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv6_set_payload_len (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t payload_len) { - *header_length = IPV6_HDRLEN + ntohs (h->ipv6.len); + _ipv6_header_t *ipv6 = pkbuf_get_ipv6 (pkbuf); + + ipv6->len = htons ((u_short) (payload_len + pkbuf->payload - IPV6_HDRLEN)); return HICN_LIB_ERROR_NONE; } int -ipv6_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv6_get_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t *payload_type) { - *header_length = IPV6_HDRLEN; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (get_payload_type, pkbuf, pos, payload_type); } int -ipv6_get_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +ipv6_set_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type) { - size_t child_header_length = 0; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - *header_length = IPV6_HDRLEN + child_header_length; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_payload_type, pkbuf, pos, payload_type); } int -ipv6_get_payload_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *payload_length) +ipv6_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *signature_size) { - size_t child_header_length; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - *payload_length = ntohs (h->ipv6.len) - child_header_length; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); } int -ipv6_set_payload_length (hicn_type_t type, hicn_protocol_t *h, - size_t payload_length) +ipv6_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) { - size_t child_header_length; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; - h->ipv6.len = htons ((u_short) (payload_length + child_header_length)); - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); } int -ipv6_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, - size_t *signature_size) +ipv6_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) { - return CHILD_OPS (get_signature_size, type, h, signature_size); + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); } int -ipv6_set_signature_size (hicn_type_t type, hicn_protocol_t *h, - size_t signature_size) +ipv6_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) +{ + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); +} + +int +ipv6_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t signature_timestamp) { - return CHILD_OPS (set_signature_size, type, h, signature_size); + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); } int -ipv6_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ipv6_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t *signature_timestamp) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); } int -ipv6_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +ipv6_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t validation_algorithm) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -ipv6_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, - uint64_t signature_timestamp) +ipv6_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *validation_algorithm) { - return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -ipv6_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, - uint64_t *signature_timestamp) +ipv6_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *key_id, size_t key_len) { - return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); } int -ipv6_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, - uint8_t validation_algorithm) +ipv6_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) { - return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); } int -ipv6_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *validation_algorithm) +ipv6_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) { - return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_signature, pkbuf, pos, signature); } int -ipv6_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +ipv6_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) { - return CHILD_OPS (set_key_id, type, h, key_id); + return CALL_CHILD (has_signature, pkbuf, pos, flag); } int -ipv6_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, - uint8_t *key_id_size) +ipv6_is_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos, int *is_last) { - return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); + return CALL_CHILD (is_last_data, pkbuf, pos, is_last); } int -ipv6_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +ipv6_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) { - return CHILD_OPS (get_signature, type, h, signature); + return CALL_CHILD (set_last_data, pkbuf, pos); } -DECLARE_HICN_OPS (ipv6); +DECLARE_HICN_OPS (ipv6, IPV6_HDRLEN); /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/protocol/ipv6.h b/lib/src/protocol/ipv6.h new file mode 100644 index 000000000..9a51096a0 --- /dev/null +++ b/lib/src/protocol/ipv6.h @@ -0,0 +1,86 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HICN_PROTOCOL_IPV6_H +#define HICN_PROTOCOL_IPV6_H + +#include <hicn/util/ip_address.h> + +#include <hicn/common.h> + +/* + * The length of the IPV6 header struct must be 40 bytes. + */ +#define EXPECTED_IPV6_HDRLEN 40 + +typedef struct +{ +#if 0 // TEMPORARY FIX + union + { + struct + { +#endif + u32 version_class_flow; /* version, traffic class and 20 bits of flow-ID */ + u16 len; /* payload length */ + u8 nxt; /* next header */ + u8 hlim; /* hop limit */ +#if 0 + }; + u8 vfc; /* 4 bits version, top 4 bits class */ + }; +#endif + ipv6_address_t saddr; /* source address */ + ipv6_address_t daddr; /* destination address */ +} _ipv6_header_t; + +#define IPV6_HDRLEN sizeof (_ipv6_header_t) +static_assert (EXPECTED_IPV6_HDRLEN == IPV6_HDRLEN, + "Size of IPV6 struct does not match its expected size."); + +/* + * The length of the IPV6 pseudo header struct must be 40 bytes. + */ +#define EXPECTED_IPV6_PSHDRLEN 40 + +typedef struct +{ + ipv6_address_t ip_src; + ipv6_address_t ip_dst; + u32 size; + u16 zeros; + u8 zero; + u8 protocol; +} ipv6_pseudo_header_t; + +#define IPV6_PSHDRLEN sizeof (ipv6_pseudo_header_t) +static_assert (EXPECTED_IPV6_PSHDRLEN == IPV6_PSHDRLEN, + "Size of IPV6_PSHDR struct does not match its expected size."); + +/* Default field values */ +#define IPV6_DEFAULT_VERSION 6 +#define IPV6_DEFAULT_TRAFFIC_CLASS 0 +#define IPV6_DEFAULT_FLOW_LABEL 0 +#define IPV6_DEFAULT_PAYLOAD_LENGTH 0 + +#endif + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/new.c b/lib/src/protocol/new.c new file mode 100644 index 000000000..e86ce4753 --- /dev/null +++ b/lib/src/protocol/new.c @@ -0,0 +1,563 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <string.h> +#include <hicn/common.h> +#include <hicn/error.h> + +#include "udp.h" +#include "../ops.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" + +int +new_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + pkbuf->newhdr = pkbuf->len; + if (NEW_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += NEW_HDRLEN; + + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + + memset (new, 0, sizeof (_new_header_t)); + _set_new_header_version (new); + uint8_t ah_flag = (HICN_PACKET_FORMAT_GET (format, pos + 1) == IPPROTO_AH) ? + HICN_NEW_FLAG_SIG : + 0; + new->flags |= ah_flag; + + return CALL_CHILD (init_packet_header, pkbuf, pos); +} + +int +new_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + /* Interest packets have the INT bit set */ + if (new->flags & HICN_NEW_FLAG_INT) + *type = HICN_PACKET_TYPE_INTEREST; + else + *type = HICN_PACKET_TYPE_DATA; + return HICN_LIB_ERROR_NONE; +} + +int +new_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + switch (type) + { + case HICN_PACKET_TYPE_INTEREST: + new->flags |= HICN_NEW_FLAG_INT; + break; + case HICN_PACKET_TYPE_DATA: + new->flags &= ~HICN_NEW_FLAG_INT; + break; + default: + return -1; + } + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_interest_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_t *name) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + name->prefix = new->prefix; + name->suffix = ntohl (new->suffix); + return HICN_LIB_ERROR_NONE; +} + +// XXX never called and thus packet is never initialized as interest +// DECLARE_set_interest_name (udp, UNEXPECTED); +int +new_set_interest_name (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_t *name) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + int rc = new_set_type (pkbuf, pos, HICN_PACKET_TYPE_INTEREST); + if (rc) + return rc; + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + new->prefix = name->prefix; + new->suffix = htonl (name->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_suffix_t *suffix) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + *suffix = ntohl (new->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + new->suffix = htonl (*suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + return CALL_CHILD (init_packet_header, pkbuf, pos); +} + +int +new_get_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_ip_address_t *ip_address) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_locator (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *ip_address) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_t *name) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + name->prefix = new->prefix; + name->suffix = ntohl (new->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_name (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_t *name) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + int rc = new_set_type (pkbuf, pos, HICN_PACKET_TYPE_DATA); + if (rc) + return rc; + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + new->prefix = name->prefix; + new->suffix = htonl (name->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_suffix_t *suffix) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + *suffix = ntohl (new->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + new->suffix = htonl (*suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t *path_label) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + *path_label = ntohl (new->path_label); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t path_label) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + new->path_label = htonl (path_label); + return HICN_LIB_ERROR_NONE; +} + +int +new_update_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_path_label_t new_pl; + update_path_label (new->path_label, face_id, &new_pl); + new->path_label = new_pl; + return HICN_LIB_ERROR_NONE; +} + +int +new_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); +} + +int +new_get_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_lifetime_t *lifetime) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + *lifetime = ntohl (new->lifetime); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_lifetime_t lifetime) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + new->lifetime = htonl (lifetime); + return HICN_LIB_ERROR_NONE; +} + +int +new_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + return HICN_LIB_ERROR_NONE; +} + +int +new_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) +{ + return HICN_LIB_ERROR_NONE; +} + +int +new_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + return HICN_LIB_ERROR_NONE; +} + +int +new_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_INTEREST); + + return HICN_LIB_ERROR_NONE; +} + +int +new_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + _unused (new); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + return HICN_LIB_ERROR_NONE; +} + +int +new_set_payload_len (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t payload_len) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + /* + * The value we have to store in the header is the sum of headers following + * the current header + the new payload size + */ + + size_t child_header_len = + (pkbuf->payload - pkbuf->newhdr) - sizeof (_new_header_t); + new->payload_len = htons ((u16) child_header_len + payload_len); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *signature_size) +{ + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); +} + +int +new_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) +{ + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); +} + +int +new_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) +{ + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); +} + +int +new_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) +{ + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); +} + +int +new_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t signature_timestamp) +{ + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); +} + +int +new_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t *signature_timestamp) +{ + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); +} + +int +new_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t validation_algorithm) +{ + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); +} + +int +new_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *validation_algorithm) +{ + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); +} + +int +new_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *key_id, + size_t key_len) +{ + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); +} + +int +new_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) +{ + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); +} + +int +new_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) +{ + return CALL_CHILD (get_signature, pkbuf, pos, signature); +} + +int +new_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + return new->flags &HICN_NEW_FLAG_SIG; +} + +int +new_get_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t *payload_type) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + *payload_type = ((new->flags &HICN_NEW_FLAG_MAN) == HICN_NEW_FLAG_MAN); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + if (payload_type != HPT_DATA && payload_type != HPT_MANIFEST) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + if (payload_type) + new->flags |= HICN_NEW_FLAG_MAN; + else + new->flags &= ~HICN_NEW_FLAG_MAN; + + return HICN_LIB_ERROR_NONE; +} + +int +new_is_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos, int *is_last) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + *is_last = new->flags &HICN_NEW_FLAG_LST; + return HICN_LIB_ERROR_NONE; +} + +int +new_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) +{ + _new_header_t *new = pkbuf_get_new (pkbuf); + + hicn_packet_type_t type; + _ASSERT (new_get_type (pkbuf, pos, &type) == 0); + _ASSERT (type == HICN_PACKET_TYPE_DATA); + + new->flags |= HICN_NEW_FLAG_LST; + return HICN_LIB_ERROR_NONE; +} + +DECLARE_HICN_OPS (new, NEW_HDRLEN); + +#pragma GCC diagnostic pop + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/new.h b/lib/src/protocol/new.h new file mode 100644 index 000000000..7679910f4 --- /dev/null +++ b/lib/src/protocol/new.h @@ -0,0 +1,95 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol/ah.h + * @brief AH packet header + */ +#ifndef HICN_PROTOCOL_NEW_H +#define HICN_PROTOCOL_NEW_H + +#include <hicn/common.h> +#include <hicn/name.h> + +/* + * The length of the new header struct must be 28 bytes. + */ +#define EXPECTED_NEW_HDRLEN 32 + +typedef struct +{ + u8 version_reserved; + u8 flags; + + /* Size of the payload direcly after the new header */ + u16 payload_len; + + u32 lifetime; + + /* Name prefix and suffix */ + hicn_ip_address_t prefix; + u32 suffix; + + /* We reserve 32 bits for the path label */ + u32 path_label; +} _new_header_t; + +#define NEW_HDRLEN sizeof (_new_header_t) +static_assert (EXPECTED_NEW_HDRLEN == NEW_HDRLEN, + "Size of new_header Struct does not match its expected size."); + +/* TCP flags bit 0 first. */ +#define foreach_hicn_new_flag \ + _ (SIG) /**< Signature header after. */ \ + _ (MAN) /**< Payload type is manifest. */ \ + _ (INT) /**< Packet is interest. */ \ + _ (LST) /**< Last data. */ + +enum +{ +#define _(f) HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ + HICN_NEW_N_FLAG_BITS, +}; + +enum +{ +#define _(f) HICN_NEW_FLAG_##f = 1 << HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ +}; + +static inline int +_get_new_header_version (const _new_header_t *new_hdr) +{ + return ((new_hdr->version_reserved >> 4) & 0x0F); +} + +static inline void +_set_new_header_version (_new_header_t *new_hdr) +{ + new_hdr->version_reserved = (0x9 << 4) & 0xF0; +} + +#endif /* HICN_PROTOCOL_NEW_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/tcp.c b/lib/src/protocol/tcp.c index cfeac1907..e09495ecd 100644 --- a/lib/src/protocol/tcp.c +++ b/lib/src/protocol/tcp.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -14,9 +14,12 @@ */ #include <string.h> -#include <hicn/protocol/tcp.h> + +#include <hicn/base.h> #include <hicn/error.h> -#include <hicn/ops.h> + +#include "tcp.h" +#include "../ops.h" #define TCP_DEFAULT_SRC_PORT 0x8000 #define TCP_DEFAULT_DST_PORT 0x0080 @@ -32,6 +35,8 @@ #define TCP_DEFAULT_SYN 1 #define TCP_DEFAULT_FIN 0 +#define UINT16_T_MASK 0x0000ffff // 1111 1111 1111 1111 + DECLARE_get_interest_locator (tcp, UNEXPECTED); DECLARE_set_interest_locator (tcp, UNEXPECTED); DECLARE_get_interest_name (tcp, UNEXPECTED); @@ -40,11 +45,27 @@ DECLARE_get_data_locator (tcp, UNEXPECTED); DECLARE_set_data_locator (tcp, UNEXPECTED); DECLARE_get_data_name (tcp, UNEXPECTED); DECLARE_set_data_name (tcp, UNEXPECTED); -DECLARE_get_length (tcp, UNEXPECTED); -DECLARE_get_payload_length (tcp, UNEXPECTED); -DECLARE_set_payload_length (tcp, UNEXPECTED); +DECLARE_set_payload_len (tcp, UNEXPECTED); -always_inline int +int tcp_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first); + +static inline void +reset_for_hash (hicn_packet_buffer_t *pkbuf) +{ + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + tcp->sport = 0; + tcp->dport = 0; + tcp->seq_ack = 0; + tcp->data_offset_and_reserved = 0; + tcp->flags = 0; + tcp->window = 0; + tcp->csum = 0; + tcp->urg_ptr = 0; +} + +static inline int check_tcp_checksum (u16 csum) { /* As per RFC1624 @@ -67,9 +88,17 @@ check_tcp_checksum (u16 csum) } int -tcp_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +tcp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) { - h->tcp = (_tcp_header_t){ + pkbuf->tcp = pkbuf->len; + if (TCP_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += TCP_HDRLEN; + + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + hicn_packet_format_t format = hicn_packet_get_format (pkbuf); + + *tcp = (_tcp_header_t){ .sport = htons (TCP_DEFAULT_SRC_PORT), .dport = htons (TCP_DEFAULT_DST_PORT), .seq = 0, @@ -84,131 +113,170 @@ tcp_init_packet_header (hicn_type_t type, hicn_protocol_t *h) .urg_ptr = 65000, }; - uint8_t ah_flag = type.l2 == IPPROTO_AH ? AH_FLAG : 0; + uint8_t ah_flag = + (HICN_PACKET_FORMAT_GET (format, pos + 1) == IPPROTO_AH) ? AH_FLAG : 0; - h->tcp.flags |= ah_flag; + tcp->flags |= ah_flag; - return CHILD_OPS (init_packet_header, type, h); + return CALL_CHILD (init_packet_header, pkbuf, pos); } int -tcp_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, - hicn_name_suffix_t *suffix) -{ - *suffix = ntohl (h->tcp.name_suffix); +tcp_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) +{ + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + /* Data packets have the ECE bit set */ + if (tcp->flags & HICN_TCP_FLAG_ECE) + *type = HICN_PACKET_TYPE_DATA; + else + *type = HICN_PACKET_TYPE_INTEREST; return HICN_LIB_ERROR_NONE; } int -tcp_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, - const hicn_name_suffix_t *suffix) +tcp_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) { - h->tcp.name_suffix = htonl (*suffix); - + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + switch (type) + { + case HICN_PACKET_TYPE_INTEREST: + tcp->flags &= ~HICN_TCP_FLAG_ECE; + break; + case HICN_PACKET_TYPE_DATA: + tcp->flags |= HICN_TCP_FLAG_ECE; + break; + default: + return HICN_LIB_ERROR_INVALID_PARAMETER; + } return HICN_LIB_ERROR_NONE; } int -tcp_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +tcp_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_suffix_t *suffix) { - h->tcp.flags &= ~HICN_TCP_FLAG_ECE; + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *suffix = ntohl (tcp->name_suffix); return HICN_LIB_ERROR_NONE; } int -tcp_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) +tcp_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix) { - h->tcp.flags |= HICN_TCP_FLAG_ECE; + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + int rc = tcp_set_type (pkbuf, pos, HICN_PACKET_TYPE_INTEREST); + if (rc) + return rc; + + tcp->name_suffix = htonl (*suffix); + return HICN_LIB_ERROR_NONE; } int -tcp_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +tcp_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { - memset (&(h->tcp), 0, 4); - memset (&(h->tcp.seq_ack), 0, 12); - - return CHILD_OPS (reset_interest_for_hash, type, h); + reset_for_hash (pkbuf); + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); } int -tcp_get_data_name_suffix (hicn_type_t type, const hicn_protocol_t *h, +tcp_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_name_suffix_t *suffix) { - *suffix = ntohl (h->tcp.name_suffix); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *suffix = ntohl (tcp->name_suffix); return HICN_LIB_ERROR_NONE; } int -tcp_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, +tcp_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_name_suffix_t *suffix) { - h->tcp.name_suffix = htonl (*suffix); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + int rc = tcp_set_type (pkbuf, pos, HICN_PACKET_TYPE_DATA); + if (rc) + return rc; + + tcp->name_suffix = htonl (*suffix); return HICN_LIB_ERROR_NONE; } int -tcp_get_data_pathlabel (hicn_type_t type, const hicn_protocol_t *h, - u32 *pathlabel) +tcp_get_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t *path_label) { - *pathlabel = h->tcp.seq_ack; + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *path_label = + (hicn_path_label_t) (tcp->seq_ack >> (32 - HICN_PATH_LABEL_SIZE_BITS)); return HICN_LIB_ERROR_NONE; } int -tcp_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const u32 pathlabel) +tcp_set_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t path_label) { - h->tcp.seq_ack = pathlabel; + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + hicn_path_label_t old_path_label; + tcp_get_data_path_label (pkbuf, pos, &old_path_label); + + tcp->seq_ack = (path_label << (32 - HICN_PATH_LABEL_SIZE_BITS)); + + tcp_update_checksums_incremental ( + pkbuf, pos, (uint16_t *) &old_path_label, (uint16_t *) &path_label, + sizeof (hicn_path_label_t) / sizeof (uint16_t), true); + return HICN_LIB_ERROR_NONE; } int -tcp_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, - const hicn_faceid_t face_id) +tcp_update_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id) { - hicn_pathlabel_t pl = - (hicn_pathlabel_t) (h->tcp.seq_ack >> (32 - HICN_PATH_LABEL_SIZE)); + assert (sizeof (hicn_path_label_t) == 1); - hicn_pathlabel_t new_pl; + hicn_path_label_t old_path_label; + hicn_path_label_t new_path_label; - update_pathlabel (pl, face_id, &new_pl); - h->tcp.seq_ack = (new_pl << (32 - HICN_PATH_LABEL_SIZE)); + tcp_get_data_path_label (pkbuf, pos, &old_path_label); + update_path_label (old_path_label, face_id, &new_path_label); + tcp_set_data_path_label (pkbuf, pos, new_path_label); return HICN_LIB_ERROR_NONE; } int -tcp_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +tcp_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) { - memset (&(h->tcp), 0, 4); - memset (&(h->tcp.seq_ack), 0, 12); - - return CHILD_OPS (reset_data_for_hash, type, h); + reset_for_hash (pkbuf); + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); } int -tcp_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, +tcp_get_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, hicn_lifetime_t *lifetime) { - *lifetime = ntohs (h->tcp.urg_ptr) - << (h->tcp.data_offset_and_reserved & 0xF); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *lifetime = ntohs (tcp->urg_ptr) << (tcp->data_offset_and_reserved & 0xF); return HICN_LIB_ERROR_NONE; } int -tcp_set_lifetime (hicn_type_t type, hicn_protocol_t *h, +tcp_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, const hicn_lifetime_t lifetime) { + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); u8 multiplier = 0; u32 lifetime_scaled = lifetime; - if (PREDICT_FALSE (lifetime >= HICN_MAX_LIFETIME)) + if (HICN_EXPECT_FALSE (lifetime >= HICN_MAX_LIFETIME)) { - h->tcp.urg_ptr = htons (HICN_MAX_LIFETIME_SCALED); - h->tcp.data_offset_and_reserved = - (h->tcp.data_offset_and_reserved & ~0x0F) | - HICN_MAX_LIFETIME_MULTIPLIER; + tcp->urg_ptr = htons (HICN_MAX_LIFETIME_SCALED); + tcp->data_offset_and_reserved = + (tcp->data_offset_and_reserved & ~0x0F) | HICN_MAX_LIFETIME_MULTIPLIER; return HICN_LIB_ERROR_NONE; } @@ -219,41 +287,76 @@ tcp_set_lifetime (hicn_type_t type, hicn_protocol_t *h, lifetime_scaled = lifetime_scaled >> 1; } - h->tcp.urg_ptr = htons (lifetime_scaled); - h->tcp.data_offset_and_reserved = - (h->tcp.data_offset_and_reserved & ~0x0F) | multiplier; + tcp->urg_ptr = htons (lifetime_scaled); + tcp->data_offset_and_reserved = + (tcp->data_offset_and_reserved & ~0x0F) | multiplier; return HICN_LIB_ERROR_NONE; } int -tcp_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +tcp_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { - h->tcp.csum = 0; + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + /* TODO bound checks for payload_len based on pkbuf size */ + assert (payload_len != ~0); - if (PREDICT_TRUE (partial_csum != 0)) + tcp->csum = 0; + + if (HICN_EXPECT_TRUE (partial_csum != 0)) { partial_csum = ~partial_csum; } - h->tcp.csum = csum (h, TCP_HDRLEN + payload_length, partial_csum); + tcp->csum = + csum (pkbuf_get_header (pkbuf), TCP_HDRLEN + payload_len, partial_csum); + + return CALL_CHILD (update_checksums, pkbuf, pos, 0, payload_len); +} + +int +tcp_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) +{ + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + if (skip_first) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + for (uint8_t i = 0; i < size; i++) + { + uint16_t old_csum = ~tcp->csum; + uint16_t not_old_val = ~(*old_val); + uint32_t sum = (uint32_t) old_csum + not_old_val + *new_val; + + while (sum >> 16) + { + sum = (sum >> 16) + (sum & UINT16_T_MASK); + } + + tcp->csum = ~sum; + ++old_val; + ++new_val; + } - return CHILD_OPS (update_checksums, type, h, 0, payload_length); + return CALL_CHILD (update_checksums_incremental, pkbuf, pos, old_val, + new_val, size, false); } int -tcp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, - size_t payload_length) +tcp_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) { - if (PREDICT_TRUE (partial_csum != 0)) + if (HICN_EXPECT_TRUE (partial_csum != 0)) { partial_csum = ~partial_csum; } - if (csum (h, TCP_HDRLEN + payload_length, partial_csum) != 0) + if (csum (pkbuf_get_header (pkbuf), TCP_HDRLEN + payload_len, + partial_csum) != 0) return HICN_LIB_ERROR_CORRUPTED_PACKET; - return CHILD_OPS (verify_checksums, type, h, 0, payload_length); + return CALL_CHILD (verify_checksums, pkbuf, pos, 0, payload_len); } #define TCP_OFFSET_MASK 13 @@ -278,10 +381,14 @@ tcp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, #define TCP_DEFAULT_FIN 0 int -tcp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old) +tcp_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) { - u16 *tcp_checksum = &(h->tcp.csum); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + _ipv6_header_t *ip6 = pkbuf_get_ipv6 (pkbuf); + + u16 *tcp_checksum = &(tcp->csum); int ret = check_tcp_checksum (*tcp_checksum); if (ret) @@ -294,14 +401,14 @@ tcp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, * whole struct by interpreting it as IPv6 in all cases * * v4 code would be: - * csum = ip_csum_sub_even (*tcp_checksum, h->ipv4.saddr.as_u32); - * csum = ip_csum_add_even (csum, h->ipv4.saddr.as_u32); + * csum = ip_csum_sub_even (*tcp_checksum, h->tcp.saddr.as_u32); + * csum = ip_csum_add_even (csum, h->tcp.saddr.as_u32); */ - ip_csum_t csum = - ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (h->ipv6.saddr.as_u64[0])); - csum = ip_csum_sub_even (csum, (ip_csum_t) (h->ipv6.saddr.as_u64[1])); - csum = ip_csum_add_even (csum, (ip_csum_t) (h->ipv6.saddr.as_u64[0])); - csum = ip_csum_add_even (csum, (ip_csum_t) (h->ipv6.saddr.as_u64[1])); + hicn_ip_csum_t csum = + ip_csum_sub_even (*tcp_checksum, (hicn_ip_csum_t) (ip6->saddr.as_u64[0])); + csum = ip_csum_sub_even (csum, (hicn_ip_csum_t) (ip6->saddr.as_u64[1])); + csum = ip_csum_add_even (csum, (hicn_ip_csum_t) (ip6->saddr.as_u64[0])); + csum = ip_csum_add_even (csum, (hicn_ip_csum_t) (ip6->saddr.as_u64[1])); *tcp_checksum = ip_csum_fold (csum); @@ -309,21 +416,22 @@ tcp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, } int -tcp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, - const hicn_faceid_t face_id, u8 reset_pl) +tcp_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) { - - u16 *tcp_checksum = &(h->tcp.csum); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + u16 *tcp_checksum = &(tcp->csum); int ret = check_tcp_checksum (*tcp_checksum); /* * update path label */ - u16 old_pl = h->tcp.seq_ack; + u16 old_pl = tcp->seq_ack; if (reset_pl) - h->tcp.seq_ack = 0; - tcp_update_data_pathlabel (type, h, face_id); + tcp->seq_ack = 0; + tcp_update_data_path_label (pkbuf, pos, face_id); if (ret) { @@ -335,18 +443,18 @@ tcp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, * whole struct by interpreting it as IPv6 in all cases * * v4 code would be: - * csum = ip_csum_sub_even (*tcp_checksum, h->ipv4.saddr.as_u32); - * csum = ip_csum_add_even (csum, h->ipv4.saddr.as_u32); + * csum = ip_csum_sub_even (*tcp_checksum, h->tcp.saddr.as_u32); + * csum = ip_csum_add_even (csum, h->tcp.saddr.as_u32); */ - ip_csum_t csum = - ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (addr_old->ip6.as_u64[0])); - csum = - ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (addr_old->ip6.as_u64[1])); - csum = ip_csum_add_even (csum, (ip_csum_t) (addr_new->ip6.as_u64[0])); - csum = ip_csum_add_even (csum, (ip_csum_t) (addr_new->ip6.as_u64[1])); + hicn_ip_csum_t csum = ip_csum_sub_even ( + *tcp_checksum, (hicn_ip_csum_t) (addr_old->v6.as_u64[0])); + csum = ip_csum_sub_even (*tcp_checksum, + (hicn_ip_csum_t) (addr_old->v6.as_u64[1])); + csum = ip_csum_add_even (csum, (hicn_ip_csum_t) (addr_new->v6.as_u64[0])); + csum = ip_csum_add_even (csum, (hicn_ip_csum_t) (addr_new->v6.as_u64[1])); csum = ip_csum_sub_even (csum, old_pl); - csum = ip_csum_add_even (csum, h->tcp.seq_ack); + csum = ip_csum_add_even (csum, tcp->seq_ack); *tcp_checksum = ip_csum_fold (csum); @@ -354,101 +462,134 @@ tcp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, } int -tcp_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +tcp_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *signature_size) { - *header_length = TCP_HDRLEN; - return HICN_LIB_ERROR_NONE; + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); } int -tcp_get_header_length (hicn_type_t type, const hicn_protocol_t *h, - size_t *header_length) +tcp_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) { - size_t child_header_length = 0; - int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); - if (rc < 0) - return rc; + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); +} - *header_length = TCP_HDRLEN + child_header_length; - return HICN_LIB_ERROR_NONE; +int +tcp_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) +{ + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); } int -tcp_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, - size_t *signature_size) +tcp_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) { - return CHILD_OPS (get_signature_size, type, h, signature_size); + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); } int -tcp_set_signature_size (hicn_type_t type, hicn_protocol_t *h, - size_t signature_size) +tcp_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t signature_timestamp) { - return CHILD_OPS (set_signature_size, type, h, signature_size); + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); } int -tcp_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +tcp_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t *signature_timestamp) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); } int -tcp_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +tcp_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t validation_algorithm) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -tcp_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, - uint64_t signature_timestamp) +tcp_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *validation_algorithm) { - return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); } int -tcp_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, - uint64_t *signature_timestamp) +tcp_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *key_id, + size_t key_len) { - return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); } int -tcp_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, - uint8_t validation_algorithm) +tcp_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) { - return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); } int -tcp_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *validation_algorithm) +tcp_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) { - return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); + return CALL_CHILD (get_signature, pkbuf, pos, signature); } int -tcp_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +tcp_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) { - return CHILD_OPS (set_key_id, type, h, key_id); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *flag = tcp->flags & AH_FLAG; + return HICN_LIB_ERROR_NONE; } int -tcp_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, - uint8_t *key_id_size) +tcp_get_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t *payload_type) { - return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *payload_type = ((tcp->flags & HICN_TCP_FLAG_URG) == HICN_TCP_FLAG_URG); + return HICN_LIB_ERROR_NONE; } int -tcp_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +tcp_set_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type) { - return CHILD_OPS (get_signature, type, h, signature); + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + if (payload_type != HPT_DATA && payload_type != HPT_MANIFEST) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + if (payload_type) + tcp->flags |= HICN_TCP_FLAG_URG; + else + tcp->flags &= ~HICN_TCP_FLAG_URG; + + return HICN_LIB_ERROR_NONE; +} + +int +tcp_is_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos, int *is_last) +{ + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + *is_last = (tcp->flags & HICN_TCP_FLAG_RST) == HICN_TCP_FLAG_RST; + return HICN_LIB_ERROR_NONE; +} + +int +tcp_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) +{ + _tcp_header_t *tcp = pkbuf_get_tcp (pkbuf); + tcp->flags |= HICN_TCP_FLAG_RST; + return HICN_LIB_ERROR_NONE; } -DECLARE_HICN_OPS (tcp); +DECLARE_HICN_OPS (tcp, TCP_HDRLEN); /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/protocol/tcp.h b/lib/src/protocol/tcp.h new file mode 100644 index 000000000..e563372d0 --- /dev/null +++ b/lib/src/protocol/tcp.h @@ -0,0 +1,165 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HICN_PROTOCOL_TCP_H +#define HICN_PROTOCOL_TCP_H + +#include <hicn/base.h> +#include <hicn/common.h> +#include <hicn/name.h> + +/* + * The length of the TCP header struct must be 20 bytes. + */ +#define EXPECTED_TCP_HDRLEN 20 + +/* + * NOTE: bitfields are problematic for portability reasons. There are provided + * here for reference and documentation purposes, we might just provide a macro + * to disable and use it instead of __BYTE_ORDER__. + */ +typedef struct +{ + u16 sport; + u16 dport; + union + { + u32 seq; + hicn_name_suffix_t name_suffix; + }; + union + { + u32 seq_ack; + struct + { + hicn_path_label_t pathlabel; + u8 pad[3]; + }; + }; + + union + { + struct + { + u8 data_offset_and_reserved; + u8 flags; + }; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + struct + { + u16 reserved : 4; + u16 doff : 4; + u16 fin : 1; + u16 syn : 1; + u16 rst : 1; + u16 psh : 1; + u16 ack : 1; + u16 urg : 1; + u16 ece : 1; + u16 cwr : 1; + }; + struct + { /* __ denotes unchanged bitfields */ + u16 timescale : 4; + u16 __doff : 4; + u16 __fin : 1; + u16 __syn : 1; + u16 __rst : 1; + u16 sig : 1; + u16 __ack : 1; + u16 man : 1; + u16 id : 1; + u16 __cwr : 1; + }; +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + struct + { + u16 doff : 4; + u16 reserved : 4; + u16 cwr : 1; + u16 ece : 1; + u16 urg : 1; + u16 ack : 1; + u16 psh : 1; + u16 rst : 1; + u16 syn : 1; + u16 fin : 1; + }; + struct + { + u16 __doff : 4; + u16 timescale : 4; + u16 __cwr : 1; + u16 id : 1 u16 man : 1; + u16 __ack : 1; + u16 sig : 1; + u16 __rst : 1; + u16 __syn : 1; + u16 __fin : 1; + }; +#endif + }; + union + { + u16 window; + u16 ldr; + }; + u16 csum; + union + { + u16 urg_ptr; + u16 lifetime; + }; +} _tcp_header_t; + +#define TCP_HDRLEN sizeof (_tcp_header_t) +static_assert (EXPECTED_TCP_HDRLEN == TCP_HDRLEN, + "Size of TCP struct does not match its expected size."); + +/* TCP flags bit 0 first. */ +#define foreach_tcp_flag \ + _ (FIN) /**< No more data from sender. */ \ + _ (SYN) /**< Synchronize sequence numbers. */ \ + _ (RST) /**< Reset the connection. */ \ + _ (PSH) /**< Push function. */ \ + _ (ACK) /**< Ack field significant. */ \ + _ (URG) /**< Urgent pointer field significant. */ \ + _ (ECE) /**< ECN-echo. Receiver got CE packet */ \ + _ (CWR) /**< Sender reduced congestion window */ + +enum +{ +#define _(f) HICN_TCP_FLAG_BIT_##f, + foreach_tcp_flag +#undef _ + HICN_TCP_N_FLAG_BITS, +}; + +enum +{ +#define _(f) HICN_TCP_FLAG_##f = 1 << HICN_TCP_FLAG_BIT_##f, + foreach_tcp_flag +#undef _ +}; + +#endif /* HICN_PROTOCOL_TCP_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/udp.c b/lib/src/protocol/udp.c new file mode 100644 index 000000000..56de0065f --- /dev/null +++ b/lib/src/protocol/udp.c @@ -0,0 +1,308 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdlib.h> +#include <string.h> +#include <hicn/common.h> +#include <hicn/error.h> + +#include "udp.h" +#include "../ops.h" + +DECLARE_get_interest_locator (udp, UNEXPECTED); +DECLARE_set_interest_locator (udp, UNEXPECTED); +DECLARE_get_interest_name (udp, UNEXPECTED); +DECLARE_set_interest_name (udp, UNEXPECTED); +DECLARE_get_data_locator (udp, UNEXPECTED); +DECLARE_set_data_locator (udp, UNEXPECTED); +DECLARE_get_data_name (udp, UNEXPECTED); +DECLARE_set_data_name (udp, UNEXPECTED); +DECLARE_set_payload_len (udp, UNEXPECTED); + +int +udp_init_packet_header (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + pkbuf->udp = pkbuf->len; + if (UDP_HDRLEN > pkbuf->buffer_size - pkbuf->len) + return -1; + pkbuf->len += UDP_HDRLEN; + + _udp_header_t *udp = pkbuf_get_udp (pkbuf); + + size_t len = hicn_packet_get_len (pkbuf) - + ((u8 *) udp - pkbuf_get_header (pkbuf)) - + sizeof (_udp_header_t); + + // clang-format off + *udp = (_udp_header_t){ + .src_port = 0, + .dst_port = 0, + .len = htons ((u16) len), + .checksum = 0 + }; + // clang-format on + return CALL_CHILD (init_packet_header, pkbuf, pos); +} + +int +udp_get_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_suffix_t *suffix) +{ + return CALL_CHILD (get_interest_name_suffix, pkbuf, pos, suffix); +} + +int +udp_set_interest_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix) +{ + return CALL_CHILD (set_interest_name_suffix, pkbuf, pos, suffix); +} + +int +udp_get_type (const hicn_packet_buffer_t *pkbuf, const size_t pos, + hicn_packet_type_t *type) +{ + return CALL_CHILD (get_type, pkbuf, pos, type); +} + +int +udp_set_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_packet_type_t type) +{ + return CALL_CHILD (set_type, pkbuf, pos, type); +} + +int +udp_reset_interest_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + return CALL_CHILD (reset_interest_for_hash, pkbuf, pos); +} + +int +udp_get_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_name_suffix_t *suffix) +{ + return CALL_CHILD (get_data_name_suffix, pkbuf, pos, suffix); +} + +int +udp_set_data_name_suffix (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_name_suffix_t *suffix) +{ + return CALL_CHILD (set_data_name_suffix, pkbuf, pos, suffix); +} + +int +udp_get_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t *path_label) +{ + return CALL_CHILD (get_data_path_label, pkbuf, pos, path_label); +} + +int +udp_set_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_path_label_t path_label) +{ + return CALL_CHILD (set_data_path_label, pkbuf, pos, path_label); +} + +int +udp_update_data_path_label (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_faceid_t face_id) +{ + return CALL_CHILD (update_data_path_label, pkbuf, pos, face_id); +} + +int +udp_reset_data_for_hash (hicn_packet_buffer_t *pkbuf, size_t pos) +{ + return CALL_CHILD (reset_data_for_hash, pkbuf, pos); +} + +int +udp_get_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_lifetime_t *lifetime) +{ + return CALL_CHILD (get_lifetime, pkbuf, pos, lifetime); +} + +int +udp_set_lifetime (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_lifetime_t lifetime) +{ + return CALL_CHILD (set_lifetime, pkbuf, pos, lifetime); +} + +int +udp_update_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + return CALL_CHILD (update_checksums, pkbuf, pos, partial_csum, payload_len); +} + +int +udp_update_checksums_incremental (const hicn_packet_buffer_t *pkbuf, + size_t pos, u16 *old_val, u16 *new_val, + u8 size, bool skip_first) +{ + return CALL_CHILD (update_checksums_incremental, pkbuf, pos, old_val, + new_val, size, false); +} + +int +udp_verify_checksums (const hicn_packet_buffer_t *pkbuf, size_t pos, + u16 partial_csum, size_t payload_len) +{ + return CALL_CHILD (verify_checksums, pkbuf, pos, partial_csum, payload_len); +} + +int +udp_rewrite_interest (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old) +{ + return CALL_CHILD (rewrite_interest, pkbuf, pos, addr_new, addr_old); +} + +int +udp_rewrite_data (const hicn_packet_buffer_t *pkbuf, size_t pos, + const hicn_ip_address_t *addr_new, + hicn_ip_address_t *addr_old, const hicn_faceid_t face_id, + u8 reset_pl) +{ + return CALL_CHILD (rewrite_data, pkbuf, pos, addr_new, addr_old, face_id, + reset_pl); +} + +int +udp_get_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t *payload_type) +{ + return CALL_CHILD (get_payload_type, pkbuf, pos, payload_type); +} + +int +udp_set_payload_type (const hicn_packet_buffer_t *pkbuf, size_t pos, + hicn_payload_type_t payload_type) +{ + return CALL_CHILD (set_payload_type, pkbuf, pos, payload_type); +} + +int +udp_get_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *signature_size) +{ + return CALL_CHILD (get_signature_size, pkbuf, pos, signature_size); +} + +int +udp_set_signature_size (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t signature_size) +{ + return CALL_CHILD (set_signature_size, pkbuf, pos, signature_size); +} + +int +udp_has_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, bool *flag) +{ + return CALL_CHILD (has_signature, pkbuf, pos, flag); +} + +int +udp_set_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t padding) +{ + return CALL_CHILD (set_signature_padding, pkbuf, pos, padding); +} + +int +udp_get_signature_padding (const hicn_packet_buffer_t *pkbuf, size_t pos, + size_t *padding) +{ + return CALL_CHILD (get_signature_padding, pkbuf, pos, padding); +} + +int +udp_set_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t signature_timestamp) +{ + return CALL_CHILD (set_signature_timestamp, pkbuf, pos, signature_timestamp); +} + +int +udp_get_signature_timestamp (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint64_t *signature_timestamp) +{ + return CALL_CHILD (get_signature_timestamp, pkbuf, pos, signature_timestamp); +} + +int +udp_set_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t validation_algorithm) +{ + return CALL_CHILD (set_validation_algorithm, pkbuf, pos, + validation_algorithm); +} + +int +udp_get_validation_algorithm (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t *validation_algorithm) +{ + return CALL_CHILD (get_validation_algorithm, pkbuf, pos, + validation_algorithm); +} + +int +udp_set_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, uint8_t *key_id, + size_t key_len) +{ + return CALL_CHILD (set_key_id, pkbuf, pos, key_id, key_len); +} + +int +udp_get_key_id (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **key_id, uint8_t *key_id_size) +{ + return CALL_CHILD (get_key_id, pkbuf, pos, key_id, key_id_size); +} + +int +udp_get_signature (const hicn_packet_buffer_t *pkbuf, size_t pos, + uint8_t **signature) +{ + return CALL_CHILD (get_signature, pkbuf, pos, signature); +} + +int +udp_is_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos, int *is_last) +{ + return CALL_CHILD (is_last_data, pkbuf, pos, is_last); +} + +int +udp_set_last_data (const hicn_packet_buffer_t *pkbuf, size_t pos) +{ + return CALL_CHILD (set_last_data, pkbuf, pos); +} + +DECLARE_HICN_OPS (udp, UDP_HDRLEN); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/protocol/udp.h b/lib/src/protocol/udp.h new file mode 100644 index 000000000..23f5317b3 --- /dev/null +++ b/lib/src/protocol/udp.h @@ -0,0 +1,44 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef HICN_PROTOCOL_UDP_H +#define HICN_PROTOCOL_UDP_H + +/* + * The len of the UDP header struct must be 8 bytes. + */ +#define EXPECTED_UDP_HDRLEN 8 + +typedef struct +{ + u16 src_port; + u16 dst_port; + u16 len; + u16 checksum; +} _udp_header_t; + +#define UDP_HDRLEN sizeof (_udp_header_t) +static_assert (EXPECTED_UDP_HDRLEN == UDP_HDRLEN, + "Size of UDP struct does not match its expected size."); + +#endif /* HICN_PROTOCOL_UDP_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/src/strategy.c b/lib/src/strategy.c new file mode 100644 index 000000000..3b797664a --- /dev/null +++ b/lib/src/strategy.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <hicn/strategy.h> + +const char *strategy_str[] = { +#define _(x) [STRATEGY_TYPE_##x] = #x, + foreach_strategy_type +#undef _ +};
\ No newline at end of file diff --git a/lib/src/test/CMakeLists.txt b/lib/src/test/CMakeLists.txt new file mode 100644 index 000000000..8cf4359d2 --- /dev/null +++ b/lib/src/test/CMakeLists.txt @@ -0,0 +1,55 @@ +# 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: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +find_package(Threads REQUIRED) + +############################################################## +# Test sources +############################################################## +list(APPEND TESTS_SRC + main.cc + test_name.cc + test_new_header.cc + test_udp_header.cc + test_validation.cc + test_bitmap.cc + test_interest_manifest.cc + test_ip_address.cc + test_khash.cc + test_pool.cc + test_ring.cc + test_slab.cc + test_vector.cc +) + +############################################################## +# Build single unit test executable and add it to test list +############################################################## +build_executable(lib_tests + NO_INSTALL + SOURCES ${TESTS_SRC} + LINK_LIBRARIES + PRIVATE ${LIBHICN_STATIC} + PRIVATE ${GTEST_LIBRARIES} + PRIVATE ${CMAKE_THREAD_LIBS_INIT} + INCLUDE_DIRS + PRIVATE ${Libhicn_INCLUDE_DIRS} + PRIVATE ${GTEST_INCLUDE_DIRS} + DEPENDS gtest ${LIBHICN_SHARED} + COMPONENT ${LIBHICN_COMPONENT} + DEFINITIONS ${COMPILER_DEFINITIONS} + COMPILE_OPTIONS ${DEFAULT_COMPILER_OPTIONS} + LINK_FLAGS ${LINK_FLAGS} +) + +add_test_internal(lib_tests) diff --git a/lib/src/test/main.cc b/lib/src/test/main.cc new file mode 100644 index 000000000..042fcd6c0 --- /dev/null +++ b/lib/src/test/main.cc @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +int +main (int argc, char **argv) +{ + ::testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/lib/src/test/test_bitmap.cc b/lib/src/test/test_bitmap.cc new file mode 100644 index 000000000..7fc8eef71 --- /dev/null +++ b/lib/src/test/test_bitmap.cc @@ -0,0 +1,261 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <netinet/in.h> + +extern "C" +{ +#define WITH_TESTS +#include <hicn/util/bitmap.h> +} + +#define DEFAULT_SIZE 10 + +class BitmapTest : public ::testing::Test +{ +protected: + BitmapTest () {} + + virtual ~BitmapTest () {} + + bitmap_t *bitmap; +}; + +/* + * TEST: bitmap allocation + */ +TEST_F (BitmapTest, BitmapAllocation) +{ + int rc; + + /* + * We take a value < 32 on purpose to avoid confusion on the choice of a 32 + * or 64 bit integer for storage + */ + size_t size_not_pow2 = DEFAULT_SIZE; + bitmap_init (bitmap, size_not_pow2, 0); + + /* + * Bitmap should have been allocated with a size rounded to the next power + * of 2 + */ + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 1UL); + + /* By default, no element should be set */ + EXPECT_FALSE (bitmap_is_set (bitmap, 0)); + EXPECT_TRUE (bitmap_is_unset (bitmap, 0)); + + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 1UL); + + EXPECT_FALSE (bitmap_is_set (bitmap, size_not_pow2 - 1)); + EXPECT_TRUE (bitmap_is_unset (bitmap, size_not_pow2 - 1)); + + /* Bitmap should not have been reallocated */ + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 1UL); + + /* After setting a bit after the end, bitmap should have been reallocated */ + bitmap_set (bitmap, sizeof (bitmap[0]) * 8 - 1); + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 1UL); + + /* After setting a bit after the end, bitmap should have been reallocated */ + rc = bitmap_set (bitmap, sizeof (bitmap[0]) * 8); + EXPECT_GE (rc, 0); + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 2UL); + + rc = bitmap_set (bitmap, sizeof (bitmap[0]) * 8 + 1); + EXPECT_GE (rc, 0); + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 2UL); + + bitmap_free (bitmap); + + size_t size_pow2 = 16; + + /* Limiting test for allocation size */ + bitmap_init (bitmap, size_pow2, 0); + EXPECT_EQ (bitmap_get_alloc_size (bitmap), 1UL); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + bitmap_set (bitmap, 20); + EXPECT_TRUE (bitmap_is_set (bitmap, 20)); + EXPECT_FALSE (bitmap_is_unset (bitmap, 20)); + EXPECT_FALSE (bitmap_is_set (bitmap, 19)); + EXPECT_TRUE (bitmap_is_unset (bitmap, 19)); + + // Test edge cases (i.e. start and end of block) + off_t start_position = 0; + bitmap_set (bitmap, start_position); + EXPECT_TRUE (bitmap_is_set (bitmap, start_position)); + EXPECT_FALSE (bitmap_is_unset (bitmap, start_position)); + + off_t end_position = BITMAP_WIDTH (bitmap) - 1; + bitmap_set (bitmap, end_position); + EXPECT_TRUE (bitmap_is_set (bitmap, end_position)); + EXPECT_FALSE (bitmap_is_unset (bitmap, end_position)); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapUnSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + bitmap_set (bitmap, 20); + bitmap_set (bitmap, 19); + bitmap_unset (bitmap, 20); + EXPECT_FALSE (bitmap_is_set (bitmap, 20)); + EXPECT_TRUE (bitmap_is_unset (bitmap, 20)); + EXPECT_TRUE (bitmap_is_set (bitmap, 19)); + EXPECT_FALSE (bitmap_is_unset (bitmap, 19)); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapSetTo) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + bitmap_set_to (bitmap, 40); + EXPECT_TRUE (bitmap_is_set (bitmap, 20)); + EXPECT_TRUE (bitmap_is_set (bitmap, 21)); + EXPECT_TRUE (bitmap_is_unset (bitmap, 41)); + EXPECT_TRUE (bitmap_is_unset (bitmap, 42)); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapFirstSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + // Get first set bit. It should be INVALID_INDEX + EXPECT_EQ (bitmap_first_set (bitmap), BITMAP_INVALID_INDEX); + + // set bit 40 + bitmap_set (bitmap, 40); + EXPECT_TRUE (bitmap_is_set (bitmap, 40)); + + // Get first set bit. It should be bit 40 (surprise):) + EXPECT_EQ (bitmap_first_set (bitmap), hicn_uword (40)); + + // set bit 3 + bitmap_set (bitmap, 3); + EXPECT_TRUE (bitmap_is_set (bitmap, 3)); + + // The first set bit now should be bit #3 + EXPECT_EQ (bitmap_first_set (bitmap), hicn_uword (3)); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapFirstUnSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + // Get first unset bit. It should be 0 + EXPECT_EQ (bitmap_first_unset (bitmap), hicn_uword (0)); + + // set bit 0 + bitmap_set (bitmap, 0); + EXPECT_TRUE (bitmap_is_set (bitmap, 0)); + + // Get first unset bit. It should be bit 1 + EXPECT_EQ (bitmap_first_unset (bitmap), hicn_uword (1)); + + // set bit 3 + bitmap_set (bitmap, 3); + EXPECT_TRUE (bitmap_is_set (bitmap, 3)); + + // The first set bit now should be still 1 + EXPECT_EQ (bitmap_first_unset (bitmap), hicn_uword (1)); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapNextSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + // Get next unset bit >= 0. It should be INVALID + EXPECT_EQ (bitmap_next_set (bitmap, 0), BITMAP_INVALID_INDEX); + + // set bit 0 + bitmap_set (bitmap, 0); + EXPECT_TRUE (bitmap_is_set (bitmap, 0)); + + // Get next set bit >= 0. It should be bit 0 + EXPECT_EQ (bitmap_next_set (bitmap, 0), hicn_uword (0)); + + // set bit 3 + bitmap_set (bitmap, 3); + EXPECT_TRUE (bitmap_is_set (bitmap, 3)); + + // Get next set bit >= 1. It should be 3 + EXPECT_EQ (bitmap_next_set (bitmap, 1), hicn_uword (3)); + + // set (N-2)th bit + bitmap_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 2); + EXPECT_TRUE (bitmap_is_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 2)); + EXPECT_EQ (bitmap_next_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 3), + DEFAULT_SIZE * WORD_WIDTH - 2); + + // set (N-1)th bit + bitmap_unset (bitmap, DEFAULT_SIZE * WORD_WIDTH - 2); + bitmap_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 1); + EXPECT_TRUE (bitmap_is_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 1)); + EXPECT_EQ (bitmap_next_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 2), + DEFAULT_SIZE * WORD_WIDTH - 1); + EXPECT_EQ (bitmap_next_set (bitmap, DEFAULT_SIZE * WORD_WIDTH - 1), + DEFAULT_SIZE * WORD_WIDTH - 1); + + bitmap_free (bitmap); +} + +TEST_F (BitmapTest, BitmapNextUnSet) +{ + bitmap_init (bitmap, DEFAULT_SIZE, 0); + + // Get next unset bit >= 0. It should be 0 + EXPECT_EQ (bitmap_next_unset (bitmap, 0), hicn_uword (0)); + + // set bit 0 + bitmap_set (bitmap, 0); + EXPECT_TRUE (bitmap_is_set (bitmap, 0)); + + // Get next set bit >= 0. It should be bit 1 + EXPECT_EQ (bitmap_next_unset (bitmap, 0), hicn_uword (1)); + + // set bit 3 + bitmap_set (bitmap, 3); + EXPECT_TRUE (bitmap_is_set (bitmap, 3)); + + // Get next unset bit after 3. It should be 4 + EXPECT_EQ (bitmap_next_unset (bitmap, 3), hicn_uword (4)); + + bitmap_free (bitmap); +} diff --git a/lib/src/test/test_interest_manifest.cc b/lib/src/test/test_interest_manifest.cc new file mode 100644 index 000000000..25ac83728 --- /dev/null +++ b/lib/src/test/test_interest_manifest.cc @@ -0,0 +1,224 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> +#include <gmock/gmock.h> + +extern "C" +{ +#include <hicn/interest_manifest.h> +} + +static constexpr hicn_uword WORD_SIZE = WORD_WIDTH; + +class InterestManifestTest : public ::testing::Test +{ +protected: + static constexpr u32 n_suffixes = 0x00000014 + 1; + static constexpr u32 padding = 0x21232425; + static constexpr hicn_uword bitmap_word = ~0ULL; + static inline std::vector<uint32_t> values = { 10, 22, 23, 43, 54, 65, 66, + 4, 33, 2, 44, 99, 87, 67, + 78, 98, 76, 1, 7, 123 }; + InterestManifestTest () {} + virtual ~InterestManifestTest () {} + + uint8_t buffer[512]; + hicn_uword bitmap_saved[BITMAP_SIZE]; +}; + +TEST_F (InterestManifestTest, OneWordBitmapUpdate) +{ + hicn_uword initial_bitmap[1]; + hicn_uword curr_bitmap[1] = { 0 }; + initial_bitmap[0] = + 0x0000000000000b07; // ...000000000000000000000101100000111 + + // Consume first 4 'one' bits (i.e. suffixes), reaching position 9 + size_t pos = 0, max_suffixes = 4; + pos = interest_manifest_update_bitmap (initial_bitmap, curr_bitmap, pos, + WORD_SIZE, max_suffixes); + EXPECT_EQ (pos, std::size_t (9)); + EXPECT_EQ (curr_bitmap[0], hicn_uword (0x0000000000000107)); + + // Consume the remaining 2 'one' bits, reaching end of bitmap + hicn_uword curr_bitmap2[1] = { 0 }; + pos = interest_manifest_update_bitmap (initial_bitmap, curr_bitmap2, pos, + WORD_SIZE, max_suffixes); + EXPECT_EQ (pos, WORD_SIZE); + EXPECT_EQ (curr_bitmap2[0], hicn_uword (0x00000a00)); + + // Consume all suffixes at once + hicn_uword curr_bitmap3[1] = { 0 }; + max_suffixes = 16; + pos = interest_manifest_update_bitmap (initial_bitmap, curr_bitmap3, 0, + WORD_SIZE, max_suffixes); + EXPECT_EQ (pos, WORD_SIZE); + EXPECT_EQ (curr_bitmap3[0], initial_bitmap[0]); +} + +TEST_F (InterestManifestTest, TwoWordBitmapUpdate) +{ + hicn_uword initial_bitmap[2]; + initial_bitmap[0] = 0x0000000000000b07; + initial_bitmap[1] = 0x0000000000000b07; + // -> 0000000000000000000010110000011100000000000000000000101100000111 + + int expected_pos[] = { WORD_SIZE + 2, 2 * WORD_SIZE }; + u32 expected_bitmap[][2] = { { 0x00000b07, 0x00000003 }, + { 0x0, 0x00000b04 } }; + + // Loop to consume all suffixes + int pos = 0, max_suffixes = 8, i = 0, len = WORD_SIZE * 2; + while (pos != len) + { + hicn_uword curr_bitmap[2] = { 0 }; + pos = interest_manifest_update_bitmap (initial_bitmap, curr_bitmap, pos, + len, max_suffixes); + + EXPECT_EQ (pos, expected_pos[i]); + EXPECT_EQ (curr_bitmap[0], expected_bitmap[i][0]); + EXPECT_EQ (curr_bitmap[1], expected_bitmap[i][1]); + i++; + } +} + +TEST_F (InterestManifestTest, SerializeDeserialize) +{ +#if hicn_uword_bits == 64 +#define F(x) hicn_host_to_net_64 (x) +#elif hicn_uword_bits == 32 +#define F(x) hicn_host_to_net_32 (x) +#else +#error "Unrecognized architecture" +#endif + + auto header = reinterpret_cast<interest_manifest_header_t *> (buffer); + interest_manifest_init (header, 0); + + for (const auto &v : values) + { + interest_manifest_add_suffix (header, v); + } + + EXPECT_EQ (header->n_suffixes, n_suffixes); + + // Save bitmap + memcpy (bitmap_saved, header->request_bitmap, sizeof (bitmap_saved)); + + // Serialize manifest + interest_manifest_serialize (header); + + // If architecture is little endian, bytes should be now swapped + EXPECT_THAT (header->n_suffixes, ::testing::Eq (hicn_host_to_net_32 ( + n_suffixes) /* 0x14000000 */)); + + for (unsigned i = 0; i < BITMAP_SIZE; i++) + { + EXPECT_THAT (header->request_bitmap[i], + ::testing::Eq (F (bitmap_saved[i]))); + } + + hicn_name_suffix_t *suffix = (hicn_name_suffix_t *) (header + 1); + for (unsigned i = 0; i < n_suffixes; i++) + { + if (i == 0) + { + EXPECT_THAT (*(suffix + i), ::testing::Eq (hicn_name_suffix_t (0))); + } + else + { + EXPECT_THAT (*(suffix + i), + ::testing::Eq (hicn_host_to_net_32 (values[i - 1]))); + } + } + + // Deserialize manifest + interest_manifest_deserialize (header); + + // Bytes should now be as before + EXPECT_THAT (header->n_suffixes, ::testing::Eq (n_suffixes)); + + int i = 0; + unsigned long pos; + interest_manifest_foreach_suffix (header, suffix, pos) + { + if (pos == 0) + { + EXPECT_THAT (*suffix, ::testing::Eq (hicn_name_suffix_t (0))); + } + else + { + EXPECT_THAT (*suffix, ::testing::Eq (values[i])); + i++; + } + } +} + +TEST_F (InterestManifestTest, ForEach) +{ + unsigned long pos; + hicn_name_suffix_t *suffix = nullptr; + auto header = reinterpret_cast<interest_manifest_header_t *> (buffer); + interest_manifest_init (header, 0); + + for (const auto &v : values) + { + interest_manifest_add_suffix (header, v); + } + + // Iterate over interest manifest. bBbitmap should be all 1, we should be + // able to iterate over all suffixes. + interest_manifest_foreach_suffix (header, suffix, pos) + { + if (pos == 0) + { + EXPECT_EQ (*suffix, hicn_name_suffix_t (0)); + } + else + { + EXPECT_EQ (*suffix, values[pos - 1]); + } + } + + std::set<uint32_t> set_values (values.begin (), values.end ()); + + // Unset few bitmap positions + interest_manifest_del_suffix (header, 5 + 1); + set_values.erase (values[5]); + + interest_manifest_del_suffix (header, 6 + 1); + set_values.erase (values[6]); + + interest_manifest_del_suffix (header, 12 + 1); + set_values.erase (values[12]); + + interest_manifest_del_suffix (header, 17 + 1); + set_values.erase (values[17]); + + // Iterate over interest manifest and remove elements in manifest from set. + // The set should be empty at the end. + interest_manifest_foreach_suffix (header, suffix, pos) + { + if (pos > 0) + { + EXPECT_TRUE (set_values.find (*suffix) != set_values.end ()) + << "The value was " << *suffix; + set_values.erase (*suffix); + } + } + + EXPECT_TRUE (set_values.empty ()); +}
\ No newline at end of file diff --git a/lib/src/test/test_ip_address.cc b/lib/src/test/test_ip_address.cc new file mode 100644 index 000000000..1a7efb603 --- /dev/null +++ b/lib/src/test/test_ip_address.cc @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdio.h> +#include <stdlib.h> + +extern "C" +{ +#define WITH_TESTS +#include <hicn/util/ip_address.h> +} + +#define DEFAULT_SIZE 10 + +class IPAddressTest : public ::testing::Test +{ +protected: + IPAddressTest () {} + + virtual ~IPAddressTest () {} +}; + +TEST_F (IPAddressTest, IPAddressGetBit) +{ + hicn_ip_address_t a_0, a_1, a_1_0, a_1_1, a_1_48, a_1_49, a_1_63, a_1_64, + a_1_127; + + hicn_ip_address_pton ("0::0", &a_0); + hicn_ip_address_pton ("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &a_1); + + hicn_ip_address_pton ("8000::0", &a_1_0); + hicn_ip_address_pton ("4000::0", &a_1_1); + hicn_ip_address_pton ("0:0:0:8000::0", &a_1_48); + hicn_ip_address_pton ("0:0:0:4000::0", &a_1_49); + hicn_ip_address_pton ("0:0:0:1::0", &a_1_63); + hicn_ip_address_pton ("0::8000:0:0:0", &a_1_64); + hicn_ip_address_pton ("0::1", &a_1_127); + +#if 0 + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_0, i), 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1, i), 1); +#endif + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_0, i), (i == 0) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_1, i), (i == 1) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_48, i), (i == 48) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_49, i), (i == 49) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_63, i), (i == 63) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_64, i), (i == 64) ? 1 : 0); + for (unsigned i = 0; i < 128; i++) + EXPECT_EQ (hicn_ip_address_get_bit (&a_1_127, i), (i == 127) ? 1 : 0); +} diff --git a/lib/src/test/test_khash.cc b/lib/src/test/test_khash.cc new file mode 100644 index 000000000..da6b62071 --- /dev/null +++ b/lib/src/test/test_khash.cc @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <netinet/in.h> + +extern "C" +{ +#include <hicn/util/khash.h> +} + +KHASH_MAP_INIT_INT (int, unsigned char) + +typedef struct +{ + unsigned key; + unsigned char val; +} int_unpack_t; + +typedef struct +{ + unsigned key; + unsigned char val; +} __attribute__ ((__packed__)) int_packed_t; + +#define hash_eq(a, b) ((a).key == (b).key) +#define hash_func(a) ((a).key) + +KHASH_INIT (iun, int_unpack_t, char, 0, hash_func, hash_eq) +KHASH_INIT (ipk, int_packed_t, char, 0, hash_func, hash_eq) + +class KHashTest : public ::testing::Test +{ +protected: + KHashTest () {} + + virtual ~KHashTest () + { + // You can do clean-up work that doesn't throw exceptions here. + } + + // If the constructor and destructor are not enough for setting up + // and cleaning up each test, you can define the following methods: + + virtual void + SetUp () + { + khash = kh_init (int); + } + + virtual void + TearDown () + { + kh_destroy (int, khash); + } + khash_t (int) * khash; +}; + +TEST_F (KHashTest, KhashIntSize) +{ + int ret; + int k; + int size = kh_size (khash); + + EXPECT_EQ (size, 0); + k = kh_put (int, khash, 10, &ret); + if (ret == 1) + { + kh_val (khash, k) = 10; + } + size = kh_size (khash); + EXPECT_EQ (size, 1); +} + +TEST_F (KHashTest, KhashIntPut) +{ + int ret; + int k; + k = kh_put (int, khash, 10, &ret); + if (ret == 1) + { + kh_val (khash, k) = 10; + } + int size = kh_size (khash); + EXPECT_EQ (size, 1); + k = kh_put (int, khash, 20, &ret); + if (ret == 1) + { + kh_val (khash, k) = 20; + } + size = kh_size (khash); + EXPECT_EQ (size, 2); +} + +TEST_F (KHashTest, KhashCheckValue) +{ + int ret; + int k; + k = kh_put (int, khash, 10, &ret); + if (ret == 1) + { + kh_val (khash, k) = 100; + } + k = kh_put (int, khash, 20, &ret); + if (ret == 1) + { + kh_val (khash, k) = 200; + } + + k = kh_put (int, khash, 10, &ret); + int val = -1; + if (!ret) + val = kh_val (khash, k); + EXPECT_EQ (val, 100); + + k = kh_put (int, khash, 20, &ret); + val = -1; + if (!ret) + val = kh_val (khash, k); + EXPECT_EQ (val, 200); +} + +// Check that there are no collisions in case of same key hash +typedef struct +{ + int x; +} Key; +#define hash_key(key) 1 // Hash is always 1 to simulate collisions +#define key_hash_eq(a, b) (a->x == b->x) // Function used in case of collisions +KHASH_INIT (test_map, const Key *, unsigned, 1, hash_key, key_hash_eq); + +TEST_F (KHashTest, Collisions) +{ + int ret; + khiter_t k; + + kh_test_map_t *map = kh_init (test_map); + Key key1 = { .x = 10 }; + Key key2 = { .x = 11 }; + + k = kh_put_test_map (map, &key1, &ret); + ASSERT_EQ (ret, 1); + kh_val (map, k) = 15u; + + k = kh_put_test_map (map, &key2, &ret); + ASSERT_EQ (ret, 1); + kh_val (map, k) = 27u; + + k = kh_get_test_map (map, &key1); + ASSERT_NE (k, kh_end (map)); + unsigned val = kh_val (map, k); + EXPECT_EQ (val, 15u); + + k = kh_get_test_map (map, &key2); + ASSERT_NE (k, kh_end (map)); + val = kh_val (map, k); + EXPECT_EQ (val, 27u); + + kh_destroy_test_map (map); +} diff --git a/lib/src/test/test_name.cc b/lib/src/test/test_name.cc new file mode 100644 index 000000000..0cf160f70 --- /dev/null +++ b/lib/src/test/test_name.cc @@ -0,0 +1,401 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +} + +class NameTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + NameTest () : name_{}, name4_{}, name6_{} + { + ipv6_prefix_bytes = {}; + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + ipv4_prefix_bytes = {}; + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + virtual ~NameTest () {} + + void + nameHashTest (const char *prefix) + { + // Create 2 names + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (prefix, suffix, &name_b); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // The hash should be equal, with and without considering the suffix + uint32_t hash_a, hash_b; + hash_a = hicn_name_get_hash (&name_a); + hash_b = hicn_name_get_hash (&name_b); + EXPECT_EQ (hash_a, hash_b); + + hash_a = hicn_name_get_prefix_hash (&name_a); + hash_b = hicn_name_get_prefix_hash (&name_b); + EXPECT_EQ (hash_a, hash_b); + + // Now let's change the suffix + rc = hicn_name_set_suffix (&name_a, 97531); + // They should result equal if we do not consider the suffix + hash_a = hicn_name_get_prefix_hash (&name_a); + hash_b = hicn_name_get_prefix_hash (&name_b); + EXPECT_EQ (hash_a, hash_b); + + // And different if we consider it + hash_a = hicn_name_get_hash (&name_a); + hash_b = hicn_name_get_hash (&name_b); + EXPECT_NE (hash_a, hash_b); + } + + void + nameCopyTest (const char *prefix) + { + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_copy (&name_b, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_EQ (rc, 0); + } + + void + nameCompareTest (const char *prefix) + { + // Create 2 names + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (prefix, suffix, &name_b); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // They should be equal, with and without considering the suffix + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_compare (&name_a, &name_b, 0); + EXPECT_EQ (rc, 0); + + // Now let's change the suffix + rc = hicn_name_set_suffix (&name_a, 97531); + // They should result equal if we do not consider the suffix + rc = hicn_name_compare (&name_a, &name_b, 0); + EXPECT_EQ (rc, 0); + // And different if we consider the suffix + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_NE (rc, 0); + } + + void + nameFromIpPrefixTest (const hicn_ip_prefix_t &hicn_ip_prefix) + { + uint32_t suffix = 54321; + hicn_name_t name; + int rc = hicn_name_create_from_ip_prefix (&hicn_ip_prefix, suffix, &name); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = memcmp (hicn_ip_prefix.address.v6.as_u8, name.prefix.v6.as_u8, + sizeof (name.prefix.v6)); + EXPECT_EQ (rc, 0); + EXPECT_EQ (suffix, name.suffix); + } + + void + nameToIpPrefixTest (const char *prefix) + { + uint32_t suffix = 54321; + hicn_name_t name; + int rc = hicn_name_create (prefix, suffix, &name); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Get family + int family; + rc = hicn_name_get_family (&name, &family); + + hicn_ip_prefix_t hicn_ip_prefix; + rc = hicn_name_to_hicn_ip_prefix (&name, &hicn_ip_prefix); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hicn_ip_prefix.family, family); + rc = hicn_ip_address_cmp (&hicn_ip_prefix.address, &name.prefix); + EXPECT_EQ (rc, 0); + } + + hicn_name_t name_, name4_, name6_; + hicn_ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +/** + * Name Initialization + */ +TEST_F (NameTest, NameInitialization) +{ + EXPECT_TRUE (_is_unspec (&name_)); + uint32_t suffix = 12345; + + // Initialize ipv6 name + hicn_name_t name6; + int rc = hicn_name_create (ipv6_prefix, suffix, &name6); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = hicn_ip_address_cmp (&name6.prefix, &ipv6_prefix_bytes); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name6.suffix, suffix); + + // Initialize ipv4 name + hicn_name_t name4; + rc = hicn_name_create (ipv4_prefix, suffix, &name4); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = hicn_ip_address_cmp (&name4.prefix, &ipv4_prefix_bytes); + EXPECT_EQ (name4.prefix.pad[0], 0UL); + EXPECT_EQ (name4.prefix.pad[1], 0UL); + EXPECT_EQ (name4.prefix.pad[2], 0UL); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name4.suffix, suffix); + + // Try also to reuse previously initialized name + rc = hicn_name_create (ipv4_prefix, suffix, &name6); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = hicn_ip_address_cmp (&name6.prefix, &ipv4_prefix_bytes); + EXPECT_EQ (name6.prefix.pad[0], 0UL); + EXPECT_EQ (name6.prefix.pad[1], 0UL); + EXPECT_EQ (name6.prefix.pad[2], 0UL); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name6.suffix, suffix); +} + +/** + * Name from ip prefix + */ +TEST_F (NameTest, NameFromIpPrefix6) +{ + hicn_ip_prefix_t hicn_ip_prefix = { .family = AF_INET6, + .address = {}, + .len = 64 }; + + hicn_ip_prefix.address.v6.as_u64[0] = ipv6_prefix_bytes.v6.as_u64[0]; + hicn_ip_prefix.address.v6.as_u64[1] = ipv6_prefix_bytes.v6.as_u64[1]; + + nameFromIpPrefixTest (hicn_ip_prefix); +} + +TEST_F (NameTest, NameFromIpPrefix4) +{ + hicn_ip_prefix_t hicn_ip_prefix = { .family = AF_INET, + .address = {}, + .len = 64 }; + hicn_ip_prefix.address.v4.as_u32 = ipv4_prefix_bytes.v4.as_u32; + hicn_ip_prefix.address.pad[0] = 0; + hicn_ip_prefix.address.pad[1] = 0; + hicn_ip_prefix.address.pad[2] = 0; + nameFromIpPrefixTest (hicn_ip_prefix); +} + +TEST_F (NameTest, NameCompare6) { nameCompareTest (ipv6_prefix); } + +TEST_F (NameTest, NameCompare4) { nameCompareTest (ipv4_prefix); } + +TEST_F (NameTest, NameHash6) { nameHashTest (ipv6_prefix); } + +TEST_F (NameTest, NameHash4) { nameHashTest (ipv4_prefix); } + +TEST_F (NameTest, NameEmpty) +{ + int rc = hicn_name_empty (&name_); + EXPECT_EQ (rc, 1); + + name_.prefix.v6 = ipv6_prefix_bytes.v6; + rc = hicn_name_empty (&name_); + EXPECT_EQ (rc, 0); +} + +TEST_F (NameTest, NameCopy6) { nameCopyTest (ipv6_prefix); } + +TEST_F (NameTest, NameCopy4) { nameCopyTest (ipv4_prefix); } + +TEST_F (NameTest, NameCopyToDestination) +{ + ipv4_address_t dst4; + ipv6_address_t dst6; + + // Copy names to destination + int rc = hicn_name_copy_prefix_to_destination (dst4.as_u8, &name4_); + EXPECT_EQ (rc, 0); + rc = hicn_name_copy_prefix_to_destination (dst6.as_u8, &name6_); + EXPECT_EQ (rc, 0); + + // Check copy succeeded + EXPECT_TRUE (dst4.as_u32 == name4_.prefix.v4.as_u32); + EXPECT_TRUE (dst6.as_u64[0] == name6_.prefix.v6.as_u64[0]); + EXPECT_TRUE (dst6.as_u64[1] == name6_.prefix.v6.as_u64[1]); +} + +TEST_F (NameTest, SetGetSuffix) +{ + uint32_t suffix2 = 55555, suffix_ret; + + // Check if suffix is correct + int rc = hicn_name_get_seq_number (&name6_, &suffix_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (suffix, suffix_ret); + + // Set new suffix + rc = hicn_name_set_suffix (&name6_, suffix2); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check suffix was set + rc = hicn_name_get_seq_number (&name6_, &suffix_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (suffix2, suffix_ret); +} + +TEST_F (NameTest, NameToSockAddr) +{ + struct sockaddr_in saddr4; + struct sockaddr_in6 saddr6; + + int rc = + hicn_name_to_sockaddr_address (&name6_, (struct sockaddr *) (&saddr6)); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = memcmp (name6_.prefix.v6.as_u8, saddr6.sin6_addr.s6_addr, + sizeof (name6_.prefix.v6)); + EXPECT_EQ (rc, 0); + + rc = hicn_name_to_sockaddr_address (&name4_, (struct sockaddr *) (&saddr4)); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (name4_.prefix.v4.as_u32, saddr4.sin_addr.s_addr); +} + +TEST_F (NameTest, NameToIpPrefix) +{ + nameToIpPrefixTest (ipv4_prefix); + nameToIpPrefixTest (ipv6_prefix); +} + +TEST_F (NameTest, NameNToP) +{ + char dst[128]; + + // V6 + int rc = hicn_name_ntop (&name6_, dst, 128); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Build expected name + std::stringstream expected6; + expected6 << ipv6_prefix << "|" << suffix; + + rc = strcmp (dst, expected6.str ().c_str ()); + EXPECT_EQ (rc, 0); + + // V4 + rc = hicn_name_ntop (&name4_, dst, 128); + std::stringstream expected4; + expected4 << ipv4_prefix << "|" << suffix; + + rc = strcmp (dst, expected4.str ().c_str ()); + EXPECT_EQ (rc, 0); +} + +class PrefixTest : public ::testing::Test +{ +protected: + PrefixTest () {} + + virtual ~PrefixTest () {} +}; + +#define HICN_PREFIX(P, STR) \ + hicn_prefix_t P; \ + hicn_ip_prefix_t _##P; \ + EXPECT_EQ (hicn_ip_prefix_pton (STR, &_##P), 0); \ + EXPECT_EQ (hicn_prefix_create_from_ip_prefix (&_##P, &P), 0); + +TEST_F (PrefixTest, PrefixClear) +{ + HICN_PREFIX (hp_all, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"); + + for (unsigned i = 0; i < 127; i++) + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, i), 1); + + hicn_prefix_truncate (&hp_all, 127); + EXPECT_EQ (hicn_prefix_get_len (&hp_all), 127); + + for (unsigned i = 0; i < 126; i++) + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, i), 1) << "bit[" << i << "] != 1"; + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 127), 0); + + hicn_prefix_truncate (&hp_all, 126); + EXPECT_EQ (hicn_prefix_get_len (&hp_all), 126); + + for (unsigned i = 0; i < 125; i++) + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, i), 1); + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 126), 0); + EXPECT_EQ (_hicn_prefix_get_bit (&hp_all, 127), 0); +} + +TEST_F (PrefixTest, PrefixClear2) +{ + HICN_PREFIX (hp_all, "b002::3"); + + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 125), 0); + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 126), 1); + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 127), 1); + + hicn_prefix_truncate (&hp_all, 127); + EXPECT_EQ (hicn_prefix_get_len (&hp_all), 127); + + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 125), 0); + EXPECT_EQ (hicn_prefix_get_bit (&hp_all, 126), 1); + EXPECT_EQ (_hicn_prefix_get_bit (&hp_all, 127), 0); +} + +TEST_F (PrefixTest, PrefixLPM) +{ + HICN_PREFIX (b007, "b007::/64"); + HICN_PREFIX (b009, "b009::/64"); + + EXPECT_EQ (hicn_prefix_lpm (&b007, &b009), (uint32_t) 12); + + HICN_PREFIX (pfx, "1122:3344:5566:7788:9900:aabb:ccdd:eeff/128"); + EXPECT_EQ (hicn_prefix_lpm (&pfx, &pfx), (uint32_t) 128); +} diff --git a/lib/src/test/test_new_header.cc b/lib/src/test/test_new_header.cc new file mode 100644 index 000000000..c936b6910 --- /dev/null +++ b/lib/src/test/test_new_header.cc @@ -0,0 +1,346 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/packet.h> + +#include "../protocol/ah.h" +#include "../protocol/new.h" +} + +class NewHeaderTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + NewHeaderTest (hicn_packet_format_t format) + : buffer_ (new uint8_t[NEW_HDRLEN]), + format_ (format), name_{}, name4_{}, name6_{} + { + + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + NewHeaderTest () : NewHeaderTest (HICN_PACKET_FORMAT_NEW) {} + + virtual ~NewHeaderTest () { delete[] buffer_; } + + void + checkCommon () + { + // Initialize header + hicn_packet_set_format (&pkbuf_, format_); + // pkbuf_set_type (&pkbuf_, HICN_PACKET_TYPE_UNDEFINED); + int rc = hicn_packet_init_header (&pkbuf_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + auto new_hdr = (_new_header_t *) buffer_; + + // Check fields + EXPECT_EQ (new_hdr->prefix.v6.as_u64[0], 0UL); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[1], 0UL); + EXPECT_EQ (new_hdr->suffix, 0UL); + EXPECT_EQ (new_hdr->lifetime, 0UL); + EXPECT_EQ (new_hdr->path_label, 0UL); + EXPECT_EQ (new_hdr->payload_len, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + EXPECT_EQ (new_hdr->flags, 0); + } + + virtual void + SetUp () override + { + checkCommon (); + } + + uint8_t *buffer_; + hicn_packet_buffer_t pkbuf_; + hicn_packet_format_t format_; + hicn_name_t name_, name4_, name6_; + hicn_ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +#if 0 +class NewHeaderAHTest : public NewHeaderTest +{ +protected: + NewHeaderAHTest () : NewHeaderTest (NEW_HDRLEN + AH_HDRLEN, HF_NEW_AH) {} + + virtual void + SetUp () override + { + auto new_hdr = &header_->protocol.newhdr; + checkCommon (new_hdr); + EXPECT_NE (new_hdr->flags, 0); + } +}; + +/** + * Header Initialization + */ +TEST_F (NewHeaderTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_NEW); +} + +TEST_F (NewHeaderAHTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_NEW_AH); +} + +/** + * @brief Checksum functions are not required, but we keep them for + * compatibility. + */ +TEST_F (NewHeaderTest, Checksum) +{ + // Get format from existing packet + int rc = hicn_packet_compute_checksum (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_compute_header_checksum (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderAHTest, Checksum) +{ + // Get format from existing packet + int rc = hicn_packet_compute_checksum (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_compute_header_checksum (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN); +} + +TEST_F (NewHeaderAHTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (NewHeaderTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN); +} + +TEST_F (NewHeaderAHTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (NewHeaderTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (NewHeaderAHTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (NewHeaderTest, SetGetName) +{ + // Get v6 name and set it to new_header + hicn_name_t name_ret; + int rc = hicn_packet_set_name (format_, header_, &name6_, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_name (format_, header_, &name_ret, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name6_, &name_ret, 1); + EXPECT_EQ (rc, 0); +} + +TEST_F (NewHeaderTest, SetGetLocator) +{ + // This function does nothing but it is set for compatibility + hicn_ip_address_t locator; + memset (&locator, 0, sizeof (locator)); + locator.v6.as_u8[15] = 1; + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + // FixMe no error raised here + size_t signature_size = 128; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + (void) rc; + // EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size); + // EXPECT_NE (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderAHTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + size_t signature_size = 128, signature_size_ret; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (signature_size, signature_size_ret); +} + +TEST_F (NewHeaderTest, IsInterestIsData) +{ + // Mark packet as interest + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + int ret; + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 1); + + // Mark packet as data + rc = hicn_packet_set_data (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 0); +} + +TEST_F (NewHeaderTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (NewHeaderAHTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (NewHeaderTest, SetGetPayloadType) +{ + // Lifetime + hicn_payload_type_t payload_type = HPT_MANIFEST, payload_type_ret; + int rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); + + payload_type = HPT_DATA; + + rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); +} +#endif diff --git a/lib/src/test/test_pool.cc b/lib/src/test/test_pool.cc new file mode 100644 index 000000000..cbb5ce068 --- /dev/null +++ b/lib/src/test/test_pool.cc @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <netinet/in.h> + +extern "C" +{ +#define WITH_TESTS +#include <hicn/util/pool.h> +} + +/* + * TODO + * - test max_size + */ + +#define DEFAULT_SIZE 10 + +class PoolTest : public ::testing::Test +{ +protected: + PoolTest () {} + virtual ~PoolTest () {} + + int *pool; +}; + +TEST_F (PoolTest, PoolAllocation) +{ + int rc; + + pool_init (pool, DEFAULT_SIZE, 0); + + size_t pool_size = next_pow2 (DEFAULT_SIZE); + + EXPECT_EQ (pool_get_alloc_size (pool), pool_size); + + /* Check that free indices and bitmaps are correctly initialize */ + off_t *fi = pool_get_free_indices (pool); + EXPECT_EQ (vector_len (fi), pool_size); + EXPECT_EQ (fi[0], (long) (pool_size - 1)); + EXPECT_EQ (fi[pool_size - 1], 0); + + /* The allocated size of the underlying vector should be the next power of + * two + */ + EXPECT_EQ (vector_get_alloc_size (fi), pool_size); + + bitmap_t *fb = pool_get_free_bitmap (pool); + EXPECT_TRUE (bitmap_is_set (fb, 0)); + EXPECT_TRUE (bitmap_is_set (fb, pool_size - 2)); + EXPECT_TRUE (bitmap_is_set (fb, pool_size - 1)); + EXPECT_TRUE (bitmap_is_unset (fb, pool_size)); + + /* Getting elements from the pool should correctly update the free indices + * and bitmap */ + int *elt; + + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + EXPECT_EQ (vector_len (fi), pool_size - 1); + EXPECT_TRUE (bitmap_is_unset (fb, 0)); + + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + EXPECT_EQ (vector_len (fi), pool_size - 2); + EXPECT_TRUE (bitmap_is_unset (fb, 1)); + + for (unsigned i = 0; i < pool_size - 4; i++) + { + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + } + + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + EXPECT_EQ (vector_len (fi), 1UL); + EXPECT_TRUE (bitmap_is_unset (fb, pool_size - 2)); + + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + EXPECT_EQ (vector_len (fi), 0UL); + EXPECT_TRUE (bitmap_is_unset (fb, pool_size - 1)); + + /* + * Getting elements within the allocated range should not have triggered a + * resize + */ + EXPECT_EQ (pool_len (pool), pool_size); + + /* + * Getting elements once the allocated range has been exceeded should + * trigger a resize + */ + rc = pool_get (pool, elt); + EXPECT_GE (rc, 0); + + EXPECT_EQ (pool_get_alloc_size (pool), pool_size * 2); + + EXPECT_EQ (pool_len (pool), pool_size + 1); + + /* + * Doubling the size, we should have again pool_size elements free, minus 1 + */ + EXPECT_EQ (pool_get_free_indices_size (pool), pool_size - 1); + + /* + * NOTE: this is wrong as there has been a realloc and the old fi + * pointer is now invalid + */ + // EXPECT_EQ(vector_len(fi), pool_size - 1); + + /* And the bitmap should also be correctly modified */ + fb = pool_get_free_bitmap (pool); + EXPECT_TRUE (bitmap_is_unset (fb, pool_size)); + + /* Check that surrounding values are also correct */ + EXPECT_TRUE (bitmap_is_unset (fb, pool_size - 1)); + EXPECT_TRUE (bitmap_is_set (fb, pool_size + 1)); + + /* Setting elements after should through */ + + /* Check that free indices and bitmaps are correctly updated */ + + pool_free (pool); +} + +TEST_F (PoolTest, PoolPut) +{ + pool_init (pool, DEFAULT_SIZE, 0); + + int *elt; + pool_get (pool, elt); + *elt = 10; + pool_put (pool, elt); + + pool_free (pool); +} + +TEST_F (PoolTest, PoolGetForceBitmapRealloc) +{ + const int N = 64; + int *elts[N]; + int *elt = NULL; + pool_init (pool, N, 0); + + for (int i = 0; i < N; i++) + pool_get (pool, elts[i]); + pool_get (pool, elt); + + pool_free (pool); +} + +TEST_F (PoolTest, PoolGetAfterReleasing) +{ + int *elt1 = NULL, *elt2 = NULL, *tmp = NULL; + pool_init (pool, DEFAULT_SIZE, 0); + + // If two elements are requested... + off_t id1 = pool_get (pool, elt1); + pool_get (pool, tmp); + + // ...and the first one is released... + pool_put (pool, elt1); + + // ...requesting a new one should return + // the first one (that was freed) + off_t id2 = pool_get (pool, elt2); + EXPECT_EQ (id1, id2); + EXPECT_EQ (elt1, elt2); + + pool_free (pool); +} + +TEST_F (PoolTest, PoolGetMultipleElementsAfterReleasing) +{ + const int N = 2; + int *elts[N]; + pool_init (pool, N, 0); + + for (int i = 0; i < N; i++) + pool_get (pool, elts[i]); + for (int i = 0; i < N; i++) + pool_put (pool, elts[i]); + for (int i = 0; i < N; i++) + pool_get (pool, elts[i]); + + pool_free (pool); +} diff --git a/lib/src/test/test_ring.cc b/lib/src/test/test_ring.cc new file mode 100644 index 000000000..f0b0371e8 --- /dev/null +++ b/lib/src/test/test_ring.cc @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <netinet/in.h> + +extern "C" +{ +#define WITH_TESTS +#include <hicn/util/ring.h> +} + +#define DEFAULT_SIZE 10UL + +class RingTest : public ::testing::Test +{ +protected: + RingTest () { ring_init (ring, DEFAULT_SIZE); } + virtual ~RingTest () { ring_free (ring); } + + int *ring = NULL; +}; + +/* TEST: Ring allocation and initialization */ +TEST_F (RingTest, RingAddOne) +{ + int val = -1; + /* Allocated size should be the next power of two */ + EXPECT_EQ (ring_get_size (ring), 0UL); + ring_add_value (ring, 1); + EXPECT_EQ (ring_get_size (ring), 1UL); + ring_get (ring, 0, &val); + EXPECT_EQ (val, 1); + EXPECT_EQ (ring_get_size (ring), 1UL); + ring_advance (ring, 1); + EXPECT_EQ (ring_get_size (ring), 0UL); +} + +TEST_F (RingTest, RingAddMany) +{ + size_t i = 0; + int val = -1; + size_t count = 0; + + /* Allocated size should be the next power of two */ + EXPECT_EQ (ring_get_size (ring), 0UL); + for (unsigned i = 0; i < DEFAULT_SIZE; i++) + ring_add_value (ring, i); + EXPECT_EQ (ring_get_size (ring), DEFAULT_SIZE); + + count = 0; + ring_enumerate_n (ring, i, &val, 1, { + EXPECT_EQ (val, (int) (i)); + count++; + }); + EXPECT_EQ (count, 1UL); + + count = 0; + ring_enumerate_n (ring, i, &val, DEFAULT_SIZE, { + EXPECT_EQ (val, (int) (i)); + count++; + }); + EXPECT_EQ (count, DEFAULT_SIZE); + + count = 0; + ring_enumerate_n (ring, i, &val, DEFAULT_SIZE + 1, { + EXPECT_EQ (val, (int) (i)); + count++; + }); + EXPECT_EQ (count, DEFAULT_SIZE); + + // Drop one + ring_add_value (ring, DEFAULT_SIZE); + EXPECT_EQ (ring_get_size (ring), DEFAULT_SIZE); + + count = 0; + ring_enumerate_n (ring, i, &val, DEFAULT_SIZE, { + EXPECT_EQ (val, (int) (i + 1)); // all values shoud be shifted + count++; + }); + EXPECT_EQ (count, DEFAULT_SIZE); + + ring_advance (ring, DEFAULT_SIZE); + EXPECT_EQ (ring_get_size (ring), 0UL); +} diff --git a/lib/src/test/test_slab.cc b/lib/src/test/test_slab.cc new file mode 100644 index 000000000..15deff3c1 --- /dev/null +++ b/lib/src/test/test_slab.cc @@ -0,0 +1,148 @@ +/* + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/util/slab.h> +} + +class SlabTest : public ::testing::Test +{ +protected: + SlabTest () {} + virtual ~SlabTest () { slab_free (slab); } + + slab_t *slab; +}; + +typedef struct +{ + int index; + int val; +} element_t; + +TEST_F (SlabTest, SlabCreateAndGetSingleChunk) +{ + slab = slab_create (element_t, 16); + ASSERT_NE (slab, nullptr); + + element_t *e = slab_get (element_t, slab); + ASSERT_NE (e, nullptr); + slab_put (slab, e); +} + +TEST_F (SlabTest, SlabGetChunks) +{ + slab = slab_create (element_t, 2); + + // Force creation of multiple blocks (since initial size is only 2) + for (int i = 0; i < 100; i++) + { + element_t *e = slab_get (element_t, slab); + EXPECT_NE (e, nullptr); + } +} + +TEST_F (SlabTest, SlabGetAndPutChunksNoResize) +{ + constexpr int NUM_ELEMENTS = 64; + element_t *elements[NUM_ELEMENTS]; + for (int i = 0; i < NUM_ELEMENTS; i++) + elements[i] = NULL; + + // Initial size=NUM_ELEMENTS, only one block will be created + slab = slab_create (element_t, NUM_ELEMENTS); + + for (int i = 0; i < NUM_ELEMENTS; i++) + { + elements[i] = slab_get (element_t, slab); + EXPECT_NE (elements[i], nullptr); + } + + // Release all chunks + for (int i = 0; i < NUM_ELEMENTS; i++) + slab_put (slab, elements[i]); +} + +TEST_F (SlabTest, SlabGetAndPutChunks) +{ + constexpr int NUM_ELEMENTS = 100; + element_t *elements[NUM_ELEMENTS]; + for (int i = 0; i < NUM_ELEMENTS; i++) + elements[i] = NULL; + + // Initial size=2 while NUM_ELEMENTS=100, to force creation of multiple + // blocks + slab = slab_create (sizeof (element_t), 2); + + for (int i = 0; i < NUM_ELEMENTS; i++) + { + elements[i] = slab_get (element_t, slab); + EXPECT_NE (elements[i], nullptr); + } + + // Release all chunks + for (int i = 0; i < NUM_ELEMENTS; i++) + slab_put (slab, elements[i]); +} + +TEST_F (SlabTest, SlabGetAndPutSomeChunks) +{ + slab = slab_create (element_t, 2); + + constexpr int NUM_ELEMENTS = 100; + element_t *elements[NUM_ELEMENTS]; + for (int i = 0; i < NUM_ELEMENTS; i++) + elements[i] = NULL; + + // Get chunks... + for (int i = 0; i < NUM_ELEMENTS; i++) + { + elements[i] = slab_get (element_t, slab); + EXPECT_NE (elements[i], nullptr); + + // ...and return only some of them + if (i % 5 == 0) + slab_put (slab, elements[i]); + } +} + +TEST_F (SlabTest, SlabGetSameChunkTwice) +{ + slab = slab_create (element_t, 1); + + // Get chunk and update it before returning it + element_t *e = slab_get (element_t, slab); + ASSERT_NE (e, nullptr); + element_t *prev = e; + e->index = 2; + e->val = 3; + slab_put (slab, e); + + // Get a chunk again: it should return the previous one + // without wiping its memory + e = slab_get (element_t, slab); + ASSERT_NE (e, nullptr); + EXPECT_EQ (e, prev); + EXPECT_EQ (e->index, 2); + EXPECT_EQ (e->val, 3); + + // Try to get an additional chunk: it should return a new chunk + // (different from previous one) + e = slab_get (element_t, slab); + EXPECT_NE (e, prev); +}
\ No newline at end of file diff --git a/lib/src/test/test_udp_header.cc b/lib/src/test/test_udp_header.cc new file mode 100644 index 000000000..2856c8ebf --- /dev/null +++ b/lib/src/test/test_udp_header.cc @@ -0,0 +1,360 @@ +/* + * 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/packet.h> + +#include "../protocol/ah.h" +#include "../protocol/ipv6.h" +#include "../protocol/udp.h" +#include "../protocol/new.h" +} + +class UdpHeaderTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + UdpHeaderTest (size_t hdr_size, hicn_packet_format_t format) + : buffer_ (new uint8_t[hdr_size]), hdr_size_ (hdr_size), + format_ (format), name_{}, name4_{}, name6_{} + { + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + UdpHeaderTest () + : UdpHeaderTest (NEW_HDRLEN + UDP_HDRLEN + IPV6_HDRLEN, + HICN_PACKET_FORMAT_IPV6_UDP) + { + } + + virtual ~UdpHeaderTest () { delete[] buffer_; } + + // checked everytime we build the packet... + void + checkCommon () + { + /* Initialize packet buffer headers */ + hicn_packet_set_format (&pkbuf_, format_); + hicn_packet_set_type (&pkbuf_, HICN_PACKET_TYPE_INTEREST); + hicn_packet_set_buffer (&pkbuf_, buffer_, hdr_size_, 0); + int rc = hicn_packet_init_header (&pkbuf_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + auto ip6_hdr = (_ipv6_header_t *) buffer_; + + // Check fields + EXPECT_EQ (ip6_hdr->saddr.as_u64[0], 0UL); + EXPECT_EQ (ip6_hdr->saddr.as_u64[1], 0UL); + EXPECT_EQ (ip6_hdr->daddr.as_u64[0], 0UL); + EXPECT_EQ (ip6_hdr->daddr.as_u64[1], 0UL); + EXPECT_EQ (ip6_hdr->nxt, IPPROTO_UDP); + + _udp_header_t *udp_hdr = (_udp_header_t *) (ip6_hdr + 1); + EXPECT_EQ (udp_hdr->src_port, 0UL); + EXPECT_EQ (udp_hdr->dst_port, 0UL); + EXPECT_EQ (udp_hdr->checksum, 0UL); + // EXPECT_EQ (ntohs (udp_hdr->length), NEW_HDRLEN + AH_HDRLEN); + + _new_header_t *new_hdr = (_new_header_t *) (udp_hdr + 1); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[0], 0UL); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[1], 0UL); + EXPECT_EQ (new_hdr->suffix, 0UL); + EXPECT_EQ (new_hdr->lifetime, 0UL); + EXPECT_EQ (new_hdr->path_label, 0UL); + EXPECT_EQ (new_hdr->payload_len, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + } + + virtual void + SetUp () override + { + checkCommon (); + } + + uint8_t *buffer_; + size_t hdr_size_; + hicn_packet_buffer_t pkbuf_; + hicn_packet_format_t format_; + hicn_name_t name_, name4_, name6_; + hicn_ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +class UdpHeaderAHTest : public UdpHeaderTest +{ +protected: + UdpHeaderAHTest () + : UdpHeaderTest (AH_HDRLEN + NEW_HDRLEN + UDP_HDRLEN + IPV6_HDRLEN, + HICN_PACKET_FORMAT_IPV6_UDP_AH) + { + } +}; + +/** + * Header Initialization + */ +TEST_F (UdpHeaderTest, GetFormat) +{ + hicn_packet_format_t format = hicn_packet_get_format (&pkbuf_); + EXPECT_EQ (format, HICN_PACKET_FORMAT_IPV6_UDP); +} + +TEST_F (UdpHeaderAHTest, GetFormat) +{ + // Get format from existing packet + hicn_packet_format_t format = hicn_packet_get_format (&pkbuf_); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HICN_PACKET_FORMAT_IPV6_UDP_AH); +} + +#if 0 + +// /** +// * @brief Checksum functions are not required, but we keep them for +// * compatibility. +// */ +// TEST_F (NewHeaderTest, Checksum) +// { +// // Get format from existing packet +// int rc = hicn_packet_compute_checksum (format_, header_); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_compute_header_checksum (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +// } + +// TEST_F (NewHeaderAHTest, Checksum) +// { +// // Get format from existing packet +// int rc = hicn_packet_compute_checksum (format_, header_); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_compute_header_checksum (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +// } + +TEST_F (UdpHeaderTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN); +} + +TEST_F (UdpHeaderAHTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (UdpHeaderTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN); +} + +TEST_F (UdpHeaderAHTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (UdpHeaderTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (UdpHeaderAHTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (UdpHeaderTest, SetGetName) +{ + // Get v6 name and set it to new_header + hicn_name_t name_ret; + + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_name (format_, header_, &name6_, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_name (format_, header_, &name_ret, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name6_, &name_ret, 1); + EXPECT_EQ (rc, 0); +} + +TEST_F (UdpHeaderTest, SetGetLocator) +{ + // This function does nothing but it is set for compatibility + hicn_ip_address_t locator; + memset (&locator, 0, sizeof (locator)); + locator.v6.as_u8[15] = 1; + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (UdpHeaderTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + // FixMe no error raised here + size_t signature_size = 128; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + (void) rc; + // EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size); + // EXPECT_NE (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (UdpHeaderAHTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + size_t signature_size = 128, signature_size_ret; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (signature_size, signature_size_ret); +} + +TEST_F (UdpHeaderTest, IsInterestIsData) +{ + // Mark packet as interest + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + int ret; + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 1); + + // Mark packet as data + rc = hicn_packet_set_data (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 0); +} + +TEST_F (UdpHeaderTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (UdpHeaderAHTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (UdpHeaderTest, SetGetPayloadType) +{ + // Lifetime + hicn_payload_type_t payload_type = HPT_MANIFEST, payload_type_ret; + int rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); + + payload_type = HPT_DATA; + + rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); +} +#endif diff --git a/lib/src/test/test_validation.cc b/lib/src/test/test_validation.cc new file mode 100644 index 000000000..091f26d65 --- /dev/null +++ b/lib/src/test/test_validation.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/validation.h> +} + +static constexpr int BUF_SIZE = 10; + +class ValidationTest : public ::testing::Test +{ +}; + +TEST_F (ValidationTest, SymbolicName) +{ + const char symbolic_name_correct[BUF_SIZE] = "conn0"; + const char symbolic_name_empty[BUF_SIZE] = ""; + const char symbolic_name_wrong[BUF_SIZE] = "1conn0"; + + EXPECT_TRUE (is_symbolic_name (symbolic_name_correct, BUF_SIZE)); + EXPECT_FALSE (is_symbolic_name (symbolic_name_empty, BUF_SIZE)); + EXPECT_FALSE (is_symbolic_name (symbolic_name_wrong, BUF_SIZE)); +} + +TEST_F (ValidationTest, Number) +{ + const char number_correct[BUF_SIZE] = "123"; + const char number_empty[BUF_SIZE] = ""; + const char number_wrong[BUF_SIZE] = "a123"; + const char number_wrong_2[BUF_SIZE] = "12T3"; + const char number_wrong_3[BUF_SIZE] = "a"; + const char number_wrong_negative[BUF_SIZE] = "-123"; + + EXPECT_TRUE (is_number (number_correct, BUF_SIZE)); + EXPECT_FALSE (is_number (number_empty, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_2, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_3, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_negative, BUF_SIZE)); +}
\ No newline at end of file diff --git a/lib/src/test/test_vector.cc b/lib/src/test/test_vector.cc new file mode 100644 index 000000000..88b0bb7cf --- /dev/null +++ b/lib/src/test/test_vector.cc @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/util/vector.h> +} + +static constexpr size_t DEFAULT_SIZE = 10; +static constexpr size_t N_ELEMENTS = 5; + +class VectorTest : public ::testing::Test +{ +protected: + VectorTest () { vector_init (vector, DEFAULT_SIZE, 0); } + virtual ~VectorTest () { vector_free (vector); } + + int *vector = NULL; +}; + +TEST_F (VectorTest, VectorAllocateAndResize) +{ + // Allocated size should be the next power of two + EXPECT_EQ (vector_get_alloc_size (vector), 16UL); + + // Setting elements within the allocated size should not trigger a resize + vector_ensure_pos (vector, 15); + EXPECT_EQ (vector_get_alloc_size (vector), 16UL); + + // Setting elements after should through + vector_ensure_pos (vector, 16); + EXPECT_EQ (vector_get_alloc_size (vector), 32UL); +} + +TEST_F (VectorTest, VectorSize) +{ + EXPECT_EQ (vector_len (vector), size_t (0)); + + // Check size after pushing one element + vector_push (vector, 1); + EXPECT_EQ (vector_len (vector), size_t (1)); + + // Check size after pushing additional elements + vector_push (vector, 2); + vector_push (vector, 3); + EXPECT_EQ (vector_len (vector), size_t (3)); + + // Try adding multiple elements + const int n_elements_to_add = 5; + size_t expected_new_len = vector_len (vector) + n_elements_to_add; + for (int i = 0; i < n_elements_to_add; i++) + vector_push (vector, i); + EXPECT_EQ (vector_len (vector), expected_new_len); +} + +TEST_F (VectorTest, VectorCheckValue) +{ + // Add elements + vector_push (vector, 109); + vector_push (vector, 200); + EXPECT_EQ (vector_at (vector, 0), 109); + EXPECT_EQ (vector_at (vector, 1), 200); + + // Update element + vector_set (vector, 1, 400); + EXPECT_EQ (vector_at (vector, 1), 400); + + // Add at last available position + size_t prev_size = vector_len (vector); + vector_set (vector, vector_len (vector) - 1, 123); + EXPECT_EQ (vector_at (vector, vector_len (vector) - 1), 123); + EXPECT_EQ (prev_size, vector_len (vector)) << "Size should not have changed"; +} + +TEST_F (VectorTest, RemoveElement) +{ + // Populate vector + for (size_t i = 0; i < N_ELEMENTS; i++) + vector_push (vector, i); + EXPECT_EQ (vector_len (vector), N_ELEMENTS); + for (size_t i = 0; i < vector_len (vector); i++) + EXPECT_EQ (vector_at (vector, i), (int) i); + + // Remove element + int value_to_remove = 3; + int num_removed = vector_remove_unordered (vector, value_to_remove); + + EXPECT_EQ (vector_len (vector), N_ELEMENTS - 1); + EXPECT_EQ (num_removed, 1); + for (size_t i = 0; i < vector_len (vector); i++) + EXPECT_NE (vector_at (vector, i), value_to_remove); +} + +TEST_F (VectorTest, RemoveNonExistingElement) +{ + // Push some initial values + vector_push (vector, 1); + vector_push (vector, 2); + vector_push (vector, 3); + EXPECT_EQ (vector_len (vector), size_t (3)); + + // Remove non-existing element + int num_removed = vector_remove_unordered (vector, 5); + EXPECT_EQ (num_removed, 0); + size_t prev_size = vector_len (vector); + EXPECT_EQ (prev_size, vector_len (vector)) << "Size should not have changed"; +} + +TEST_F (VectorTest, RemoveDuplicatedElement) +{ + // Populate vector + for (size_t i = 0; i < N_ELEMENTS; i++) + vector_push (vector, i); + EXPECT_EQ (vector_len (vector), N_ELEMENTS); + for (size_t i = 0; i < vector_len (vector); i++) + EXPECT_EQ (vector_at (vector, i), (int) i); + vector_set (vector, 0, 3); // Duplicate element + + // Remove (duplicated) elements + int value_to_remove = 3; + int num_removed = vector_remove_unordered (vector, value_to_remove); + + EXPECT_EQ (vector_len (vector), N_ELEMENTS - 2); + EXPECT_EQ (num_removed, 2); + for (size_t i = 0; i < vector_len (vector); i++) + EXPECT_NE (vector_at (vector, i), value_to_remove); +} + +TEST_F (VectorTest, Iterate) +{ + for (size_t i = 0; i < N_ELEMENTS; i++) + vector_push (vector, i); + + int count = 0; + int *elem; + vector_foreach (vector, elem, { EXPECT_EQ (*elem, count++); }); +} + +TEST_F (VectorTest, MultipleResize) +{ + // Use small vector (size=1) to force multiple realloc operations + int *small_vector; + vector_init (small_vector, 1, 0); + + for (size_t i = 0; i < N_ELEMENTS; i++) + vector_push (small_vector, i); + + for (size_t i = 0; i < N_ELEMENTS; i++) + EXPECT_EQ (vector_at (small_vector, i), (int) i); + + EXPECT_EQ (vector_len (small_vector), 5UL); + EXPECT_EQ (vector_get_alloc_size (small_vector), 8UL); + + vector_free (small_vector); +} + +TEST_F (VectorTest, MaxSize) +{ + const int max_size = 4; + + // Fill the vector until max size is reached + int *small_vector; + vector_init (small_vector, 2, max_size); + for (int i = 0; i < max_size; i++) + vector_push (small_vector, i); + + // Try expanding or appending elements should fail + int rc = vector_ensure_pos (small_vector, max_size); + EXPECT_EQ (rc, -1); + rc = vector_push (small_vector, 123); + EXPECT_EQ (rc, -1); + + vector_free (small_vector); +} + +TEST_F (VectorTest, Contains) +{ + // No elements + EXPECT_EQ (vector_contains (vector, 1), false); + + // Push one element + vector_push (vector, 1); + EXPECT_EQ (vector_contains (vector, 1), true); + + // Update element + vector_set (vector, 0, 2); + EXPECT_EQ (vector_contains (vector, 1), false); + EXPECT_EQ (vector_contains (vector, 2), true); +} + +TEST_F (VectorTest, Remove) +{ + // Remove element at invalid position + int rc = vector_remove_at (vector, 2); + EXPECT_EQ (rc, -1); // Failure + + // Push two elements and remove the second one + vector_push (vector, 1); + vector_push (vector, 2); + rc = vector_remove_at (vector, 1); + EXPECT_EQ (rc, 0); // Success + EXPECT_EQ (vector_len (vector), size_t (1)); + + // Push another element: it should replace the previous one + vector_push (vector, 3); + EXPECT_EQ (vector_len (vector), size_t (2)); + EXPECT_EQ (vector_at (vector, 1), 3); +} + +TEST_F (VectorTest, RemoveInTheMiddle) +{ + for (size_t i = 0; i < N_ELEMENTS; i++) + vector_push (vector, i); + + // Remove element in central position + int rc = vector_remove_at (vector, 2); + EXPECT_EQ (rc, 0); // Success + EXPECT_EQ (vector_contains (vector, 2), false); + EXPECT_EQ (vector_len (vector), N_ELEMENTS - 1); + + // Check if elements have been shifted (preserving the order) + int expected[] = { 0, 1, 3, 4 }; + for (size_t i = 0; i < vector_len (vector); i++) + EXPECT_EQ (vector_at (vector, i), expected[i]); +} + +TEST_F (VectorTest, Reset) +{ + vector_push (vector, 1); + vector_push (vector, 2); + EXPECT_EQ (vector_len (vector), size_t (2)); + + vector_reset (vector); + EXPECT_EQ (vector_len (vector), size_t (0)); + + vector_push (vector, 5); + EXPECT_EQ (vector_len (vector), size_t (1)); + EXPECT_EQ (vector_contains (vector, 5), true); + EXPECT_EQ (vector_at (vector, 0), 5); +}
\ No newline at end of file diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c index 49916547d..b50864c96 100644 --- a/lib/src/util/ip_address.c +++ b/lib/src/util/ip_address.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -20,44 +20,58 @@ #include <hicn/util/ip_address.h> #include <hicn/util/log.h> +#include <hicn/util/sstrncpy.h> +#include <hicn/common.h> #if __BYTE_ORDER == __LITTLE_ENDIAN #ifdef __ANDROID__ -#define SWAP(x) bswap_32(x) +#define SWAP(x) bswap_32 (x) #else -#define SWAP(x) __bswap_constant_32(x) +#define SWAP(x) __bswap_constant_32 (x) #endif #else #define SWAP(x) x #endif - /* No htonl() with const */ -const ip_address_t IPV4_LOOPBACK = { - .v4.as_inaddr.s_addr = SWAP(INADDR_LOOPBACK), +const hicn_ip_address_t IPV4_LOOPBACK = { + .v4.as_inaddr.s_addr = SWAP (INADDR_LOOPBACK), +}; + +const hicn_ip_address_t IPV6_LOOPBACK = { + .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT, }; -const ip_address_t IPV6_LOOPBACK ={ - .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT, +const hicn_ip_address_t IPV4_ANY = { + .v4.as_inaddr.s_addr = INADDR_ANY, }; -const ip_address_t IPV4_ANY = { - .v4.as_inaddr.s_addr = INADDR_ANY, +const hicn_ip_address_t IPV6_ANY = { + .v6.as_in6addr = IN6ADDR_ANY_INIT, }; -const ip_address_t IPV6_ANY = { - .v6.as_in6addr = IN6ADDR_ANY_INIT, +const ipv4_address_t IP4_ADDRESS_EMPTY = { + .as_u32 = 0, }; -const ip_address_t IP_ADDRESS_EMPTY = { - .v6.as_u64 = { 0 }, +const ipv6_address_t IP6_ADDRESS_EMPTY = { + .as_u64 = { 0, 0 }, }; +const hicn_ip_address_t IP_ADDRESS_EMPTY = { + .v6.as_u64 = { 0, 0 }, +}; /* IP address */ int -ip_address_get_family (const char * ip_address) +hicn_ip_address_get_family (const hicn_ip_address_t *address) +{ + return hicn_ip_address_is_v4 (address) ? AF_INET : AF_INET6; +} + +int +hicn_ip_address_str_get_family (const char *ip_address) { struct addrinfo hint, *res = NULL; int rc; @@ -78,27 +92,41 @@ ip_address_get_family (const char * ip_address) } int -ip_address_len (int family) +hicn_ip_address_len (int family) { return (family == AF_INET6) ? IPV6_ADDR_LEN : - (family == AF_INET) ? IPV4_ADDR_LEN : 0; + (family == AF_INET) ? IPV4_ADDR_LEN : + 0; } int -ip_address_ntop (const ip_address_t * ip_address, char *dst, const size_t len, - int family) +hicn_ip_address_get_len (const hicn_ip_address_t *ip_address) { - const char * s; - switch(family) { + return hicn_ip_address_len (hicn_ip_address_get_family (ip_address)); +} + +int +hicn_ip_address_get_len_bits (const hicn_ip_address_t *ip_address) +{ + return bytes_to_bits (hicn_ip_address_get_len (ip_address)); +} + +int +hicn_ip_address_ntop (const hicn_ip_address_t *ip_address, char *dst, + const size_t len, int family) +{ + const char *s; + switch (family) + { case AF_INET: - s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, (socklen_t)len); + s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, (socklen_t) len); break; case AF_INET6: - s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, (socklen_t)len); + s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, (socklen_t) len); break; default: return -1; - } + } return (s ? 1 : -1); } @@ -106,22 +134,26 @@ ip_address_ntop (const ip_address_t * ip_address, char *dst, const size_t len, * Parse ip addresses in presentation format */ int -ip_address_pton (const char *ip_address_str, ip_address_t * ip_address) +hicn_ip_address_pton (const char *hicn_ip_address_str, + hicn_ip_address_t *ip_address) { int pton_fd; int family; - family = ip_address_get_family (ip_address_str); + family = hicn_ip_address_str_get_family (hicn_ip_address_str); - switch (family) { + switch (family) + { case AF_INET: ip_address->pad[0] = 0; ip_address->pad[1] = 0; ip_address->pad[2] = 0; - pton_fd = inet_pton (AF_INET, ip_address_str, &ip_address->v4.buffer); + pton_fd = + inet_pton (AF_INET, hicn_ip_address_str, &ip_address->v4.buffer); break; case AF_INET6: - pton_fd = inet_pton (AF_INET6, ip_address_str, &ip_address->v6.buffer); + pton_fd = + inet_pton (AF_INET6, hicn_ip_address_str, &ip_address->v6.buffer); break; default: return -1; @@ -132,36 +164,38 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address) if (pton_fd <= 0) return -1; - return 1; + return 0; } int -ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family) +hicn_ip_address_snprintf (char *s, size_t size, + const hicn_ip_address_t *ip_address) { - - const char * rc; - switch(family) { - case AF_INET: - if (size < INET_ADDRSTRLEN) - return -1; - rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN); - break; - case AF_INET6: - if (size < INET6_ADDRSTRLEN) - return -1; - rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN); - break; - default: - return -1; + const char *rc; + int family = hicn_ip_address_get_family (ip_address); + switch (family) + { + case AF_INET: + if (size < INET_ADDRSTRLEN) + return -1; + rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN); + break; + case AF_INET6: + if (size < INET6_ADDRSTRLEN) + return -1; + rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN); + break; + default: + return -1; } - if (!rc) - return -1; - return (int)strlen(s); + if (!rc) + return -1; + return (int) strnlen_s (s, MAXSZ_IP_ADDRESS); } int -ip_address_to_sockaddr(const ip_address_t * ip_address, - struct sockaddr *sa, int family) +hicn_ip_address_to_sockaddr (const hicn_ip_address_t *ip_address, + struct sockaddr *sa, int family) { struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) sa; struct sockaddr_in *tmp4 = (struct sockaddr_in *) sa; @@ -187,64 +221,72 @@ ip_address_to_sockaddr(const ip_address_t * ip_address, } int -ip_address_cmp(const ip_address_t * ip1, const ip_address_t * ip2, int family) +hicn_ip_address_cmp (const hicn_ip_address_t *ip1, + const hicn_ip_address_t *ip2) { - switch(family) { - case AF_INET: - return memcmp(&ip1->v4, &ip2->v4, sizeof(ip1->v4)); - break; - case AF_INET6: - return memcmp(&ip1->v6, &ip2->v6, sizeof(ip1->v6)); - break; - default: - return memcmp(ip1, ip2, sizeof(ip_address_t)); - } + /* This works as soon as all members are initialized */ + return memcmp (ip1, ip2, sizeof (hicn_ip_address_t)); } -int -ip_address_empty(const ip_address_t * ip) +bool +hicn_ip_address_equals (const hicn_ip_address_t *ip1, + const hicn_ip_address_t *ip2) { - return (memcmp(ip, &IP_ADDRESS_EMPTY, sizeof(IP_ADDRESS_EMPTY)) == 0); + return hicn_ip_address_cmp (ip1, ip2) == 0; } +int +hicn_ip_address_empty (const hicn_ip_address_t *ip) +{ + return (memcmp (ip, &IP_ADDRESS_EMPTY, sizeof (hicn_ip_address_t)) == 0); +} +void +hicn_ip_address_clear (hicn_ip_address_t *address) +{ + memset (address, 0, sizeof (hicn_ip_address_t)); +} /* Prefix */ /* Parse IP Prefixes in presentation format (in bits, separated by a slash) */ int -ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix) +hicn_ip_prefix_pton (const char *hicn_ip_address_str, + hicn_ip_prefix_t *hicn_ip_prefix) { int pton_fd; char *p; char *eptr; - char *addr = strdup (ip_address_str); + char *addr = strdup (hicn_ip_address_str); p = strchr (addr, '/'); - if (!p) { - ip_prefix->len = ~0; // until we get the ip address family - } else { - ip_prefix->len = (u8)strtoul (p + 1, &eptr, 10); - *p = 0; - } + if (!p) + { + hicn_ip_prefix->len = ~0; // until we get the ip address family + } + else + { + hicn_ip_prefix->len = (u8) strtoul (p + 1, &eptr, 10); + *p = 0; + } - ip_prefix->family = ip_address_get_family (addr); + hicn_ip_prefix->family = hicn_ip_address_str_get_family (addr); - switch (ip_prefix->family) + switch (hicn_ip_prefix->family) { case AF_INET6: - if (ip_prefix->len == (u8)~0) - ip_prefix->len = IPV6_ADDR_LEN_BITS; - if (ip_prefix->len > IPV6_ADDR_LEN_BITS) - goto ERR; - pton_fd = inet_pton (AF_INET6, addr, &ip_prefix->address.v6.buffer); + if (hicn_ip_prefix->len == (u8) ~0) + hicn_ip_prefix->len = IPV6_ADDR_LEN_BITS; + if (hicn_ip_prefix->len > IPV6_ADDR_LEN_BITS) + goto ERR; + pton_fd = inet_pton (AF_INET6, addr, &hicn_ip_prefix->address.v6.buffer); break; case AF_INET: - if (ip_prefix->len == (u8)~0) - ip_prefix->len = IPV4_ADDR_LEN_BITS; - if (ip_prefix->len > IPV4_ADDR_LEN_BITS) - goto ERR; - pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.v4.buffer); + if (hicn_ip_prefix->len == (u8) ~0) + hicn_ip_prefix->len = IPV4_ADDR_LEN_BITS; + if (hicn_ip_prefix->len > IPV4_ADDR_LEN_BITS) + goto ERR; + pton_fd = inet_pton (AF_INET, addr, &hicn_ip_prefix->address.v4.buffer); break; default: goto ERR; @@ -253,139 +295,180 @@ ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix) // 0 = not in presentation format // < 0 = other error (use perror) if (pton_fd <= 0) - goto ERR; + goto ERR; - free(addr); - return 1; + free (addr); + return 0; ERR: free (addr); return -1; } int -ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size) +hicn_ip_prefix_ntop_short (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst, + size_t size) { char ip_s[MAXSZ_IP_ADDRESS]; - const char * s; - switch(ip_prefix->family) { + const char *s; + switch (hicn_ip_prefix->family) + { case AF_INET: - s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS); + s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s, + MAXSZ_IP_ADDRESS); break; case AF_INET6: - s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS); + s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s, + MAXSZ_IP_ADDRESS); break; default: return -1; - } + } if (!s) - return -1; - int rc = snprintf(dst, size, "%s", ip_s); + return -1; + int rc = snprintf (dst, size, "%s", ip_s); if (rc >= size) - return (int)size; + return (int) size; return rc; } int -ip_prefix_ntop(const ip_prefix_t * ip_prefix, char *dst, size_t size) +hicn_ip_prefix_ntop (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst, + size_t size) { char ip_s[MAXSZ_IP_ADDRESS]; - const char * s; - switch(ip_prefix->family) { + const char *s; + switch (hicn_ip_prefix->family) + { case AF_INET: - s = inet_ntop (AF_INET, ip_prefix->address.v4.buffer, ip_s, MAXSZ_IP_ADDRESS); + s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s, + MAXSZ_IP_ADDRESS); break; case AF_INET6: - s = inet_ntop (AF_INET6, ip_prefix->address.v6.buffer, ip_s, MAXSZ_IP_ADDRESS); + s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s, + MAXSZ_IP_ADDRESS); break; default: return -1; - } + } if (!s) - return -1; - int rc = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len); + return -1; + int rc = snprintf (dst, size, "%s/%d", ip_s, hicn_ip_prefix->len); if (rc >= size) - return (int)size; + return (int) size; return rc; } int -ip_prefix_len (const ip_prefix_t * prefix) +hicn_ip_prefix_snprintf (char *s, size_t size, const hicn_ip_prefix_t *prefix) +{ + return hicn_ip_prefix_ntop (prefix, s, size); +} + +int +hicn_ip_prefix_len (const hicn_ip_prefix_t *prefix) { - return prefix->len; // ip_address_len(&prefix->address, prefix->family); + return prefix->len; // hicn_ip_address_len(&prefix->address, prefix->family); } const u8 * -ip_address_get_buffer(const ip_address_t * ip_address, int family) +hicn_ip_address_get_buffer (const hicn_ip_address_t *ip_address, int family) { - switch(family) { + switch (family) + { case AF_INET: return ip_address->v4.buffer; case AF_INET6: return ip_address->v6.buffer; default: return NULL; - } + } } bool -ip_prefix_empty (const ip_prefix_t * prefix) +hicn_ip_prefix_empty (const hicn_ip_prefix_t *prefix) { return prefix->len == 0; } int -ip_prefix_to_sockaddr(const ip_prefix_t * prefix, - struct sockaddr *sa) +hicn_ip_prefix_to_sockaddr (const hicn_ip_prefix_t *prefix, + struct sockaddr *sa) { - // XXX assert len == ip_address_len - return ip_address_to_sockaddr(&prefix->address, sa, prefix->family); + // XXX assert len == hicn_ip_address_len + return hicn_ip_address_to_sockaddr (&prefix->address, sa, prefix->family); } int -ip_prefix_cmp(const ip_prefix_t * prefix1, const ip_prefix_t * prefix2) +hicn_ip_prefix_cmp (const hicn_ip_prefix_t *prefix1, + const hicn_ip_prefix_t *prefix2) +{ + if (prefix1->family < prefix2->family) + return -1; + else if (prefix1->family > prefix2->family) + return 1; + + if (prefix1->len < prefix2->len) + return -1; + else if (prefix1->len > prefix2->len) + return 1; + + return hicn_ip_address_cmp (&prefix1->address, &prefix2->address); +} + +/* Network byte order + host bit order */ + +uint8_t +hicn_ip_address_get_bit (const hicn_ip_address_t *address, uint8_t pos) { - if (prefix1->family < prefix2->family) - return -1; - else if (prefix1->family > prefix2->family) - return 1; + return (address->v6.as_u8[pos / 8] >> (7 - pos % 8)) & 0x1; +} - if (prefix1->len < prefix2->len) - return -1; - else if (prefix1->len > prefix2->len) - return 1; +bool +hicn_ip_address_match_family (const hicn_ip_address_t *address, int family) +{ + return hicn_ip_address_get_family (address) == family; +} - return ip_address_cmp(&prefix1->address, &prefix2->address, prefix1->family); +uint32_t +hicn_ip_address_get_hash (const hicn_ip_address_t *address) +{ + return hash32 (address, sizeof (address)); } /* URL */ #define MAXSZ_PROTO_ 8 /* inetX:// */ -#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM +#define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM #define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_ #define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_ -#define MAXSZ_URL_ MAXSZ_URL6_ -#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM -#define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM -#define MAXSZ_URL MAXSZ_URL_ + NULLTERM +#define MAXSZ_URL_ MAXSZ_URL6_ +#define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM +#define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM +#define MAXSZ_URL MAXSZ_URL_ + NULLTERM int -url_snprintf(char * s, size_t size, int family, - const ip_address_t * ip_address, u16 port) +url_snprintf (char *s, size_t size, const hicn_ip_address_t *ip_address, + u16 port) { - char ip_address_s[MAXSZ_IP_ADDRESS]; - int rc; + char hicn_ip_address_s[MAXSZ_IP_ADDRESS]; + int rc; - /* Other address are currently not supported */ - if (!IS_VALID_FAMILY(family)) - return -1; + int family = hicn_ip_address_get_family (ip_address); - rc = ip_address_snprintf(ip_address_s, MAXSZ_IP_ADDRESS, ip_address, family); - if (rc >= MAXSZ_IP_ADDRESS) - WARN("[url_snprintf] Unexpected ip_address truncation"); - if (rc < 0) - return rc; + /* Other address are currently not supported */ + if (!IS_VALID_FAMILY (family)) + return -1; - return snprintf(s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6', - ip_address_s, port); + if (!hicn_ip_address_match_family (ip_address, family)) + return -1; + rc = + hicn_ip_address_snprintf (hicn_ip_address_s, MAXSZ_IP_ADDRESS, ip_address); + if (rc >= MAXSZ_IP_ADDRESS) + WARN ("[url_snprintf] Unexpected ip_address truncation"); + if (rc < 0) + return rc; + + return snprintf (s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6', + hicn_ip_address_s, port); } diff --git a/lib/src/util/log.c b/lib/src/util/log.c index c1fc999ad..91a87e848 100644 --- a/lib/src/util/log.c +++ b/lib/src/util/log.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -18,6 +18,7 @@ #include <stdarg.h> #include <stdlib.h> #include <stdio.h> +#include <strings.h> #ifdef __ANDROID__ #include <android/log.h> @@ -25,154 +26,184 @@ log_conf_t log_conf = DEFAULT_LOG_CONF; -#define FMT_DATETIME "%02d-%02d-%04d %02d:%02d:%02d" -#define FMT_DATETIME_LEN 20 -#define snprintf_nowarn(...) (snprintf(__VA_ARGS__) < 0 ? abort() : (void)0) +#define FMT_DATETIME "%02d-%02d-%04d %02d:%02d:%02d" +#define FMT_DATETIME_LEN 20 +#define snprintf_nowarn(...) (snprintf (__VA_ARGS__) < 0 ? abort () : (void) 0) +#define COLOR_RED "\033[31m" +#define COLOR_YELLOW "\033[33m" +#define COLOR_RESET "\033[0m" static char ts[FMT_DATETIME_LEN]; -static char *timestamp(void) +static char * +timestamp (void) { - time_t tv; - struct tm *tm; + time_t tv; + struct tm *tm; - time(&tv); - tm = localtime(&tv); + time (&tv); + tm = localtime (&tv); - snprintf_nowarn(ts, FMT_DATETIME_LEN, FMT_DATETIME, tm->tm_mday, - tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, - tm->tm_sec); - return ts; + snprintf_nowarn (ts, FMT_DATETIME_LEN, FMT_DATETIME, tm->tm_mday, + tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, + tm->tm_sec); + return ts; } -void _log_va(int level, const char *fmt, va_list ap) -{ +void +_log_va (int level, const char *fmt, va_list ap) +{ #if 0 if (!conf.log_system) return; #endif - char *prefix; + char *prefix; #ifdef __ANDROID__ - int prio = -1; - if (level > log_conf.log_level) - return; - - switch (level) { - case LOG_FATAL: - prio = ANDROID_LOG_FATAL; - prefix = "FATAL: "; - break; - case LOG_ERROR: - prio = ANDROID_LOG_ERROR; - prefix = "ERROR: "; - break; - case LOG_WARN: - prio = ANDROID_LOG_WARN; - prefix = "WARNING: "; - break; - case LOG_INFO: - prio = ANDROID_LOG_INFO; - prefix = ""; - break; - case LOG_DEBUG: - prio = ANDROID_LOG_DEBUG; - prefix = "DEBUG: "; - break; - case LOG_TRACE: - prio = ANDROID_LOG_DEBUG; - prefix = "TRACE: "; - break; - default: - prio = ANDROID_LOG_INFO; - prefix = ""; - break; - } - - if (log_conf.log_file) { - FILE *f = log_conf.log_file; - fprintf(f, "%s %s", timestamp(), prefix); - vfprintf(f, fmt, ap); - fprintf(f, "\n"); - } else { - __android_log_vprint(ANDROID_LOG_INFO, "HICN FACEMGR", fmt, ap); - } + int prio = -1; + if (level > log_conf.log_level) + return; + + switch (level) + { + case LOG_FATAL: + prio = ANDROID_LOG_FATAL; + prefix = "FATAL: "; + break; + case LOG_ERROR: + prio = ANDROID_LOG_ERROR; + prefix = "ERROR: "; + break; + case LOG_WARN: + prio = ANDROID_LOG_WARN; + prefix = "WARNING: "; + break; + case LOG_INFO: + prio = ANDROID_LOG_INFO; + prefix = ""; + break; + case LOG_DEBUG: + prio = ANDROID_LOG_DEBUG; + prefix = "DEBUG: "; + break; + case LOG_TRACE: + prio = ANDROID_LOG_DEBUG; + prefix = "TRACE: "; + break; + default: + prio = ANDROID_LOG_INFO; + prefix = ""; + break; + } + + if (log_conf.log_file) + { + FILE *f = log_conf.log_file; + fprintf (f, "%s %s", timestamp (), prefix); + vfprintf (f, fmt, ap); + fprintf (f, "\n"); + } + else + { + __android_log_vprint (prio, "HICN FACEMGR", fmt, ap); + } #else - if (level > log_conf.log_level) - return; - - switch (level) { - case LOG_FATAL: - prefix = "FATAL: "; - break; - case LOG_ERROR: - prefix = "ERROR: "; - break; - case LOG_WARN: - prefix = "WARNING: "; - break; - case LOG_INFO: - prefix = ""; - break; - case LOG_DEBUG: - prefix = "DEBUG: "; - break; - case LOG_TRACE: - prefix = "TRACE: "; - break; - default: - prefix = ""; - break; - } - FILE *f = log_conf.log_file ? log_conf.log_file : stdout; - fprintf(f, "%s %s", timestamp(), prefix); - vfprintf(f, fmt, ap); - fprintf(f, "\n"); + if (level > log_conf.log_level) + return; + + char *color = COLOR_RESET; + switch (level) + { + case LOG_FATAL: + prefix = "FATAL: "; + break; + case LOG_ERROR: + prefix = "ERROR: "; + color = COLOR_RED; + break; + case LOG_WARN: + prefix = "WARNING: "; + color = COLOR_YELLOW; + break; + case LOG_INFO: + prefix = ""; + break; + case LOG_DEBUG: + prefix = "DEBUG: "; + break; + case LOG_TRACE: + prefix = "TRACE: "; + break; + default: + prefix = ""; + break; + } + FILE *f = log_conf.log_file ? log_conf.log_file : stdout; + fprintf (f, "%s%s %s", color, timestamp (), prefix); + vfprintf (f, fmt, ap); + fprintf (f, "%s\n", COLOR_RESET); #ifdef DEBUG - fflush(f); + fflush (f); #endif #endif } -void _log(int level, const char *fmt, ...) +void +_log (int level, const char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - _log_va(level, fmt, ap); - va_end(ap); + va_start (ap, fmt); + _log_va (level, fmt, ap); + va_end (ap); } #ifdef HAVE_BACKTRACE #include <execinfo.h> -void print_trace(void) +void +print_trace (void) { - void *array[32]; - size_t size; + void *array[32]; + size_t size; - size = backtrace(array, 32); - fflush(conf.log_file); - backtrace_symbols_fd(array, size, fileno(conf.log_file)); + size = backtrace (array, 32); + fflush (conf.log_file); + backtrace_symbols_fd (array, size, fileno (conf.log_file)); } #endif -void fatal(char *fmt, ...) +void +fatal (char *fmt, ...) { - va_list ap; + va_list ap; - va_start(ap, fmt); - _log_va(LOG_FATAL, fmt, ap); - va_end(ap); + va_start (ap, fmt); + _log_va (LOG_FATAL, fmt, ap); + va_end (ap); #ifdef HAVE_BACKTRACE - print_trace(); + print_trace (); #endif - exit(200); + exit (200); } + +const char *loglevel_str[] = { "fatal", "error", "warn", + "info", "debug", "trace" }; + +int +loglevel_from_str (const char *loglevel) +{ + for (int i = 0; i <= LOG_TRACE; i++) + { + if (strcasecmp (loglevel, loglevel_str[i]) == 0) + return i; + } + return -1; +}
\ No newline at end of file diff --git a/lib/src/util/pool.c b/lib/src/util/pool.c new file mode 100644 index 000000000..2c1e90b5e --- /dev/null +++ b/lib/src/util/pool.c @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file pool.c + * \brief Implementation of fixed-size pool allocator. + * + * NOTE: + * - Ideally, we should have a single realloc per resize, that would encompass + * both the free indices vector and bitmap, by nesting data structures. + * Because of the added complexity, and by lack of evidence of the need for + * this, we currently rely on a simpler implementation. + */ + +#include <assert.h> +#include <stdlib.h> // calloc + +#include <hicn/util/pool.h> +#include <hicn/util/log.h> + +#include <stdio.h> // XXX + +void +_pool_init (void **pool_ptr, size_t elt_size, size_t init_size, + size_t max_size) +{ + assert (pool_ptr); + assert (elt_size); + + init_size = next_pow2 (init_size); + + if (max_size && init_size > max_size) + goto ERR_MAX_SIZE; + + /* The initial pool size is rounded to the next power of two */ + size_t alloc_size = next_pow2 (init_size); + + pool_hdr_t *ph = calloc (POOL_HDRLEN + alloc_size * elt_size, 1); + if (!ph) + goto ERR_MALLOC; + + ph->elt_size = elt_size; + ph->alloc_size = alloc_size; + ph->max_size = max_size; + + /* Free indices */ + off_t *free_indices; + vector_init (free_indices, init_size, max_size); + for (unsigned i = 0; i < init_size; i++) + free_indices[i] = (init_size - 1) - i; + vector_len (free_indices) = init_size; + ph->free_indices = free_indices; + + /* Free bitmap */ + bitmap_t *fb = ph->free_bitmap; + bitmap_init (fb, init_size, max_size); + bitmap_set_to (fb, init_size); + ph->free_bitmap = fb; + + *pool_ptr = (uint8_t *) ph + POOL_HDRLEN; + + return; + +ERR_MALLOC: +ERR_MAX_SIZE: + *pool_ptr = NULL; + return; +} + +void +_pool_free (void **pool_ptr) +{ + pool_hdr_t *ph = pool_hdr (*pool_ptr); + vector_free (ph->free_indices); + bitmap_free (ph->free_bitmap); + + free (pool_hdr (*pool_ptr)); + *pool_ptr = NULL; +} + +bool +_pool_validate_id (void **pool_ptr, off_t id) +{ + pool_hdr_t *ph = pool_hdr (*pool_ptr); + size_t pool_size = pool_get_alloc_size (*pool_ptr); + if (id >= pool_size || !bitmap_is_unset (ph->free_bitmap, id)) + return false; + + return true; +} + +void +_pool_resize (void **pool_ptr, size_t elt_size) +{ + pool_hdr_t *ph = pool_hdr (*pool_ptr); + size_t old_size = ph->alloc_size; + size_t new_size = old_size * 2; + + WARN ("pool_resize to %lu", new_size); + + if (ph->max_size && new_size > ph->max_size) + goto ERR_MAX_SIZE; + + /* Double pool storage */ + ph = realloc (ph, POOL_HDRLEN + new_size * elt_size); + if (!ph) + goto ERR_REALLOC; + ph->elt_size = elt_size; + ph->alloc_size = new_size; + + /* + * After resize, the pool will have new free indices, ranging from + * old_size to (new_size - 1) + */ + vector_ensure_pos (ph->free_indices, old_size); + for (unsigned i = 0; i < old_size; i++) + ph->free_indices[i] = new_size - 1 - i; + vector_len (ph->free_indices) = old_size; + + /* We also need to update the bitmap */ + bitmap_ensure_pos (&(ph->free_bitmap), new_size - 1); + bitmap_set_range (ph->free_bitmap, old_size, new_size - 1); + + /* Reassign pool pointer */ + *pool_ptr = (uint8_t *) ph + POOL_HDRLEN; + + return; + +ERR_REALLOC: +ERR_MAX_SIZE: + *pool_ptr = NULL; + return; +} + +off_t +_pool_get (void **pool_ptr, void **elt, size_t elt_size) +{ + pool_hdr_t *ph = pool_hdr (*pool_ptr); + uint64_t l = vector_len (ph->free_indices); + if (l == 0) + { + _pool_resize (pool_ptr, elt_size); + ph = pool_hdr (*pool_ptr); + l = vector_len (ph->free_indices); + } + off_t free_id = ph->free_indices[l - 1]; + vector_len (ph->free_indices)--; + bitmap_unset (ph->free_bitmap, free_id); + *elt = *pool_ptr + free_id * elt_size; + return free_id; +} + +void +_pool_put (void **pool_ptr, void **elt, size_t elt_size) +{ + pool_hdr_t *ph = pool_hdr (*pool_ptr); + uint64_t l = vector_len (ph->free_indices); + vector_ensure_pos (ph->free_indices, l); + off_t freed_id = (*elt - *pool_ptr) / elt_size; + ph->free_indices[l] = freed_id; + vector_len (ph->free_indices)++; + bitmap_set (ph->free_bitmap, freed_id); +} diff --git a/lib/src/util/ring.c b/lib/src/util/ring.c new file mode 100644 index 000000000..2c722a842 --- /dev/null +++ b/lib/src/util/ring.c @@ -0,0 +1,49 @@ + +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file ring.c + * \brief Implementation of ring buffer. + */ + +#include <stdlib.h> + +#include <hicn/util/ring.h> + +void +_ring_init (void **ring_ptr, size_t elt_size, size_t max_size) +{ + assert (ring_ptr); + assert (elt_size > 0); + // we use a static array, not a vector (for now) + assert (max_size != 0); + + ring_hdr_t *rh = malloc (RING_HDRLEN + max_size * elt_size); + + rh->roff = 0; + rh->woff = 0; + rh->size = 0; + rh->max_size = max_size; + + *ring_ptr = (uint8_t *) rh + RING_HDRLEN; +} + +void +_ring_free (void **ring_ptr) +{ + free (ring_hdr (*ring_ptr)); + *ring_ptr = NULL; +} diff --git a/lib/src/util/slab.c b/lib/src/util/slab.c new file mode 100644 index 000000000..763dcb444 --- /dev/null +++ b/lib/src/util/slab.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <hicn/util/pool.h> +#include <hicn/util/slab.h> +#include <stdlib.h> + +/* BLOCK LINKED LISTS */ + +static void +block_add_to_head (block_t **head_ptr, block_t *block) +{ + if (head_ptr == NULL || block == NULL) + return; + + block->prev = NULL; + if (*head_ptr != NULL) + { + block->next = *head_ptr; + block->next->prev = block; + } + + *head_ptr = block; +} + +static block_t * +block_remove_from_head (block_t **head_ptr) +{ + if (head_ptr == NULL || *head_ptr == NULL) + return NULL; + + block_t *old_head = *head_ptr; + *head_ptr = old_head->next; + + if (*head_ptr != NULL) + (*head_ptr)->prev = NULL; + return old_head; +} + +static void +block_remove (block_t **head_ptr, block_t *block) +{ + if (*head_ptr == NULL || block == NULL) + return; + + if (block == *head_ptr) + *head_ptr = block->next; + if (block->next != NULL) + block->next->prev = block->prev; + if (block->prev != NULL) + block->prev->next = block->next; +} + +static bool +is_block_list_empty (block_t *block) +{ + return block == NULL; +} + +static void +block_list_free (block_t **head) +{ + if (head == NULL || *head == NULL) + return; + + block_t *curr_block = *head; + while (curr_block != NULL) + { + block_t *next = curr_block->next; + pool_free (curr_block->pool); + free (curr_block); + + curr_block = next; + } + + *head = NULL; +} + +/* BLOCK */ + +static bool +is_block_full (block_t *block) +{ + return pool_get_free_indices_size (block->pool) == 0; +} + +static void +block_set_ptr_in_chunks (block_t *block, size_t chunk_size) +{ + void *chunk = block->pool; + + // Cannot use `pool_foreach()` since it requires to know the type + // while here we use a generic (void *) + for (int i = 0; i < pool_get_alloc_size (block->pool); i++) + { + chunk_hdr_t *hdr = (chunk_hdr_t *) chunk; + hdr->block = block; + + chunk = (uint8_t *) (chunk) + chunk_size; // Move to next chunk + } +} + +static block_t * +block_create (size_t chunk_size, size_t num_chunks) +{ + block_t *block = malloc (sizeof (block_t)); + if (block == NULL) + return NULL; + + block->prev = block->next = NULL; + _pool_init (&block->pool, chunk_size, num_chunks, 0); + block_set_ptr_in_chunks (block, chunk_size); + + return block; +} + +/* SLAB */ + +slab_t * +_slab_create (size_t elt_size, size_t num_elts) +{ + // Initialize slab + slab_t *slab = malloc (sizeof (slab_t)); + if (slab == NULL) + return NULL; + + *slab = (slab_t){ .num_chunks = next_pow2 (num_elts), + .chunk_size = CHUNK_HDRLEN + elt_size, + .full = NULL, + .partial_or_empty = NULL }; + + // Add initial empty block to partial or empty list + block_t *block = block_create (slab->chunk_size, slab->num_chunks); + block_add_to_head (&slab->partial_or_empty, block); + + return slab; +} + +void +slab_free (slab_t *slab) +{ + block_list_free (&slab->full); + block_list_free (&slab->partial_or_empty); + free (slab); +} + +void * +_slab_get (slab_t *slab) +{ + // Create new empty block if none with available chunks + if (is_block_list_empty (slab->partial_or_empty)) + { + block_t *block = block_create (slab->chunk_size, slab->num_chunks); + block_add_to_head (&slab->partial_or_empty, block); + + slab->num_chunks *= 2; // Grow size exponentially + } + + // Get chunck from first block in 'partial_or_empty' list + void *chunk; + _pool_get (&slab->partial_or_empty->pool, &chunk, slab->chunk_size); + + // If the current block (i.e. head of 'partial_or_empty' list) if full, + // move it to the 'full' list + if (is_block_full (slab->partial_or_empty)) + { + block_t *block = block_remove_from_head (&slab->partial_or_empty); + block_add_to_head (&slab->full, block); + } + + return (uint8_t *) chunk + CHUNK_HDRLEN; +} + +void +_slab_put (slab_t *slab, void *chunk) +{ + // Get which block the chunk (that we want to release) belong to + chunk_hdr_t *hdr = chunk_hdr (chunk); + block_t *block = hdr->block; + + // Put chunk back into block + bool is_full = is_block_full (block); + _pool_put (&block->pool, (void *) &hdr, slab->chunk_size); + + // If the block was previously full, move it to 'partial_or_empty' list + if (is_full) + { + block_remove (&slab->full, block); + block_add_to_head (&slab->partial_or_empty, block); + } +}
\ No newline at end of file diff --git a/lib/src/util/types.c b/lib/src/util/types.c new file mode 100644 index 000000000..744192593 --- /dev/null +++ b/lib/src/util/types.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <hicn/util/types.h> + +uint32_t +htonf (float f) +{ + uint32_t i; + uint32_t sign = 0; + + if (f < 0) + { + sign = 1; + f = -f; + } + + // i[31] = sign bit + i = sign << 31; + + // i[30 to 16] = int(f)[14 to 0] + i |= (((uint32_t) f) & 0x7fff) << 16; + + // i[15 to 0] = fraction(f) bits [15 to 0] + i |= (uint32_t) ((f - (uint32_t) f) * 65536.0f) & 0xffff; + + return i; +} + +float +ntohf (uint32_t i) +{ + // integer part = i[14 to 0] + float f = (i >> 16) & 0x7fff; + + // fraction part = i[15 to 0] + f += (i & 0xffff) / 65536.0f; + + // sign = i[31] + if ((i >> 31) & 1) + f = -f; + + return f; +} diff --git a/lib/src/util/vector.c b/lib/src/util/vector.c new file mode 100644 index 000000000..1f5cd0269 --- /dev/null +++ b/lib/src/util/vector.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2021 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: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file vector.c + * \brief Implementation of resizeable static array + */ + +#include <assert.h> +#include <stddef.h> // size_t +#include <stdlib.h> // calloc +#include <stdio.h> + +#include <hicn/util/vector.h> + +#define DEFAULT_VECTOR_SIZE 64 + +int +_vector_init (void **vector_ptr, size_t elt_size, size_t init_size, + size_t max_size) +{ + assert (vector_ptr); + assert (max_size == 0 || init_size < max_size); + + if (init_size == 0) + init_size = DEFAULT_VECTOR_SIZE; + + *vector_ptr = NULL; + int rc = _vector_resize (vector_ptr, elt_size, init_size); + if (rc < 0) + return -1; + + vector_hdr_t *vh = vector_hdr (*vector_ptr); + vh->cur_size = 0; + vh->max_size = max_size; + + return 0; +} + +void +_vector_free (void **vector_ptr) +{ + free (vector_hdr (*vector_ptr)); + *vector_ptr = NULL; +} + +int +_vector_resize (void **vector_ptr, size_t elt_size, off_t pos) +{ + vector_hdr_t *vh; + size_t old_size; + + if (*vector_ptr) + { + vh = vector_hdr (*vector_ptr); + old_size = vh->alloc_size; + } + else + { + vh = NULL; + old_size = 0; + } + + /* Round the allocated size to the next power of 2 of the requested position + */ + size_t new_size = next_pow2 (pos); + + /* Don't grow the vector back */ + if (new_size < old_size) + return 0; + + /* Don't exceed maximum size (for init, check is done beforehand) */ + if (vh && vh->max_size && new_size > vh->max_size) + return -1; + + vh = realloc (vh, VECTOR_HDRLEN + new_size * elt_size); + if (!vh) + return -1; + vh->alloc_size = new_size; + + /* Zero out the newly allocated memory (except headers) */ + memset ((uint8_t *) vh + VECTOR_HDRLEN + old_size * elt_size, 0, + (new_size - old_size) * elt_size); + + /* Reassign vector pointer */ + *vector_ptr = (uint8_t *) vh + VECTOR_HDRLEN; + + return 0; +} diff --git a/lib/src/util/windows/dlfcn.c b/lib/src/util/windows/dlfcn.c index c8173cdb0..5606074a0 100644 --- a/lib/src/util/windows/dlfcn.c +++ b/lib/src/util/windows/dlfcn.c @@ -1,4 +1,4 @@ -/* dlfcn.c */ +/* dlfcn.c */ #include <inttypes.h> #include <stdio.h> @@ -6,60 +6,68 @@ #include <string.h> #include <windows.h> -static struct { - long lasterror; - const char *err_rutin; -} var = { - 0, - NULL -}; +static struct +{ + long lasterror; + const char *err_rutin; +} var = { 0, NULL }; -void *dlopen (const char *filename, int flags) +void * +dlopen (const char *filename, int flags) { - HINSTANCE hInst; + HINSTANCE hInst; - hInst= LoadLibrary (filename); - if (hInst==NULL) { - var.lasterror = GetLastError (); - var.err_rutin = "dlopen"; + hInst = LoadLibrary (filename); + if (hInst == NULL) + { + var.lasterror = GetLastError (); + var.err_rutin = "dlopen"; } - return hInst; + return hInst; } -int dlclose (void *handle) +int +dlclose (void *handle) { - BOOL ok; - int rc= 0; + BOOL ok; + int rc = 0; - ok= FreeLibrary ((HINSTANCE)handle); - if (! ok) { - var.lasterror = GetLastError (); - var.err_rutin = "dlclose"; - rc= -1; + ok = FreeLibrary ((HINSTANCE) handle); + if (!ok) + { + var.lasterror = GetLastError (); + var.err_rutin = "dlclose"; + rc = -1; } - return rc; + return rc; } -void *dlsym (void *handle, const char *name) +void * +dlsym (void *handle, const char *name) { - FARPROC fp; + FARPROC fp; - fp= GetProcAddress ((HINSTANCE)handle, name); - if (!fp) { - var.lasterror = GetLastError (); - var.err_rutin = "dlsym"; + fp = GetProcAddress ((HINSTANCE) handle, name); + if (!fp) + { + var.lasterror = GetLastError (); + var.err_rutin = "dlsym"; } - return (void *)(intptr_t)fp; + return (void *) (intptr_t) fp; } -const char *dlerror (void) +const char * +dlerror (void) { -static char errstr [88]; + static char errstr[88]; - if (var.lasterror) { - sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror); - return errstr; - } else { - return NULL; + if (var.lasterror) + { + snprintf (errstr, 88, "%s error #%ld", var.err_rutin, var.lasterror); + return errstr; + } + else + { + return NULL; } }
\ No newline at end of file |