diff options
Diffstat (limited to 'lib')
63 files changed, 5585 insertions, 2557 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 65c1cd4ee..ca845ab48 100644 --- a/lib/CMakeLists.txt +++ b/lib/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,41 +11,50 @@ # See the License for the specific language governing permissions and # limitations under the License. +############################################################## +# Project and cmake version +############################################################## cmake_minimum_required (VERSION 3.10 FATAL_ERROR) project(libhicn C) -#include(CTest) + +############################################################## +# Cmake modules +############################################################## set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules" - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" ) -if (NOT CMAKE_BUILD_TYPE) - message(STATUS "${PROJECT_NAME}: No build type selected, default to Release") - set(CMAKE_BUILD_TYPE "Release") +if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + include(CommonSetup) endif() + +############################################################## +# Libs and Bins names +############################################################## set(LIBHICN hicn CACHE INTERNAL "" FORCE) +set(LIBHICN_COMPONENT lib${LIBHICN} CACHE INTERNAL "" FORCE) set(LIBHICN_SHARED ${LIBHICN}.shared CACHE INTERNAL "" FORCE) set(LIBHICN_STATIC ${LIBHICN}.static CACHE INTERNAL "" FORCE) -set(HICN_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/includes CACHE INTERNAL "" FORCE) - -include (Packaging) -option(CMAKE_BUILD_TEST "Build unit tests" OFF) +############################################################## +# Dependencies +############################################################## -if (NOT CMAKE_BUILD_TYPE) - message(STATUS "${PROJECT_NAME}: No build type selected, default to Release") - set(CMAKE_BUILD_TYPE "Release") +if(INTERNAL_ENVIRONMENT) + include(ImportInternal) + ImportInternal() + set(INTERNAL_ENVIRONMENT_FOUND True CACHE STRING "Internal dependency found") endif() -if (NOT WIN32) - set(CMAKE_C_FLAGS -Wall) -else () - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996") -endif () +############################################################## +# Packaging and versioning +############################################################## +include(${CMAKE_CURRENT_SOURCE_DIR}/../versions.cmake) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/packaging.cmake) -add_subdirectory(includes) -add_subdirectory (src) +add_subdirectory(includes) +add_subdirectory(src) diff --git a/lib/cmake/Modules/Packaging.cmake b/lib/cmake/packaging.cmake index 2ceda1426..5f1691722 100644 --- a/lib/cmake/Modules/Packaging.cmake +++ b/lib/cmake/packaging.cmake @@ -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: @@ -32,11 +32,21 @@ set(lib${LIBHICN}-dev_DESCRIPTION ${${LIBHICN}_DESCRIPTION} ) set(lib${LIBHICN}-dev_DEB_DEPENDENCIES - "libhicn (>= stable_version)" + "libhicn (= stable_version)" CACHE STRING "Dependencies for deb/rpm package." ) + +if (INTERNAL_ENVIRONMENT) + include(CheckSafeC) + CheckSafeC() + set(lib${LIBHICN}-dev_DEB_DEPENDENCIES + "${lib${LIBHICN}-dev_DEB_DEPENDENCIES}, ${SAFEC_DEPENDENCY}" + CACHE STRING "Dependencies for deb/rpm package." + ) +endif() + set(lib${LIBHICN}-dev_RPM_DEPENDENCIES - "libhicn >= stable_version" + "libhicn = stable_version" CACHE STRING "Dependencies for deb/rpm package." ) diff --git a/lib/includes/CMakeLists.txt b/lib/includes/CMakeLists.txt index 6c2c34b86..821feb2bb 100644 --- a/lib/includes/CMakeLists.txt +++ b/lib/includes/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,8 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -set(HICN_INCLUDE_DIRS - ${CMAKE_CURRENT_SOURCE_DIR} "" +############################################################## +# Public headers directory +############################################################## +set(Libhicn_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "" FORCE ) @@ -23,12 +26,15 @@ set(LIBHICN_HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/hicn/common.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/compat.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/error.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/face.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/header.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/mapme.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/name.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/policy.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/ops.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/strategy.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/validation.h PARENT_SCOPE ) @@ -40,19 +46,14 @@ set(LIBHICN_HEADER_FILES_PROTOCOL ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ipv6.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/tcp.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/udp.h - PARENT_SCOPE -) - -set(LIBHICN_HEADER_FILES_UTIL + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/new.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/array.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/ip_address.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/log.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/map.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/set.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/sstrncpy.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/token.h ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/types.h PARENT_SCOPE ) - -set_property(GLOBAL PROPERTY LIBHICN_HEADER_FILES_UTIL_PROPERTY "${LIBHICN_HEADER_FILES_UTIL}") - diff --git a/lib/includes/hicn/base.h b/lib/includes/hicn/base.h index 1061724ce..844814d57 100644 --- a/lib/includes/hicn/base.h +++ b/lib/includes/hicn/base.h @@ -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: @@ -34,16 +34,17 @@ typedef u32 hicn_faceid_t; typedef u8 hicn_pathlabel_t; typedef u32 hicn_lifetime_t; -#define HICN_MAX_LIFETIME_SCALED 0xFFFF -#define HICN_MAX_LIFETIME_MULTIPLIER 0x0F /* 4 bits */ -#define HICN_MAX_LIFETIME HICN_MAX_LIFETIME_SCALED << HICN_MAX_LIFETIME_MULTIPLIER +#define HICN_MAX_LIFETIME_SCALED 0xFFFF +#define HICN_MAX_LIFETIME_MULTIPLIER 0x0F /* 4 bits */ +#define HICN_MAX_LIFETIME \ + HICN_MAX_LIFETIME_SCALED << HICN_MAX_LIFETIME_MULTIPLIER /** * @brief hICN packet format type * - * The hICN type represents the sequence of protocols that we can find in packet - * headers. They are represented as a quartet of u8 values, correponding to - * IANA protocol assignment, and read from right to left. This is done to + * The hICN type represents the sequence of protocols that we can find in + * packet headers. They are represented as a quartet of u8 values, correponding + * to IANA protocol assignment, and read from right to left. This is done to * faciliate decapsulation of packet header by simple shift/mask operations. * * For instance, an IPv6/TCP packet will be identified as : @@ -55,19 +56,19 @@ typedef u32 hicn_lifetime_t; */ typedef union { - /** protocol layers representation */ + /** protocol layers representation */ struct { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - u8 l1; /**< First layer */ - u8 l2; /**< Second layer */ - u8 l3; /**< Third layer */ - u8 l4; /**< Fourth layer */ + u8 l1; /**< First layer */ + u8 l2; /**< Second layer */ + u8 l3; /**< Third layer */ + u8 l4; /**< Fourth layer */ #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - u8 l4; /**< Fourth layer */ - u8 l3; /**< Third layer */ - u8 l2; /**< Second layer */ - u8 l1; /**< First layer */ + u8 l4; /**< Fourth layer */ + u8 l3; /**< Third layer */ + u8 l2; /**< Second layer */ + u8 l1; /**< First layer */ #elif _WIN32 /* Windows is assumed little-endian */ u8 l1; u8 l2; @@ -77,46 +78,73 @@ typedef union #error "Unsupported endianness" #endif }; - /** u32 representation */ + /** u32 representation */ u32 as_u32; } hicn_type_t; /* Common protocol layers */ /* Common protocol layers */ #ifndef _WIN32 -#define HICN_TYPE(x,y,z,t) (hicn_type_t) {{ .l1 = x, .l2 = y, .l3 = z, .l4 = t }} +#define HICN_TYPE(x, y, z, t) \ + (hicn_type_t) \ + { \ + { \ + .l1 = x, .l2 = y, .l3 = z, .l4 = t \ + } \ + } #else inline hicn_type_t -HICN_TYPE(int x, int y, int z, int t) +HICN_TYPE (int x, int y, int z, int t) { - hicn_type_t type; - type.l1 = x; - type.l2 = y; - type.l3 = z; - type.l4 = t; - return type; + hicn_type_t type; + type.l1 = x; + type.l2 = y; + type.l3 = z; + type.l4 = t; + return type; } #endif -#define HICN_TYPE_IPV4_TCP HICN_TYPE(IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV4_ICMP HICN_TYPE(IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV6_TCP HICN_TYPE(IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV6_ICMP HICN_TYPE(IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, IPPROTO_NONE) -#define HICN_TYPE_IPV4_TCP_AH HICN_TYPE(IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV4_ICMP_AH HICN_TYPE(IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV6_TCP_AH HICN_TYPE(IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_IPV6_ICMP_AH HICN_TYPE(IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE) -#define HICN_TYPE_NONE HICN_TYPE(IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV4_TCP \ + HICN_TYPE (IPPROTO_IP, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV4_ICMP \ + HICN_TYPE (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV6_TCP \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV6_ICMP \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_NEW \ + HICN_TYPE (IPPROTO_ENCAP, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV4_UDP \ + HICN_TYPE (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_TYPE_IPV6_UDP \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_NONE) +#define HICN_TYPE_IPV4_TCP_AH \ + HICN_TYPE (IPPROTO_IP, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_TYPE_IPV4_ICMP_AH \ + HICN_TYPE (IPPROTO_IP, IPPROTO_ICMP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_TYPE_IPV6_TCP_AH \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_TCP, IPPROTO_AH, IPPROTO_NONE) +#define HICN_TYPE_IPV6_ICMP_AH \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_ICMPV6, IPPROTO_AH, IPPROTO_NONE) +#define HICN_TYPE_NEW_AH \ + HICN_TYPE (IPPROTO_ENCAP, IPPROTO_AH, IPPROTO_NONE, IPPROTO_NONE) +#define HICN_TYPE_IPV6_UDP_AH \ + HICN_TYPE (IPPROTO_IPV6, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_TYPE_IPV4_UDP_AH \ + HICN_TYPE (IPPROTO_IP, IPPROTO_UDP, IPPROTO_ENCAP, IPPROTO_AH) +#define HICN_TYPE_NONE \ + HICN_TYPE (IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE, IPPROTO_NONE) /** * @brief Check if type is none. * @return 1 if none, 0 otherwise */ -always_inline int -hicn_type_is_none(hicn_type_t type) +static inline int +hicn_type_is_none (hicn_type_t type) { return (type.l1 == IPPROTO_NONE) && (type.l2 == IPPROTO_NONE) && - (type.l3 == IPPROTO_NONE) && (type.l4 == IPPROTO_NONE); + (type.l3 == IPPROTO_NONE) && (type.l4 == IPPROTO_NONE); } /** @@ -151,11 +179,12 @@ typedef enum * @param [in] face_id The face identifier to combine into the path label * @param [out] new_label Computed pathlabel * - * This function updates the current_label based on the new face_id, and returns + * This function updates the current_label based on the new face_id, and + * returns */ -always_inline void +static inline void update_pathlabel (hicn_pathlabel_t current_label, hicn_faceid_t face_id, - hicn_pathlabel_t * new_label) + hicn_pathlabel_t *new_label) { hicn_pathlabel_t pl_face_id = (hicn_pathlabel_t) (face_id & HICN_PATH_LABEL_MASK); diff --git a/lib/includes/hicn/common.h b/lib/includes/hicn/common.h index 05f8ad95f..b0898ce1f 100644 --- a/lib/includes/hicn/common.h +++ b/lib/includes/hicn/common.h @@ -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,13 +51,17 @@ typedef uint8_t u8; #ifndef HICN_VPP_PLUGIN -#define PREDICT_FALSE(x) (x) -#define PREDICT_TRUE(x) (x) -#define always_inline static inline -#define static_always_inline static inline -#define STRUCT_SIZE_OF(type, member) sizeof(((type *)0)->member) +#define PREDICT_FALSE(x) (x) +#define PREDICT_TRUE(x) (x) +#define STRUCT_SIZE_OF(type, member) sizeof (((type *) 0)->member) #define ASSERT +#ifndef NDEBUG +#define _ASSERT(x) assert (x) +#else +#define _ASSERT(x) ((void) (x)) +#endif + #define STATIC_ASSERT(x) /* Architecture-dependent uword size */ @@ -82,13 +86,17 @@ typedef u32 uword; typedef uword ip_csum_t; +#else + +#include <vppinfra/clib.h> + #endif /* ! HICN_VPP_PLUGIN */ /* - * Windows compilers do not support named initilizers when .h files are included - * inside C++ files. For readability, we either use the following macro, or - * duplicate some code, with the intent of preserving those safeguards for - * non-Windows platforms. + * Windows compilers do not support named initilizers when .h files are + * included inside C++ files. For readability, we either use the following + * macro, or duplicate some code, with the intent of preserving those + * safeguards for non-Windows platforms. */ #ifndef _WIN32 #define ATTR_INIT(key, value) .key = value @@ -97,12 +105,12 @@ typedef uword ip_csum_t; #endif #ifdef _WIN32 - /* Endianness detection for Windows platforms */ +/* Endianness detection for Windows platforms */ #define __ORDER_LITTLE_ENDIAN__ 0x41424344UL -#define __ORDER_BIG_ENDIAN__ 0x44434241UL -#define __BYTE_ORDER__ ('ABCD') +#define __ORDER_BIG_ENDIAN__ 0x44434241UL +#define __BYTE_ORDER__ ('ABCD') - /* Windows compatibility headers */ +/* Windows compatibility headers */ #define WIN32_LEAN_AND_MEAN #ifndef NOMINMAX #define NOMINMAX @@ -116,10 +124,10 @@ typedef uword ip_csum_t; #define strdup _strdup #define __attribute__(A) -#ifndef IOVEC +#ifndef IOVEC #define IOVEC #define UIO_MAXIOV 16 -#define IOV_MAX UIO_MAXIOV +#define IOV_MAX UIO_MAXIOV struct iovec { void *iov_base; @@ -132,66 +140,20 @@ struct iovec * Portable attribute packed. */ #ifndef _WIN32 -#define PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__)) +#define PACKED(__Declaration__) __Declaration__ __attribute__ ((__packed__)) #else -#define PACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) ) +#define PACKED(__Declaration__) \ + __pragma (pack (push, 1)) __Declaration__ __pragma (pack (pop)) #endif - /* * IP address types */ -#ifdef HICN_VPP_PLUGIN - -#include <vnet/ip/ip4_packet.h> // ip4_address_t -#include <vnet/ip/ip6_packet.h> // ip6_address_t - -#if __GNUC__ >= 9 -#pragma GCC diagnostic ignored "-Waddress-of-packed-member" -#endif - -#include <vnet/ip/ip46_address.h> - -#if __GNUC__ >= 9 -#pragma GCC diagnostic pop -#endif - -#else - - #ifndef _WIN32 #include <netinet/in.h> #endif -typedef union -{ - u32 as_u32; - struct in_addr as_inaddr; -} ip4_address_t; - -typedef union -{ - u64 as_u64[2]; - u32 as_u32[4]; - u8 as_u8[16]; - struct in6_addr as_in6addr; -} ip6_address_t; - -typedef union -{ - struct - { - u32 pad[3]; - ip4_address_t ip4; - }; - ip6_address_t ip6; -} ip46_address_t; - -#define ip46_address_is_ip4(ip46) (((ip46)->pad[0] | (ip46)->pad[1] | (ip46)->pad[2]) == 0) - -#endif /* ! HICN_VPP_PLUGIN */ - /** * @brief Returns the family of an IP address * @param [in] ip_address - IP address in presentation format @@ -215,7 +177,7 @@ int get_addr_family (const char *ip_address); * borrow this code here. */ -static_always_inline u16 +static inline u16 ip_csum_fold (ip_csum_t c) { /* Reduce to 16 bits. */ @@ -227,10 +189,10 @@ ip_csum_fold (ip_csum_t c) c = (c & 0xffff) + (c >> 16); c = (c & 0xffff) + (c >> 16); - return (u16)c; + return (u16) c; } -static_always_inline ip_csum_t +static inline ip_csum_t ip_csum_with_carry (ip_csum_t sum, ip_csum_t x) { ip_csum_t t = sum + x; @@ -238,7 +200,7 @@ ip_csum_with_carry (ip_csum_t sum, ip_csum_t x) } /* Update checksum changing field at even byte offset from x -> 0. */ -static_always_inline ip_csum_t +static inline ip_csum_t ip_csum_add_even (ip_csum_t c, ip_csum_t x) { ip_csum_t d; @@ -252,19 +214,19 @@ ip_csum_add_even (ip_csum_t c, ip_csum_t x) } /* Update checksum changing field at even byte offset from 0 -> x. */ -static_always_inline ip_csum_t +static inline ip_csum_t ip_csum_sub_even (ip_csum_t c, ip_csum_t x) { return ip_csum_with_carry (c, x); } +#endif /* ! HICN_VPP_PLUGIN */ + u32 cumulative_hash32 (const void *data, size_t len, u32 lastValue); u32 hash32 (const void *data, size_t len); u64 cumulative_hash64 (const void *data, size_t len, u64 lastValue); u64 hash64 (const void *data, size_t len); -void hicn_packet_dump (const uint8_t * buffer, size_t len); - -#endif /* ! HICN_VPP_PLUGIN */ +void hicn_packet_dump (const uint8_t *buffer, size_t len); /** * @brief Computes buffer checksum @@ -273,7 +235,7 @@ void hicn_packet_dump (const uint8_t * buffer, size_t len); * @param [in] init - Checksum initial value * @return Checksum of specified buffer */ -always_inline u16 +static inline u16 csum (const void *addr, size_t size, u16 init) { u32 sum = init; @@ -291,7 +253,7 @@ csum (const void *addr, size_t size, u16 init) sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); - return (u16) ~ sum; + return (u16) ~sum; } /* @@ -305,17 +267,24 @@ csum (const void *addr, size_t size, u16 init) * Query IP version from packet (either 4 or 6) * (version is located as same offsets in both protocol headers) */ -#define HICN_IP_VERSION(packet) ((hicn_header_t *)packet)->v4.ip.version +#define HICN_IP_VERSION(packet) \ + ((hicn_header_t *) packet)->protocol.ipv4.version /* * ntohll / htonll allows byte swapping for 64 bits integers */ #ifndef htonll -#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) +#define htonll(x) \ + ((1 == htonl (1)) ? \ + (x) : \ + ((uint64_t) htonl ((x) &0xFFFFFFFF) << 32) | htonl ((x) >> 32)) #endif #ifndef ntohll -#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) +#define ntohll(x) \ + ((1 == ntohl (1)) ? \ + (x) : \ + ((uint64_t) ntohl ((x) &0xFFFFFFFF) << 32) | ntohl ((x) >> 32)) #endif #endif /* HICN_COMMON_H */ diff --git a/lib/includes/hicn/compat.h b/lib/includes/hicn/compat.h index 486c82348..8de3f9d7e 100644 --- a/lib/includes/hicn/compat.h +++ b/lib/includes/hicn/compat.h @@ -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: @@ -35,14 +35,18 @@ #define HFO_INET 1 << 0 #define HFO_INET6 1 << 1 #define HFO_TCP 1 << 2 -#define HFO_ICMP 1 << 3 -#define HFO_AH 1 << 4 +#define HFO_UDP 1 << 3 +#define HFO_ICMP 1 << 4 +#define HFO_CMPR 1 << 5 +#define HFO_AH 1 << 6 #define _is_ipv4(format) ((format & HFO_INET)) #define _is_ipv6(format) ((format & HFO_INET6) >> 1) #define _is_tcp(format) ((format & HFO_TCP) >> 2) -#define _is_icmp(format) ((format & HFO_ICMP) >> 3) -#define _is_ah(format) ((format & HFO_AH) >> 4) +#define _is_udp(format) ((format & HFO_UDP) >> 3) +#define _is_icmp(format) ((format & HFO_ICMP) >> 4) +#define _is_cmpr(format) ((format & HFO_CMPR) >> 5) +#define _is_ah(format) ((format & HFO_AH) >> 6) typedef enum { @@ -51,13 +55,32 @@ typedef enum HF_INET6_TCP = HFO_INET6 | HFO_TCP, HF_INET_ICMP = HFO_INET | HFO_ICMP, HF_INET6_ICMP = HFO_INET6 | HFO_ICMP, + HF_NEW = HFO_CMPR, + HF_INET_UDP = HFO_INET | HFO_UDP | HFO_CMPR, + HF_INET6_UDP = HFO_INET6 | HFO_UDP | HFO_CMPR, HF_INET_TCP_AH = HFO_INET | HFO_TCP | HFO_AH, HF_INET6_TCP_AH = HFO_INET6 | HFO_TCP | HFO_AH, HF_INET_ICMP_AH = HFO_INET | HFO_ICMP | HFO_AH, HF_INET6_ICMP_AH = HFO_INET6 | HFO_ICMP | HFO_AH, + HF_NEW_AH = HFO_CMPR | HFO_AH, + HF_INET_UDP_AH = HFO_INET | HFO_UDP | HFO_CMPR | HFO_AH, + HF_INET6_UDP_AH = HFO_INET6 | HFO_UDP | HFO_CMPR | HFO_AH, } hicn_format_t; /** + * @brief Add AH header to current format. E.g. if format is IP + TCP, this + * will change it to IP = TCP + AH + * + * @param [in] format - The input format + * @return The format with the AH bit set to 1 + */ +static inline hicn_format_t +hicn_get_ah_format (hicn_format_t format) +{ + return (hicn_format_t) (format | HFO_AH); +} + +/** * Minimum required header length to determine the type and length of a * supposed hICN packet. This should be equal to the maximum value over all * possible hICN packet formats, and less than the minimum possible IP packet @@ -351,18 +374,49 @@ int hicn_packet_get_hoplimit (const hicn_header_t *packet, u8 *hops); */ int hicn_packet_set_hoplimit (hicn_header_t *packet, u8 hops); +/** + * @brief Check if this packet is interest + * + * @param format - hICN format + * @param packet - Packet header + * @return hICN error code + */ +int hicn_packet_is_interest (hicn_format_t format, const hicn_header_t *h, + int *ret); + +/** + * @brief Mark this packet as interest + * + * @param format - hICN format + * @param packet - Packet header + * @return hICN error code + */ +int hicn_packet_set_interest (hicn_format_t format, hicn_header_t *packet); + +/** + * @brief Mark this packet as data + * + * @param format - hICN format + * @param packet - Packet header + * @return hICN error code + */ +int hicn_packet_set_data (hicn_format_t format, hicn_header_t *h); + int hicn_packet_copy_header (hicn_format_t format, const hicn_header_t *packet, hicn_header_t *destination, bool copy_ah); -int hicn_packet_get_lifetime (const hicn_header_t *packet, u32 *lifetime); -int hicn_packet_set_lifetime (hicn_header_t *packet, u32 lifetime); +int hicn_packet_get_lifetime (hicn_format_t format, + const hicn_header_t *packet, u32 *lifetime); +int hicn_packet_set_lifetime (hicn_format_t format, hicn_header_t *packet, + u32 lifetime); int hicn_packet_get_reserved_bits (const hicn_header_t *packet, u8 *reserved_bits); int hicn_packet_set_reserved_bits (hicn_header_t *packet, const u8 reserved_bits); -int hicn_packet_get_payload_type (const hicn_header_t *packet, +int hicn_packet_get_payload_type (hicn_format_t format, + const hicn_header_t *packet, hicn_payload_type_t *payload_type); -int hicn_packet_set_payload_type (hicn_header_t *packet, +int hicn_packet_set_payload_type (hicn_format_t format, hicn_header_t *packet, const hicn_payload_type_t payload_type); int hicn_packet_set_syn (hicn_format_t format, hicn_header_t *packet); @@ -454,10 +508,14 @@ int hicn_data_get_payload_type (const hicn_header_t *data, int hicn_data_set_payload_type (hicn_header_t *data, hicn_payload_type_t payload_type); int hicn_data_reset_for_hash (hicn_format_t format, hicn_header_t *packet); -int hicn_packet_get_signature_gap (hicn_format_t format, - const hicn_header_t *h, uint8_t *bytes); -int hicn_packet_set_signature_gap (hicn_format_t format, hicn_header_t *h, - uint8_t bytes); +int hicn_data_is_last (hicn_format_t format, hicn_header_t *h, int *is_last); +int hicn_data_set_last (hicn_format_t format, hicn_header_t *h); + +int hicn_packet_get_signature_padding (hicn_format_t format, + const hicn_header_t *h, size_t *bytes); +int hicn_packet_set_signature_padding (hicn_format_t format, hicn_header_t *h, + size_t bytes); + #endif /* HICN_COMPAT_H */ /* diff --git a/lib/includes/hicn/error.h b/lib/includes/hicn/error.h index 9303aeb7e..d769ef693 100644 --- a/lib/includes/hicn/error.h +++ b/lib/includes/hicn/error.h @@ -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,21 +24,22 @@ * Error definitions ******************************************************************************/ -#define foreach_libhicn_error \ -_(NONE, 0, "OK") \ -_(UNSPECIFIED, 128, "Unspecified Error") \ -_(NOT_IMPLEMENTED, 180, "Function not yet implemented") \ -_(NOT_HICN, 202, "Non hICN packet") \ -_(UNKNOWN_ADDRESS, 210, "Unknown address") \ -_(INVALID_PARAMETER, 220, "Invalid parameter") \ -_(INVALID_IP_ADDRESS, 221, "Invalid IP address") \ -_(CORRUPTED_PACKET, 222, "Corrupted packet ") \ -_(REWRITE_CKSUM_REQUIRED, 223, "Incremental csum calculation error: cksum required.") \ -_(UNEXPECTED, 298, "Unexpected error") +#define foreach_libhicn_error \ + _ (NONE, 0, "OK") \ + _ (UNSPECIFIED, 128, "Unspecified Error") \ + _ (NOT_IMPLEMENTED, 180, "Function not yet implemented") \ + _ (NOT_HICN, 202, "Non hICN packet") \ + _ (UNKNOWN_ADDRESS, 210, "Unknown address") \ + _ (INVALID_PARAMETER, 220, "Invalid parameter") \ + _ (INVALID_IP_ADDRESS, 221, "Invalid IP address") \ + _ (CORRUPTED_PACKET, 222, "Corrupted packet ") \ + _ (REWRITE_CKSUM_REQUIRED, 223, \ + "Incremental csum calculation error: cksum required.") \ + _ (UNEXPECTED, 298, "Unexpected error") typedef enum { -#define _(a,b,c) HICN_LIB_ERROR_##a = (-b), +#define _(a, b, c) HICN_LIB_ERROR_##a = (-b), foreach_libhicn_error #undef _ HICN_LIB_N_ERROR, @@ -46,7 +47,7 @@ typedef enum extern const char *HICN_LIB_ERROR_STRING[]; -#define hicn_strerror(errno) (char *)(HICN_LIB_ERROR_STRING[-errno]) +#define hicn_strerror(errno) (char *) (HICN_LIB_ERROR_STRING[-errno]) #endif /* HICN_ERROR_H */ diff --git a/lib/includes/hicn/face.h b/lib/includes/hicn/face.h new file mode 100644 index 000000000..31906afab --- /dev/null +++ b/lib/includes/hicn/face.h @@ -0,0 +1,233 @@ +/* + * 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.h + * \brief Face abstraction + */ +#ifndef HICN_FACE_H +#define HICN_FACE_H + +#ifndef SPACES +#define SPACES(x) x +#endif +#ifndef SPACE +#define SPACE 1 +#endif +#ifndef NULLTERM +#define NULLTERM 1 +#endif + +#include <hicn/policy.h> + +#include <hicn/util/ip_address.h> + +/* Netdevice type */ + +#include <net/if.h> // IFNAMSIZ + +#define foreach_netdevice_type \ + _ (UNDEFINED) \ + _ (LOOPBACK) \ + _ (WIRED) \ + _ (WIFI) \ + _ (CELLULAR) \ + _ (VPN) \ + _ (MEMIF) \ + _ (N) + +#define MAXSZ_NETDEVICE_TYPE_ 9 +#define MAXSZ_NETDEVICE_TYPE MAXSZ_NETDEVICE_TYPE_ + NULLTERM + +typedef enum +{ +#define _(x) NETDEVICE_TYPE_##x, + foreach_netdevice_type +#undef _ +} netdevice_type_t; + +extern const char *_netdevice_type_str[]; +#define netdevice_type_str(x) _netdevice_type_str[x] + +#define NETDEVICE_INVALID_INDEX ~0 + +/* Netdevice */ + +/** + * \brief Netdevice type + * + * NOTE + * - This struct cannot be made opaque as it is currently part of face_t + * - We recommand using the API as to keep redundant attributes consistent + */ + +typedef struct +{ + u32 index; + char name[IFNAMSIZ]; +} netdevice_t; + +#define NETDEVICE_EMPTY \ + (netdevice_t) \ + { \ + .index = 0, .name = { 0 } \ + } + +netdevice_t *netdevice_create_from_index (u32 index); +netdevice_t *netdevice_create_from_name (const char *name); +#define netdevice_initialize_from_index netdevice_set_index +#define netdevice_initialize_from_name netdevice_set_name +void netdevice_free (netdevice_t *netdevice); +int netdevice_get_index (const netdevice_t *netdevice, u32 *index); +int netdevice_set_index (netdevice_t *netdevice, u32 index); +int netdevice_get_name (const netdevice_t *netdevice, const char **name); +int netdevice_set_name (netdevice_t *netdevice, const char *name); +int netdevice_update_index (netdevice_t *netdevice); +int netdevice_update_name (netdevice_t *netdevice); +int netdevice_cmp (const netdevice_t *nd1, const netdevice_t *nd2); + +#define NETDEVICE_UNDEFINED_INDEX 0 + +/* Face state */ + +#define foreach_face_state \ + _ (UNDEFINED) \ + _ (DOWN) \ + _ (UP) \ + _ (N) + +#define MAXSZ_FACE_STATE_ 9 +#define MAXSZ_FACE_STATE MAXSZ_FACE_STATE_ + 1 + +typedef enum +{ +#define _(x) FACE_STATE_##x, + foreach_face_state +#undef _ +} face_state_t; + +extern const char *_face_state_str[]; + +#define face_state_str(x) _face_state_str[x] + +/* Face type */ + +#define foreach_face_type \ + _ (UNDEFINED) \ + _ (HICN) \ + _ (HICN_LISTENER) \ + _ (TCP) \ + _ (TCP_LISTENER) \ + _ (UDP) \ + _ (UDP_LISTENER) \ + _ (N) + +#define MAXSZ_FACE_TYPE_ 13 +#define MAXSZ_FACE_TYPE MAXSZ_FACE_TYPE_ + 1 + +typedef enum +{ +#define _(x) FACE_TYPE_##x, + foreach_face_type +#undef _ +} face_type_t; + +typedef enum +{ + FACE_PROTOCOL_HICN, + FACE_PROTOCOL_UDP, + FACE_PROTOCOL_TCP, + FACE_PROTOCOL_UNKNOWN, +} face_protocol_t; + +#define face_type_is_valid(face_type) \ + (((face_type) >= FACE_TYPE_UNDEFINED) && (face_type < FACE_TYPE_N)) +#define face_type_is_defined(face_type) \ + (((face_type) > FACE_TYPE_UNDEFINED) && (face_type < FACE_TYPE_N)) + +extern const char *_face_type_str[]; +#define face_type_str(x) _face_type_str[x] + +face_type_t face_type_from_str (const char *str); + +#ifdef WITH_POLICY +#define MAXSZ_FACE_ \ + MAXSZ_FACE_TYPE_ + 2 * MAXSZ_URL_ + 2 * MAXSZ_FACE_STATE_ + \ + MAXSZ_POLICY_TAGS_ + 7 +#else +#define MAXSZ_FACE_ \ + MAXSZ_FACE_TYPE_ + 2 * MAXSZ_URL_ + 2 * MAXSZ_FACE_STATE_ + 4 +#endif /* WITH_POLICY */ +#define MAXSZ_FACE MAXSZ_FACE_ + 1 + +/* Face */ + +typedef u32 face_id_t; + +typedef struct +{ + face_type_t type; + face_state_t admin_state; + face_state_t state; +#ifdef WITH_POLICY + uint32_t priority; + policy_tags_t tags; /**< \see policy_tag_t */ +#endif /* WITH_POLICY */ + + /* + * Depending on the face type, some of the following fields will be unused + */ + netdevice_t netdevice; + int family; /* To access family independently of face type */ + ip_address_t local_addr; + ip_address_t remote_addr; + u16 local_port; + u16 remote_port; +} face_t; + +int face_initialize (face_t *face); +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); +int face_initialize_udp_sa (face_t *face, const char *interface_name, + const struct sockaddr *local_addr, + const struct sockaddr *remote_addr); + +face_t *face_create (); +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_create_udp_sa (const char *interface_name, + const struct sockaddr *local_addr, + const struct sockaddr *remote_addr); + +int face_finalize (face_t *face); + +void face_free (face_t *face); + +typedef int (*face_cmp_t) (const face_t *f1, const face_t *f2); + +int face_cmp (const face_t *f1, const face_t *f2); + +size_t face_snprintf (char *s, size_t size, const face_t *face); + +policy_tags_t face_get_tags (const face_t *face); +int face_set_tags (face_t *face, policy_tags_t tags); + +face_protocol_t get_protocol (face_type_t face_type); + +#endif /* HICN_FACE_H */ diff --git a/lib/includes/hicn/header.h b/lib/includes/hicn/header.h index b21fe5c84..8af9170f8 100644 --- a/lib/includes/hicn/header.h +++ b/lib/includes/hicn/header.h @@ -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 @@ * NOTE: These structures are used as convenient facade for accessing * the encapsulated headers. They are not written taking compiler padding * into account, then a sizeof() on these struct could not give the expected - * result. For accessing the size of the hicn headers use the macros at the end of - * this file. + * result. For accessing the size of the hicn headers use the macros at the end + * of this file. */ #ifndef HICN_HEADER_H @@ -36,6 +36,11 @@ typedef struct union { _tcp_header_t tcp; + struct + { + _udp_header_t udp; + _new_header_t newhdr; + }; _icmp_header_t icmp; _icmp_wldr_header_t wldr; }; @@ -53,25 +58,36 @@ typedef struct }; struct { + _udp_header_t udp; + _new_header_t newhdr; + _ah_header_t udp_ah; + }; + struct + { _icmp_header_t icmp; _ah_header_t icmp_ah; }; }; } hicn_v6ah_hdr_t; -typedef struct -{ +// For ipv4 we need to use packed structs as fields may be aligned to 64 bits +// (So for instance tcp header may start at byte #24 instead of byte 20) +typedef PACKED (struct { _ipv4_header_t ip; union { _tcp_header_t tcp; + struct + { + _udp_header_t udp; + _new_header_t newhdr; + }; _icmp_header_t icmp; _icmp_wldr_header_t wldr; }; -} hicn_v4_hdr_t; +}) hicn_v4_hdr_t; -typedef struct -{ +typedef PACKED (struct { _ipv4_header_t ip; union { @@ -82,11 +98,17 @@ typedef struct }; struct { + _udp_header_t udp; + _new_header_t newhdr; + _ah_header_t udp_ah; + }; + struct + { _icmp_header_t icmp; _ah_header_t icmp_ah; }; }; -} hicn_v4ah_hdr_t; +}) hicn_v4ah_hdr_t; typedef union { @@ -99,24 +121,19 @@ typedef union hicn_protocol_t protocol; } hicn_header_t; +#define HICN_V6_TCP_HDRLEN (IPV6_HDRLEN + TCP_HDRLEN) +#define HICN_V6_ICMP_HDRLEN (IPV6_HDRLEN + ICMP_HDRLEN) +#define HICN_V6_WLDR_HDRLEN (IPV6_HDRLEN + ICMPWLDR_HDRLEN) -#define HICN_V6_TCP_HDRLEN (IPV6_HDRLEN + TCP_HDRLEN) -#define HICN_V6_ICMP_HDRLEN (IPV6_HDRLEN + ICMP_HDRLEN) -#define HICN_V6_WLDR_HDRLEN (IPV6_HDRLEN + ICMPWLDR_HDRLEN) - -#define HICN_V6_TCP_AH_HDRLEN (HICN_V6_TCP_HDRLEN + AH_HDRLEN) -#define HICN_V6_ICMP_AH_HDRLEN (HICN_V6_ICMP_HDRLEN + AH_HDRLEN) - - -#define HICN_V4_TCP_HDRLEN (IPV4_HDRLEN + TCP_HDRLEN) -#define HICN_V4_ICMP_HDRLEN (IPV4_HDRLEN + ICMP_HDRLEN) -#define HICN_V4_WLDR_HDRLEN (IPV4_HDRLEN + ICMPWLDR_HDRLEN) - -#define HICN_V4_TCP_AH_HDRLEN (HICN_V4_TCP_HDRLEN + AH_HDRLEN) -#define HICN_V4_ICMP_AH_HDRLEN (HICN_V4_ICMP_HDRLEN + AH_HDRLEN) - +#define HICN_V6_TCP_AH_HDRLEN (HICN_V6_TCP_HDRLEN + AH_HDRLEN) +#define HICN_V6_ICMP_AH_HDRLEN (HICN_V6_ICMP_HDRLEN + AH_HDRLEN) +#define HICN_V4_TCP_HDRLEN (IPV4_HDRLEN + TCP_HDRLEN) +#define HICN_V4_ICMP_HDRLEN (IPV4_HDRLEN + ICMP_HDRLEN) +#define HICN_V4_WLDR_HDRLEN (IPV4_HDRLEN + ICMPWLDR_HDRLEN) +#define HICN_V4_TCP_AH_HDRLEN (HICN_V4_TCP_HDRLEN + AH_HDRLEN) +#define HICN_V4_ICMP_AH_HDRLEN (HICN_V4_ICMP_HDRLEN + AH_HDRLEN) #endif /* HICN_HEADER_H */ diff --git a/lib/includes/hicn/hicn.h b/lib/includes/hicn/hicn.h index 749fd4247..4a5b4dd56 100644 --- a/lib/includes/hicn/hicn.h +++ b/lib/includes/hicn/hicn.h @@ -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,15 +31,17 @@ * * [1] Hybrid Information-Centric Networking * L. Muscariello, G. Carofiglio, J. Augé, M. Papalini - * IETF draft (intarea) @ https://tools.ietf.org/html/draft-muscariello-intarea-hicn + * IETF draft (intarea) @ + * https://tools.ietf.org/html/draft-muscariello-intarea-hicn * - * [2] Leveraging ICN in-network control for loss detection and recovery in wireless mobile networks - * G. Carofiglio, L. Muscariello, M. Papalini, N. Rozhnova, X. Zeng - * In proc. ICN'2016, Kyoto, JP + * [2] Leveraging ICN in-network control for loss detection and recovery in + * wireless mobile networks G. Carofiglio, L. Muscariello, M. Papalini, N. + * Rozhnova, X. Zeng In proc. ICN'2016, Kyoto, JP * * [3] Anchorless mobility through hICN * J. Augé, G. Carofiglio, L. Muscariello, M. Papalini - * IETF draft (DMM) @ https://tools.ietf.org/html/draft-auge-dmm-hicn-mobility + * IETF draft (DMM) @ + * https://tools.ietf.org/html/draft-auge-dmm-hicn-mobility * * * [4] MAP-Me : Managing Anchorless Mobility in Content Centric Networking diff --git a/lib/includes/hicn/mapme.h b/lib/includes/hicn/mapme.h index 8fae44530..63a5cd77e 100644 --- a/lib/includes/hicn/mapme.h +++ b/lib/includes/hicn/mapme.h @@ -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,7 +20,7 @@ #ifndef HICN_MAPME_H #define HICN_MAPME_H -#include <stdint.h> // u32 +#include <stdint.h> // u32 #include <stdbool.h> #include "common.h" @@ -32,16 +32,16 @@ */ typedef struct { - /** MAP-Me enabled flag (default: false) */ + /** MAP-Me enabled flag (default: false) */ bool enabled; - /** timescale (T_U parameter) in ms (default: 0 for no notifications) */ + /** timescale (T_U parameter) in ms (default: 0 for no notifications) */ u32 timescale; - /** retransmission timer in ms (default: 50) */ + /** retransmission timer in ms (default: 50) */ u32 retx; - /** - * Discovery enabled flag (default: true, should be true if mandatory is - * notifications are enabled) - */ + /** + * Discovery enabled flag (default: true, should be true if mandatory is + * notifications are enabled) + */ bool discovery; } hicn_mapme_conf_t; @@ -74,37 +74,45 @@ typedef struct seq_t seq; } mapme_params_t; - /* MAP-Me API */ -size_t hicn_mapme_create_packet (u8 * buf, const hicn_prefix_t * prefix, - const mapme_params_t * params); -size_t hicn_mapme_create_ack (u8 * buf, const mapme_params_t * params); -int hicn_mapme_parse_packet (const u8 * packet, hicn_prefix_t * prefix, - mapme_params_t * params); +size_t hicn_mapme_create_packet (u8 *buf, const hicn_prefix_t *prefix, + const mapme_params_t *params); +size_t hicn_mapme_create_ack (u8 *buf, const mapme_params_t *params); +int hicn_mapme_parse_packet (const u8 *packet, hicn_prefix_t *prefix, + mapme_params_t *params); /* Implementation & parsing : ICMP Redirect */ #define HICN_MAPME_ACK_FLAG (0x20 | 0x60) -#define HICN_MAPME_ICMP_TYPE_IPV4 5 -#define HICN_MAPME_ICMP_TYPE_IPV6 137 -#define HICN_MAPME_ICMP_TYPE_ACK_IPV4 (HICN_MAPME_ICMP_TYPE_IPV4 | HICN_MAPME_ACK_FLAG) -#define HICN_MAPME_ICMP_TYPE_ACK_IPV6 (HICN_MAPME_ICMP_TYPE_IPV6 | HICN_MAPME_ACK_FLAG) -#define HICN_MAPME_ICMP_CODE 0 /* Redirect Datagrams for the Network (or subnet) */ - -#define HICN_MAPME_TYPE_IS_IU(type) ((type == HICN_MAPME_ICMP_TYPE_IPV4) || (type == HICN_MAPME_ICMP_TYPE_IPV6)) -#define HICN_MAPME_TYPE_IS_IU_ACK(type) ((type == HICN_MAPME_ICMP_TYPE_ACK_IPV4) || (type == HICN_MAPME_ICMP_TYPE_ACK_IPV6)) - -#define HICN_MAPME_IS_IU(type, code) (HICN_MAPME_TYPE_IS_IU(type) && (code == HICN_MAPME_ICMP_CODE)) -#define HICN_MAPME_IS_ACK(type, code) (HICN_MAPME_TYPE_IS_IU_ACK(type) && (code == HICN_MAPME_ICMP_CODE)) - -#define HICN_IS_MAPME(type, code) (HICN_MAPME_IS_IU(type, code) || HICN_MAPME_IS_ACK(type, code)) +#define HICN_MAPME_ICMP_TYPE_IPV4 5 +#define HICN_MAPME_ICMP_TYPE_IPV6 137 +#define HICN_MAPME_ICMP_TYPE_ACK_IPV4 \ + (HICN_MAPME_ICMP_TYPE_IPV4 | HICN_MAPME_ACK_FLAG) +#define HICN_MAPME_ICMP_TYPE_ACK_IPV6 \ + (HICN_MAPME_ICMP_TYPE_IPV6 | HICN_MAPME_ACK_FLAG) +#define HICN_MAPME_ICMP_CODE \ + 0 /* Redirect Datagrams for the Network (or subnet) */ + +#define HICN_MAPME_TYPE_IS_IU(type) \ + ((type == HICN_MAPME_ICMP_TYPE_IPV4) || (type == HICN_MAPME_ICMP_TYPE_IPV6)) +#define HICN_MAPME_TYPE_IS_IU_ACK(type) \ + ((type == HICN_MAPME_ICMP_TYPE_ACK_IPV4) || \ + (type == HICN_MAPME_ICMP_TYPE_ACK_IPV6)) + +#define HICN_MAPME_IS_IU(type, code) \ + (HICN_MAPME_TYPE_IS_IU (type) && (code == HICN_MAPME_ICMP_CODE)) +#define HICN_MAPME_IS_ACK(type, code) \ + (HICN_MAPME_TYPE_IS_IU_ACK (type) && (code == HICN_MAPME_ICMP_CODE)) + +#define HICN_IS_MAPME(type, code) \ + (HICN_MAPME_IS_IU (type, code) || HICN_MAPME_IS_ACK (type, code)) /* Fast check for ACK flag */ #define HICN_MAPME_IS_ACK_FAST(icmp_type) (icmp_type & HICN_MAPME_ACK_FLAG) /* Default TTL */ -#define HICN_MAPME_TTL 255 // typical for redirect (ref?) +#define HICN_MAPME_TTL 255 // typical for redirect (ref?) /* * The length of the MAPME4 header struct must be 120 bytes. @@ -143,8 +151,8 @@ typedef union hicn_mapme_v6_header_t v6; } hicn_mapme_header_t; -#define HICN_MAPME_V4_HDRLEN sizeof(hicn_mapme_v4_header_t) -#define HICN_MAPME_V6_HDRLEN sizeof(hicn_mapme_v6_header_t) +#define HICN_MAPME_V4_HDRLEN sizeof (hicn_mapme_v4_header_t) +#define HICN_MAPME_V6_HDRLEN sizeof (hicn_mapme_v6_header_t) static_assert (EXPECTED_MAPME_V4_HDRLEN == HICN_MAPME_V4_HDRLEN, "Size of MAPME_V4 struct does not match its expected size."); diff --git a/lib/includes/hicn/name.h b/lib/includes/hicn/name.h index d5202068b..f85b0bc3f 100644 --- a/lib/includes/hicn/name.h +++ b/lib/includes/hicn/name.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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,7 +26,7 @@ #include <stdbool.h> #ifndef _WIN32 -#include <netinet/in.h> // struct sockadd +#include <netinet/in.h> // struct sockadd #endif #include <hicn/util/ip_address.h> #include "common.h" @@ -35,12 +35,16 @@ * hICN names ******************************************************************************/ -#define TCP_SEQNO_LEN 4 /* bytes */ +#define TCP_SEQNO_LEN 4 /* bytes */ #define HICN_V4_PREFIX_LEN IPV4_ADDR_LEN #define HICN_V6_PREFIX_LEN IPV6_ADDR_LEN -#define HICN_SEGMENT_LEN TCP_SEQNO_LEN -#define HICN_V6_NAME_LEN (HICN_V6_PREFIX_LEN + HICN_SEGMENT_LEN) /* 20 bytes */ -#define HICN_V4_NAME_LEN (HICN_V4_PREFIX_LEN + HICN_SEGMENT_LEN) /* 8 bytes */ +#define HICN_SEGMENT_LEN TCP_SEQNO_LEN +#define HICN_V6_NAME_LEN \ + (HICN_V6_PREFIX_LEN + HICN_SEGMENT_LEN) /* 20 bytes \ + */ +#define HICN_V4_NAME_LEN \ + (HICN_V4_PREFIX_LEN + HICN_SEGMENT_LEN) /* 8 bytes \ + */ /* Prefix */ @@ -48,7 +52,7 @@ typedef u32 hicn_name_suffix_t; typedef struct { - ip46_address_t name; + ip_address_t name; u8 len; } hicn_prefix_t; @@ -58,85 +62,17 @@ typedef struct * A name is a prefix + a segment name (suffix) */ -typedef union -{ - struct - { - union - { - u32 prefix; - u8 prefix_as_u8[4]; - ip4_address_t prefix_as_ip4; - }; - hicn_name_suffix_t suffix; - }; - u8 buffer[HICN_V4_NAME_LEN]; -} hicn_v4_name_t; - -typedef union -{ - struct - { - union - { - u64 prefix[2]; - u8 prefix_as_u8[16]; - ip6_address_t prefix_as_ip6; - }; - hicn_name_suffix_t suffix; - }; - u8 buffer[HICN_V6_NAME_LEN]; -} hicn_v6_name_t; - -#ifndef HICN_VPP_PLUGIN -#define HICN_NAME_COMPONENT_SIZE 2 - -typedef struct -{ - struct iovec buffers[HICN_NAME_COMPONENT_SIZE]; -} hicn_iov_name_t; - -#define UNSPEC 1 << 0 -#define HNT_CONTIGUOUS 1 << 1 -#define HNT_IOV 1 << 2 -#define HNT_INET 1 << 3 -#define HNT_INET6 1 << 4 - -typedef enum -{ - HNT_UNSPEC = UNSPEC, - HNT_CONTIGUOUS_V4 = HNT_CONTIGUOUS | HNT_INET, - HNT_CONTIGUOUS_V6 = HNT_CONTIGUOUS | HNT_INET6, - HNT_IOV_V4 = HNT_IOV | HNT_INET, - HNT_IOV_V6 = HNT_IOV | HNT_INET6, -} hicn_name_type_t; -#endif /* HICN_VPP_PLUGIN */ - typedef struct { -#ifndef HICN_VPP_PLUGIN - hicn_name_type_t type; - u8 len; -#endif /* HICN_VPP_PLUGIN */ - union - { - hicn_v4_name_t ip4; - hicn_v6_name_t ip6; - ip46_address_t ip46; -#ifndef HICN_VPP_PLUGIN - hicn_iov_name_t iov; - u8 buffer[HICN_V6_NAME_LEN]; -#endif /* HICN_VPP_PLUGIN */ - }; + ip_address_t prefix; + hicn_name_suffix_t suffix; } hicn_name_t; -#ifndef HICN_VPP_PLUGIN -#define _is_unspec(name) ((name->type & UNSPEC)) -#define _is_contiguous(name) ((name->type & HNT_CONTIGUOUS) >> 1) -#define _is_iov(name) ((name->type & HNT_IOV) >> 2) -#define _is_inet4(name) ((name->type & HNT_INET) >> 3) -#define _is_inet6(name) ((name->type & HNT_INET6) >> 4) -#endif /* HICN_VPP_PLUGIN */ +#define _is_unspec(name) \ + (((name)->prefix.pad[0] | (name)->prefix.pad[1] | (name)->prefix.pad[2] | \ + (name)->prefix.v4.as_u32) == 0) +#define _is_inet4(name) (ip_address_is_v4 (&name->prefix)) +#define _is_inet6(name) (!_is_inet4 (name)) /** * @brief Create an hICN name from IP address in presentation format @@ -145,7 +81,7 @@ typedef struct * @param [out] Resulting hICN name * @return hICN error code */ -int hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name); +int hicn_name_create (const char *ip_address, u32 id, hicn_name_t *name); /** * @brief Create an hICN name from IP address @@ -154,28 +90,21 @@ int hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name); * @param [out] Resulting - hICN name * @return hICN error code */ -int hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id, - hicn_name_t * name); - -/** - * @brief Returns the length of an hICN name - * @param [in] name - hICN name - * @return Name length - */ -u8 hicn_name_get_length (const hicn_name_t * name); +int hicn_name_create_from_ip_prefix (const ip_prefix_t *prefix, u32 id, + hicn_name_t *name); /** * @brief Compare two hICN names * @param [in] name_1 - First name to compare * @param [in] name_2 - Second name to compare - * @param [in] consider_segment - Flag indicating whether the segment part has to be - * considered + * @param [in] consider_segment - Flag indicating whether the segment part has + * to be considered * @return An integer less than, equal to, or greater than zero if name_1 is * found, respectively, to be lest than, to match, or be greater than name_2 * based on numeric order. */ -int hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2, - bool consider_segment); +int hicn_name_compare (const hicn_name_t *name_1, const hicn_name_t *name_2, + bool consider_segment); /** * @brief Provides a 32-bit hash of an hICN name @@ -184,7 +113,7 @@ int hicn_name_compare (const hicn_name_t * name_1, const hicn_name_t * name_2, * @param [in] consider_suffix - Consider the suffix in the hash computation * @return hICN error code */ -int hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix); +int hicn_name_hash (const hicn_name_t *name, u32 *hash, bool consider_suffix); /** * @brief Test whether an hICN name is empty @@ -192,7 +121,7 @@ int hicn_name_hash (const hicn_name_t * name, u32 * hash, bool consider_suffix); * @return 0 if the name is empty, any other value otherwise (implementation * returns 1) */ -int hicn_name_empty (hicn_name_t * name); +int hicn_name_empty (hicn_name_t *name); /** * @brief Copy an hICN name @@ -200,7 +129,7 @@ int hicn_name_empty (hicn_name_t * name); * @param [in] src - Source name to copy * @return hICN error code */ -int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src); +int hicn_name_copy (hicn_name_t *dst, const hicn_name_t *src); /** * @brief Copy an hICN name to a buffer @@ -209,8 +138,7 @@ int hicn_name_copy (hicn_name_t * dst, const hicn_name_t * src); * @param [in] copy_suffix - Flag indicating whether the suffix has to be * considered */ -int hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src, - bool copy_suffix); +int hicn_name_copy_prefix_to_destination (u8 *dst, const hicn_name_t *src); /** * @brief Sets the segment part of an hICN name @@ -218,7 +146,7 @@ int hicn_name_copy_to_destination (u8 * dst, const hicn_name_t * src, * @param [in] seq_number - Segment identifier * @return hICN error code */ -int hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number); +int hicn_name_set_seq_number (hicn_name_t *name, u32 seq_number); /** * @brief Retrieves the segment part of an hICN name @@ -226,7 +154,7 @@ int hicn_name_set_seq_number (hicn_name_t * name, u32 seq_number); * @param [in] seq_number - Segment identifier * @return hICN error code */ -int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number); +int hicn_name_get_seq_number (const hicn_name_t *name, u32 *seq_number); /** * @brief Convert an hICN name to a socket address @@ -234,8 +162,8 @@ int hicn_name_get_seq_number (const hicn_name_t * name, u32 * seq_number); * @param [out] ip_address - Resulting socket address * @return hICN error code */ -int hicn_name_to_sockaddr_address (const hicn_name_t * name, - struct sockaddr *ip_address); +int hicn_name_to_sockaddr_address (const hicn_name_t *name, + struct sockaddr *ip_address); /** * @brief Convert an hICN name to an IP address @@ -243,8 +171,7 @@ int hicn_name_to_sockaddr_address (const hicn_name_t * name, * @param [out] ip_address - Resulting IP address * @return hICN error code */ -int hicn_name_to_ip_prefix (const hicn_name_t * name, - ip_prefix_t * ip_prefix); +int hicn_name_to_ip_prefix (const hicn_name_t *name, ip_prefix_t *ip_prefix); /** * @brief Convert an hICN name to presentation format @@ -253,7 +180,7 @@ int hicn_name_to_ip_prefix (const hicn_name_t * name, * @param [in] len - Number of bytes available in the buffer * @return hICN error code */ -int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len); +int hicn_name_ntop (const hicn_name_t *src, char *dst, size_t len); /** * @brief Convert an hICN name from presentation format @@ -261,7 +188,7 @@ int hicn_name_ntop (const hicn_name_t * src, char *dst, size_t len); * @param [out] dst - Resulting name * @return hICN error code */ -int hicn_name_pton (const char *src, hicn_name_t * dst); +int hicn_name_pton (const char *src, hicn_name_t *dst); /** * @brief Returns the IP address family of an hICN name @@ -269,7 +196,7 @@ int hicn_name_pton (const char *src, hicn_name_t * dst); * @param [out] family - Resulting IP address family (AF_INET or AF_INET6) * @return hICN error code */ -int hicn_name_get_family (const hicn_name_t * name, int *family); +int hicn_name_get_family (const hicn_name_t *name, int *family); /** * @brief Creates an hICN prefix from an IP address @@ -277,8 +204,8 @@ int hicn_name_get_family (const hicn_name_t * name, int *family); * @param [out] prefix - Resulting prefix * @return hICN error code */ -int hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, - hicn_prefix_t * prefix); +int hicn_prefix_create_from_ip_prefix (const ip_prefix_t *ip_prefix, + hicn_prefix_t *prefix); #endif /* HICN_NAME_H */ diff --git a/lib/includes/hicn/ops.h b/lib/includes/hicn/ops.h index e5b1c088c..e9eebc76c 100644 --- a/lib/includes/hicn/ops.h +++ b/lib/includes/hicn/ops.h @@ -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: @@ -23,6 +23,7 @@ #include <stdlib.h> +#include "common.h" #include "error.h" #include "header.h" #include "name.h" @@ -52,7 +53,7 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*get_interest_locator) (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address); + ip_address_t *ip_address); /** * @brief Sets an Interest locator @@ -62,7 +63,7 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*set_interest_locator) (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address); + const ip_address_t *ip_address); /** * @brief Retrieves an Interest name @@ -105,6 +106,15 @@ typedef struct hicn_ops_s const hicn_name_suffix_t *suffix); /** + * @brief Check if packet is an interest + * @param [in] type - hICN packet type + * @param [in] h - Buffer holding the Interest packet + * @param [out] h - 1 if interest, 0 otherwise + * @return hICN error code + */ + int (*is_interest) (hicn_type_t type, const hicn_protocol_t *h, int *ret); + + /** * @brief Set flag to mark current packet as interest * @param [in] type - hICN packet type * @param [in,out] h - Buffer holding the Interest packet @@ -136,7 +146,7 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*get_data_locator) (hicn_type_t type, const hicn_protocol_t *h, - ip46_address_t *ip_address); + ip_address_t *ip_address); /** * @brief Sets a Data locator @@ -146,7 +156,7 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*set_data_locator) (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *ip_address); + const ip_address_t *ip_address); /** * @brief Retrieves a Data name @@ -282,8 +292,8 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*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); /** * @brief Rewrite a Data packet header (locator + pathlabel) @@ -298,9 +308,8 @@ typedef struct hicn_ops_s * @return hICN error code */ int (*rewrite_data) (hicn_type_t type, hicn_protocol_t *h, - const ip46_address_t *addr_new, - ip46_address_t *addr_old, const hicn_faceid_t face_id, - u8 reset_pl); + const ip_address_t *addr_new, ip_address_t *addr_old, + const hicn_faceid_t face_id, u8 reset_pl); /** * @brief Return the packet length @@ -373,27 +382,27 @@ typedef struct hicn_ops_s size_t signature_size); /** - * @brief Sets an Interest or Data signature gap between maximum size and + * @brief Sets an Interest or Data signature padding between maximum size and * real size * @param [in] type - hICN packet type * @param [in,out] h - Buffer holding the Interest or Data packet * @param [in] signature_size - Signature size to set * @return hICN error code */ - int (*set_signature_gap) (hicn_type_t type, hicn_protocol_t *h, - uint8_t signature_gap); + int (*set_signature_padding) (hicn_type_t type, hicn_protocol_t *h, + size_t signature_padding); /** - * @brief gets an Interest or Data signature gap between maximum size and + * @brief gets an Interest or Data signature padding between maximum size and * real size * @param [in] type - hICN packet type * @param [in,out] h - Buffer holding the Interest or Data packet - * @param [in] signature_size - retrieve the gap between maximum size and + * @param [in] signature_size - retrieve the padding between maximum size and * real size * @return hICN error code */ - int (*get_signature_gap) (hicn_type_t type, const hicn_protocol_t *h, - uint8_t *signature_gap); + int (*get_signature_padding) (hicn_type_t type, const hicn_protocol_t *h, + size_t *signature_padding); /** * @brief Gets the signature timestamp @@ -464,6 +473,44 @@ typedef struct hicn_ops_s */ int (*get_signature) (hicn_type_t type, hicn_protocol_t *h, uint8_t **signature); + + /** + * @brief Set payload type of the packet + * @param [in] type - hICN packet type + * @param [in,out] h - Buffer holding the Interest or Data packet + * @param [in] payload_type - The payload type of this packet + * @return hICN error code + */ + int (*set_payload_type) (hicn_type_t type, hicn_protocol_t *h, + hicn_payload_type_t payload_type); + + /** + * @brief Get payload type from the packet + * @param [in] type - hICN packet type + * @param [in] h - Buffer holding the Interest or Data packet + * @param [out] payload_type - The payload type of this packet + * @return hICN error code + */ + int (*get_payload_type) (hicn_type_t type, const hicn_protocol_t *h, + hicn_payload_type_t *payload_type); + + /** + * @brief Check if data packet is last one. + * @param [in] type - hICN packet type + * @param [in] h - Buffer holding the Interest or Data packet + * @param [out] is_last - 1 if last data, 0 otherwise + * @return hICN error code + */ + int (*is_last_data) (hicn_type_t type, const hicn_protocol_t *h, + int *is_last); + + /** + * @brief Mark data packet as last + * @param [in] type - hICN packet type + * @param [in, out] h - Buffer holding the Interest or Data packet + * @return hICN error code + */ + int (*set_last_data) (hicn_type_t type, hicn_protocol_t *h); } hicn_ops_t; #define DECLARE_HICN_OPS(protocol) \ @@ -477,6 +524,7 @@ typedef struct hicn_ops_s protocol##_get_interest_name_suffix), \ ATTR_INIT (set_interest_name_suffix, \ protocol##_set_interest_name_suffix), \ + ATTR_INIT (is_interest, protocol##_is_interest), \ ATTR_INIT (mark_packet_as_interest, protocol##_mark_packet_as_interest), \ ATTR_INIT (mark_packet_as_data, protocol##_mark_packet_as_data), \ ATTR_INIT (reset_interest_for_hash, protocol##_reset_interest_for_hash), \ @@ -502,6 +550,8 @@ typedef struct hicn_ops_s ATTR_INIT (get_header_length, protocol##_get_header_length), \ ATTR_INIT (get_payload_length, protocol##_get_payload_length), \ ATTR_INIT (set_payload_length, protocol##_set_payload_length), \ + ATTR_INIT (get_payload_type, protocol##_get_payload_type), \ + ATTR_INIT (set_payload_type, protocol##_set_payload_type), \ ATTR_INIT (get_signature_size, protocol##_get_signature_size), \ ATTR_INIT (get_signature_timestamp, protocol##_get_signature_timestamp), \ ATTR_INIT (set_signature_timestamp, protocol##_set_signature_timestamp), \ @@ -512,9 +562,11 @@ typedef struct hicn_ops_s ATTR_INIT (get_key_id, protocol##_get_key_id), \ ATTR_INIT (set_key_id, protocol##_set_key_id), \ ATTR_INIT (get_signature, protocol##_get_signature), \ - ATTR_INIT (set_signature_gap, protocol##_set_signature_gap), \ + ATTR_INIT (set_signature_padding, protocol##_set_signature_padding), \ ATTR_INIT (set_signature_size, protocol##_set_signature_size), \ - ATTR_INIT (get_signature_gap, protocol##_get_signature_gap), \ + ATTR_INIT (get_signature_padding, protocol##_get_signature_padding), \ + ATTR_INIT (is_last_data, protocol##_is_last_data), \ + ATTR_INIT (set_last_data, protocol##_set_last_data), \ } /** @@ -529,7 +581,7 @@ extern const hicn_ops_t *const hicn_ops_vft[]; * NOTE : we cannot use a shift operation as IPPROTO_NONE != 0 (and 0 is * IPv4...) */ -always_inline hicn_type_t +static inline hicn_type_t TYPE_POP (hicn_type_t type) { #ifndef _WIN32 @@ -544,7 +596,7 @@ TYPE_POP (hicn_type_t type) #endif } -always_inline hicn_protocol_t * +static inline hicn_protocol_t * PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) { size_t header_length; @@ -573,14 +625,14 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) #define DECLARE_get_interest_locator(protocol, error) \ int protocol##_get_interest_locator ( \ - hicn_type_t type, const hicn_protocol_t *h, ip46_address_t *ip_address) \ + hicn_type_t type, const hicn_protocol_t *h, ip_address_t *ip_address) \ { \ return HICN_LIB_ERROR_##error; \ } #define DECLARE_set_interest_locator(protocol, error) \ int protocol##_set_interest_locator (hicn_type_t type, hicn_protocol_t *h, \ - const ip46_address_t *ip_address) \ + const ip_address_t *ip_address) \ { \ return HICN_LIB_ERROR_##error; \ } @@ -613,6 +665,13 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) return HICN_LIB_ERROR_##error; \ } +#define DECLARE_is_interest(protocol, error) \ + int protocol##_is_interest (hicn_type_t type, const hicn_protocol_t *h, \ + int *ret) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + #define DECLARE_mark_packet_as_interest(protocol, error) \ int protocol##_mark_packet_as_interest (hicn_type_t type, \ hicn_protocol_t *h) \ @@ -635,14 +694,14 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) #define DECLARE_get_data_locator(protocol, error) \ int protocol##_get_data_locator ( \ - hicn_type_t type, const hicn_protocol_t *h, ip46_address_t *ip_address) \ + hicn_type_t type, const hicn_protocol_t *h, ip_address_t *ip_address) \ { \ return HICN_LIB_ERROR_##error; \ } #define DECLARE_set_data_locator(protocol, error) \ int protocol##_set_data_locator (hicn_type_t type, hicn_protocol_t *h, \ - const ip46_address_t *ip_address) \ + const ip_address_t *ip_address) \ { \ return HICN_LIB_ERROR_##error; \ } @@ -732,16 +791,16 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) #define DECLARE_rewrite_interest(protocol, error) \ int protocol##_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_##error; \ } #define DECLARE_rewrite_data(protocol, error) \ int protocol##_rewrite_data ( \ - hicn_type_t type, hicn_protocol_t *h, const ip46_address_t *addr_new, \ - ip46_address_t *addr_old, const hicn_faceid_t face_id, u8 reset_pl) \ + 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 HICN_LIB_ERROR_##error; \ } @@ -781,6 +840,21 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) return HICN_LIB_ERROR_##error; \ } +#define DECLARE_get_payload_type(protocol, error) \ + int protocol##_get_payload_type (hicn_type_t type, \ + const hicn_protocol_t *h, \ + hicn_payload_type_t *payload_type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_payload_type(protocol, error) \ + int protocol##_set_payload_type (hicn_type_t type, hicn_protocol_t *h, \ + hicn_payload_type_t payload_type) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + #define DECLARE_get_signature_size(protocol, error) \ int protocol##_get_signature_size ( \ hicn_type_t type, const hicn_protocol_t *h, size_t *signature_size) \ @@ -795,16 +869,16 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) return HICN_LIB_ERROR_##error; \ } -#define DECLARE_set_signature_gap(protocol, error) \ - int protocol##_set_signature_gap (hicn_type_t type, hicn_protocol_t *h, \ - uint8_t signature_size) \ +#define DECLARE_set_signature_padding(protocol, error) \ + int protocol##_set_signature_padding (hicn_type_t type, hicn_protocol_t *h, \ + size_t padding) \ { \ return HICN_LIB_ERROR_##error; \ } -#define DECLARE_get_signature_gap(protocol, error) \ - int protocol##_get_signature_gap ( \ - hicn_type_t type, const hicn_protocol_t *h, uint8_t *signature_size) \ +#define DECLARE_get_signature_padding(protocol, error) \ + int protocol##_get_signature_padding ( \ + hicn_type_t type, const hicn_protocol_t *h, size_t *padding) \ { \ return HICN_LIB_ERROR_##error; \ } @@ -860,6 +934,19 @@ PAYLOAD (hicn_type_t type, const hicn_protocol_t *h) return HICN_LIB_ERROR_##error; \ } +#define DECLARE_is_last_data(protocol, error) \ + int protocol##_is_last_data (hicn_type_t type, const hicn_protocol_t *h, \ + int *is_last) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + +#define DECLARE_set_last_data(protocol, error) \ + int protocol##_set_last_data (hicn_type_t type, hicn_protocol_t *h) \ + { \ + return HICN_LIB_ERROR_##error; \ + } + #endif /* HICN_OPS_H */ /* diff --git a/lib/includes/hicn/policy.h b/lib/includes/hicn/policy.h index e29888bd2..f39fa9272 100644 --- a/lib/includes/hicn/policy.h +++ b/lib/includes/hicn/policy.h @@ -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: @@ -30,37 +30,44 @@ /* POLICY TAG */ -#define foreach_policy_tag \ - /* Interface type */ \ - _(WIRED, 'E') \ - _(WIFI, 'W') \ - _(CELLULAR, 'C') \ - /* QoS */ \ - _(BEST_EFFORT, 'b') \ - _(REALTIME, 'r') \ - _(MULTIPATH, 'M') \ - /* Security */ \ - _(TRUSTED, 'T') - -typedef enum { -#define _(x, y) POLICY_TAG_ ## x, -foreach_policy_tag +#define foreach_policy_tag \ + /* Interface type */ \ + _ (WIRED, 'E') \ + _ (WIFI, 'W') \ + _ (CELLULAR, 'C') \ + /* QoS */ \ + _ (BEST_EFFORT, 'b') \ + _ (REALTIME, 'r') \ + _ (MULTIPATH, 'M') \ + /* Security */ \ + _ (TRUSTED, 'T') + +typedef enum +{ +#define _(x, y) POLICY_TAG_##x, + foreach_policy_tag #undef _ POLICY_TAG_N } policy_tag_t; +#define IS_VALID_POLICY_TAG(x) (x != POLICY_TAG_N) + #define MAXSZ_POLICY_TAG_ 11 -#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 +#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 -extern const char * policy_tag_str[]; +extern const char *policy_tag_str[]; extern const char policy_tag_short_str[]; -static inline -policy_tag_t -policy_tag_from_str(const char * str) +static inline policy_tag_t +policy_tag_from_str (const char *str) { -#define _(x, y) if (strcasecmp(str, policy_tag_str[POLICY_TAG_ ## x] ) == 0) { return POLICY_TAG_ ## x; } else -foreach_policy_tag +#define _(x, y) \ + if (strcasecmp (str, policy_tag_str[POLICY_TAG_##x]) == 0) \ + { \ + return POLICY_TAG_##x; \ + } \ + else + foreach_policy_tag #undef _ return POLICY_TAG_N; } @@ -69,182 +76,188 @@ foreach_policy_tag typedef int policy_tags_t; -static inline -void policy_tags_add(policy_tags_t * tags, policy_tag_t tag) +static inline void +policy_tags_add (policy_tags_t *tags, policy_tag_t tag) { - *tags |= (1 << tag); + *tags |= (1 << tag); } -static inline -void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag) +static inline void +policy_tags_remove (policy_tags_t *tags, policy_tag_t tag) { - *tags &= ~(1 << tag); + *tags &= ~(1 << tag); } -static inline -int policy_tags_has(policy_tags_t tags, policy_tag_t tag) +static inline int +policy_tags_has (policy_tags_t tags, policy_tag_t tag) { - return tags & (1 << tag); + return tags & (1 << tag); } -static inline -void policy_tags_union(policy_tags_t * tags, policy_tags_t * tags_to_union) +static inline void +policy_tags_union (policy_tags_t *tags, policy_tags_t *tags_to_union) { -#define _(x, y) *tags |= policy_tags_has(*tags_to_union, POLICY_TAG_ ## x) ? (1 << POLICY_TAG_ ## x) : 0; -foreach_policy_tag +#define _(x, y) \ + *tags |= policy_tags_has (*tags_to_union, POLICY_TAG_##x) ? \ + (1 << POLICY_TAG_##x) : \ + 0; + foreach_policy_tag #undef _ } #define POLICY_TAGS_EMPTY 0 -static inline -int -policy_tags_snprintf(char * s, size_t size, policy_tags_t tags) +static inline int +policy_tags_snprintf (char *s, size_t size, policy_tags_t tags) { -#define _(x, y) s[POLICY_TAG_ ## x] = policy_tags_has(tags, POLICY_TAG_ ## x) ? y : '.'; -foreach_policy_tag +#define _(x, y) \ + s[POLICY_TAG_##x] = policy_tags_has (tags, POLICY_TAG_##x) ? y : '.'; + foreach_policy_tag #undef _ s[POLICY_TAG_N] = '\0'; - return POLICY_TAG_N + 1; + return POLICY_TAG_N + 1; } #define MAXSZ_POLICY_TAGS_ POLICY_TAG_N + 1 -#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1 +#define MAXSZ_POLICY_TAGS MAXSZ_POLICY_TAGS_ + 1 /* POLICY STATE */ /* TODO vs. weight */ -#define foreach_policy_state \ - _(NEUTRAL) \ - _(REQUIRE) \ - _(PREFER) \ - _(AVOID) \ - _(PROHIBIT) \ - _(N) - -typedef enum { -#define _(x) POLICY_STATE_ ## x, -foreach_policy_state +#define foreach_policy_state \ + _ (NEUTRAL) \ + _ (REQUIRE) \ + _ (PREFER) \ + _ (AVOID) \ + _ (PROHIBIT) \ + _ (N) + +typedef enum +{ +#define _(x) POLICY_STATE_##x, + foreach_policy_state #undef _ } policy_state_t; #define MAXSZ_POLICY_STATE_ 8 -#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 +#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 + +extern const char *policy_state_str[]; -extern const char * policy_state_str[]; +#define policy_state_str(x) policy_state_str[x] +policy_state_t policy_state_from_str (const char *str); /* POLICY TAG STATE */ -typedef struct { - policy_state_t state; - uint8_t disabled; +typedef struct +{ + policy_state_t state; + uint8_t disabled; } policy_tag_state_t; #define MAXSZ_POLICY_TAG_STATE_ 8 -#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1 - -int policy_tag_state_snprintf(char * s, size_t size, const policy_tag_state_t * tag_state); +#define MAXSZ_POLICY_TAG_STATE MAXSZ_POLICY_TAG_STATE_ + 1 +int policy_tag_state_snprintf (char *s, size_t size, + const policy_tag_state_t *tag_state); /* INTERFACE STATS */ -typedef struct { - float throughput; - float latency; - float loss_rate; +typedef struct +{ + float throughput; + float latency; + float loss_rate; } interface_stats_t; -#define INTERFACE_STATS_NONE { \ - .throughput = 0, \ - .latency = 0, \ - .loss_rate = 0, \ -} - +#define INTERFACE_STATS_EMPTY \ + { \ + .throughput = 0, .latency = 0, .loss_rate = 0, \ + } /* POLICY STATS */ -typedef struct { - interface_stats_t wired; - interface_stats_t wifi; - interface_stats_t cellular; - interface_stats_t all; +typedef struct +{ + interface_stats_t wired; + interface_stats_t wifi; + interface_stats_t cellular; + interface_stats_t all; } policy_stats_t; -#define POLICY_STATS_NONE { \ - .wired = INTERFACE_STATS_NONE, \ - .wifi = INTERFACE_STATS_NONE, \ - .cellular = INTERFACE_STATS_NONE, \ - .all = INTERFACE_STATS_NONE, \ -} +#define POLICY_STATS_EMPTY \ + (policy_stats_t) \ + { \ + .wired = INTERFACE_STATS_EMPTY, .wifi = INTERFACE_STATS_EMPTY, \ + .cellular = INTERFACE_STATS_EMPTY, .all = INTERFACE_STATS_EMPTY, \ + } -typedef struct { - uint32_t num_packets; - uint32_t num_bytes; - uint32_t num_losses; - uint32_t latency_idle; +typedef struct +{ + uint32_t num_packets; + uint32_t num_bytes; + uint32_t num_losses; + uint32_t latency_idle; } interface_counters_t; -#define INTERFACE_COUNTERS_NONE { \ - .num_packets = 0, \ - .num_bytes = 0, \ - .num_losses = 0, \ - .latency_idle = 0, \ -} +#define INTERFACE_COUNTERS_EMPTY \ + { \ + .num_packets = 0, .num_bytes = 0, .num_losses = 0, .latency_idle = 0, \ + } -typedef struct { - interface_counters_t wired; - interface_counters_t wifi; - interface_counters_t cellular; - interface_counters_t all; - uint64_t last_update; +typedef struct +{ + interface_counters_t wired; + interface_counters_t wifi; + interface_counters_t cellular; + interface_counters_t all; + uint64_t last_update; } policy_counters_t; -#define POLICY_COUNTERS_NONE (policy_counters_t) { \ - .wired = INTERFACE_COUNTERS_NONE, \ - .wifi = INTERFACE_COUNTERS_NONE, \ - .cellular = INTERFACE_COUNTERS_NONE, \ - .all = INTERFACE_COUNTERS_NONE, \ - .last_update = 0, \ -} +#define POLICY_COUNTERS_EMPTY \ + (policy_counters_t) \ + { \ + .wired = INTERFACE_COUNTERS_EMPTY, .wifi = INTERFACE_COUNTERS_EMPTY, \ + .cellular = INTERFACE_COUNTERS_EMPTY, .all = INTERFACE_COUNTERS_EMPTY, \ + .last_update = 0, \ + } /* POLICY */ #define APP_NAME_LEN 128 -typedef struct { - char app_name[APP_NAME_LEN]; - policy_tag_state_t tags[POLICY_TAG_N]; - policy_stats_t stats; +typedef struct +{ + char app_name[APP_NAME_LEN]; + policy_tag_state_t tags[POLICY_TAG_N]; + policy_stats_t stats; } hicn_policy_t; -static const hicn_policy_t POLICY_NONE = { +static const hicn_policy_t POLICY_EMPTY = { .app_name = { 0 }, .tags = { -#ifdef __ANDROID__ #define _(x, y) { POLICY_STATE_NEUTRAL, 0 }, -#else -#define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 }, -#endif foreach_policy_tag #undef _ }, - .stats = POLICY_STATS_NONE, + .stats = POLICY_STATS_EMPTY, }; - /* POLICY DESCRIPTION */ #define PFX_STRLEN 4 /* eg. /128 */ -typedef struct { - int family; - union { - char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN]; - char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN]; - }; - hicn_policy_t policy; +typedef struct +{ + int family; + union + { + char ipv4_prefix[INET_ADDRSTRLEN + PFX_STRLEN]; + char ipv6_prefix[INET6_ADDRSTRLEN + PFX_STRLEN]; + }; + hicn_policy_t policy; } policy_description_t; #endif /* HICN_POLICY_H */ diff --git a/lib/includes/hicn/protocol.h b/lib/includes/hicn/protocol.h index a97cc99cf..fb142e4c3 100644 --- a/lib/includes/hicn/protocol.h +++ b/lib/includes/hicn/protocol.h @@ -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,9 +27,11 @@ #include "protocol/icmprd.h" #include "protocol/tcp.h" #include "protocol/udp.h" +#include "protocol/new.h" typedef union { + _new_header_t newhdr; _ipv4_header_t ipv4; _ipv6_header_t ipv6; _tcp_header_t tcp; diff --git a/lib/includes/hicn/protocol/ah.h b/lib/includes/hicn/protocol/ah.h index 575da80d7..36fd7d509 100644 --- a/lib/includes/hicn/protocol/ah.h +++ b/lib/includes/hicn/protocol/ah.h @@ -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: @@ -35,19 +35,19 @@ typedef struct { - u8 nh; // (to match with reserved in IPSEC AH) - u8 payloadlen; // Len of signature/HMAC in 4-bytes words (maximum size) + u8 nh; // To match with reserved in IPSEC AH + // Length of the signature field. Note that the signature might be smaller + // than the field: the actual size is computed from the field size and + // signaturePadding. + u8 payloadlen; union { u16 reserved; struct { - u8 validationAlgorithm; // As defined in parc_SignerAlgorithm.h - u8 signatureGap; // used to match IPSEC specification and to - // have the real size of the signature(without - // padding). It is the result of Maximum - // signature size - real size + u8 validationAlgorithm; + u8 signaturePadding; }; }; union diff --git a/lib/includes/hicn/protocol/icmp.h b/lib/includes/hicn/protocol/icmp.h index 36954bb6d..9315e1e56 100644 --- a/lib/includes/hicn/protocol/icmp.h +++ b/lib/includes/hicn/protocol/icmp.h @@ -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: @@ -34,7 +34,7 @@ typedef struct u16 csum; } _icmp_header_t; -#define ICMP_HDRLEN sizeof(_icmp_header_t) +#define ICMP_HDRLEN sizeof (_icmp_header_t) static_assert (EXPECTED_ICMP_HDRLEN == ICMP_HDRLEN, "Size of ICMP struct does not match its expected size."); @@ -54,13 +54,13 @@ typedef struct { u16 id; u16 sequence; - } echo; /* echo datagram */ - u32 gateway; /* gateway address */ + } echo; /* echo datagram */ + u32 gateway; /* gateway address */ struct { u16 _unused; u16 mtu; - } frag; /* path mtu discovery */ + } frag; /* path mtu discovery */ struct { u16 expected_lbl; @@ -69,7 +69,7 @@ typedef struct }; } _icmp_wldr_header_t; -#define ICMPWLDR_HDRLEN sizeof(_icmp_wldr_header_t) +#define ICMPWLDR_HDRLEN sizeof (_icmp_wldr_header_t) static_assert (EXPECTED_ICMPWLDR_HDRLEN == ICMPWLDR_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); diff --git a/lib/includes/hicn/protocol/icmprd.h b/lib/includes/hicn/protocol/icmprd.h index aa1fa01ae..897e7969e 100644 --- a/lib/includes/hicn/protocol/icmprd.h +++ b/lib/includes/hicn/protocol/icmprd.h @@ -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: @@ -38,7 +38,7 @@ typedef struct u8 data[64]; } _icmprd4_header_t; -#define ICMPRD4_HDRLEN sizeof(_icmprd4_header_t) +#define ICMPRD4_HDRLEN sizeof (_icmprd4_header_t) static_assert (EXPECTED_ICMPRD4_HDRLEN == ICMPRD4_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); @@ -57,7 +57,7 @@ typedef struct ip6_address_t dst; } _icmprd_header_t; -#define ICMPRD_HDRLEN sizeof(_icmprd_header_t) +#define ICMPRD_HDRLEN sizeof (_icmprd_header_t) static_assert (EXPECTED_ICMPRD_HDRLEN == ICMPRD_HDRLEN, "Size of ICMPWLDR struct does not match its expected size."); diff --git a/lib/includes/hicn/protocol/ipv4.h b/lib/includes/hicn/protocol/ipv4.h index 8a5b6683b..44e95c1e3 100644 --- a/lib/includes/hicn/protocol/ipv4.h +++ b/lib/includes/hicn/protocol/ipv4.h @@ -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: @@ -16,9 +16,10 @@ #ifndef HICN_PROTOCOL_IPV4 #define HICN_PROTOCOL_IPV4 +#include <hicn/util/ip_address.h> + #include "../base.h" #include "../common.h" -#include "../protocol.h" /* Headers were adapted from linux' definitions in netinet/ip.h */ @@ -34,11 +35,11 @@ typedef struct struct { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - u8 ihl:4; - u8 version:4; + u8 ihl : 4; + u8 version : 4; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ - u8 version:4; - u8 ihl:4; + u8 version : 4; + u8 ihl : 4; #else #error "Unsupported endianness" #endif @@ -57,9 +58,10 @@ typedef struct ip4_address_t daddr; } _ipv4_header_t; -#define ipv4_header_bytes(ipv4_header) (sizeof(u32) * (ipv4_header->version_ihl & 0xf)) +#define ipv4_header_bytes(ipv4_header) \ + (sizeof (u32) * (ipv4_header->version_ihl & 0xf)) -#define IPV4_HDRLEN sizeof(_ipv4_header_t) +#define IPV4_HDRLEN sizeof (_ipv4_header_t) static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN, "Size of IPV4 struct does not match its expected size."); @@ -77,22 +79,21 @@ typedef struct u16 size; } ipv4_pseudo_header_t; -#define IPV4_PSHDRLEN sizeof(ipv4_pseudo_header_t) +#define IPV4_PSHDRLEN sizeof (ipv4_pseudo_header_t) static_assert (EXPECTED_IPV4_PSHDRLEN == IPV4_PSHDRLEN, "Size of IPV4_PSHDR struct does not match its expected size."); /* Default field values */ -#define IPV4_DEFAULT_VERSION 4 -#define IPV4_DEFAULT_IHL 5 -#define IPV4_DEFAULT_TOS 0 -#define IPV4_DEFAULT_PAYLOAD_LENGTH 0 -#define IPV4_DEFAULT_ID 300 -#define IPV4_DEFAULT_FRAG_OFF 0x000 -#define IPV4_DEFAULT_TTL 64 -#define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP -#define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0 -#define IPV4_DEFAULT_DST_IP 0, 0, 0, 0 - +#define IPV4_DEFAULT_VERSION 4 +#define IPV4_DEFAULT_IHL 5 +#define IPV4_DEFAULT_TOS 0 +#define IPV4_DEFAULT_PAYLOAD_LENGTH 0 +#define IPV4_DEFAULT_ID 300 +#define IPV4_DEFAULT_FRAG_OFF 0x000 +#define IPV4_DEFAULT_TTL 64 +#define IPV4_DEFAULT_PROTOCOL IPPROTO_TCP +#define IPV4_DEFAULT_SRC_IP 0, 0, 0, 0 +#define IPV4_DEFAULT_DST_IP 0, 0, 0, 0 #endif /* HICN_PROTOCOL_IPV4 */ diff --git a/lib/includes/hicn/protocol/ipv6.h b/lib/includes/hicn/protocol/ipv6.h index 5a83abcae..86301e7d1 100644 --- a/lib/includes/hicn/protocol/ipv6.h +++ b/lib/includes/hicn/protocol/ipv6.h @@ -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: @@ -16,6 +16,8 @@ #ifndef HICN_PROTOCOL_IPV6_H #define HICN_PROTOCOL_IPV6_H +#include <hicn/util/ip_address.h> + #include "../common.h" /* @@ -25,22 +27,26 @@ typedef struct { +#if 0 // TEMPORARY FIX union { struct { - u32 version_class_flow; /* version, traffic class and 20 bits of flow-ID */ - u16 len; /* payload length */ - u8 nxt; /* next header */ - u8 hlim; /* hop limit */ +#endif + u32 version_class_flow; /* version, traffic class and 20 bits of flow-ID */ + u16 len; /* payload length */ + u8 nxt; /* next header */ + u8 hlim; /* hop limit */ +#if 0 }; u8 vfc; /* 4 bits version, top 4 bits class */ }; - ip6_address_t saddr; /* source address */ - ip6_address_t daddr; /* destination address */ +#endif + ip6_address_t saddr; /* source address */ + ip6_address_t daddr; /* destination address */ } _ipv6_header_t; -#define IPV6_HDRLEN sizeof(_ipv6_header_t) +#define IPV6_HDRLEN sizeof (_ipv6_header_t) static_assert (EXPECTED_IPV6_HDRLEN == IPV6_HDRLEN, "Size of IPV6 struct does not match its expected size."); @@ -59,15 +65,15 @@ typedef struct u8 protocol; } ipv6_pseudo_header_t; -#define IPV6_PSHDRLEN sizeof(ipv6_pseudo_header_t) +#define IPV6_PSHDRLEN sizeof (ipv6_pseudo_header_t) static_assert (EXPECTED_IPV6_PSHDRLEN == IPV6_PSHDRLEN, "Size of IPV6_PSHDR struct does not match its expected size."); /* Default field values */ -#define IPV6_DEFAULT_VERSION 6 -#define IPV6_DEFAULT_TRAFFIC_CLASS 0 -#define IPV6_DEFAULT_FLOW_LABEL 0 -#define IPV6_DEFAULT_PAYLOAD_LENGTH 0 +#define IPV6_DEFAULT_VERSION 6 +#define IPV6_DEFAULT_TRAFFIC_CLASS 0 +#define IPV6_DEFAULT_FLOW_LABEL 0 +#define IPV6_DEFAULT_PAYLOAD_LENGTH 0 #endif diff --git a/lib/includes/hicn/protocol/new.h b/lib/includes/hicn/protocol/new.h new file mode 100644 index 000000000..47f7758d2 --- /dev/null +++ b/lib/includes/hicn/protocol/new.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file protocol/ah.h + * @brief AH packet header + */ +#ifndef HICN_PROTOCOL_NEW_H +#define HICN_PROTOCOL_NEW_H + +#include "../common.h" +#include "../name.h" + +/* + * The length of the new header struct must be 28 bytes. + */ +#define EXPECTED_NEW_HDRLEN 32 + +typedef struct +{ + u8 version_reserved; + u8 flags; + u16 payload_length; + u32 lifetime; + ip_address_t prefix; + u32 suffix; + u32 path_label; +} _new_header_t; + +#define NEW_HDRLEN sizeof (_new_header_t) +static_assert (EXPECTED_NEW_HDRLEN == NEW_HDRLEN, + "Size of new_header Struct does not match its expected size."); + +/* TCP flags bit 0 first. */ +#define foreach_hicn_new_flag \ + _ (SIG) /**< Signature header after. */ \ + _ (MAN) /**< Payload type is manifest. */ \ + _ (INT) /**< Packet is interest. */ \ + _ (LST) /**< Last data. */ + +enum +{ +#define _(f) HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ + HICN_NEW_N_FLAG_BITS, +}; + +enum +{ +#define _(f) HICN_NEW_FLAG_##f = 1 << HICN_NEW_FLAG_BIT_##f, + foreach_hicn_new_flag +#undef _ +}; + +static inline int +_get_new_header_version (const _new_header_t *new_hdr) +{ + return ((new_hdr->version_reserved >> 4) & 0x0F); +} + +static inline void +_set_new_header_version (_new_header_t *new_hdr) +{ + new_hdr->version_reserved = (0x9 << 4) & 0xF0; +} + +#endif /* HICN_PROTOCOL_NEW_H */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/lib/includes/hicn/protocol/tcp.h b/lib/includes/hicn/protocol/tcp.h index 347682299..1731f0c24 100644 --- a/lib/includes/hicn/protocol/tcp.h +++ b/lib/includes/hicn/protocol/tcp.h @@ -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: @@ -59,55 +59,55 @@ typedef struct #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ struct { - u16 reserved:4; - u16 doff:4; - u16 fin:1; - u16 syn:1; - u16 rst:1; - u16 psh:1; - u16 ack:1; - u16 urg:1; - u16 ece:1; - u16 cwr:1; + u16 reserved : 4; + u16 doff : 4; + u16 fin : 1; + u16 syn : 1; + u16 rst : 1; + u16 psh : 1; + u16 ack : 1; + u16 urg : 1; + u16 ece : 1; + u16 cwr : 1; }; struct - { /* __ denotes unchanged bitfields */ - u16 timescale:4; - u16 __doff:4; - u16 __fin:1; - u16 __syn:1; - u16 __rst:1; - u16 sig:1; - u16 __ack:1; - u16 man:1; - u16 id:1; - u16 __cwr:1; + { /* __ denotes unchanged bitfields */ + u16 timescale : 4; + u16 __doff : 4; + u16 __fin : 1; + u16 __syn : 1; + u16 __rst : 1; + u16 sig : 1; + u16 __ack : 1; + u16 man : 1; + u16 id : 1; + u16 __cwr : 1; }; #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ struct { - u16 doff:4; - u16 reserved:4; - u16 cwr:1; - u16 ece:1; - u16 urg:1; - u16 ack:1; - u16 psh:1; - u16 rst:1; - u16 syn:1; - u16 fin:1; + u16 doff : 4; + u16 reserved : 4; + u16 cwr : 1; + u16 ece : 1; + u16 urg : 1; + u16 ack : 1; + u16 psh : 1; + u16 rst : 1; + u16 syn : 1; + u16 fin : 1; }; struct { - u16 __doff:4; - u16 timescale:4; - u16 __cwr:1; - u16 id:1 u16 man:1; - u16 __ack:1; - u16 sig:1; - u16 __rst:1; - u16 __syn:1; - u16 __fin:1; + u16 __doff : 4; + u16 timescale : 4; + u16 __cwr : 1; + u16 id : 1 u16 man : 1; + u16 __ack : 1; + u16 sig : 1; + u16 __rst : 1; + u16 __syn : 1; + u16 __fin : 1; }; #endif }; @@ -124,20 +124,20 @@ typedef struct }; } _tcp_header_t; -#define TCP_HDRLEN sizeof(_tcp_header_t) +#define TCP_HDRLEN sizeof (_tcp_header_t) static_assert (EXPECTED_TCP_HDRLEN == TCP_HDRLEN, "Size of TCP struct does not match its expected size."); /* TCP flags bit 0 first. */ -#define foreach_tcp_flag \ - _ (FIN) /**< No more data from sender. */ \ - _ (SYN) /**< Synchronize sequence numbers. */ \ - _ (RST) /**< Reset the connection. */ \ - _ (PSH) /**< Push function. */ \ - _ (ACK) /**< Ack field significant. */ \ - _ (URG) /**< Urgent pointer field significant. */ \ - _ (ECE) /**< ECN-echo. Receiver got CE packet */ \ - _ (CWR) /**< Sender reduced congestion window */ +#define foreach_tcp_flag \ + _ (FIN) /**< No more data from sender. */ \ + _ (SYN) /**< Synchronize sequence numbers. */ \ + _ (RST) /**< Reset the connection. */ \ + _ (PSH) /**< Push function. */ \ + _ (ACK) /**< Ack field significant. */ \ + _ (URG) /**< Urgent pointer field significant. */ \ + _ (ECE) /**< ECN-echo. Receiver got CE packet */ \ + _ (CWR) /**< Sender reduced congestion window */ enum { diff --git a/lib/includes/hicn/protocol/udp.h b/lib/includes/hicn/protocol/udp.h index 75d1ea98c..0e0dddd2c 100644 --- a/lib/includes/hicn/protocol/udp.h +++ b/lib/includes/hicn/protocol/udp.h @@ -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: @@ -29,7 +29,7 @@ typedef struct u16 checksum; } _udp_header_t; -#define UDP_HDRLEN sizeof(_udp_header_t) +#define UDP_HDRLEN sizeof (_udp_header_t) static_assert (EXPECTED_UDP_HDRLEN == UDP_HDRLEN, "Size of UDP struct does not match its expected size."); diff --git a/lib/includes/hicn/strategy.h b/lib/includes/hicn/strategy.h new file mode 100644 index 000000000..c71db451a --- /dev/null +++ b/lib/includes/hicn/strategy.h @@ -0,0 +1,63 @@ +/* + * 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 strategy.h + * \brief hICN forwarding strategy + */ +#ifndef HICN_STRATEGY_H +#define HICN_STRATEGY_H + +#include <string.h> + +#define foreach_strategy_type \ + _ (UNDEFINED) \ + _ (LOADBALANCER) \ + _ (LOW_LATENCY) \ + _ (RANDOM) \ + _ (REPLICATION) \ + _ (BESTPATH) \ + _ (N) + +typedef enum +{ +#define _(x) STRATEGY_TYPE_##x, + foreach_strategy_type +#undef _ +} strategy_type_t; + +extern const char *strategy_str[]; +#define strategy_str(x) strategy_str[x] + +#define IS_VALID_STRATEGY_TYPE(x) IS_VALID_ENUM_TYPE (STRATEGY_TYPE, x) + +static inline strategy_type_t +strategy_type_from_str (const char *strategy_str) +{ +#define _(x) \ + if (strcasecmp (strategy_str, #x) == 0) \ + return STRATEGY_TYPE_##x; \ + else + foreach_strategy_type +#undef _ + return STRATEGY_TYPE_UNDEFINED; +} + +#define STRATEGY_TYPE_VALID(type) \ + ((type != STRATEGY_TYPE_UNDEFINED) && (type != STRATEGY_TYPE_N)) + +#define MAX_FWD_STRATEGY_RELATED_PREFIXES 10 + +#endif /* HICN_STRATEGY_H */ diff --git a/lib/includes/hicn/util/array.h b/lib/includes/hicn/util/array.h index 56cfcad8b..46d60976e 100644 --- a/lib/includes/hicn/util/array.h +++ b/lib/includes/hicn/util/array.h @@ -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: @@ -23,178 +23,169 @@ #include <assert.h> #include <hicn/util/log.h> -#include <math.h> // log2 +#include <math.h> // log2 #include <string.h> // memmove #define BUFSIZE 1024 -typedef int(*cmp_t)(const void * x, const void * y); - -#define TYPEDEF_ARRAY_H(NAME, T) \ - \ -typedef struct { \ - size_t size; \ - size_t max_size_log; \ - T * elements; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * array); \ - \ -int NAME ## _finalize(NAME ## _t * array); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * array); \ - \ -int NAME ## _add(NAME ## _t * array, T element); \ - \ -int NAME ## _remove_index(NAME ## _t * array, int index, T * element); \ - \ -int NAME ## _remove(NAME ## _t * array, const T search, T * element); \ - \ -int NAME ## _get(const NAME ## _t * array, const T search, T * element); \ - \ -int NAME ## _get_index(const NAME ## _t * array, int index, T * element); \ - \ -int NAME ## _get_elements(const NAME ## _t * array, T ** elements); \ - \ -size_t NAME ## _len(const NAME ## _t * array); +typedef int (*cmp_t) (const void *x, const void *y); +#define TYPEDEF_ARRAY_H(NAME, T) \ + \ + typedef struct \ + { \ + size_t size; \ + size_t max_size_log; \ + T *elements; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *array); \ + \ + int NAME##_finalize (NAME##_t *array); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *array); \ + \ + int NAME##_add (NAME##_t *array, T element); \ + \ + int NAME##_remove_index (NAME##_t *array, int index, T *element); \ + \ + int NAME##_remove (NAME##_t *array, const T search, T *element); \ + \ + int NAME##_get (const NAME##_t *array, const T search, T *element); \ + \ + int NAME##_get_index (const NAME##_t *array, int index, T *element); \ + \ + int NAME##_get_elements (const NAME##_t *array, T **elements); \ + \ + size_t NAME##_len (const NAME##_t *array); #define ARRAY_MAX_SIZE_LOG_INIT 0 -#define TYPEDEF_ARRAY(NAME, T, CMP, SNPRINTF) \ -int \ -NAME ## _initialize(NAME ## _t * array) \ -{ \ - array->max_size_log = ARRAY_MAX_SIZE_LOG_INIT; \ - array->size = 0; \ - if (array->max_size_log == 0) { \ - array->elements = NULL; \ - return 0; \ - } \ - array->elements = malloc((1 << array->max_size_log) * sizeof(T)); \ - if (!array->elements) \ - return -1; \ - return 0; \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * array) \ -{ \ - for (unsigned i = 0; i < array->size; i++) { \ - NAME ## _remove_index(array, i, NULL); \ - } \ - return 0; \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * array = malloc(sizeof(NAME ## _t)); \ - if (!array) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(array) < 0) \ - goto ERR_INITIALIZE; \ - \ - return array; \ - \ -ERR_INITIALIZE: \ - free(array); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * array) \ -{ \ - NAME ## _finalize(array); \ - free(array->elements); \ - free(array); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * array, T element) \ -{ \ - /* Ensure sufficient space for next addition */ \ - size_t new_size_log = (array->size > 0) ? log2(array->size)+1 : 1; \ - if (new_size_log > array->max_size_log) { \ - array->max_size_log = new_size_log; \ - array->elements = realloc(array->elements, \ - (1 << new_size_log) * sizeof(T)); \ - } \ - \ - if (!array->elements) \ - goto ERR_REALLOC; \ - \ - array->elements[array->size++] = element; \ - return 0; \ - \ -ERR_REALLOC: \ - return -1; \ -} \ - \ -int \ -NAME ## _remove_index(NAME ## _t * array, int index, T * element) \ -{ \ - if (index > NAME ## _len(array)) \ - return -1; \ - if (element) \ - *element = array->elements[index]; \ - if (index < array->size) \ - memmove(array->elements + index, array->elements + index + 1, \ - array->size - index); \ - array->size--; \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * array, const T search, T * element) \ -{ \ - for (unsigned i = 0; i < array->size; i++) { \ - if (CMP(search, array->elements[i]) == 0) \ - return facelet_array_remove_index(array, i, element); \ - } \ - /* Not found */ \ - if (element) \ - *element = NULL; \ - return 0; \ -} \ - \ -int \ -NAME ## _get(const NAME ## _t * array, const T search, T * element) \ -{ \ - assert(element); \ - for (unsigned i = 0; i < array->size; i++) \ - if (CMP(search, array->elements[i]) == 0) { \ - *element = array->elements[i]; \ - return 0; \ - } \ - /* Not found */ \ - *element = NULL; \ - return 0; \ -} \ - \ -int \ -NAME ## _get_index(const NAME ## _t * array, int index, T * element) \ -{ \ - assert(element); \ - *element = array->elements[index]; \ - return 0; \ -} \ - \ -int \ -NAME ## _get_elements(const NAME ## _t * array, T ** elements) \ -{ \ - *elements = array->elements; \ - return 0; \ -} \ - \ -size_t \ -NAME ## _len(const NAME ## _t * array) \ -{ \ - return array->size; \ -} +#define TYPEDEF_ARRAY(NAME, T, CMP, SNPRINTF) \ + int NAME##_initialize (NAME##_t *array) \ + { \ + array->max_size_log = ARRAY_MAX_SIZE_LOG_INIT; \ + array->size = 0; \ + if (array->max_size_log == 0) \ + { \ + array->elements = NULL; \ + return 0; \ + } \ + array->elements = malloc ((1 << array->max_size_log) * sizeof (T)); \ + if (!array->elements) \ + return -1; \ + return 0; \ + } \ + \ + int NAME##_finalize (NAME##_t *array) \ + { \ + for (unsigned i = 0; i < array->size; i++) \ + { \ + NAME##_remove_index (array, i, NULL); \ + } \ + return 0; \ + } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *array = malloc (sizeof (NAME##_t)); \ + if (!array) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (array) < 0) \ + goto ERR_INITIALIZE; \ + \ + return array; \ + \ + ERR_INITIALIZE: \ + free (array); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *array) \ + { \ + NAME##_finalize (array); \ + free (array->elements); \ + free (array); \ + } \ + \ + int NAME##_add (NAME##_t *array, T element) \ + { \ + /* Ensure sufficient space for next addition */ \ + size_t new_size_log = (array->size > 0) ? log2 (array->size) + 1 : 1; \ + if (new_size_log > array->max_size_log) \ + { \ + array->max_size_log = new_size_log; \ + array->elements = \ + realloc (array->elements, (1 << new_size_log) * sizeof (T)); \ + } \ + \ + if (!array->elements) \ + goto ERR_REALLOC; \ + \ + array->elements[array->size++] = element; \ + return 0; \ + \ + ERR_REALLOC: \ + return -1; \ + } \ + \ + int NAME##_remove_index (NAME##_t *array, int index, T *element) \ + { \ + if (index > NAME##_len (array)) \ + return -1; \ + if (element) \ + *element = array->elements[index]; \ + if (index < array->size) \ + memmove (array->elements + index, array->elements + index + 1, \ + array->size - index); \ + array->size--; \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *array, const T search, T *element) \ + { \ + for (unsigned i = 0; i < array->size; i++) \ + { \ + if (CMP (search, array->elements[i]) == 0) \ + return facelet_array_remove_index (array, i, element); \ + } \ + /* Not found */ \ + if (element) \ + *element = NULL; \ + return 0; \ + } \ + \ + int NAME##_get (const NAME##_t *array, const T search, T *element) \ + { \ + assert (element); \ + for (unsigned i = 0; i < array->size; i++) \ + if (CMP (search, array->elements[i]) == 0) \ + { \ + *element = array->elements[i]; \ + return 0; \ + } \ + /* Not found */ \ + *element = NULL; \ + return 0; \ + } \ + \ + int NAME##_get_index (const NAME##_t *array, int index, T *element) \ + { \ + assert (element); \ + *element = array->elements[index]; \ + return 0; \ + } \ + \ + int NAME##_get_elements (const NAME##_t *array, T **elements) \ + { \ + *elements = array->elements; \ + return 0; \ + } \ + \ + size_t NAME##_len (const NAME##_t *array) { return array->size; } #endif /* UTIL_ARRAY_H */ diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h index 4facd9ad0..89a4c11e0 100644 --- a/lib/includes/hicn/util/ip_address.h +++ b/lib/includes/hicn/util/ip_address.h @@ -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,10 +20,9 @@ #ifndef UTIL_IP_ADDRESS_H #define UTIL_IP_ADDRESS_H - #ifdef __APPLE__ #include <libkern/OSByteOrder.h> -#define __bswap_constant_32(x) OSSwapInt32(x) +#define __bswap_constant_32(x) OSSwapInt32 (x) #include <machine/endian.h> #else #ifdef __ANDROID__ @@ -34,8 +33,8 @@ #include <errno.h> #ifndef _WIN32 -#include <netinet/in.h> // struct sockadd -#include <arpa/inet.h> // inet_ntop +#include <netinet/in.h> // struct sockadd +#include <arpa/inet.h> #include <netdb.h> // struct addrinfo #endif #include <stdbool.h> @@ -45,11 +44,12 @@ #include "types.h" -#define bytes_to_bits(x) (x * 8) -#define IPV6_ADDR_LEN 16 /* bytes */ -#define IPV4_ADDR_LEN 4 /* bytes */ -#define IPV6_ADDR_LEN_BITS bytes_to_bits(IPV6_ADDR_LEN) -#define IPV4_ADDR_LEN_BITS bytes_to_bits(IPV4_ADDR_LEN) +#define bytes_to_bits(x) (x * 8) +#define IPV6_ADDR_LEN 16 /* bytes */ +#define IPV4_ADDR_LEN 4 /* bytes */ +#define IPV6_ADDR_LEN_BITS bytes_to_bits (IPV6_ADDR_LEN) +#define IPV4_ADDR_LEN_BITS bytes_to_bits (IPV4_ADDR_LEN) +#define MAX_IPV6_PREFIX_LEN 128 /* Presentation format */ #ifndef INET_ADDRSTRLEN @@ -65,25 +65,34 @@ #define DUMMY_PORT 1234 -typedef union { - struct { - u32 pad[3]; - union { - struct in_addr as_inaddr; - u8 buffer[4]; - u8 as_u8[4]; - u16 as_u16[2]; - u32 as_u32; - } v4; - }; - union { - struct in6_addr as_in6addr; - u8 buffer[16]; - u8 as_u8[16]; - u16 as_u16[8]; - u32 as_u32[4]; - u64 as_u64[2]; - } v6; +#ifndef HICN_VPP_PLUGIN +typedef union +{ + struct in_addr as_inaddr; + u8 buffer[4]; + u8 as_u8[4]; + u16 as_u16[2]; + u32 as_u32; +} ip4_address_t; + +typedef union +{ + struct in6_addr as_in6addr; + u8 buffer[16]; + u8 as_u8[16]; + u16 as_u16[8]; + u32 as_u32[4]; + u64 as_u64[2]; +} ip6_address_t; + +typedef union +{ + struct + { + u32 pad[3]; + ip4_address_t v4; + }; + ip6_address_t v6; #if 0 /* removed as prone to error due to IPv4 padding */ u8 buffer[IP_MAX_ADDR_LEN]; u8 as_u8[IP_MAX_ADDR_LEN]; @@ -93,77 +102,105 @@ typedef union { #endif } ip_address_t; +#else + +#include <vnet/ip/ip4_packet.h> // ip4_address_t +#include <vnet/ip/ip6_packet.h> // ip6_address_t + +#if __GNUC__ >= 9 +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" +#endif + +#include <vnet/ip/ip46_address.h> + +#if __GNUC__ >= 9 +#pragma GCC diagnostic pop +#endif + +typedef ip46_address_t ip_address_t; + +#endif /* HICN_VPP_PLUGIN */ + +#define ip_address_is_v4(ip) \ + (((ip)->pad[0] | (ip)->pad[1] | (ip)->pad[2]) == 0) + #define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1 #define MAXSZ_IP6_ADDRESS_ INET6_ADDRSTRLEN - 1 -#define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_ -#define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1 -#define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1 -#define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1 +#define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_ +#define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1 +#define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1 +#define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1 -typedef struct { +typedef struct +{ int family; ip_address_t address; u8 len; } ip_prefix_t; -#define MAXSZ_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3 -#define MAXSZ_PREFIX MAXSZ_PREFIX_ + 1 +#define MAXSZ_IP_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3 +#define MAXSZ_IP_PREFIX MAXSZ_IP_PREFIX_ + 1 extern const ip_address_t IPV4_LOOPBACK; extern const ip_address_t IPV6_LOOPBACK; extern const ip_address_t IPV4_ANY; extern const ip_address_t IPV6_ANY; + +extern const ip4_address_t IP4_ADDRESS_EMPTY; +extern const ip6_address_t IP6_ADDRESS_EMPTY; extern const ip_address_t IP_ADDRESS_EMPTY; #define IP_ANY(family) (family == AF_INET) ? IPV4_ANY : IPV6_ANY - -#define MAX_PORT 1 << (8 * sizeof(u16)) -#define IS_VALID_PORT(x) ((x > 0) && ((int)x < MAX_PORT)) +#define MAX_PORT 1 << (8 * sizeof (u16)) +#define IS_VALID_PORT(x) ((x > 0) && ((int) x < MAX_PORT)) #define MAXSZ_PORT_ 5 -#define MAXSZ_PORT MAXSZ_PORT_ + 1 +#define MAXSZ_PORT MAXSZ_PORT_ + 1 #define IS_VALID_FAMILY(x) ((x == AF_INET) || (x == AF_INET6)) /* IP address */ -int ip_address_get_family (const char * ip_address); +int ip_address_get_family (const char *ip_address); int ip_address_len (int family); -const u8 * ip_address_get_buffer(const ip_address_t * ip_address, int family); -int ip_address_ntop (const ip_address_t * ip_address, char *dst, - const size_t len, int family); -int 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); -int ip_address_to_sockaddr(const ip_address_t * ip_address, struct sockaddr *sa, - int family); -int ip_address_cmp(const ip_address_t * ip1, const ip_address_t * ip2, int family); -int ip_address_empty(const ip_address_t * ip); +const u8 *ip_address_get_buffer (const ip_address_t *ip_address, int family); +int ip_address_ntop (const ip_address_t *ip_address, char *dst, + const size_t len, int family); +int 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); +int ip_address_to_sockaddr (const ip_address_t *ip_address, + struct sockaddr *sa, int family); +int ip_address_cmp (const ip_address_t *ip1, const ip_address_t *ip2, + int family); +int ip_address_empty (const ip_address_t *ip); /* Prefix */ -int ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix); -int ip_prefix_ntop_short (const ip_prefix_t * ip_prefix, char *dst, size_t size); -int ip_prefix_ntop (const ip_prefix_t * ip_prefix, char *dst, size_t size); -int ip_prefix_len (const ip_prefix_t * prefix); -bool ip_prefix_empty (const ip_prefix_t * prefix); -int ip_prefix_to_sockaddr(const ip_prefix_t * prefix, struct sockaddr *sa); -int ip_prefix_cmp(const ip_prefix_t * prefix1, const ip_prefix_t * prefix2); +int ip_prefix_pton (const char *ip_address_str, ip_prefix_t *ip_prefix); +int ip_prefix_ntop_short (const ip_prefix_t *ip_prefix, char *dst, + size_t size); +int ip_prefix_ntop (const ip_prefix_t *ip_prefix, char *dst, size_t size); +int ip_prefix_snprintf (char *s, size_t size, const ip_prefix_t *prefix); +int ip_prefix_len (const ip_prefix_t *prefix); +bool ip_prefix_empty (const ip_prefix_t *prefix); +int ip_prefix_to_sockaddr (const ip_prefix_t *prefix, struct sockaddr *sa); +int ip_prefix_cmp (const ip_prefix_t *prefix1, const ip_prefix_t *prefix2); /* 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); +int url_snprintf (char *s, size_t size, int family, + const ip_address_t *ip_address, u16 port); #endif /* UTIL_IP_ADDRESS_H */ diff --git a/lib/includes/hicn/util/log.h b/lib/includes/hicn/util/log.h index 6763d464f..6b35d1fef 100644 --- a/lib/includes/hicn/util/log.h +++ b/lib/includes/hicn/util/log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -17,8 +17,8 @@ #define UTIL_LOG_H #include <stdarg.h> // va_* -#include <stdio.h> // FILE -#include <time.h> // time, localtime +#include <stdio.h> // FILE +#include <time.h> // time, localtime #define LOG_FATAL 0 #define LOG_ERROR 1 @@ -27,43 +27,59 @@ #define LOG_DEBUG 4 #define LOG_TRACE 5 -typedef struct { +typedef struct +{ int log_level; int debug; - FILE * log_file; + FILE *log_file; } log_conf_t; -#define DEFAULT_LOG_CONF { \ - .log_level = LOG_INFO, \ - .debug = 0, \ - .log_file = NULL, \ -}; +#define DEFAULT_LOG_CONF \ + { \ + .log_level = LOG_INFO, \ + .debug = 0, \ + .log_file = NULL, \ + }; extern log_conf_t log_conf; -#define WITH_DEBUG(BLOCK) \ - if (log_conf.log_level >= LOG_DEBUG) \ - BLOCK +#define WITH_ERROR(BLOCK) \ + if (log_conf.log_level >= LOG_ERROR) \ + BLOCK +#define WITH_WARN(BLOCK) \ + if (log_conf.log_level >= LOG_WARN) \ + BLOCK +#define WITH_INFO(BLOCK) \ + if (log_conf.log_level >= LOG_INFO) \ + BLOCK +#define WITH_DEBUG(BLOCK) \ + if (log_conf.log_level >= LOG_DEBUG) \ + BLOCK +#define WITH_TRACE(BLOCK) \ + if (log_conf.log_level >= LOG_TRACE) \ + BLOCK -#define FATAL(fmt, ...) (_log(LOG_FATAL, fmt, ##__VA_ARGS__ )) +#define FATAL(fmt, ...) (_log (LOG_FATAL, fmt, ##__VA_ARGS__)) #ifdef ERROR #undef ERROR #endif -#define ERROR(fmt, ...) (_log(LOG_ERROR, fmt, ##__VA_ARGS__ )) -#define WARN(fmt, ...) (_log(LOG_WARN, fmt, ##__VA_ARGS__ )) -#define INFO(fmt, ...) (_log(LOG_INFO, fmt, ##__VA_ARGS__ )) -#define DEBUG(fmt, ...) (_log(LOG_DEBUG, fmt, ##__VA_ARGS__ )) -#define TRACE(fmt, ...) (_log(LOG_TRACE, fmt, ##__VA_ARGS__ )) +#define ERROR(fmt, ...) (_log (LOG_ERROR, fmt, ##__VA_ARGS__)) +#define WARN(fmt, ...) (_log (LOG_WARN, fmt, ##__VA_ARGS__)) +#define INFO(fmt, ...) (_log (LOG_INFO, fmt, ##__VA_ARGS__)) +#define DEBUG(fmt, ...) (_log (LOG_DEBUG, fmt, ##__VA_ARGS__)) +#define TRACE(fmt, ...) (_log (LOG_TRACE, fmt, ##__VA_ARGS__)) -void _log_va(int level, const char *fmt, va_list ap); +void _log_va (int level, const char *fmt, va_list ap); -void _log(int level, const char *fmt, ...); +void _log (int level, const char *fmt, ...); -void fatal(char *fmt, ...); +void fatal (char *fmt, ...); + +int loglevel_from_str (const char *loglevel); #ifdef HAVE_BACKTRACE #include <execinfo.h> -void print_trace(void); +void print_trace (void); #endif #endif // UTIL_LOG_H diff --git a/lib/includes/hicn/util/map.h b/lib/includes/hicn/util/map.h index 01195865e..6e23f222f 100644 --- a/lib/includes/hicn/util/map.h +++ b/lib/includes/hicn/util/map.h @@ -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,231 +20,227 @@ #include "set.h" -#define ERR_MAP_EXISTS -2 +#define ERR_MAP_EXISTS -2 #define ERR_MAP_NOT_FOUND -3 -#define TYPEDEF_MAP_H(NAME, KEY_T, VAL_T) \ - \ -typedef struct { \ - KEY_T key; \ - VAL_T value; \ -} NAME ## _pair_t; \ - \ -NAME ## _pair_t * NAME ## _pair_create(KEY_T key, VAL_T value); \ - \ -void NAME ## _pair_free(NAME ## _pair_t * pair); \ - \ -int NAME ## _pair_cmp(const NAME ## _pair_t * p1, const NAME ## _pair_t * p2); \ - \ -TYPEDEF_SET_H(NAME ## _pair_set, NAME ## _pair_t *) \ - \ -typedef struct NAME ## _s { \ - NAME ## _pair_set_t pair_set; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * map); \ - \ -int NAME ## _finalize(NAME ## _t * map); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * map); \ - \ -int NAME ## _add(NAME ## _t * map, KEY_T key, VAL_T value); \ - \ -int NAME ## _remove(NAME ## _t * map, KEY_T key, VAL_T * value); \ - \ -int NAME ## _get(NAME ## _t * map, KEY_T key, VAL_T * value); \ - \ -void NAME ## _dump(NAME ## _t * map); +#define TYPEDEF_MAP_H(NAME, KEY_T, VAL_T) \ + \ + typedef struct \ + { \ + KEY_T key; \ + VAL_T value; \ + } NAME##_pair_t; \ + \ + NAME##_pair_t *NAME##_pair_create (KEY_T key, VAL_T value); \ + \ + void NAME##_pair_free (NAME##_pair_t *pair); \ + \ + int NAME##_pair_cmp (const NAME##_pair_t *p1, const NAME##_pair_t *p2); \ + \ + TYPEDEF_SET_H (NAME##_pair_set, NAME##_pair_t *) \ + \ + typedef struct NAME##_s \ + { \ + NAME##_pair_set_t pair_set; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *map); \ + \ + int NAME##_finalize (NAME##_t *map); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *map); \ + \ + int NAME##_add (NAME##_t *map, KEY_T key, VAL_T value); \ + \ + int NAME##_remove (NAME##_t *map, KEY_T key, VAL_T *value); \ + \ + int NAME##_get (NAME##_t *map, KEY_T key, VAL_T *value); \ + \ + void NAME##_dump (NAME##_t *map); \ + \ + int NAME##_get_key_array (NAME##_t *map, KEY_T **array); \ + \ + int NAME##_get_value_array (NAME##_t *map, VAL_T **array); - - - -#define TYPEDEF_MAP(NAME, KEY_T, VAL_T, CMP, KEY_SNPRINTF, VALUE_SNPRINTF) \ - \ -NAME ## _pair_t * NAME ## _pair_create(KEY_T key, VAL_T value) \ -{ \ - /* Create pair */ \ - NAME ## _pair_t * pair = malloc(sizeof(NAME ## _pair_t)); \ - if (!pair) \ - return NULL; \ - \ - pair->key = key; \ - pair->value = value; \ - \ - return pair; \ -} \ - \ -void NAME ## _pair_free(NAME ## _pair_t * pair) \ -{ \ - free(pair); \ -} \ - \ -int \ -NAME ## _pair_cmp(const NAME ## _pair_t * p1, const NAME ## _pair_t * p2) \ -{ \ - return (CMP(p1->key, p2->key)); \ -} \ - \ -int \ -NAME ## _pair_snprintf(char * buf, size_t size, const NAME ## _pair_t * pair) { \ - int rc; \ - rc = KEY_SNPRINTF(buf, BUFSIZE/2, (KEY_T)pair->key); \ - if (rc < 0) \ - return rc; \ - rc = VALUE_SNPRINTF(buf+rc, BUFSIZE/2, (VAL_T)pair->value); \ - return (int)rc; \ -} \ - \ -TYPEDEF_SET(NAME ## _pair_set, NAME ## _pair_t *, NAME ## _pair_cmp, NAME ## _pair_snprintf); \ - \ -int \ -NAME ## _initialize(NAME ## _t * map) \ -{ \ - return NAME ## _pair_set_initialize(&map->pair_set); \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * map) \ -{ \ - NAME ## _pair_t ** array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &array); \ - if (n < 0) \ - return -1; \ - for (unsigned i = 0; i < n; i++) { \ - NAME ## _pair_t * pair = array[i]; \ - NAME ## _pair_set_remove(&map->pair_set, pair, NULL); \ - NAME ## _pair_free(pair); \ - } \ - free(array); \ - return NAME ## _pair_set_finalize(&map->pair_set); \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * map = malloc(sizeof(NAME ## _t)); \ - if (!map) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(map) < 0) \ - goto ERR_INITIALIZE; \ - \ - return map; \ - \ -ERR_INITIALIZE: \ - free(map); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * map) \ -{ \ - NAME ## _finalize(map); \ - free(map); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * map, KEY_T key, VAL_T value) \ -{ \ - int rc; \ - NAME ## _pair_t * found = NULL; \ - \ - NAME ## _pair_t * pair = NAME ## _pair_create(key, value); \ - if (!pair) \ - return -1; \ - \ - rc = NAME ## _pair_set_get(&map->pair_set, pair, &found); \ - if (rc < 0) \ - return -1; \ - if (found) { \ - NAME ## _pair_free(pair); \ - return ERR_MAP_EXISTS; \ - } \ - \ - rc = NAME ## _pair_set_add(&map->pair_set, pair); \ - if (rc < 0) { \ - NAME ## _pair_free(pair); \ - return -1; \ - } \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * map, KEY_T key, VAL_T * value) \ -{ \ - NAME ## _pair_t * found = NULL; \ - NAME ## _pair_t search = { .key = key }; \ - int rc = NAME ## _pair_set_remove(&map->pair_set, &search, &found); \ - if (rc < 0) \ - return ERR_MAP_NOT_FOUND; \ - if (value) \ - *value = found->value; \ - NAME ## _pair_free(found); \ - return 0; \ -} \ - \ -int \ -NAME ## _get(NAME ## _t * map, KEY_T key, VAL_T * value) \ -{ \ - NAME ## _pair_t * found = NULL, search = { .key = key }; \ - int rc = NAME ## _pair_set_get(&map->pair_set, &search, &found); \ - if (rc < 0) \ - return -1; \ - if (found) \ - *value = found->value; \ - return 0; \ -} \ - \ -void \ -NAME ## _dump(NAME ## _t * map) { \ - NAME ## _pair_set_dump(&map->pair_set); \ -} \ - \ -int \ -NAME ## _get_key_array(NAME ## _t * map, KEY_T **array) { \ - NAME ## _pair_t ** pair_array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &pair_array); \ - if (n < 0) \ - return -1; \ - if (!array) \ - goto END; \ - /* Allocate result array */ \ - *array = malloc(n * sizeof(KEY_T)); \ - if (!array) { \ - free(pair_array); \ - return -1; \ - } \ - /* Copy keys */ \ - for (int i = 0; i < n; i++) \ - (*array)[i] = pair_array[i]->key; \ - free(pair_array); \ -END: \ - return n; \ -} \ - \ -int \ -NAME ## _get_value_array(NAME ## _t * map, VAL_T **array) { \ - NAME ## _pair_t ** pair_array; \ - int n = NAME ## _pair_set_get_array(&map->pair_set, &pair_array); \ - if (n < 0) \ - return -1; \ - if (!array) \ - goto END; \ - /* Allocate result array */ \ - *array = malloc(n * sizeof(VAL_T)); \ - if (!array) { \ - free(pair_array); \ - return -1; \ - } \ - /* Copy values */ \ - for (int i = 0; i < n; i++) \ - (*array)[i] = pair_array[i]->value; \ - free(pair_array); \ -END: \ - return n; \ -} +#define TYPEDEF_MAP(NAME, KEY_T, VAL_T, CMP, KEY_SNPRINTF, VALUE_SNPRINTF) \ + \ + NAME##_pair_t *NAME##_pair_create (KEY_T key, VAL_T value) \ + { \ + /* Create pair */ \ + NAME##_pair_t *pair = malloc (sizeof (NAME##_pair_t)); \ + if (!pair) \ + return NULL; \ + \ + pair->key = key; \ + pair->value = value; \ + \ + return pair; \ + } \ + \ + void NAME##_pair_free (NAME##_pair_t *pair) { free (pair); } \ + \ + int NAME##_pair_cmp (const NAME##_pair_t *p1, const NAME##_pair_t *p2) \ + { \ + return (CMP (p1->key, p2->key)); \ + } \ + \ + int NAME##_pair_snprintf (char *buf, size_t size, \ + const NAME##_pair_t *pair) \ + { \ + int rc; \ + rc = KEY_SNPRINTF (buf, BUFSIZE / 2, (KEY_T) pair->key); \ + if (rc < 0) \ + return rc; \ + rc = VALUE_SNPRINTF (buf + rc, BUFSIZE / 2, (VAL_T) pair->value); \ + return (int) rc; \ + } \ + \ + TYPEDEF_SET (NAME##_pair_set, NAME##_pair_t *, NAME##_pair_cmp, \ + NAME##_pair_snprintf); \ + \ + int NAME##_initialize (NAME##_t *map) \ + { \ + return NAME##_pair_set_initialize (&map->pair_set); \ + } \ + \ + int NAME##_finalize (NAME##_t *map) \ + { \ + NAME##_pair_t **array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &array); \ + if (n < 0) \ + return -1; \ + for (unsigned i = 0; i < n; i++) \ + { \ + NAME##_pair_t *pair = array[i]; \ + NAME##_pair_set_remove (&map->pair_set, pair, NULL); \ + NAME##_pair_free (pair); \ + } \ + free (array); \ + return NAME##_pair_set_finalize (&map->pair_set); \ + } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *map = malloc (sizeof (NAME##_t)); \ + if (!map) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (map) < 0) \ + goto ERR_INITIALIZE; \ + \ + return map; \ + \ + ERR_INITIALIZE: \ + free (map); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *map) \ + { \ + NAME##_finalize (map); \ + free (map); \ + } \ + \ + int NAME##_add (NAME##_t *map, KEY_T key, VAL_T value) \ + { \ + int rc; \ + NAME##_pair_t *found = NULL; \ + \ + NAME##_pair_t *pair = NAME##_pair_create (key, value); \ + if (!pair) \ + return -1; \ + \ + rc = NAME##_pair_set_get (&map->pair_set, pair, &found); \ + if (rc < 0) \ + return -1; \ + if (found) \ + { \ + NAME##_pair_free (pair); \ + return ERR_MAP_EXISTS; \ + } \ + \ + rc = NAME##_pair_set_add (&map->pair_set, pair); \ + if (rc < 0) \ + { \ + NAME##_pair_free (pair); \ + return -1; \ + } \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *map, KEY_T key, VAL_T *value) \ + { \ + NAME##_pair_t *found = NULL; \ + NAME##_pair_t search = { .key = key }; \ + int rc = NAME##_pair_set_remove (&map->pair_set, &search, &found); \ + if (rc < 0) \ + return ERR_MAP_NOT_FOUND; \ + if (value) \ + *value = found->value; \ + NAME##_pair_free (found); \ + return 0; \ + } \ + \ + int NAME##_get (NAME##_t *map, KEY_T key, VAL_T *value) \ + { \ + NAME##_pair_t *found = NULL, search = { .key = key }; \ + int rc = NAME##_pair_set_get (&map->pair_set, &search, &found); \ + if (rc < 0) \ + return -1; \ + if (found) \ + *value = found->value; \ + return 0; \ + } \ + \ + void NAME##_dump (NAME##_t *map) { NAME##_pair_set_dump (&map->pair_set); } \ + \ + int NAME##_get_key_array (NAME##_t *map, KEY_T **array) \ + { \ + NAME##_pair_t **pair_array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &pair_array); \ + if (n < 0) \ + return -1; \ + if (!array) \ + goto END; \ + /* Allocate result array */ \ + *array = malloc (n * sizeof (KEY_T)); \ + if (!array) \ + { \ + free (pair_array); \ + return -1; \ + } \ + /* Copy keys */ \ + for (int i = 0; i < n; i++) \ + (*array)[i] = pair_array[i]->key; \ + free (pair_array); \ + END: \ + return n; \ + } \ + \ + int NAME##_get_value_array (NAME##_t *map, VAL_T **array) \ + { \ + NAME##_pair_t **pair_array; \ + int n = NAME##_pair_set_get_array (&map->pair_set, &pair_array); \ + if (n < 0) \ + return -1; \ + if (!array) \ + goto END; \ + /* Allocate result array */ \ + *array = malloc (n * sizeof (VAL_T)); \ + if (!array) \ + { \ + free (pair_array); \ + return -1; \ + } \ + /* Copy values */ \ + for (int i = 0; i < n; i++) \ + (*array)[i] = pair_array[i]->value; \ + free (pair_array); \ + END: \ + return n; \ + } #endif /* UTIL_MAP_H */ diff --git a/lib/includes/hicn/util/set.h b/lib/includes/hicn/util/set.h index bc2e3caac..0a5ff6777 100644 --- a/lib/includes/hicn/util/set.h +++ b/lib/includes/hicn/util/set.h @@ -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: @@ -25,214 +25,196 @@ #define thread_local _Thread_local //#endif /* ! __ANDROID__ */ -#define ERR_SET_EXISTS -2 +#define ERR_SET_EXISTS -2 #define ERR_SET_NOT_FOUND -3 /* FIXME: buffer overflow when this is too small... investigate */ #define BUFSIZE 1024 -static inline -int -int_cmp(const int x, const int y) +static inline int +int_cmp (const int x, const int y) { - return x - y; + return x - y; } -static inline -int -int_snprintf(char * buf, size_t size, int value) { - return snprintf(buf, size, "%d", value); +static inline int +int_snprintf (char *buf, size_t size, int value) +{ + return snprintf (buf, size, "%d", value); } -static inline -int -string_snprintf(char * buf, size_t size, const char * s) { - return snprintf(buf, size, "%s", s); +static inline int +string_snprintf (char *buf, size_t size, const char *s) +{ + return snprintf (buf, size, "%s", s); } -static inline -int -generic_snprintf(char * buf, size_t size, const void * value) { - return snprintf(buf, BUFSIZE, "%p", value); +static inline int +generic_snprintf (char *buf, size_t size, const void *value) +{ + return snprintf (buf, BUFSIZE, "%p", value); } -typedef int(*cmp_t)(const void * x, const void * y); - -#define TYPEDEF_SET_H(NAME, T) \ - \ -typedef struct { \ - size_t size; \ - void * root; \ -} NAME ## _t; \ - \ -int NAME ## _initialize(NAME ## _t * set); \ - \ -int NAME ## _finalize(NAME ## _t * set); \ - \ -NAME ## _t * NAME ## _create(); \ - \ -void NAME ## _free(NAME ## _t * set); \ - \ -int NAME ## _add(NAME ## _t * set, const T element); \ - \ -int NAME ## _remove(NAME ## _t * set, const T search, T * element); \ - \ -int NAME ## _clear(NAME ## _t * set); \ - \ -int NAME ## _get(const NAME ## _t * set, const T search, T * element); \ - \ -int NAME ## _get_array(const NAME ## _t * set, T ** element); \ - \ -void NAME ## _dump(NAME ## _t * set); +typedef int (*cmp_t) (const void *x, const void *y); +#define TYPEDEF_SET_H(NAME, T) \ + \ + typedef struct \ + { \ + size_t size; \ + void *root; \ + } NAME##_t; \ + \ + int NAME##_initialize (NAME##_t *set); \ + \ + int NAME##_finalize (NAME##_t *set); \ + \ + NAME##_t *NAME##_create (); \ + \ + void NAME##_free (NAME##_t *set); \ + \ + int NAME##_add (NAME##_t *set, const T element); \ + \ + int NAME##_remove (NAME##_t *set, const T search, T *element); \ + \ + int NAME##_clear (NAME##_t *set); \ + \ + int NAME##_get (const NAME##_t *set, const T search, T *element); \ + \ + int NAME##_get_array (const NAME##_t *set, T **element); \ + \ + void NAME##_dump (NAME##_t *set); - - -#define TYPEDEF_SET(NAME, T, CMP, SNPRINTF) \ -int \ -NAME ## _initialize(NAME ## _t * set) \ -{ \ - set->root = NULL; \ - set->size = 0; \ - return 0; \ -} \ - \ -int \ -NAME ## _finalize(NAME ## _t * set) \ -{ \ - return NAME ## _clear(set); \ -} \ - \ -NAME ## _t * \ -NAME ## _create() \ -{ \ - NAME ## _t * set = malloc(sizeof(NAME ## _t)); \ - if (!set) \ - goto ERR_MALLOC; \ - \ - if (NAME ## _initialize(set) < 0) \ - goto ERR_INITIALIZE; \ - \ - return set; \ - \ -ERR_INITIALIZE: \ - free(set); \ -ERR_MALLOC: \ - return NULL; \ -} \ - \ -void \ -NAME ## _free(NAME ## _t * set) \ -{ \ - NAME ## _finalize(set); \ - free(set); \ -} \ - \ -int \ -NAME ## _add(NAME ## _t * set, const T element) \ -{ \ - T * found = tfind(element, &set->root, (cmp_t)CMP); \ - void * ptr = tsearch(element, &set->root, (cmp_t)CMP); \ - if (!ptr) \ - return -1; \ - if (!found) \ - set->size++; \ - return 0; \ -} \ - \ -int \ -NAME ## _remove(NAME ## _t * set, const T search, T * element) \ -{ \ - T * found = tfind(search, &set->root, (cmp_t)CMP); \ - if (!found) \ - return ERR_SET_NOT_FOUND; \ - if (element) \ - *element = *found; \ - tdelete(search, &set->root, (cmp_t)CMP); \ - set->size--; \ - return 0; \ -} \ - \ -int \ -NAME ## _clear(NAME ## _t * set) \ -{ \ - T * array; \ - int n = NAME ## _get_array(set, &array); \ - if (n < 0) \ - return -1; \ - for (unsigned i = 0; i < n; i++) { \ - T element = array[i]; \ - NAME ## _remove(set, element, NULL); \ - } \ - free(array); \ - return 0; \ -} \ - \ -int \ -NAME ## _get(const NAME ## _t * set, const T search, T * element) \ -{ \ - T * found = tfind(search, &set->root, (cmp_t)CMP); \ - if (element) \ - *element = found ? *found : NULL; \ - return 0; \ -} \ - \ -static void \ -NAME ## _dump_node(const void *nodep, const VISIT which, \ - const int depth) \ -{ \ - char buf[BUFSIZE]; \ - switch (which) { \ - case preorder: \ - case endorder: \ - break; \ - case postorder: \ - case leaf: \ - SNPRINTF(buf, BUFSIZE, *(T*)nodep); \ - INFO("%s", buf); \ - break; \ - } \ -} \ - \ -void \ -NAME ## _dump(NAME ## _t * set) { \ - twalk(set->root, NAME ## _dump_node); \ -} \ - \ -thread_local \ -T * NAME ## _array_pos = NULL; \ - \ -static void \ -NAME ## _add_node_to_array(const void *nodep, const VISIT which, \ - const int depth) \ -{ \ - if (!NAME ## _array_pos) \ - return; \ - switch (which) { \ - case preorder: \ - case endorder: \ - break; \ - case postorder: \ - case leaf: \ - *NAME ## _array_pos = *(T*)nodep; \ - NAME ## _array_pos++; \ - break; \ - } \ -} \ - \ -int \ -NAME ## _get_array(const NAME ## _t * set, T ** element) \ -{ \ - if (!element) \ - goto END; \ - *element = malloc(set->size * sizeof(T)); \ - if (!*element) \ - return -1; \ - NAME ## _array_pos = *element; \ - twalk(set->root, NAME ## _add_node_to_array); \ - NAME ## _array_pos = NULL; \ -END: \ - return (int)(set->size); \ -} +#define TYPEDEF_SET(NAME, T, CMP, SNPRINTF) \ + int NAME##_initialize (NAME##_t *set) \ + { \ + set->root = NULL; \ + set->size = 0; \ + return 0; \ + } \ + \ + int NAME##_finalize (NAME##_t *set) { return NAME##_clear (set); } \ + \ + NAME##_t *NAME##_create () \ + { \ + NAME##_t *set = malloc (sizeof (NAME##_t)); \ + if (!set) \ + goto ERR_MALLOC; \ + \ + if (NAME##_initialize (set) < 0) \ + goto ERR_INITIALIZE; \ + \ + return set; \ + \ + ERR_INITIALIZE: \ + free (set); \ + ERR_MALLOC: \ + return NULL; \ + } \ + \ + void NAME##_free (NAME##_t *set) \ + { \ + NAME##_finalize (set); \ + free (set); \ + } \ + \ + int NAME##_add (NAME##_t *set, const T element) \ + { \ + T *found = tfind (element, &set->root, (cmp_t) CMP); \ + void *ptr = tsearch (element, &set->root, (cmp_t) CMP); \ + if (!ptr) \ + return -1; \ + if (!found) \ + set->size++; \ + return 0; \ + } \ + \ + int NAME##_remove (NAME##_t *set, const T search, T *element) \ + { \ + T *found = tfind (search, &set->root, (cmp_t) CMP); \ + if (!found) \ + return ERR_SET_NOT_FOUND; \ + if (element) \ + *element = *found; \ + tdelete (search, &set->root, (cmp_t) CMP); \ + set->size--; \ + return 0; \ + } \ + \ + int NAME##_clear (NAME##_t *set) \ + { \ + T *array; \ + int n = NAME##_get_array (set, &array); \ + if (n < 0) \ + return -1; \ + for (unsigned i = 0; i < n; i++) \ + { \ + T element = array[i]; \ + NAME##_remove (set, element, NULL); \ + } \ + free (array); \ + return 0; \ + } \ + \ + int NAME##_get (const NAME##_t *set, const T search, T *element) \ + { \ + T *found = tfind (search, &set->root, (cmp_t) CMP); \ + if (element) \ + *element = found ? *found : NULL; \ + return 0; \ + } \ + \ + static void NAME##_dump_node (const void *nodep, const VISIT which, \ + const int depth) \ + { \ + char buf[BUFSIZE]; \ + switch (which) \ + { \ + case preorder: \ + case endorder: \ + break; \ + case postorder: \ + case leaf: \ + SNPRINTF (buf, BUFSIZE, *(T *) nodep); \ + INFO ("%s", buf); \ + break; \ + } \ + } \ + \ + void NAME##_dump (NAME##_t *set) { twalk (set->root, NAME##_dump_node); } \ + \ + thread_local T *NAME##_array_pos = NULL; \ + \ + static void NAME##_add_node_to_array (const void *nodep, const VISIT which, \ + const int depth) \ + { \ + if (!NAME##_array_pos) \ + return; \ + switch (which) \ + { \ + case preorder: \ + case endorder: \ + break; \ + case postorder: \ + case leaf: \ + *NAME##_array_pos = *(T *) nodep; \ + NAME##_array_pos++; \ + break; \ + } \ + } \ + \ + int NAME##_get_array (const NAME##_t *set, T **element) \ + { \ + if (!element) \ + goto END; \ + *element = calloc (set->size, sizeof (T)); \ + if (!*element) \ + return -1; \ + NAME##_array_pos = *element; \ + twalk (set->root, NAME##_add_node_to_array); \ + NAME##_array_pos = NULL; \ + END: \ + return (int) (set->size); \ + } #endif /* UTIL_SET_H */ diff --git a/lib/includes/hicn/util/sstrncpy.h b/lib/includes/hicn/util/sstrncpy.h new file mode 100644 index 000000000..b316201be --- /dev/null +++ b/lib/includes/hicn/util/sstrncpy.h @@ -0,0 +1,68 @@ +/* + * 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. + */ + +#ifndef UTIL_SSTRNCPY_H +#define UTIL_SSTRNCPY_H + +#define __STDC_WANT_LIB_EXT1__ 1 +#include <string.h> + +#ifdef __STDC_LIB_EXT1__ +// If safe string functions already available in the system, use them +#elif ENABLE_SAFEC +// If safe string functions not available and SafeC is enabled, +// use SafeC +#include <safe_string.h> +#else +// Use custom safe string functions +typedef int errno_t; +#define EOK 0 + +/** + * @brief This function assures a null byte at the end of the buffer. + */ +static inline errno_t +strcpy_s (char *dst, size_t n, const char *src) +{ + if (!dst || !src || !n) + { + fprintf (stderr, "[strncpy] invalid input received"); + return EINVAL; + } + + dst[n - 1] = 0; + strncpy (dst, src, n); + + if (dst[n - 1] != 0) + { + fprintf (stderr, "[strncpy] '%s' has been trucated\n", src); + dst[n - 1] = 0; + return EINVAL; + } + + return EOK; +} + +static inline size_t +strnlen_s (const char *s, size_t maxlen) +{ + if (s == NULL) + return 0; + + return strnlen (s, maxlen); +} + +#endif /* __STDC_LIB_EXT1__ */ +#endif /* UTIL_SSTRNCPY_H */ diff --git a/lib/includes/hicn/util/token.h b/lib/includes/hicn/util/token.h index 43e0a77b2..c62c294bc 100644 --- a/lib/includes/hicn/util/token.h +++ b/lib/includes/hicn/util/token.h @@ -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,12 +19,12 @@ * Concatenate preprocessor tokens A and B without expanding macro definitions * (however, if invoked from a macro, macro arguments are expanded). */ -#define PPCAT_NX(A, B) A ## B +#define PPCAT_NX(A, B) A##B /* * Concatenate preprocessor tokens A and B after macro-expanding them. */ -#define PPCAT(A, B) PPCAT_NX(A, B) +#define PPCAT(A, B) PPCAT_NX (A, B) /* Token stringification */ @@ -37,4 +37,4 @@ /* * Turn A into a string literal after macro-expanding it. */ -#define STRINGIZE(A) STRINGIZE_NX(A) +#define STRINGIZE(A) STRINGIZE_NX (A) diff --git a/lib/includes/hicn/util/types.h b/lib/includes/hicn/util/types.h index 017e85b72..50c5362d3 100644 --- a/lib/includes/hicn/util/types.h +++ b/lib/includes/hicn/util/types.h @@ -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: @@ -25,15 +25,19 @@ typedef uint32_t u32; typedef uint64_t u64; /* Helper for avoiding warnings about type-punning */ -#define UNION_CAST(x, destType) \ - (((union {__typeof__(x) a; destType b;})x).b) +#define UNION_CAST(x, destType) \ + (((union { \ + __typeof__ (x) a; \ + destType b; \ + }) x) \ + .b) -//typedef unsigned int hash_t; +// typedef unsigned int hash_t; -typedef int (*cmp_t)(const void *, const void *); +typedef int (*cmp_t) (const void *, const void *); /* Enums */ -#define IS_VALID_ENUM_TYPE(NAME, x) ((x > NAME ## _UNDEFINED) && (x < NAME ## _N)) +#define IS_VALID_ENUM_TYPE(NAME, x) ((x > NAME##_UNDEFINED) && (x < NAME##_N)) #endif /* UTIL_TYPES */ diff --git a/lib/includes/hicn/util/win_portability.h b/lib/includes/hicn/util/win_portability.h index 5f30cfbb2..609203afc 100644 --- a/lib/includes/hicn/util/win_portability.h +++ b/lib/includes/hicn/util/win_portability.h @@ -1,45 +1,45 @@ -/*
- * Copyright (c) 2019 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-#include <hicn/util/windows/windows_Utils.h>
-#include <afunix.h>
-#include <io.h>
-#include <iphlpapi.h>
-#include <process.h>
-#include <stdio.h>
-#pragma comment(lib, "IPHLPAPI.lib")
-
-#ifndef in_port_t
-#define in_port_t uint16_t
-#endif
-
-#ifndef in_addr_t
-#define in_addr_t uint32_t
-#endif
-
-#ifndef strncasecmp
-#define strncasecmp _strnicmp
-#endif
-
-#ifndef strcasecmp
-#define strcasecmp _stricmp
-#endif
-
-#define HAVE_STRUCT_TIMESPEC
-
-#ifndef getline
-int getline(char **lineptr, size_t *n, FILE *stream);
+/* + * 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. + */ + +#pragma once +#include <hicn/util/windows/windows_Utils.h> +#include <afunix.h> +#include <io.h> +#include <iphlpapi.h> +#include <process.h> +#include <stdio.h> +#pragma comment(lib, "IPHLPAPI.lib") + +#ifndef in_port_t +#define in_port_t uint16_t +#endif + +#ifndef in_addr_t +#define in_addr_t uint32_t +#endif + +#ifndef strncasecmp +#define strncasecmp _strnicmp +#endif + +#ifndef strcasecmp +#define strcasecmp _stricmp +#endif + +#define HAVE_STRUCT_TIMESPEC + +#ifndef getline +int getline (char **lineptr, size_t *n, FILE *stream); #endif
\ No newline at end of file diff --git a/lib/includes/hicn/util/windows/dlfcn.h b/lib/includes/hicn/util/windows/dlfcn.h index 7775226cd..f1457964e 100644 --- a/lib/includes/hicn/util/windows/dlfcn.h +++ b/lib/includes/hicn/util/windows/dlfcn.h @@ -5,8 +5,8 @@ #define RTLD_GLOBAL 0x100 /* do not hide entries in this module */ #define RTLD_LOCAL 0x000 /* hide entries in this module */ -#define RTLD_LAZY 0x000 /* accept unresolved externs */ -#define RTLD_NOW 0x001 /* abort if module has unresolved externs */ +#define RTLD_LAZY 0x000 /* accept unresolved externs */ +#define RTLD_NOW 0x001 /* abort if module has unresolved externs */ /* How to call in Windows: @@ -16,15 +16,16 @@ */ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif - void *dlopen (const char *filename, int flag); - int dlclose (void *handle); + void *dlopen (const char *filename, int flag); + int dlclose (void *handle); - void *dlsym (void *handle, const char *name); + void *dlsym (void *handle, const char *name); -const char *dlerror (void); + const char *dlerror (void); #ifdef __cplusplus } diff --git a/lib/includes/hicn/util/windows/windows_utils.h b/lib/includes/hicn/util/windows/windows_utils.h index d24aaadbf..e15c0d752 100755..100644 --- a/lib/includes/hicn/util/windows/windows_utils.h +++ b/lib/includes/hicn/util/windows/windows_utils.h @@ -1,162 +1,166 @@ -/*
- * Copyright (c) 2019 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef WINDOWS_UTILS_H
-#define WINDOWS_UTILS_H
-#define WIN32_LEAN_AND_MEAN
-#define HAVE_STRUCT_TIMESPEC
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-#include <Windows.h>
-#include <stdint.h>
-#include <io.h>
-#include <stdlib.h>
-#include <winsock2.h>
-#include <WS2tcpip.h>
-#include "dlfcn.h"
-
-#ifndef IOVEC
-#define IOVEC
-struct iovec {
- void* iov_base;
- size_t iov_len;
-};
-#endif
-
-typedef uint16_t in_port_t;
-
-#ifndef SLEEP
-#define SLEEP
-#define sleep Sleep
-#endif
-
-#ifndef USLEEP
-#define USLEEP
-void usleep(__int64 usec);
-#endif
-
-#ifndef S_ISDIR
-#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
-#endif
-
-#define PARCLibrary_DISABLE_ATOMICS
-#include <BaseTsd.h>
-typedef SSIZE_T ssize_t;
-
-#ifndef __ATTRIBUTE__
-#define __ATTRIBUTE__
-#define __attribute__(A)
-#endif
-
-#ifndef RESTRICT
-#define RESTRICT
-#define restrict __restrict
-#endif
-
-#ifndef GETTIMEOFDAY
-#define GETTIMEOFDAY
-int gettimeofday(struct timeval * tp, struct timezone * tzp);
-#endif
-
-#ifndef timersub
-#define timersub(a, b, result) \
- do { \
- (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
- (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
- if ((result)->tv_usec < 0) { \
- --(result)->tv_sec; \
- (result)->tv_usec += 1000000; \
- } \
- } while (0)
-#endif // timersub
-
-#ifndef dup
-#define dup _dup
-#endif
-
-#ifndef access
-#define access _access
-#endif
-
-#ifndef __cplusplus
-
-#ifndef read
-#define read _read
-#endif
-
-#ifndef close
-#define close _close
-#endif
-
-#ifndef write
-#define write _write
-#endif
-
-#ifndef open
-#define open _open
-#endif
-
-#endif
-
-#ifndef unlink
-#define unlink _unlink
-#endif
-
-#ifndef strcasecmp
-#define strncasecmp _strnicmp
-#endif
-
-#ifndef strcasecmp
-
-#define strcasecmp _stricmp
-#endif
-
-#ifndef S_ISREG
-#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
-#endif
-#ifndef R_OK
-#define R_OK 4 /* Test for read permission. */
-#endif
-#ifndef W_OK
-#define W_OK 2 /* Test for write permission. */
-#endif
-#ifndef F_OK
-#define F_OK 0
-#endif
-
-#ifndef STDIN_FILENO
-#define STDIN_FILENO _fileno(stdin)
-#endif
-
-#ifndef STDOUT_FILENO
-#define STDOUT_FILENO _fileno(stdout)
-#endif
-
-#ifndef STDERR_FILENO
-#define STDERR_FILENO _fileno(stderr)
-#endif
-
-#endif
-
-#ifndef __bswap_constant_32
-#define __bswap_constant_32(x) \
- ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) \
- | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
-#endif
-
-#ifndef bzero
-#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
+/* + * 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. + */ + +#ifndef WINDOWS_UTILS_H +#define WINDOWS_UTILS_H +#define WIN32_LEAN_AND_MEAN +#define HAVE_STRUCT_TIMESPEC +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include <Windows.h> +#include <stdint.h> +#include <io.h> +#include <stdlib.h> +#include <winsock2.h> +#include <WS2tcpip.h> +#include "dlfcn.h" + +#ifndef IOVEC +#define IOVEC +struct iovec +{ + void *iov_base; + size_t iov_len; +}; +#endif + +typedef uint16_t in_port_t; + +#ifndef SLEEP +#define SLEEP +#define sleep Sleep +#endif + +#ifndef USLEEP +#define USLEEP +void usleep (__int64 usec); +#endif + +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) &S_IFMT) == S_IFDIR) +#endif + +#define PARCLibrary_DISABLE_ATOMICS +#include <BaseTsd.h> +typedef SSIZE_T ssize_t; + +#ifndef __ATTRIBUTE__ +#define __ATTRIBUTE__ +#define __attribute__(A) +#endif + +#ifndef RESTRICT +#define RESTRICT +#define restrict __restrict +#endif + +#ifndef GETTIMEOFDAY +#define GETTIMEOFDAY +int gettimeofday (struct timeval *tp, struct timezone *tzp); +#endif + +#ifndef timersub +#define timersub(a, b, result) \ + do \ + { \ + (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ + (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ + if ((result)->tv_usec < 0) \ + { \ + --(result)->tv_sec; \ + (result)->tv_usec += 1000000; \ + } \ + } \ + while (0) +#endif // timersub + +#ifndef dup +#define dup _dup +#endif + +#ifndef access +#define access _access +#endif + +#ifndef __cplusplus + +#ifndef read +#define read _read +#endif + +#ifndef close +#define close _close +#endif + +#ifndef write +#define write _write +#endif + +#ifndef open +#define open _open +#endif + +#endif + +#ifndef unlink +#define unlink _unlink +#endif + +#ifndef strcasecmp +#define strncasecmp _strnicmp +#endif + +#ifndef strcasecmp + +#define strcasecmp _stricmp +#endif + +#ifndef S_ISREG +#define S_ISREG(mode) (((mode) &S_IFMT) == S_IFREG) +#endif +#ifndef R_OK +#define R_OK 4 /* Test for read permission. */ +#endif +#ifndef W_OK +#define W_OK 2 /* Test for write permission. */ +#endif +#ifndef F_OK +#define F_OK 0 +#endif + +#ifndef STDIN_FILENO +#define STDIN_FILENO _fileno (stdin) +#endif + +#ifndef STDOUT_FILENO +#define STDOUT_FILENO _fileno (stdout) +#endif + +#ifndef STDERR_FILENO +#define STDERR_FILENO _fileno (stderr) +#endif + +#endif + +#ifndef __bswap_constant_32 +#define __bswap_constant_32(x) \ + ((((x) &0xff000000u) >> 24) | (((x) &0x00ff0000u) >> 8) | \ + (((x) &0x0000ff00u) << 8) | (((x) &0x000000ffu) << 24)) +#endif + +#ifndef bzero +#define bzero(b, len) (memset ((b), '\0', (len)), (void) 0) #endif
\ No newline at end of file diff --git a/lib/includes/hicn/validation.h b/lib/includes/hicn/validation.h new file mode 100644 index 000000000..05be6d306 --- /dev/null +++ b/lib/includes/hicn/validation.h @@ -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. + */ + +/** + * \file validation.h + * \brief Functions for input validation + */ +#include <ctype.h> +#include <hicn/util/sstrncpy.h> + +static inline bool +is_number (const char *string, size_t maxlen) +{ + size_t len = strnlen_s (string, maxlen); + for (size_t i = 0; i < len; i++) + { + if (!isdigit (string[i])) + return false; + } + return len != 0; +} + +/** + * A symbolic name must be at least 1 character and must begin with an alpha. + * The remainder must be an alphanum. + */ +static inline bool +is_symbolic_name (const char *name, size_t maxlen) +{ + size_t len = strnlen_s (name, maxlen); + if (len <= 0) + return false; + if (!isalpha (name[0])) + return false; + + for (size_t i = 1; i < len; i++) + { + if (!isalnum (name[i]) && name[i] != '_' && name[i] != '-') + return false; + } + + return true; +} 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 |