2016-03-13 09:45:56 +01:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
|
|
|
|
|
|
|
actube 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2018-02-25 19:12:28 +01:00
|
|
|
* @brief Functions for modules (mods) management.
|
2016-03-13 09:45:56 +01:00
|
|
|
*/
|
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
#include <string.h>
|
2016-03-27 04:48:36 +02:00
|
|
|
#include <errno.h>
|
2018-02-25 19:12:28 +01:00
|
|
|
#include <dlfcn.h>
|
2016-03-27 04:48:36 +02:00
|
|
|
|
2018-03-17 17:29:09 +01:00
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
#include "mavl.h"
|
|
|
|
#include "dbg.h"
|
2016-03-27 04:48:36 +02:00
|
|
|
#include "log.h"
|
2018-02-26 14:44:27 +01:00
|
|
|
#include "file.h"
|
2018-02-26 18:28:12 +01:00
|
|
|
#include "cw.h"
|
2018-03-26 15:11:57 +02:00
|
|
|
#include "msgset.h"
|
2016-03-27 04:48:36 +02:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
/*
|
2018-02-25 19:12:28 +01:00
|
|
|
static void (*actions_registered_cb) (struct cw_Mod * capwap, struct cw_Mod * bindings,
|
2016-03-27 04:48:36 +02:00
|
|
|
struct cw_actiondef * actions) = NULL;
|
2018-03-26 13:21:42 +02:00
|
|
|
*/
|
2016-03-27 04:48:36 +02:00
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
/*
|
2016-03-27 04:48:36 +02:00
|
|
|
void mod_set_actions_registered_cb(void (*fun)
|
2018-02-25 19:12:28 +01:00
|
|
|
(struct cw_Mod *, struct cw_Mod *,
|
|
|
|
struct cw_actiondef *))
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2016-03-27 04:48:36 +02:00
|
|
|
actions_registered_cb = fun;
|
|
|
|
}
|
2018-03-26 13:21:42 +02:00
|
|
|
*/
|
2016-03-27 04:48:36 +02:00
|
|
|
|
|
|
|
struct cache_item {
|
2016-03-08 01:20:22 +01:00
|
|
|
const char *capwap;
|
|
|
|
const char *bindings;
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_MsgSet *msgset;
|
2016-03-08 01:20:22 +01:00
|
|
|
};
|
2018-02-26 18:28:12 +01:00
|
|
|
static struct mavl *msgset_cache = NULL;
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2018-03-06 03:08:14 +01:00
|
|
|
/*
|
2016-03-27 04:48:36 +02:00
|
|
|
static int mod_null_register_actions(struct cw_actiondef *def, int mode)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-06 03:08:14 +01:00
|
|
|
*/
|
2016-03-13 09:45:56 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-27 04:48:36 +02:00
|
|
|
* mod_null is a dummy mod
|
2016-03-13 09:45:56 +01:00
|
|
|
*/
|
2018-02-25 19:12:28 +01:00
|
|
|
struct cw_Mod mod_null = {
|
2018-03-26 13:21:42 +02:00
|
|
|
"none", /* name */
|
|
|
|
NULL, /* init */
|
|
|
|
NULL, /* init_config */
|
|
|
|
NULL, /* detect */
|
|
|
|
NULL /* data */
|
2016-03-08 01:20:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-11 00:56:41 +01:00
|
|
|
static int cmp(const void *p1, const void *p2)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2018-03-11 00:56:41 +01:00
|
|
|
struct cache_item *c1 = ((struct cache_item **) p1)[0];
|
|
|
|
struct cache_item *c2 = ((struct cache_item **) p2)[0];
|
2016-03-08 01:20:22 +01:00
|
|
|
|
|
|
|
int r;
|
2016-03-27 04:48:36 +02:00
|
|
|
r = strcmp(c1->capwap, c2->capwap);
|
|
|
|
if (r != 0)
|
2016-03-08 01:20:22 +01:00
|
|
|
return r;
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
return strcmp(c1->bindings, c2->bindings);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
struct cw_actiondef *mod_cache_get(const char *capwap, const char *bindings)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
|
|
|
struct cw_Mod *capwap_mod,
|
|
|
|
struct cw_Mod *bindings_mod)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2018-03-06 03:08:14 +01:00
|
|
|
struct cache_item search;
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cache_item *cached_set;
|
|
|
|
struct cw_MsgSet *set;
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-02-26 18:28:12 +01:00
|
|
|
if (!msgset_cache) {
|
2018-03-26 13:21:42 +02:00
|
|
|
msgset_cache = mavl_create(cmp, NULL, 1312);
|
2018-02-26 18:28:12 +01:00
|
|
|
if (!msgset_cache) {
|
|
|
|
cw_log(LOG_ERR, "Can't initialize msgset cache: %s",
|
2016-03-27 04:48:36 +02:00
|
|
|
strerror(errno));
|
2016-03-08 01:20:22 +01:00
|
|
|
return NULL;
|
2016-03-27 04:48:36 +02:00
|
|
|
}
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-02-26 18:28:12 +01:00
|
|
|
search.capwap = capwap_mod->name;
|
|
|
|
search.bindings = bindings_mod->name;
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2018-03-06 03:08:14 +01:00
|
|
|
cached_set = mavl_get_ptr(msgset_cache, &search);
|
2018-02-26 18:28:12 +01:00
|
|
|
if (cached_set) {
|
2018-03-26 13:21:42 +02:00
|
|
|
cw_dbg(DBG_INFO, "Using cached message set for %s,%s", capwap_mod->name,
|
|
|
|
bindings_mod->name);
|
2018-02-26 18:28:12 +01:00
|
|
|
return cached_set->msgset;
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-26 18:28:12 +01:00
|
|
|
cached_set = malloc(sizeof(struct cache_item));
|
|
|
|
if (!cached_set) {
|
2016-03-27 04:48:36 +02:00
|
|
|
cw_log(LOG_ERR, "Can't allocate memory for mod cache item %s",
|
|
|
|
strerror(errno));
|
2016-03-08 01:20:22 +01:00
|
|
|
return NULL;
|
2016-03-27 04:48:36 +02:00
|
|
|
}
|
2018-02-26 18:28:12 +01:00
|
|
|
memset(cached_set, 0, sizeof(struct cache_item));
|
2018-03-26 13:21:42 +02:00
|
|
|
|
2018-03-06 03:08:14 +01:00
|
|
|
set = cw_msgset_create();
|
2018-02-26 18:28:12 +01:00
|
|
|
if (!set) {
|
|
|
|
free(cached_set);
|
|
|
|
cw_log(LOG_ERR, "Can't allocate memory for mod cache item %s",
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-26 13:21:42 +02:00
|
|
|
cached_set->msgset = set;
|
2018-02-26 18:28:12 +01:00
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2018-02-26 18:28:12 +01:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
cw_dbg(DBG_INFO, "Loading message set for %s,%s", capwap_mod->name,
|
|
|
|
bindings_mod->name);
|
2018-02-26 18:28:12 +01:00
|
|
|
|
|
|
|
|
2018-03-31 10:10:22 +02:00
|
|
|
cached_set->capwap = capwap_mod->name;
|
|
|
|
cached_set->bindings = bindings_mod->name;
|
|
|
|
|
2018-03-26 21:11:07 +02:00
|
|
|
if (capwap_mod != MOD_NULL) {
|
2018-03-31 10:10:22 +02:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
capwap_mod->register_messages(cached_set->msgset, CW_MOD_MODE_CAPWAP);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
2018-03-26 21:11:07 +02:00
|
|
|
if (bindings_mod != MOD_NULL) {
|
2018-03-31 10:10:22 +02:00
|
|
|
|
2018-03-26 20:18:24 +02:00
|
|
|
bindings_mod->register_messages(cached_set->msgset, CW_MOD_MODE_BINDINGS);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
2018-03-06 03:08:14 +01:00
|
|
|
/*
|
2018-02-26 18:28:12 +01:00
|
|
|
// if (actions_registered_cb)
|
|
|
|
// actions_registered_cb(capwap_mod, bindings_mod, &(cached_set->actions));
|
2018-03-06 03:08:14 +01:00
|
|
|
*/
|
|
|
|
mavl_add_ptr(msgset_cache, cached_set);
|
2018-02-26 18:28:12 +01:00
|
|
|
return cached_set->msgset;
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
2018-02-25 19:12:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* static mavl to store modules */
|
2018-03-26 13:21:42 +02:00
|
|
|
static struct mavl *mods_loaded = NULL;
|
|
|
|
static int mod_cmp_mavl(const void *e1, const void *e2)
|
|
|
|
{
|
|
|
|
const struct cw_Mod *m1 = *((const struct cw_Mod **) e1);
|
|
|
|
const struct cw_Mod *m2 = *((const struct cw_Mod **) e2);
|
|
|
|
return strcmp(m1->name, m2->name);
|
2018-03-06 03:08:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
static int mod_cmp_mlist(const void *e1, const void *e2)
|
|
|
|
{
|
|
|
|
const struct cw_Mod *m1 = e1;
|
|
|
|
const struct cw_Mod *m2 = e2;
|
|
|
|
return strcmp(m1->name, m2->name);
|
2018-02-25 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 03:08:14 +01:00
|
|
|
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
static const char *mod_path = "";
|
2018-02-25 19:12:28 +01:00
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
/**
|
|
|
|
* @brief Set module path, where to search for modules
|
|
|
|
* @param path Path to search
|
|
|
|
*/
|
2018-03-26 13:21:42 +02:00
|
|
|
void cw_mod_set_path(const char *path)
|
|
|
|
{
|
2018-02-26 12:21:50 +01:00
|
|
|
mod_path = path;
|
2018-02-25 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2018-02-26 12:21:50 +01:00
|
|
|
/**
|
|
|
|
* @brief Load a module
|
2018-02-26 14:13:57 +01:00
|
|
|
* @param mod_name Name of the module
|
|
|
|
* @return a pointer to the module interface
|
2018-02-26 12:21:50 +01:00
|
|
|
*/
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_Mod *cw_mod_load(const char *mod_name, mavl_t global_cfg, int role)
|
|
|
|
{
|
2018-03-06 03:08:14 +01:00
|
|
|
struct cw_Mod search;
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_Mod *mod;
|
|
|
|
char mod_filename[CW_MOD_MAX_MOD_NAME_LEN + 5];
|
|
|
|
char *filename;
|
|
|
|
void *handle;
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-02-26 12:21:50 +01:00
|
|
|
/* if modlist is not initialized, initialize ... */
|
2018-03-26 13:21:42 +02:00
|
|
|
if (mods_loaded == NULL) {
|
|
|
|
mods_loaded = mavl_create(mod_cmp_mavl, NULL, 1312);
|
|
|
|
if (mods_loaded == NULL) {
|
2018-02-26 12:21:50 +01:00
|
|
|
cw_log(LOG_ERROR, "Can't init modlist, no memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 09:04:53 +01:00
|
|
|
|
2018-02-26 14:13:57 +01:00
|
|
|
/* Search for the module in mods_loaded, to see if it is
|
2018-02-26 09:04:53 +01:00
|
|
|
* already loaded or was statically linked */
|
2018-03-26 13:21:42 +02:00
|
|
|
|
|
|
|
cw_dbg(DBG_MOD, "MOD: Load module '%s'", mod_name);
|
|
|
|
|
|
|
|
memset(&search, 0, sizeof(search));
|
|
|
|
search.name = mod_name;
|
|
|
|
|
|
|
|
mod = mavl_find_ptr(mods_loaded, &search);
|
|
|
|
if (mod) {
|
|
|
|
cw_dbg(DBG_MOD, "MOD: Module already loaded '%s'", mod_name);
|
2018-02-26 09:04:53 +01:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
if (strlen(mod_name) > CW_MOD_MAX_MOD_NAME_LEN) {
|
|
|
|
cw_log(LOG_ERROR, "Mod name too long: %s (max allowed = %d)",
|
|
|
|
mod_name, CW_MOD_MAX_MOD_NAME_LEN);
|
2018-02-26 12:21:50 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-26 13:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
sprintf(mod_filename, "mod_%s", mod_name);
|
2018-02-26 09:04:53 +01:00
|
|
|
|
2018-02-26 12:21:50 +01:00
|
|
|
/* we have to load the module dynamically */
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
filename = cw_filename(mod_path, mod_filename, ".so");
|
|
|
|
if (filename == NULL)
|
2018-02-26 12:21:50 +01:00
|
|
|
return NULL;
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
cw_dbg(DBG_MOD, "MOD: loading module from file: %s", filename);
|
|
|
|
|
2018-02-26 09:04:53 +01:00
|
|
|
/* Open the DLL */
|
2018-03-26 13:21:42 +02:00
|
|
|
handle = dlopen(filename, RTLD_NOW);
|
|
|
|
|
|
|
|
if (!handle) {
|
|
|
|
cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
|
2018-02-26 00:19:44 +01:00
|
|
|
goto errX;
|
2018-02-25 19:12:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
mod = dlsym(handle, mod_filename);
|
|
|
|
if (mod == NULL) {
|
|
|
|
cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
|
2018-02-26 00:19:44 +01:00
|
|
|
goto errX;
|
|
|
|
}
|
2018-02-25 19:12:28 +01:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
mod->dll_handle = handle;
|
|
|
|
|
|
|
|
if (!mavl_add_ptr(mods_loaded, mod)) {
|
2018-02-26 14:13:57 +01:00
|
|
|
dlclose(handle);
|
2018-03-26 13:21:42 +02:00
|
|
|
cw_log(LOG_ERR, "Can' add module %s", mod_name);
|
2018-02-26 14:13:57 +01:00
|
|
|
goto errX;
|
|
|
|
}
|
2018-03-26 13:21:42 +02:00
|
|
|
cw_dbg(DBG_MOD, "MOD: %s sucessfull loaded, calling init now.", filename);
|
|
|
|
mod->init(mod, global_cfg, role);
|
|
|
|
errX:
|
2018-02-26 00:19:44 +01:00
|
|
|
free(filename);
|
2018-02-26 09:04:53 +01:00
|
|
|
return mod;
|
2018-02-26 14:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
static struct mlist *mods_list = NULL;
|
2018-02-26 14:13:57 +01:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_Mod *cw_mod_add_to_list(struct cw_Mod *mod)
|
|
|
|
{
|
2018-03-11 10:34:20 +01:00
|
|
|
mlistelem_t *elem;
|
2018-03-26 13:21:42 +02:00
|
|
|
if (!mods_list) {
|
|
|
|
mods_list = mlist_create(mod_cmp_mlist, NULL, sizeof(struct cw_Mod *));
|
|
|
|
if (!mods_list) {
|
|
|
|
cw_log(LOG_ERROR, "Can't init mods_list");
|
2018-02-26 14:13:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 13:21:42 +02:00
|
|
|
|
|
|
|
elem = mlist_append(mods_list, &mod);
|
2018-03-11 10:34:20 +01:00
|
|
|
if (elem == NULL)
|
|
|
|
return NULL;
|
|
|
|
return mlistelem_dataptr(elem);
|
2018-02-26 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
struct cw_Mod *cw_mod_detect(struct conn *conn,
|
|
|
|
uint8_t * rawmsg, int len,
|
|
|
|
int elems_len, struct sockaddr *from, int mode)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct mlistelem *e;
|
|
|
|
|
|
|
|
if (mods_list == NULL)
|
2018-02-26 14:44:27 +01:00
|
|
|
return MOD_NULL;
|
2018-03-06 03:08:14 +01:00
|
|
|
|
2018-03-11 10:34:20 +01:00
|
|
|
|
2018-03-26 13:21:42 +02:00
|
|
|
mlist_foreach(e, mods_list) {
|
|
|
|
|
|
|
|
struct cw_Mod *mod = *(struct cw_Mod **) (mlistelem_dataptr(e)); /* = e->data; */
|
|
|
|
cw_dbg(DBG_MOD, "Checking mod: %s", mod->name);
|
|
|
|
|
2018-02-26 18:28:12 +01:00
|
|
|
/* if there is no detect method, skip */
|
|
|
|
if (!mod->detect)
|
|
|
|
continue;
|
2018-03-26 13:21:42 +02:00
|
|
|
|
|
|
|
if (mod->detect(conn, rawmsg, len, elems_len, from, mode)) {
|
2018-02-26 18:28:12 +01:00
|
|
|
return mod;
|
|
|
|
}
|
2018-02-26 14:44:27 +01:00
|
|
|
}
|
2018-02-26 18:28:12 +01:00
|
|
|
return MOD_NULL;
|
2018-02-26 14:13:57 +01:00
|
|
|
}
|