2 This file is part of GNUnet.
3 Copyright (C) 2010,2011 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 peerinfo-tool/gnunet-peerinfo_plugins.c
18 * @brief plugin management
19 * @author Christian Grothoff
22 #include "gnunet-peerinfo_plugins.h"
23 #include "gnunet_transport_plugin.h"
24 #include "gnunet_hello_lib.h"
27 * Entry in doubly-linked list of all of our plugins.
29 struct TransportPlugin
32 * This is a doubly-linked list.
34 struct TransportPlugin *next;
37 * This is a doubly-linked list.
39 struct TransportPlugin *prev;
42 * API of the transport as returned by the plugin's
43 * initialization function.
45 struct GNUNET_TRANSPORT_PluginFunctions *api;
48 * Short name for the plugin (i.e. "tcp").
53 * Name of the library (i.e. "gnunet_plugin_transport_tcp").
58 * Environment this transport service is using
61 struct GNUNET_TRANSPORT_PluginEnvironment env;
66 * Head of DLL of all loaded plugins.
68 static struct TransportPlugin *plugins_head;
71 * Head of DLL of all loaded plugins.
73 static struct TransportPlugin *plugins_tail;
78 * Load and initialize all plugins. The respective functions will be
79 * invoked by the plugins when the respective events happen. The
80 * closure will be set to a 'const char*' containing the name of the
81 * plugin that caused the call.
83 * @param cfg configuration to use
86 GPI_plugins_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
88 struct TransportPlugin *plug;
89 struct TransportPlugin *next;
94 if (NULL != plugins_head)
95 return; /* already loaded */
97 GNUNET_CONFIGURATION_get_value_string (cfg, "TRANSPORT", "PLUGINS",
100 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting transport plugins `%s'\n"),
102 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
104 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' transport plugin\n"),
106 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos);
107 plug = GNUNET_new (struct TransportPlugin);
108 plug->short_name = GNUNET_strdup (pos);
109 plug->lib_name = libname;
111 plug->env.cls = plug->short_name;
112 GNUNET_CONTAINER_DLL_insert (plugins_head, plugins_tail, plug);
120 plug->api = GNUNET_PLUGIN_load (plug->lib_name, &plug->env);
121 if (plug->api == NULL)
123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124 _("Failed to load transport plugin for `%s'\n"),
126 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
127 GNUNET_free (plug->short_name);
128 GNUNET_free (plug->lib_name);
139 GPI_plugins_unload ()
141 struct TransportPlugin *plug;
143 while (NULL != (plug = plugins_head))
145 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
146 GNUNET_free (plug->lib_name);
147 GNUNET_free (plug->short_name);
148 GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug);
155 * Obtain the plugin API based on a plugin name.
157 * @param name name of the plugin
158 * @return the plugin's API, NULL if the plugin is not loaded
160 struct GNUNET_TRANSPORT_PluginFunctions *
161 GPI_plugins_find (const char *name)
163 struct TransportPlugin *head = plugins_head;
165 char *stripped = GNUNET_strdup (name);
167 char *sep = strchr (stripped, '_');
173 head_stripped = GNUNET_strdup(head->short_name);
174 char *head_sep = strchr (head_stripped, '_');
175 if (NULL != head_sep)
177 if (0 == strcmp (head_stripped, stripped))
179 GNUNET_free (head_stripped);
182 GNUNET_free (head_stripped);
185 GNUNET_free (stripped);
192 /* end of file gnunet-peerinfo_plugins.c */