2 This file is part of GNUnet.
3 Copyright (C) 2010-2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
17 * @file transport/gnunet-service-transport_plugins.c
18 * @brief plugin management
19 * @author Christian Grothoff
22 #include "gnunet-service-transport.h"
23 #include "gnunet-service-transport_hello.h"
24 #include "gnunet-service-transport_ats.h"
25 #include "gnunet-service-transport_plugins.h"
28 * Entry in doubly-linked list of all of our plugins.
30 struct TransportPlugin
33 * This is a doubly-linked list.
35 struct TransportPlugin *next;
38 * This is a doubly-linked list.
40 struct TransportPlugin *prev;
43 * API of the transport as returned by the plugin's
44 * initialization function.
46 struct GNUNET_TRANSPORT_PluginFunctions *api;
49 * Short name for the plugin (i.e. "tcp").
54 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
59 * Environment this transport service is using
62 struct GNUNET_TRANSPORT_PluginEnvironment env;
67 * Head of DLL of all loaded plugins.
69 static struct TransportPlugin *plugins_head;
72 * Head of DLL of all loaded plugins.
74 static struct TransportPlugin *plugins_tail;
78 * Function that will be called to update metrics for an address
81 * @param address address to update metrics for
82 * @param session the session
83 * @param distance new distance
86 plugin_env_update_distance (void *cls,
87 const struct GNUNET_HELLO_Address *address,
90 GST_ats_update_distance (address,
96 * Function that will be called to figure if an address is an loopback,
97 * LAN, WAN etc. address
100 * @param addr binary address
101 * @param addrlen length of the @a addr
102 * @return type of the network @a addr belongs to
104 static enum GNUNET_ATS_Network_Type
105 plugin_env_address_to_type (void *cls,
106 const struct sockaddr *addr,
112 return GNUNET_ATS_NET_UNSPECIFIED;
114 return GNUNET_ATS_scanner_address_get_type (GST_is,
121 * Load and initialize all plugins. The respective functions will be
122 * invoked by the plugins when the respective events happen. The
123 * closure will be set to a 'const char*' containing the name of the
124 * plugin that caused the call.
126 * @param recv_cb function to call when data is received
127 * @param address_cb function to call when our public addresses changed
128 * @param session_start_cb function to call when a session was created
129 * @param session_end_cb function to call when a session was terminated
130 * @param address_type_cb function to call when a address type is requested
133 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
134 GNUNET_TRANSPORT_AddressNotification address_cb,
135 GNUNET_TRANSPORT_SessionStart session_start_cb,
136 GNUNET_TRANSPORT_SessionEnd session_end_cb)
138 struct TransportPlugin *plug;
139 struct TransportPlugin *next;
140 unsigned long long tneigh;
147 GNUNET_CONFIGURATION_get_value_number (GST_cfg,
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
157 GNUNET_CONFIGURATION_get_value_string (GST_cfg,
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
163 _("Starting transport plugins `%s'\n"),
165 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
167 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168 _("Loading `%s' transport plugin\n"),
170 GNUNET_asprintf (&libname,
171 "libgnunet_plugin_transport_%s",
173 plug = GNUNET_new (struct TransportPlugin);
174 plug->short_name = GNUNET_strdup (pos);
175 plug->lib_name = libname;
176 plug->env.cfg = GST_cfg;
177 plug->env.my_identity = &GST_my_identity;
178 plug->env.get_our_hello = &GST_hello_get;
179 plug->env.cls = plug->short_name;
180 plug->env.receive = recv_cb;
181 plug->env.notify_address = address_cb;
182 plug->env.session_start = session_start_cb;
183 plug->env.session_end = session_end_cb;
184 plug->env.get_address_type = &plugin_env_address_to_type;
185 plug->env.update_address_distance = &plugin_env_update_distance;
186 plug->env.max_connections = tneigh;
187 plug->env.stats = GST_stats;
188 GNUNET_CONTAINER_DLL_insert (plugins_head,
198 plug->api = GNUNET_PLUGIN_load (plug->lib_name,
200 if (NULL == plug->api)
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 _("Failed to load transport plugin for `%s'\n"),
205 GNUNET_CONTAINER_DLL_remove (plugins_head,
208 GNUNET_free (plug->short_name);
209 GNUNET_free (plug->lib_name);
214 if (NULL == plug->api->address_pretty_printer)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Missing function `%s' in transport plugin for `%s'\n"),
219 "address_pretty_printer",
222 if (NULL == plug->api->address_to_string)
225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226 _("Missing function `%s' in transport plugin for `%s'\n"),
230 if (NULL == plug->api->string_to_address)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 _("Missing function `%s' in transport plugin for `%s'\n"),
238 if (NULL == plug->api->check_address)
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 _("Missing function `%s' in transport plugin for `%s'\n"),
246 if (NULL == plug->api->get_session)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Missing function `%s' in transport plugin for `%s'\n"),
254 if (NULL == plug->api->get_network)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 _("Missing function `%s' in transport plugin for `%s'\n"),
262 if (NULL == plug->api->send)
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 _("Missing function `%s' in transport plugin for `%s'\n"),
270 if (NULL == plug->api->disconnect_peer)
273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274 _("Missing function `%s' in transport plugin for `%s'\n"),
278 if (NULL == plug->api->disconnect_session)
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 _("Missing function `%s' in transport plugin for `%s'\n"),
283 "disconnect_session",
286 if (NULL == plug->api->query_keepalive_factor)
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 _("Missing function `%s' in transport plugin for `%s'\n"),
291 "query_keepalive_factor",
294 if (NULL == plug->api->update_session_timeout)
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298 _("Missing function `%s' in transport plugin for `%s'\n"),
299 "update_session_timeout",
302 if (GNUNET_YES == fail)
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305 _("Did not load plugin `%s' due to missing functions\n"),
307 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
308 GNUNET_CONTAINER_DLL_remove (plugins_head,
311 GNUNET_free (plug->short_name);
312 GNUNET_free (plug->lib_name);
323 GST_plugins_unload ()
325 struct TransportPlugin *plug;
327 while (NULL != (plug = plugins_head))
329 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
330 GNUNET_free (plug->lib_name);
331 GNUNET_free (plug->short_name);
332 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
339 * Obtain the plugin API based on a plugin name.
341 * @param name name of the plugin
342 * @return the plugin's API, NULL if the plugin is not loaded
344 struct GNUNET_TRANSPORT_PluginFunctions *
345 GST_plugins_find (const char *name)
347 struct TransportPlugin *pos;
349 for (pos = plugins_head; NULL != pos; pos = pos->next)
350 if (0 == strcmp (name, pos->short_name))
359 * Obtain the plugin API based on a the stripped plugin name after the underscore.
361 * Example: GST_plugins_printer_find (http_client) will return all plugins
362 * starting with the prefix "http":
363 * http_client or server if loaded
365 * @param name name of the plugin
366 * @return the plugin's API, NULL if the plugin is not loaded
368 struct GNUNET_TRANSPORT_PluginFunctions *
369 GST_plugins_printer_find (const char *name)
371 struct TransportPlugin *pos;
372 char *stripped = GNUNET_strdup (name);
373 char *sep = strchr (stripped, '_');
377 for (pos = plugins_head; NULL != pos; pos = pos->next)
378 if (pos->short_name == strstr (pos->short_name, stripped))
380 GNUNET_free (stripped);
388 * Convert a given address to a human-readable format. Note that the
389 * return value will be overwritten on the next call to this function.
391 * @param address the address to convert
392 * @return statically allocated (!) human-readable address
395 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
397 struct GNUNET_TRANSPORT_PluginFunctions *api;
398 static char unable_to_show[1024];
399 static const char *s;
403 if (0 == address->address_length)
404 return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
405 api = GST_plugins_printer_find (address->transport_name);
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Failed to find transport plugin `%s'\n",
410 address->transport_name);
411 return "<plugin unknown>";
413 if (0 == address->address_length)
415 GNUNET_snprintf (unable_to_show,
416 sizeof (unable_to_show),
417 "<unable to stringify %u-byte long address of %s transport>",
418 (unsigned int) address->address_length,
419 address->transport_name);
420 return unable_to_show;
422 return (NULL != (s = api->address_to_string (NULL,
424 address->address_length))
431 * Register callback with all plugins to monitor their status.
433 * @param cb callback to register, NULL to unsubscribe
434 * @param cb_cls closure for @a cb
437 GST_plugins_monitor_subscribe (GNUNET_TRANSPORT_SessionInfoCallback cb,
440 struct TransportPlugin *pos;
442 for (pos = plugins_head; NULL != pos; pos = pos->next)
443 if (NULL == pos->api->setup_monitor)
446 pos->api->setup_monitor (pos->api->cls,
452 /* end of file gnunet-service-transport_plugins.c */