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:
7u83@mail.ru 2018-03-26 11:21:42 +00:00
parent 4718bcf3bb
commit 00b51bacf9
2 changed files with 104 additions and 103 deletions

View File

@ -33,25 +33,26 @@
#include "cw.h" #include "cw.h"
#include "cw/msgset.h" #include "cw/msgset.h"
/*
static void (*actions_registered_cb) (struct cw_Mod * capwap, struct cw_Mod * bindings, static void (*actions_registered_cb) (struct cw_Mod * capwap, struct cw_Mod * bindings,
struct cw_actiondef * actions) = NULL; struct cw_actiondef * actions) = NULL;
*/
/*
void mod_set_actions_registered_cb(void (*fun) void mod_set_actions_registered_cb(void (*fun)
(struct cw_Mod *, struct cw_Mod *, (struct cw_Mod *, struct cw_Mod *,
struct cw_actiondef *)) struct cw_actiondef *))
{ {
actions_registered_cb = fun; actions_registered_cb = fun;
} }
*/
struct cache_item { struct cache_item {
const char *capwap; const char *capwap;
const char *bindings; const char *bindings;
struct cw_MsgSet * msgset; struct cw_MsgSet *msgset;
}; };
static struct mavl *msgset_cache = NULL; 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 * mod_null is a dummy mod
*/ */
struct cw_Mod mod_null = { struct cw_Mod mod_null = {
"none", /* name */ "none", /* name */
NULL, /* init */ NULL, /* init */
NULL, /* init_config */ NULL, /* init_config */
NULL, /* detect */ NULL, /* detect */
NULL /* data */ 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_MsgSet *cw_mod_get_msg_set(struct conn *conn,
struct cw_Mod * capwap_mod, struct cw_Mod *bindings_mod) struct cw_Mod *capwap_mod,
struct cw_Mod *bindings_mod)
{ {
struct cache_item search; struct cache_item search;
struct cache_item * cached_set; struct cache_item *cached_set;
struct cw_MsgSet * set; struct cw_MsgSet *set;
if (!msgset_cache) { if (!msgset_cache) {
msgset_cache = mavl_create(cmp, NULL,1312); msgset_cache = mavl_create(cmp, NULL, 1312);
if (!msgset_cache) { if (!msgset_cache) {
cw_log(LOG_ERR, "Can't initialize msgset cache: %s", cw_log(LOG_ERR, "Can't initialize msgset cache: %s",
strerror(errno)); 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); cached_set = mavl_get_ptr(msgset_cache, &search);
if (cached_set) { 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; return cached_set->msgset;
} }
@ -130,7 +132,7 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
return NULL; return NULL;
} }
memset(cached_set, 0, sizeof(struct cache_item)); memset(cached_set, 0, sizeof(struct cache_item));
set = cw_msgset_create(); set = cw_msgset_create();
if (!set) { if (!set) {
free(cached_set); free(cached_set);
@ -138,16 +140,17 @@ struct cw_MsgSet *cw_mod_get_msg_set(struct conn *conn,
strerror(errno)); strerror(errno));
return NULL; 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) { if (capwap_mod) {
cached_set->capwap = capwap_mod->name; 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) { if (bindings_mod) {
cached_set->bindings = bindings_mod->name; 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 mavl to store modules */
static struct mavl * mods_loaded = NULL; static struct mavl *mods_loaded = NULL;
static int mod_cmp_mavl(const void *e1, const void *e2){ 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); const struct cw_Mod *m1 = *((const struct cw_Mod **) e1);
return strcmp(m1->name,m2->name); 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){ static int mod_cmp_mlist(const void *e1, const void *e2)
const struct cw_Mod * m1 = e1; {
const struct cw_Mod * m2 = e2; const struct cw_Mod *m1 = e1;
return strcmp(m1->name,m2->name); 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 * @brief Set module path, where to search for modules
* @param path Path to search * @param path Path to search
*/ */
void cw_mod_set_path(const char * path){ void cw_mod_set_path(const char *path)
{
mod_path = path; mod_path = path;
} }
@ -193,18 +199,18 @@ void cw_mod_set_path(const char * path){
* @param mod_name Name of the module * @param mod_name Name of the module
* @return a pointer to the module interface * @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 search;
struct cw_Mod * mod; struct cw_Mod *mod;
char mod_filename[CW_MOD_MAX_MOD_NAME_LEN+5]; char mod_filename[CW_MOD_MAX_MOD_NAME_LEN + 5];
char * filename; char *filename;
void * handle; void *handle;
struct cw_Mod * (*mod_get_interface)();
/* if modlist is not initialized, initialize ... */ /* if modlist is not initialized, initialize ... */
if (mods_loaded==NULL){ if (mods_loaded == NULL) {
mods_loaded=mavl_create(mod_cmp_mavl,NULL,1312); mods_loaded = mavl_create(mod_cmp_mavl, NULL, 1312);
if (mods_loaded==NULL){ if (mods_loaded == NULL) {
cw_log(LOG_ERROR, "Can't init modlist, no memory"); cw_log(LOG_ERROR, "Can't init modlist, no memory");
return NULL; 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 /* Search for the module in mods_loaded, to see if it is
* already loaded or was statically linked */ * 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); cw_dbg(DBG_MOD, "MOD: Load module '%s'", mod_name);
if (mod){
cw_dbg(DBG_MOD,"MOD: Module already loaded '%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; return mod;
} }
if (strlen(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)", cw_log(LOG_ERROR, "Mod name too long: %s (max allowed = %d)",
mod_name,CW_MOD_MAX_MOD_NAME_LEN); mod_name, CW_MOD_MAX_MOD_NAME_LEN);
return NULL; return NULL;
} }
sprintf(mod_filename,"mod_%s",mod_name); sprintf(mod_filename, "mod_%s", mod_name);
/* we have to load the module dynamically */ /* we have to load the module dynamically */
filename = cw_filename(mod_path,mod_filename,".so"); filename = cw_filename(mod_path, mod_filename, ".so");
if (filename==NULL) if (filename == NULL)
return NULL; return NULL;
cw_dbg(DBG_MOD, "MOD: loading module from file: %s", filename); cw_dbg(DBG_MOD, "MOD: loading module from file: %s", filename);
/* Open the DLL */ /* Open the DLL */
handle = dlopen(filename,RTLD_NOW); handle = dlopen(filename, RTLD_NOW);
if (!handle){ if (!handle) {
cw_log(LOG_ERROR,"Failed to load module: %s",dlerror()); cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
goto errX; goto errX;
} }
mod = dlsym(handle, mod_filename);
mod_get_interface = (struct cw_Mod*(*)()) dlsym(handle,mod_filename); if (mod == NULL) {
cw_log(LOG_ERROR, "Failed to load module: %s", dlerror());
if (!mod_get_interface){
cw_log(LOG_ERROR,"Failed to load module: %s",dlerror());
goto errX; goto errX;
} }
mod = mod_get_interface(); mod->dll_handle = handle;
mod->dll_handle=handle;
if (!mavl_add_ptr(mods_loaded, mod)) {
if (!mavl_add_ptr(mods_loaded,mod)){
dlclose(handle); dlclose(handle);
cw_log(LOG_ERR,"Can' add module %s",mod_name); cw_log(LOG_ERR, "Can' add module %s", mod_name);
goto errX; goto errX;
} }
cw_dbg(DBG_MOD, "MOD: %s sucessfull loaded, calling init now.",filename); cw_dbg(DBG_MOD, "MOD: %s sucessfull loaded, calling init now.", filename);
mod->init(mod,global_cfg,role); mod->init(mod, global_cfg, role);
errX: errX:
free(filename); free(filename);
return mod; 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; mlistelem_t *elem;
if (!mods_list){ if (!mods_list) {
mods_list = mlist_create(mod_cmp_mlist,NULL,sizeof(struct cw_Mod *)); mods_list = mlist_create(mod_cmp_mlist, NULL, sizeof(struct cw_Mod *));
if (!mods_list){ if (!mods_list) {
cw_log(LOG_ERROR,"Can't init mods_list"); cw_log(LOG_ERROR, "Can't init mods_list");
return 0; return 0;
} }
} }
elem = mlist_append(mods_list,&mod); elem = mlist_append(mods_list, &mod);
if (elem == NULL) if (elem == NULL)
return NULL; return NULL;
return mlistelem_dataptr(elem); return mlistelem_dataptr(elem);
/*return mlist_append(mods_list,mod)->data;*/
} }
struct cw_Mod * cw_mod_detect(struct conn *conn, struct cw_Mod *cw_mod_detect(struct conn *conn,
uint8_t * rawmsg, int len, uint8_t * rawmsg, int len,
int elems_len, struct sockaddr *from, int elems_len, struct sockaddr *from, int mode)
int mode){ {
struct mlistelem * e; struct mlistelem *e;
if (mods_list==NULL) if (mods_list == NULL)
return MOD_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 there is no detect method, skip */
if (!mod->detect) if (!mod->detect)
continue; continue;
if ( mod->detect(conn,rawmsg,len,elems_len,from,mode) ){ if (mod->detect(conn, rawmsg, len, elems_len, from, mode)) {
return mod; return mod;
} }
} }

View File

@ -31,9 +31,6 @@
#include "conn.h" #include "conn.h"
struct cw_actiondef;
enum { enum {
CW_MOD_MODE_CAPWAP, CW_MOD_MODE_CAPWAP,
CW_MOD_MODE_BINDINGS CW_MOD_MODE_BINDINGS
@ -85,12 +82,15 @@ extern struct cw_Mod mod_null;
#define MOD_NULL (&mod_null) #define MOD_NULL (&mod_null)
/*
struct cw_actiondef * mod_cache_add(struct conn *conn,struct cw_Mod *c, struct cw_Mod *b); 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) #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 *)); void mod_set_actions_registered_cb(void (*fun) (struct cw_Mod *, struct cw_Mod *, struct cw_actiondef *));
*/
extern int mod_caching; extern int mod_caching;