hlswmaster/plugin.c

114 lines
3.5 KiB
C
Raw Permalink Normal View History

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. *
***************************************************************************/
2006-11-24 21:53:25 +01:00
#include <stdio.h>
2006-02-02 16:24:06 +01:00
#include <stdlib.h>
2006-11-24 21:53:25 +01:00
#include <string.h>
2006-02-02 16:24:06 +01:00
#include <dlfcn.h>
#include "plugin.h"
#include "configfile.h"
2006-11-24 21:53:25 +01:00
#include "logging.h"
2006-02-02 16:24:06 +01:00
#include "netpkt.h"
#include "list.h"
2006-11-24 21:53:25 +01:00
#include "event.h"
#define BUFSIZE 512
2006-02-02 16:24:06 +01:00
static LIST_HEAD(plugin_list);
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
static int plugin_load(const char *filename, void *privdata)
{
2006-11-24 21:53:25 +01:00
char *plugin_dir = (char *)privdata;
2006-11-24 21:53:25 +01:00
char *fullname = malloc(BUFSIZE);
if (fullname == NULL) {
log_print(LOG_ERROR, "plugin_load: out of memory");
return -1;
}
int len = snprintf(fullname, BUFSIZE, "%s/%s", plugin_dir, filename);
if (len < 0 || len >= BUFSIZE) {
log_print(LOG_ERROR, "plugin_load: file name too long: %s/%s", plugin_dir, filename);
free(fullname);
return -1;
}
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
dlerror();
void *dlref = dlopen(fullname, RTLD_NOW);
if (dlref == NULL) {
log_print(LOG_WARN, "plugin_load: %s", dlerror());
free(fullname);
return -1;
}
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
struct hlswmaster_plugin *plugin = dlsym(dlref, "plugin");
if (plugin == NULL) {
log_print(LOG_WARN, "plugin_load: invalid plugin '%s'", fullname);
dlclose(dlref);
free(fullname);
return -1;
}
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
log_print(LOG_INFO, "loading plugin '%s'", plugin->name);
2006-11-24 21:53:25 +01:00
if (plugin->init == NULL || (plugin->init() == 0)) {
list_add_tail(&plugin->list, &plugin_list);
free(fullname);
return 0;
}
2006-11-24 21:53:25 +01:00
log_print(LOG_WARN, "plugin_load: failed to load '%s'", plugin->name);
dlclose(dlref);
free(fullname);
return -1;
2006-02-02 16:24:06 +01:00
}
int plugins_scan(void)
{
2006-02-02 16:24:06 +01:00
struct hlswmaster_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list)
if (plugin->scan && !plugin->scan())
2006-11-24 21:53:25 +01:00
log_print(LOG_WARN, "plugin %s: scan error", plugin->name);
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
return 0;
2006-02-02 16:24:06 +01:00
}
int plugins_parse(struct net_pkt *pkt)
{
int retval;
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
struct hlswmaster_plugin *plugin;
list_for_each_entry(plugin, &plugin_list, list)
if (plugin->parse && (retval = plugin->parse(pkt)))
return retval;
2006-02-02 16:24:06 +01:00
return PARSE_REJECT;
2006-02-02 16:24:06 +01:00
}
2006-11-24 21:53:25 +01:00
int plugin_init(void)
{
2006-11-24 21:53:25 +01:00
char *dir = config_get_string("global", "plugin_dir", ".");
int cnt = config_get_strings("global", "plugin", plugin_load, (void *)dir);
2006-02-02 16:24:06 +01:00
2006-11-24 21:53:25 +01:00
log_print(LOG_INFO, "%d plugins loaded", cnt);
return 0;
2006-02-02 16:24:06 +01:00
}
2006-11-24 21:53:25 +01:00