stream: add a manager to dynamically register streams and services
authorMartin Willi <martin@revosec.ch>
Wed, 26 Jun 2013 15:28:19 +0000 (17:28 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 18 Jul 2013 14:00:28 +0000 (16:00 +0200)
src/libstrongswan/Android.mk
src/libstrongswan/Makefile.am
src/libstrongswan/networking/streams/stream_manager.c [new file with mode: 0644]
src/libstrongswan/networking/streams/stream_manager.h [new file with mode: 0644]

index 790105f..3811ed0 100644 (file)
@@ -27,7 +27,7 @@ database/database_factory.c fetcher/fetcher.c fetcher/fetcher_manager.c eap/eap.
 ipsec/ipsec_types.c \
 networking/host.c networking/host_resolver.c networking/packet.c \
 networking/tun_device.c networking/streams/stream.c \
-networking/streams/stream_service.c \
+networking/streams/stream_service.c networking/streams/stream_manager.c \
 pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \
 processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
 processing/watcher.c resolver/resolver_manager.c resolver/rr_set.c \
index 1edd3ff..dfe6e7e 100644 (file)
@@ -25,7 +25,7 @@ database/database_factory.c fetcher/fetcher.c fetcher/fetcher_manager.c eap/eap.
 ipsec/ipsec_types.c \
 networking/host.c networking/host_resolver.c networking/packet.c \
 networking/tun_device.c networking/streams/stream.c \
-networking/streams/stream_service.c \
+networking/streams/stream_service.c networking/streams/stream_manager.c \
 pen/pen.c plugins/plugin_loader.c plugins/plugin_feature.c processing/jobs/job.c \
 processing/jobs/callback_job.c processing/processor.c processing/scheduler.c \
 processing/watcher.c resolver/resolver_manager.c resolver/rr_set.c \
@@ -67,7 +67,7 @@ database/database.h database/database_factory.h fetcher/fetcher.h \
 fetcher/fetcher_manager.h eap/eap.h pen/pen.h ipsec/ipsec_types.h \
 networking/host.h networking/host_resolver.h networking/packet.h \
 networking/tun_device.h networking/streams/stream.h \
-networking/streams/stream_service.h \
+networking/streams/stream_service.h networking/streams/stream_manager.h \
 resolver/resolver.h resolver/resolver_response.h resolver/rr_set.h \
 resolver/rr.h resolver/resolver_manager.h \
 plugins/plugin_loader.h plugins/plugin.h plugins/plugin_feature.h \
diff --git a/src/libstrongswan/networking/streams/stream_manager.c b/src/libstrongswan/networking/streams/stream_manager.c
new file mode 100644 (file)
index 0000000..d28cb70
--- /dev/null
@@ -0,0 +1,282 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program 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 General Public License
+ * for more details.
+ */
+
+#include "stream_manager.h"
+
+#include <threading/rwlock.h>
+
+typedef struct private_stream_manager_t private_stream_manager_t;
+
+/**
+ * Private data of an stream_manager_t object.
+ */
+struct private_stream_manager_t {
+
+       /**
+        * Public stream_manager_t interface.
+        */
+       stream_manager_t public;
+
+       /**
+        * List of registered stream constructors, as stream_entry_t
+        */
+       linked_list_t *streams;
+
+       /**
+        * List of registered service constructors, as service_entry_t
+        */
+       linked_list_t *services;
+
+       /**
+        * List of registered running services, as running_entry_t
+        */
+       linked_list_t *running;
+
+       /**
+        * Lock for all lists
+        */
+       rwlock_t *lock;
+};
+
+/**
+ * Registered stream backend
+ */
+typedef struct {
+       /** URI prefix */
+       char *prefix;
+       /** constructor function */
+       stream_constructor_t create;
+} stream_entry_t;
+
+/**
+ * Registered service backend
+ */
+typedef struct {
+       /** URI prefix */
+       char *prefix;
+       /** constructor function */
+       stream_service_constructor_t create;
+} service_entry_t;
+
+/**
+ * Running service
+ */
+typedef struct {
+       /** URI of service */
+       char *uri;
+       /** stream accept()ing connections */
+       stream_service_t *service;
+} running_entry_t;
+
+METHOD(stream_manager_t, connect_, stream_t*,
+       private_stream_manager_t *this, char *uri)
+{
+       enumerator_t *enumerator;
+       stream_entry_t *entry;
+       stream_t *stream = NULL;
+
+       this->lock->read_lock(this->lock);
+       enumerator = this->streams->create_enumerator(this->streams);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               if (strpfx(uri, entry->prefix))
+               {
+                       stream = entry->create(uri);
+                       if (stream)
+                       {
+                               break;
+                       }
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+
+       return stream;
+}
+
+METHOD(stream_manager_t, start_service, bool,
+       private_stream_manager_t *this, char *uri,
+       stream_service_cb_t cb, void *data)
+{
+       running_entry_t *running;
+       enumerator_t *enumerator;
+       service_entry_t *entry;
+       stream_service_t *service = NULL;
+
+       this->lock->read_lock(this->lock);
+       enumerator = this->services->create_enumerator(this->services);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               if (strpfx(uri, entry->prefix))
+               {
+                       service = entry->create(uri);
+                       if (service)
+                       {
+                               break;
+                       }
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+
+       if (!service)
+       {
+               return FALSE;
+       }
+
+       INIT(running,
+               .uri = strdup(uri),
+               .service = service,
+       );
+       service->on_accept(service, cb, data);
+
+       this->lock->write_lock(this->lock);
+       this->running->insert_last(this->running, running);
+       this->lock->unlock(this->lock);
+
+       return TRUE;
+}
+
+METHOD(stream_manager_t, stop_service, void,
+       private_stream_manager_t *this, char *uri)
+{
+       enumerator_t *enumerator;
+       running_entry_t *entry;
+
+       this->lock->write_lock(this->lock);
+       enumerator = this->running->create_enumerator(this->running);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               if (streq(entry->uri, uri))
+               {
+                       this->running->remove_at(this->running, enumerator);
+                       entry->service->destroy(entry->service);
+                       free(entry->uri);
+                       free(entry);
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+}
+
+METHOD(stream_manager_t, add_stream, void,
+       private_stream_manager_t *this, char *prefix, stream_constructor_t create)
+{
+       stream_entry_t *entry;
+
+       INIT(entry,
+               .prefix = strdup(prefix),
+               .create = create,
+       );
+
+       this->lock->write_lock(this->lock);
+       this->streams->insert_last(this->streams, entry);
+       this->lock->unlock(this->lock);
+}
+
+METHOD(stream_manager_t, remove_stream, void,
+       private_stream_manager_t *this, stream_constructor_t create)
+{
+       enumerator_t *enumerator;
+       stream_entry_t *entry;
+
+       this->lock->write_lock(this->lock);
+       enumerator = this->streams->create_enumerator(this->streams);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               if (entry->create == create)
+               {
+                       this->streams->remove_at(this->streams, enumerator);
+                       free(entry->prefix);
+                       free(entry);
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+}
+
+METHOD(stream_manager_t, add_service, void,
+       private_stream_manager_t *this, char *prefix,
+       stream_service_constructor_t create)
+{
+       service_entry_t *entry;
+
+       INIT(entry,
+               .prefix = strdup(prefix),
+               .create = create,
+       );
+
+       this->lock->write_lock(this->lock);
+       this->services->insert_last(this->services, entry);
+       this->lock->unlock(this->lock);
+}
+
+METHOD(stream_manager_t, remove_service, void,
+       private_stream_manager_t *this, stream_service_constructor_t create)
+{
+       enumerator_t *enumerator;
+       service_entry_t *entry;
+
+       this->lock->write_lock(this->lock);
+       enumerator = this->services->create_enumerator(this->services);
+       while (enumerator->enumerate(enumerator, &entry))
+       {
+               if (entry->create == create)
+               {
+                       this->services->remove_at(this->services, enumerator);
+                       free(entry->prefix);
+                       free(entry);
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->lock->unlock(this->lock);
+}
+
+METHOD(stream_manager_t, destroy, void,
+       private_stream_manager_t *this)
+{
+       this->streams->destroy(this->streams);
+       this->services->destroy(this->services);
+       this->running->destroy(this->running);
+       this->lock->destroy(this->lock);
+       free(this);
+}
+
+/**
+ * See header
+ */
+stream_manager_t *stream_manager_create()
+{
+       private_stream_manager_t *this;
+
+       INIT(this,
+               .public = {
+                       .connect = _connect_,
+                       .start_service = _start_service,
+                       .stop_service = _stop_service,
+                       .add_stream = _add_stream,
+                       .remove_stream = _remove_stream,
+                       .add_service = _add_service,
+                       .remove_service = _remove_service,
+                       .destroy = _destroy,
+               },
+               .streams = linked_list_create(),
+               .services = linked_list_create(),
+               .running = linked_list_create(),
+               .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
+       );
+
+       return &this->public;
+}
diff --git a/src/libstrongswan/networking/streams/stream_manager.h b/src/libstrongswan/networking/streams/stream_manager.h
new file mode 100644 (file)
index 0000000..347596f
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program 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 General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup stream_manager stream_manager
+ * @{ @ingroup streams
+ */
+
+#ifndef STREAM_MANAGER_H_
+#define STREAM_MANAGER_H_
+
+typedef struct stream_manager_t stream_manager_t;
+
+#include <networking/streams/stream.h>
+#include <networking/streams/stream_service.h>
+
+/**
+ * Manages client-server connections and services using stream_t backends.
+ */
+struct stream_manager_t {
+
+       /**
+        * Create a client-server connection to a service.
+        *
+        * @param uri           URI of service to connect to
+        * @return                      stream instance, NULL on error
+        */
+       stream_t* (*connect)(stream_manager_t *this, char *uri);
+
+       /**
+        * Start a new service under an URI, accept()ing client connections.
+        *
+        * @param uri           URI of service to provide
+        * @param cb            callback function invoked for each client connection
+        * @param data          user data to pass to callback
+        * @return                      TRUE if service started, FALSE on failure
+        */
+       bool (*start_service)(stream_manager_t *this, char *uri,
+                                                 stream_service_cb_t cb, void *data);
+
+       /**
+        * Stop a service previously create with start_service().
+        *
+        * @param uri           URI of service to stop
+        */
+       void (*stop_service)(stream_manager_t *this, char *uri);
+
+       /**
+        * Register a stream backend to the manager.
+        *
+        * @param prefix        prefix of URIs to use the backend for
+        * @param create        constructor function for the stream
+        */
+       void (*add_stream)(stream_manager_t *this, char *prefix,
+                                          stream_constructor_t create);
+
+       /**
+        * Unregister stream backends from the manager.
+        *
+        * @param create        constructor function passed to add_stream()
+        */
+       void (*remove_stream)(stream_manager_t *this, stream_constructor_t create);
+
+       /**
+        * Register a stream service backend to the manager.
+        *
+        * @param prefix        prefix of URIs to use the backend for
+        * @param create        constructor function for the stream service
+        */
+       void (*add_service)(stream_manager_t *this, char *prefix,
+                                               stream_service_constructor_t create);
+
+       /**
+        * Unregister stream service backends from the manager.
+        *
+        * @param create        constructor function passed to add_service()
+        */
+       void (*remove_service)(stream_manager_t *this,
+                                                  stream_service_constructor_t create);
+
+       /**
+        * Destroy a stream_manager_t.
+        */
+       void (*destroy)(stream_manager_t *this);
+};
+
+/**
+ * Create a stream_manager instance.
+ */
+stream_manager_t *stream_manager_create();
+
+#endif /** STREAM_MANAGER_H_ @}*/