medcli plugin writes connection status to database
authorMartin Willi <martin@strongswan.org>
Thu, 19 Jun 2008 08:46:34 +0000 (08:46 -0000)
committerMartin Willi <martin@strongswan.org>
Thu, 19 Jun 2008 08:46:34 +0000 (08:46 -0000)
src/charon/plugins/medcli/Makefile.am
src/charon/plugins/medcli/medcli_listener.c [new file with mode: 0644]
src/charon/plugins/medcli/medcli_listener.h [new file with mode: 0644]
src/charon/plugins/medcli/medcli_plugin.c

index f657868..f15950a 100644 (file)
@@ -6,6 +6,7 @@ AM_CFLAGS = -rdynamic
 plugin_LTLIBRARIES = libstrongswan-medcli.la
 libstrongswan_medcli_la_SOURCES = medcli_plugin.h medcli_plugin.c \
                              medcli_creds.h medcli_creds.c \
-                             medcli_config.h medcli_config.c
+                             medcli_config.h medcli_config.c \
+                             medcli_listener.h medcli_listener.c
 libstrongswan_medcli_la_LDFLAGS = -module
 
diff --git a/src/charon/plugins/medcli/medcli_listener.c b/src/charon/plugins/medcli/medcli_listener.c
new file mode 100644 (file)
index 0000000..cb370ba
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2008 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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.
+ *
+ * $Id$
+ */
+
+#include "medcli_listener.h"
+
+#include <daemon.h>
+#include <library.h>
+
+typedef struct private_medcli_listener_t private_medcli_listener_t;
+typedef enum mediated_state_t mediated_state_t;
+
+/**
+ * state of a mediated connection
+ */
+enum mediated_state_t {
+       STATE_DOWN = 1,
+       STATE_CONNECTING = 2,
+       STATE_UP = 3,
+};
+
+/**
+ * Private data of an medcli_listener_t object
+ */
+struct private_medcli_listener_t {
+
+       /**
+        * Public part
+        */
+       medcli_listener_t public;
+       
+       /**
+        * underlying database handle
+        */
+       database_t *db;
+};
+
+/**
+ * Implementation of bus_listener_t.signal.
+ */
+static bool signal_(private_medcli_listener_t *this, signal_t signal, level_t level,
+                                       int thread, ike_sa_t* ike_sa, char *format, va_list args)
+{
+       mediated_state_t state;
+       
+       if (!ike_sa)
+       {
+               return TRUE;
+       }
+
+       switch (signal)
+       {
+               case IKE_UP_START:
+                       state = STATE_CONNECTING;
+                       break;
+               case IKE_UP_FAILED:
+               case IKE_DOWN_SUCCESS:
+               case IKE_DOWN_FAILED:
+                       state = STATE_DOWN;
+                       break;
+               case IKE_UP_SUCCESS:
+                       state = STATE_UP;
+                       break;
+               default:
+                       return TRUE;
+       }
+       this->db->execute(this->db, NULL,
+                                         "UPDATE Connection SET Status = ? WHERE Alias = ?",
+                                         DB_UINT, state, DB_TEXT, ike_sa->get_name(ike_sa));
+       return TRUE;
+}
+
+/**
+ * Implementation of backend_t.destroy.
+ */
+static void destroy(private_medcli_listener_t *this)
+{
+       this->db->execute(this->db, NULL, "UPDATE Connection SET Status = ?",
+                                         DB_UINT, STATE_DOWN);
+    free(this);
+}
+
+/**
+ * Described in header.
+ */
+medcli_listener_t *medcli_listener_create(database_t *db)
+{
+       private_medcli_listener_t *this = malloc_thing(private_medcli_listener_t);
+
+       this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
+       this->public.destroy = (void (*)(medcli_listener_t*))destroy;
+       
+       this->db = db;
+       db->execute(db, NULL, "UPDATE Connection SET Status = ?",
+                               DB_UINT, STATE_DOWN);
+       
+       return &this->public;
+}
+
diff --git a/src/charon/plugins/medcli/medcli_listener.h b/src/charon/plugins/medcli/medcli_listener.h
new file mode 100644 (file)
index 0000000..f07218d
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2008 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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.
+ *
+ * $Id$
+ */
+
+/**
+ * @defgroup medcli_listener_i medcli_listener
+ * @{ @ingroup medcli
+ */
+
+#ifndef MEDCLI_LISTENER_H_
+#define MEDCLI_LISTENER_H_
+
+#include <bus/bus.h>
+#include <database/database.h>
+
+typedef struct medcli_listener_t medcli_listener_t;
+
+/**
+ * Mediation client listener, writes connection status to database
+ */
+struct medcli_listener_t {
+
+       /**
+        * Implements bus_listener_t interface
+        */
+       bus_listener_t listener;
+       
+       /**
+        * Destroy the credentials databse.
+        */
+       void (*destroy)(medcli_listener_t *this);       
+};
+
+/**
+ * Create the medcli credential set.
+ *
+ * @param database             underlying database
+ * @return                             listener
+ */
+medcli_listener_t *medcli_listener_create(database_t *database);
+
+#endif /* MEDCLI_LISTENER_H_ @}*/
index 016a2d6..1642ed2 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "medcli_creds.h"
 #include "medcli_config.h"
+#include "medcli_listener.h"
 
 #include <daemon.h>
 
@@ -48,6 +49,11 @@ struct private_medcli_plugin_t {
         * medcli config database
         */
        medcli_config_t *config;
+       
+       /**
+        * Listener to update database connection state
+        */
+       medcli_listener_t *listener;
 };
 
 /**
@@ -55,8 +61,10 @@ struct private_medcli_plugin_t {
  */
 static void destroy(private_medcli_plugin_t *this)
 {
+       charon->bus->remove_listener(charon->bus, &this->listener->listener);
        charon->backends->remove_backend(charon->backends, &this->config->backend);
        charon->credentials->remove_set(charon->credentials, &this->creds->set);
+       this->listener->destroy(this->listener);
        this->config->destroy(this->config);
        this->creds->destroy(this->creds);
        this->db->destroy(this->db);
@@ -74,7 +82,7 @@ plugin_t *plugin_create()
        this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
        
        uri = lib->settings->get_str(lib->settings,
-                                                                "medclient.database", NULL);
+                                                                "medcli.database", NULL);
        if (!uri)
        {
                DBG1(DBG_CFG, "mediation client database URI not defined, skipped");
@@ -92,9 +100,11 @@ plugin_t *plugin_create()
        
        this->creds = medcli_creds_create(this->db);
        this->config = medcli_config_create(this->db);
+       this->listener = medcli_listener_create(this->db);
        
        charon->credentials->add_set(charon->credentials, &this->creds->set);
        charon->backends->add_backend(charon->backends, &this->config->backend);
+       charon->bus->add_listener(charon->bus, &this->listener->listener);
        
        return &this->public.plugin;
 }