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 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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_ats.h"
30 #include "gnunet-service-transport_plugins.h"
33 * Entry in doubly-linked list of all of our plugins.
35 struct TransportPlugin
38 * This is a doubly-linked list.
40 struct TransportPlugin *next;
43 * This is a doubly-linked list.
45 struct TransportPlugin *prev;
48 * API of the transport as returned by the plugin's
49 * initialization function.
51 struct GNUNET_TRANSPORT_PluginFunctions *api;
54 * Short name for the plugin (i.e. "tcp").
59 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
64 * Environment this transport service is using
67 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;
82 * Function that will be called to update metrics for an address
85 * @param address address to update metrics for
86 * @param session the session
87 * @param distance new distance
90 plugin_env_update_distance (void *cls,
91 const struct GNUNET_HELLO_Address *address,
94 GST_ats_update_distance (address,
100 * Function that will be called to figure if an address is an loopback,
101 * LAN, WAN etc. address
104 * @param addr binary address
105 * @param addrlen length of the @a addr
106 * @return type of the network @a addr belongs to
108 static enum GNUNET_NetworkType
109 plugin_env_address_to_type (void *cls,
110 const struct sockaddr *addr,
116 return GNUNET_NT_UNSPECIFIED;
118 return GNUNET_NT_scanner_get_type (GST_is,
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.
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
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)
142 struct TransportPlugin *plug;
143 struct TransportPlugin *next;
144 unsigned long long tneigh;
151 GNUNET_CONFIGURATION_get_value_number (GST_cfg,
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 _ ("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
161 GNUNET_CONFIGURATION_get_value_string (GST_cfg,
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
167 _ ("Starting transport plugins `%s'\n"),
169 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172 _ ("Loading `%s' transport plugin\n"),
174 GNUNET_asprintf (&libname,
175 "libgnunet_plugin_transport_%s",
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,
202 plug->api = GNUNET_PLUGIN_load (plug->lib_name,
204 if (NULL == plug->api)
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 _ ("Failed to load transport plugin for `%s'\n"),
209 GNUNET_CONTAINER_DLL_remove (plugins_head,
212 GNUNET_free (plug->short_name);
213 GNUNET_free (plug->lib_name);
218 if (NULL == plug->api->address_pretty_printer)
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 _ ("Missing function `%s' in transport plugin for `%s'\n"),
223 "address_pretty_printer",
226 if (NULL == plug->api->address_to_string)
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 _ ("Missing function `%s' in transport plugin for `%s'\n"),
234 if (NULL == plug->api->string_to_address)
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 _ ("Missing function `%s' in transport plugin for `%s'\n"),
242 if (NULL == plug->api->check_address)
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 _ ("Missing function `%s' in transport plugin for `%s'\n"),
250 if (NULL == plug->api->get_session)
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 _ ("Missing function `%s' in transport plugin for `%s'\n"),
258 if (NULL == plug->api->get_network)
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 _ ("Missing function `%s' in transport plugin for `%s'\n"),
266 if (NULL == plug->api->send)
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 _ ("Missing function `%s' in transport plugin for `%s'\n"),
274 if (NULL == plug->api->disconnect_peer)
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 _ ("Missing function `%s' in transport plugin for `%s'\n"),
282 if (NULL == plug->api->disconnect_session)
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 _ ("Missing function `%s' in transport plugin for `%s'\n"),
287 "disconnect_session",
290 if (NULL == plug->api->query_keepalive_factor)
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _ ("Missing function `%s' in transport plugin for `%s'\n"),
295 "query_keepalive_factor",
298 if (NULL == plug->api->update_session_timeout)
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 _ ("Missing function `%s' in transport plugin for `%s'\n"),
303 "update_session_timeout",
306 if (GNUNET_YES == fail)
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 _ ("Did not load plugin `%s' due to missing functions\n"),
311 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
312 GNUNET_CONTAINER_DLL_remove (plugins_head,
315 GNUNET_free (plug->short_name);
316 GNUNET_free (plug->lib_name);
327 GST_plugins_unload ()
329 struct TransportPlugin *plug;
331 while (NULL != (plug = plugins_head))
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);
343 * Obtain the plugin API based on a plugin name.
345 * @param name name of the plugin
346 * @return the plugin's API, NULL if the plugin is not loaded
348 struct GNUNET_TRANSPORT_PluginFunctions *
349 GST_plugins_find (const char *name)
351 struct TransportPlugin *pos;
353 for (pos = plugins_head; NULL != pos; pos = pos->next)
354 if (0 == strcmp (name, pos->short_name))
363 * Obtain the plugin API based on a the stripped plugin name after the underscore.
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
369 * @param name name of the plugin
370 * @return the plugin's API, NULL if the plugin is not loaded
372 struct GNUNET_TRANSPORT_PluginFunctions *
373 GST_plugins_printer_find (const char *name)
375 struct TransportPlugin *pos;
376 char *stripped = GNUNET_strdup (name);
377 char *sep = strchr (stripped, '_');
381 for (pos = plugins_head; NULL != pos; pos = pos->next)
382 if (pos->short_name == strstr (pos->short_name, stripped))
384 GNUNET_free (stripped);
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.
395 * @param address the address to convert
396 * @return statically allocated (!) human-readable address
399 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
401 struct GNUNET_TRANSPORT_PluginFunctions *api;
402 static char unable_to_show[1024];
403 static const char *s;
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);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Failed to find transport plugin `%s'\n",
414 address->transport_name);
415 return "<plugin unknown>";
417 if (0 == address->address_length)
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;
426 return(NULL != (s = api->address_to_string (NULL,
428 address->address_length))
435 * Register callback with all plugins to monitor their status.
437 * @param cb callback to register, NULL to unsubscribe
438 * @param cb_cls closure for @a cb
441 GST_plugins_monitor_subscribe (GNUNET_TRANSPORT_SessionInfoCallback cb,
444 struct TransportPlugin *pos;
446 for (pos = plugins_head; NULL != pos; pos = pos->next)
447 if (NULL == pos->api->setup_monitor)
450 pos->api->setup_monitor (pos->api->cls,
456 /* end of file gnunet-service-transport_plugins.c */