diff options
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/CMakeLists.txt | 138 | ||||
-rw-r--r-- | lib/src/common.c | 22 | ||||
-rw-r--r-- | lib/src/compat.c | 269 | ||||
-rw-r--r-- | lib/src/error.c | 4 | ||||
-rw-r--r-- | lib/src/face.c | 451 | ||||
-rw-r--r-- | lib/src/libhicn-config.cmake.in | 12 | ||||
-rw-r--r-- | lib/src/mapme.c | 64 | ||||
-rw-r--r-- | lib/src/name.c | 493 | ||||
-rw-r--r-- | lib/src/ops.c | 15 | ||||
-rw-r--r-- | lib/src/policy.c | 73 | ||||
-rw-r--r-- | lib/src/protocol/ah.c | 24 | ||||
-rw-r--r-- | lib/src/protocol/icmp.c | 23 | ||||
-rw-r--r-- | lib/src/protocol/ipv4.c | 98 | ||||
-rw-r--r-- | lib/src/protocol/ipv6.c | 102 | ||||
-rw-r--r-- | lib/src/protocol/new.c | 441 | ||||
-rw-r--r-- | lib/src/protocol/tcp.c | 114 | ||||
-rw-r--r-- | lib/src/protocol/udp.c | 312 | ||||
-rw-r--r-- | lib/src/strategy.c | 22 | ||||
-rw-r--r-- | lib/src/test/CMakeLists.txt | 47 | ||||
-rw-r--r-- | lib/src/test/main.cc | 23 | ||||
-rw-r--r-- | lib/src/test/test_name.cc | 339 | ||||
-rw-r--r-- | lib/src/test/test_new_header.cc | 340 | ||||
-rw-r--r-- | lib/src/test/test_udp_header.cc | 355 | ||||
-rw-r--r-- | lib/src/test/test_validation.cc | 55 | ||||
-rw-r--r-- | lib/src/util/ip_address.c | 284 | ||||
-rw-r--r-- | lib/src/util/log.c | 242 | ||||
-rw-r--r-- | lib/src/util/windows/dlfcn.c | 84 |
27 files changed, 3398 insertions, 1048 deletions
diff --git a/lib/src/CMakeLists.txt b/lib/src/CMakeLists.txt index 2588bde8b..ef74127cc 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,117 @@ # 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 + common.c + compat.c + error.c + face.c + mapme.c + name.c + ops.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 ) 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} +) + + +############################################################## +# 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/common.c b/lib/src/common.c index 562771e09..c4cb8bc3e 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) { @@ -59,7 +57,8 @@ get_addr_family (const char *ip_address) 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 + // 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; @@ -78,7 +77,8 @@ cumulative_hash32 (const void *data, size_t len, u32 lastValue) u32 hash32 (const void *data, size_t len) { - // Standard FNV 32-bit offset: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param + // 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); } @@ -86,7 +86,8 @@ hash32 (const void *data, size_t len) 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 + // 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; @@ -104,13 +105,14 @@ cumulative_hash64 (const void *data, size_t len, u64 lastValue) u64 hash64 (const void *data, size_t len) { - // Standard FNV 64-bit offset: see http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param + // 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); } 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 index 932c11d9e..cb771dbdd 100644 --- a/lib/src/compat.c +++ b/lib/src/compat.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: @@ -51,6 +51,12 @@ hicn_packet_get_format (const hicn_header_t *h, hicn_format_t *format) else *format = HF_INET_TCP; break; + case IPPROTO_UDP: + if (h->v4.newhdr.flags & HICN_NEW_FLAG_SIG) + *format = HF_INET_UDP_AH; + else + *format = HF_INET_UDP; + break; case IPPROTO_ICMP: *format = HF_INET_ICMP; break; @@ -67,6 +73,12 @@ hicn_packet_get_format (const hicn_header_t *h, hicn_format_t *format) else *format = HF_INET6_TCP; break; + case IPPROTO_UDP: + if (h->v6.newhdr.flags & HICN_NEW_FLAG_SIG) + *format = HF_INET6_UDP_AH; + else + *format = HF_INET6_UDP; + break; case IPPROTO_ICMPV6: *format = HF_INET6_ICMP; break; @@ -74,6 +86,12 @@ hicn_packet_get_format (const hicn_header_t *h, hicn_format_t *format) return HICN_LIB_ERROR_NOT_HICN; } break; + case 9: + { + uint8_t ah = (HICN_NEW_FLAG_SIG & h->protocol.newhdr.flags); + *format = HF_NEW_AH * ah + (1 - ah) * HF_NEW; + break; + } default: return HICN_LIB_ERROR_NOT_HICN; } @@ -93,20 +111,32 @@ hicn_format_to_type (hicn_format_t format) { case HF_INET_TCP: return HICN_TYPE_IPV4_TCP; + case HF_INET_UDP: + return HICN_TYPE_IPV4_UDP; case HF_INET6_TCP: return HICN_TYPE_IPV6_TCP; + case HF_INET6_UDP: + return HICN_TYPE_IPV6_UDP; case HF_INET_ICMP: return HICN_TYPE_IPV4_ICMP; case HF_INET6_ICMP: return HICN_TYPE_IPV6_ICMP; + case HF_NEW: + return HICN_TYPE_NEW; case HF_INET_TCP_AH: return HICN_TYPE_IPV4_TCP_AH; + case HF_INET_UDP_AH: + return HICN_TYPE_IPV4_UDP_AH; case HF_INET6_TCP_AH: return HICN_TYPE_IPV6_TCP_AH; + case HF_INET6_UDP_AH: + return HICN_TYPE_IPV6_UDP_AH; case HF_INET_ICMP_AH: return HICN_TYPE_IPV4_ICMP_AH; case HF_INET6_ICMP_AH: return HICN_TYPE_IPV6_ICMP_AH; + case HF_NEW_AH: + return HICN_TYPE_NEW_AH; default: break; } @@ -176,6 +206,8 @@ hicn_packet_get_header_length_from_format (hicn_format_t format, *header_length += _is_ipv6 (format) * IPV6_HDRLEN; *header_length += _is_icmp (format) * ICMP_HDRLEN; *header_length += _is_tcp (format) * TCP_HDRLEN; + *header_length += _is_udp (format) * UDP_HDRLEN; + *header_length += _is_cmpr (format) * NEW_HDRLEN; *header_length += _is_ah (format) * AH_HDRLEN; return HICN_LIB_ERROR_NONE; @@ -185,17 +217,9 @@ 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; + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->get_header_length (type, &h->protocol, + header_length); } int @@ -261,11 +285,6 @@ hicn_packet_set_name (hicn_format_t format, hicn_header_t *h, { hicn_type_t type = hicn_format_to_type (format); -#ifndef HICN_VPP_PLUGIN - if (name->type & HNT_IOV) - return HICN_LIB_ERROR_NOT_IMPLEMENTED; -#endif /* HICN_VPP_PLUGIN */ - if (is_interest) return hicn_ops_vft[type.l1]->set_interest_name (type, &h->protocol, name); else @@ -325,54 +344,26 @@ 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; - } + hicn_type_t type = hicn_format_to_type (format); + if (is_interest) + return hicn_ops_vft[type.l1]->get_interest_locator (type, &h->protocol, + address); else - { - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } - - return HICN_LIB_ERROR_NONE; + return hicn_ops_vft[type.l1]->get_data_locator (type, &h->protocol, + address); } 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; - } + hicn_type_t type = hicn_format_to_type (format); + if (is_interest) + return hicn_ops_vft[type.l1]->set_interest_locator (type, &h->protocol, + address); else - { - return HICN_LIB_ERROR_INVALID_PARAMETER; - } - - return HICN_LIB_ERROR_NONE; + return hicn_ops_vft[type.l1]->set_data_locator (type, &h->protocol, + address); } int @@ -392,19 +383,21 @@ hicn_packet_set_signature_size (hicn_format_t format, hicn_header_t *h, } int -hicn_packet_get_signature_gap (hicn_format_t format, const hicn_header_t *h, - uint8_t *bytes) +hicn_packet_get_signature_padding (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_gap (type, &h->protocol, bytes); + return hicn_ops_vft[type.l1]->get_signature_padding (type, &h->protocol, + bytes); } int -hicn_packet_set_signature_gap (hicn_format_t format, hicn_header_t *h, - uint8_t bytes) +hicn_packet_set_signature_padding (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_gap (type, &h->protocol, bytes); + return hicn_ops_vft[type.l1]->set_signature_padding (type, &h->protocol, + bytes); } int @@ -499,7 +492,30 @@ hicn_packet_set_hoplimit (hicn_header_t *h, u8 hops) } int -hicn_packet_get_lifetime (const hicn_header_t *h, u32 *lifetime) +hicn_packet_is_interest (hicn_format_t format, const hicn_header_t *h, + int *ret) +{ + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->is_interest (type, &h->protocol, ret); +} + +int +hicn_packet_set_interest (hicn_format_t format, hicn_header_t *h) +{ + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->mark_packet_as_interest (type, &h->protocol); +} + +int +hicn_packet_set_data (hicn_format_t format, hicn_header_t *h) +{ + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->mark_packet_as_data (type, &h->protocol); +} + +int +hicn_packet_get_lifetime (hicn_format_t format, 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, @@ -507,7 +523,7 @@ hicn_packet_get_lifetime (const hicn_header_t *h, u32 *lifetime) } int -hicn_packet_set_lifetime (hicn_header_t *h, u32 lifetime) +hicn_packet_set_lifetime (hicn_format_t format, 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, @@ -551,59 +567,21 @@ hicn_packet_set_reserved_bits (hicn_header_t *h, const u8 reserved_bits) } int -hicn_packet_get_payload_type (const hicn_header_t *h, +hicn_packet_get_payload_type (hicn_format_t format, 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; + hicn_type_t type = hicn_header_to_type (h); + return hicn_ops_vft[type.l1]->get_payload_type (type, &h->protocol, + payload_type); } int -hicn_packet_set_payload_type (hicn_header_t *h, +hicn_packet_set_payload_type (hicn_format_t format, 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; + hicn_type_t type = hicn_header_to_type (h); + return hicn_ops_vft[type.l1]->set_payload_type (type, &h->protocol, + payload_type); } int @@ -1057,10 +1035,6 @@ 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); } @@ -1088,13 +1062,25 @@ hicn_interest_compare (const hicn_header_t *interest_1, int hicn_interest_get_lifetime (const hicn_header_t *interest, u32 *lifetime) { - return hicn_packet_get_lifetime (interest, lifetime); + hicn_format_t format; + int rc = hicn_packet_get_format (interest, &format); + + if (rc) + return rc; + + return hicn_packet_get_lifetime (format, interest, lifetime); } int hicn_interest_set_lifetime (hicn_header_t *interest, u32 lifetime) { - return hicn_packet_set_lifetime (interest, lifetime); + hicn_format_t format; + int rc = hicn_packet_get_format (interest, &format); + + if (rc) + return rc; + + return hicn_packet_set_lifetime (format, interest, lifetime); } int @@ -1150,10 +1136,6 @@ 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); } @@ -1180,13 +1162,26 @@ hicn_data_compare (const hicn_header_t *data_1, const hicn_header_t *data_2) int hicn_data_get_expiry_time (const hicn_header_t *data, u32 *expiry_time) { - return hicn_packet_get_lifetime (data, expiry_time); + hicn_format_t format; + int rc = hicn_packet_get_format (data, &format); + + if (rc) + return rc; + + return hicn_packet_get_lifetime (format, 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); + hicn_format_t format; + int rc = hicn_packet_get_format (data, &format); + + if (rc) + return rc; + + return hicn_packet_set_lifetime (format, data, + (hicn_lifetime_t) expiry_time); } int @@ -1204,20 +1199,6 @@ hicn_data_get_payload_length (hicn_format_t format, const hicn_header_t *data, } 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); @@ -1256,6 +1237,20 @@ hicn_data_reset_for_hash (hicn_format_t format, hicn_header_t *packet) } int +hicn_data_is_last (hicn_format_t format, hicn_header_t *h, int *is_last) +{ + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->is_last_data (type, &h->protocol, is_last); +} + +int +hicn_data_set_last (hicn_format_t format, hicn_header_t *h) +{ + hicn_type_t type = hicn_format_to_type (format); + return hicn_ops_vft[type.l1]->set_last_data (type, &h->protocol); +} + +int hicn_packet_get_signature (hicn_format_t format, hicn_header_t *packet, uint8_t **sign_buf) { 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..46b36e8cd --- /dev/null +++ b/lib/src/face.c @@ -0,0 +1,451 @@ +/* + * 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); +} + +/* 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 ip_address_t *local_addr, u16 local_port, + const 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 ip_address_t *local_addr, + u16 local_port, const 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 = ip_address_cmp (&f1->local_addr, &f2->local_addr, f1->family); + if (ret != 0) + return ret; + + ret = ip_address_cmp (&f1->remote_addr, &f2->remote_addr, f1->family); + if (ret != 0) + return ret; + + break; + + case FACE_TYPE_TCP: + case FACE_TYPE_UDP: + ret = ip_address_cmp (&f1->local_addr, &f2->local_addr, f1->family); + if (ret != 0) + return ret; + + ret = f1->local_port - f2->local_port; + if (ret != 0) + return ret; + + ret = ip_address_cmp (&f1->remote_addr, &f2->remote_addr, f1->family); + 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) +{ + switch (face->type) + { + case FACE_TYPE_HICN: + { + char local[MAXSZ_IP_ADDRESS]; + char remote[MAXSZ_IP_ADDRESS]; + char tags[MAXSZ_POLICY_TAGS]; + + ip_address_snprintf (local, MAXSZ_IP_ADDRESS, &face->local_addr, + face->family); + ip_address_snprintf (remote, MAXSZ_IP_ADDRESS, &face->remote_addr, + face->family); + policy_tags_snprintf (tags, MAXSZ_POLICY_TAGS, face->tags); + 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: + { + char local[MAXSZ_IP_ADDRESS]; + char remote[MAXSZ_IP_ADDRESS]; + char tags[MAXSZ_POLICY_TAGS]; + + ip_address_snprintf (local, MAXSZ_IP_ADDRESS, &face->local_addr, + face->family); + ip_address_snprintf (remote, MAXSZ_IP_ADDRESS, &face->remote_addr, + face->family); + policy_tags_snprintf (tags, MAXSZ_POLICY_TAGS, face->tags); + + 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; + + default: + 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..b8e8eaed2 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: @@ -26,8 +26,8 @@ #include <hicn/protocol/ipv6.h> 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 +41,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 +59,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 +81,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,8 +92,8 @@ 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)) @@ -103,9 +103,9 @@ hicn_mapme_create_packet (u8 * buf, const hicn_prefix_t * prefix, } 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 + ip4_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 +119,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 + ip6_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,7 +135,7 @@ 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)) @@ -145,22 +145,23 @@ hicn_mapme_create_ack (u8 * buf, const mapme_params_t * 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 +170,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 +195,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)) { diff --git a/lib/src/name.c b/lib/src/name.c index 54b2a76aa..3bdcbbdb4 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,522 +19,210 @@ */ #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> -#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_prefix (const ip_prefix_t *prefix, u32 id, + hicn_name_t *name) { - switch (prefix->family) - { - case AF_INET: - name->type = HNT_CONTIGUOUS_V4; - memcpy (name->buffer, prefix->address.v4.buffer, - ip_address_len(prefix->family)); - name->len = IPV4_ADDR_LEN; - break; - case AF_INET6: - name->type = HNT_CONTIGUOUS_V6; - memcpy (name->buffer, prefix->address.v6.buffer, - ip_address_len(prefix->family)); - name->len = IPV6_ADDR_LEN; - break; - default: - return HICN_LIB_ERROR_INVALID_IP_ADDRESS; - } - - *(u32 *) (name->buffer + name->len) = id; + name->prefix.v6.as_u64[0] = prefix->address.v6.as_u64[0]; + name->prefix.v6.as_u64[1] = prefix->address.v6.as_u64[1]; + name->suffix = id; return HICN_LIB_ERROR_NONE; } -u8 -hicn_name_get_length (const hicn_name_t * name) -{ - return name->len; -} - 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; - - if ((name1->type == HNT_CONTIGUOUS_V4 && name2->type == HNT_CONTIGUOUS_V6) - || (name1->type == HNT_CONTIGUOUS_V6 - && name2->type == HNT_CONTIGUOUS_V4)) - { - return -1; - } - - if ((name1->type == HNT_IOV_V4 && name2->type == HNT_IOV_V6) || - (name1->type == HNT_IOV_V6 && name2->type == HNT_IOV_V4)) - { - return -1; - } - - if ((name1->type == HNT_IOV_V4 && name2->type == HNT_CONTIGUOUS_V6) || - (name1->type == HNT_IOV_V6 && name2->type == HNT_CONTIGUOUS_V4)) - { - return -1; - } - - if (name1->type == HNT_UNSPEC || name2->type == HNT_UNSPEC) - { - return -1; - } - - size_t len1 = 0, len2 = 0; + int ret; + ret = memcmp (&name_1->prefix.v6.buffer, &name_2->prefix.v6.buffer, + sizeof (name_1->prefix.v6)); - u8 *buffer11, *buffer12, *buffer21, *buffer22; + if (consider_segment) + ret |= !(name_1->suffix == name_2->suffix); - switch (name1->type) - { - case HNT_CONTIGUOUS_V4: - buffer11 = name1->buffer; - buffer12 = name1->buffer + IPV4_ADDR_LEN; - len1 = IPV4_ADDR_LEN; - break; - case HNT_CONTIGUOUS_V6: - buffer11 = name1->buffer; - buffer12 = name1->buffer + IPV6_ADDR_LEN; - len1 = IPV6_ADDR_LEN; - break; - case HNT_IOV_V4: - buffer11 = name1->iov.buffers[0].iov_base; - buffer12 = name1->iov.buffers[1].iov_base; - len1 = IPV4_ADDR_LEN; - break; - case HNT_IOV_V6: - buffer11 = name1->iov.buffers[0].iov_base; - buffer12 = name1->iov.buffers[1].iov_base; - len1 = IPV6_ADDR_LEN; - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } - - switch (name2->type) - { - case HNT_CONTIGUOUS_V4: - buffer21 = name2->buffer; - buffer22 = name2->buffer + IPV4_ADDR_LEN; - len2 = IPV4_ADDR_LEN; - break; - case HNT_CONTIGUOUS_V6: - buffer21 = name2->buffer; - buffer22 = name2->buffer + IPV6_ADDR_LEN; - len2 = IPV6_ADDR_LEN; - break; - case HNT_IOV_V4: - buffer21 = name2->iov.buffers[0].iov_base; - buffer22 = name2->iov.buffers[1].iov_base; - len2 = IPV4_ADDR_LEN; - break; - case HNT_IOV_V6: - buffer21 = name2->iov.buffers[0].iov_base; - buffer22 = name2->iov.buffers[1].iov_base; - len2 = IPV6_ADDR_LEN; - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } - - // Sanity check - if (len1 != len2) - { - return HICN_LIB_ERROR_UNEXPECTED; - } - - int ret1 = memcmp ((u8 *) buffer11, (u8 *) buffer21, len1); - - if (!consider_segment) - { - return ret1; - } - - int ret2 = memcmp ((u8 *) buffer12, (u8 *) buffer22, HICN_SEGMENT_LEN); - - return ret1 || ret2; + return ret; } int -hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix) +hicn_name_hash (const hicn_name_t *name, u32 *hash, bool consider_suffix) { - switch (name->type) - { - case HNT_CONTIGUOUS_V4: - *hash = hash32 (name->buffer, consider_suffix ? HICN_V4_NAME_LEN : HICN_V4_PREFIX_LEN); - break; - case HNT_CONTIGUOUS_V6: - *hash = hash32 (name->buffer, consider_suffix ? HICN_V6_NAME_LEN : HICN_V6_PREFIX_LEN); - break; - case HNT_IOV_V4: - case HNT_IOV_V6: - *hash = - hash32 (name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len); - if (consider_suffix) - { - *hash = cumulative_hash32 (name->iov.buffers[1].iov_base, - name->iov.buffers[1].iov_len, *hash); - } - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } + *hash = hash32 (&name->prefix, sizeof (name->prefix)); + + if (consider_suffix) + *hash = cumulative_hash32 (&name->suffix, sizeof (name->suffix), *hash); return HICN_LIB_ERROR_NONE; } int -hicn_name_empty (hicn_name_t * name) +hicn_name_empty (hicn_name_t *name) { - return name->type == HNT_UNSPEC ? 1 : 0; + return _is_unspec (name); } int -hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src) +hicn_name_copy (hicn_name_t *dst, const hicn_name_t *src) { - switch (src->type) - { - case HNT_CONTIGUOUS_V4: - case HNT_CONTIGUOUS_V6: - *dst = *src; - break; - case HNT_IOV_V4: - case HNT_IOV_V6: - dst->type = - src->type == HNT_IOV_V4 ? HNT_CONTIGUOUS_V4 : HNT_CONTIGUOUS_V6; - memcpy (dst->buffer, src->iov.buffers[0].iov_base, - src->iov.buffers[0].iov_len); - memcpy (dst->buffer + src->iov.buffers[0].iov_len, - src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len); - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } - + *dst = *src; return HICN_LIB_ERROR_NONE; } int -hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src, - bool copy_suffix) +hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *name) { - size_t length; - - switch (src->type) - { - case HNT_CONTIGUOUS_V4: - if (copy_suffix) - { - length = HICN_V4_NAME_LEN; - } - else - { - length = IPV4_ADDR_LEN; - } - memcpy (dst, src->buffer, length); - break; - case HNT_CONTIGUOUS_V6: - if (copy_suffix) - { - length = HICN_V6_NAME_LEN; - } - else - { - length = IPV6_ADDR_LEN; - } - memcpy (dst, src->buffer, length); - break; - case HNT_IOV_V4: - case HNT_IOV_V6: - memcpy (dst, src->iov.buffers[0].iov_base, src->iov.buffers[0].iov_len); - if (copy_suffix) - { - memcpy (dst + src->iov.buffers[0].iov_len, - src->iov.buffers[1].iov_base, src->iov.buffers[1].iov_len); - } - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } + 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); return HICN_LIB_ERROR_NONE; } int -hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number) +hicn_name_set_seq_number (hicn_name_t *name, u32 seq_number) { - u8 *sequence_number = NULL; - - switch (name->type) - { - case HNT_CONTIGUOUS_V6: - sequence_number = name->buffer + IPV6_ADDR_LEN; - break; - case HNT_CONTIGUOUS_V4: - sequence_number = name->buffer + IPV4_ADDR_LEN; - break; - case HNT_IOV_V6: - case HNT_IOV_V4: - sequence_number = name->iov.buffers[1].iov_base; - break; - case HNT_UNSPEC: - return HICN_LIB_ERROR_UNEXPECTED; - } - - if (sequence_number) - { - *(u32 *) sequence_number = seq_number; - } - else - { - return HICN_LIB_ERROR_UNEXPECTED; - } - + name->suffix = seq_number; return HICN_LIB_ERROR_NONE; } int -hicn_name_to_sockaddr_address (const hicn_name_t * name, - struct sockaddr *ip_address) +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; - switch (name->type) + 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 { - case HNT_CONTIGUOUS_V6: - tmp6->sin6_family = AF_INET6; - tmp6->sin6_scope_id = 0; - tmp6->sin6_port = DUMMY_PORT; - memcpy (&tmp6->sin6_addr, name->buffer, IPV6_ADDR_LEN); - break; - case HNT_IOV_V6: tmp6->sin6_family = AF_INET6; tmp6->sin6_scope_id = 0; tmp6->sin6_port = DUMMY_PORT; - memcpy (&tmp6->sin6_addr, name->iov.buffers[0].iov_base, - name->iov.buffers[0].iov_len); - break; - case HNT_CONTIGUOUS_V4: - tmp4->sin_family = AF_INET; - tmp4->sin_port = DUMMY_PORT; - memcpy (&tmp4->sin_addr, name->buffer, IPV4_ADDR_LEN); - break; - case HNT_IOV_V4: - tmp4->sin_family = AF_INET; - tmp4->sin_port = DUMMY_PORT; - memcpy (&tmp4->sin_addr, name->iov.buffers[0].iov_base, - name->iov.buffers[0].iov_len); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; + memcpy (&tmp6->sin6_addr, &name->prefix.v6, IPV6_ADDR_LEN); } return HICN_LIB_ERROR_NONE; } int -hicn_name_to_ip_prefix (const hicn_name_t * name, ip_prefix_t * prefix) +hicn_name_to_ip_prefix (const hicn_name_t *name, ip_prefix_t *prefix) { - switch (name->type) - { - case HNT_CONTIGUOUS_V6: - memcpy (&prefix->address.v6.buffer, name->buffer, IPV6_ADDR_LEN); - prefix->family = AF_INET6; - break; - case HNT_IOV_V6: - memcpy (&prefix->address.v6.buffer, name->iov.buffers[0].iov_base, - name->iov.buffers[0].iov_len); - prefix->family = AF_INET6; - break; - case HNT_CONTIGUOUS_V4: - memcpy (&prefix->address.v4.buffer, name->buffer, IPV4_ADDR_LEN); - prefix->family = AF_INET; - break; - case HNT_IOV_V4: - memcpy (&prefix->address.v4.buffer, name->iov.buffers[0].iov_base, - name->iov.buffers[0].iov_len); - prefix->family = AF_INET; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } + 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_get_seq_number (const hicn_name_t * name, u32 * seq_number) +hicn_name_get_seq_number (const hicn_name_t *name, u32 *seq_number) { - const u8 *sequence_number = NULL; - - switch (name->type) - { - case HNT_CONTIGUOUS_V6: - sequence_number = name->buffer + IPV6_ADDR_LEN; - break; - case HNT_CONTIGUOUS_V4: - sequence_number = name->buffer + IPV4_ADDR_LEN; - break; - case HNT_IOV_V6: - case HNT_IOV_V4: - sequence_number = name->iov.buffers[1].iov_base; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - if (sequence_number) - { - *seq_number = *(u32 *) sequence_number; - } - else - { - return HICN_LIB_ERROR_UNEXPECTED; - } - + *seq_number = name->suffix; return HICN_LIB_ERROR_NONE; } int -hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len) +hicn_name_ntop (const hicn_name_t *src, char *dst, size_t len) { int offset; - const char *rc; - void *seg_number = NULL; - - switch (src->type) - { - case HNT_CONTIGUOUS_V6: - rc = inet_ntop (AF_INET6, src->buffer, dst, (socklen_t)len); - seg_number = (u8 *) src->buffer + IPV6_ADDR_LEN; - break; - case HNT_CONTIGUOUS_V4: - rc = inet_ntop (AF_INET, src->buffer, dst, (socklen_t)len); - seg_number = (u8 *) src->buffer + IPV4_ADDR_LEN; - break; - case HNT_IOV_V6: - rc = inet_ntop (AF_INET6, src->iov.buffers[0].iov_base, dst, (socklen_t)len); - seg_number = src->iov.buffers[1].iov_base; - break; - case HNT_IOV_V4: - rc = inet_ntop (AF_INET, src->iov.buffers[0].iov_base, dst, (socklen_t)len); - seg_number = src->iov.buffers[1].iov_base; - break; - default: - return HICN_LIB_ERROR_NOT_IMPLEMENTED; - } + 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) { - goto ERR; + return HICN_LIB_ERROR_UNSPECIFIED; } - offset = (int) strlen (dst); + offset = (int) strnlen_s (dst, len); dst[offset] = '|'; - sprintf (dst + offset + 1, "%lu", (unsigned long) (*(u32 *) seg_number)); + snprintf (dst + offset + 1, len - offset - 1, "%lu", + (unsigned long) (src->suffix)); return HICN_LIB_ERROR_NONE; - -ERR: - return HICN_LIB_ERROR_UNSPECIFIED; } int -hicn_name_pton (const char *src, hicn_name_t * dst) +hicn_name_pton (const char *src, hicn_name_t *dst) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; } int -hicn_name_get_family (const hicn_name_t * name, int *family) +hicn_name_get_family (const hicn_name_t *name, int *family) { - switch (name->type) - { - case HNT_CONTIGUOUS_V6: - case HNT_IOV_V6: - *family = AF_INET6; - break; - case HNT_CONTIGUOUS_V4: - case HNT_IOV_V4: - *family = AF_INET; - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } + assert (!_is_unspec (name)); + int v4 = _is_inet4 (name); + *family = v4 * AF_INET + (1 - v4) * AF_INET6; return HICN_LIB_ERROR_NONE; } int -hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, - hicn_prefix_t * prefix) +hicn_prefix_create_from_ip_prefix (const ip_prefix_t *ip_prefix, + hicn_prefix_t *prefix) { switch (ip_prefix->family) { case AF_INET: - prefix->name.ip4.as_u32 = ip_prefix->address.v4.as_u32; + prefix->name.v4.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]; + prefix->name.v6.as_u64[0] = ip_prefix->address.v6.as_u64[0]; + prefix->name.v6.as_u64[1] = ip_prefix->address.v6.as_u64[1]; break; default: return HICN_LIB_ERROR_INVALID_IP_ADDRESS; @@ -544,9 +232,6 @@ hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, return HICN_LIB_ERROR_NONE; } -#endif /* ! HICN_VPP_PLUGIN */ - - /* * fd.io coding-style-patch-verification: ON * diff --git a/lib/src/ops.c b/lib/src/ops.c index ef12e9bf0..80ffcf3c5 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: @@ -27,8 +27,10 @@ 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) */ @@ -39,6 +41,7 @@ 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_is_interest (none, NONE); DECLARE_mark_packet_as_interest (none, NONE); DECLARE_mark_packet_as_data (none, NONE); DECLARE_reset_interest_for_hash (none, NONE); @@ -63,6 +66,8 @@ 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_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,8 +77,10 @@ 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_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); /** @@ -84,9 +91,11 @@ 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, + /* 98 */[IPPROTO_ENCAP] = &hicn_ops_new, [IPPROTO_NONE] = &hicn_ops_none, }; diff --git a/lib/src/policy.c b/lib/src/policy.c index 694f0ea5e..0b4850b93 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,56 @@ #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; + if (size != 0 && cur >= s + size) + return (int) (cur - s); + + return (int) (cur - s); } diff --git a/lib/src/protocol/ah.c b/lib/src/protocol/ah.c index 13340eae1..b3d24161d 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 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: @@ -31,6 +31,7 @@ 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_is_interest (ah, UNEXPECTED); DECLARE_mark_packet_as_interest (ah, UNEXPECTED); DECLARE_mark_packet_as_data (ah, UNEXPECTED); DECLARE_get_data_locator (ah, UNEXPECTED); @@ -46,6 +47,10 @@ 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_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) @@ -69,6 +74,7 @@ ah_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) if (rc < 0) return rc; memset (&(h->ah.validationPayload), 0, signature_size); + h->ah.signaturePadding = 0; return CHILD_OPS (reset_interest_for_hash, type, h); } @@ -81,6 +87,7 @@ ah_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) if (rc < 0) return rc; memset (&(h->ah.validationPayload), 0, signature_size); + h->ah.signaturePadding = 0; return CHILD_OPS (reset_interest_for_hash, type, h); } @@ -102,7 +109,7 @@ ah_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, int ah_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old) + const ip_address_t *addr_new, ip_address_t *addr_old) { /* Nothing to do on signature */ return HICN_LIB_ERROR_NONE; @@ -110,7 +117,7 @@ ah_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, int ah_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, + const ip_address_t *addr_new, ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) { /* Nothing to do on signature */ @@ -162,7 +169,7 @@ int ah_set_signature_size (hicn_type_t type, hicn_protocol_t *h, const size_t signature_size) { - h->ah.payloadlen = (u8) (signature_size >> 2); + h->ah.payloadlen = signature_size >> 2; return HICN_LIB_ERROR_NONE; } @@ -201,16 +208,17 @@ ah_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, } int -ah_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ah_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, size_t padding) { - h->ah.signatureGap = gap; + h->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 (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) { - *gap = h->ah.signatureGap; + *padding = h->ah.signaturePadding; return HICN_LIB_ERROR_NONE; } diff --git a/lib/src/protocol/icmp.c b/lib/src/protocol/icmp.c index 5783cf52c..0452e4fbb 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 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -24,6 +24,7 @@ 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_is_interest (icmp, UNEXPECTED); DECLARE_mark_packet_as_interest (icmp, UNEXPECTED); DECLARE_mark_packet_as_data (icmp, UNEXPECTED); DECLARE_get_data_locator (icmp, UNEXPECTED); @@ -40,7 +41,11 @@ 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_type (icmp, UNEXPECTED); +DECLARE_set_payload_type (icmp, UNEXPECTED); DECLARE_get_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) @@ -94,8 +99,7 @@ icmp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, int icmp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) + const ip_address_t *addr_new, ip_address_t *addr_old) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; // u16 *icmp_checksum = &(h->icmp.csum); @@ -120,7 +124,7 @@ 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 ip_address_t *addr_new, ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; @@ -184,16 +188,17 @@ icmp_set_signature_size (hicn_type_t type, hicn_protocol_t *h, } int -icmp_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +icmp_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CHILD_OPS (set_signature_padding, type, h, padding); } int -icmp_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +icmp_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CHILD_OPS (get_signature_padding, type, h, padding); } int diff --git a/lib/src/protocol/ipv4.c b/lib/src/protocol/ipv4.c index cf50f9996..5d445f018 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: @@ -64,17 +64,17 @@ ipv4_init_packet_header (hicn_type_t type, hicn_protocol_t *h) int ipv4_get_interest_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) + ip_address_t *ip_address) { - ip_address->ip4 = h->ipv4.saddr; + ip_address->v4 = h->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) + const ip_address_t *ip_address) { - h->ipv4.saddr = ip_address->ip4; + h->ipv4.saddr = ip_address->v4; return HICN_LIB_ERROR_NONE; } @@ -82,20 +82,16 @@ int ipv4_get_interest_name (hicn_type_t type, const hicn_protocol_t *h, hicn_name_t *name) { - name->ip4.prefix_as_ip4 = h->ipv4.daddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V4; - name->len = HICN_V4_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_interest_name_suffix, type, h, &(name->ip4.suffix)); + name->prefix.v4 = h->ipv4.daddr; + return CHILD_OPS (get_interest_name_suffix, type, h, &(name->suffix)); } int ipv4_set_interest_name (hicn_type_t type, hicn_protocol_t *h, const hicn_name_t *name) { - h->ipv4.daddr = name->ip4.prefix_as_ip4; - return CHILD_OPS (set_interest_name_suffix, type, h, &(name->ip4.suffix)); + h->ipv4.daddr = name->prefix.v4; + return CHILD_OPS (set_interest_name_suffix, type, h, &(name->suffix)); } int @@ -113,6 +109,12 @@ ipv4_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, } int +ipv4_is_interest (hicn_type_t type, const hicn_protocol_t *h, int *is_interest) +{ + return CHILD_OPS (is_interest, type, h, is_interest); +} + +int ipv4_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) { return CHILD_OPS (mark_packet_as_interest, type, h); @@ -135,17 +137,17 @@ ipv4_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) int ipv4_get_data_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) + ip_address_t *ip_address) { - ip_address->ip4 = h->ipv4.daddr; + ip_address->v4 = h->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) + const ip_address_t *ip_address) { - h->ipv4.daddr = ip_address->ip4; + h->ipv4.daddr = ip_address->v4; return HICN_LIB_ERROR_NONE; } @@ -153,20 +155,16 @@ int ipv4_get_data_name (hicn_type_t type, const hicn_protocol_t *h, hicn_name_t *name) { - name->ip4.prefix_as_ip4 = h->ipv4.saddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V4; - name->len = HICN_V4_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_data_name_suffix, type, h, &(name->ip4.suffix)); + name->prefix.v4 = h->ipv4.saddr; + return CHILD_OPS (get_data_name_suffix, type, h, &(name->suffix)); } int ipv4_set_data_name (hicn_type_t type, hicn_protocol_t *h, const hicn_name_t *name) { - h->ipv4.saddr = name->ip4.prefix_as_ip4; - return CHILD_OPS (set_data_name_suffix, type, h, &(name->ip4.suffix)); + h->ipv4.saddr = name->prefix.v4; + return CHILD_OPS (set_data_name_suffix, type, h, &(name->suffix)); } int @@ -311,16 +309,15 @@ ipv4_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, int ipv4_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) + const ip_address_t *addr_new, ip_address_t *addr_old) { // ASSERT(addr_old == NULL); - addr_old->ip4 = h->ipv4.saddr; + addr_old->v4 = h->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.saddr = addr_new->v4; h->ipv4.csum = 0; h->ipv4.csum = csum (&h->ipv4, IPV4_HDRLEN, 0); @@ -329,16 +326,16 @@ ipv4_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, int ipv4_rewrite_data (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old, + const ip_address_t *addr_new, ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) { // ASSERT(addr_old == NULL); - addr_old->ip4 = h->ipv4.daddr; + addr_old->v4 = h->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.daddr = addr_new->v4; h->ipv4.csum = 0; h->ipv4.csum = csum (&h->ipv4, IPV4_HDRLEN, 0); @@ -408,6 +405,20 @@ ipv4_set_payload_length (hicn_type_t type, hicn_protocol_t *h, } int +ipv4_get_payload_type (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type) +{ + return CHILD_OPS (get_payload_type, type, h, payload_type); +} + +int +ipv4_set_payload_type (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type) +{ + return CHILD_OPS (set_payload_type, type, h, payload_type); +} + +int ipv4_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, size_t *signature_size) { @@ -422,16 +433,17 @@ ipv4_set_signature_size (hicn_type_t type, hicn_protocol_t *h, } int -ipv4_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ipv4_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CHILD_OPS (set_signature_padding, type, h, padding); } int -ipv4_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +ipv4_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CHILD_OPS (get_signature_padding, type, h, padding); } int @@ -481,6 +493,18 @@ ipv4_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) return CHILD_OPS (get_signature, type, h, signature); } +int +ipv4_is_last_data (hicn_type_t type, const hicn_protocol_t *h, int *is_last) +{ + return CHILD_OPS (is_last_data, type, h, is_last); +} + +int +ipv4_set_last_data (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (set_last_data, type, h); +} + DECLARE_HICN_OPS (ipv4); /* diff --git a/lib/src/protocol/ipv6.c b/lib/src/protocol/ipv6.c index 7ac55b2be..b3a107a13 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: @@ -33,8 +33,8 @@ ipv6_init_packet_header (hicn_type_t type, hicn_protocol_t *h) /* *INDENT-OFF* */ h->ipv6 = (_ipv6_header_t){ - .saddr = { { 0 } }, - .daddr = { { 0 } }, + .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)), @@ -48,17 +48,17 @@ ipv6_init_packet_header (hicn_type_t type, hicn_protocol_t *h) int ipv6_get_interest_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) + ip_address_t *ip_address) { - ip_address->ip6 = h->ipv6.saddr; + ip_address->v6 = h->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) + const ip_address_t *ip_address) { - h->ipv6.saddr = ip_address->ip6; + h->ipv6.saddr = ip_address->v6; return HICN_LIB_ERROR_NONE; } @@ -66,20 +66,16 @@ int ipv6_get_interest_name (hicn_type_t type, const hicn_protocol_t *h, hicn_name_t *name) { - name->ip6.prefix_as_ip6 = h->ipv6.daddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V6; - name->len = HICN_V6_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_interest_name_suffix, type, h, &(name->ip6.suffix)); + name->prefix.v6 = h->ipv6.daddr; + return CHILD_OPS (get_interest_name_suffix, type, h, &(name->suffix)); } int ipv6_set_interest_name (hicn_type_t type, hicn_protocol_t *h, const hicn_name_t *name) { - h->ipv6.daddr = name->ip6.prefix_as_ip6; - return CHILD_OPS (set_interest_name_suffix, type, h, &(name->ip6.suffix)); + h->ipv6.daddr = name->prefix.v6; + return CHILD_OPS (set_interest_name_suffix, type, h, &(name->suffix)); } int @@ -97,6 +93,12 @@ ipv6_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, } int +ipv6_is_interest (hicn_type_t type, const hicn_protocol_t *h, int *is_interest) +{ + return CHILD_OPS (is_interest, type, h, is_interest); +} + +int ipv6_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) { return CHILD_OPS (mark_packet_as_interest, type, h); @@ -119,17 +121,17 @@ ipv6_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) int ipv6_get_data_locator (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address) + ip_address_t *ip_address) { - ip_address->ip6 = h->ipv6.daddr; + ip_address->v6 = h->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) + const ip_address_t *ip_address) { - h->ipv6.daddr = ip_address->ip6; + h->ipv6.daddr = ip_address->v6; return HICN_LIB_ERROR_NONE; } @@ -137,20 +139,16 @@ int ipv6_get_data_name (hicn_type_t type, const hicn_protocol_t *h, hicn_name_t *name) { - name->ip6.prefix_as_ip6 = h->ipv6.saddr; -#ifndef HICN_VPP_PLUGIN - name->type = HNT_CONTIGUOUS_V6; - name->len = HICN_V6_NAME_LEN; -#endif /* HICN_VPP_PLUGIN */ - return CHILD_OPS (get_data_name_suffix, type, h, &(name->ip6.suffix)); + name->prefix.v6 = h->ipv6.saddr; + return CHILD_OPS (get_data_name_suffix, type, h, &(name->suffix)); } int ipv6_set_data_name (hicn_type_t type, hicn_protocol_t *h, const hicn_name_t *name) { - h->ipv6.saddr = name->ip6.prefix_as_ip6; - return CHILD_OPS (set_data_name_suffix, type, h, &(name->ip6.suffix)); + h->ipv6.saddr = name->prefix.v6; + return CHILD_OPS (set_data_name_suffix, type, h, &(name->suffix)); } int @@ -281,24 +279,23 @@ ipv6_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, int ipv6_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old) + const ip_address_t *addr_new, ip_address_t *addr_old) { // ASSERT(addr_old == NULL); - addr_old->ip6 = h->ipv6.saddr; - h->ipv6.saddr = addr_new->ip6; + addr_old->v6 = h->ipv6.saddr; + h->ipv6.saddr = addr_new->v6; return CHILD_OPS (rewrite_interest, type, h, 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 ip_address_t *addr_new, ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) { // ASSERT(addr_old == NULL); - addr_old->ip6 = h->ipv6.daddr; - h->ipv6.daddr = addr_new->ip6; + addr_old->v6 = h->ipv6.daddr; + h->ipv6.daddr = addr_new->v6; return CHILD_OPS (rewrite_data, type, h, addr_new, addr_old, face_id, reset_pl); @@ -357,6 +354,20 @@ ipv6_set_payload_length (hicn_type_t type, hicn_protocol_t *h, } int +ipv6_get_payload_type (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type) +{ + return CHILD_OPS (get_payload_type, type, h, payload_type); +} + +int +ipv6_set_payload_type (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type) +{ + return CHILD_OPS (set_payload_type, type, h, payload_type); +} + +int ipv6_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, size_t *signature_size) { @@ -371,16 +382,17 @@ ipv6_set_signature_size (hicn_type_t type, hicn_protocol_t *h, } int -ipv6_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +ipv6_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CHILD_OPS (set_signature_padding, type, h, padding); } int -ipv6_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +ipv6_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CHILD_OPS (get_signature_padding, type, h, padding); } int @@ -430,6 +442,18 @@ ipv6_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) return CHILD_OPS (get_signature, type, h, signature); } +int +ipv6_is_last_data (hicn_type_t type, const hicn_protocol_t *h, int *is_last) +{ + return CHILD_OPS (is_last_data, type, h, is_last); +} + +int +ipv6_set_last_data (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (set_last_data, type, h); +} + DECLARE_HICN_OPS (ipv6); /* diff --git a/lib/src/protocol/new.c b/lib/src/protocol/new.c new file mode 100644 index 000000000..8c79963ad --- /dev/null +++ b/lib/src/protocol/new.c @@ -0,0 +1,441 @@ +/* + * 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 <hicn/ops.h> + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" + +static int +is_interest (u8 flags) +{ + return flags & HICN_NEW_FLAG_INT; +} + +int +new_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +{ + memset (&h->newhdr, 0, sizeof (h->newhdr)); + _set_new_header_version (&h->newhdr); + uint8_t ah_flag = type.l2 == IPPROTO_AH ? HICN_NEW_FLAG_SIG : 0; + h->newhdr.flags |= ah_flag; + + return CHILD_OPS (init_packet_header, type, h); +} + +int +new_is_interest (hicn_type_t type, const hicn_protocol_t *h, int *is_interest) +{ + *is_interest = (h->newhdr.flags & HICN_NEW_FLAG_INT) != 0; + return HICN_LIB_ERROR_NONE; +} + +int +new_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +{ + h->newhdr.flags |= HICN_NEW_FLAG_INT; + return HICN_LIB_ERROR_NONE; +} + +int +new_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) +{ + h->newhdr.flags &= ~HICN_NEW_FLAG_INT; + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_locator (hicn_type_t type, const hicn_protocol_t *h, + ip_address_t *ip_address) +{ + assert (is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_interest_locator (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *ip_address) +{ + assert (is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_name (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_t *name) +{ + assert (is_interest (h->newhdr.flags)); + name->prefix = h->newhdr.prefix; + name->suffix = ntohl (h->newhdr.suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_interest_name (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_t *name) +{ + int rc = new_mark_packet_as_interest (type, h); + if (rc) + return rc; + + assert (is_interest (h->newhdr.flags)); + h->newhdr.prefix = name->prefix; + h->newhdr.suffix = htonl (name->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_suffix_t *suffix) +{ + assert (is_interest (h->newhdr.flags)); + *suffix = ntohl (h->newhdr.suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_suffix_t *suffix) +{ + assert (is_interest (h->newhdr.flags)); + h->newhdr.suffix = htonl (*suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +{ + assert (is_interest (h->newhdr.flags)); + return CHILD_OPS (init_packet_header, type, h); +} + +int +new_get_data_locator (hicn_type_t type, const hicn_protocol_t *h, + ip_address_t *ip_address) +{ + assert (!is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_locator (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *ip_address) +{ + assert (!is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_name (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_t *name) +{ + assert (!is_interest (h->newhdr.flags)); + name->prefix = h->newhdr.prefix; + name->suffix = ntohl (h->newhdr.suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_name (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_t *name) +{ + new_mark_packet_as_data (type, h); + assert (!is_interest (h->newhdr.flags)); + h->newhdr.prefix = name->prefix; + h->newhdr.suffix = htonl (name->suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_name_suffix (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_suffix_t *suffix) +{ + assert (!is_interest (h->newhdr.flags)); + *suffix = ntohl (h->newhdr.suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_suffix_t *suffix) +{ + assert (!is_interest (h->newhdr.flags)); + h->newhdr.suffix = htonl (*suffix); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_data_pathlabel (hicn_type_t type, const hicn_protocol_t *h, + u32 *pathlabel) +{ + assert (!is_interest (h->newhdr.flags)); + *pathlabel = h->newhdr.path_label; + return HICN_LIB_ERROR_NONE; +} + +int +new_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, + const u32 pathlabel) +{ + assert (!is_interest (h->newhdr.flags)); + h->newhdr.path_label = pathlabel; + return HICN_LIB_ERROR_NONE; +} + +int +new_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, + const hicn_faceid_t face_id) +{ + hicn_pathlabel_t new_pl; + update_pathlabel (h->newhdr.path_label, face_id, &new_pl); + h->newhdr.path_label = new_pl; + return HICN_LIB_ERROR_NONE; +} + +int +new_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (reset_data_for_hash, type, h); +} + +int +new_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, + hicn_lifetime_t *lifetime) +{ + *lifetime = ntohl (h->newhdr.lifetime); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_lifetime (hicn_type_t type, hicn_protocol_t *h, + const hicn_lifetime_t lifetime) +{ + h->newhdr.lifetime = htonl (lifetime); + return HICN_LIB_ERROR_NONE; +} + +int +new_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, + size_t payload_length) +{ + return HICN_LIB_ERROR_NONE; +} + +int +new_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, + size_t payload_length) +{ + return HICN_LIB_ERROR_NONE; +} + +int +new_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *addr_new, ip_address_t *addr_old) +{ + assert (is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_rewrite_data (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *addr_new, ip_address_t *addr_old, + const hicn_faceid_t face_id, u8 reset_pl) +{ + assert (!is_interest (h->newhdr.flags)); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + *header_length = NEW_HDRLEN + ntohs (h->newhdr.payload_length); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + *header_length = NEW_HDRLEN; + return HICN_LIB_ERROR_NONE; +} + +int +new_get_header_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + 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 = NEW_HDRLEN + child_header_length; + return HICN_LIB_ERROR_NONE; +} + +int +new_get_payload_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *payload_length) +{ + size_t child_header_length = 0; + int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); + if (rc < 0) + return rc; + *payload_length = ntohs (h->newhdr.payload_length) - child_header_length; + return HICN_LIB_ERROR_NONE; +} + +int +new_set_payload_length (hicn_type_t type, hicn_protocol_t *h, + size_t payload_length) +{ + size_t child_header_length = 0; + int rc = CHILD_OPS (get_header_length, type, h, &child_header_length); + if (rc < 0) + return rc; + h->newhdr.payload_length = + htons ((u_short) (payload_length + child_header_length)); + return HICN_LIB_ERROR_NONE; +} + +int +new_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, + size_t *signature_size) +{ + return CHILD_OPS (get_signature_size, type, h, signature_size); +} + +int +new_set_signature_size (hicn_type_t type, hicn_protocol_t *h, + size_t signature_size) +{ + return CHILD_OPS (set_signature_size, type, h, signature_size); +} + +int +new_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) +{ + return CHILD_OPS (set_signature_padding, type, h, padding); +} + +int +new_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) +{ + return CHILD_OPS (get_signature_padding, type, h, padding); +} + +int +new_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, + uint64_t signature_timestamp) +{ + return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); +} + +int +new_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, + uint64_t *signature_timestamp) +{ + return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); +} + +int +new_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, + uint8_t validation_algorithm) +{ + return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); +} + +int +new_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, + uint8_t *validation_algorithm) +{ + return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); +} + +int +new_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +{ + return CHILD_OPS (set_key_id, type, h, key_id); +} + +int +new_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, + uint8_t *key_id_size) +{ + return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); +} + +int +new_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +{ + return CHILD_OPS (get_signature, type, h, signature); +} + +int +new_get_payload_type (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type) +{ + *payload_type = ((h->newhdr.flags & HICN_NEW_FLAG_MAN) == HICN_NEW_FLAG_MAN); + return HICN_LIB_ERROR_NONE; +} + +int +new_set_payload_type (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type) +{ + if (payload_type != HPT_DATA && payload_type != HPT_MANIFEST) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + if (payload_type) + h->newhdr.flags |= HICN_NEW_FLAG_MAN; + else + h->newhdr.flags &= ~HICN_NEW_FLAG_MAN; + + return HICN_LIB_ERROR_NONE; +} + +int +new_is_last_data (hicn_type_t type, const hicn_protocol_t *h, int *is_last) +{ + assert (!is_interest (h->newhdr.flags)); + *is_last = h->newhdr.flags & HICN_NEW_FLAG_LST; + return HICN_LIB_ERROR_NONE; +} + +int +new_set_last_data (hicn_type_t type, hicn_protocol_t *h) +{ + assert (!is_interest (h->newhdr.flags)); + h->newhdr.flags |= HICN_NEW_FLAG_LST; + return HICN_LIB_ERROR_NONE; +} + +DECLARE_HICN_OPS (new); + +#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/tcp.c b/lib/src/protocol/tcp.c index cfeac1907..8097cfd12 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: @@ -44,7 +44,20 @@ DECLARE_get_length (tcp, UNEXPECTED); DECLARE_get_payload_length (tcp, UNEXPECTED); DECLARE_set_payload_length (tcp, UNEXPECTED); -always_inline int +static inline void +reset_for_hash (hicn_protocol_t *h) +{ + h->tcp.sport = 0; + h->tcp.dport = 0; + h->tcp.seq_ack = 0; + h->tcp.data_offset_and_reserved = 0; + h->tcp.flags = 0; + h->tcp.window = 0; + h->tcp.csum = 0; + h->tcp.urg_ptr = 0; +} + +static inline int check_tcp_checksum (u16 csum) { /* As per RFC1624 @@ -92,6 +105,20 @@ tcp_init_packet_header (hicn_type_t type, hicn_protocol_t *h) } int +tcp_is_interest (hicn_type_t type, const hicn_protocol_t *h, int *is_interest) +{ + *is_interest = (h->tcp.flags & HICN_TCP_FLAG_ECE) == 0; + return HICN_LIB_ERROR_NONE; +} + +int +tcp_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +{ + h->tcp.flags &= ~HICN_TCP_FLAG_ECE; + return HICN_LIB_ERROR_NONE; +} + +int tcp_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, hicn_name_suffix_t *suffix) { @@ -103,15 +130,12 @@ int tcp_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, const hicn_name_suffix_t *suffix) { - h->tcp.name_suffix = htonl (*suffix); + int rc = tcp_mark_packet_as_interest (type, h); + if (rc) + return rc; - return HICN_LIB_ERROR_NONE; -} + h->tcp.name_suffix = htonl (*suffix); -int -tcp_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) -{ - h->tcp.flags &= ~HICN_TCP_FLAG_ECE; return HICN_LIB_ERROR_NONE; } @@ -125,9 +149,7 @@ tcp_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) int tcp_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) { - memset (&(h->tcp), 0, 4); - memset (&(h->tcp.seq_ack), 0, 12); - + reset_for_hash (h); return CHILD_OPS (reset_interest_for_hash, type, h); } @@ -143,6 +165,10 @@ int tcp_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, const hicn_name_suffix_t *suffix) { + int rc = tcp_mark_packet_as_data (type, h); + if (rc) + return rc; + h->tcp.name_suffix = htonl (*suffix); return HICN_LIB_ERROR_NONE; } @@ -181,9 +207,7 @@ tcp_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, int tcp_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) { - memset (&(h->tcp), 0, 4); - memset (&(h->tcp.seq_ack), 0, 12); - + reset_for_hash (h); return CHILD_OPS (reset_data_for_hash, type, h); } @@ -279,7 +303,7 @@ tcp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, int tcp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, ip46_address_t *addr_old) + const ip_address_t *addr_new, ip_address_t *addr_old) { u16 *tcp_checksum = &(h->tcp.csum); int ret = check_tcp_checksum (*tcp_checksum); @@ -310,7 +334,7 @@ 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 ip_address_t *addr_new, ip_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) { @@ -339,11 +363,11 @@ tcp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, * csum = ip_csum_add_even (csum, h->ipv4.saddr.as_u32); */ ip_csum_t csum = - ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (addr_old->ip6.as_u64[0])); + ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (addr_old->v6.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])); + ip_csum_sub_even (*tcp_checksum, (ip_csum_t) (addr_old->v6.as_u64[1])); + csum = ip_csum_add_even (csum, (ip_csum_t) (addr_new->v6.as_u64[0])); + csum = ip_csum_add_even (csum, (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); @@ -389,16 +413,17 @@ tcp_set_signature_size (hicn_type_t type, hicn_protocol_t *h, } int -tcp_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, uint8_t gap) +tcp_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) { - return CHILD_OPS (set_signature_gap, type, h, gap); + return CHILD_OPS (set_signature_padding, type, h, padding); } int -tcp_get_signature_gap (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *gap) +tcp_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) { - return CHILD_OPS (get_signature_gap, type, h, gap); + return CHILD_OPS (get_signature_padding, type, h, padding); } int @@ -448,6 +473,43 @@ tcp_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) return CHILD_OPS (get_signature, type, h, signature); } +int +tcp_get_payload_type (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type) +{ + *payload_type = ((h->tcp.flags & HICN_TCP_FLAG_URG) == HICN_TCP_FLAG_URG); + return HICN_LIB_ERROR_NONE; +} + +int +tcp_set_payload_type (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type) +{ + if (payload_type != HPT_DATA && payload_type != HPT_MANIFEST) + return HICN_LIB_ERROR_INVALID_PARAMETER; + + if (payload_type) + h->tcp.flags |= HICN_TCP_FLAG_URG; + else + h->tcp.flags &= ~HICN_TCP_FLAG_URG; + + return HICN_LIB_ERROR_NONE; +} + +int +tcp_is_last_data (hicn_type_t type, const hicn_protocol_t *h, int *is_last) +{ + *is_last = (h->tcp.flags & HICN_TCP_FLAG_RST) == HICN_TCP_FLAG_RST; + return HICN_LIB_ERROR_NONE; +} + +int +tcp_set_last_data (hicn_type_t type, hicn_protocol_t *h) +{ + h->tcp.flags |= HICN_TCP_FLAG_RST; + return HICN_LIB_ERROR_NONE; +} + DECLARE_HICN_OPS (tcp); /* diff --git a/lib/src/protocol/udp.c b/lib/src/protocol/udp.c new file mode 100644 index 000000000..ee46b8e9d --- /dev/null +++ b/lib/src/protocol/udp.c @@ -0,0 +1,312 @@ +/* + * 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 <hicn/ops.h> + +#include <hicn/protocol/udp.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_get_payload_length (udp, UNEXPECTED); +DECLARE_set_payload_length (udp, UNEXPECTED); + +int +udp_init_packet_header (hicn_type_t type, hicn_protocol_t *h) +{ + size_t total_header_length; + int rc = CHILD_OPS (get_header_length, type, h, &total_header_length); + if (rc < 0) + return rc; + + /* *INDENT-OFF* */ + h->udp = (_udp_header_t){ .src_port = 0, + .dst_port = 0, + .length = htons ((u16) total_header_length), + .checksum = 0 }; + /* *INDENT-ON* */ + return CHILD_OPS (init_packet_header, type, h); +} + +int +udp_get_interest_name_suffix (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_suffix_t *suffix) +{ + return CHILD_OPS (get_interest_name_suffix, type, h, suffix); +} + +int +udp_set_interest_name_suffix (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_suffix_t *suffix) +{ + return CHILD_OPS (set_interest_name_suffix, type, h, suffix); +} + +int +udp_is_interest (hicn_type_t type, const hicn_protocol_t *h, int *is_interest) +{ + return CHILD_OPS (is_interest, type, h, is_interest); +} + +int +udp_mark_packet_as_interest (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (mark_packet_as_interest, type, h); +} + +int +udp_mark_packet_as_data (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (mark_packet_as_data, type, h); +} + +int +udp_reset_interest_for_hash (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (reset_interest_for_hash, type, h); +} + +int +udp_get_data_name_suffix (hicn_type_t type, const hicn_protocol_t *h, + hicn_name_suffix_t *suffix) +{ + return CHILD_OPS (get_data_name_suffix, type, h, suffix); +} + +int +udp_set_data_name_suffix (hicn_type_t type, hicn_protocol_t *h, + const hicn_name_suffix_t *suffix) +{ + return CHILD_OPS (set_data_name_suffix, type, h, suffix); +} + +int +udp_get_data_pathlabel (hicn_type_t type, const hicn_protocol_t *h, + u32 *pathlabel) +{ + return CHILD_OPS (get_data_pathlabel, type, h, pathlabel); +} + +int +udp_set_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, + const u32 pathlabel) +{ + return CHILD_OPS (set_data_pathlabel, type, h, pathlabel); +} + +int +udp_update_data_pathlabel (hicn_type_t type, hicn_protocol_t *h, + const hicn_faceid_t face_id) +{ + return CHILD_OPS (update_data_pathlabel, type, h, face_id); +} + +int +udp_reset_data_for_hash (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (reset_data_for_hash, type, h); +} + +int +udp_get_lifetime (hicn_type_t type, const hicn_protocol_t *h, + hicn_lifetime_t *lifetime) +{ + return CHILD_OPS (get_lifetime, type, h, lifetime); +} + +int +udp_set_lifetime (hicn_type_t type, hicn_protocol_t *h, + const hicn_lifetime_t lifetime) +{ + return CHILD_OPS (set_lifetime, type, h, lifetime); +} + +int +udp_update_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, + size_t payload_length) +{ + return CHILD_OPS (update_checksums, type, h, partial_csum, payload_length); +} + +int +udp_verify_checksums (hicn_type_t type, hicn_protocol_t *h, u16 partial_csum, + size_t payload_length) +{ + return CHILD_OPS (verify_checksums, type, h, partial_csum, payload_length); +} + +int +udp_rewrite_interest (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *addr_new, ip_address_t *addr_old) +{ + return CHILD_OPS (rewrite_interest, type, h, addr_new, addr_old); +} + +int +udp_rewrite_data (hicn_type_t type, hicn_protocol_t *h, + const ip_address_t *addr_new, ip_address_t *addr_old, + const hicn_faceid_t face_id, u8 reset_pl) +{ + return CHILD_OPS (rewrite_data, type, h, addr_new, addr_old, face_id, + reset_pl); +} + +int +udp_get_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + *header_length = IPV6_HDRLEN + ntohs (h->ipv6.len); + return HICN_LIB_ERROR_NONE; +} + +int +udp_get_current_header_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + *header_length = UDP_HDRLEN; + return HICN_LIB_ERROR_NONE; +} + +int +udp_get_header_length (hicn_type_t type, const hicn_protocol_t *h, + size_t *header_length) +{ + 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 = UDP_HDRLEN + child_header_length; + return HICN_LIB_ERROR_NONE; +} + +int +udp_get_payload_type (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type) +{ + return CHILD_OPS (get_payload_type, type, h, payload_type); +} + +int +udp_set_payload_type (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type) +{ + return CHILD_OPS (set_payload_type, type, h, payload_type); +} + +int +udp_get_signature_size (hicn_type_t type, const hicn_protocol_t *h, + size_t *signature_size) +{ + return CHILD_OPS (get_signature_size, type, h, signature_size); +} + +int +udp_set_signature_size (hicn_type_t type, hicn_protocol_t *h, + size_t signature_size) +{ + return CHILD_OPS (set_signature_size, type, h, signature_size); +} + +int +udp_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, + size_t padding) +{ + return CHILD_OPS (set_signature_padding, type, h, padding); +} + +int +udp_get_signature_padding (hicn_type_t type, const hicn_protocol_t *h, + size_t *padding) +{ + return CHILD_OPS (get_signature_padding, type, h, padding); +} + +int +udp_set_signature_timestamp (hicn_type_t type, hicn_protocol_t *h, + uint64_t signature_timestamp) +{ + return CHILD_OPS (set_signature_timestamp, type, h, signature_timestamp); +} + +int +udp_get_signature_timestamp (hicn_type_t type, const hicn_protocol_t *h, + uint64_t *signature_timestamp) +{ + return CHILD_OPS (get_signature_timestamp, type, h, signature_timestamp); +} + +int +udp_set_validation_algorithm (hicn_type_t type, hicn_protocol_t *h, + uint8_t validation_algorithm) +{ + return CHILD_OPS (set_validation_algorithm, type, h, validation_algorithm); +} + +int +udp_get_validation_algorithm (hicn_type_t type, const hicn_protocol_t *h, + uint8_t *validation_algorithm) +{ + return CHILD_OPS (get_validation_algorithm, type, h, validation_algorithm); +} + +int +udp_set_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t *key_id) +{ + return CHILD_OPS (set_key_id, type, h, key_id); +} + +int +udp_get_key_id (hicn_type_t type, hicn_protocol_t *h, uint8_t **key_id, + uint8_t *key_id_size) +{ + return CHILD_OPS (get_key_id, type, h, key_id, key_id_size); +} + +int +udp_get_signature (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature) +{ + return CHILD_OPS (get_signature, type, h, signature); +} + +int +udp_is_last_data (hicn_type_t type, const hicn_protocol_t *h, int *is_last) +{ + return CHILD_OPS (is_last_data, type, h, is_last); +} + +int +udp_set_last_data (hicn_type_t type, hicn_protocol_t *h) +{ + return CHILD_OPS (set_last_data, type, h); +} + +DECLARE_HICN_OPS (udp); + +/* + * 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..3e3c025c7 --- /dev/null +++ b/lib/src/test/CMakeLists.txt @@ -0,0 +1,47 @@ +# 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 +) + +############################################################## +# 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_name.cc b/lib/src/test/test_name.cc new file mode 100644 index 000000000..207725adb --- /dev/null +++ b/lib/src/test/test_name.cc @@ -0,0 +1,339 @@ +/* + * 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/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_{} + { + 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); + } + + 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; + rc = hicn_name_hash (&name_a, &hash_a, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 1); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + rc = hicn_name_hash (&name_a, &hash_a, 0); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 0); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + // Now let's change the suffix + rc = hicn_name_set_seq_number (&name_a, 97531); + // They should result equal if we do not consider the suffix + rc = hicn_name_hash (&name_a, &hash_a, 0); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 0); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + // And different if we consider it + rc = hicn_name_hash (&name_a, &hash_a, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 1); + EXPECT_EQ (rc, 0); + 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_seq_number (&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 ip_prefix_t &ip_prefix) + { + uint32_t suffix = 54321; + hicn_name_t name; + int rc = hicn_name_create_from_ip_prefix (&ip_prefix, suffix, &name); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = memcmp (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); + + ip_prefix_t ip_prefix; + rc = hicn_name_to_ip_prefix (&name, &ip_prefix); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ip_prefix.family, family); + rc = ip_address_cmp (&ip_prefix.address, &name.prefix, AF_INET6); + EXPECT_EQ (rc, 0); + } + + hicn_name_t name_, name4_, name6_; + 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 = ip_address_cmp (&name6.prefix, &ipv6_prefix_bytes, AF_INET6); + 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 = ip_address_cmp (&name4.prefix, &ipv4_prefix_bytes, AF_INET); + 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 = ip_address_cmp (&name6.prefix, &ipv4_prefix_bytes, AF_INET); + 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) +{ + ip_prefix_t ip_prefix = { .family = AF_INET6, .address = {}, .len = 64 }; + + ip_prefix.address.v6.as_u64[0] = ipv6_prefix_bytes.v6.as_u64[0]; + ip_prefix.address.v6.as_u64[1] = ipv6_prefix_bytes.v6.as_u64[1]; + + nameFromIpPrefixTest (ip_prefix); +} + +TEST_F (NameTest, NameFromIpPrefix4) +{ + ip_prefix_t ip_prefix = { .family = AF_INET, .address = {}, .len = 64 }; + ip_prefix.address.v4.as_u32 = ipv4_prefix_bytes.v4.as_u32; + ip_prefix.address.pad[0] = 0; + ip_prefix.address.pad[1] = 0; + ip_prefix.address.pad[2] = 0; + nameFromIpPrefixTest (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) +{ + ip4_address_t dst4; + ip6_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_seq_number (&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); +} diff --git a/lib/src/test/test_new_header.cc b/lib/src/test/test_new_header.cc new file mode 100644 index 000000000..33c9e13c9 --- /dev/null +++ b/lib/src/test/test_new_header.cc @@ -0,0 +1,340 @@ +/* + * 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/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/protocol/new.h> +#include <hicn/protocol/ah.h> +#include <hicn/header.h> +#include <hicn/compat.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 (size_t hdr_size, hicn_format_t format) + : buffer_ (new uint8_t[hdr_size]), header_ ((hicn_header_t *) (buffer_)), + 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 (NEW_HDRLEN, HF_NEW) {} + + virtual ~NewHeaderTest () { delete[] buffer_; } + + void + checkCommon (const _new_header_t *new_hdr) + { + // Initialize header + int rc = hicn_packet_init_header (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // 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_length, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + } + + virtual void + SetUp () override + { + auto new_hdr = &header_->protocol.newhdr; + checkCommon (new_hdr); + EXPECT_EQ (new_hdr->flags, 0); + } + + uint8_t *buffer_; + hicn_header_t *header_; + hicn_format_t format_; + hicn_name_t name_, name4_, name6_; + ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +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 + 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); +} diff --git a/lib/src/test/test_udp_header.cc b/lib/src/test/test_udp_header.cc new file mode 100644 index 000000000..5d9f4d1eb --- /dev/null +++ b/lib/src/test/test_udp_header.cc @@ -0,0 +1,355 @@ +/* + * 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/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/protocol/new.h> +#include <hicn/protocol/ah.h> +#include <hicn/header.h> +#include <hicn/compat.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_format_t format) + : buffer_ (new uint8_t[hdr_size]), header_ ((hicn_header_t *) (buffer_)), + 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, HF_INET6_UDP) + { + } + + virtual ~UdpHeaderTest () { delete[] buffer_; } + + void + checkCommon (const _ipv6_header_t *ip6_hdr) + { + // Initialize header + int rc = hicn_packet_init_header (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // 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_length, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + } + + virtual void + SetUp () override + { + auto ip6_hdr = &header_->protocol.ipv6; + checkCommon (ip6_hdr); + } + + uint8_t *buffer_; + hicn_header_t *header_; + hicn_format_t format_; + hicn_name_t name_, name4_, name6_; + ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +class UdpHeaderAHTest : public UdpHeaderTest +{ +protected: + UdpHeaderAHTest () + : UdpHeaderTest (AH_HDRLEN + NEW_HDRLEN + UDP_HDRLEN + IPV6_HDRLEN, + HF_INET6_UDP_AH) + { + } +}; + +/** + * Header Initialization + */ +TEST_F (UdpHeaderTest, 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_INET6_UDP); +} + +TEST_F (UdpHeaderAHTest, 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_INET6_UDP_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 (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 + 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); +} 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/util/ip_address.c b/lib/src/util/ip_address.c index 49916547d..412baddcf 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,51 @@ #include <hicn/util/ip_address.h> #include <hicn/util/log.h> +#include <hicn/util/sstrncpy.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), + .v4.as_inaddr.s_addr = SWAP (INADDR_LOOPBACK), }; -const ip_address_t IPV6_LOOPBACK ={ - .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT, +const ip_address_t IPV6_LOOPBACK = { + .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT, }; -const 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 ip_address_t IPV6_ANY = { - .v6.as_in6addr = IN6ADDR_ANY_INIT, + .v6.as_in6addr = IN6ADDR_ANY_INIT, }; -const ip_address_t IP_ADDRESS_EMPTY = { - .v6.as_u64 = { 0 }, +const ip4_address_t IP4_ADDRESS_EMPTY = { + .as_u32 = 0, +}; + +const ip6_address_t IP6_ADDRESS_EMPTY = { + .as_u64 = { 0, 0 }, }; +const ip_address_t IP_ADDRESS_EMPTY = { + .v6.as_u64 = { 0, 0 }, +}; /* IP address */ int -ip_address_get_family (const char * ip_address) +ip_address_get_family (const char *ip_address) { struct addrinfo hint, *res = NULL; int rc; @@ -81,24 +88,26 @@ int 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) +ip_address_ntop (const ip_address_t *ip_address, char *dst, const size_t len, + int family) { - const char * s; - switch(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,14 +115,15 @@ 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) +ip_address_pton (const char *ip_address_str, ip_address_t *ip_address) { int pton_fd; int family; family = ip_address_get_family (ip_address_str); - switch (family) { + switch (family) + { case AF_INET: ip_address->pad[0] = 0; ip_address->pad[1] = 0; @@ -136,32 +146,34 @@ ip_address_pton (const char *ip_address_str, ip_address_t * ip_address) } int -ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family) +ip_address_snprintf (char *s, size_t size, const ip_address_t *ip_address, + int family) { - 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; + 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) +ip_address_to_sockaddr (const 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,33 +199,30 @@ 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) +ip_address_cmp (const ip_address_t *ip1, const ip_address_t *ip2, int family) { - 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)); + switch (family) + { + case AF_INET: + return memcmp (ip1->v4.buffer, ip2->v4.buffer, sizeof (ip1->v4)); + case AF_INET6: + default: + return memcmp (ip1->v6.buffer, ip2->v6.buffer, sizeof (ip1->v6)); } } int -ip_address_empty(const ip_address_t * ip) +ip_address_empty (const ip_address_t *ip) { - return (memcmp(ip, &IP_ADDRESS_EMPTY, sizeof(IP_ADDRESS_EMPTY)) == 0); + return (memcmp (ip->v6.buffer, &IP_ADDRESS_EMPTY.v6.buffer, + sizeof (IP_ADDRESS_EMPTY)) == 0); } - - /* 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) +ip_prefix_pton (const char *ip_address_str, ip_prefix_t *ip_prefix) { int pton_fd; char *p; @@ -221,29 +230,32 @@ ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix) char *addr = strdup (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) + { + ip_prefix->len = ~0; // until we get the ip address family + } + else + { + ip_prefix->len = (u8) strtoul (p + 1, &eptr, 10); + *p = 0; + } ip_prefix->family = ip_address_get_family (addr); switch (ip_prefix->family) { case AF_INET6: - if (ip_prefix->len == (u8)~0) - ip_prefix->len = IPV6_ADDR_LEN_BITS; + if (ip_prefix->len == (u8) ~0) + ip_prefix->len = IPV6_ADDR_LEN_BITS; if (ip_prefix->len > IPV6_ADDR_LEN_BITS) - goto ERR; + goto ERR; pton_fd = inet_pton (AF_INET6, addr, &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 == (u8) ~0) + ip_prefix->len = IPV4_ADDR_LEN_BITS; if (ip_prefix->len > IPV4_ADDR_LEN_BITS) - goto ERR; + goto ERR; pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.v4.buffer); break; default: @@ -253,9 +265,9 @@ 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); + free (addr); return 1; ERR: free (addr); @@ -263,129 +275,143 @@ ERR: } int -ip_prefix_ntop_short(const ip_prefix_t * ip_prefix, char *dst, size_t size) +ip_prefix_ntop_short (const ip_prefix_t *ip_prefix, char *dst, size_t size) { char ip_s[MAXSZ_IP_ADDRESS]; - const char * s; - switch(ip_prefix->family) { + const char *s; + switch (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, 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, 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) +ip_prefix_ntop (const ip_prefix_t *ip_prefix, char *dst, size_t size) { char ip_s[MAXSZ_IP_ADDRESS]; - const char * s; - switch(ip_prefix->family) { + const char *s; + switch (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, 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, 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, ip_prefix->len); if (rc >= size) - return (int)size; + return (int) size; return rc; } int -ip_prefix_len (const ip_prefix_t * prefix) +ip_prefix_snprintf (char *s, size_t size, const ip_prefix_t *prefix) +{ + return ip_prefix_ntop (prefix, s, size); +} + +int +ip_prefix_len (const ip_prefix_t *prefix) { - return prefix->len; // ip_address_len(&prefix->address, prefix->family); + return prefix->len; // ip_address_len(&prefix->address, prefix->family); } const u8 * -ip_address_get_buffer(const ip_address_t * ip_address, int family) +ip_address_get_buffer (const 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) +ip_prefix_empty (const ip_prefix_t *prefix) { return prefix->len == 0; } int -ip_prefix_to_sockaddr(const ip_prefix_t * prefix, - struct sockaddr *sa) +ip_prefix_to_sockaddr (const 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 == ip_address_len + return ip_address_to_sockaddr (&prefix->address, sa, prefix->family); } int -ip_prefix_cmp(const ip_prefix_t * prefix1, const ip_prefix_t * prefix2) +ip_prefix_cmp (const ip_prefix_t *prefix1, const ip_prefix_t *prefix2) { - if (prefix1->family < prefix2->family) - return -1; - else if (prefix1->family > prefix2->family) - return 1; + 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; + if (prefix1->len < prefix2->len) + return -1; + else if (prefix1->len > prefix2->len) + return 1; - return ip_address_cmp(&prefix1->address, &prefix2->address, prefix1->family); + return ip_address_cmp (&prefix1->address, &prefix2->address, + prefix1->family); } /* 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, int family, const ip_address_t *ip_address, + u16 port) { - char ip_address_s[MAXSZ_IP_ADDRESS]; - int rc; + char ip_address_s[MAXSZ_IP_ADDRESS]; + int rc; - /* Other address are currently not supported */ - if (!IS_VALID_FAMILY(family)) - return -1; + /* Other address are currently not supported */ + if (!IS_VALID_FAMILY (family)) + return -1; - 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; + 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; - return snprintf(s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6', - ip_address_s, port); + return snprintf (s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6', + ip_address_s, port); } diff --git a/lib/src/util/log.c b/lib/src/util/log.c index c1fc999ad..975762603 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,177 @@ 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) 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 (ANDROID_LOG_INFO, "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; + + 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"); #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/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 |