implementing plugins subsystem
authorChristian Grothoff <christian@grothoff.org>
Thu, 4 Aug 2011 19:29:40 +0000 (19:29 +0000)
committerChristian Grothoff <christian@grothoff.org>
Thu, 4 Aug 2011 19:29:40 +0000 (19:29 +0000)
src/include/gnunet_transport_plugin.h
src/transport/gnunet-service-transport-new.c
src/transport/gnunet-service-transport.c
src/transport/gnunet-service-transport_plugins.c
src/transport/plugin_transport_wlan.c

index 7704d2b0c3713286617503ed91e65674b7446917..76b8512552d9b8a1eed1bd44fa1aca2f68714cd4 100644 (file)
@@ -176,6 +176,12 @@ typedef void (*GNUNET_TRANSPORT_CostReport) (void *cls,
                                             struct GNUNET_TRANSPORT_ATS_Information * cost);
 
 
+/**
+ * Function that returns a HELLO message.
+ */ 
+typedef const struct GNUNET_MessageHeader * (*GNUNET_TRANSPORT_GetHelloCallback)(void);
+
+
 /**
  * The transport service will pass a pointer to a struct
  * of this type as the first and only argument to the
@@ -193,12 +199,6 @@ struct GNUNET_TRANSPORT_PluginEnvironment
    */
   const struct GNUNET_PeerIdentity *my_identity;
 
-  /**
-   * Pointer (!) to our HELLO message.  Note that the address
-   * referred to "*our_hello" might change over time.
-   */
-  struct GNUNET_HELLO_Message *const*our_hello;
-
   /**
    * Closure for the various callbacks.
    */
@@ -215,6 +215,12 @@ struct GNUNET_TRANSPORT_PluginEnvironment
    */
   GNUNET_TRANSPORT_PluginReceiveCallback receive;
 
+
+  /**
+   * Function that returns our HELLO.
+   */
+  GNUNET_TRANSPORT_GetHelloCallback get_our_hello;
+
   /**
    * Function that must be called by each plugin to notify the
    * transport service about the addresses under which the transport
index bade1cf5abbd35930f4220af7fdc6d7a34aeaf6a..2ba280d193fac8a6f03c0c5ce3ae0f52cbfb234c 100644 (file)
@@ -88,6 +88,32 @@ process_hello_update (void *cls,
 }
 
 
+/**
+ * Function that will be called for each address the transport
+ * is aware that it might be reachable under.  Update our HELLO.
+ *
+ * @param cls name of the plugin (const char*)
+ * @param add_remove should the address added (YES) or removed (NO) from the
+ *                   set of valid addresses?
+ * @param addr one of the addresses of the host
+ *        the specific address format depends on the transport
+ * @param addrlen length of the address
+ */
+static void 
+plugin_env_address_change_notification (void *cls,
+                                       int add_remove,
+                                       const void *addr,
+                                       size_t addrlen)
+{
+  const char *plugin_name = cls;
+
+  GST_hello_modify_addresses (add_remove,
+                             plugin_name,
+                             addr,
+                             addrlen);
+}
+
+
 /**
  * Function called when the service shuts down.  Unloads our plugins
  * and cancels pending validations.
@@ -99,6 +125,8 @@ static void
 shutdown_task (void *cls, 
               const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+
+  GST_plugins_unload ();
   GST_hello_stop ();
 
   if (GST_peerinfo != NULL)
@@ -174,7 +202,14 @@ run (void *cls,
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
+  
+  /* start subsystems */
   GST_hello_start (&process_hello_update, NULL);
+  GST_plugins_load (NULL,  // FIXME...
+                   &plugin_env_address_change_notification, 
+                   NULL, // FIXME...
+                   NULL, // FIXME...
+                   NULL); // FIXME...
 }
 
 
index 9658d119b0e6ea2575303073ed9a8592bbecd91b..269d21f63d73a0c7d17d63833d55a2155f14788b 100644 (file)
@@ -6131,6 +6131,13 @@ handle_address_iterate (void *cls,
 }
 
 
