uses exported cw_Mod struct from dll
using an exported function from dlsym which is a void * ptr is not portable FossilOrigin-Name: c3abfc7581aba9f8e050cdbcb0a452a74fbb076c65a5a243aa8e2483a098129d
This commit is contained in:
parent
4718bcf3bb
commit
00b51bacf9
199
src/cw/mod.c
199
src/cw/mod.c
@ -33,25 +33,26 @@
|
||||
#include "cw.h"
|
||||
#include "cw/msgset.h"
|
||||
|
||||
/*
|
||||
static void (*actions_registered_cb) (struct cw_Mod * capwap, struct cw_Mod * bindings,
|
||||
struct cw_actiondef * actions) = NULL;
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
void mod_set_actions_registered_cb(void (*fun)
|
||||
(struct cw_Mod *, struct cw_Mod *,
|
||||
struct cw_actiondef *))
|
||||
{
|
||||
actions_registered_cb = fun;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
struct cache_item {
|
||||
const char *capwap;
|
||||
const char *bindings;
|
||||
struct cw_MsgSet * msgset;
|
||||
struct cw_MsgSet *msgset;
|
||||
};
|
||||
static struct mavl *msgset_cache = NULL;
|
||||
|
||||
@ -66,12 +67,11 @@ static int mod_null_register_actions(struct cw_actiondef *def, int mode)
|
||||
* mod_null is a dummy mod
|
||||
*/
|
||||
struct cw_Mod mod_null = {
|
||||
"none", /* name */
|
||||
NULL, /* init */
|
||||
NULL, /* init_config */
|
||||
NULL, /* detect */
|
||||
NULL /* data */
|
||||
|
||||
"none", /* name */
|
||||
NULL, /* init */
|
||||
NULL, /* init_config */
|
||||
NULL, /* detect */
|
||||
NULL /* data */
|
||||
};
|
||||
|
||||
|
||||
@ -96,15 +96,16 @@ struct cw_actiondef *mod_cache_get(const char *capwap, const char *bindings)
|
||||
}
|
||||
|
||||
|
||||
struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
struct cw_Mod * capwap_mod, struct cw_Mod *bindings_mod)
|
||||
struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
struct cw_Mod *capwap_mod,
|
||||
struct cw_Mod *bindings_mod)
|
||||
{
|
||||
struct cache_item search;
|
||||
struct cache_item * cached_set;
|
||||
struct cw_MsgSet * set;
|
||||
struct cache_item *cached_set;
|
||||
struct cw_MsgSet *set;
|
||||
|
||||
if (!msgset_cache) {
|
||||
msgset_cache = mavl_create(cmp, NULL,1312);
|
||||
msgset_cache = mavl_create(cmp, NULL, 1312);
|
||||
if (!msgset_cache) {
|
||||
cw_log(LOG_ERR, "Can't initialize msgset cache: %s",
|
||||
strerror(errno));
|
||||
@ -118,7 +119,8 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
|
||||
cached_set = mavl_get_ptr(msgset_cache, &search);
|
||||
if (cached_set) {
|
||||
cw_dbg(DBG_INFO, "Using cached message set for %s,%s", capwap_mod->name, bindings_mod->name);
|
||||
cw_dbg(DBG_INFO, "Using cached message set for %s,%s", capwap_mod->name,
|
||||
bindings_mod->name);
|
||||
return cached_set->msgset;
|
||||
}
|
||||
|
||||
@ -130,7 +132,7 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
return NULL;
|
||||
}
|
||||
memset(cached_set, 0, sizeof(struct cache_item));
|
||||
|
||||
|
||||
set = cw_msgset_create();
|
||||
if (!set) {
|
||||
free(cached_set);
|
||||
@ -138,16 +140,17 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
cached_set->msgset=set;
|
||||
cached_set->msgset = set;
|
||||
|
||||
|
||||
|
||||
cw_dbg(DBG_INFO, "Loading message set for %s,%s", capwap_mod->name, bindings_mod->name);
|
||||
cw_dbg(DBG_INFO, "Loading message set for %s,%s", capwap_mod->name,
|
||||
bindings_mod->name);
|
||||
|
||||
|
||||
if (capwap_mod) {
|
||||
cached_set->capwap = capwap_mod->name;
|
||||
capwap_mod->register_messages(cached_set->msgset,CW_MOD_MODE_CAPWAP);
|
||||
capwap_mod->register_messages(cached_set->msgset, CW_MOD_MODE_CAPWAP);
|
||||
}
|
||||
if (bindings_mod) {
|
||||
cached_set->bindings = bindings_mod->name;
|
||||
@ -163,28 +166,31 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
|
||||
|
||||
|
||||
/* static mavl to store modules */
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char * mod_path="";
|
||||
static const char *mod_path = "";
|
||||
|
||||
/**
|
||||
* @brief Set module path, where to search for modules
|
||||
* @param path Path to search
|
||||
*/
|
||||
void cw_mod_set_path(const char * path){
|
||||
void cw_mod_set_path(const char *path)
|
||||
{
|
||||
mod_path = path;
|
||||
}
|
||||
|
||||
@ -193,18 +199,18 @@ void cw_mod_set_path(const char * path){
|
||||
* @param mod_name Name of the module
|
||||
* @return a pointer to the module interface
|
||||
*/
|
||||
struct cw_Mod * cw_mod_load(const char * mod_name, mavl_t global_cfg, int role){
|
||||
struct cw_Mod *cw_mod_load(const char *mod_name, mavl_t global_cfg, int role)
|
||||
{
|
||||
struct cw_Mod search;
|
||||
struct cw_Mod * mod;
|
||||
char mod_filename[CW_MOD_MAX_MOD_NAME_LEN+5];
|
||||
char * filename;
|
||||
void * handle;
|
||||
struct cw_Mod * (*mod_get_interface)();
|
||||
struct cw_Mod *mod;
|
||||
char mod_filename[CW_MOD_MAX_MOD_NAME_LEN + 5];
|
||||
char *filename;
|
||||
void *handle;
|
||||
|
||||
/* if modlist is not initialized, initialize ... */
|
||||
if (mods_loaded==NULL){
|
||||
mods_loaded=mavl_create(mod_cmp_mavl,NULL,1312);
|
||||
if (mods_loaded==NULL){
|
||||
if (mods_loaded == NULL) {
|
||||
mods_loaded = mavl_create(mod_cmp_mavl, NULL, 1312);
|
||||
if (mods_loaded == NULL) {
|
||||
cw_log(LOG_ERROR, "Can't init modlist, no memory");
|
||||
return NULL;
|
||||
}
|
||||
@ -212,109 +218,104 @@ struct cw_Mod * cw_mod_load(const char * mod_name, mavl_t global_cfg, int role){
|
||||
|
||||
/* Search for the module in mods_loaded, to see if it is
|
||||
* already loaded or was statically linked */
|
||||
|
||||
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);
|
||||
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);
|
||||
return mod;
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
sprintf(mod_filename,"mod_%s",mod_name);
|
||||
|
||||
|
||||
sprintf(mod_filename, "mod_%s", mod_name);
|
||||
|
||||
/* we have to load the module dynamically */
|
||||
|
||||
filename = cw_filename(mod_path,mod_filename,".so");
|
||||
if (filename==NULL)
|
||||
filename = cw_filename(mod_path, mod_filename, ".so");
|
||||
if (filename == NULL)
|
||||
return NULL;
|
||||
|
||||
cw_dbg(DBG_MOD, "MOD: loading module from file: %s", filename);
|
||||
|
||||
/* Open the DLL */
|
||||
handle = dlopen(filename,RTLD_NOW);
|
||||
|
||||
if (!handle){
|
||||
cw_log(LOG_ERROR,"Failed to load module: %s",dlerror());
|
||||
handle = dlopen(filename, RTLD_NOW);
|
||||
|
||||
if (!handle) {
|
||||
cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
|
||||
goto errX;
|
||||
}
|
||||
|
||||
|
||||
mod_get_interface = (struct cw_Mod*(*)()) dlsym(handle,mod_filename);
|
||||
|
||||
if (!mod_get_interface){
|
||||
cw_log(LOG_ERROR,"Failed to load module: %s",dlerror());
|
||||
mod = dlsym(handle, mod_filename);
|
||||
if (mod == NULL) {
|
||||
cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
|
||||
goto errX;
|
||||
}
|
||||
|
||||
mod = mod_get_interface();
|
||||
mod->dll_handle=handle;
|
||||
|
||||
if (!mavl_add_ptr(mods_loaded,mod)){
|
||||
mod->dll_handle = handle;
|
||||
|
||||
if (!mavl_add_ptr(mods_loaded, mod)) {
|
||||
dlclose(handle);
|
||||
cw_log(LOG_ERR,"Can' add module %s",mod_name);
|
||||
cw_log(LOG_ERR, "Can' add module %s", mod_name);
|
||||
goto errX;
|
||||
}
|
||||
cw_dbg(DBG_MOD, "MOD: %s sucessfull loaded, calling init now.",filename);
|
||||
mod->init(mod,global_cfg,role);
|
||||
errX:
|
||||
cw_dbg(DBG_MOD, "MOD: %s sucessfull loaded, calling init now.", filename);
|
||||
mod->init(mod, global_cfg, role);
|
||||
errX:
|
||||
free(filename);
|
||||
return mod;
|
||||
}
|
||||
|
||||
|
||||
static struct mlist * mods_list = NULL;
|
||||
static struct mlist *mods_list = NULL;
|
||||
|
||||
struct cw_Mod * cw_mod_add_to_list(struct cw_Mod * mod ){
|
||||
struct cw_Mod *cw_mod_add_to_list(struct cw_Mod *mod)
|
||||
{
|
||||
mlistelem_t *elem;
|
||||
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");
|
||||
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");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
elem = mlist_append(mods_list,&mod);
|
||||
|
||||
elem = mlist_append(mods_list, &mod);
|
||||
if (elem == NULL)
|
||||
return NULL;
|
||||
return mlistelem_dataptr(elem);
|
||||
|
||||
/*return mlist_append(mods_list,mod)->data;*/
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
return MOD_NULL;
|
||||
|
||||
|
||||
mlist_foreach(e,mods_list){
|
||||
/* /// 1312 */
|
||||
struct cw_Mod * mod = *(struct cw_Mod**)(mlistelem_dataptr(e)); /* = e->data;*/
|
||||
cw_dbg(DBG_MOD,"Checking mod: %s",mod->name);
|
||||
|
||||
/*printf("Got the mod %p\n",mod);*/
|
||||
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);
|
||||
|
||||
/* if there is no detect method, skip */
|
||||
if (!mod->detect)
|
||||
continue;
|
||||
|
||||
if ( mod->detect(conn,rawmsg,len,elems_len,from,mode) ){
|
||||
|
||||
if (mod->detect(conn, rawmsg, len, elems_len, from, mode)) {
|
||||
return mod;
|
||||
}
|
||||
}
|
||||
|
@ -31,9 +31,6 @@
|
||||
#include "conn.h"
|
||||
|
||||
|
||||
struct cw_actiondef;
|
||||
|
||||
|
||||
enum {
|
||||
CW_MOD_MODE_CAPWAP,
|
||||
CW_MOD_MODE_BINDINGS
|
||||
@ -85,12 +82,15 @@ extern struct cw_Mod mod_null;
|
||||
|
||||
#define MOD_NULL (&mod_null)
|
||||
|
||||
/*
|
||||
struct cw_actiondef * mod_cache_add(struct conn *conn,struct cw_Mod *c, struct cw_Mod *b);
|
||||
|
||||
*/
|
||||
|
||||
#define mod_init_config(mod,cfg) (mod->init_config ? mod->init_config(cfg):1)
|
||||
|
||||
/*
|
||||
void mod_set_actions_registered_cb(void (*fun) (struct cw_Mod *, struct cw_Mod *, struct cw_actiondef *));
|
||||
*/
|
||||
|
||||
extern int mod_caching;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user