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 {
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;
70 * Head of DLL of all loaded plugins.
72 static struct TransportPlugin *plugins_head;
75 * Head of DLL of all loaded plugins.
77 static struct TransportPlugin *plugins_tail;
81 * Function that will be called to update metrics for an address
84 * @param address address to update metrics for
85 * @param session the session
86 * @param distance new distance
89 plugin_env_update_distance(void *cls,
90 const struct GNUNET_HELLO_Address *address,
93 GST_ats_update_distance(address,
99 * Function that will be called to figure if an address is an loopback,
100 * LAN, WAN etc. address
103 * @param addr binary address
104 * @param addrlen length of the @a addr
105 * @return type of the network @a addr belongs to
107 static enum GNUNET_NetworkType
108 plugin_env_address_to_type(void *cls,
109 const struct sockaddr *addr,
115 return GNUNET_NT_UNSPECIFIED;
117 return GNUNET_NT_scanner_get_type(GST_is,
124 * Load and initialize all plugins. The respective functions will be
125 * invoked by the plugins when the respective events happen. The
126 * closure will be set to a 'const char*' containing the name of the
127 * plugin that caused the call.
129 * @param recv_cb function to call when data is received
130 * @param address_cb function to call when our public addresses changed
131 * @param session_start_cb function to call when a session was created
132 * @param session_end_cb function to call when a session was terminated
133 * @param address_type_cb function to call when a address type is requested
136 GST_plugins_load(GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
137 GNUNET_TRANSPORT_AddressNotification address_cb,
138 GNUNET_TRANSPORT_SessionStart session_start_cb,
139 GNUNET_TRANSPORT_SessionEnd session_end_cb)
141 struct TransportPlugin *plug;
142 struct TransportPlugin *next;
143 unsigned long long tneigh;
150 GNUNET_CONFIGURATION_get_value_number(GST_cfg,
155 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
156 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
160 GNUNET_CONFIGURATION_get_value_string(GST_cfg,
165 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
166 _("Starting transport plugins `%s'\n"),
168 for (pos = strtok(plugs, " "); pos != NULL; pos = strtok(NULL, " "))
170 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
171 _("Loading `%s' transport plugin\n"),
173 GNUNET_asprintf(&libname,
174 "libgnunet_plugin_transport_%s",
176 plug = GNUNET_new(struct TransportPlugin);
177 plug->short_name = GNUNET_strdup(pos);
178 plug->lib_name = libname;
179 plug->env.cfg = GST_cfg;
180 plug->env.my_identity = &GST_my_identity;
181 plug->env.get_our_hello = &GST_hello_get;
182 plug->env.cls = plug->short_name;
183 plug->env.receive = recv_cb;
184 plug->env.notify_address = address_cb;
185 plug->env.session_start = session_start_cb;
186 plug->env.session_end = session_end_cb;
187 plug->env.get_address_type = &plugin_env_address_to_type;
188 plug->env.update_address_distance = &plugin_env_update_distance;
189 plug->env.max_connections = tneigh;
190 plug->env.stats = GST_stats;
191 GNUNET_CONTAINER_DLL_insert(plugins_head,
201 plug->api = GNUNET_PLUGIN_load(plug->lib_name,
203 if (NULL == plug->api)
205 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
206 _("Failed to load transport plugin for `%s'\n"),
208 GNUNET_CONTAINER_DLL_remove(plugins_head,
211 GNUNET_free(plug->short_name);
212 GNUNET_free(plug->lib_name);
217 if (NULL == plug->api->address_pretty_printer)
220 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
221 _("Missing function `%s' in transport plugin for `%s'\n"),
222 "address_pretty_printer",
225 if (NULL == plug->api->address_to_string)
228 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
229 _("Missing function `%s' in transport plugin for `%s'\n"),
233 if (NULL == plug->api->string_to_address)
236 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
237 _("Missing function `%s' in transport plugin for `%s'\n"),
241 if (NULL == plug->api->check_address)
244 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
245 _("Missing function `%s' in transport plugin for `%s'\n"),
249 if (NULL == plug->api->get_session)
252 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
253 _("Missing function `%s' in transport plugin for `%s'\n"),
257 if (NULL == plug->api->get_network)
260 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
261 _("Missing function `%s' in transport plugin for `%s'\n"),
265 if (NULL == plug->api->send)
268 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
269 _("Missing function `%s' in transport plugin for `%s'\n"),
273 if (NULL == plug->api->disconnect_peer)
276 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
277 _("Missing function `%s' in transport plugin for `%s'\n"),
281 if (NULL == plug->api->disconnect_session)
284 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
285 _("Missing function `%s' in transport plugin for `%s'\n"),
286 "disconnect_session",
289 if (NULL == plug->api->query_keepalive_factor)
292 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
293 _("Missing function `%s' in transport plugin for `%s'\n"),
294 "query_keepalive_factor",
297 if (NULL == plug->api->update_session_timeout)
300 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
301 _("Missing function `%s' in transport plugin for `%s'\n"),
302 "update_session_timeout",
305 if (GNUNET_YES == fail)
307 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
308 _("Did not load plugin `%s' due to missing functions\n"),
310 GNUNET_break(NULL == GNUNET_PLUGIN_unload(plug->lib_name, plug->api));
311 GNUNET_CONTAINER_DLL_remove(plugins_head,
314 GNUNET_free(plug->short_name);
315 GNUNET_free(plug->lib_name);
328 struct TransportPlugin *plug;
330 while (NULL != (plug = plugins_head))
332 GNUNET_break(NULL == GNUNET_PLUGIN_unload(plug->lib_name, plug->api));
333 GNUNET_free(plug->lib_name);
334 GNUNET_free(plug->short_name);
335 GNUNET_CONTAINER_DLL_remove(plugins_head, plugins_tail, plug);
342 * Obtain the plugin API based on a plugin name.
344 * @param name name of the plugin
345 * @return the plugin's API, NULL if the plugin is not loaded
347 struct GNUNET_TRANSPORT_PluginFunctions *
348 GST_plugins_find(const char *name)
350 struct TransportPlugin *pos;
352 for (pos = plugins_head; NULL != pos; pos = pos->next)
353 if (0 == strcmp(name, pos->short_name))
362 * Obtain the plugin API based on a the stripped plugin name after the underscore.
364 * Example: GST_plugins_printer_find (http_client) will return all plugins
365 * starting with the prefix "http":
366 * http_client or server if loaded
368 * @param name name of the plugin
369 * @return the plugin's API, NULL if the plugin is not loaded
371 struct GNUNET_TRANSPORT_PluginFunctions *
372 GST_plugins_printer_find(const char *name)
374 struct TransportPlugin *pos;
375 char *stripped = GNUNET_strdup(name);
376 char *sep = strchr(stripped, '_');
380 for (pos = plugins_head; NULL != pos; pos = pos->next)
381 if (pos->short_name == strstr(pos->short_name, stripped))
383 GNUNET_free(stripped);
391 * Convert a given address to a human-readable format. Note that the
392 * return value will be overwritten on the next call to this function.
394 * @param address the address to convert
395 * @return statically allocated (!) human-readable address
398 GST_plugins_a2s(const struct GNUNET_HELLO_Address *address)
400 struct GNUNET_TRANSPORT_PluginFunctions *api;
401 static char unable_to_show[1024];
402 static const char *s;
406 if (0 == address->address_length)
407 return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
408 api = GST_plugins_printer_find(address->transport_name);
411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
412 "Failed to find transport plugin `%s'\n",
413 address->transport_name);
414 return "<plugin unknown>";
416 if (0 == address->address_length)
418 GNUNET_snprintf(unable_to_show,
419 sizeof(unable_to_show),
420 "<unable to stringify %u-byte long address of %s transport>",
421 (unsigned int)address->address_length,
422 address->transport_name);
423 return unable_to_show;
425 return(NULL != (s = api->address_to_string(NULL,
427 address->address_length))
434 * Register callback with all plugins to monitor their status.
436 * @param cb callback to register, NULL to unsubscribe
437 * @param cb_cls closure for @a cb
440 GST_plugins_monitor_subscribe(GNUNET_TRANSPORT_SessionInfoCallback cb,
443 struct TransportPlugin *pos;
445 for (pos = plugins_head; NULL != pos; pos = pos->next)
446 if (NULL == pos->api->setup_monitor)
449 pos->api->setup_monitor(pos->api->cls,
455 /* end of file gnunet-service-transport_plugins.c */