+static const struct GNUNET_MessageHeader *
+do_get_our_hello ()
+{
+  return (const struct GNUNET_MessageHeader*) our_hello;
+}
+
+
 /**
  * Setup the environment for this plugin.
  */
@@ -6139,7 +6146,7 @@ create_environment (struct TransportPlugin *plug)
 {
   plug->env.cfg = cfg;
   plug->env.my_identity = &my_identity;
-  plug->env.our_hello = &our_hello;
+  plug->env.get_our_hello = &do_get_our_hello;
   plug->env.cls = plug;
   plug->env.receive = &plugin_env_receive;
   plug->env.notify_address = &plugin_env_notify_address;
index c4bc030391ede724297f9a1da8bb6db7f9c8d923..bbc8d4183673ba585d7fc49d7c46258f23fec8e9 100644 (file)
@@ -24,6 +24,8 @@
  * @author Christian Grothoff
  */
 #include "platform.h"
+#include "gnunet-service-transport.h"
+#include "gnunet-service-transport_hello.h"
 #include "gnunet-service-transport_plugins.h"
 
 /**
@@ -73,7 +75,7 @@ static struct TransportPlugin *plugins_head;
 /**
  * Head of DLL of all loaded plugins.
  */
-// static struct TransportPlugin *plugins_tail;
+static struct TransportPlugin *plugins_tail;
 
 
 
@@ -96,56 +98,66 @@ GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
                  GNUNET_TRANSPORT_SessionEnd session_end_cb,
                  GNUNET_TRANSPORT_CostReport cost_cb)
 {
-#if 0
   struct TransportPlugin *plug;
+  unsigned long long tneigh;
   char *libname;
-
-  /* load plugins... */
-  no_transports = 1;
-  if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_string (c,
-                                             "TRANSPORT", "PLUGINS", &plugs))
+  char *plugs;
+  char *pos;
+
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_number (GST_cfg,
+                                            "TRANSPORT",
+                                            "NEIGHBOUR_LIMIT",
+                                            &tneigh))
     {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
+      return;
+    }
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_string (GST_cfg,
+                                             "TRANSPORT", "PLUGINS", &plugs))
+    return;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+             _("Starting transport plugins `%s'\n"),
+             plugs);
+  for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
+    {    
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  _("Starting transport plugins `%s'\n"), plugs);
-      pos = strtok (plugs, " ");
-      while (pos != NULL)
-        {
-         
-         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                     _("Loading `%s' transport plugin\n"), name);
-         GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", name);
-         plug = GNUNET_malloc (sizeof (struct TransportPlugin));
-         plug->short_name = GNUNET_strdup (name);
-         plug->lib_name = libname;
-         plug->env.cfg = cfg;
-         plug->env.my_identity = &my_identity;
-         plug->env.our_hello = &our_hello;
-         plug->env.cls = plug->short_name;
-         plug->env.receive = &plugin_env_receive;
-         plug->env.notify_address = &plugin_env_notify_address;
-         plug->env.session_end = &plugin_env_session_end;
-         plug->env.max_connections = max_connect_per_transport;
-         plug->env.stats = stats;
-         plug->next = plugins;
-         plugins = plug;
-         plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
-         if (plug->api == NULL)
-           {
-             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                         _("Failed to load transport plugin for `%s'\n"), name);
-             GNUNET_free (plug->short_name);
-             plugins = plug->next;
-             GNUNET_free (libname);
-             GNUNET_free (plug);
-           }
-          start_transport (server, pos);
-          no_transports = 0;
-          pos = strtok (NULL, " ");
-        }
-      GNUNET_free (plugs);
+                 _("Loading `%s' transport plugin\n"), pos);
+      GNUNET_asprintf (&libname, 
+                      "libgnunet_plugin_transport_%s",
+                      pos);
+      plug = GNUNET_malloc (sizeof (struct TransportPlugin));
+      plug->short_name = GNUNET_strdup (pos);
+      plug->lib_name = libname;
+      plug->env.cfg = GST_cfg;
+      plug->env.my_identity = &GST_my_identity;
+      plug->env.get_our_hello = &GST_hello_get;
+      plug->env.cls = plug->short_name;
+      plug->env.receive = recv_cb;
+      plug->env.notify_address = address_cb;
+      plug->env.session_end = session_end_cb;
+      plug->env.max_connections = tneigh;
+      plug->env.stats = GST_stats;
+      GNUNET_CONTAINER_DLL_insert (plugins_head,
+                                  plugins_tail,
+                                  plug);
+      plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
+      if (plug->api == NULL)
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                     _("Failed to load transport plugin for `%s'\n"), 
+                     pos);
+         GNUNET_CONTAINER_DLL_remove (plugins_head,
+                                      plugins_tail,
+                                      plug);
+         GNUNET_free (plug->short_name);
+         GNUNET_free (plug->lib_name);
+         GNUNET_free (plug);
+       }
     }
