From 18b9d85936135c4430dccc4a359907106e8eac3a Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Fri, 11 May 2018 18:06:14 +0200 Subject: [PATCH 01/16] transport: add TransportFactory interface Signed-off-by: Laszlo Budai --- lib/transport/CMakeLists.txt | 1 + lib/transport/Makefile.am | 3 +- lib/transport/tests/CMakeLists.txt | 1 + lib/transport/tests/Makefile.am | 9 +- lib/transport/tests/test_transport_factory.c | 117 +++++++++++++++++++ lib/transport/transport-factory-id.h | 14 ++- lib/transport/transport-factory.h | 67 +++++++++++ 7 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 lib/transport/tests/test_transport_factory.c create mode 100644 lib/transport/transport-factory.h diff --git a/lib/transport/CMakeLists.txt b/lib/transport/CMakeLists.txt index 6ea7e75e0c3..c6042838667 100644 --- a/lib/transport/CMakeLists.txt +++ b/lib/transport/CMakeLists.txt @@ -6,6 +6,7 @@ set(TRANSPORT_HEADERS transport/transport-pipe.h transport/transport-socket.h transport/transport-factory-id.h + transport/transport-factory.h PARENT_SCOPE) set(TRANSPORT_SOURCES diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am index 5fb7e346bd6..08220660beb 100644 --- a/lib/transport/Makefile.am +++ b/lib/transport/Makefile.am @@ -7,7 +7,8 @@ transportinclude_HEADERS = \ lib/transport/transport-file.h \ lib/transport/transport-pipe.h \ lib/transport/transport-socket.h \ - lib/transport/transport-factory-id.h + lib/transport/transport-factory-id.h \ + lib/transport/transport-factory.h transport_sources = \ lib/transport/logtransport.c \ diff --git a/lib/transport/tests/CMakeLists.txt b/lib/transport/tests/CMakeLists.txt index 75d28d665a4..02348a8b494 100644 --- a/lib/transport/tests/CMakeLists.txt +++ b/lib/transport/tests/CMakeLists.txt @@ -1,2 +1,3 @@ add_unit_test(LIBTEST TARGET test_aux_data) add_unit_test(LIBTEST TARGET test_transport_factory_id) +add_unit_test(LIBTEST TARGET test_transport_factory) diff --git a/lib/transport/tests/Makefile.am b/lib/transport/tests/Makefile.am index b72b9e15de4..8c334055328 100644 --- a/lib/transport/tests/Makefile.am +++ b/lib/transport/tests/Makefile.am @@ -1,6 +1,7 @@ lib_transport_tests_TESTS = \ lib/transport/tests/test_aux_data \ - lib/transport/tests/test_transport_factory_id + lib/transport/tests/test_transport_factory_id \ + lib/transport/tests/test_transport_factory check_PROGRAMS += ${lib_transport_tests_TESTS} @@ -15,3 +16,9 @@ lib_transport_tests_test_transport_factory_id_CFLAGS = $(TEST_CFLAGS) \ lib_transport_tests_test_transport_factory_id_LDADD = $(TEST_LDADD) lib_transport_tests_test_transport_factory_id_SOURCES = \ lib/transport/tests/test_transport_factory_id.c + +lib_transport_tests_test_transport_factory_CFLAGS = $(TEST_CFLAGS) \ + -I${top_srcdir}/lib/transport/tests +lib_transport_tests_test_transport_factory_LDADD = $(TEST_LDADD) +lib_transport_tests_test_transport_factory_SOURCES = \ + lib/transport/tests/test_transport_factory.c diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c new file mode 100644 index 00000000000..8666aee272a --- /dev/null +++ b/lib/transport/tests/test_transport_factory.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2002-2018 Balabit + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/transport-factory.h" +#include "apphook.h" +#include + +TestSuite(transport_factory_id, .init = app_startup, .fini = app_shutdown); + +typedef struct _FakeTransport FakeTransport; +typedef struct _FakeTransportFactory FakeTransportFactory; + +struct _FakeTransport +{ + LogTransport super; + gboolean constructed; +}; + +static gssize +_fake_read(LogTransport *s, gpointer buf, gsize count, LogTransportAuxData *auc) +{ + return count; +} + +static gssize +_fake_write(LogTransport *s, const gpointer buf, gsize count) +{ + return count; +} + +LogTransport * +_fake_transport_new(void) +{ + FakeTransport *instance = g_new0(FakeTransport, 1); + + instance->super.read = _fake_read; + instance->super.write = _fake_write; + instance->constructed = TRUE; + + return &instance->super; +} + +struct _FakeTransportFactory +{ + TransportFactory super; + gboolean destroyed; +}; + +DEFINE_TRANSPORT_FACTORY_ID_FUN(_fake_transport_factory_id); + +static LogTransport * +_transport_factory_construct(TransportFactory *s, gint fd) +{ + FakeTransportFactory *self = (FakeTransportFactory *)s; + + LogTransport *fake_transport = _fake_transport_new(); + log_transport_init_instance(fake_transport, fd); + + return fake_transport; +} + +static void +_transport_factory_destroy(TransportFactory *s) +{ + FakeTransportFactory *self = (FakeTransportFactory *)s; + self->destroyed = TRUE; +} + +TransportFactory * +_fake_transport_factory_new(void) +{ + FakeTransportFactory *instance = g_new0(FakeTransportFactory, 1); + instance->super.id = _fake_transport_factory_id(); + instance->super.construct = _transport_factory_construct; + instance->super.destroy = _transport_factory_destroy; //TODO: confusing the naming: construct_transport... destroy_self + return &instance->super; +} + +TestSuite(transport_factory, .init = app_startup, .fini = app_shutdown); + +Test(transport_factory, fake_transport_factory) +{ + FakeTransportFactory *fake_factory = (FakeTransportFactory *)_fake_transport_factory_new(); + cr_expect_not_null(fake_factory->super.id); + cr_expect_eq(fake_factory->destroyed, FALSE); + + gint fd = 11; + FakeTransport *fake_transport = (FakeTransport *) transport_factory_construct(&fake_factory->super, fd); + cr_expect_eq(fake_transport->constructed, TRUE); + cr_expect_eq(fake_transport->super.read, _fake_read); + cr_expect_eq(fake_transport->super.write, _fake_write); + log_transport_free(&fake_transport->super); + + transport_factory_destroy(&fake_factory->super); + cr_expect_eq(fake_factory->destroyed, TRUE); +} + diff --git a/lib/transport/transport-factory-id.h b/lib/transport/transport-factory-id.h index 49f553a5038..0298b207d26 100644 --- a/lib/transport/transport-factory-id.h +++ b/lib/transport/transport-factory-id.h @@ -37,10 +37,22 @@ void transport_factory_id_free(gpointer); gpointer transport_factory_id_clone(gconstpointer); const gchar *transport_factory_id_to_string(TransportFactoryId *); -#define TRANSPORT_FACTORY_ID_NEW(name) ({GString *str = g_string_new(""); g_string_printf(str, "%s-%s:%s:%d", name, __FILE__, __func__, __LINE__); str;}) +#define TRANSPORT_FACTORY_ID_NEW(name) ({GString *str = g_string_new(""); g_string_printf(str, "%s-%s:%d", name, __FILE__, __LINE__); str;}) #define TRANSPORT_FACTORY_ID_FREE_FUNC transport_factory_id_free #define TRANSPORT_FACTORY_ID_HASH_FUNC g_string_hash #define TRANSPORT_FACTORY_ID_CMP_FUNC g_string_equal #define TRANSPORT_FACTORY_ID_CLONE transport_factory_id_clone +#define DEFINE_TRANSPORT_FACTORY_ID_FUN(name) \ + const TransportFactoryId* name(void) \ + {\ + static TransportFactoryId *id;\ + if (!id)\ + {\ + id = TRANSPORT_FACTORY_ID_NEW(#name);\ + transport_factory_id_register(id);\ + }\ + return id;\ + } + #endif diff --git a/lib/transport/transport-factory.h b/lib/transport/transport-factory.h new file mode 100644 index 00000000000..f07532ad1aa --- /dev/null +++ b/lib/transport/transport-factory.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) 2018 Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef TRANSPORT_FACTORY_H_INCLUDED +#define TRANSPORT_FACTORY_H_INCLUDED + +#include "transport/logtransport.h" +#include "transport/transport-factory-id.h" + +/* TransportFactory is an interface for representing + * concrete TransportFactory instances + * Each TransportFactory has + * - a reference to a unique id + * - a construct method for creating new LogTransport instances + * - a destroy method for releasing resources that are needed for construct() + */ +typedef struct _TransportFactory TransportFactory; + +struct _TransportFactory +{ + const TransportFactoryId *id; + LogTransport *(*construct)(TransportFactory *self, gint fd); + void (*destroy)(TransportFactory *self); +}; + +static inline LogTransport *transport_factory_construct(TransportFactory *self, gint fd) +{ + g_assert(self->construct); + return self->construct(self, fd); +} + +static inline void transport_factory_destroy(TransportFactory *self) +{ + if (self->destroy) + self->destroy(self); +} + +static inline const TransportFactoryId *transport_factory_id(TransportFactory *self) +{ + /* each concrete TransportFactory has to have an id + */ + g_assert(self->id); + return self->id; +} + +#endif From 07000e80fa81b393556ec94a0af78dac7c4a065d Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Tue, 15 May 2018 15:47:24 +0200 Subject: [PATCH 02/16] transport-factory: fix internal api Signed-off-by: Laszlo Budai --- lib/transport/tests/test_transport_factory.c | 10 +++++----- lib/transport/transport-factory.h | 17 +++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index 8666aee272a..3e5463965ec 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -80,7 +80,7 @@ _transport_factory_construct(TransportFactory *s, gint fd) } static void -_transport_factory_destroy(TransportFactory *s) +_transport_factory_free(TransportFactory *s) { FakeTransportFactory *self = (FakeTransportFactory *)s; self->destroyed = TRUE; @@ -91,8 +91,8 @@ _fake_transport_factory_new(void) { FakeTransportFactory *instance = g_new0(FakeTransportFactory, 1); instance->super.id = _fake_transport_factory_id(); - instance->super.construct = _transport_factory_construct; - instance->super.destroy = _transport_factory_destroy; //TODO: confusing the naming: construct_transport... destroy_self + instance->super.construct_transport = _transport_factory_construct; + instance->super.free_fn = _transport_factory_free; return &instance->super; } @@ -105,13 +105,13 @@ Test(transport_factory, fake_transport_factory) cr_expect_eq(fake_factory->destroyed, FALSE); gint fd = 11; - FakeTransport *fake_transport = (FakeTransport *) transport_factory_construct(&fake_factory->super, fd); + FakeTransport *fake_transport = (FakeTransport *) transport_factory_construct_transport(&fake_factory->super, fd); cr_expect_eq(fake_transport->constructed, TRUE); cr_expect_eq(fake_transport->super.read, _fake_read); cr_expect_eq(fake_transport->super.write, _fake_write); log_transport_free(&fake_transport->super); - transport_factory_destroy(&fake_factory->super); + transport_factory_free(&fake_factory->super); cr_expect_eq(fake_factory->destroyed, TRUE); } diff --git a/lib/transport/transport-factory.h b/lib/transport/transport-factory.h index f07532ad1aa..e0b135631d6 100644 --- a/lib/transport/transport-factory.h +++ b/lib/transport/transport-factory.h @@ -40,20 +40,21 @@ typedef struct _TransportFactory TransportFactory; struct _TransportFactory { const TransportFactoryId *id; - LogTransport *(*construct)(TransportFactory *self, gint fd); - void (*destroy)(TransportFactory *self); + LogTransport *(*construct_transport)(TransportFactory *self, gint fd); + void (*free_fn)(TransportFactory *self); }; -static inline LogTransport *transport_factory_construct(TransportFactory *self, gint fd) +static inline LogTransport *transport_factory_construct_transport(TransportFactory *self, gint fd) { - g_assert(self->construct); - return self->construct(self, fd); + g_assert(self->construct_transport); + return self->construct_transport(self, fd); } -static inline void transport_factory_destroy(TransportFactory *self) +static inline void transport_factory_free(TransportFactory *self) { - if (self->destroy) - self->destroy(self); + if (self->free_fn) + self->free_fn(self); + g_free(self); } static inline const TransportFactoryId *transport_factory_id(TransportFactory *self) From d9a185865b1327f1be9c860531239044f2192001 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Tue, 15 May 2018 15:48:49 +0200 Subject: [PATCH 03/16] transport: add TransportFactoryRegistry Signed-off-by: Laszlo Budai --- lib/transport/CMakeLists.txt | 2 + lib/transport/Makefile.am | 6 +- lib/transport/tests/CMakeLists.txt | 1 + lib/transport/tests/Makefile.am | 9 ++- .../tests/test_transport_factory_registry.c | 58 ++++++++++++++++ lib/transport/transport-factory-registry.c | 69 +++++++++++++++++++ lib/transport/transport-factory-registry.h | 41 +++++++++++ 7 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 lib/transport/tests/test_transport_factory_registry.c create mode 100644 lib/transport/transport-factory-registry.c create mode 100644 lib/transport/transport-factory-registry.h diff --git a/lib/transport/CMakeLists.txt b/lib/transport/CMakeLists.txt index c6042838667..e9ccbf52763 100644 --- a/lib/transport/CMakeLists.txt +++ b/lib/transport/CMakeLists.txt @@ -7,6 +7,7 @@ set(TRANSPORT_HEADERS transport/transport-socket.h transport/transport-factory-id.h transport/transport-factory.h + transport/transport-factory-registry.h PARENT_SCOPE) set(TRANSPORT_SOURCES @@ -17,6 +18,7 @@ set(TRANSPORT_SOURCES transport/transport-socket.c transport/transport-tls.c transport/transport-factory-id.c + transport/transport-factory-registry.c PARENT_SCOPE) add_test_subdirectory(tests) diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am index 08220660beb..8d837dbf8a8 100644 --- a/lib/transport/Makefile.am +++ b/lib/transport/Makefile.am @@ -8,7 +8,8 @@ transportinclude_HEADERS = \ lib/transport/transport-pipe.h \ lib/transport/transport-socket.h \ lib/transport/transport-factory-id.h \ - lib/transport/transport-factory.h + lib/transport/transport-factory.h \ + lib/transport/transport-factory-registry.h transport_sources = \ lib/transport/logtransport.c \ @@ -16,7 +17,8 @@ transport_sources = \ lib/transport/transport-file.c \ lib/transport/transport-pipe.c \ lib/transport/transport-socket.c \ - lib/transport/transport-factory-id.c + lib/transport/transport-factory-id.c \ + lib/transport/transport-factory-registry.c transport_crypto_sources = \ lib/transport/transport-tls.c diff --git a/lib/transport/tests/CMakeLists.txt b/lib/transport/tests/CMakeLists.txt index 02348a8b494..dd8aa26a33c 100644 --- a/lib/transport/tests/CMakeLists.txt +++ b/lib/transport/tests/CMakeLists.txt @@ -1,3 +1,4 @@ add_unit_test(LIBTEST TARGET test_aux_data) add_unit_test(LIBTEST TARGET test_transport_factory_id) add_unit_test(LIBTEST TARGET test_transport_factory) +add_unit_test(LIBTEST TARGET test_transport_factory_registry) diff --git a/lib/transport/tests/Makefile.am b/lib/transport/tests/Makefile.am index 8c334055328..9b209a0aefb 100644 --- a/lib/transport/tests/Makefile.am +++ b/lib/transport/tests/Makefile.am @@ -1,7 +1,8 @@ lib_transport_tests_TESTS = \ lib/transport/tests/test_aux_data \ lib/transport/tests/test_transport_factory_id \ - lib/transport/tests/test_transport_factory + lib/transport/tests/test_transport_factory \ + lib/transport/tests/test_transport_factory_registry check_PROGRAMS += ${lib_transport_tests_TESTS} @@ -22,3 +23,9 @@ lib_transport_tests_test_transport_factory_CFLAGS = $(TEST_CFLAGS) \ lib_transport_tests_test_transport_factory_LDADD = $(TEST_LDADD) lib_transport_tests_test_transport_factory_SOURCES = \ lib/transport/tests/test_transport_factory.c + +lib_transport_tests_test_transport_factory_registry_CFLAGS = $(TEST_CFLAGS) \ + -I${top_srcdir}/lib/transport/tests +lib_transport_tests_test_transport_factory_registry_LDADD = $(TEST_LDADD) +lib_transport_tests_test_transport_factory_registry_SOURCES = \ + lib/transport/tests/test_transport_factory_registry.c diff --git a/lib/transport/tests/test_transport_factory_registry.c b/lib/transport/tests/test_transport_factory_registry.c new file mode 100644 index 00000000000..16193ac900d --- /dev/null +++ b/lib/transport/tests/test_transport_factory_registry.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2002-2018 Balabit + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/transport-factory-registry.h" +#include "apphook.h" +#include + +typedef struct _TestTransportFactory +{ + TransportFactory super; +}TestTransportFactory; + +DEFINE_TRANSPORT_FACTORY_ID_FUN(test_transport_factory_id); + +static TestTransportFactory * +_test_transport_factory_new(void) +{ + TestTransportFactory *instance = g_new(TestTransportFactory, 1); + + instance->super.id = test_transport_factory_id(); + + return instance; +} + +TestSuite(transport_factory_registry, .init = app_startup, .fini = app_shutdown); + +Test(transport_factory_registry, basic_functionality) +{ + TransportFactoryRegistry *registry = transport_factory_registry_new(); + + TestTransportFactory *factory = _test_transport_factory_new(); + + transport_factory_registry_add(registry, &factory->super); + const TransportFactory *looked_up_factory = (TransportFactory*)transport_factory_registry_lookup(registry, test_transport_factory_id()); + cr_expect_eq(factory, looked_up_factory); + + transport_factory_registry_free(registry); +} diff --git a/lib/transport/transport-factory-registry.c b/lib/transport/transport-factory-registry.c new file mode 100644 index 00000000000..19dac973813 --- /dev/null +++ b/lib/transport/transport-factory-registry.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/transport-factory-registry.h" + +struct _TransportFactoryRegistry +{ + /* + * hash_table + * */ + GHashTable *registry; +}; + +static void +_transport_factory_destroy_notify(gpointer s) +{ + TransportFactory *self = (TransportFactory *)s; + transport_factory_free(self); +} + +TransportFactoryRegistry* transport_factory_registry_new(void) +{ + TransportFactoryRegistry *instance = g_new0(TransportFactoryRegistry, 1); + + instance->registry = g_hash_table_new_full((GHashFunc)TRANSPORT_FACTORY_ID_HASH_FUNC, + (GEqualFunc)TRANSPORT_FACTORY_ID_CMP_FUNC, + NULL,_transport_factory_destroy_notify); + + return instance; +} + +void transport_factory_registry_free(TransportFactoryRegistry *self) +{ + g_hash_table_unref(self->registry); + g_free(self); +} + +gboolean +transport_factory_registry_add(TransportFactoryRegistry *self, TransportFactory *factory) +{ + return g_hash_table_insert(self->registry, factory->id, factory); +} + +const TransportFactory * +transport_factory_registry_lookup(TransportFactoryRegistry *self, const TransportFactoryId *id) +{ + return g_hash_table_lookup(self->registry, id); +} diff --git a/lib/transport/transport-factory-registry.h b/lib/transport/transport-factory-registry.h new file mode 100644 index 00000000000..de0682779dd --- /dev/null +++ b/lib/transport/transport-factory-registry.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef TRANSPORT_FACTORY_REGISTRY_H_INCLUDED +#define TRANSPORT_FACTORY_REGISTRY_H_INCLUDED + +#include "syslog-ng.h" +#include "transport/transport-factory.h" +#include "transport/transport-factory-id.h" + +typedef struct _TransportFactoryRegistry TransportFactoryRegistry; + +TransportFactoryRegistry* transport_factory_registry_new(void); +void transport_factory_registry_free(TransportFactoryRegistry *self); + +gboolean transport_factory_registry_add(TransportFactoryRegistry *self, TransportFactory *factory); +const TransportFactory *transport_factory_registry_lookup(TransportFactoryRegistry *self, const TransportFactoryId *id); + +#endif + From 3a24d72b118ee21967831a361296370b7436399a Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 17 May 2018 13:23:02 +0200 Subject: [PATCH 04/16] transport: add release_fd method Signed-off-by: Laszlo Budai --- lib/transport/logtransport.c | 10 ++++++++++ lib/transport/logtransport.h | 1 + 2 files changed, 11 insertions(+) diff --git a/lib/transport/logtransport.c b/lib/transport/logtransport.c index 367c568f11c..a69b6297a7e 100644 --- a/lib/transport/logtransport.c +++ b/lib/transport/logtransport.c @@ -52,3 +52,13 @@ log_transport_free(LogTransport *self) self->free_fn(self); g_free(self); } + +gint +log_transport_release_fd(LogTransport *s) +{ + gint fd = s->fd; + s->fd = -1; + + return fd; +} + diff --git a/lib/transport/logtransport.h b/lib/transport/logtransport.h index f60660de85d..ef8b14952ee 100644 --- a/lib/transport/logtransport.h +++ b/lib/transport/logtransport.h @@ -54,5 +54,6 @@ log_transport_read(LogTransport *self, gpointer buf, gsize count, LogTransportAu void log_transport_init_instance(LogTransport *s, gint fd); void log_transport_free_method(LogTransport *s); void log_transport_free(LogTransport *s); +gint log_transport_release_fd(LogTransport *s); #endif From 463cb63d0bc559938da15bc413364d2728d8e5c3 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 17 May 2018 18:20:57 +0200 Subject: [PATCH 05/16] transport: fix coding style and make some arguments const Signed-off-by: Laszlo Budai --- lib/transport/tests/test_transport_factory.c | 2 +- lib/transport/tests/test_transport_factory_registry.c | 5 +++-- lib/transport/transport-factory-id.c | 2 +- lib/transport/transport-factory-id.h | 2 +- lib/transport/transport-factory-registry.c | 5 +++-- lib/transport/transport-factory-registry.h | 2 +- lib/transport/transport-factory.h | 6 +++--- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index 3e5463965ec..7b9ab206091 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -69,7 +69,7 @@ struct _FakeTransportFactory DEFINE_TRANSPORT_FACTORY_ID_FUN(_fake_transport_factory_id); static LogTransport * -_transport_factory_construct(TransportFactory *s, gint fd) +_transport_factory_construct(const TransportFactory *s, gint fd) { FakeTransportFactory *self = (FakeTransportFactory *)s; diff --git a/lib/transport/tests/test_transport_factory_registry.c b/lib/transport/tests/test_transport_factory_registry.c index 16193ac900d..acca019a6eb 100644 --- a/lib/transport/tests/test_transport_factory_registry.c +++ b/lib/transport/tests/test_transport_factory_registry.c @@ -28,7 +28,7 @@ typedef struct _TestTransportFactory { TransportFactory super; -}TestTransportFactory; +} TestTransportFactory; DEFINE_TRANSPORT_FACTORY_ID_FUN(test_transport_factory_id); @@ -51,7 +51,8 @@ Test(transport_factory_registry, basic_functionality) TestTransportFactory *factory = _test_transport_factory_new(); transport_factory_registry_add(registry, &factory->super); - const TransportFactory *looked_up_factory = (TransportFactory*)transport_factory_registry_lookup(registry, test_transport_factory_id()); + const TransportFactory *looked_up_factory = (TransportFactory *)transport_factory_registry_lookup(registry, + test_transport_factory_id()); cr_expect_eq(factory, looked_up_factory); transport_factory_registry_free(registry); diff --git a/lib/transport/transport-factory-id.c b/lib/transport/transport-factory-id.c index 5e14c6be152..88da6f00b89 100644 --- a/lib/transport/transport-factory-id.c +++ b/lib/transport/transport-factory-id.c @@ -69,7 +69,7 @@ transport_factory_id_clone(gconstpointer id) } const gchar * -transport_factory_id_to_string(TransportFactoryId *id) +transport_factory_id_to_string(const TransportFactoryId *id) { GString *str = (GString *)id; return str->str; diff --git a/lib/transport/transport-factory-id.h b/lib/transport/transport-factory-id.h index 0298b207d26..a19f55fbe3e 100644 --- a/lib/transport/transport-factory-id.h +++ b/lib/transport/transport-factory-id.h @@ -35,7 +35,7 @@ void transport_factory_id_register(TransportFactoryId *); GList *transport_factory_id_clone_registered_ids(void); void transport_factory_id_free(gpointer); gpointer transport_factory_id_clone(gconstpointer); -const gchar *transport_factory_id_to_string(TransportFactoryId *); +const gchar *transport_factory_id_to_string(const TransportFactoryId *); #define TRANSPORT_FACTORY_ID_NEW(name) ({GString *str = g_string_new(""); g_string_printf(str, "%s-%s:%d", name, __FILE__, __LINE__); str;}) #define TRANSPORT_FACTORY_ID_FREE_FUNC transport_factory_id_free diff --git a/lib/transport/transport-factory-registry.c b/lib/transport/transport-factory-registry.c index 19dac973813..c00a3e32982 100644 --- a/lib/transport/transport-factory-registry.c +++ b/lib/transport/transport-factory-registry.c @@ -39,7 +39,7 @@ _transport_factory_destroy_notify(gpointer s) transport_factory_free(self); } -TransportFactoryRegistry* transport_factory_registry_new(void) +TransportFactoryRegistry *transport_factory_registry_new(void) { TransportFactoryRegistry *instance = g_new0(TransportFactoryRegistry, 1); @@ -59,7 +59,8 @@ void transport_factory_registry_free(TransportFactoryRegistry *self) gboolean transport_factory_registry_add(TransportFactoryRegistry *self, TransportFactory *factory) { - return g_hash_table_insert(self->registry, factory->id, factory); + const TransportFactoryId *id = transport_factory_get_id(factory); + return g_hash_table_insert(self->registry, (TransportFactoryId *)id, factory); } const TransportFactory * diff --git a/lib/transport/transport-factory-registry.h b/lib/transport/transport-factory-registry.h index de0682779dd..30ecda89eda 100644 --- a/lib/transport/transport-factory-registry.h +++ b/lib/transport/transport-factory-registry.h @@ -31,7 +31,7 @@ typedef struct _TransportFactoryRegistry TransportFactoryRegistry; -TransportFactoryRegistry* transport_factory_registry_new(void); +TransportFactoryRegistry *transport_factory_registry_new(void); void transport_factory_registry_free(TransportFactoryRegistry *self); gboolean transport_factory_registry_add(TransportFactoryRegistry *self, TransportFactory *factory); diff --git a/lib/transport/transport-factory.h b/lib/transport/transport-factory.h index e0b135631d6..9552911480e 100644 --- a/lib/transport/transport-factory.h +++ b/lib/transport/transport-factory.h @@ -40,11 +40,11 @@ typedef struct _TransportFactory TransportFactory; struct _TransportFactory { const TransportFactoryId *id; - LogTransport *(*construct_transport)(TransportFactory *self, gint fd); + LogTransport *(*construct_transport)(const TransportFactory *self, gint fd); void (*free_fn)(TransportFactory *self); }; -static inline LogTransport *transport_factory_construct_transport(TransportFactory *self, gint fd) +static inline LogTransport *transport_factory_construct_transport(const TransportFactory *self, gint fd) { g_assert(self->construct_transport); return self->construct_transport(self, fd); @@ -57,7 +57,7 @@ static inline void transport_factory_free(TransportFactory *self) g_free(self); } -static inline const TransportFactoryId *transport_factory_id(TransportFactory *self) +static inline const TransportFactoryId *transport_factory_get_id(const TransportFactory *self) { /* each concrete TransportFactory has to have an id */ From 681ebf0dfb02db2136ba724f94bd99edb76d5031 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 17 May 2018 18:22:14 +0200 Subject: [PATCH 06/16] transport: add multitransport MultiTransport is responsible for switching between the supported LogTransport instances. MultiTransport is-a LogTransport which means that it can be used in any places where LogTransport can be used. Signed-off-by: Laszlo Budai --- lib/transport/CMakeLists.txt | 2 + lib/transport/Makefile.am | 6 +- lib/transport/multitransport.c | 139 ++++++++++++++++++++++ lib/transport/multitransport.h | 47 ++++++++ lib/transport/tests/CMakeLists.txt | 1 + lib/transport/tests/Makefile.am | 9 +- lib/transport/tests/test_multitransport.c | 105 ++++++++++++++++ 7 files changed, 306 insertions(+), 3 deletions(-) create mode 100644 lib/transport/multitransport.c create mode 100644 lib/transport/multitransport.h create mode 100644 lib/transport/tests/test_multitransport.c diff --git a/lib/transport/CMakeLists.txt b/lib/transport/CMakeLists.txt index e9ccbf52763..5bc579410f7 100644 --- a/lib/transport/CMakeLists.txt +++ b/lib/transport/CMakeLists.txt @@ -8,6 +8,7 @@ set(TRANSPORT_HEADERS transport/transport-factory-id.h transport/transport-factory.h transport/transport-factory-registry.h + transport/multitransport.h PARENT_SCOPE) set(TRANSPORT_SOURCES @@ -19,6 +20,7 @@ set(TRANSPORT_SOURCES transport/transport-tls.c transport/transport-factory-id.c transport/transport-factory-registry.c + transport/multitransport.c PARENT_SCOPE) add_test_subdirectory(tests) diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am index 8d837dbf8a8..b6aa0136f14 100644 --- a/lib/transport/Makefile.am +++ b/lib/transport/Makefile.am @@ -9,7 +9,8 @@ transportinclude_HEADERS = \ lib/transport/transport-socket.h \ lib/transport/transport-factory-id.h \ lib/transport/transport-factory.h \ - lib/transport/transport-factory-registry.h + lib/transport/transport-factory-registry.h \ + lib/transport/multitransport.h transport_sources = \ lib/transport/logtransport.c \ @@ -18,7 +19,8 @@ transport_sources = \ lib/transport/transport-pipe.c \ lib/transport/transport-socket.c \ lib/transport/transport-factory-id.c \ - lib/transport/transport-factory-registry.c + lib/transport/transport-factory-registry.c \ + lib/transport/multitransport.c transport_crypto_sources = \ lib/transport/transport-tls.c diff --git a/lib/transport/multitransport.c b/lib/transport/multitransport.c new file mode 100644 index 00000000000..fd5fb7f5e5e --- /dev/null +++ b/lib/transport/multitransport.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/multitransport.h" +#include "transport/transport-factory-registry.h" +#include "messages.h" + +void multitransport_add_factory(MultiTransport *self, TransportFactory *transport_factory) +{ + transport_factory_registry_add(self->registry, transport_factory); +} + +static void +_do_transport_switch(MultiTransport *self, LogTransport *new_transport, const TransportFactory *new_transport_factory) +{ + self->super.fd = log_transport_release_fd(self->active_transport); + self->super.cond = new_transport->cond; + log_transport_free(self->active_transport); + self->active_transport = new_transport; + self->active_transport_factory = new_transport_factory; +} + +static const TransportFactory * +_lookup_transport_factory(TransportFactoryRegistry *registry, const TransportFactoryId *factory_id) +{ + const TransportFactory *factory = transport_factory_registry_lookup(registry, factory_id); + + if (!factory) + { + msg_error("multitransport_switch failed, requested transport factory not found", + evt_tag_str("TransportFactoryId", transport_factory_id_to_string(factory_id))); + return NULL; + } + + return factory; +} + +static LogTransport * +_construct_transport(const TransportFactory *factory, gint fd) +{ + LogTransport *transport = transport_factory_construct_transport(factory, fd); + const TransportFactoryId *factory_id = transport_factory_get_id(factory); + + if (!transport) + { + msg_error("multitransport_switch failed, failed to construct new transport", + evt_tag_str("TransportFactoryId", transport_factory_id_to_string(factory_id))); + return NULL; + } + + return transport; +} + +gboolean multitransport_switch(MultiTransport *self, const TransportFactoryId *factory_id) +{ + const TransportFactoryId *old = transport_factory_get_id(self->active_transport_factory); + msg_debug("Transport switch requested", + evt_tag_str("Old TransportFactoryId", transport_factory_id_to_string(old)), + evt_tag_str("Requested TransportFactoryId", transport_factory_id_to_string(factory_id))); + + const TransportFactory *transport_factory = _lookup_transport_factory(self->registry, factory_id); + if (!transport_factory) + return FALSE; + + LogTransport *transport = _construct_transport(transport_factory, self->super.fd); + if (!transport) + return FALSE; + + _do_transport_switch(self, transport, transport_factory); + + msg_debug("multitransport_switch succeded", + evt_tag_str("New TransportFactoryId", transport_factory_id_to_string(factory_id))); + + return TRUE; +} + +static gssize +_multitransport_write(LogTransport *s, gpointer buf, gsize count) +{ + MultiTransport *self = (MultiTransport *)s; + gssize r = log_transport_write(self->active_transport, buf, count); + return r; +} + +static gssize +_multitransport_read(LogTransport *s, gpointer buf, gsize count, LogTransportAuxData *aux) +{ + MultiTransport *self = (MultiTransport *)s; + gssize r = log_transport_read(self->active_transport, buf, count, aux); + return r; +} + +static void +_multitransport_free(LogTransport *s) +{ + MultiTransport *self = (MultiTransport *)s; + s->fd = log_transport_release_fd(self->active_transport); + log_transport_free(self->active_transport); + transport_factory_registry_free(self->registry); + log_transport_free_method(s); +} + +LogTransport * +multitransport_new(TransportFactory *default_transport_factory, gint fd) +{ + MultiTransport *self = g_new0(MultiTransport, 1); + self->registry = transport_factory_registry_new(); + transport_factory_registry_add(self->registry, default_transport_factory); + + log_transport_init_instance(&self->super, fd); + self->super.read = _multitransport_read; + self->super.write = _multitransport_write; + self->super.free_fn = _multitransport_free; + self->active_transport = transport_factory_construct_transport(default_transport_factory, fd); + self->active_transport_factory = default_transport_factory; + + return &self->super; +} diff --git a/lib/transport/multitransport.h b/lib/transport/multitransport.h new file mode 100644 index 00000000000..71c3bf14f3e --- /dev/null +++ b/lib/transport/multitransport.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef MULTITRANSPORT_H_INCLUDED +#define MULTITRANSPORT_H_INCLUDED + +#include "transport/logtransport.h" +#include "transport/transport-factory.h" +#include "transport/transport-factory-registry.h" + +typedef struct _MultiTransport MultiTransport; + +struct _MultiTransport +{ + LogTransport super; + TransportFactoryRegistry *registry; + LogTransport *active_transport; + const TransportFactory *active_transport_factory; +}; + +LogTransport *multitransport_new(TransportFactory *default_transport_factory, gint fd); +void multitransport_add_factory(MultiTransport *, TransportFactory *); +gboolean multitransport_switch(MultiTransport *, const TransportFactoryId *); + +#endif + diff --git a/lib/transport/tests/CMakeLists.txt b/lib/transport/tests/CMakeLists.txt index dd8aa26a33c..49ee6178c5c 100644 --- a/lib/transport/tests/CMakeLists.txt +++ b/lib/transport/tests/CMakeLists.txt @@ -2,3 +2,4 @@ add_unit_test(LIBTEST TARGET test_aux_data) add_unit_test(LIBTEST TARGET test_transport_factory_id) add_unit_test(LIBTEST TARGET test_transport_factory) add_unit_test(LIBTEST TARGET test_transport_factory_registry) +add_unit_test(LIBTEST TARGET test_multitransport) diff --git a/lib/transport/tests/Makefile.am b/lib/transport/tests/Makefile.am index 9b209a0aefb..7dcbdbd4577 100644 --- a/lib/transport/tests/Makefile.am +++ b/lib/transport/tests/Makefile.am @@ -2,7 +2,8 @@ lib_transport_tests_TESTS = \ lib/transport/tests/test_aux_data \ lib/transport/tests/test_transport_factory_id \ lib/transport/tests/test_transport_factory \ - lib/transport/tests/test_transport_factory_registry + lib/transport/tests/test_transport_factory_registry \ + lib/transport/tests/test_multitransport check_PROGRAMS += ${lib_transport_tests_TESTS} @@ -29,3 +30,9 @@ lib_transport_tests_test_transport_factory_registry_CFLAGS = $(TEST_CFLAGS) \ lib_transport_tests_test_transport_factory_registry_LDADD = $(TEST_LDADD) lib_transport_tests_test_transport_factory_registry_SOURCES = \ lib/transport/tests/test_transport_factory_registry.c + +lib_transport_tests_test_multitransport_CFLAGS = $(TEST_CFLAGS) \ + -I${top_srcdir}/lib/transport/tests +lib_transport_tests_test_multitransport_LDADD = $(TEST_LDADD) +lib_transport_tests_test_multitransport_SOURCES = \ + lib/transport/tests/test_multitransport.c diff --git a/lib/transport/tests/test_multitransport.c b/lib/transport/tests/test_multitransport.c new file mode 100644 index 00000000000..f303278f6b4 --- /dev/null +++ b/lib/transport/tests/test_multitransport.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2002-2018 Balabit + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/multitransport.h" +#include "transport/transport-factory.h" +#include "apphook.h" +#include + +#define DEFINE_TEST_TRANSPORT_WITH_FACTORY(TypePrefix, FunPrefix) \ + typedef struct TypePrefix ## Transport_ TypePrefix ## Transport; \ + typedef struct TypePrefix ## TransportFactory_ TypePrefix ## TransportFactory; \ + struct TypePrefix ## Transport_ \ + { \ + LogTransport super; \ + }; \ + static gssize \ + FunPrefix ## _read(LogTransport *s, gpointer buf, gsize count, LogTransportAuxData *aux) \ + { \ + return count; \ + }\ + static gssize \ + FunPrefix ## _write(LogTransport *s, const gpointer buf, gsize count) \ + { \ + return count; \ + }\ + LogTransport * \ + FunPrefix ## _transport_new(void) \ + { \ + TypePrefix ## Transport *self = g_new0(TypePrefix ## Transport, 1); \ + self->super.read = FunPrefix ## _read; \ + self->super.write = FunPrefix ## _write; \ + return &self->super; \ + } \ + struct TypePrefix ## TransportFactory_\ + { \ + TransportFactory super; \ + }; \ + DEFINE_TRANSPORT_FACTORY_ID_FUN(FunPrefix ## _transport_factory_id); \ + static LogTransport * \ + FunPrefix ## _factory_construct(const TransportFactory *s, gint fd) \ + {\ + LogTransport *transport = FunPrefix ## _transport_new(); \ + log_transport_init_instance(transport, fd); \ + return transport; \ + } \ + TransportFactory * \ + FunPrefix ## _transport_factory_new(void) \ + { \ + TypePrefix ## TransportFactory *self = g_new0(TypePrefix ## TransportFactory, 1); \ + self->super.id = FunPrefix ## _transport_factory_id(); \ + self->super.construct_transport = FunPrefix ## _factory_construct; \ + return &self->super; \ + } \ + +TestSuite(multitransport, .init = app_startup, .fini = app_shutdown); + +DEFINE_TEST_TRANSPORT_WITH_FACTORY(Fake, fake); +DEFINE_TEST_TRANSPORT_WITH_FACTORY(Default, default); +DEFINE_TEST_TRANSPORT_WITH_FACTORY(Unsupported, unsupported); + +Test(multitransport, test_switch_transport) +{ + TransportFactory *default_factory = default_transport_factory_new(); + TransportFactory *fake_factory = fake_transport_factory_new(); + gint fd = -2; + + MultiTransport *multi_transport = (MultiTransport *) multitransport_new(default_factory, fd); + + cr_expect_eq(multi_transport->active_transport->read, default_read); + cr_expect_eq(multi_transport->active_transport->write, default_write); + + multitransport_add_factory(multi_transport, fake_factory); + cr_expect_not(multitransport_switch(multi_transport, unsupported_transport_factory_id())); + cr_expect_eq(multi_transport->active_transport->read, default_read); + cr_expect_eq(multi_transport->active_transport->write, default_write); + cr_expect_eq(multi_transport->active_transport_factory, default_factory); + + cr_expect(multitransport_switch(multi_transport, fake_transport_factory_id())); + cr_expect_eq(multi_transport->active_transport->read, fake_read); + cr_expect_eq(multi_transport->active_transport->write, fake_write); + cr_expect_eq(multi_transport->active_transport_factory, fake_factory); + + log_transport_free(&multi_transport->super); +} + From 4b717daaecf2cd10c45915450f4838478119b9a4 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 17 May 2018 21:00:39 +0200 Subject: [PATCH 07/16] transport: add TLS transport factory Signed-off-by: Laszlo Budai --- lib/transport/CMakeLists.txt | 2 + lib/transport/Makefile.am | 6 ++- lib/transport/transport-factory-tls.c | 66 +++++++++++++++++++++++++++ lib/transport/transport-factory-tls.h | 37 +++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 lib/transport/transport-factory-tls.c create mode 100644 lib/transport/transport-factory-tls.h diff --git a/lib/transport/CMakeLists.txt b/lib/transport/CMakeLists.txt index 5bc579410f7..a62a141d5fa 100644 --- a/lib/transport/CMakeLists.txt +++ b/lib/transport/CMakeLists.txt @@ -9,6 +9,7 @@ set(TRANSPORT_HEADERS transport/transport-factory.h transport/transport-factory-registry.h transport/multitransport.h + transport/transport-factory-tls.h PARENT_SCOPE) set(TRANSPORT_SOURCES @@ -21,6 +22,7 @@ set(TRANSPORT_SOURCES transport/transport-factory-id.c transport/transport-factory-registry.c transport/multitransport.c + transport/transport-factory-tls.c PARENT_SCOPE) add_test_subdirectory(tests) diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am index b6aa0136f14..3a5fde4ffe5 100644 --- a/lib/transport/Makefile.am +++ b/lib/transport/Makefile.am @@ -10,7 +10,8 @@ transportinclude_HEADERS = \ lib/transport/transport-factory-id.h \ lib/transport/transport-factory.h \ lib/transport/transport-factory-registry.h \ - lib/transport/multitransport.h + lib/transport/multitransport.h \ + lib/transport/transport-factory-tls.h transport_sources = \ lib/transport/logtransport.c \ @@ -20,7 +21,8 @@ transport_sources = \ lib/transport/transport-socket.c \ lib/transport/transport-factory-id.c \ lib/transport/transport-factory-registry.c \ - lib/transport/multitransport.c + lib/transport/multitransport.c \ + lib/transport/transport-factory-tls.c transport_crypto_sources = \ lib/transport/transport-tls.c diff --git a/lib/transport/transport-factory-tls.c b/lib/transport/transport-factory-tls.c new file mode 100644 index 00000000000..8b1cfb7a429 --- /dev/null +++ b/lib/transport/transport-factory-tls.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) 2018 Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/transport-factory-tls.h" +#include "transport/transport-tls.h" + +DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_factory_tls_id); + +typedef struct _TransportFactoryTLS TransportFactoryTLS; + +struct _TransportFactoryTLS +{ + TransportFactory super; + TLSContext *tls_context; + TLSSessionVerifyFunc tls_verify_cb; + gpointer tls_verify_data; +}; + +static LogTransport * +_construct_transport(const TransportFactory *s, gint fd) +{ + TransportFactoryTLS *self = (TransportFactoryTLS *)s; + TLSSession *tls_session = tls_context_setup_session(self->tls_context); + if (!tls_session) + return NULL; + tls_session_set_verify(tls_session, self->tls_verify_cb, self->tls_verify_data, NULL); + return log_transport_tls_new(tls_session, fd); +} + +TransportFactory * +transport_factory_tls_new(TLSContext *ctx, + TLSSessionVerifyFunc tls_verify_cb, + gpointer tls_verify_data) +{ + TransportFactoryTLS *instance = g_new0(TransportFactoryTLS, 1); + + instance->tls_context = ctx; + instance->tls_verify_cb = tls_verify_cb; + instance->tls_verify_data = tls_verify_data; + + instance->super.id = transport_factory_tls_id(); + instance->super.construct_transport = _construct_transport; + + return &instance->super; +} diff --git a/lib/transport/transport-factory-tls.h b/lib/transport/transport-factory-tls.h new file mode 100644 index 00000000000..a1b88f09187 --- /dev/null +++ b/lib/transport/transport-factory-tls.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) 2018 Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef TRANSPORT_FACTORY_TLS_H_INCLUDED +#define TRANSPORT_FACTORY_TLS_H_INCLUDED + +#include "transport/transport-factory.h" +#include "tlscontext.h" + +TransportFactory *transport_factory_tls_new(TLSContext *ctx, + TLSSessionVerifyFunc tls_verify_cb, + gpointer tls_verify_data); + +const TransportFactoryId *transport_factory_tls_id(void); + +#endif From b279e50a19447da64ba86b11ab544e2205df965b Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Fri, 18 May 2018 18:35:46 +0200 Subject: [PATCH 08/16] transport: add transport-factory-socket Signed-off-by: Laszlo Budai --- lib/transport/CMakeLists.txt | 2 + lib/transport/Makefile.am | 6 ++- lib/transport/transport-factory-socket.c | 63 ++++++++++++++++++++++++ lib/transport/transport-factory-socket.h | 35 +++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 lib/transport/transport-factory-socket.c create mode 100644 lib/transport/transport-factory-socket.h diff --git a/lib/transport/CMakeLists.txt b/lib/transport/CMakeLists.txt index a62a141d5fa..45f727b7ac6 100644 --- a/lib/transport/CMakeLists.txt +++ b/lib/transport/CMakeLists.txt @@ -10,6 +10,7 @@ set(TRANSPORT_HEADERS transport/transport-factory-registry.h transport/multitransport.h transport/transport-factory-tls.h + transport/transport-factory-socket.h PARENT_SCOPE) set(TRANSPORT_SOURCES @@ -23,6 +24,7 @@ set(TRANSPORT_SOURCES transport/transport-factory-registry.c transport/multitransport.c transport/transport-factory-tls.c + transport/transport-factory-socket.c PARENT_SCOPE) add_test_subdirectory(tests) diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am index 3a5fde4ffe5..fac35f3673d 100644 --- a/lib/transport/Makefile.am +++ b/lib/transport/Makefile.am @@ -11,7 +11,8 @@ transportinclude_HEADERS = \ lib/transport/transport-factory.h \ lib/transport/transport-factory-registry.h \ lib/transport/multitransport.h \ - lib/transport/transport-factory-tls.h + lib/transport/transport-factory-tls.h \ + lib/transport/transport-factory-socket.h transport_sources = \ lib/transport/logtransport.c \ @@ -22,7 +23,8 @@ transport_sources = \ lib/transport/transport-factory-id.c \ lib/transport/transport-factory-registry.c \ lib/transport/multitransport.c \ - lib/transport/transport-factory-tls.c + lib/transport/transport-factory-tls.c \ + lib/transport/transport-factory-socket.c transport_crypto_sources = \ lib/transport/transport-tls.c diff --git a/lib/transport/transport-factory-socket.c b/lib/transport/transport-factory-socket.c new file mode 100644 index 00000000000..9ac22501a45 --- /dev/null +++ b/lib/transport/transport-factory-socket.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) 2018 Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "transport/transport-factory-socket.h" +#include "transport/transport-socket.h" + +typedef struct _TransportFactorySocket TransportFactorySocket; + +DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_factory_socket_id); + +struct _TransportFactorySocket +{ + TransportFactory super; +}; + +static LogTransport * +_construct_transport_dgram(const TransportFactory *s, gint fd) +{ + return log_transport_dgram_socket_new(fd); +} + +static LogTransport * +_construct_transport_stream(const TransportFactory *s, gint fd) +{ + return log_transport_stream_socket_new(fd); +} + +TransportFactory * +transport_factory_socket_new(int sock_type) +{ + TransportFactorySocket *self = g_new0(TransportFactorySocket, 1); + + if (sock_type == SOCK_DGRAM) + self->super.construct_transport = _construct_transport_dgram; + else + self->super.construct_transport = _construct_transport_stream; + + self->super.id = transport_factory_socket_id(); + + return &self->super; +} + diff --git a/lib/transport/transport-factory-socket.h b/lib/transport/transport-factory-socket.h new file mode 100644 index 00000000000..97f62f7bdd2 --- /dev/null +++ b/lib/transport/transport-factory-socket.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2002-2018 Balabit + * Copyright (c) 2018 Laszlo Budai + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef TRANSPORT_FACTORY_SOCKET_H_INCLUDED +#define TRANSPORT_FACTORY_SOCKET_H_INCLUDED + +#include "transport/transport-factory.h" +#include "transport/transport-socket.h" + +TransportFactory *transport_factory_socket_new(gint sock_type); + +const TransportFactoryId *transport_factory_socket_id(void); + +#endif From cde118da737da9dae03dd3590a6437912b5d977b Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Fri, 18 May 2018 18:36:20 +0200 Subject: [PATCH 09/16] afsocket: use MultiTransport when TLS options are set but not required In this case a logproto can do the runtime protocol switch. Signed-off-by: Laszlo Budai --- modules/afsocket/transport-mapper-inet.c | 50 +++++++++++++++++++----- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/modules/afsocket/transport-mapper-inet.c b/modules/afsocket/transport-mapper-inet.c index 3f5acc0c60a..33c4073c33b 100644 --- a/modules/afsocket/transport-mapper-inet.c +++ b/modules/afsocket/transport-mapper-inet.c @@ -26,6 +26,9 @@ #include "messages.h" #include "stats/stats-registry.h" #include "transport/transport-tls.h" +#include "transport/multitransport.h" +#include "transport/transport-factory-tls.h" +#include "transport/transport-factory-socket.h" #include "secret-storage/secret-storage.h" #include @@ -83,24 +86,51 @@ transport_mapper_inet_apply_transport_method(TransportMapper *s, GlobalConfig *c return transport_mapper_inet_validate_tls_options(self); } +static LogTransport * +_construct_tls_transport(TransportMapperInet *self, gint fd) +{ + LogTransport *transport = NULL; + TLSSession *tls_session = tls_context_setup_session(self->tls_context); + if (!tls_session) + return NULL; + + tls_session_set_verify(tls_session, self->tls_verify_callback, self->tls_verify_data, NULL); + transport = log_transport_tls_new(tls_session, fd); + + return transport; +} + +static LogTransport * +_construct_multitransport_with_plain_and_tls_factories(TransportMapperInet *self, gint fd) +{ + LogTransport *transport = NULL; + + TransportFactory *default_factory = transport_factory_socket_new(self->super.sock_type); + transport = multitransport_new(default_factory, fd); + TransportFactory *tls_factory = transport_factory_tls_new(self->tls_context, + self->tls_verify_callback, + self->tls_verify_data); + multitransport_add_factory((MultiTransport *)transport, tls_factory); + + return transport; +} + static LogTransport * transport_mapper_inet_construct_log_transport(TransportMapper *s, gint fd) { TransportMapperInet *self = (TransportMapperInet *) s; - if (self->tls_context) + if (self->tls_context && self->require_tls) { - TLSSession *tls_session; - - tls_session = tls_context_setup_session(self->tls_context); - if (!tls_session) - return NULL; + return _construct_tls_transport(self, fd); + } - tls_session_set_verify(tls_session, self->tls_verify_callback, self->tls_verify_data, NULL); - return log_transport_tls_new(tls_session, fd); + if (self->tls_context) + { + return _construct_multitransport_with_plain_and_tls_factories(self, fd); } - else - return transport_mapper_construct_log_transport_method(s, fd); + + return transport_mapper_construct_log_transport_method(s, fd); } static gboolean From e34b52fe70f7d5222e9bb05c429db0e6c4c6ce7d Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Wed, 23 May 2018 14:33:38 +0200 Subject: [PATCH 10/16] transport: fix unit tests based on review notes Signed-off-by: Laszlo Budai --- lib/transport/tests/test_multitransport.c | 4 ++-- lib/transport/tests/test_transport_factory.c | 2 -- lib/transport/tests/test_transport_factory_id.c | 1 - 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/transport/tests/test_multitransport.c b/lib/transport/tests/test_multitransport.c index f303278f6b4..3378aad5100 100644 --- a/lib/transport/tests/test_multitransport.c +++ b/lib/transport/tests/test_multitransport.c @@ -76,7 +76,7 @@ TestSuite(multitransport, .init = app_startup, .fini = app_shutdown); DEFINE_TEST_TRANSPORT_WITH_FACTORY(Fake, fake); DEFINE_TEST_TRANSPORT_WITH_FACTORY(Default, default); -DEFINE_TEST_TRANSPORT_WITH_FACTORY(Unsupported, unsupported); +DEFINE_TEST_TRANSPORT_WITH_FACTORY(Unregistered, unregistered); Test(multitransport, test_switch_transport) { @@ -90,7 +90,7 @@ Test(multitransport, test_switch_transport) cr_expect_eq(multi_transport->active_transport->write, default_write); multitransport_add_factory(multi_transport, fake_factory); - cr_expect_not(multitransport_switch(multi_transport, unsupported_transport_factory_id())); + cr_expect_not(multitransport_switch(multi_transport, unregistered_transport_factory_id())); cr_expect_eq(multi_transport->active_transport->read, default_read); cr_expect_eq(multi_transport->active_transport->write, default_write); cr_expect_eq(multi_transport->active_transport_factory, default_factory); diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index 7b9ab206091..0210adf3f06 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -71,8 +71,6 @@ DEFINE_TRANSPORT_FACTORY_ID_FUN(_fake_transport_factory_id); static LogTransport * _transport_factory_construct(const TransportFactory *s, gint fd) { - FakeTransportFactory *self = (FakeTransportFactory *)s; - LogTransport *fake_transport = _fake_transport_new(); log_transport_init_instance(fake_transport, fd); diff --git a/lib/transport/tests/test_transport_factory_id.c b/lib/transport/tests/test_transport_factory_id.c index f2759882519..3b5fd115aa7 100644 --- a/lib/transport/tests/test_transport_factory_id.c +++ b/lib/transport/tests/test_transport_factory_id.c @@ -29,7 +29,6 @@ TestSuite(transport_factory_id, .init = app_startup, .fini = app_shutdown); Test(transport_factory_id, lifecycle) { - transport_factory_id_global_init(); GList *ids = transport_factory_id_clone_registered_ids(); cr_expect_null(ids); From 2aaab00aef7fa9d3975f907788a460e9eb6471b3 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Wed, 23 May 2018 16:18:39 +0200 Subject: [PATCH 11/16] transport-factory-id: use functions instead of macros Signed-off-by: Laszlo Budai --- .../tests/test_transport_factory_id.c | 16 ++++---- lib/transport/transport-factory-id.c | 38 ++++++++++++++----- lib/transport/transport-factory-id.h | 8 ++-- lib/transport/transport-factory-registry.c | 4 +- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/lib/transport/tests/test_transport_factory_id.c b/lib/transport/tests/test_transport_factory_id.c index 3b5fd115aa7..8f581bc71f6 100644 --- a/lib/transport/tests/test_transport_factory_id.c +++ b/lib/transport/tests/test_transport_factory_id.c @@ -34,14 +34,14 @@ Test(transport_factory_id, lifecycle) cr_expect_null(ids); TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); - TransportFactoryId *expected = TRANSPORT_FACTORY_ID_CLONE(id); + TransportFactoryId *expected = transport_factory_id_clone(id); transport_factory_id_register(id); ids = transport_factory_id_clone_registered_ids(); cr_assert_not_null(ids, "transport_factory_id_register failed"); - cr_expect(TRANSPORT_FACTORY_ID_CMP_FUNC(ids->data, expected)); - TRANSPORT_FACTORY_ID_FREE_FUNC(expected); - g_list_free_full(ids, TRANSPORT_FACTORY_ID_FREE_FUNC); + cr_expect(transport_factory_id_equal(ids->data, expected)); + transport_factory_id_free(expected); + g_list_free_full(ids, transport_factory_id_free); transport_factory_id_global_deinit(); @@ -53,13 +53,13 @@ Test(transport_factory_id_basic_macros, generated_ids_are_uniq) { TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); TransportFactoryId *id2 = TRANSPORT_FACTORY_ID_NEW("tcp"); - cr_expect_not(TRANSPORT_FACTORY_ID_CMP_FUNC(id, id2)); + cr_expect_not(transport_factory_id_equal(id, id2)); } Test(transport_factory_id_basic_macros, clones_are_equals) { TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); - TransportFactoryId *cloned = TRANSPORT_FACTORY_ID_CLONE(id); - cr_expect(TRANSPORT_FACTORY_ID_CMP_FUNC(id, cloned)); - cr_expect_eq(TRANSPORT_FACTORY_ID_HASH_FUNC(id), TRANSPORT_FACTORY_ID_HASH_FUNC(cloned)); + TransportFactoryId *cloned = transport_factory_id_clone(id); + cr_expect(transport_factory_id_equal(id, cloned)); + cr_expect_eq(transport_factory_id_hash(id), transport_factory_id_hash(cloned)); } diff --git a/lib/transport/transport-factory-id.c b/lib/transport/transport-factory-id.c index 88da6f00b89..abf9bce903d 100644 --- a/lib/transport/transport-factory-id.c +++ b/lib/transport/transport-factory-id.c @@ -33,7 +33,7 @@ void transport_factory_id_global_init(void) void transport_factory_id_global_deinit(void) { - g_list_free_full(transport_factory_ids, TRANSPORT_FACTORY_ID_FREE_FUNC); + g_list_free_full(transport_factory_ids, transport_factory_id_free); transport_factory_ids = NULL; } @@ -45,15 +45,26 @@ transport_factory_id_register(TransportFactoryId *id) } static gpointer -_clone_func(gconstpointer src, gpointer data) +_clone(gconstpointer id) { - return TRANSPORT_FACTORY_ID_CLONE(src); + return g_string_new(((const GString *)id)->str); +} + +TransportFactoryId *transport_factory_id_clone(const TransportFactoryId *id) +{ + return (TransportFactoryId *)_clone((gconstpointer) id); +} + +static gpointer +_copy_func(gconstpointer src, gpointer data) +{ + return _clone(src); } GList * transport_factory_id_clone_registered_ids(void) { - return g_list_copy_deep(transport_factory_ids, _clone_func, NULL); + return g_list_copy_deep(transport_factory_ids, _copy_func, NULL); } void @@ -62,15 +73,22 @@ transport_factory_id_free(gpointer id) g_string_free((GString *)id, TRUE); } -gpointer -transport_factory_id_clone(gconstpointer id) -{ - return g_string_new(((const GString *)id)->str); -} - const gchar * transport_factory_id_to_string(const TransportFactoryId *id) { GString *str = (GString *)id; return str->str; } + +guint +transport_factory_id_hash(gconstpointer key) +{ + return g_string_hash((const GString *)key); +} + +gboolean +transport_factory_id_equal(const TransportFactoryId *id1, const TransportFactoryId *id2) +{ + return g_string_equal(id1, id2); +} + diff --git a/lib/transport/transport-factory-id.h b/lib/transport/transport-factory-id.h index a19f55fbe3e..5c9cfaf981f 100644 --- a/lib/transport/transport-factory-id.h +++ b/lib/transport/transport-factory-id.h @@ -34,14 +34,12 @@ void transport_factory_id_global_deinit(void); void transport_factory_id_register(TransportFactoryId *); GList *transport_factory_id_clone_registered_ids(void); void transport_factory_id_free(gpointer); -gpointer transport_factory_id_clone(gconstpointer); const gchar *transport_factory_id_to_string(const TransportFactoryId *); +guint transport_factory_id_hash(gconstpointer); +gboolean transport_factory_id_equal(const TransportFactoryId *, const TransportFactoryId *); +TransportFactoryId *transport_factory_id_clone(const TransportFactoryId *); #define TRANSPORT_FACTORY_ID_NEW(name) ({GString *str = g_string_new(""); g_string_printf(str, "%s-%s:%d", name, __FILE__, __LINE__); str;}) -#define TRANSPORT_FACTORY_ID_FREE_FUNC transport_factory_id_free -#define TRANSPORT_FACTORY_ID_HASH_FUNC g_string_hash -#define TRANSPORT_FACTORY_ID_CMP_FUNC g_string_equal -#define TRANSPORT_FACTORY_ID_CLONE transport_factory_id_clone #define DEFINE_TRANSPORT_FACTORY_ID_FUN(name) \ const TransportFactoryId* name(void) \ diff --git a/lib/transport/transport-factory-registry.c b/lib/transport/transport-factory-registry.c index c00a3e32982..97f7114be08 100644 --- a/lib/transport/transport-factory-registry.c +++ b/lib/transport/transport-factory-registry.c @@ -43,8 +43,8 @@ TransportFactoryRegistry *transport_factory_registry_new(void) { TransportFactoryRegistry *instance = g_new0(TransportFactoryRegistry, 1); - instance->registry = g_hash_table_new_full((GHashFunc)TRANSPORT_FACTORY_ID_HASH_FUNC, - (GEqualFunc)TRANSPORT_FACTORY_ID_CMP_FUNC, + instance->registry = g_hash_table_new_full((GHashFunc)transport_factory_id_hash, + (GEqualFunc)transport_factory_id_equal, NULL,_transport_factory_destroy_notify); return instance; From cc4f826d6b33387c5a6e590582ead1d59d3701db Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Wed, 23 May 2018 16:38:39 +0200 Subject: [PATCH 12/16] transport: fix copyright issues Signed-off-by: Laszlo Budai --- lib/transport/multitransport.c | 2 +- lib/transport/multitransport.h | 2 +- lib/transport/transport-factory-registry.c | 2 +- lib/transport/transport-factory-registry.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/transport/multitransport.c b/lib/transport/multitransport.c index fd5fb7f5e5e..7e26a2bb095 100644 --- a/lib/transport/multitransport.c +++ b/lib/transport/multitransport.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2002-2018 Balabit - * Copyright (c) Laszlo Budai + * Copyright (c) 2018 Laszlo Budai * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/lib/transport/multitransport.h b/lib/transport/multitransport.h index 71c3bf14f3e..56da8c9e38f 100644 --- a/lib/transport/multitransport.h +++ b/lib/transport/multitransport.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2002-2018 Balabit - * Copyright (c) Laszlo Budai + * Copyright (c) 2018 Laszlo Budai * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/lib/transport/transport-factory-registry.c b/lib/transport/transport-factory-registry.c index 97f7114be08..21a7f34d213 100644 --- a/lib/transport/transport-factory-registry.c +++ b/lib/transport/transport-factory-registry.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2002-2018 Balabit - * Copyright (c) Laszlo Budai + * Copyright (c) 2018 Laszlo Budai * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/lib/transport/transport-factory-registry.h b/lib/transport/transport-factory-registry.h index 30ecda89eda..0fe6e61a6b0 100644 --- a/lib/transport/transport-factory-registry.h +++ b/lib/transport/transport-factory-registry.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2002-2018 Balabit - * Copyright (c) Laszlo Budai + * Copyright (c) 2018 Laszlo Budai * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public From 4e7c197776921e26dc97813e3a52b9ae4a7c0378 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Wed, 23 May 2018 17:38:21 +0200 Subject: [PATCH 13/16] transport: refactor the way how transport change is logged Signed-off-by: Laszlo Budai --- lib/transport/logtransport.h | 1 + lib/transport/multitransport.c | 17 +++--- lib/transport/tests/test_multitransport.c | 5 +- lib/transport/tests/test_transport_factory.c | 2 +- .../tests/test_transport_factory_id.c | 2 +- .../tests/test_transport_factory_registry.c | 2 +- lib/transport/transport-factory-id.c | 54 ++++++++++++++----- lib/transport/transport-factory-id.h | 18 ++++--- lib/transport/transport-factory-socket.c | 2 +- lib/transport/transport-factory-tls.c | 2 +- lib/transport/transport-factory.h | 15 ++++-- 11 files changed, 80 insertions(+), 40 deletions(-) diff --git a/lib/transport/logtransport.h b/lib/transport/logtransport.h index ef8b14952ee..3ffd04f9f61 100644 --- a/lib/transport/logtransport.h +++ b/lib/transport/logtransport.h @@ -34,6 +34,7 @@ struct _LogTransport { gint fd; GIOCondition cond; + const gchar *name; gssize (*read)(LogTransport *self, gpointer buf, gsize count, LogTransportAuxData *aux); gssize (*write)(LogTransport *self, const gpointer buf, gsize count); void (*free_fn)(LogTransport *self); diff --git a/lib/transport/multitransport.c b/lib/transport/multitransport.c index 7e26a2bb095..286b903f224 100644 --- a/lib/transport/multitransport.c +++ b/lib/transport/multitransport.c @@ -48,8 +48,8 @@ _lookup_transport_factory(TransportFactoryRegistry *registry, const TransportFac if (!factory) { - msg_error("multitransport_switch failed, requested transport factory not found", - evt_tag_str("TransportFactoryId", transport_factory_id_to_string(factory_id))); + msg_error("Requested transport not found", + evt_tag_str("transport", transport_factory_id_get_transport_name(factory_id))); return NULL; } @@ -64,8 +64,8 @@ _construct_transport(const TransportFactory *factory, gint fd) if (!transport) { - msg_error("multitransport_switch failed, failed to construct new transport", - evt_tag_str("TransportFactoryId", transport_factory_id_to_string(factory_id))); + msg_error("Failed to construct transport", + evt_tag_str("transport", transport_factory_id_get_transport_name(factory_id))); return NULL; } @@ -74,10 +74,9 @@ _construct_transport(const TransportFactory *factory, gint fd) gboolean multitransport_switch(MultiTransport *self, const TransportFactoryId *factory_id) { - const TransportFactoryId *old = transport_factory_get_id(self->active_transport_factory); msg_debug("Transport switch requested", - evt_tag_str("Old TransportFactoryId", transport_factory_id_to_string(old)), - evt_tag_str("Requested TransportFactoryId", transport_factory_id_to_string(factory_id))); + evt_tag_str("active-transport", self->active_transport->name), + evt_tag_str("requested-transport", transport_factory_id_get_transport_name(factory_id))); const TransportFactory *transport_factory = _lookup_transport_factory(self->registry, factory_id); if (!transport_factory) @@ -89,8 +88,8 @@ gboolean multitransport_switch(MultiTransport *self, const TransportFactoryId *f _do_transport_switch(self, transport, transport_factory); - msg_debug("multitransport_switch succeded", - evt_tag_str("New TransportFactoryId", transport_factory_id_to_string(factory_id))); + msg_debug("Transport switch succeded", + evt_tag_str("new-active-transport", self->active_transport->name)); return TRUE; } diff --git a/lib/transport/tests/test_multitransport.c b/lib/transport/tests/test_multitransport.c index 3378aad5100..9ab1ef81cf7 100644 --- a/lib/transport/tests/test_multitransport.c +++ b/lib/transport/tests/test_multitransport.c @@ -55,7 +55,7 @@ { \ TransportFactory super; \ }; \ - DEFINE_TRANSPORT_FACTORY_ID_FUN(FunPrefix ## _transport_factory_id); \ + DEFINE_TRANSPORT_FACTORY_ID_FUN(FunPrefix, FunPrefix ## _transport_factory_id); \ static LogTransport * \ FunPrefix ## _factory_construct(const TransportFactory *s, gint fd) \ {\ @@ -88,17 +88,20 @@ Test(multitransport, test_switch_transport) cr_expect_eq(multi_transport->active_transport->read, default_read); cr_expect_eq(multi_transport->active_transport->write, default_write); + cr_expect_str_eq(multi_transport->active_transport->name, "default"); multitransport_add_factory(multi_transport, fake_factory); cr_expect_not(multitransport_switch(multi_transport, unregistered_transport_factory_id())); cr_expect_eq(multi_transport->active_transport->read, default_read); cr_expect_eq(multi_transport->active_transport->write, default_write); cr_expect_eq(multi_transport->active_transport_factory, default_factory); + cr_expect_str_eq(multi_transport->active_transport->name, "default"); cr_expect(multitransport_switch(multi_transport, fake_transport_factory_id())); cr_expect_eq(multi_transport->active_transport->read, fake_read); cr_expect_eq(multi_transport->active_transport->write, fake_write); cr_expect_eq(multi_transport->active_transport_factory, fake_factory); + cr_expect_str_eq(multi_transport->active_transport->name, "fake"); log_transport_free(&multi_transport->super); } diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index 0210adf3f06..d4b9a9da1eb 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -66,7 +66,7 @@ struct _FakeTransportFactory gboolean destroyed; }; -DEFINE_TRANSPORT_FACTORY_ID_FUN(_fake_transport_factory_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN(fake, _fake_transport_factory_id); static LogTransport * _transport_factory_construct(const TransportFactory *s, gint fd) diff --git a/lib/transport/tests/test_transport_factory_id.c b/lib/transport/tests/test_transport_factory_id.c index 8f581bc71f6..49b98ede05b 100644 --- a/lib/transport/tests/test_transport_factory_id.c +++ b/lib/transport/tests/test_transport_factory_id.c @@ -41,7 +41,7 @@ Test(transport_factory_id, lifecycle) cr_assert_not_null(ids, "transport_factory_id_register failed"); cr_expect(transport_factory_id_equal(ids->data, expected)); transport_factory_id_free(expected); - g_list_free_full(ids, transport_factory_id_free); + g_list_free_full(ids, (GDestroyNotify)transport_factory_id_free); transport_factory_id_global_deinit(); diff --git a/lib/transport/tests/test_transport_factory_registry.c b/lib/transport/tests/test_transport_factory_registry.c index acca019a6eb..cba820f5278 100644 --- a/lib/transport/tests/test_transport_factory_registry.c +++ b/lib/transport/tests/test_transport_factory_registry.c @@ -30,7 +30,7 @@ typedef struct _TestTransportFactory TransportFactory super; } TestTransportFactory; -DEFINE_TRANSPORT_FACTORY_ID_FUN(test_transport_factory_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN(test, test_transport_factory_id); static TestTransportFactory * _test_transport_factory_new(void) diff --git a/lib/transport/transport-factory-id.c b/lib/transport/transport-factory-id.c index abf9bce903d..1d55d052aea 100644 --- a/lib/transport/transport-factory-id.c +++ b/lib/transport/transport-factory-id.c @@ -25,15 +25,26 @@ #include "transport/transport-factory-id.h" #include "mainloop.h" +struct _TransportFactoryId +{ + gchar *transport_name; + gchar *uniq_id; +}; + static GList *transport_factory_ids; void transport_factory_id_global_init(void) { } +static inline void _free(gpointer s) +{ + transport_factory_id_free((TransportFactoryId *)s); +} + void transport_factory_id_global_deinit(void) { - g_list_free_full(transport_factory_ids, transport_factory_id_free); + g_list_free_full(transport_factory_ids, _free); transport_factory_ids = NULL; } @@ -45,9 +56,12 @@ transport_factory_id_register(TransportFactoryId *id) } static gpointer -_clone(gconstpointer id) +_clone(gconstpointer s) { - return g_string_new(((const GString *)id)->str); + TransportFactoryId *id = (TransportFactoryId *)s; + TransportFactoryId *cloned = transport_factory_id_new(g_strdup(id->transport_name), + g_strdup(id->uniq_id)); + return cloned; } TransportFactoryId *transport_factory_id_clone(const TransportFactoryId *id) @@ -68,27 +82,39 @@ transport_factory_id_clone_registered_ids(void) } void -transport_factory_id_free(gpointer id) -{ - g_string_free((GString *)id, TRUE); -} - -const gchar * -transport_factory_id_to_string(const TransportFactoryId *id) +transport_factory_id_free(TransportFactoryId *id) { - GString *str = (GString *)id; - return str->str; + g_free(id->transport_name); + g_free(id->uniq_id); + g_free(id); } guint transport_factory_id_hash(gconstpointer key) { - return g_string_hash((const GString *)key); + TransportFactoryId *id = (TransportFactoryId *)key; + return g_str_hash(id->uniq_id); } gboolean transport_factory_id_equal(const TransportFactoryId *id1, const TransportFactoryId *id2) { - return g_string_equal(id1, id2); + return g_str_equal(id1->uniq_id, id2->uniq_id); } +const gchar * +transport_factory_id_get_transport_name(const TransportFactoryId *id) +{ + return id->transport_name; +} + +TransportFactoryId * +transport_factory_id_new(const gchar *transport_name, const gchar *uniq_id) +{ + TransportFactoryId *id = g_new0(TransportFactoryId, 1); + + id->transport_name = g_strdup(transport_name); + id->uniq_id = g_strdup(uniq_id); + + return id; +} diff --git a/lib/transport/transport-factory-id.h b/lib/transport/transport-factory-id.h index 5c9cfaf981f..232e7f7a439 100644 --- a/lib/transport/transport-factory-id.h +++ b/lib/transport/transport-factory-id.h @@ -27,27 +27,31 @@ #include "syslog-ng.h" -typedef GString TransportFactoryId; +typedef struct _TransportFactoryId TransportFactoryId; void transport_factory_id_global_init(void); void transport_factory_id_global_deinit(void); void transport_factory_id_register(TransportFactoryId *); GList *transport_factory_id_clone_registered_ids(void); -void transport_factory_id_free(gpointer); -const gchar *transport_factory_id_to_string(const TransportFactoryId *); +void transport_factory_id_free(TransportFactoryId *); guint transport_factory_id_hash(gconstpointer); gboolean transport_factory_id_equal(const TransportFactoryId *, const TransportFactoryId *); TransportFactoryId *transport_factory_id_clone(const TransportFactoryId *); +TransportFactoryId *transport_factory_id_new(const gchar *transport_name, const gchar *uniq_id); +const gchar *transport_factory_id_get_transport_name(const TransportFactoryId *); -#define TRANSPORT_FACTORY_ID_NEW(name) ({GString *str = g_string_new(""); g_string_printf(str, "%s-%s:%d", name, __FILE__, __LINE__); str;}) +#define TRANSPORT_FACTORY_ID_NEW(name) ({gchar *uniq_id = g_strdup_printf("%s-%s:%d", name, __FILE__, __LINE__); \ + TransportFactoryId *id_ = transport_factory_id_new(name, uniq_id); \ + g_free(uniq_id);\ + id_;}) -#define DEFINE_TRANSPORT_FACTORY_ID_FUN(name) \ - const TransportFactoryId* name(void) \ +#define DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_name, func_name) \ + const TransportFactoryId* func_name(void) \ {\ static TransportFactoryId *id;\ if (!id)\ {\ - id = TRANSPORT_FACTORY_ID_NEW(#name);\ + id = TRANSPORT_FACTORY_ID_NEW(#transport_name);\ transport_factory_id_register(id);\ }\ return id;\ diff --git a/lib/transport/transport-factory-socket.c b/lib/transport/transport-factory-socket.c index 9ac22501a45..394e4b8307c 100644 --- a/lib/transport/transport-factory-socket.c +++ b/lib/transport/transport-factory-socket.c @@ -27,7 +27,7 @@ typedef struct _TransportFactorySocket TransportFactorySocket; -DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_factory_socket_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN(socket, transport_factory_socket_id); struct _TransportFactorySocket { diff --git a/lib/transport/transport-factory-tls.c b/lib/transport/transport-factory-tls.c index 8b1cfb7a429..04a8c14a2aa 100644 --- a/lib/transport/transport-factory-tls.c +++ b/lib/transport/transport-factory-tls.c @@ -25,7 +25,7 @@ #include "transport/transport-factory-tls.h" #include "transport/transport-tls.h" -DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_factory_tls_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN(tls, transport_factory_tls_id); typedef struct _TransportFactoryTLS TransportFactoryTLS; diff --git a/lib/transport/transport-factory.h b/lib/transport/transport-factory.h index 9552911480e..49bc7655dad 100644 --- a/lib/transport/transport-factory.h +++ b/lib/transport/transport-factory.h @@ -44,20 +44,27 @@ struct _TransportFactory void (*free_fn)(TransportFactory *self); }; -static inline LogTransport *transport_factory_construct_transport(const TransportFactory *self, gint fd) +static inline LogTransport +*transport_factory_construct_transport(const TransportFactory *self, gint fd) { g_assert(self->construct_transport); - return self->construct_transport(self, fd); + + LogTransport *transport = self->construct_transport(self, fd); + transport->name = transport_factory_id_get_transport_name(self->id); + + return transport; } -static inline void transport_factory_free(TransportFactory *self) +static inline void +transport_factory_free(TransportFactory *self) { if (self->free_fn) self->free_fn(self); g_free(self); } -static inline const TransportFactoryId *transport_factory_get_id(const TransportFactory *self) +static inline const +TransportFactoryId *transport_factory_get_id(const TransportFactory *self) { /* each concrete TransportFactory has to have an id */ From e102cf9e7251107c56b18c1b58df6016ca1e27e5 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Wed, 23 May 2018 20:49:39 +0200 Subject: [PATCH 14/16] transport: use a sequence as uniq id instead of location macros Signed-off-by: Laszlo Budai --- lib/transport/tests/test_multitransport.c | 2 +- lib/transport/tests/test_transport_factory.c | 2 +- .../tests/test_transport_factory_id.c | 8 ++-- .../tests/test_transport_factory_registry.c | 2 +- lib/transport/transport-factory-id.c | 48 +++++++++++++++---- lib/transport/transport-factory-id.h | 9 +--- lib/transport/transport-factory-socket.c | 2 +- lib/transport/transport-factory-tls.c | 2 +- 8 files changed, 50 insertions(+), 25 deletions(-) diff --git a/lib/transport/tests/test_multitransport.c b/lib/transport/tests/test_multitransport.c index 9ab1ef81cf7..8f13a3c069f 100644 --- a/lib/transport/tests/test_multitransport.c +++ b/lib/transport/tests/test_multitransport.c @@ -55,7 +55,7 @@ { \ TransportFactory super; \ }; \ - DEFINE_TRANSPORT_FACTORY_ID_FUN(FunPrefix, FunPrefix ## _transport_factory_id); \ + DEFINE_TRANSPORT_FACTORY_ID_FUN(#FunPrefix, FunPrefix ## _transport_factory_id); \ static LogTransport * \ FunPrefix ## _factory_construct(const TransportFactory *s, gint fd) \ {\ diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index d4b9a9da1eb..f3f7c69eaca 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -66,7 +66,7 @@ struct _FakeTransportFactory gboolean destroyed; }; -DEFINE_TRANSPORT_FACTORY_ID_FUN(fake, _fake_transport_factory_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN("fake", _fake_transport_factory_id); static LogTransport * _transport_factory_construct(const TransportFactory *s, gint fd) diff --git a/lib/transport/tests/test_transport_factory_id.c b/lib/transport/tests/test_transport_factory_id.c index 49b98ede05b..bf8a3a0c419 100644 --- a/lib/transport/tests/test_transport_factory_id.c +++ b/lib/transport/tests/test_transport_factory_id.c @@ -33,7 +33,7 @@ Test(transport_factory_id, lifecycle) cr_expect_null(ids); - TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); + TransportFactoryId *id = transport_factory_id_new("tcp"); TransportFactoryId *expected = transport_factory_id_clone(id); transport_factory_id_register(id); @@ -51,14 +51,14 @@ Test(transport_factory_id, lifecycle) Test(transport_factory_id_basic_macros, generated_ids_are_uniq) { - TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); - TransportFactoryId *id2 = TRANSPORT_FACTORY_ID_NEW("tcp"); + TransportFactoryId *id = transport_factory_id_new("tcp"); + TransportFactoryId *id2 = transport_factory_id_new("tcp"); cr_expect_not(transport_factory_id_equal(id, id2)); } Test(transport_factory_id_basic_macros, clones_are_equals) { - TransportFactoryId *id = TRANSPORT_FACTORY_ID_NEW("tcp"); + TransportFactoryId *id = transport_factory_id_new("tcp"); TransportFactoryId *cloned = transport_factory_id_clone(id); cr_expect(transport_factory_id_equal(id, cloned)); cr_expect_eq(transport_factory_id_hash(id), transport_factory_id_hash(cloned)); diff --git a/lib/transport/tests/test_transport_factory_registry.c b/lib/transport/tests/test_transport_factory_registry.c index cba820f5278..a1fe0309176 100644 --- a/lib/transport/tests/test_transport_factory_registry.c +++ b/lib/transport/tests/test_transport_factory_registry.c @@ -30,7 +30,7 @@ typedef struct _TestTransportFactory TransportFactory super; } TestTransportFactory; -DEFINE_TRANSPORT_FACTORY_ID_FUN(test, test_transport_factory_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN("test", test_transport_factory_id); static TestTransportFactory * _test_transport_factory_new(void) diff --git a/lib/transport/transport-factory-id.c b/lib/transport/transport-factory-id.c index 1d55d052aea..8b0c97fb809 100644 --- a/lib/transport/transport-factory-id.c +++ b/lib/transport/transport-factory-id.c @@ -28,16 +28,43 @@ struct _TransportFactoryId { gchar *transport_name; - gchar *uniq_id; + gint uniq_id; }; +typedef struct _Sequence Sequence; + +struct _Sequence +{ + gint ctr; +} sequence; + +static void +sequence_inc(Sequence *seq) +{ + seq->ctr++; +} + +static gint +sequence_get(Sequence *seq) +{ + return seq->ctr; +} + +static void +sequence_reset(Sequence *seq, gint init_value) +{ + seq->ctr = init_value; +} + static GList *transport_factory_ids; void transport_factory_id_global_init(void) { + sequence_reset(&sequence, 1); } -static inline void _free(gpointer s) +static inline void +_free(gpointer s) { transport_factory_id_free((TransportFactoryId *)s); } @@ -59,8 +86,11 @@ static gpointer _clone(gconstpointer s) { TransportFactoryId *id = (TransportFactoryId *)s; - TransportFactoryId *cloned = transport_factory_id_new(g_strdup(id->transport_name), - g_strdup(id->uniq_id)); + TransportFactoryId *cloned = g_new0(TransportFactoryId, 1); + + cloned->transport_name = g_strdup(id->transport_name); + cloned->uniq_id = id->uniq_id; + return cloned; } @@ -85,7 +115,6 @@ void transport_factory_id_free(TransportFactoryId *id) { g_free(id->transport_name); - g_free(id->uniq_id); g_free(id); } @@ -93,13 +122,13 @@ guint transport_factory_id_hash(gconstpointer key) { TransportFactoryId *id = (TransportFactoryId *)key; - return g_str_hash(id->uniq_id); + return g_direct_hash(GINT_TO_POINTER(id->uniq_id)); } gboolean transport_factory_id_equal(const TransportFactoryId *id1, const TransportFactoryId *id2) { - return g_str_equal(id1->uniq_id, id2->uniq_id); + return (id1->uniq_id == id2->uniq_id) ? TRUE : FALSE; } const gchar * @@ -109,12 +138,13 @@ transport_factory_id_get_transport_name(const TransportFactoryId *id) } TransportFactoryId * -transport_factory_id_new(const gchar *transport_name, const gchar *uniq_id) +transport_factory_id_new(const gchar *transport_name) { TransportFactoryId *id = g_new0(TransportFactoryId, 1); + sequence_inc(&sequence); id->transport_name = g_strdup(transport_name); - id->uniq_id = g_strdup(uniq_id); + id->uniq_id = sequence_get(&sequence); return id; } diff --git a/lib/transport/transport-factory-id.h b/lib/transport/transport-factory-id.h index 232e7f7a439..e99b3611820 100644 --- a/lib/transport/transport-factory-id.h +++ b/lib/transport/transport-factory-id.h @@ -37,21 +37,16 @@ void transport_factory_id_free(TransportFactoryId *); guint transport_factory_id_hash(gconstpointer); gboolean transport_factory_id_equal(const TransportFactoryId *, const TransportFactoryId *); TransportFactoryId *transport_factory_id_clone(const TransportFactoryId *); -TransportFactoryId *transport_factory_id_new(const gchar *transport_name, const gchar *uniq_id); +TransportFactoryId *transport_factory_id_new(const gchar *transport_name); const gchar *transport_factory_id_get_transport_name(const TransportFactoryId *); -#define TRANSPORT_FACTORY_ID_NEW(name) ({gchar *uniq_id = g_strdup_printf("%s-%s:%d", name, __FILE__, __LINE__); \ - TransportFactoryId *id_ = transport_factory_id_new(name, uniq_id); \ - g_free(uniq_id);\ - id_;}) - #define DEFINE_TRANSPORT_FACTORY_ID_FUN(transport_name, func_name) \ const TransportFactoryId* func_name(void) \ {\ static TransportFactoryId *id;\ if (!id)\ {\ - id = TRANSPORT_FACTORY_ID_NEW(#transport_name);\ + id = transport_factory_id_new(transport_name);\ transport_factory_id_register(id);\ }\ return id;\ diff --git a/lib/transport/transport-factory-socket.c b/lib/transport/transport-factory-socket.c index 394e4b8307c..171684ec57f 100644 --- a/lib/transport/transport-factory-socket.c +++ b/lib/transport/transport-factory-socket.c @@ -27,7 +27,7 @@ typedef struct _TransportFactorySocket TransportFactorySocket; -DEFINE_TRANSPORT_FACTORY_ID_FUN(socket, transport_factory_socket_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN("socket", transport_factory_socket_id); struct _TransportFactorySocket { diff --git a/lib/transport/transport-factory-tls.c b/lib/transport/transport-factory-tls.c index 04a8c14a2aa..ad78f22e821 100644 --- a/lib/transport/transport-factory-tls.c +++ b/lib/transport/transport-factory-tls.c @@ -25,7 +25,7 @@ #include "transport/transport-factory-tls.h" #include "transport/transport-tls.h" -DEFINE_TRANSPORT_FACTORY_ID_FUN(tls, transport_factory_tls_id); +DEFINE_TRANSPORT_FACTORY_ID_FUN("tls", transport_factory_tls_id); typedef struct _TransportFactoryTLS TransportFactoryTLS; From 7de3b2ed12c10f4efa2cf3db8ff871924847ecb3 Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 24 May 2018 16:27:42 +0200 Subject: [PATCH 15/16] transport: fix testcases Signed-off-by: Laszlo Budai --- lib/transport/tests/test_transport_factory.c | 9 ++++++--- lib/transport/tests/test_transport_factory_registry.c | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/transport/tests/test_transport_factory.c b/lib/transport/tests/test_transport_factory.c index f3f7c69eaca..2ba12a094d5 100644 --- a/lib/transport/tests/test_transport_factory.c +++ b/lib/transport/tests/test_transport_factory.c @@ -25,8 +25,6 @@ #include "apphook.h" #include -TestSuite(transport_factory_id, .init = app_startup, .fini = app_shutdown); - typedef struct _FakeTransport FakeTransport; typedef struct _FakeTransportFactory FakeTransportFactory; @@ -48,6 +46,11 @@ _fake_write(LogTransport *s, const gpointer buf, gsize count) return count; } +static void +_fake_free(LogTransport *s) +{ +} + LogTransport * _fake_transport_new(void) { @@ -73,6 +76,7 @@ _transport_factory_construct(const TransportFactory *s, gint fd) { LogTransport *fake_transport = _fake_transport_new(); log_transport_init_instance(fake_transport, fd); + fake_transport->free_fn = _fake_free; return fake_transport; } @@ -112,4 +116,3 @@ Test(transport_factory, fake_transport_factory) transport_factory_free(&fake_factory->super); cr_expect_eq(fake_factory->destroyed, TRUE); } - diff --git a/lib/transport/tests/test_transport_factory_registry.c b/lib/transport/tests/test_transport_factory_registry.c index a1fe0309176..33175575c84 100644 --- a/lib/transport/tests/test_transport_factory_registry.c +++ b/lib/transport/tests/test_transport_factory_registry.c @@ -35,7 +35,7 @@ DEFINE_TRANSPORT_FACTORY_ID_FUN("test", test_transport_factory_id); static TestTransportFactory * _test_transport_factory_new(void) { - TestTransportFactory *instance = g_new(TestTransportFactory, 1); + TestTransportFactory *instance = g_new0(TestTransportFactory, 1); instance->super.id = test_transport_factory_id(); @@ -53,7 +53,7 @@ Test(transport_factory_registry, basic_functionality) transport_factory_registry_add(registry, &factory->super); const TransportFactory *looked_up_factory = (TransportFactory *)transport_factory_registry_lookup(registry, test_transport_factory_id()); - cr_expect_eq(factory, looked_up_factory); + cr_expect_eq((gpointer)factory, (gpointer)looked_up_factory); transport_factory_registry_free(registry); } From 3753d7b20cf0a0285e6ac465c69eac0b0c9b486d Mon Sep 17 00:00:00 2001 From: Laszlo Budai Date: Thu, 24 May 2018 21:24:12 +0200 Subject: [PATCH 16/16] afsocket: fix regression: support TLS in legacy tcp driver Caught by functest where TLS is configured by using the legacy TCP driver. The problem was that Multitransport was constructed with default plain TCP transport and TLS related tests stalled. The fix: Construct TLS transport when legacy TCP is used with TLS. When transport() is not used and we have TLS context it means that user configured TLS with legacy TCP driver. Signed-off-by: Laszlo Budai --- modules/afsocket/transport-mapper-inet.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/afsocket/transport-mapper-inet.c b/modules/afsocket/transport-mapper-inet.c index 33c4073c33b..11c1a422b79 100644 --- a/modules/afsocket/transport-mapper-inet.c +++ b/modules/afsocket/transport-mapper-inet.c @@ -120,7 +120,7 @@ transport_mapper_inet_construct_log_transport(TransportMapper *s, gint fd) { TransportMapperInet *self = (TransportMapperInet *) s; - if (self->tls_context && self->require_tls) + if (self->tls_context && _is_tls_allowed(self)) { return _construct_tls_transport(self, fd); } @@ -365,7 +365,23 @@ transport_mapper_network_apply_transport(TransportMapper *s, GlobalConfig *cfg) self->super.sock_proto = IPPROTO_TCP; /* FIXME: look up port/protocol from the logproto */ self->server_port = TCP_PORT; - self->allow_tls = TRUE; + if (!transport) + { + /* + * THIS CASE IS FOR SUPPORTING TLS IN LEGACY TCP DRIVER + * example: source s_inetssl { + * tcp(port(5555) + * tls(peer-verify(none) + * cert-file("ssl.crt") + * key-file(ssl.key"))); + * }; + * + * When transport("plugin") is used with with a valid TLSContext, + * Multitransport is used, this is why allow_tls is set only when + * transport() is not used. + * */ + self->allow_tls = TRUE; + } } g_assert(self->server_port != 0);