document API, do not pass unused 'session' argument
[oweals/gnunet.git] / src / transport / gnunet-service-transport_plugins.c
1 /*
2   This file is part of GNUnet.
3   (C) 2010-2014 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  * Load and initialize all plugins.  The respective functions will be
83  * invoked by the plugins when the respective events happen.  The
84  * closure will be set to a 'const char*' containing the name of the
85  * plugin that caused the call.
86  *
87  * @param recv_cb function to call when data is received
88  * @param address_cb function to call when our public addresses changed
89  * @param session_start_cb function to call when a session was created
90  * @param session_end_cb function to call when a session was terminated
91  * @param address_type_cb function to call when a address type is requested
92  * @param metric_update_cb function to call when address metrics change
93  */
94 void
95 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
96                   GNUNET_TRANSPORT_AddressNotification address_cb,
97                   GNUNET_TRANSPORT_SessionStart session_start_cb,
98                   GNUNET_TRANSPORT_SessionEnd session_end_cb,
99                   GNUNET_TRANSPORT_AddressToType address_type_cb,
100                   GNUNET_TRANSPORT_UpdateAddressMetrics metric_update_cb)
101 {
102   struct TransportPlugin *plug;
103   struct TransportPlugin *next;
104   unsigned long long tneigh;
105   char *libname;
106   char *plugs;
107   char *pos;
108   int fail;
109
110   if (GNUNET_OK !=
111       GNUNET_CONFIGURATION_get_value_number (GST_cfg, "TRANSPORT",
112                                              "NEIGHBOUR_LIMIT", &tneigh))
113   {
114     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115                 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
116     return;
117   }
118   if (GNUNET_OK !=
119       GNUNET_CONFIGURATION_get_value_string (GST_cfg, "TRANSPORT", "PLUGINS",
120                                              &plugs))
121     return;
122   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123               _("Starting transport plugins `%s'\n"),
124               plugs);
125   for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
126   {
127     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
128                 _("Loading `%s' transport plugin\n"),
129                 pos);
130     GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos);
131     plug = GNUNET_new (struct TransportPlugin);
132     plug->short_name = GNUNET_strdup (pos);
133     plug->lib_name = libname;
134     plug->env.cfg = GST_cfg;
135     plug->env.my_identity = &GST_my_identity;
136     plug->env.get_our_hello = &GST_hello_get;
137     plug->env.cls = plug->short_name;
138     plug->env.receive = recv_cb;
139     plug->env.notify_address = address_cb;
140     plug->env.session_start = session_start_cb;
141     plug->env.session_end = session_end_cb;
142     plug->env.get_address_type = address_type_cb;
143     plug->env.update_address_metrics = metric_update_cb;
144     plug->env.max_connections = tneigh;
145     plug->env.stats = GST_stats;
146     GNUNET_CONTAINER_DLL_insert (plugins_head, plugins_tail, plug);
147   }
148   GNUNET_free (plugs);
149   next = plugins_head;
150   while (NULL != next)
151   {
152     plug = next;
153     next = plug->next;
154     plug->api = GNUNET_PLUGIN_load (plug->lib_name, &plug->env);
155     if (NULL == plug->api)
156     {
157       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158                   _("Failed to load transport plugin for `%s'\n"),
159                   plug->lib_name);
160       GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
161       GNUNET_free (plug->short_name);
162       GNUNET_free (plug->lib_name);
163       GNUNET_free (plug);
164       continue;
165     }
166     fail = GNUNET_NO;
167     if (NULL == plug->api->address_pretty_printer)
168     {
169         fail = GNUNET_YES;
170       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171                   _("Missing function `%s' in transport plugin for `%s'\n"),
172                   "address_pretty_printer",
173                   plug->lib_name);
174     }
175     if (NULL == plug->api->address_to_string)
176     {
177         fail = GNUNET_YES;
178       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179                   _("Missing function `%s' in transport plugin for `%s'\n"),
180                   "address_to_string",
181                   plug->lib_name);
182     }
183     if (NULL == plug->api->string_to_address)
184     {
185         fail = GNUNET_YES;
186       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187                   _("Missing function `%s' in transport plugin for `%s'\n"),
188                   "string_to_address",
189                   plug->lib_name);
190     }
191     if (NULL == plug->api->check_address)
192     {
193       fail = GNUNET_YES;
194       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195                   _("Missing function `%s' in transport plugin for `%s'\n"),
196                   "check_address",
197                   plug->lib_name);
198     }
199     if (NULL == plug->api->get_session)
200     {
201       fail = GNUNET_YES;
202       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203                   _("Missing function `%s' in transport plugin for `%s'\n"),
204                   "get_session",
205                   plug->lib_name);
206     }
207     if (NULL == plug->api->get_network)
208     {
209       fail = GNUNET_YES;
210       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211                   _("Missing function `%s' in transport plugin for `%s'\n"),
212                   "get_network",
213                   plug->lib_name);
214     }
215     if (NULL == plug->api->send)
216     {
217       fail = GNUNET_YES;
218       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219                   _("Missing function `%s' in transport plugin for `%s'\n"),
220                   "send",
221                   plug->lib_name);
222     }
223     if (NULL == plug->api->disconnect_peer)
224     {
225         fail = GNUNET_YES;
226       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227                   _("Missing function `%s' in transport plugin for `%s'\n"),
228                   "disconnect_peer",
229                   plug->lib_name);
230     }
231     if (NULL == plug->api->disconnect_session)
232     {
233         fail = GNUNET_YES;
234       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                   _("Missing function `%s' in transport plugin for `%s'\n"),
236                   "disconnect_session",
237                   plug->lib_name);
238     }
239     if (NULL == plug->api->query_keepalive_factor)
240     {
241       fail = GNUNET_YES;
242       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243                   _("Missing function `%s' in transport plugin for `%s'\n"),
244                   "query_keepalive_factor",
245                   plug->lib_name);
246     }
247     if (NULL == plug->api->update_session_timeout)
248     {
249         fail = GNUNET_YES;
250       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251                   _("Missing function `%s' in transport plugin for `%s'\n"),
252                   "update_session_timeout",
253                   plug->lib_name);
254     }
255     if (GNUNET_YES == fail)
256     {
257       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258                   _("Did not load plugin `%s' due to missing functions\n"),
259                   plug->lib_name);
260       GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
261       GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
262       GNUNET_free (plug->short_name);
263       GNUNET_free (plug->lib_name);
264       GNUNET_free (plug);
265     }
266   }
267 }
268
269
270 /**
271  * Unload all plugins
272  */
273 void
274 GST_plugins_unload ()
275 {
276   struct TransportPlugin *plug;
277
278   while (NULL != (plug = plugins_head))
279   {
280     GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
281     GNUNET_free (plug->lib_name);
282     GNUNET_free (plug->short_name);
283     GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
284     GNUNET_free (plug);
285   }
286 }
287
288
289 /**
290  * Obtain the plugin API based on a plugin name.
291  *
292  * @param name name of the plugin
293  * @return the plugin's API, NULL if the plugin is not loaded
294  */
295 struct GNUNET_TRANSPORT_PluginFunctions *
296 GST_plugins_find (const char *name)
297 {
298   struct TransportPlugin *pos;
299
300   for (pos = plugins_head; NULL != pos; pos = pos->next)
301     if (0 == strcmp (name, pos->short_name))
302       break;
303   if (NULL == pos)
304     return NULL;
305   return pos->api;
306 }
307
308
309 /**
310  * Obtain the plugin API based on a the stripped plugin name after the underscore.
311  *
312  * Example: GST_plugins_printer_find (http_client) will return all plugins
313  * starting with the prefix "http":
314  * http_client or server if loaded
315  *
316  * @param name name of the plugin
317  * @return the plugin's API, NULL if the plugin is not loaded
318  */
319 struct GNUNET_TRANSPORT_PluginFunctions *
320 GST_plugins_printer_find (const char *name)
321 {
322   struct TransportPlugin *pos;
323   char *stripped = GNUNET_strdup (name);
324   char *sep = strchr (stripped, '_');
325
326   if (NULL != sep)
327     sep[0] = '\0';
328   for (pos = plugins_head; NULL != pos; pos = pos->next)
329     if (pos->short_name == strstr (pos->short_name, stripped))
330         break;
331   GNUNET_free (stripped);
332   if (NULL == pos)
333     return NULL;
334   return pos->api;
335 }
336
337
338 /**
339  * Convert a given address to a human-readable format.  Note that the
340  * return value will be overwritten on the next call to this function.
341  *
342  * @param address the address to convert
343  * @return statically allocated (!) human-readable address
344  */
345 const char *
346 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
347 {
348   struct GNUNET_TRANSPORT_PluginFunctions *api;
349   static char unable_to_show[1024];
350   static const char *s;
351
352   if (NULL == address)
353     return "<NULL>";
354   if (0 == address->address_length)
355     return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
356   api = GST_plugins_printer_find (address->transport_name);
357   if (NULL == api)
358     return "<plugin unknown>";
359   if (0 == address->address_length)
360   {
361     GNUNET_snprintf (unable_to_show, sizeof (unable_to_show),
362                      "<unable to stringify %u-byte long address of %s transport>",
363                      (unsigned int) address->address_length,
364                      address->transport_name);
365     return unable_to_show;
366   }
367   return (NULL != (s = api->address_to_string (NULL, address->address,
368                                  address->address_length)) ? s : "<invalid>");
369 }
370
371
372 /**
373  * Register callback with all plugins to monitor their status.
374  *
375  * @param cb callback to register, NULL to unsubscribe
376  * @param cb_cls closure for @a cb
377  */
378 void
379 GST_plugins_monitor_subscribe (GNUNET_TRANSPORT_SessionInfoCallback cb,
380                                void *cb_cls)
381 {
382   struct TransportPlugin *pos;
383
384   for (pos = plugins_head; NULL != pos; pos = pos->next)
385     if (NULL == pos->api->setup_monitor)
386       GNUNET_break (0);
387     else
388       pos->api->setup_monitor (pos->api->cls,
389                                cb, cb_cls);
390 }
391
392
393 /* end of file gnunet-service-transport_plugins.c */