diff options
author | Jordan Augé <jordan.auge+fdio@cisco.com> | 2019-10-07 09:52:33 +0200 |
---|---|---|
committer | Jordan Augé <jordan.auge+fdio@cisco.com> | 2019-10-07 15:55:42 +0200 |
commit | 6b84ec54083da9911f5ad4816d0eb4f4745afad4 (patch) | |
tree | e4296ebb218fff02dc0bbea73ce1c8d12aba7bcc /lib | |
parent | 85a791ac2cdd35d79c00141e748b4c68fbdafb0d (diff) |
[HICN-298] Release new hICN app for Android
Change-Id: I43adc62fadf00690b687078d739788dffdc5e566
Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/includes/CMakeLists.txt | 55 | ||||
-rw-r--r-- | lib/includes/hicn/base.h (renamed from lib/src/base.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/common.h (renamed from lib/src/common.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/compat.h (renamed from lib/src/compat.h) | 12 | ||||
-rw-r--r-- | lib/includes/hicn/error.h (renamed from lib/src/error.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/header.h (renamed from lib/src/header.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/hicn.h (renamed from lib/src/hicn.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/mapme.h (renamed from lib/src/mapme.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/name.h (renamed from lib/src/name.h) | 60 | ||||
-rw-r--r-- | lib/includes/hicn/ops.h (renamed from lib/src/ops.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/policy.h | 242 | ||||
-rw-r--r-- | lib/includes/hicn/protocol.h (renamed from lib/src/protocol.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/ah.h (renamed from lib/src/protocol/ah.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/icmp.h (renamed from lib/src/protocol/icmp.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/icmprd.h (renamed from lib/src/protocol/icmprd.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/ipv4.h (renamed from lib/src/protocol/ipv4.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/ipv6.h (renamed from lib/src/protocol/ipv6.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/tcp.h (renamed from lib/src/protocol/tcp.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/protocol/udp.h (renamed from lib/src/protocol/udp.h) | 0 | ||||
-rw-r--r-- | lib/includes/hicn/util/ip_address.h | 159 | ||||
-rw-r--r-- | lib/includes/hicn/util/token.h | 40 | ||||
-rw-r--r-- | lib/includes/hicn/util/types.h | 36 | ||||
-rw-r--r-- | lib/src/CMakeLists.txt | 63 | ||||
-rw-r--r-- | lib/src/common.c | 3 | ||||
-rw-r--r-- | lib/src/compat.c | 44 | ||||
-rw-r--r-- | lib/src/error.c | 2 | ||||
-rw-r--r-- | lib/src/mapme.c | 10 | ||||
-rw-r--r-- | lib/src/name.c | 161 | ||||
-rw-r--r-- | lib/src/ops.c | 4 | ||||
-rw-r--r-- | lib/src/policy.c | 59 | ||||
-rw-r--r-- | lib/src/protocol/ah.c | 10 | ||||
-rw-r--r-- | lib/src/protocol/icmp.c | 6 | ||||
-rw-r--r-- | lib/src/protocol/ipv4.c | 10 | ||||
-rw-r--r-- | lib/src/protocol/ipv6.c | 6 | ||||
-rw-r--r-- | lib/src/protocol/tcp.c | 6 | ||||
-rw-r--r-- | lib/src/util/ip_address.c | 315 |
37 files changed, 1002 insertions, 302 deletions
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 9d8267f5f..4df09c0dd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -46,6 +46,7 @@ else () set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996") endif () +add_subdirectory(includes) add_subdirectory (src) add_subdirectory (doc) diff --git a/lib/includes/CMakeLists.txt b/lib/includes/CMakeLists.txt new file mode 100644 index 000000000..d1eaa0b57 --- /dev/null +++ b/lib/includes/CMakeLists.txt @@ -0,0 +1,55 @@ +# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# XXX + +set(HICN_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR} "" + CACHE INTERNAL + "" FORCE +) + + +set(LIBHICN_HEADER_FILES + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/hicn.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/base.h + ${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/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 + PARENT_SCOPE +) + +set(LIBHICN_HEADER_FILES_PROTOCOL + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ah.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/icmp.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/icmprd.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/protocol/ipv4.h + ${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/util/ip_address.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/token.h + ${CMAKE_CURRENT_SOURCE_DIR}/hicn/util/types.h + PARENT_SCOPE +) + diff --git a/lib/src/base.h b/lib/includes/hicn/base.h index d8a79a9c2..d8a79a9c2 100644 --- a/lib/src/base.h +++ b/lib/includes/hicn/base.h diff --git a/lib/src/common.h b/lib/includes/hicn/common.h index 33323da1b..33323da1b 100644 --- a/lib/src/common.h +++ b/lib/includes/hicn/common.h diff --git a/lib/src/compat.h b/lib/includes/hicn/compat.h index 7228843bb..b31d01a0d 100644 --- a/lib/src/compat.h +++ b/lib/includes/hicn/compat.h @@ -234,7 +234,7 @@ int hicn_packet_get_payload (hicn_format_t format, */ int hicn_packet_get_locator (hicn_format_t format, const hicn_header_t * packet, - ip_address_t * ip_address, bool is_interest); + ip_prefix_t * prefix, bool is_interest); /** * @brief Sets the locator of an interest / data packet @@ -246,7 +246,7 @@ int hicn_packet_get_locator (hicn_format_t format, * @return hICN error code */ int hicn_packet_set_locator (hicn_format_t format, hicn_header_t * packet, - const ip_address_t * ip_address, + const ip_prefix_t * prefix, bool is_interest); /** @@ -396,9 +396,9 @@ int hicn_interest_set_name (hicn_format_t format, hicn_header_t * interest, const hicn_name_t * name); int hicn_interest_get_locator (hicn_format_t format, const hicn_header_t * interest, - ip_address_t * ip_address); + ip_prefix_t * prefix); int hicn_interest_set_locator (hicn_format_t format, hicn_header_t * interest, - const ip_address_t * ip_address); + const ip_prefix_t * prefix); int hicn_interest_compare (const hicn_header_t * interest_1, const hicn_header_t * interest_2); int hicn_interest_set_lifetime (hicn_header_t * interest, u32 lifetime); @@ -425,9 +425,9 @@ int hicn_data_get_name (hicn_format_t format, const hicn_header_t * data, int hicn_data_set_name (hicn_format_t format, hicn_header_t * data, const hicn_name_t * name); int hicn_data_get_locator (hicn_format_t format, const hicn_header_t * data, - ip_address_t * ip_address); + ip_prefix_t * prefix); int hicn_data_set_locator (hicn_format_t format, hicn_header_t * data, - const ip_address_t * ip_address); + const ip_prefix_t * prefix); int 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); diff --git a/lib/src/error.h b/lib/includes/hicn/error.h index 3e027c4e5..3e027c4e5 100644 --- a/lib/src/error.h +++ b/lib/includes/hicn/error.h diff --git a/lib/src/header.h b/lib/includes/hicn/header.h index b21fe5c84..b21fe5c84 100644 --- a/lib/src/header.h +++ b/lib/includes/hicn/header.h diff --git a/lib/src/hicn.h b/lib/includes/hicn/hicn.h index 749fd4247..749fd4247 100644 --- a/lib/src/hicn.h +++ b/lib/includes/hicn/hicn.h diff --git a/lib/src/mapme.h b/lib/includes/hicn/mapme.h index 8fae44530..8fae44530 100644 --- a/lib/src/mapme.h +++ b/lib/includes/hicn/mapme.h diff --git a/lib/src/name.h b/lib/includes/hicn/name.h index 434b563b8..0a100dbce 100644 --- a/lib/src/name.h +++ b/lib/includes/hicn/name.h @@ -28,62 +28,14 @@ #ifndef _WIN32 #include <netinet/in.h> // struct sockadd #endif +#include <hicn/util/ip_address.h> #include "common.h" /****************************************************************************** - * IP address helpers - ******************************************************************************/ - -/* Presentation format */ -#ifndef INET_ADDRSTRLEN -#define INET_ADDRSTRLEN 16 -#endif - -#ifndef INET6_ADDRSTRLEN -#define INET6_ADDRSTRLEN 46 -#endif -//#define INET_MAX_ADDRSTRLEN INET6_ADDRSTRLEN - -/* Address size */ -#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 IP_MAX_ADDR_LEN IPV6_ADDR_LEN -#define TCP_SEQNO_LEN 4 /* bytes */ - -struct ip_address -{ - union - { - u8 buffer[IP_MAX_ADDR_LEN]; - u8 as_u8[IP_MAX_ADDR_LEN]; - u16 as_u16[IP_MAX_ADDR_LEN >> 1]; - u32 as_u32[IP_MAX_ADDR_LEN >> 2]; - u64 as_u64[IP_MAX_ADDR_LEN >> 3]; - ip46_address_t as_ip46; - }; - int family; - unsigned short prefix_len; -}; - -typedef struct ip_address ip_address_t; - -int ip_address_len (const ip_address_t * ip_address); -bool ip_address_empty (const ip_address_t * ip_address); - -int hicn_ip_ntop (const ip_address_t * ip_address, char *dst, - const size_t len); -int hicn_ip_pton (const char *ip_address_str, ip_address_t * ip_address); -int hicn_ip_to_sockaddr_address (const ip_address_t * ip_address, - struct sockaddr *sockaddr_address); - -/****************************************************************************** * hICN names ******************************************************************************/ +#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 @@ -207,7 +159,7 @@ 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_address (const ip_address_t * ip_address, u32 id, +int hicn_name_create_from_ip_prefix (const ip_prefix_t * prefix, u32 id, hicn_name_t * name); /** @@ -295,8 +247,8 @@ 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_address (const hicn_name_t * name, - ip_address_t * ip_address); +int hicn_name_to_ip_prefix (const hicn_name_t * name, + ip_prefix_t * ip_prefix); /** * @brief Convert an hICN name to presentation format @@ -329,7 +281,7 @@ 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_address (const ip_address_t * ip_address, +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/src/ops.h b/lib/includes/hicn/ops.h index 47795efd5..47795efd5 100644 --- a/lib/src/ops.h +++ b/lib/includes/hicn/ops.h diff --git a/lib/includes/hicn/policy.h b/lib/includes/hicn/policy.h new file mode 100644 index 000000000..12f8c1e12 --- /dev/null +++ b/lib/includes/hicn/policy.h @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file policy.h + * \brief Policy description + */ +#ifndef HICN_POLICY_H +#define HICN_POLICY_H + +#include <netinet/in.h> // INET*_ADDRSTRLEN +#include <string.h> // strcasecmp +#include <hicn/util/token.h> + +/* 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 MAXSZ_POLICY_TAG_ 11 +#define MAXSZ_POLICY_TAG MAXSZ_POLICY_TAG_ + 1 + +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) +{ +#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; +} + +/* POLICY_TAGS */ + +typedef int policy_tags_t; + +static inline +void policy_tags_add(policy_tags_t * tags, policy_tag_t tag) +{ + *tags |= (1 << tag); +} + +static inline +void policy_tags_remove(policy_tags_t * tags, policy_tag_t tag) +{ + *tags &= ~(1 << tag); +} + +static inline +int policy_tags_has(policy_tags_t tags, policy_tag_t tag) +{ + return tags & (1 << tag); +} + +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 +#undef _ +} + +#define POLICY_TAGS_EMPTY 0 + +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 +#undef _ + s[POLICY_TAG_N] = '\0'; + return POLICY_TAG_N + 1; +} + +#define MAXSZ_POLICY_TAGS_ POLICY_TAG_N +#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 +#undef _ +} policy_state_t; + +#define MAXSZ_POLICY_STATE_ 8 +#define MAXSZ_POLICY_STATE MAXSZ_POLICY_STATE_ + 1 + +extern const char * policy_state_str[]; + + +/* POLICY TAG STATE */ + +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); + + +/* INTERFACE STATS */ + +typedef struct { + float throughput; + float latency; + float loss_rate; +} interface_stats_t; + +#define INTERFACE_STATS_NONE { \ + .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; +} policy_stats_t; + +#define POLICY_STATS_NONE { \ + .wired = INTERFACE_STATS_NONE, \ + .wifi = INTERFACE_STATS_NONE, \ + .cellular = INTERFACE_STATS_NONE, \ + .all = INTERFACE_STATS_NONE, \ +} + +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, \ +} + +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, \ +} + +/* 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; +} policy_t; + +static const policy_t POLICY_NONE = { + .app_name = { 0 }, + .tags = { +#define _(x, y) [POLICY_TAG_ ## x] = { POLICY_STATE_NEUTRAL, 0 }, +foreach_policy_tag +#undef _ + }, + .stats = POLICY_STATS_NONE, +}; + + +/* 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]; + }; + policy_t policy; +} policy_description_t; + +#endif /* HICN_POLICY_H */ diff --git a/lib/src/protocol.h b/lib/includes/hicn/protocol.h index a97cc99cf..a97cc99cf 100644 --- a/lib/src/protocol.h +++ b/lib/includes/hicn/protocol.h diff --git a/lib/src/protocol/ah.h b/lib/includes/hicn/protocol/ah.h index a59a5051a..a59a5051a 100644 --- a/lib/src/protocol/ah.h +++ b/lib/includes/hicn/protocol/ah.h diff --git a/lib/src/protocol/icmp.h b/lib/includes/hicn/protocol/icmp.h index 36954bb6d..36954bb6d 100644 --- a/lib/src/protocol/icmp.h +++ b/lib/includes/hicn/protocol/icmp.h diff --git a/lib/src/protocol/icmprd.h b/lib/includes/hicn/protocol/icmprd.h index aa1fa01ae..aa1fa01ae 100644 --- a/lib/src/protocol/icmprd.h +++ b/lib/includes/hicn/protocol/icmprd.h diff --git a/lib/src/protocol/ipv4.h b/lib/includes/hicn/protocol/ipv4.h index 8a5b6683b..8a5b6683b 100644 --- a/lib/src/protocol/ipv4.h +++ b/lib/includes/hicn/protocol/ipv4.h diff --git a/lib/src/protocol/ipv6.h b/lib/includes/hicn/protocol/ipv6.h index 5a83abcae..5a83abcae 100644 --- a/lib/src/protocol/ipv6.h +++ b/lib/includes/hicn/protocol/ipv6.h diff --git a/lib/src/protocol/tcp.h b/lib/includes/hicn/protocol/tcp.h index ded9a06b2..ded9a06b2 100644 --- a/lib/src/protocol/tcp.h +++ b/lib/includes/hicn/protocol/tcp.h diff --git a/lib/src/protocol/udp.h b/lib/includes/hicn/protocol/udp.h index 75d1ea98c..75d1ea98c 100644 --- a/lib/src/protocol/udp.h +++ b/lib/includes/hicn/protocol/udp.h diff --git a/lib/includes/hicn/util/ip_address.h b/lib/includes/hicn/util/ip_address.h new file mode 100644 index 000000000..728a9da90 --- /dev/null +++ b/lib/includes/hicn/util/ip_address.h @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file ip_address.h + * \brief IP address type + */ +#ifndef UTIL_IP_ADDRESS_H +#define UTIL_IP_ADDRESS_H + +#include <arpa/inet.h> // inet_ntop +#ifdef __APPLE__ +#include <libkern/OSByteOrder.h> +#define __bswap_constant_32(x) OSSwapInt32(x) +#include <machine/endian.h> +#else +#include <endian.h> +#ifdef __ANDROID__ +#include <byteswap.h> +#endif +#include <endian.h> +#endif +#include <errno.h> +#include <netdb.h> // struct addrinfo +#ifndef _WIN32 +#include <netinet/in.h> // struct sockadd +#endif +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> // snprintf +#include <string.h> // memset + +#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) + +/* Presentation format */ +#ifndef INET_ADDRSTRLEN +#define INET_ADDRSTRLEN 16 +#endif + +#ifndef INET6_ADDRSTRLEN +#define INET6_ADDRSTRLEN 46 +#endif +//#define INET_MAX_ADDRSTRLEN INET6_ADDRSTRLEN + +#define IP_MAX_ADDR_LEN IPV6_ADDR_LEN + +#define DUMMY_PORT 1234 + +typedef union { + union { + struct in_addr as_inaddr; + u8 as_u8[4]; + u16 as_u16[2]; + u32 as_u32; + } v4; + union { + struct in6_addr as_in6addr; + u8 as_u8[16]; + u16 as_u16[8]; + u32 as_u32[4]; + u64 as_u64[2]; + } v6; + u8 buffer[IP_MAX_ADDR_LEN]; + u8 as_u8[IP_MAX_ADDR_LEN]; + u16 as_u16[IP_MAX_ADDR_LEN >> 1]; + u32 as_u32[IP_MAX_ADDR_LEN >> 2]; + u64 as_u64[IP_MAX_ADDR_LEN >> 3]; +} ip_address_t; + +#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 + +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 + +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 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 MAXSZ_PORT_ 5 +#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_len (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 (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); + + +/* URL */ + +#define MAXSZ_PROTO_ 8 /* inetX:// */ +#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 + +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/token.h b/lib/includes/hicn/util/token.h new file mode 100644 index 000000000..43e0a77b2 --- /dev/null +++ b/lib/includes/hicn/util/token.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Token concatenation */ + +/* + * 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 + +/* + * Concatenate preprocessor tokens A and B after macro-expanding them. + */ +#define PPCAT(A, B) PPCAT_NX(A, B) + +/* Token stringification */ + +/* + * Turn A into a string literal without expanding macro definitions + * (however, if invoked from a macro, macro arguments are expanded). + */ +#define STRINGIZE_NX(A) #A + +/* + * Turn A into a string literal after macro-expanding it. + */ +#define STRINGIZE(A) STRINGIZE_NX(A) diff --git a/lib/includes/hicn/util/types.h b/lib/includes/hicn/util/types.h new file mode 100644 index 000000000..50b368b8d --- /dev/null +++ b/lib/includes/hicn/util/types.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UTIL_TYPES +#define UTIL_TYPES + +typedef uint8_t u8; +typedef uint16_t u16; +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) + +//typedef unsigned int hash_t; + +typedef int (*cmp_t)(const void *, const void *); + +/* Enums */ + +#define IS_VALID_ENUM_TYPE(NAME, x) ((x > NAME ## _UNDEFINED) && (x < NAME ## _N)) + +#endif /* UTIL_TYPES */ diff --git a/lib/src/CMakeLists.txt b/lib/src/CMakeLists.txt index 65eae8d77..d8c7b11e0 100644 --- a/lib/src/CMakeLists.txt +++ b/lib/src/CMakeLists.txt @@ -13,41 +13,20 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR) -list(APPEND LIBHICN_HEADER_FILES - ${CMAKE_CURRENT_SOURCE_DIR}/hicn.h - ${CMAKE_CURRENT_SOURCE_DIR}/base.h - ${CMAKE_CURRENT_SOURCE_DIR}/common.h - ${CMAKE_CURRENT_SOURCE_DIR}/compat.h - ${CMAKE_CURRENT_SOURCE_DIR}/error.h - ${CMAKE_CURRENT_SOURCE_DIR}/header.h - ${CMAKE_CURRENT_SOURCE_DIR}/mapme.h - ${CMAKE_CURRENT_SOURCE_DIR}/name.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol.h - ${CMAKE_CURRENT_SOURCE_DIR}/ops.h -) - -list(APPEND LIBHICN_HEADER_FILES_PROTOCOL - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ah.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/icmp.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/icmprd.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ipv4.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/ipv6.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/tcp.h - ${CMAKE_CURRENT_SOURCE_DIR}/protocol/udp.h -) - 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}/common.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 ) set (COMPILER_DEFINITIONS "-DWITH_MAPME") @@ -55,48 +34,26 @@ set (COMPILER_DEFINITIONS "-DWITH_MAPME") include(BuildMacros) include(WindowsMacros) -if (ANDROID_API) +if (${CMAKE_SYSTEM_NAME} STREQUAL "Android") build_library(${LIBHICN} STATIC - SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} + SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} COMPONENT lib${LIBHICN} - INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. + INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../includes DEFINITIONS ${COMPILER_DEFINITIONS} INSTALL_ROOT_DIR hicn - INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} + INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} LINK_LIBRARIES ${WSOCK32_LIBRARY} ${WS2_32_LIBRARY} ) else () build_library(${LIBHICN} SHARED STATIC - SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} + SOURCES ${LIBHICN_SOURCE_FILES} ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} COMPONENT lib${LIBHICN} - INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. + INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../includes DEFINITIONS ${COMPILER_DEFINITIONS} INSTALL_ROOT_DIR hicn - INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} + INSTALL_HEADERS ${LIBHICN_HEADER_FILES} ${LIBHICN_HEADER_FILES_PROTOCOL} ${LIBHICN_HEADER_FILES_UTIL} LINK_LIBRARIES ${WSOCK32_LIBRARY} ${WS2_32_LIBRARY} ) endif () -add_custom_command(TARGET ${LIBHICN_STATIC} PRE_BUILD - COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/hicn -) - -add_custom_command(TARGET ${LIBHICN_STATIC} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/hicn/ - COMMAND ${CMAKE_COMMAND} -E copy ${LIBHICN_HEADER_FILES} ${CMAKE_BINARY_DIR}/hicn/ -) - -add_custom_command(TARGET ${LIBHICN_STATIC} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/hicn/protocol - COMMAND ${CMAKE_COMMAND} -E copy ${LIBHICN_HEADER_FILES_PROTOCOL} ${CMAKE_BINARY_DIR}/hicn/protocol -) - -set(HICN_INCLUDE_DIRS - ${CMAKE_BINARY_DIR} "" - CACHE INTERNAL - "" FORCE -) - -# install(FILES ${LIBHICN_HEADER_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/hicn COMPONENT libhicn) -# install(FILES ${LIBHICN_HEADER_FILES_PROTOCOL} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/hicn/protocol COMPONENT libhicn) diff --git a/lib/src/common.c b/lib/src/common.c index de4ece339..78d25b770 100644 --- a/lib/src/common.c +++ b/lib/src/common.c @@ -28,7 +28,8 @@ #endif #include <stdio.h> -#include "common.h" +#include <hicn/common.h> + int diff --git a/lib/src/compat.c b/lib/src/compat.c index 633037a0f..9834b9d44 100644 --- a/lib/src/compat.c +++ b/lib/src/compat.c @@ -23,12 +23,12 @@ #include <string.h> // memset #include <stddef.h> // offsetof -#include "common.h" -#include "compat.h" -#include "error.h" -#include "header.h" -#include "name.h" -#include "ops.h" +#include <hicn/common.h> +#include <hicn/compat.h> +#include <hicn/error.h> +#include <hicn/header.h> +#include <hicn/name.h> +#include <hicn/ops.h> #define member_size(type, member) sizeof(((type *)0)->member) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) @@ -319,7 +319,7 @@ hicn_packet_get_payload (hicn_format_t format, const hicn_header_t * h, int hicn_packet_get_locator (hicn_format_t format, const hicn_header_t * h, - ip_address_t * ip_address, bool is_interest) + ip_prefix_t * prefix, bool is_interest) { const void *locator; int is_ipv4 = (format & HFO_INET); @@ -328,28 +328,28 @@ hicn_packet_get_locator (hicn_format_t format, const hicn_header_t * h, if (is_ipv4) { locator = is_interest ? &h->v4.ip.saddr : &h->v4.ip.daddr; - ip_address->family = AF_INET; - ip_address->prefix_len = IPV4_ADDR_LEN_BITS; + prefix->family = AF_INET; + prefix->len = IPV4_ADDR_LEN_BITS; } else if (is_ipv6) { locator = is_interest ? &h->v6.ip.saddr : &h->v6.ip.daddr; - ip_address->family = AF_INET6; - ip_address->prefix_len = IPV6_ADDR_LEN_BITS; + prefix->family = AF_INET6; + prefix->len = IPV6_ADDR_LEN_BITS; } else { return HICN_LIB_ERROR_NOT_IMPLEMENTED; } - memcpy (ip_address->buffer, locator, ip_address_len (ip_address)); + memcpy (prefix->address.buffer, locator, ip_prefix_len(prefix)); return HICN_LIB_ERROR_NONE; } int hicn_packet_set_locator (hicn_format_t format, hicn_header_t * h, - const ip_address_t * ip_address, bool is_interest) + const ip_prefix_t * prefix, bool is_interest) { void *locator; int is_ipv4 = (format & HFO_INET); @@ -368,7 +368,7 @@ hicn_packet_set_locator (hicn_format_t format, hicn_header_t * h, return HICN_LIB_ERROR_INVALID_PARAMETER; } - memcpy (locator, ip_address->buffer, ip_address_len (ip_address)); + memcpy (locator, prefix->address.buffer, ip_prefix_len(prefix)); return HICN_LIB_ERROR_NONE; } @@ -951,16 +951,16 @@ hicn_interest_set_name (hicn_format_t format, hicn_header_t * interest, int hicn_interest_get_locator (hicn_format_t format, const hicn_header_t * interest, - ip_address_t * ip_address) + ip_prefix_t * prefix) { - return hicn_packet_get_locator (format, interest, ip_address, _INTEREST); + return hicn_packet_get_locator (format, interest, prefix, _INTEREST); } int hicn_interest_set_locator (hicn_format_t format, hicn_header_t * interest, - const ip_address_t * ip_address) + const ip_prefix_t * prefix) { - return hicn_packet_set_locator (format, interest, ip_address, _INTEREST); + return hicn_packet_set_locator (format, interest, prefix, _INTEREST); } int @@ -1043,16 +1043,16 @@ hicn_data_set_name (hicn_format_t format, hicn_header_t * data, int hicn_data_get_locator (hicn_format_t format, const hicn_header_t * data, - ip_address_t * ip_address) + ip_prefix_t * prefix) { - return hicn_packet_get_locator (format, data, ip_address, _DATA); + return hicn_packet_get_locator (format, data, prefix, _DATA); } int hicn_data_set_locator (hicn_format_t format, hicn_header_t * data, - const ip_address_t * ip_address) + const ip_prefix_t * prefix) { - return hicn_packet_set_locator (format, data, ip_address, _DATA); + return hicn_packet_set_locator (format, data, prefix, _DATA); } int diff --git a/lib/src/error.c b/lib/src/error.c index 865e2b47d..ab5d6e27d 100644 --- a/lib/src/error.c +++ b/lib/src/error.c @@ -18,7 +18,7 @@ * @brief Implementation of error management functions. */ -#include "error.h" +#include <hicn/error.h> const char *HICN_LIB_ERROR_STRING[] = { #define _(a,b,c) [b] = c, diff --git a/lib/src/mapme.c b/lib/src/mapme.c index ccd384b21..3e7e8bc80 100644 --- a/lib/src/mapme.c +++ b/lib/src/mapme.c @@ -18,12 +18,12 @@ * @brief Implementation of MAP-Me anchorless producer mobility management. */ -#include "mapme.h" -#include "common.h" -#include "error.h" +#include <hicn/mapme.h> +#include <hicn/common.h> +#include <hicn/error.h> -#include "protocol/ipv4.h" -#include "protocol/ipv6.h" +#include <hicn/protocol/ipv4.h> +#include <hicn/protocol/ipv6.h> size_t hicn_mapme_v4_create_packet (u8 * buf, const hicn_prefix_t * prefix, diff --git a/lib/src/name.c b/lib/src/name.c index 0a0da63cf..1f1051522 100644 --- a/lib/src/name.c +++ b/lib/src/name.c @@ -26,11 +26,9 @@ #include <stdlib.h> // strtoul #include <string.h> // memcpy -#include "common.h" -#include "error.h" -#include "name.h" - -#define DUMMY_PORT ntohs(1234) +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/name.h> #if ! HICN_VPP_PLUGIN int @@ -76,10 +74,10 @@ hicn_name_create (const char *ip_address, u32 id, hicn_name_t * name) } int -hicn_name_create_from_ip_address (const ip_address_t * ip_address, u32 id, +hicn_name_create_from_prefix (const ip_prefix_t * prefix, u32 id, hicn_name_t * name) { - switch (ip_address->family) + switch (prefix->family) { case AF_INET: name->type = HNT_CONTIGUOUS_V4; @@ -91,13 +89,13 @@ hicn_name_create_from_ip_address (const ip_address_t * ip_address, u32 id, return HICN_LIB_ERROR_INVALID_IP_ADDRESS; } - name->len = (u8) ip_address_len (ip_address); + name->len = (u8) (prefix->len); if ((name->type != HNT_CONTIGUOUS_V4) && (name->type != HNT_CONTIGUOUS_V6)) { return HICN_LIB_ERROR_NOT_IMPLEMENTED; } - memcpy (name->buffer, ip_address->buffer, name->len); + memcpy (name->buffer, prefix->address.buffer, ip_prefix_len (prefix)); *(u32 *) (name->buffer + name->len) = id; return HICN_LIB_ERROR_NONE; @@ -391,27 +389,27 @@ hicn_name_to_sockaddr_address (const hicn_name_t * name, } int -hicn_name_to_ip_address (const hicn_name_t * name, ip_address_t * ip_address) +hicn_name_to_ip_prefix (const hicn_name_t * name, ip_prefix_t * prefix) { switch (name->type) { case HNT_CONTIGUOUS_V6: - memcpy (&ip_address->buffer, name->buffer, IPV6_ADDR_LEN); - ip_address->family = AF_INET6; + memcpy (&prefix->address.buffer, name->buffer, IPV6_ADDR_LEN); + prefix->family = AF_INET6; break; case HNT_IOV_V6: - memcpy (&ip_address->buffer, name->iov.buffers[0].iov_base, + memcpy (&prefix->address.buffer, name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len); - ip_address->family = AF_INET6; + prefix->family = AF_INET6; break; case HNT_CONTIGUOUS_V4: - memcpy (&ip_address->buffer, name->buffer, IPV4_ADDR_LEN); - ip_address->family = AF_INET; + memcpy (&prefix->address.buffer, name->buffer, IPV4_ADDR_LEN); + prefix->family = AF_INET; break; case HNT_IOV_V4: - memcpy (&ip_address->buffer, name->iov.buffers[0].iov_base, + memcpy (&prefix->address.buffer, name->iov.buffers[0].iov_base, name->iov.buffers[0].iov_len); - ip_address->family = AF_INET; + prefix->family = AF_INET; break; default: return HICN_LIB_ERROR_UNEXPECTED; @@ -525,143 +523,28 @@ hicn_name_get_family (const hicn_name_t * name, int *family) } int -hicn_prefix_create_from_ip_address (const ip_address_t * ip_address, +hicn_prefix_create_from_ip_prefix (const ip_prefix_t * ip_prefix, hicn_prefix_t * prefix) { - switch (ip_address->family) + switch (ip_prefix->family) { case AF_INET: - prefix->name.ip4.as_u32 = ip_address->as_u32[0]; + prefix->name.ip4.as_u32 = ip_prefix->address.as_u32[0]; break; case AF_INET6: - prefix->name.ip6.as_u64[0] = ip_address->as_u64[0]; - prefix->name.ip6.as_u64[1] = ip_address->as_u64[1]; + prefix->name.ip6.as_u64[0] = ip_prefix->address.as_u64[0]; + prefix->name.ip6.as_u64[1] = ip_prefix->address.as_u64[1]; break; default: return HICN_LIB_ERROR_INVALID_IP_ADDRESS; } - prefix->len = (u8) (ip_address->prefix_len); + prefix->len = (u8) (ip_prefix->len); return HICN_LIB_ERROR_NONE; } #endif /* ! HICN_VPP_PLUGIN */ -/******** - * IP - */ - -inline int -ip_address_len (const ip_address_t * ip_address) -{ - return (ip_address->family == AF_INET6) ? IPV6_ADDR_LEN : - (ip_address->family == AF_INET) ? IPV4_ADDR_LEN : 0; -} - -bool -ip_address_empty (const ip_address_t * ip_address) -{ - return ip_address->prefix_len == 0; -} - -int -hicn_ip_ntop (const ip_address_t * ip_address, char *dst, const size_t len) -{ - const char *rc; - - rc = inet_ntop (ip_address->family, ip_address->buffer, dst, len); - if (!rc) - { - printf ("error ntop: %d %s\n", errno, strerror (errno)); - return HICN_LIB_ERROR_INVALID_IP_ADDRESS; - } - - return HICN_LIB_ERROR_NONE; -} - -/* - * Parse ip addresses in presentation format, or prefixes (in bits, separated by a slash) - */ -int -hicn_ip_pton (const char *ip_address_str, ip_address_t * ip_address) -{ - int pton_fd; - char *p; - char *eptr; - u32 dst_len; - char *addr = strdup (ip_address_str); - - p = strchr (addr, '/'); - if (!p) - { - dst_len = 0; // until we get the ip address family - } - else - { - dst_len = strtoul (p + 1, &eptr, 10); - *p = 0; - } - - ip_address->family = get_addr_family (addr); - - switch (ip_address->family) - { - case AF_INET6: - if (dst_len > IPV6_ADDR_LEN_BITS) - goto ERR; - pton_fd = inet_pton (AF_INET6, addr, &ip_address->buffer); - ip_address->prefix_len = dst_len ? dst_len : IPV6_ADDR_LEN_BITS; - break; - case AF_INET: - if (dst_len > IPV4_ADDR_LEN_BITS) - goto ERR; - pton_fd = inet_pton (AF_INET, addr, &ip_address->buffer); - ip_address->prefix_len = dst_len ? dst_len : IPV4_ADDR_LEN_BITS; - break; - default: - goto ERR; - } - - // 0 = not in presentation format - // < 0 = other error (use perror) - if (pton_fd <= 0) - { - goto ERR; - } - - return HICN_LIB_ERROR_NONE; -ERR: - free (addr); - return HICN_LIB_ERROR_INVALID_IP_ADDRESS; -} - -int -hicn_ip_to_sockaddr_address (const ip_address_t * ip_address, - struct sockaddr *sockaddr_address) -{ - struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) sockaddr_address; - struct sockaddr_in *tmp4 = (struct sockaddr_in *) sockaddr_address; - - switch (ip_address->family) - { - case AF_INET6: - tmp6->sin6_family = AF_INET6; - tmp6->sin6_port = DUMMY_PORT; - tmp6->sin6_scope_id = 0; - memcpy (&tmp6->sin6_addr, ip_address->buffer, IPV6_ADDR_LEN); - break; - case AF_INET: - tmp4->sin_family = AF_INET; - tmp4->sin_port = DUMMY_PORT; - memcpy (&tmp4->sin_addr, ip_address->buffer, IPV4_ADDR_LEN); - break; - default: - return HICN_LIB_ERROR_UNEXPECTED; - } - - return HICN_LIB_ERROR_NONE; -} - /* * fd.io coding-style-patch-verification: ON diff --git a/lib/src/ops.c b/lib/src/ops.c index 3e272572a..9bb78be65 100644 --- a/lib/src/ops.c +++ b/lib/src/ops.c @@ -22,9 +22,9 @@ #include <netinet/in.h> #endif #include <stdlib.h> -#include "ops.h" +#include <hicn/ops.h> -#include "header.h" +#include <hicn/header.h> extern const hicn_ops_t hicn_ops_ipv4; extern const hicn_ops_t hicn_ops_icmp; diff --git a/lib/src/policy.c b/lib/src/policy.c new file mode 100644 index 000000000..336249bbb --- /dev/null +++ b/lib/src/policy.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file policy.h + * \brief Implementation of policy description + */ + +#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_short_str[] = { + #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 _ +}; + +int +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 < 0) + return rc; + cur += rc; + if (size != 0 && cur >= s + size) + return cur - s; + + return cur - s; +} diff --git a/lib/src/protocol/ah.c b/lib/src/protocol/ah.c index 5a30f66f6..c2f3f552a 100644 --- a/lib/src/protocol/ah.c +++ b/lib/src/protocol/ah.c @@ -19,11 +19,11 @@ */ #include <string.h> // memcpy -#include "../common.h" -#include "../error.h" -#include "../header.h" -#include "../ops.h" -#include "ah.h" +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/header.h> +#include <hicn/ops.h> +#include <hicn/protocol/ah.h> DECLARE_get_interest_locator (ah, UNEXPECTED); DECLARE_set_interest_locator (ah, UNEXPECTED); diff --git a/lib/src/protocol/icmp.c b/lib/src/protocol/icmp.c index 45a28959c..85605a2c3 100644 --- a/lib/src/protocol/icmp.c +++ b/lib/src/protocol/icmp.c @@ -14,10 +14,10 @@ */ #include <string.h> -#include "icmp.h" +#include <hicn/protocol/icmp.h> -#include "../error.h" -#include "../ops.h" +#include <hicn/error.h> +#include <hicn/ops.h> DECLARE_get_interest_locator (icmp, UNEXPECTED) DECLARE_set_interest_locator (icmp, UNEXPECTED) diff --git a/lib/src/protocol/ipv4.c b/lib/src/protocol/ipv4.c index 3c82b3223..d8d958350 100644 --- a/lib/src/protocol/ipv4.c +++ b/lib/src/protocol/ipv4.c @@ -27,12 +27,12 @@ #include <stdlib.h> #include <string.h> -#include "../error.h" -#include "../ops.h" -#include "../common.h" -#include "../header.h" +#include <hicn/error.h> +#include <hicn/ops.h> +#include <hicn/common.h> +#include <hicn/header.h> -#include "ipv4.h" +#include <hicn/protocol/ipv4.h> int ipv4_get_payload_length (hicn_type_t type, const hicn_protocol_t * h, size_t * payload_length); diff --git a/lib/src/protocol/ipv6.c b/lib/src/protocol/ipv6.c index 7554eadf2..622355294 100644 --- a/lib/src/protocol/ipv6.c +++ b/lib/src/protocol/ipv6.c @@ -16,9 +16,9 @@ #include <stdlib.h> #include <string.h> -#include "../common.h" -#include "../error.h" -#include "../ops.h" +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/ops.h> int ipv6_get_payload_length (hicn_type_t type, const hicn_protocol_t * h, diff --git a/lib/src/protocol/tcp.c b/lib/src/protocol/tcp.c index b9f1d2775..0e3155020 100644 --- a/lib/src/protocol/tcp.c +++ b/lib/src/protocol/tcp.c @@ -14,10 +14,10 @@ */ #include <string.h> -#include "tcp.h" +#include <hicn/protocol/tcp.h> -#include "../error.h" -#include "../ops.h" +#include <hicn/error.h> +#include <hicn/ops.h> #define TCP_DEFAULT_SRC_PORT 0x8000 #define TCP_DEFAULT_DST_PORT 0x0080 diff --git a/lib/src/util/ip_address.c b/lib/src/util/ip_address.c new file mode 100644 index 000000000..ea238167f --- /dev/null +++ b/lib/src/util/ip_address.c @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * \file ip_address.c + * \brief Implementation of IP address type + */ + +#include <hicn/util/ip_address.h> + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#ifdef __ANDROID__ +#define SWAP(x) bswap_32(x) +#else +#define SWAP(x) __bswap_constant_32(x) +#endif +#else +#define SWAP(x) x +#endif + + +/* No htonl() with const */ +const ip_address_t IPV4_LOOPBACK = (ip_address_t) { + .v4.as_inaddr.s_addr = SWAP(INADDR_LOOPBACK), +}; + +const ip_address_t IPV6_LOOPBACK = (ip_address_t) { + .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT, +}; + +const ip_address_t IPV4_ANY = (ip_address_t) { + .v4.as_inaddr.s_addr = INADDR_ANY, +}; + +const ip_address_t IPV6_ANY = (ip_address_t) { + .v6.as_in6addr = IN6ADDR_ANY_INIT, +}; + +const ip_address_t IP_ADDRESS_EMPTY = { + .as_u64 = { 0 }, +}; + + +/* IP address */ + +int +ip_address_get_family (const char * ip_address) +{ + struct addrinfo hint, *res = NULL; + int rc; + + memset (&hint, '\0', sizeof hint); + + hint.ai_family = PF_UNSPEC; + hint.ai_flags = AI_NUMERICHOST; + + rc = getaddrinfo (ip_address, NULL, &hint, &res); + if (rc) + { + return -1; + } + rc = res->ai_family; + freeaddrinfo (res); + return rc; +} + +int +ip_address_len (const ip_address_t * ip_address, int family) +{ + return (family == AF_INET6) ? IPV6_ADDR_LEN : + (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) +{ + const char * s = inet_ntop (family, ip_address->buffer, dst, len); + return (s ? 1 : -1); +} + +/* + * Parse ip addresses in presentation format + */ +int +ip_address_pton (const char *ip_address_str, ip_address_t * ip_address) +{ + int pton_fd; + char *addr = strdup (ip_address_str); + int family; + + + family = ip_address_get_family (addr); + + switch (family) + { + case AF_INET6: + pton_fd = inet_pton (AF_INET6, addr, &ip_address->buffer); + break; + case AF_INET: + pton_fd = inet_pton (AF_INET, addr, &ip_address->buffer); + break; + default: + goto ERR; + } + + // 0 = not in presentation format + // < 0 = other error (use perror) + if (pton_fd <= 0) + { + goto ERR; + } + + return 1; +ERR: + free (addr); + return -1; +} + +int +ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address, int family) +{ + size_t len = family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; + const char * rc = inet_ntop (family, ip_address->buffer, s, len); + return rc ? strlen(rc) : -1; +} + +int +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; + + switch (family) + { + case AF_INET6: + tmp6->sin6_family = AF_INET6; + tmp6->sin6_port = DUMMY_PORT; + tmp6->sin6_scope_id = 0; + memcpy (&tmp6->sin6_addr, ip_address->buffer, IPV6_ADDR_LEN); + break; + case AF_INET: + tmp4->sin_family = AF_INET; + tmp4->sin_port = DUMMY_PORT; + memcpy (&tmp4->sin_addr, ip_address->buffer, IPV4_ADDR_LEN); + break; + default: + return -1; + } + + return 1; +} + +int +ip_address_cmp(const ip_address_t * ip1, const ip_address_t * ip2, int family) +{ + return memcmp(ip1, ip2, ip_address_len(ip1, family)); +} + +int +ip_address_empty(const ip_address_t * ip) +{ + return (memcmp(ip, &IP_ADDRESS_EMPTY, 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) +{ + int pton_fd; + char *p; + char *eptr; + 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 = 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 > IPV6_ADDR_LEN_BITS) + goto ERR; + pton_fd = inet_pton (AF_INET6, addr, &ip_prefix->address.buffer); + break; + case AF_INET: + if (ip_prefix->len > IPV4_ADDR_LEN_BITS) + goto ERR; + pton_fd = inet_pton (AF_INET, addr, &ip_prefix->address.buffer); + break; + default: + goto ERR; + } + + // 0 = not in presentation format + // < 0 = other error (use perror) + if (pton_fd <= 0) + goto ERR; + + return 1; +ERR: + free (addr); + return -1; +} + +int +ip_prefix_ntop (const ip_prefix_t * ip_prefix, char *dst, size_t size) +{ + char ip_s[MAXSZ_IP_ADDRESS]; + const char * s = inet_ntop (ip_prefix->family, ip_prefix->address.buffer, ip_s, MAXSZ_IP_ADDRESS); + if (!s) + return -1; + size_t n = snprintf(dst, size, "%s/%d", ip_s, ip_prefix->len); + + return (n > 0 ? 1 : -1); +} + +int +ip_prefix_len (const ip_prefix_t * prefix) +{ + return prefix->len; // ip_address_len(&prefix->address, prefix->family); +} + +bool +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) +{ + // XXX assert len == ip_address_len + return ip_address_to_sockaddr(&prefix->address, sa, prefix->family); +} + + +/* URL */ + +#define MAXSZ_PROTO_ 8 /* inetX:// */ +#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 + +int +url_snprintf(char * s, size_t size, int family, + const ip_address_t * ip_address, u16 port) +{ + char * cur = s; + int rc; + + /* Other address are currently not supported */ + if (!IS_VALID_FAMILY(family)) { + return -1; + } + + rc = snprintf(cur, s + size - cur, "inet%c://", + (family == AF_INET) ? '4' : '6'); + if (rc < 0) + return rc; + cur += rc; + if (size != 0 && cur >= s + size) + return cur - s; + + rc = ip_address_snprintf(cur, s + size - cur, ip_address, family); + if (rc < 0) + return rc; + cur += rc; + if (size != 0 && cur >= s + size) + return cur - s; + + rc = snprintf(cur, s + size - cur, ":"); + if (rc < 0) + return rc; + cur += rc; + if (size != 0 && cur >= s + size) + return cur - s; + + rc = snprintf(cur, s + size - cur, "%d", port); + if (rc < 0) + return rc; + cur += rc; + if (size != 0 && cur >= s + size) + return cur - s; + + return cur - s; +} |