Add features support to tnccs plugins
authorAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 20 Oct 2011 12:05:59 +0000 (14:05 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 20 Oct 2011 12:06:12 +0000 (14:06 +0200)
src/libcharon/plugins/tnccs_11/tnccs_11_plugin.c
src/libcharon/plugins/tnccs_20/tnccs_20_plugin.c
src/libcharon/plugins/tnccs_dynamic/tnccs_dynamic_plugin.c
src/libcharon/tnc/tnccs/tnccs.c
src/libcharon/tnc/tnccs/tnccs.h

index 9ec91f0..9d227f0 100644 (file)
@@ -24,11 +24,22 @@ METHOD(plugin_t, get_name, char*,
        return "tnccs-11";
 }
 
+METHOD(plugin_t, get_features, int,
+       tnccs_11_plugin_t *this, plugin_feature_t *features[])
+{
+       static plugin_feature_t f[] = {
+               PLUGIN_CALLBACK(tnccs_method_register, tnccs_11_create),
+                       PLUGIN_PROVIDE(CUSTOM, "tnccs-1.1"),
+                               PLUGIN_DEPENDS(EAP_SERVER, EAP_TNC),
+                               PLUGIN_DEPENDS(EAP_PEER, EAP_TNC),
+       };
+       *features = f;
+       return countof(f);
+}
+
 METHOD(plugin_t, destroy, void,
        tnccs_11_plugin_t *this)
 {
-       charon->tnccs->remove_method(charon->tnccs,
-                                                               (tnccs_constructor_t)tnccs_11_create);
        free(this);
 }
 
@@ -42,14 +53,11 @@ plugin_t *tnccs_11_plugin_create()
        INIT(this,
                .plugin = {
                        .get_name = _get_name,
-                       .reload = (void*)return_false,
+                       .get_features = _get_features,
                        .destroy = _destroy,
                },
        );
 
-       charon->tnccs->add_method(charon->tnccs, TNCCS_1_1,
-                                                        (tnccs_constructor_t)tnccs_11_create);
-
        return &this->plugin;
 }
 
index e6dc699..a44c614 100644 (file)
@@ -24,11 +24,22 @@ METHOD(plugin_t, get_name, char*,
        return "tnccs-20";
 }
 
+METHOD(plugin_t, get_features, int,
+       tnccs_20_plugin_t *this, plugin_feature_t *features[])
+{
+       static plugin_feature_t f[] = {
+               PLUGIN_CALLBACK(tnccs_method_register, tnccs_20_create),
+                       PLUGIN_PROVIDE(CUSTOM, "tnccs-2.0"),
+                               PLUGIN_DEPENDS(EAP_SERVER, EAP_TNC),
+                               PLUGIN_DEPENDS(EAP_PEER, EAP_TNC),
+       };
+       *features = f;
+       return countof(f);
+}
+
 METHOD(plugin_t, destroy, void,
        tnccs_20_plugin_t *this)
 {
-       charon->tnccs->remove_method(charon->tnccs,
-                                                               (tnccs_constructor_t)tnccs_20_create);
        free(this);
 }
 
@@ -42,14 +53,11 @@ plugin_t *tnccs_20_plugin_create()
        INIT(this,
                .plugin = {
                        .get_name = _get_name,
-                       .reload = (void*)return_false,
+                       .get_features = _get_features,
                        .destroy = _destroy,
                },
        );
 
-       charon->tnccs->add_method(charon->tnccs, TNCCS_2_0,
-                                                        (tnccs_constructor_t)tnccs_20_create);
-
        return &this->plugin;
 }
 
index 089a3ed..8a6298a 100644 (file)
@@ -24,11 +24,24 @@ METHOD(plugin_t, get_name, char*,
        return "tnccs-dynamic";
 }
 
+METHOD(plugin_t, get_features, int,
+       tnccs_dynamic_plugin_t *this, plugin_feature_t *features[])
+{
+       static plugin_feature_t f[] = {
+               PLUGIN_CALLBACK(tnccs_method_register, tnccs_dynamic_create),
+                       PLUGIN_PROVIDE(CUSTOM, "tnccs-dynamic"),
+                               PLUGIN_DEPENDS(CUSTOM, "tnccs-1.1"),
+                               PLUGIN_DEPENDS(CUSTOM, "tnccs-2.0"),
+                               PLUGIN_DEPENDS(EAP_SERVER, EAP_TNC),
+                               PLUGIN_DEPENDS(EAP_PEER, EAP_TNC),
+       };
+       *features = f;
+       return countof(f);
+}
+
 METHOD(plugin_t, destroy, void,
        tnccs_dynamic_plugin_t *this)
 {
-       charon->tnccs->remove_method(charon->tnccs,
-                                                               (tnccs_constructor_t)tnccs_dynamic_create);
        free(this);
 }
 
