2 * OLSR ad-hoc routing table management protocol
3 * Copyright (C) 2004 Andreas Tønnesen (andreto@ifi.uio.no)
5 * This file is part of the olsr.org OLSR daemon.
7 * olsr.org is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * olsr.org is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with olsr.org; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * $Id: plugin_loader.c,v 1.10 2004/11/06 09:20:09 kattemat Exp $
26 #include "plugin_loader.h"
33 init_olsr_plugin(struct olsr_plugin *);
36 olsr_load_dl(char *, struct plugin_param *);
42 *Function that loads all registered plugins
44 *@return the number of plugins loaded
49 struct plugin_entry *entry;
52 entry = olsr_cnf->plugins;
55 olsr_printf(1, "Loading plugins...\n\n");
59 if(olsr_load_dl(entry->name, entry->params) < 0)
60 olsr_printf(1, "-- PLUGIN LOADING FAILED! --\n\n");
71 *Try to load a shared library and extract
72 *the required information
74 *@param libname the name of the library(file)
76 *@return negative on error
79 olsr_load_dl(char *libname, struct plugin_param *params)
81 struct olsr_plugin new_entry, *entry;
82 int *interface_version;
85 olsr_printf(1, "---------- Plugin loader ----------\nLibrary: %s\n", libname);
87 if((new_entry.dlhandle = dlopen(libname, RTLD_NOW)) == NULL)
89 olsr_printf(1, "DL loading failed: \"%s\"!\n", dlerror());
93 /* Fetch the multipurpose function */
94 olsr_printf(1, "Checking plugin interface version....");
95 /* Register mp function */
96 if((interface_version = dlsym(new_entry.dlhandle, "plugin_interface_version")) == NULL)
98 olsr_printf(1, "FAILED: \"%s\"\n", dlerror());
99 dlclose(new_entry.dlhandle);
104 olsr_printf(1, " %d - ", *interface_version);
105 if(*interface_version != PLUGIN_INTERFACE_VERSION)
106 olsr_printf(1, "WARNING: VERSION MISSMATCH!\n");
108 olsr_printf(1, "OK\n");
111 olsr_printf(1, "Trying to fetch register function....");
113 if((new_entry.register_olsr_data = dlsym(new_entry.dlhandle, "register_olsr_data")) == NULL)
115 /* This function must be present */
116 olsr_printf(1, "\nCould not find function registration function in plugin!\n%s\nCRITICAL ERROR - aborting!\n", dlerror());
117 dlclose(new_entry.dlhandle);
120 olsr_printf(1, "OK\n");
123 /* Fetch the multipurpose function */
124 olsr_printf(1, "Trying to fetch plugin IO function....");
125 if((new_entry.plugin_io = dlsym(new_entry.dlhandle, "plugin_io")) == NULL)
126 olsr_printf(1, "FAILED: \"%s\"\n", dlerror());
128 olsr_printf(1, "OK\n");
130 /* Fetch the parameter function */
131 olsr_printf(1, "Trying to fetch param function....");
132 if((new_entry.register_param = dlsym(new_entry.dlhandle, "register_olsr_param")) == NULL)
133 olsr_printf(1, "FAILED: \"%s\"\n", dlerror());
135 olsr_printf(1, "OK\n");
138 entry = olsr_malloc(sizeof(struct olsr_plugin), "Plugin entry");
140 memcpy(entry, &new_entry, sizeof(struct olsr_plugin));
142 entry->params = params;
144 /* Initialize the plugin */
145 init_olsr_plugin(entry);
148 entry->next = olsr_plugins;
149 olsr_plugins = entry;
151 olsr_printf(1, "---------- LIBRARY LOADED ----------\n\n");
159 *Initialize a loaded plugin
160 *This includes sending information
161 *from olsrd to the plugin and
162 *register the functions from the flugin with olsrd
164 *@param entry the plugin to initialize
169 init_olsr_plugin(struct olsr_plugin *entry)
171 struct olsr_plugin_data plugin_data;
172 struct plugin_param *params = entry->params;
175 if(entry->register_param)
177 olsr_printf(1, "Sending parameters...\n");
180 olsr_printf(1, "\"%s\"/\"%s\".... ", params->key, params->value);
181 if((retval = entry->register_param(params->key, params->value)) < 0)
183 fprintf(stderr, "\nFatal error in plugin parameter \"%s\"/\"%s\"\n", params->key, params->value);
186 retval == 0 ? olsr_printf(1, "FAILED\n") : olsr_printf(1, "OK\n");
188 params = params->next;
192 olsr_printf(1, "Running registration function...\n");
194 plugin_data.ipversion = olsr_cnf->ip_version;
195 plugin_data.main_addr = &main_addr;
197 plugin_data.olsr_plugin_io = &olsr_plugin_io;
199 /* Register data with plugin */
200 entry->register_olsr_data(&plugin_data);
207 *Close all loaded plugins
212 struct olsr_plugin *entry;
214 olsr_printf(1, "Closing plugins...\n");
215 for(entry = olsr_plugins;
219 dlclose(&entry->dlhandle);