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