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