-#endif
+  GNUNET_free (plugs);
 }
 
 
@@ -155,26 +167,18 @@ GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
 void
 GST_plugins_unload ()
 {
-#if 0
-  while (NULL != (plug = plugins))
+  struct TransportPlugin *plug;
+
+  while (NULL != (plug = plugins_head))
     {
-      if (plug->address_update_task != GNUNET_SCHEDULER_NO_TASK)
-       {
-         GNUNET_SCHEDULER_cancel (plug->address_update_task);
-         plug->address_update_task = GNUNET_SCHEDULER_NO_TASK;
-       }
       GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
       GNUNET_free (plug->lib_name);
       GNUNET_free (plug->short_name);
-      while (NULL != (al = plug->addresses))
-        {
-          plug->addresses = al->next;
-          GNUNET_free (al);
-        }
-      plugins = plug->next;
+      GNUNET_CONTAINER_DLL_remove (plugins_head,
+                                  plugins_tail,
+                                  plug);
       GNUNET_free (plug);
     }
-#endif
 }
 
 
index d9052206290ff018f4ba78a64e612a4c9d49ef4d..2f32b53c2803ce17a7845e1692eb9cf27f0581c4 100644 (file)
@@ -1353,8 +1353,10 @@ send_hello_beacon(struct Plugin * plugin)
   struct ieee80211_frame * ieeewlanheader;
   struct Radiotap_Send * radioHeader;
   struct GNUNET_MessageHeader * msgheader2;
+  const struct GNUNET_MessageHeader *hello;
 
-  hallo_size = GNUNET_HELLO_size(*(plugin->env->our_hello));
+  hello = plugin->env->get_our_hello ();
+  hallo_size = GNUNET_HELLO_size(hello);
   GNUNET_assert(sizeof(struct WlanHeader) + hallo_size <= WLAN_MTU);
   size = sizeof(struct GNUNET_MessageHeader) + sizeof(struct Radiotap_Send)
       + sizeof(struct ieee80211_frame) + sizeof(struct GNUNET_MessageHeader)
@@ -1370,12 +1372,11 @@ send_hello_beacon(struct Plugin * plugin)
   getWlanHeader(ieeewlanheader, &bc_all_mac, plugin, size);
 
   msgheader2 = (struct GNUNET_MessageHeader*) &ieeewlanheader[1];
-  msgheader2->size = htons(
-      GNUNET_HELLO_size(*(plugin->env->our_hello))
+  msgheader2->size = htons(GNUNET_HELLO_size(hello)
           + sizeof(struct GNUNET_MessageHeader));
 
   msgheader2->type = htons(GNUNET_MESSAGE_TYPE_WLAN_ADVERTISEMENT);
-  memcpy(&msgheader2[1], *plugin->env->our_hello, hallo_size);
+  memcpy(&msgheader2[1], hello, hallo_size);
 
   bytes = GNUNET_DISK_file_write(plugin->server_stdin_handle, msgheader, size);