diff --git a/libcw.project b/libcw.project index 001da76a..a3cc5a86 100644 --- a/libcw.project +++ b/libcw.project @@ -306,6 +306,7 @@ + diff --git a/src/ac/ac_main.c b/src/ac/ac_main.c index e852a35d..fb9d24aa 100644 --- a/src/ac/ac_main.c +++ b/src/ac/ac_main.c @@ -93,15 +93,11 @@ extern struct cw_Mod * cw_get_mod_ac(const char *name); extern void test_sets(); +#include "cw/file.h" int main(int argc, char *argv[]) { - - - - - int rc = 0; /* parse arguments */ @@ -124,7 +120,8 @@ int main(int argc, char *argv[]) developers is turned on ;) */ DBGX("Attention! %s", "DBG X is ON!"); - cw_mod_add_dynamic("../../lib/actube","capwap"); + cw_mod_set_mod_path("../../lib/actube"); + cw_mod_load("capwap"); exit(0); diff --git a/src/ac/conf.c b/src/ac/conf.c index 1d67ee1a..9e1ed22a 100644 --- a/src/ac/conf.c +++ b/src/ac/conf.c @@ -573,11 +573,15 @@ static int conf_read_mods(cfg_t *cfg){ conf_mods = malloc(sizeof(struct cw_Mod *)*(n+1)); cw_dbg(DBG_INFO,"Mods directory: %s",conf_mods_dir); + cw_mod_set_mod_path(conf_mods_dir); for (i=0; i < n; i++){ char *modname = cfg_getnstr(cfg, CFG_ENTRY_MODS, i); - - struct cw_Mod * mod = cw_mod_add_dynamic(conf_mods_dir,modname); + +printf("Modname: %s\n",modname); + + + struct cw_Mod * mod = cw_mod_load(modname); if (!mod) return 0; diff --git a/src/cw/conn_process_packet.c b/src/cw/conn_process_packet.c index b25dac44..c432b7b4 100644 --- a/src/cw/conn_process_packet.c +++ b/src/cw/conn_process_packet.c @@ -191,7 +191,7 @@ static struct cw_actiondef *load_mods(struct conn *conn, uint8_t * rawmsg, int l { struct cw_Mod *cmod = - detect_mod(conn, rawmsg, len, elems_len, from, MOD_MODE_CAPWAP); + detect_mod(conn, rawmsg, len, elems_len, from, CW_MOD_MODE_CAPWAP); if (cmod == MOD_NULL) { cw_dbg(DBG_MSG_ERR, "Can't find mod to handle connection from %s, discarding message", diff --git a/src/cw/mod.c b/src/cw/mod.c index ca391e22..2672a4e6 100644 --- a/src/cw/mod.c +++ b/src/cw/mod.c @@ -125,7 +125,7 @@ struct cw_actiondef *mod_cache_add(struct conn *conn, struct cw_Mod *c, struct c memset(i, 0, sizeof(struct cache_item)); if (c) { i->capwap = c->name; - c->register_actions(&(i->actions), MOD_MODE_CAPWAP); + c->register_actions(&(i->actions), CW_MOD_MODE_CAPWAP); } if (b) { i->bindings = b->name; @@ -149,40 +149,38 @@ static int mod_cmp(const void *e1, const void *e2){ return strcmp(m1->name,m2->name); } +static const char * mod_path="./"; -/** - * @brief Load a module - * @param name Name of the module - * @return pointer to the module structure or NULL if the - * module cannot be loaded. - */ -struct cw_Mod * cw_mod_get(const char *name) -{ - -/*int i; - for (i=0; modlist[i];i++){ - - struct cw_Mod * m = modlist[i](); - if (strcmp(m->name,name)==0) - return m; - } - return NULL; -*/ +void cw_mod_set_mod_path(const char * path){ + mod_path = path; } -int cw_mod_add(struct cw_Mod * (*modfn)() ){ +static int cw_mod_add(struct cw_Mod * mod ){ if (modlist == NULL){ modlist = mavl_create(mod_cmp,NULL); if (modlist==NULL){ return 0; } } - mavl_add(modlist,modfn); + //mavl_add(modlist,modfn); return 1; } - -struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * mod_name){ +/** + * @brief Load a module + * @param path + * @param mod_name + * @return + */ +struct cw_Mod * cw_mod_load(const char * mod_name){ + /* if modlist is not initialized, initialize ... */ + if (modlist==NULL){ + modlist=mavl_create(mod_cmp,NULL); + if (modlist==NULL){ + cw_log(LOG_ERROR, "Can't init modlist, no memory"); + return NULL; + } + } /* Search for the module in modlist, to see if it is * already loaded or was statically linked */ @@ -195,17 +193,20 @@ struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * 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); + return NULL; + } + + char mod_filename[CW_MOD_MAX_MOD_NAME_LEN+5]; + sprintf(mod_filename,"mod_%s",mod_name); - int n = strlen(path)+strlen(mod_name)+8; - - char * filename = malloc(n); - - if (!filename) - return 0; - strcpy(filename,path); - strcat(filename,"/"); - strcat(filename,mod_name); - strcat(filename,".so"); + /* we have to load the module dynamically */ + char * filename; + filename = cw_filename(mod_path,mod_filename,".so"); + if (filename==NULL) + return NULL; /* Open the DLL */ void * handle; @@ -217,7 +218,9 @@ struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * mod_name){ } struct cw_Mod * (*mod_get_interface)(); + mod_get_interface = dlsym(handle,"mod_get_interface"); + if (!mod_get_interface){ cw_log(LOG_ERROR,"Failed to load module: %s",dlerror()); goto errX; diff --git a/src/cw/mod.h b/src/cw/mod.h index f18bbee6..b9e1f002 100644 --- a/src/cw/mod.h +++ b/src/cw/mod.h @@ -35,7 +35,7 @@ struct cw_actiondef; enum { - MOD_MODE_CAPWAP, + CW_MOD_MODE_CAPWAP, MOD_MODE_BINDINGS }; @@ -91,6 +91,9 @@ extern int mod_caching; #define mod_set_caching(var) (mod_caching=var) #define mod_get_caching() (mod_caching) -struct cw_Mod * cw_mod_add_dynamic(const char * path, const char * file); +struct cw_Mod * cw_mod_load(const char * mod_name); + +#define CW_MOD_MAX_MOD_NAME_LEN 128 +#define CW_MOD_INTERFACE_FUNCTION_NAME_SUFFIX "_get_interface" #endif diff --git a/src/mod/capwap/mod_capwap_ac.c b/src/mod/capwap/mod_capwap_ac.c index 37ac3278..91262c99 100644 --- a/src/mod/capwap/mod_capwap_ac.c +++ b/src/mod/capwap/mod_capwap_ac.c @@ -27,7 +27,7 @@ static int init() static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int elems_len, struct sockaddr *from, int mode) { - if (mode != MOD_MODE_CAPWAP) + if (mode != CW_MOD_MODE_CAPWAP) return 0; cw_dbg(DBG_MOD,"CAPWAP detected: yes"); return 1; @@ -35,7 +35,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele static int register_actions(struct cw_actiondef *def, int mode) { - if (mode != MOD_MODE_CAPWAP) + if (mode != CW_MOD_MODE_CAPWAP) return 0; return capwap_register_actions_ac(def); } diff --git a/src/mod/capwap/mod_capwap_wtp.c b/src/mod/capwap/mod_capwap_wtp.c index 87745df3..91591d70 100644 --- a/src/mod/capwap/mod_capwap_wtp.c +++ b/src/mod/capwap/mod_capwap_wtp.c @@ -27,7 +27,7 @@ static int init() static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int elems_len, struct sockaddr *from, int mode) { - if (mode != MOD_MODE_CAPWAP) + if (mode != CW_MOD_MODE_CAPWAP) return 0; conn->detected = 1; conn->actions = &actions; @@ -36,7 +36,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele static int register_actions(struct cw_actiondef *def, int mode) { - if (mode != MOD_MODE_CAPWAP) + if (mode != CW_MOD_MODE_CAPWAP) return 0; return capwap_register_actions_wtp(def); } diff --git a/src/mod/cipwap/mod_cipwap_ac.c b/src/mod/cipwap/mod_cipwap_ac.c index b2cba020..cf18fb8f 100644 --- a/src/mod/cipwap/mod_cipwap_ac.c +++ b/src/mod/cipwap/mod_cipwap_ac.c @@ -18,7 +18,7 @@ int cipwap_init() static int detect(struct conn *conn,const uint8_t *rawmsg, int rawlen,int elems_len, struct sockaddr *from, int mode) { - if (mode == MOD_MODE_CAPWAP) + if (mode == CW_MOD_MODE_CAPWAP) return 1; return 0; @@ -27,7 +27,7 @@ static int detect(struct conn *conn,const uint8_t *rawmsg, int rawlen,int elems_ static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_ac("capwap"); @@ -36,7 +36,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) "Can't initialize mod_cisco, failed to load base mod mod_capwap"); return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = cipwap_register_actions_ac(actions); cw_dbg(DBG_INFO, "Initialized mod_cisco with %d actions", rc); return rc; diff --git a/src/mod/cipwap/mod_cipwap_wtp.c b/src/mod/cipwap/mod_cipwap_wtp.c index 5f388c83..99f85620 100644 --- a/src/mod/cipwap/mod_cipwap_wtp.c +++ b/src/mod/cipwap/mod_cipwap_wtp.c @@ -18,7 +18,7 @@ int cipwap_init() static int detect(struct conn *conn,const uint8_t *rawmsg, int rawlen,int elems_len, struct sockaddr *from, int mode) { - if (mode == MOD_MODE_CAPWAP) + if (mode == CW_MOD_MODE_CAPWAP) return 1; return 0; @@ -27,7 +27,7 @@ static int detect(struct conn *conn,const uint8_t *rawmsg, int rawlen,int elems_ static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_wtp("capwap"); @@ -36,7 +36,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) "Can't initialize mod_cisco, failed to load base mod mod_capwap"); return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = cipwap_register_actions_ac(actions); cw_dbg(DBG_INFO, "Initialized mod_cisco with %d actions", rc); return rc; diff --git a/src/mod/cisco/mod_cisco_ac.c b/src/mod/cisco/mod_cisco_ac.c index 5024d441..08c05db1 100644 --- a/src/mod/cisco/mod_cisco_ac.c +++ b/src/mod/cisco/mod_cisco_ac.c @@ -24,7 +24,7 @@ mbag_t cisco_config = NULL; static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_ac("cipwap"); @@ -33,7 +33,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) "Can't initialize mod_cisco, failed to load base mod mod_cipwap"); return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = cisco_register_actions_ac(actions); cw_dbg(DBG_INFO, "Initialized mod_cisco with %d actions", rc); return 0; @@ -144,7 +144,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele uint32_t vendor_id = cw_get_dword(cw_get_elem_data(elem)); if (vendor_id == CW_VENDOR_ID_CISCO) { // conn->actions = &actions; - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "CISCO capwap detected: yes"); } else { cw_dbg(DBG_MOD, "CISCO bindings detected: yes"); @@ -158,7 +158,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele } - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "CISCO capwap detected: no"); } else { cw_dbg(DBG_MOD, "CISCO bindings detected: no"); diff --git a/src/mod/cisco/mod_cisco_wtp.c b/src/mod/cisco/mod_cisco_wtp.c index 329bc9cb..d66b687c 100644 --- a/src/mod/cisco/mod_cisco_wtp.c +++ b/src/mod/cisco/mod_cisco_wtp.c @@ -21,7 +21,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_wtp("cipwap"); @@ -31,7 +31,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = cisco_register_actions_wtp(actions); @@ -132,7 +132,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele uint32_t vendor_id = cw_get_dword(cw_get_elem_data(elem)); if (vendor_id == CW_VENDOR_ID_CISCO) { // conn->actions = &actions; - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "CISCO capwap detected: yes"); } else { cw_dbg(DBG_MOD, "CISCO bindings detected: yes"); @@ -146,7 +146,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele } - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "CISCO capwap detected: no"); } else { cw_dbg(DBG_MOD, "CISCO bindings detected: no"); diff --git a/src/mod/fortinet/mod_fortinet_ac.c b/src/mod/fortinet/mod_fortinet_ac.c index 0ba67e53..a1eda779 100644 --- a/src/mod/fortinet/mod_fortinet_ac.c +++ b/src/mod/fortinet/mod_fortinet_ac.c @@ -18,7 +18,7 @@ extern int fortinet_register_actions_ac(struct cw_actiondef *def); static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_ac("capwap"); @@ -27,7 +27,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) "Can't initialize mod_fortinet, failed to load base mod mod_capwap"); return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = fortinet_register_actions_ac(actions); cw_dbg(DBG_INFO, "Initialized mod fortinet with %d actions", rc); return 0; @@ -85,7 +85,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele uint32_t vendor_id = cw_get_dword(cw_get_elem_data(elem)); if (vendor_id == CW_VENDOR_ID_FORTINET) { // conn->actions = &actions; - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "Fortinet capwap detected: yes"); } else { cw_dbg(DBG_MOD, "Fortinet bindings detected: yes"); @@ -99,7 +99,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele } - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "Fortinet capwap detected: no"); } else { cw_dbg(DBG_MOD, "Fortinet bindings detected: no"); diff --git a/src/mod/fortinet/mod_fortinet_wtp.c b/src/mod/fortinet/mod_fortinet_wtp.c index cadbc959..5ae3c011 100644 --- a/src/mod/fortinet/mod_fortinet_wtp.c +++ b/src/mod/fortinet/mod_fortinet_wtp.c @@ -18,7 +18,7 @@ extern int cisco_register_actions_wtp(struct cw_actiondef *def); static int register_actions(struct cw_actiondef *actions, int mode) { switch (mode) { - case MOD_MODE_CAPWAP: + case CW_MOD_MODE_CAPWAP: { struct cw_Mod *cmod = modload_wtp("capwap"); @@ -28,7 +28,7 @@ static int register_actions(struct cw_actiondef *actions, int mode) return 1; } - cmod->register_actions(actions, MOD_MODE_CAPWAP); + cmod->register_actions(actions, CW_MOD_MODE_CAPWAP); int rc = cisco_register_actions_wtp(actions); @@ -130,7 +130,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele uint32_t vendor_id = cw_get_dword(cw_get_elem_data(elem)); if (vendor_id == CW_VENDOR_ID_FORTINET) { // conn->actions = &actions; - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "Fortinet capwap detected: yes"); } else { cw_dbg(DBG_MOD, "Fortinet bindings detected: yes"); @@ -144,7 +144,7 @@ static int detect(struct conn *conn, const uint8_t * rawmsg, int rawlen, int ele } - if (mode == MOD_MODE_CAPWAP) { + if (mode == CW_MOD_MODE_CAPWAP) { cw_dbg(DBG_MOD, "Fortinet capwap detected: no"); } else { cw_dbg(DBG_MOD, "Fortinet bindings detected: no"); diff --git a/src/wtp/wtp_main.c b/src/wtp/wtp_main.c index 4cdec9bb..8230dfe8 100644 --- a/src/wtp/wtp_main.c +++ b/src/wtp/wtp_main.c @@ -130,7 +130,7 @@ int main() } mod->init(); - mod->register_actions(&capwap_actions,MOD_MODE_CAPWAP); + mod->register_actions(&capwap_actions,CW_MOD_MODE_CAPWAP); mod = modload_wtp(CWBIND); if (!mod) { printf("Can't load mod capwap80211\n");