From af389692c0a687675c74fd432e3a2309337ad3c9 Mon Sep 17 00:00:00 2001 From: "Enrico Loparco (eloparco)" Date: Thu, 24 Jun 2021 17:04:19 +0200 Subject: [HICN-712] Fix listener table retrieval Signed-off-by: Enrico Loparco (eloparco) Change-Id: I8cd7c37a570011c2215255fab5e020291dfd0ef7 --- hicn-light/src/hicn/config/command.h | 6 +-- hicn-light/src/hicn/config/configuration.c | 9 +++-- hicn-light/src/hicn/core/listener.c | 12 ++---- hicn-light/src/hicn/core/listener_table.c | 10 +++++ hicn-light/src/hicn/core/listener_table.h | 5 ++- .../src/hicn/core/test/test-listener_table.cc | 44 ++++++++++++++++++++++ 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/hicn-light/src/hicn/config/command.h b/hicn-light/src/hicn/config/command.h index 4cc770e0d..ddeb94b03 100644 --- a/hicn-light/src/hicn/config/command.h +++ b/hicn-light/src/hicn/config/command.h @@ -13,7 +13,7 @@ /* Update sscanf accordingly in parse_cmd.c */ #define MAX_PARAMETERS 10 -#define MAX_SCANF_PARAM_LEN 10 +#define MAX_SCANF_PARAM_LEN 100 typedef int (*parser_hook_t)(void * arg); @@ -126,7 +126,7 @@ typedef struct { }, \ } /* We need to allocate room for the intermediate string */ -#define TYPE_FMT_ENUM "%ms" +#define TYPE_FMT_ENUM "%s" #define TYPE_POLICY_STATE(TAG) (parser_type_t) { \ .name = TYPENAME_POLICY_STATE, \ @@ -135,7 +135,7 @@ typedef struct { }, \ } /* We need to allocate room for the intermediate string */ -#define TYPE_FMT_POLICY_STATE "%ms" +#define TYPE_FMT_POLICY_STATE "%s" /** * \brief Register a protocol diff --git a/hicn-light/src/hicn/config/configuration.c b/hicn-light/src/hicn/config/configuration.c index 591352377..f56ce73ce 100644 --- a/hicn-light/src/hicn/config/configuration.c +++ b/hicn-light/src/hicn/config/configuration.c @@ -198,8 +198,10 @@ configuration_on_listener_add(configuration_t * config, uint8_t * packet, /* Verify that the listener DOES NOT exist */ listener_t * listener = listener_table_get_by_name(table, control->symbolic); - if (listener) + if (listener) { + DEBUG("Listener %s already exists", control->symbolic); goto NACK; + } address_t address; if (address_from_ip_port(&address, control->family, &control->address, @@ -384,10 +386,9 @@ configuration_on_connection_add(configuration_t * config, uint8_t * packet, const char *symbolic_name = control->symbolic; - face_type_t face_type; - if (!face_type_is_defined(control->type)) + face_type_t face_type = get_face_type_from_listener_type((hc_connection_type_t) control->type); + if (!face_type_is_defined(face_type)) goto NACK; - face_type = (face_type_t)control->type; connection_table_t * table = forwarder_get_connection_table(config->forwarder); if (connection_table_get_by_name(table, symbolic_name)) { diff --git a/hicn-light/src/hicn/core/listener.c b/hicn-light/src/hicn/core/listener.c index 7c3ba5235..01f62bb39 100644 --- a/hicn-light/src/hicn/core/listener.c +++ b/hicn-light/src/hicn/core/listener.c @@ -39,7 +39,10 @@ listener_create(face_type_t type, const address_t * address, .type = type, .address = *address, }; - listener_table_allocate(table, listener, &key, name); + listener_table_allocate(table, listener, &key, strdup(name)); + WITH_DEBUG( + listener_table_print(table); + ) unsigned listener_id = listener_table_get_listener_id(table, listener); @@ -376,13 +379,6 @@ listener_setup_all(const forwarder_t * forwarder, uint16_t port, const char *loc void listener_setup_local_ipv4(forwarder_t * forwarder, uint16_t port) { -#if 0 - // XXX memset - address_t address = ADDRESS4_LOCALHOST(port); - - _setupUdpListener(forwarder, "lo_udp", &address, "lo"); - _setupTcpListener(forwarder, "lo_tcp", &address, "lo"); -#endif address_t address; memset(&address, 0, sizeof(address_t)); address = ADDRESS4_LOCALHOST(port); diff --git a/hicn-light/src/hicn/core/listener_table.c b/hicn-light/src/hicn/core/listener_table.c index 7a3987c37..560c23f20 100644 --- a/hicn-light/src/hicn/core/listener_table.c +++ b/hicn-light/src/hicn/core/listener_table.c @@ -107,4 +107,14 @@ listener_table_get_by_name(listener_table_t * table, const char * name) listener_t *_listener_table_get_by_id(listener_table_t *table, off_t id) { return listener_table_get_by_id(table, id); +} + +void listener_table_print(const listener_table_t *table) { + const char *k; + unsigned v; + + printf("*** Listener table ***\n"); + kh_foreach(table->id_by_name, k, v, { + printf("%s:\t%u\n", k, v); + }) } \ No newline at end of file diff --git a/hicn-light/src/hicn/core/listener_table.h b/hicn-light/src/hicn/core/listener_table.h index 292c489a5..91e985221 100644 --- a/hicn-light/src/hicn/core/listener_table.h +++ b/hicn-light/src/hicn/core/listener_table.h @@ -253,6 +253,9 @@ listener_t * listener_table_get_by_name(listener_table_t * table, */ void listener_table_remove_by_id(listener_table_t * table, off_t id); -unsigned listener_table_add(listener_table_t * table, listener_t * listener); +// TODO: not implemented yet +unsigned listener_table_add(const listener_table_t * table, listener_t * listener); + +void listener_table_print(const listener_table_t *table); #endif /* HICNLIGHT_LISTENER_TABLE_H */ diff --git a/hicn-light/src/hicn/core/test/test-listener_table.cc b/hicn-light/src/hicn/core/test/test-listener_table.cc index 89bc3357f..34db546fc 100644 --- a/hicn-light/src/hicn/core/test/test-listener_table.cc +++ b/hicn-light/src/hicn/core/test/test-listener_table.cc @@ -14,6 +14,7 @@ */ #include +#include #include #include @@ -29,6 +30,7 @@ extern "C" { } #define LISTENER_NAME "listener_name_test" +#define LISTENER_NAME_2 "listener_name_test_2" class ListenerTableTest : public ::testing::Test { protected: @@ -142,6 +144,48 @@ TEST_F(ListenerTableTest, RemoveListener) EXPECT_EQ(listener_not_found, nullptr); } +TEST_F(ListenerTableTest, PrintTable) +{ + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + + listener_t *listener_2; + listener_key_t key_2 = { + .address = _ADDRESS4_LOCALHOST(2), + .type = FACE_TYPE_TCP + }; + listener_table_allocate(listener_table, listener_2, &key_2, LISTENER_NAME_2); + + testing::internal::CaptureStdout(); + listener_table_print(listener_table); + std::string std_out = testing::internal::GetCapturedStdout(); + + ASSERT_NE(std_out, ""); + EXPECT_THAT(std_out, testing::HasSubstr(LISTENER_NAME)); + EXPECT_THAT(std_out, testing::HasSubstr(LISTENER_NAME_2)); +} + +TEST_F(ListenerTableTest, AddMultipleListeners) +{ + listener_table_allocate(listener_table, listener, &key, LISTENER_NAME); + + listener_t *listener_2; + listener_key_t key_2 = { + .address = _ADDRESS4_LOCALHOST(2), + .type = FACE_TYPE_TCP + }; + listener_table_allocate(listener_table, listener_2, &key_2, "listener_name_test_2"); + + // Check listener table size + size_t listener_table_size = listener_table_len(listener_table); + EXPECT_EQ(listener_table_size, (size_t) 2); + + listener_t *l1 = listener_table_get_by_name(listener_table, LISTENER_NAME); + ASSERT_NE(l1, nullptr); + listener_t *l2 = listener_table_get_by_name(listener_table, LISTENER_NAME_2); + ASSERT_NE(l2, nullptr); + EXPECT_NE(l1, l2); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); -- cgit 1.2.3-korg