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