Mor work on dynamic loading of modules

FossilOrigin-Name: b02170339426d0c9efc42d700e891a90f7ddb0961a67b679909a1c929e1501e1
This commit is contained in:
7u83@mail.ru 2018-02-26 08:04:53 +00:00
parent a71b86b125
commit 6316dd6576
7 changed files with 75 additions and 33 deletions

View File

@ -3,9 +3,9 @@
<Project Name="ac" Path="ac.project" Active="No"/> <Project Name="ac" Path="ac.project" Active="No"/>
<Project Name="wtp" Path="wtp.project" Active="No"/> <Project Name="wtp" Path="wtp.project" Active="No"/>
<Project Name="mod_cipwap" Path="mod_cipwap.project" Active="No"/> <Project Name="mod_cipwap" Path="mod_cipwap.project" Active="No"/>
<Project Name="mod_capwap" Path="mod_capwap.project" Active="Yes"/> <Project Name="mod_capwap" Path="mod_capwap.project" Active="No"/>
<Project Name="mod_cisco" Path="mod_cisco.project" Active="No"/> <Project Name="mod_cisco" Path="mod_cisco.project" Active="No"/>
<Project Name="libcw" Path="libcw.project" Active="No"/> <Project Name="libcw" Path="libcw.project" Active="Yes"/>
<Project Name="mod_capwap80211" Path="mod_capwap80211.project" Active="No"/> <Project Name="mod_capwap80211" Path="mod_capwap80211.project" Active="No"/>
<Project Name="mod_fortinet" Path="mod_fortinet.project" Active="No"/> <Project Name="mod_fortinet" Path="mod_fortinet.project" Active="No"/>
<BuildMatrix> <BuildMatrix>

View File

