2006-02-02 16:24:06 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 03/2005 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "hlswmaster.h"
|
|
|
|
#include "plugin.h"
|
2006-02-02 16:41:56 +01:00
|
|
|
#include "configfile.h"
|
2006-02-02 16:24:06 +01:00
|
|
|
#include "netpkt.h"
|
|
|
|
#include "list.h"
|
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/** liste der geladenen plugins */
|
2006-02-02 16:43:41 +01:00
|
|
|
static LIST_HEAD(plugin_list);
|
2006-02-02 16:24:06 +01:00
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/** sichert die plugin liste UND die plugins selbst ab */
|
2006-02-02 16:24:06 +01:00
|
|
|
static pthread_mutex_t plugin_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* plugin_load()
|
2006-02-02 16:44:46 +01:00
|
|
|
* laedt ein plugin und fuegt es in die plugin liste ein
|
2006-02-02 16:25:55 +01:00
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *name filename des plugins
|
2006-02-02 16:25:55 +01:00
|
|
|
* @return false on error
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugin_load(char *name)
|
|
|
|
{
|
2006-02-02 16:41:56 +01:00
|
|
|
struct hlswmaster_plugin *plugin;
|
2006-02-02 16:24:06 +01:00
|
|
|
void *tmp;
|
2006-02-02 16:41:56 +01:00
|
|
|
struct conf_section *section;
|
|
|
|
|
|
|
|
dlerror();
|
2006-02-02 16:43:41 +01:00
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/* shared objekt oeffnen */
|
2006-02-02 16:41:56 +01:00
|
|
|
if ((tmp = dlopen(name, RTLD_NOW))) {
|
2006-02-02 16:44:46 +01:00
|
|
|
|
|
|
|
/* plugin struct suchen */
|
2006-02-02 16:41:56 +01:00
|
|
|
if ((plugin = dlsym(tmp, "plugin"))) {
|
|
|
|
plugin->dlref = tmp;
|
|
|
|
|
|
|
|
section = config_get_section(plugin->name);
|
|
|
|
log_print("loading plugin '%s'", plugin->name);
|
2006-02-02 16:43:41 +01:00
|
|
|
|
2006-02-02 16:44:46 +01:00
|
|
|
/* wenn vorhanden, init aufrufen */
|
2006-02-02 16:41:56 +01:00
|
|
|
if (!plugin->init || (plugin->init(section))) {
|
|
|
|
pthread_mutex_lock(&plugin_lock);
|
|
|
|
list_add_tail(&plugin->list, &plugin_list);
|
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-02-02 16:44:46 +01:00
|
|
|
|
2006-02-02 16:41:56 +01:00
|
|
|
log_print("failed to load '%s'", name);
|
|
|
|
dlclose(tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dlclose(tmp);
|
|
|
|
}
|
2006-02-02 16:43:41 +01:00
|
|
|
log_print("%s", dlerror());
|
2006-02-02 16:41:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-02-02 16:24:06 +01:00
|
|
|
|
|
|
|
|
2006-02-02 16:41:56 +01:00
|
|
|
/**
|
|
|
|
* plugin_load_all()
|
|
|
|
* laedt alle in der config angegebenen plugins
|
|
|
|
*
|
|
|
|
* @return false on error
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugin_load_all()
|
|
|
|
{
|
2006-02-02 16:41:56 +01:00
|
|
|
struct conf_section *section;
|
|
|
|
struct conf_tupel *tupel;
|
|
|
|
|
|
|
|
section = config_get_section("global");
|
|
|
|
if (section) {
|
|
|
|
list_for_each_entry(tupel, §ion->tupel, list)
|
|
|
|
if (!strcmp(tupel->option, "plugin"))
|
|
|
|
plugin_load(tupel->parameter);
|
|
|
|
}
|
2006-02-02 16:25:55 +01:00
|
|
|
return 1;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-02 16:41:56 +01:00
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
2006-02-02 16:41:56 +01:00
|
|
|
* plugin_unload_all()
|
|
|
|
* entfernt alle plugins aus der liste,
|
2006-02-02 16:25:55 +01:00
|
|
|
*
|
|
|
|
* @return false on error
|
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugin_unload_all()
|
|
|
|
{
|
2006-02-02 16:41:56 +01:00
|
|
|
struct hlswmaster_plugin *plugin, *tmp;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&plugin_lock);
|
|
|
|
list_for_each_entry_safe(plugin, tmp, &plugin_list, list) {
|
|
|
|
list_del(&plugin->list);
|
|
|
|
|
|
|
|
if (plugin->fini)
|
|
|
|
plugin->fini();
|
|
|
|
|
|
|
|
log_print("plugin '%s' unloaded", plugin->name);
|
|
|
|
dlclose(plugin->dlref);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
2006-02-02 16:25:55 +01:00
|
|
|
return 1;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* plugins_scan()
|
2006-02-02 16:44:46 +01:00
|
|
|
* ruft die scan() Funktionen der Plugins auf
|
2006-02-02 16:25:55 +01:00
|
|
|
*
|
|
|
|
* @return true
|
2006-02-02 16:27:19 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugins_scan(void)
|
|
|
|
{
|
2006-02-02 16:24:06 +01:00
|
|
|
struct hlswmaster_plugin *plugin;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&plugin_lock);
|
|
|
|
list_for_each_entry(plugin, &plugin_list, list)
|
2006-02-02 16:44:46 +01:00
|
|
|
/* wenn vorhanden die scan funktion aufrufen */
|
2006-02-02 16:24:06 +01:00
|
|
|
if (plugin->scan && !plugin->scan())
|
|
|
|
log_print("plugin %s: scan error", plugin->name);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* plugins_parse()
|
2006-02-02 16:44:46 +01:00
|
|
|
* ruft die parse() Funktionen der Plugins auf
|
2006-02-02 16:25:55 +01:00
|
|
|
* bis ein Plugin das Paket annimmt
|
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param *pkt das zu parsene paket
|
2006-02-02 16:47:20 +01:00
|
|
|
* @return false wenn kein Plugin das Paket angenommen,
|
|
|
|
* sonst rueckgabewert des Plugins
|
2006-02-02 16:27:19 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugins_parse(struct net_pkt *pkt)
|
|
|
|
{
|
2006-02-02 16:24:06 +01:00
|
|
|
struct hlswmaster_plugin *plugin;
|
2006-02-02 16:47:20 +01:00
|
|
|
int retval;
|
2006-02-02 16:24:06 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&plugin_lock);
|
|
|
|
list_for_each_entry(plugin, &plugin_list, list) {
|
2006-02-02 16:44:46 +01:00
|
|
|
/* wenn vorhanden die parse funktion aufrufen */
|
2006-02-02 16:47:20 +01:00
|
|
|
if (plugin->parse && (retval = plugin->parse(pkt))) {
|
2006-02-02 16:24:06 +01:00
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
2006-02-02 16:47:20 +01:00
|
|
|
return retval;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
2006-02-02 16:47:20 +01:00
|
|
|
return PARSE_REJECT;
|
2006-02-02 16:24:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-02 16:25:55 +01:00
|
|
|
/**
|
|
|
|
* plugins_gc()
|
2006-02-02 16:44:46 +01:00
|
|
|
* ruft die gc() Funktionen der Plugins auf
|
2006-02-02 16:25:55 +01:00
|
|
|
*
|
2006-02-02 16:44:46 +01:00
|
|
|
* @param timeout timeout interval in sekunden
|
2006-02-02 16:25:55 +01:00
|
|
|
* @return true
|
2006-02-02 16:27:19 +01:00
|
|
|
*/
|
2006-02-02 16:47:20 +01:00
|
|
|
int plugins_gc(unsigned long timeout)
|
|
|
|
{
|
2006-02-02 16:24:06 +01:00
|
|
|
struct hlswmaster_plugin *plugin;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&plugin_lock);
|
|
|
|
list_for_each_entry(plugin, &plugin_list, list)
|
|
|
|
if (plugin->gc)
|
|
|
|
plugin->gc(timeout);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&plugin_lock);
|
|
|
|
return 1;
|
|
|
|
}
|