d6165c5920739de35c77fe7063b0a5736d6af148
[oweals/gnunet.git] / src / transport / gnunet-service-transport_plugins.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_plugins.c
23  * @brief plugin management
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_hello.h"
29 #include "gnunet-service-transport_plugins.h"
30
31 /**
32  * Entry in doubly-linked list of all of our plugins.
33  */
34 struct TransportPlugin
35 {
36   /**
37    * This is a doubly-linked list.
38    */
39   struct TransportPlugin *next;
40
41   /**
42    * This is a doubly-linked list.
43    */
44   struct TransportPlugin *prev;
45
46   /**
47    * API of the transport as returned by the plugin's
48    * initialization function.
49    */
50   struct GNUNET_TRANSPORT_PluginFunctions *api;
51
52   /**
53    * Short name for the plugin (i.e. "tcp").
54    */
55   char *short_name;
56
57   /**
58    * Name of the library (i.e. "gnunet_plugin_transport_tcp").
59    */
60   char *lib_name;
61
62   /**
63    * Environment this transport service is using
64    * for this plugin.
65    */
66   struct GNUNET_TRANSPORT_PluginEnvironment env;
67
68 };
69
70 /**
71  * Head of DLL of all loaded plugins.
72  */
73 static struct TransportPlugin *plugins_head;
74
75 /**
76  * Head of DLL of all loaded plugins.
77  */
78 static struct TransportPlugin *plugins_tail;
79
80
81
82 /**
83  * Load and initialize all plugins.  The respective functions will be
84  * invoked by the plugins when the respective events happen.  The
85  * closure will be set to a 'const char*' containing the name of the
86  * plugin that caused the call.
87  *
88  * @param recv_cb function to call when data is received
89  * @param address_cb function to call when our public addresses changed
90  * @param session_end_cb function to call when a session was terminated
91  */
92 void
93 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
94                   GNUNET_TRANSPORT_AddressNotification address_cb,
95                   GNUNET_TRANSPORT_SessionEnd session_end_cb)
96 {
97   struct TransportPlugin *plug;
98   struct TransportPlugin *next;
99   unsigned long long tneigh;
100   char *libname;
101   char *plugs;
102   char *pos;
103
104   if (GNUNET_OK !=
105       GNUNET_CONFIGURATION_get_value_number (GST_cfg, "TRANSPORT",
106                                              "NEIGHBOUR_LIMIT", &tneigh))
107   {
108     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
109                 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
110     return;
111   }
112   if (GNUNET_OK !=
113       GNUNET_CONFIGURATION_get_value_string (GST_cfg, "TRANSPORT", "PLUGINS",
114                                              &plugs))
115     return;
116   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting transport plugins `%s'\n"),
117               plugs);
118   for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
119   {
120     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' transport plugin\n"),
121                 pos);
122     GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos);
123     plug = GNUNET_malloc (sizeof (struct TransportPlugin));
124     plug->short_name = GNUNET_strdup (pos);
125     plug->lib_name = libname;
126     plug->env.cfg = GST_cfg;
127     plug->env.my_identity = &GST_my_identity;
128     plug->env.get_our_hello = &GST_hello_get;
129     plug->env.cls = plug->short_name;
130     plug->env.receive = recv_cb;
131     plug->env.notify_address = address_cb;
132     plug->env.session_end = session_end_cb;
133     plug->env.max_connections = tneigh;
134     plug->env.stats = GST_stats;
135     GNUNET_CONTAINER_DLL_insert (plugins_head, plugins_tail, plug);
136   }
137   GNUNET_free (plugs);
138   next = plugins_head;
139   while (next != NULL)
140   {
141     plug = next;
142     next = plug->next;
143     plug->api = GNUNET_PLUGIN_load (libname, &plug->env);
144     if (plug->api == NULL)
145     {
146       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147                   _("Failed to load transport plugin for `%s'\n"), pos);
148       GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
149       GNUNET_free (plug->short_name);
150       GNUNET_free (plug->lib_name);
151       GNUNET_free (plug);
152     }
153   }
154 }
155
156
157 /**
158  * Unload all plugins
159  */
160 void
161 GST_plugins_unload ()
162 {
163   struct TransportPlugin *plug;
164
165   while (NULL != (plug = plugins_head))
166   {
167     GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
168     GNUNET_free (plug->lib_name);
169     GNUNET_free (plug->short_name);
170     GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
171     GNUNET_free (plug);
172   }
173 }
174
175
176 /**
177  * Obtain the plugin API based on a plugin name.
178  *
179  * @param name name of the plugin
180  * @return the plugin's API, NULL if the plugin is not loaded
181  */
182 struct GNUNET_TRANSPORT_PluginFunctions *
183 GST_plugins_find (const char *name)
184 {
185   struct TransportPlugin *head = plugins_head;
186
187   while ((head != NULL) && (0 != strcmp (name, head->short_name)))
188     head = head->next;
189   if (NULL == head)
190     return NULL;
191   return head->api;
192 }
193
194
195 /**
196  * Convert a given address to a human-readable format.  Note that the
197  * return value will be overwritten on the next call to this function.
198  *
199  * @param address the address to convert
200  * @return statically allocated (!) human-readable address
201  */
202 const char *
203 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
204 {
205   struct GNUNET_TRANSPORT_PluginFunctions *api;
206
207   if (address == NULL)
208     return "<inbound>";
209   api = GST_plugins_find (address->transport_name);
210   if ((api == NULL) || (address->address_length == 0) || (address->address == NULL))
211     return NULL;
212   return api->address_to_string (NULL, address->address, address->address_length);
213 }
214
215
216 /* end of file gnunet-service-transport_plugins.c */