2 This file is part of GNUnet.
3 Copyright (C) 2010-2014 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_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;
72 * Head of DLL of all loaded plugins.
74 static struct TransportPlugin *plugins_head;
77 * Head of DLL of all loaded plugins.
79 static struct TransportPlugin *plugins_tail;
83 * Function that will be called to update metrics for an address
86 * @param address address to update metrics for
87 * @param session the session
88 * @param ats the ats information to update
89 * @param ats_count the number of @a ats elements
92 plugin_env_update_metrics (void *cls,
93 const struct GNUNET_HELLO_Address *address,
94 struct Session *session,
95 const struct GNUNET_ATS_Information *ats,
98 GST_ats_update_metrics (address,
105 * Function that will be called to figure if an address is an loopback,
106 * LAN, WAN etc. address
109 * @param addr binary address
110 * @param addrlen length of the @a addr
111 * @return type of the network @a addr belongs to
113 static enum GNUNET_ATS_Network_Type
114 plugin_env_address_to_type (void *cls,
115 const struct sockaddr *addr,
121 return GNUNET_ATS_NET_UNSPECIFIED;
123 return GNUNET_ATS_scanner_address_get_type (GST_is,
130 * Load and initialize all plugins. The respective functions will be
131 * invoked by the plugins when the respective events happen. The
132 * closure will be set to a 'const char*' containing the name of the
133 * plugin that caused the call.
135 * @param recv_cb function to call when data is received
136 * @param address_cb function to call when our public addresses changed
137 * @param session_start_cb function to call when a session was created
138 * @param session_end_cb function to call when a session was terminated
139 * @param address_type_cb function to call when a address type is requested
142 GST_plugins_load (GNUNET_TRANSPORT_PluginReceiveCallback recv_cb,
143 GNUNET_TRANSPORT_AddressNotification address_cb,
144 GNUNET_TRANSPORT_SessionStart session_start_cb,
145 GNUNET_TRANSPORT_SessionEnd session_end_cb)
147 struct TransportPlugin *plug;
148 struct TransportPlugin *next;
149 unsigned long long tneigh;
156 GNUNET_CONFIGURATION_get_value_number (GST_cfg,
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 _("Transport service is lacking NEIGHBOUR_LIMIT option.\n"));
166 GNUNET_CONFIGURATION_get_value_string (GST_cfg,
171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
172 _("Starting transport plugins `%s'\n"),
174 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 _("Loading `%s' transport plugin\n"),
179 GNUNET_asprintf (&libname,
180 "libgnunet_plugin_transport_%s",
182 plug = GNUNET_new (struct TransportPlugin);
183 plug->short_name = GNUNET_strdup (pos);
184 plug->lib_name = libname;
185 plug->env.cfg = GST_cfg;
186 plug->env.my_identity = &GST_my_identity;
187 plug->env.get_our_hello = &GST_hello_get;
188 plug->env.cls = plug->short_name;
189 plug->env.receive = recv_cb;
190 plug->env.notify_address = address_cb;
191 plug->env.session_start = session_start_cb;
192 plug->env.session_end = session_end_cb;
193 plug->env.get_address_type = &plugin_env_address_to_type;
194 plug->env.update_address_metrics = &plugin_env_update_metrics;
195 plug->env.max_connections = tneigh;
196 plug->env.stats = GST_stats;
197 GNUNET_CONTAINER_DLL_insert (plugins_head,
207 plug->api = GNUNET_PLUGIN_load (plug->lib_name,
209 if (NULL == plug->api)
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 _("Failed to load transport plugin for `%s'\n"),
214 GNUNET_CONTAINER_DLL_remove (plugins_head,
217 GNUNET_free (plug->short_name);
218 GNUNET_free (plug->lib_name);
223 if (NULL == plug->api->address_pretty_printer)
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 _("Missing function `%s' in transport plugin for `%s'\n"),
228 "address_pretty_printer",
231 if (NULL == plug->api->address_to_string)
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 _("Missing function `%s' in transport plugin for `%s'\n"),
239 if (NULL == plug->api->string_to_address)
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 _("Missing function `%s' in transport plugin for `%s'\n"),
247 if (NULL == plug->api->check_address)
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 _("Missing function `%s' in transport plugin for `%s'\n"),
255 if (NULL == plug->api->get_session)
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 _("Missing function `%s' in transport plugin for `%s'\n"),
263 if (NULL == plug->api->get_network)
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 _("Missing function `%s' in transport plugin for `%s'\n"),
271 if (NULL == plug->api->send)
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 _("Missing function `%s' in transport plugin for `%s'\n"),
279 if (NULL == plug->api->disconnect_peer)
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283 _("Missing function `%s' in transport plugin for `%s'\n"),
287 if (NULL == plug->api->disconnect_session)
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
291 _("Missing function `%s' in transport plugin for `%s'\n"),
292 "disconnect_session",
295 if (NULL == plug->api->query_keepalive_factor)
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 _("Missing function `%s' in transport plugin for `%s'\n"),
300 "query_keepalive_factor",
303 if (NULL == plug->api->update_session_timeout)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 _("Missing function `%s' in transport plugin for `%s'\n"),
308 "update_session_timeout",
311 if (GNUNET_YES == fail)
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 _("Did not load plugin `%s' due to missing functions\n"),
316 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
317 GNUNET_CONTAINER_DLL_remove (plugins_head,
320 GNUNET_free (plug->short_name);
321 GNUNET_free (plug->lib_name);
332 GST_plugins_unload ()
334 struct TransportPlugin *plug;
336 while (NULL != (plug = plugins_head))
338 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
339 GNUNET_free (plug->lib_name);
340 GNUNET_free (plug->short_name);
341 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
348 * Obtain the plugin API based on a plugin name.
350 * @param name name of the plugin
351 * @return the plugin's API, NULL if the plugin is not loaded
353 struct GNUNET_TRANSPORT_PluginFunctions *
354 GST_plugins_find (const char *name)
356 struct TransportPlugin *pos;
358 for (pos = plugins_head; NULL != pos; pos = pos->next)
359 if (0 == strcmp (name, pos->short_name))
368 * Obtain the plugin API based on a the stripped plugin name after the underscore.
370 * Example: GST_plugins_printer_find (http_client) will return all plugins
371 * starting with the prefix "http":
372 * http_client or server if loaded
374 * @param name name of the plugin
375 * @return the plugin's API, NULL if the plugin is not loaded
377 struct GNUNET_TRANSPORT_PluginFunctions *
378 GST_plugins_printer_find (const char *name)
380 struct TransportPlugin *pos;
381 char *stripped = GNUNET_strdup (name);
382 char *sep = strchr (stripped, '_');
386 for (pos = plugins_head; NULL != pos; pos = pos->next)
387 if (pos->short_name == strstr (pos->short_name, stripped))
389 GNUNET_free (stripped);
397 * Convert a given address to a human-readable format. Note that the
398 * return value will be overwritten on the next call to this function.
400 * @param address the address to convert
401 * @return statically allocated (!) human-readable address
404 GST_plugins_a2s (const struct GNUNET_HELLO_Address *address)
406 struct GNUNET_TRANSPORT_PluginFunctions *api;
407 static char unable_to_show[1024];
408 static const char *s;
412 if (0 == address->address_length)
413 return TRANSPORT_SESSION_INBOUND_STRING; /* Addresse with length 0 are inbound, address->address itself may be NULL */
414 api = GST_plugins_printer_find (address->transport_name);
416 return "<plugin unknown>";
417 if (0 == address->address_length)
419 GNUNET_snprintf (unable_to_show, 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, address->address,
426 address->address_length)) ? s : "<invalid>");
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,
451 /* end of file gnunet-service-transport_plugins.c */