-fixing some memory leaks from #3667, also reindentation and code cleanup
[oweals/gnunet.git] / src / transport / gnunet-service-transport_plugins.c
1 /*
2   This file is part of GNUnet.
3   Copyright (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_ats.h"
30 #include "gnunet-service-transport_plugins.h"
31
32 /**
33  * Entry in doubly-linked list of all of our plugins.
34  */
35 struct TransportPlugin
36 {
37   /**
38    * This is a doubly-linked list.
39    */
40   struct TransportPlugin *next;
41
42   /**
43    * This is a doubly-linked list.
44    */
45   struct TransportPlugin *prev;
46
47   /**
48    * API of the transport as returned by the plugin's
49    * initialization function.
50    */
51   struct GNUNET_TRANSPORT_PluginFunctions *api;
52
53   /**
54    * Short name for the plugin (i.e. "tcp").
55    */
56   char *short_name;
57
58   /**
59    * Name of the library (i.e. "gnunet_plugin_transport_tcp").
60    */
61   char *lib_name;
62
63   /**
64    * Environment this transport service is using
65    * for this plugin.
66    */
67   struct GNUNET_TRANSPORT_PluginEnvironment env;
68
69 };
70
71 /**
72  * Head of DLL of all loaded plugins.
73  */
74 static struct TransportPlugin *plugins_head;
75
76 /**
77  * Head of DLL of all loaded plugins.
78  */
79 static struct TransportPlugin *plugins_tail;
80
81
82 /**
83  * Function that will be called to update metrics for an address
84  *
85  * @param cls closure
86  * @param address address to update metrics for
87  * @param session the session
88  * @param ats the ats information to update
89  * @param ats_count the number of @a ats elements
90  */
91 static void
92 plugin_env_update_metrics (void *cls,
93                            const struct GNUNET_HELLO_Address *address,
94                            struct Session *session,
95                            const struct GNUNET_ATS_Information *ats,
96                            uint32_t ats_count)
97 {
98   GST_ats_update_metrics (address,
99                           session,
100                           ats, ats_count);
101 }
102
103
104 /**
105  * Function that will be called to figure if an address is an loopback,
106  * LAN, WAN etc. address
107  *
108  * @param cls closure
109  * @param addr binary address
110  * @param addrlen length of the @a addr
111  * @return type of the network @a addr belongs to
112  */
113 static enum GNUNET_ATS_Network_Type
114 plugin_env_address_to_type (void *cls,
115                             const struct sockaddr *addr,
116                             size_t addrlen)
117 {
118   if (NULL == GST_is)
119   {
120     GNUNET_break(0);
121     return GNUNET_ATS_NET_UNSPECIFIED;
122   }
123   return GNUNET_ATS_scanner_address_get_type (GST_is,
124                                               addr,
125                                               addrlen);
126 }
127
128
129 /**
130  * Load and initialize all plugins.  The respective functions will be
131  * invoked by the plugins when the respective events happen.  The
132  * closure will be set to a 'const char*' containing the name of the
133  * plugin that caused the call.
134  *
135  * @param recv_cb function to call when data is received
136  * @param address_cb function to call when our public addresses changed
137  * @param session_start_cb function to call when a session was created
138  * @param session_end_cb function to call when a session was terminated
139  * @param address_type_cb function to call when a address type is requested
140  */
141 void
142 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
143                   GNUNET_TRANSPORT_AddressNotification address_cb,
144                   GNUNET_TRANSPORT_SessionStart session_start_cb,
145                   GNUNET_TRANSPORT_SessionEnd session_end_cb)
146 {
147   struct TransportPlugin *plug;
148   struct TransportPlugin *next;
149   unsigned long long tneigh;
150   char *libname;
151   char *plugs;
152   char *pos;
153   int fail;
154
155   if (GNUNET_OK !=
156       GNUNET_CONFIGURATION_get_value_number (GST_cfg,
157                                              "TRANSPORT",
158                                              "NEIGHBOUR_LIMIT",
159                                              &tneigh))
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162                 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
163     return;
164   }
165   if (GNUNET_OK !=
166       GNUNET_CONFIGURATION_get_value_string (GST_cfg,
167                                              "TRANSPORT",
168                                              "PLUGINS",
169                                              &plugs))
170     return;
171   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172               _("Starting transport plugins `%s'\n"),
173               plugs);
174   for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177                 _("Loading `%s' transport plugin\n"),
178                 pos);
179     GNUNET_asprintf (&libname,
180                      "libgnunet_plugin_transport_%s",
181                      pos);
182     plug = GNUNET_new (struct TransportPlugin);
183     plug->short_name = GNUNET_strdup (pos);
184     plug->lib_name = libname;
185     plug->env.cfg = GST_cfg;
186     plug->env.my_identity = &GST_my_identity;
187     plug->env.get_our_hello = &GST_hello_get;
188     plug->env.cls = plug->short_name;
189     plug->env.receive = recv_cb;
190     plug->env.notify_address = address_cb;
191     plug->env.session_start = session_start_cb;
192     plug->env.session_end = session_end_cb;
193     plug->env.get_address_type = &plugin_env_address_to_type;
194     plug->env.update_address_metrics = &plugin_env_update_metrics;
195     plug->env.max_connections = tneigh;
196     plug->env.stats = GST_stats;
197     GNUNET_CONTAINER_DLL_insert (plugins_head,
198                                  plugins_tail,
199                                  plug);
200   }
201   GNUNET_free (plugs);
202   next = plugins_head;
203   while (NULL != next)
204   {
205     plug = next;
206     next = plug->next;
207     plug->api = GNUNET_PLUGIN_load (plug->lib_name,
208                                     &plug->env);
209     if (NULL == plug->api)
210     {
211       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                   _("Failed to load transport plugin for `%s'\n"),
213                   plug->lib_name);
214       GNUNET_CONTAINER_DLL_remove (plugins_head,
215                                    plugins_tail,
216                                    plug);
217       GNUNET_free (plug->short_name);
218       GNUNET_free (plug->lib_name);
219       GNUNET_free (plug);
220       continue;
221     }
222     fail = GNUNET_NO;
223     if (NULL == plug->api->address_pretty_printer)
224     {
225         fail = GNUNET_YES;
226       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227                   _("Missing function `%s' in transport plugin for `%s'\n"),
228                   "address_pretty_printer",
229                   plug->lib_name);
230     }
231     if (NULL == plug->api->address_to_string)
232     {
233         fail = GNUNET_YES;
234       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                   _("Missing function `%s' in transport plugin for `%s'\n"),
236                   "address_to_string",
237                   plug->lib_name);
238     }
239     if (NULL == plug->api->string_to_address)
240     {
241         fail = GNUNET_YES;
242       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243                   _("Missing function `%s' in transport plugin for `%s'\n"),
244                   "string_to_address",
245                   plug->lib_name);
246     }
247     if (NULL == plug->api->check_address)
248     {
249       fail = GNUNET_YES;
250       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251                   _("Missing function `%s' in transport plugin for `%s'\n"),
252                   "check_address",
253                   plug->lib_name);
254     }
255     if (NULL == plug->api->get_session)
256     {
257       fail = GNUNET_YES;
258       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259                   _("Missing function `%s' in transport plugin for `%s'\n"),
260                   "get_session",
261                   plug->lib_name);
262     }
263     if (NULL == plug->api->get_network)
264     {
265       fail = GNUNET_YES;
266       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267                   _("Missing function `%s' in transport plugin for `%s'\n"),
268                   "get_network",
269                   plug->lib_name);
270     }
271     if (NULL == plug->api->send)
272     {
273       fail = GNUNET_YES;
274       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275                   _("Missing function `%s' in transport plugin for `%s'\n"),
276                   "send",
277                   plug->lib_name);
278     }
279     if (NULL == plug->api->disconnect_peer)
280     {
281         fail = GNUNET_YES;
282       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283                   _("Missing function `%s' in transport plugin for `%s'\n"),
284                   "disconnect_peer",
285                   plug->lib_name);
286     }
287     if (NULL == plug->api->disconnect_session)
288     {
289         fail = GNUNET_YES;
290       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291                   _("Missing function `%s' in transport plugin for `%s'\n"),
292                   "disconnect_session",
293                   plug->lib_name);
294     }
295     if (NULL == plug->api->query_keepalive_factor)
296     {
297       fail = GNUNET_YES;
298       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299                   _("Missing function `%s' in transport plugin for `%s'\n"),
300                   "query_keepalive_factor",
301                   plug->lib_name);
302     }
303     if (NULL == plug->api->update_session_timeout)
304     {
305         fail = GNUNET_YES;
306       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                   _("Missing function `%s' in transport plugin for `%s'\n"),
308                   "update_session_timeout",
309                   plug->lib_name);
310     }
311     if (GNUNET_YES == fail)
312     {
313       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314                   _("Did not load plugin `%s' due to missing functions\n"),
315                   plug->lib_name);
316       GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
317       GNUNET_CONTAINER_DLL_remove (plugins_head,
318                                    plugins_tail,
319                                    plug);
320       GNUNET_free (plug->short_name);
321       GNUNET_free (plug->lib_name);
322       GNUNET_free (plug);
323     }
324   }
325 }
326
327
328 /**
329  * Unload all plugins
330  */
331 void
332 GST_plugins_unload ()
333 {
334   struct TransportPlugin *plug;
335
336   while (NULL != (plug = plugins_head))
337   {
338     GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
339     GNUNET_free (plug->lib_name);
340     GNUNET_free (plug->short_name);
341     GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
342     GNUNET_free (plug);
343   }
344 }
345
346
347 /**
348  * Obtain the plugin API based on a plugin name.
349  *
350  * @param name name of the plugin
351  * @return the plugin's API, NULL if the plugin is not loaded
352  */
353 struct GNUNET_TRANSPORT_PluginFunctions *
354 GST_plugins_find (const char *name)
355 {
356   struct TransportPlugin *pos;
357
358   for (pos = plugins_head; NULL != pos; pos = pos->next)
359     if (0 == strcmp (name, pos->short_name))
360       break;
361   if (NULL == pos)
362     return NULL;
363   return pos->api;
364 }
365
366
367 /**
368  * Obtain the plugin API based on a the stripped plugin name after the underscore.
369  *
370  * Example: GST_plugins_printer_find (http_client) will return all plugins
371  * starting with the prefix "http":
372  * http_client or server if loaded
373  *
374  * @param name name of the plugin
375  * @return the plugin's API, NULL if the plugin is not loaded
376  */
377 struct GNUNET_TRANSPORT_PluginFunctions *
378 GST_plugins_printer_find (const char *name)
379 {
380   struct TransportPlugin *pos;
381   char *stripped = GNUNET_strdup (name);
382   char *sep = strchr (stripped, '_');
383
384   if (NULL != sep)
385     sep[0] = '\0';
386   for (pos = plugins_head; NULL != pos; pos = pos->next)
387     if (pos->short_name == strstr (pos->short_name, stripped))
388         break;
389   GNUNET_free (stripped);
390   if (NULL == pos)
391     return NULL;
392   return pos->api;
393 }
394
395
396 /**
397  * Convert a given address to a human-readable format.  Note that the
398  * return value will be overwritten on the next call to this function.
399  *
400  * @param address the address to convert
401  * @return statically allocated (!) human-readable address
402  */
403 const char *
404 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
405 {
406   struct GNUNET_TRANSPORT_PluginFunctions *api;
407   static char unable_to_show[1024];
408   static const char *s;
409
410   if (NULL == address)
411     return "<NULL>";
412   if (0 == address->address_length)
413     return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
414   api = GST_plugins_printer_find (address->transport_name);
415   if (NULL == api)
416     return "<plugin unknown>";
417   if (0 == address->address_length)
418   {
419     GNUNET_snprintf (unable_to_show, sizeof (unable_to_show),
420                      "<unable to stringify %u-byte long address of %s transport>",
421                      (unsigned int) address->address_length,
422                      address->transport_name);
423     return unable_to_show;
424   }
425   return (NULL != (s = api->address_to_string (NULL, address->address,
426                                  address->address_length)) ? s : "<invalid>");
427 }
428
429
430 /**
431  * Register callback with all plugins to monitor their status.
432  *
433  * @param cb callback to register, NULL to unsubscribe
434  * @param cb_cls closure for @a cb
435  */
436 void
437 GST_plugins_monitor_subscribe (GNUNET_TRANSPORT_SessionInfoCallback cb,
438                                void *cb_cls)
439 {
440   struct TransportPlugin *pos;
441
442   for (pos = plugins_head; NULL != pos; pos = pos->next)
443     if (NULL == pos->api->setup_monitor)
444       GNUNET_break (0);
445     else
446       pos->api->setup_monitor (pos->api->cls,
447                                cb, cb_cls);
448 }
449
450
451 /* end of file gnunet-service-transport_plugins.c */