2 This file is part of GNUnet.
3 (C) 2010,2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/gnunet-service-transport_plugins.c
23 * @brief plugin management
24 * @author Christian Grothoff
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_hello.h"
29 #include "gnunet-service-transport_plugins.h"
32 * Entry in doubly-linked list of all of our plugins.
34 struct TransportPlugin
37 * This is a doubly-linked list.
39 struct TransportPlugin *next;
42 * This is a doubly-linked list.
44 struct TransportPlugin *prev;
47 * API of the transport as returned by the plugin's
48 * initialization function.
50 struct GNUNET_TRANSPORT_PluginFunctions *api;
53 * Short name for the plugin (i.e. "tcp").
58 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
63 * Environment this transport service is using
66 struct GNUNET_TRANSPORT_PluginEnvironment env;
71 * Head of DLL of all loaded plugins.
73 static struct TransportPlugin *plugins_head;
76 * Head of DLL of all loaded plugins.
78 static struct TransportPlugin *plugins_tail;
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.
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
96 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
97 GNUNET_TRANSPORT_AddressNotification address_cb,
98 GNUNET_TRANSPORT_SessionStart session_start_cb,
99 GNUNET_TRANSPORT_SessionEnd session_end_cb,
100 GNUNET_TRANSPORT_AddressToType address_type_cb,
101 GNUNET_TRANSPORT_UpdateAddressMetrics metric_update_cb)
103 struct TransportPlugin *plug;
104 struct TransportPlugin *next;
105 unsigned long long tneigh;
112 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "TRANSPORT",
113 "NEIGHBOUR_LIMIT", &tneigh))
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
120 GNUNET_CONFIGURATION_get_value_string (GST_cfg, "TRANSPORT", "PLUGINS",
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting transport plugins `%s'\n"),
125 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
127 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' transport plugin\n"),
129 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos);
130 plug = GNUNET_malloc (sizeof (struct TransportPlugin));
131 plug->short_name = GNUNET_strdup (pos);
132 plug->lib_name = libname;
133 plug->env.cfg = GST_cfg;
134 plug->env.my_identity = &GST_my_identity;
135 plug->env.get_our_hello = &GST_hello_get;
136 plug->env.cls = plug->short_name;
137 plug->env.receive = recv_cb;
138 plug->env.notify_address = address_cb;
139 plug->env.session_start = session_start_cb;
140 plug->env.session_end = session_end_cb;
141 plug->env.get_address_type = address_type_cb;
142 plug->env.update_address_metrics = metric_update_cb;
143 plug->env.max_connections = tneigh;
144 plug->env.stats = GST_stats;
145 GNUNET_CONTAINER_DLL_insert (plugins_head, plugins_tail, plug);
153 plug->api = GNUNET_PLUGIN_load (plug->lib_name, &plug->env);
154 if (plug->api == NULL)
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 _("Failed to load transport plugin for `%s'\n"),
159 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
160 GNUNET_free (plug->short_name);
161 GNUNET_free (plug->lib_name);
166 if (NULL == plug->api->address_pretty_printer)
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 _("Missing function `%s' in transport plugin for `%s'\n"),
171 "address_pretty_printer",
174 if (NULL == plug->api->address_to_string)
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 _("Missing function `%s' in transport plugin for `%s'\n"),
182 if (NULL == plug->api->string_to_address)
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _("Missing function `%s' in transport plugin for `%s'\n"),
190 if (NULL == plug->api->check_address)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 _("Missing function `%s' in transport plugin for `%s'\n"),
198 if (NULL == plug->api->get_session)
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 _("Missing function `%s' in transport plugin for `%s'\n"),
206 if (NULL == plug->api->get_network)
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 _("Missing function `%s' in transport plugin for `%s'\n"),
214 if (NULL == plug->api->send)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Missing function `%s' in transport plugin for `%s'\n"),
222 if (NULL == plug->api->disconnect)
225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226 _("Missing function `%s' in transport plugin for `%s'\n"),
230 if (GNUNET_YES == fail)
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 _("Did not load plugin `%s' due to missing functions\n"),
235 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
236 GNUNET_free (plug->short_name);
237 GNUNET_free (plug->lib_name);
248 GST_plugins_unload ()
250 struct TransportPlugin *plug;
252 while (NULL != (plug = plugins_head))
254 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
255 GNUNET_free (plug->lib_name);
256 GNUNET_free (plug->short_name);
257 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
264 * Obtain the plugin API based on a plugin name.
266 * @param name name of the plugin
267 * @return the plugin's API, NULL if the plugin is not loaded
269 struct GNUNET_TRANSPORT_PluginFunctions *
270 GST_plugins_find (const char *name)
272 struct TransportPlugin *head = plugins_head;
274 while ((head != NULL) && (0 != strcmp (name, head->short_name)))
283 * Obtain the plugin API based on a the stripped plugin name after the underscore.
285 * Example: GST_plugins_printer_find (http_client) will return all plugins
286 * starting with the prefix "http":
287 * http_client or server if loaded
289 * @param name name of the plugin
290 * @return the plugin's API, NULL if the plugin is not loaded
292 struct GNUNET_TRANSPORT_PluginFunctions *
293 GST_plugins_printer_find (const char *name)
295 struct TransportPlugin *head = plugins_head;
297 char *stripped = GNUNET_strdup (name);
298 char *sep = strchr (stripped, '_');
304 if (head->short_name == strstr (head->short_name, stripped))
308 GNUNET_free (stripped);
316 * Convert a given address to a human-readable format. Note that the
317 * return value will be overwritten on the next call to this function.
319 * @param address the address to convert
320 * @return statically allocated (!) human-readable address
323 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
325 struct GNUNET_TRANSPORT_PluginFunctions *api;
326 static char unable_to_show[1024];
331 GNUNET_break (0); /* a HELLO address cannot be NULL */
334 if (0 == address->address_length)
335 return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
336 api = GST_plugins_printer_find (address->transport_name);
338 return "<plugin unknown>";
339 if (0 == address->address_length)
341 GNUNET_snprintf (unable_to_show, sizeof (unable_to_show),
342 "<unable to stringify %u-byte long address of %s transport>",
343 (unsigned int) address->address_length,
344 address->transport_name);
345 return unable_to_show;
347 return (NULL != (s = api->address_to_string (NULL, address->address,
348 address->address_length)) ? s : "<invalid>");
352 /* end of file gnunet-service-transport_plugins.c */