Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/transport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ set(TRANSPORT_HEADERS
transport/transport-pipe.h
transport/transport-socket.h
transport/transport-factory-id.h
transport/transport-factory.h
transport/transport-factory-registry.h
transport/multitransport.h
transport/transport-factory-tls.h
transport/transport-factory-socket.h
PARENT_SCOPE)

set(TRANSPORT_SOURCES
Expand All @@ -16,6 +21,10 @@ set(TRANSPORT_SOURCES
transport/transport-socket.c
transport/transport-tls.c
transport/transport-factory-id.c
transport/transport-factory-registry.c
transport/multitransport.c
transport/transport-factory-tls.c
transport/transport-factory-socket.c
PARENT_SCOPE)

add_test_subdirectory(tests)
13 changes: 11 additions & 2 deletions lib/transport/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@ 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 \
lib/transport/transport-factory-registry.h \
lib/transport/multitransport.h \
lib/transport/transport-factory-tls.h \
lib/transport/transport-factory-socket.h

transport_sources = \
lib/transport/logtransport.c \
lib/transport/transport-aux-data.c \
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 \
lib/transport/multitransport.c \
lib/transport/transport-factory-tls.c \
lib/transport/transport-factory-socket.c

transport_crypto_sources = \
lib/transport/transport-tls.c
Expand Down
10 changes: 10 additions & 0 deletions lib/transport/logtransport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

2 changes: 2 additions & 0 deletions lib/transport/logtransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -54,5 +55,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
138 changes: 138 additions & 0 deletions lib/transport/multitransport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2002-2018 Balabit
* Copyright (c) 2018 Laszlo Budai <laszlo.budai@balabit.com>
*
* 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("Requested transport not found",
evt_tag_str("transport", transport_factory_id_get_transport_name(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("Failed to construct transport",
evt_tag_str("transport", transport_factory_id_get_transport_name(factory_id)));
return NULL;
}

return transport;
}

gboolean multitransport_switch(MultiTransport *self, const TransportFactoryId *factory_id)
{
msg_debug("Transport switch requested",
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)
return FALSE;

LogTransport *transport = _construct_transport(transport_factory, self->super.fd);
if (!transport)
return FALSE;

_do_transport_switch(self, transport, transport_factory);

msg_debug("Transport switch succeded",
evt_tag_str("new-active-transport", self->active_transport->name));

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;
}
47 changes: 47 additions & 0 deletions lib/transport/multitransport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2002-2018 Balabit
* Copyright (c) 2018 Laszlo Budai <laszlo.budai@balabit.com>
*
* 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

3 changes: 3 additions & 0 deletions lib/transport/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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)
23 changes: 22 additions & 1 deletion lib/transport/tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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 \
lib/transport/tests/test_transport_factory_registry \
lib/transport/tests/test_multitransport

check_PROGRAMS += ${lib_transport_tests_TESTS}

Expand All @@ -15,3 +18,21 @@ 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

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

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
Loading