@ -38,7 +38,7 @@ uint8_t conf_macaddress_len = 0;
long conf_strict_capwap = 1; long conf_strict_capwap = 1;
long conf_strict_headers = 0; long conf_strict_headers = 0;
char *conf_capwap_mode_str = NULL; //char *conf_capwap_mode_str = NULL;
int conf_capwap_mode = CW_MODE_CAPWAP; int conf_capwap_mode = CW_MODE_CAPWAP;
@ -540,7 +540,7 @@ static int conf_read_dbg_level(cfg_t * cfg)
struct cw_Mod ** conf_mods; //[10]; struct cw_Mod ** conf_mods; //[10];
char *conf_mods_dir = NULL;
static int init_mods() static int init_mods()
@ -571,20 +571,29 @@ static int conf_read_mods(cfg_t *cfg){
n = cfg_size(cfg,CFG_ENTRY_MODS); n = cfg_size(cfg,CFG_ENTRY_MODS);
conf_mods = malloc(sizeof(struct cw_Mod *)*(n+1)); conf_mods = malloc(sizeof(struct cw_Mod *)*(n+1));
cw_dbg(DBG_INFO,"Mods directory: %s",conf_mods_dir);
for (i=0; i < n; i++){ for (i=0; i < n; i++){
char *modname = cfg_getnstr(cfg, CFG_ENTRY_MODS, i); char *modname = cfg_getnstr(cfg, CFG_ENTRY_MODS, i);
conf_mods[i] = NULL; //modload_ac(modname);
struct cw_Mod * mod = cw_mod_add_dynamic(conf_mods_dir,modname);
if (!mod)
return 0;
/* conf_mods[i] = NULL; //modload_ac(modname);
if (!conf_mods[i]){ if (!conf_mods[i]){
cw_log(LOG_ERR,"Can't load mod: %s",modname); cw_log(LOG_ERR,"Can't load mod: %s",modname);
return 0; return 0;
} }
*/
} }
conf_mods[i]=NULL;
return 1; return 1;
} }
/*
void conf_init_capwap_mode() void conf_init_capwap_mode()
{ {
if (conf_capwap_mode_str == NULL) if (conf_capwap_mode_str == NULL)
@ -600,7 +609,7 @@ void conf_init_capwap_mode()
} }
*/
int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *proto) int conf_parse_listen_addr(const char *addrstr, char *saddr, char *port, int *proto)
@ -700,6 +709,8 @@ int read_config(const char *filename)
cfg_opt_t opts[] = { cfg_opt_t opts[] = {
CFG_STR_LIST("mods", "{}", CFGF_NONE), CFG_STR_LIST("mods", "{}", CFGF_NONE),
CFG_SIMPLE_STR("mods_dir", &conf_mods_dir),
CFG_STR_LIST("dbg", "{}", CFGF_NONE), CFG_STR_LIST("dbg", "{}", CFGF_NONE),
CFG_STR_LIST("listen", "{}", CFGF_NONE), CFG_STR_LIST("listen", "{}", CFGF_NONE),
CFG_STR_LIST("mcast_groups", "{}", CFGF_NONE), CFG_STR_LIST("mcast_groups", "{}", CFGF_NONE),
@ -710,7 +721,7 @@ int read_config(const char *filename)
CFG_SIMPLE_BOOL("strict_capwap", &conf_strict_capwap), CFG_SIMPLE_BOOL("strict_capwap", &conf_strict_capwap),
CFG_SIMPLE_BOOL("strict_headers", &conf_strict_headers), CFG_SIMPLE_BOOL("strict_headers", &conf_strict_headers),
CFG_SIMPLE_BOOL("use_loopback", &conf_use_loopback), CFG_SIMPLE_BOOL("use_loopback", &conf_use_loopback),
CFG_SIMPLE_STR("capwap_mode", &conf_capwap_mode_str), // CFG_SIMPLE_STR("capwap_mode", &conf_capwap_mode_str),
#ifdef WITH_LWAPP #ifdef WITH_LWAPP
@ -817,7 +828,7 @@ int read_config(const char *filename)
init_mods(); init_mods();
conf_init_capwap_mode(); // conf_init_capwap_mode();
init_listen_addrs(); init_listen_addrs();

View File

@ -3,7 +3,8 @@ include ../CWConfig.mak
-include ../Config.local.mak -include ../Config.local.mak
LIBARCHDIR := ../../lib/$(ARCH) LIBDIR := ../../lib
LIBARCHDIR := $(LIBDIR)/$(ARCH)
OBJDIR := ../../obj/cw/$(ARCH) OBJDIR := ../../obj/cw/$(ARCH)
SNAME := $(LIBARCHDIR)/libcw.a SNAME := $(LIBARCHDIR)/libcw.a

View File

@ -144,9 +144,9 @@ struct cw_actiondef *mod_cache_add(struct conn *conn, struct cw_Mod *c, struct c
/* static mavl to store modules */ /* static mavl to store modules */
static struct mavl * modlist = NULL; static struct mavl * modlist = NULL;
static int mod_cmp(const void *e1, const void *e2){ static int mod_cmp(const void *e1, const void *e2){
struct cw_Mod * (*m1fn)() = e1; struct cw_Mod * m1 = e1;
struct cw_Mod * (*m2fn)() = e2; struct cw_Mod * m2 = e2;
return strcmp(m1fn()->name,m2fn()->name); return strcmp(m1->name,m2->name);
} }
@ -182,32 +182,50 @@ int cw_mod_add(struct cw_Mod * (*modfn)() ){
} }
int cw_mod_add_dynamic(const char * path, const char * mod_name){ struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * mod_name){
char * filename = malloc(strlen(path)+strlen(mod_name)+8); /* Search for the module in modlist, to see if it is
* already loaded or was statically linked */
struct cw_Mod search;
memset(&search,0,sizeof(search));
search.name=mod_name;
struct cw_Mod * mod;
mod = mavl_find(modlist,&search);
if (mod){
return mod;
}
int n = strlen(path)+strlen(mod_name)+8;
char * filename = malloc(n);
if (!filename) if (!filename)
return 0; return 0;
strcpy(filename,path); strcpy(filename,path);
strcat(filename,"/"); strcat(filename,"/");
strcat(filename,mod_name); strcat(filename,mod_name);
strcat(filename,".so"); strcat(filename,".so");
int rc=0; /* Open the DLL */
void * handle; void * handle;
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;
} }
void * ifu = dlsym(handle,"mod_get_interface"); struct cw_Mod * (*mod_get_interface)();
if (!ifu){ mod_get_interface = dlsym(handle,"mod_get_interface");
if (!mod_get_interface){
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 = mod_get_interface();
mod->init();
errX: errX:
free(filename); free(filename);
return rc; return mod;
} }

View File

@ -64,6 +64,11 @@ struct cw_Mod {
int (*register_actions) (struct cw_actiondef * def,int mode); int (*register_actions) (struct cw_actiondef * def,int mode);
struct cw_MsgSet * (*register_messages)(struct cw_MsgSet set, int mode); struct cw_MsgSet * (*register_messages)(struct cw_MsgSet set, int mode);
/**
* Handle returned by dlopen, if this module was loaded dynamically
* with dlopen */
void * handle;
}; };
@ -84,6 +89,6 @@ extern int mod_caching;
#define mod_set_caching(var) (mod_caching=var) #define mod_set_caching(var) (mod_caching=var)
#define mod_get_caching() (mod_caching) #define mod_get_caching() (mod_caching)
extern int cw_mod_add_dynamic(const char * path, const char * file); struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * file);
#endif #endif