@@ -42,14 +55,11 @@ plugin_t *tnccs_dynamic_plugin_create()
        INIT(this,
                .plugin = {
                        .get_name = _get_name,
-                       .reload = (void*)return_false,
+                       .get_features = _get_features,
                        .destroy = _destroy,
                },
        );
 
-       charon->tnccs->add_method(charon->tnccs, TNCCS_DYNAMIC,
-                                                        (tnccs_constructor_t)tnccs_dynamic_create);
-
        return &this->plugin;
 }
 
index 575b850..5a6d5a4 100644 (file)
  * for more details.
  */
 
+#define USE_TNC
+
 #include "tnccs.h"
 
+#include <daemon.h>
+
+
 ENUM(tnccs_type_names, TNCCS_UNKNOWN, TNCCS_2_0,
        "unknown TNCCS",
        "TNCCS 1.1",
        "TNCCS SOH",
        "TNCCS 2.0",
 );
+
+/**
+ * See header
+ */
+bool tnccs_method_register(plugin_t *plugin, plugin_feature_t *feature,
+                                                  bool reg, void *data)
+{
+       if (reg)
+       {
+               if (feature->type == FEATURE_CUSTOM)
+               {
+                       tnccs_type_t type = TNCCS_UNKNOWN;
+
+                       if (streq(feature->arg.custom, "tnccs-2.0"))
+                       {
+                               type = TNCCS_2_0;
+                       }
+                       else if (streq(feature->arg.custom, "tnccs-1.1"))
+                       {
+                               type = TNCCS_1_1;
+                       }
+                       else if (streq(feature->arg.custom, "tnccs-dynamic"))
+                       {
+                               type = TNCCS_DYNAMIC;
+                       }
+                       else
+                       {
+                               return FALSE;
+                       }
+                       charon->tnccs->add_method(charon->tnccs, type,
+                                                                        (tnccs_constructor_t)data);
+               }
+       }
+       else
+       {
+               charon->tnccs->remove_method(charon->tnccs, (tnccs_constructor_t)data);
+       }
+       return TRUE;
+}
index dc8a422..7145c6a 100644 (file)
 #ifndef TNCCS_H_
 #define TNCCS_H_
 
+typedef struct tnccs_t tnccs_t;
+typedef enum tnccs_type_t tnccs_type_t;
+
 #include <tncif.h>
 #include <tncifimc.h>
 #include <tncifimv.h>
 
 #include <library.h>
-
-typedef enum tnccs_type_t tnccs_type_t;
+#include <plugins/plugin.h>
 
 /**
  * Type of TNC Client/Server protocol
@@ -48,15 +50,27 @@ enum tnccs_type_t {
  */
 extern enum_name_t *tnccs_type_names;
 
-typedef struct tnccs_t tnccs_t;
-
 /**
  * Constructor definition for a pluggable TNCCS protocol implementation.
  *
  * @param is_server            TRUE if TNC Server, FALSE if TNC Client
  * @return                             implementation of the tnccs_t interface
  */
-typedef tnccs_t* (*tnccs_constructor_t)(bool is_server);
+typedef tnccs_t *(*tnccs_constructor_t)(bool is_server);
+
+/**
+ * Helper function to (un-)register TNCCS methods from plugin features.
+ *
+ * This function is a plugin_feature_callback_t and can be used with the
+ * PLUGIN_CALLBACK macro to register a TNCCS method constructor.
+ *
+ * @param plugin               plugin registering the TNCCS method constructor
+ * @param feature              associated plugin feature
+ * @param reg                  TRUE to register, FALSE to unregister.
+ * @param data                 data passed to callback, a tnccs_constructor_t
+ */
+bool tnccs_method_register(plugin_t *plugin, plugin_feature_t *feature,
+                                                  bool reg, void *data);
 
 /**
  * Callback function adding a message to a TNCCS batch