View File

@ -10,7 +10,7 @@ CFLAGS = -fPIC -Wall -g -O0 -D_REENTRANT -DWITH_IPV6 $(COMPDEFS) -DWITH_RMAC_SUP
SRCS = $(OBJS:.o=.c) SRCS = $(OBJS:.o=.c)
all: $(SNAME) $(DNAME) all: $(SNAME) $(DNAME) $(MODNAME)
$(OBJDIR)/%.o:%.c $(OBJDIR)/%.o:%.c
@mkdir -p $(OBJDIR) @mkdir -p $(OBJDIR)
@ -19,20 +19,25 @@ $(OBJDIR)/%.o:%.c
$(SNAME) : $(OBJS) $(MODOBJS) $(SNAME) : $(OBJS) $(MODOBJS)
@mkdir -p $(LIBDIR) @mkdir -p $(LIBARCHDIR)
@echo " $(AR) $(SNAME)" @echo " $(AR) $(SNAME)"
@$(AR) rcs $(SNAME) $(OBJS) $(MODOBJS) @$(AR) rcs $(SNAME) $(OBJS) $(MODOBJS)
$(DNAME) : $(OBJS) $(MODOBJS) $(DNAME) : $(OBJS) $(MODOBJS)
@mkdir -p $(LIBDIR) @mkdir -p $(LIBARCHDIR)
@echo " $(CC) $(DNAME)" @echo " $(CC) $(DNAME)"
@$(CC) $(LDFLAGS) -shared -o $(DNAME) $(OBJS) $(MODOBJS) $(LIBS) @$(CC) -L$(LIBARCHDIR) $(LDFLAGS) -shared -o $(DNAME) $(OBJS) $(MODOBJS) $(SLIBS) $(LIBS)
# $(CC) -L$(LIBDIR) $(OBJS) $(MODOBJS) $(SLIBS) -v -shared -o ../../../lib/actube/capwap.so $(MODNAME) : $(DNAME)
cp $(DNAME) $(MODNAME)
# $(CC) -L$(LIBARCHDIR) $(OBJS) $(MODOBJS) $(SLIBS) -v -shared -o ../../../lib/actube/capwap.so
clean: clean:
rm -rf $(OBJDIR) rm -rf $(OBJDIR)
rm -f $(SNAME) rm -f $(SNAME)
rm -f $(DNAME)
rm -f $(MODNAME)

View File

@ -19,10 +19,12 @@ OBJS=\
capwap_in_session_id.o \ capwap_in_session_id.o \
LIBDIR := ../../../lib/$(ARCH) LIBDIR := ../../../lib
LIBARCHDIR := $(LIBDIR)/$(ARCH)
OBJDIR := ../../../obj/mod_capwap/$(ARCH) OBJDIR := ../../../obj/mod_capwap/$(ARCH)
SNAME := $(LIBDIR)/libcapwap.a SNAME := $(LIBARCHDIR)/libcapwap.a
DNAME := $(LIBDIR)/capwap.so DNAME := $(LIBARCHDIR)/capwap.so
MODNAME := $(LIBDIR)/capwap.so
SLIBS := -lcw SLIBS := -lcw
include ../Mod.mak include ../Mod.mak