diff --git a/src/cw/mod.c b/src/cw/mod.c index fff396a4..4e4c610f 100644 --- a/src/cw/mod.c +++ b/src/cw/mod.c @@ -22,32 +22,49 @@ */ #include +#include + #include "action.h" #include "mavl.h" #include "dbg.h" +#include "log.h" -struct cache_item + +static void (*actions_registered_cb) (struct mod_ac * capwap, struct mod_ac * bindings, + struct cw_actiondef * actions) = NULL; + + + +int mod_caching = 1; + +void mod_set_actions_registered_cb(void (*fun) + (struct mod_ac *, struct mod_ac *, struct cw_actiondef *)) { + actions_registered_cb = fun; +} + + +struct cache_item { const char *capwap; const char *bindings; struct cw_actiondef actions; }; -static struct mavl * cache = NULL; +static struct mavl *cache = NULL; -static int mod_null_register_actions(struct cw_actiondef *def,int mode) +static int mod_null_register_actions(struct cw_actiondef *def, int mode) { return 0; } /** - * mod_null + * mod_null is a dummy mod */ -struct mod_ac mod_null={ - .name="none", - .register_actions=mod_null_register_actions +struct mod_ac mod_null = { + .name = "none", + .register_actions = mod_null_register_actions }; @@ -55,60 +72,66 @@ struct mod_ac mod_null={ static int cmp(const void *p1, const void *p2) { - struct cache_item * c1 = (struct cache_item *)p1; - struct cache_item * c2 = (struct cache_item *)p2; + struct cache_item *c1 = (struct cache_item *) p1; + struct cache_item *c2 = (struct cache_item *) p2; int r; - r = strcmp(c1->capwap,c2->capwap); - if (r!=0) + r = strcmp(c1->capwap, c2->capwap); + if (r != 0) return r; - return strcmp(c1->bindings,c2->bindings); - + return strcmp(c1->bindings, c2->bindings); } -struct cw_actiondef * mod_cache_get(const char *capwap, const char *bindings) +struct cw_actiondef *mod_cache_get(const char *capwap, const char *bindings) { - return NULL; } -struct cw_actiondef * mod_cache_add(struct mod_ac *c, struct mod_ac *b) + +struct cw_actiondef *mod_cache_add(struct conn *conn, struct mod_ac *c, struct mod_ac *b) { - if (!cache){ - cache = mavl_create(cmp,NULL); - if (!cache) + if (!cache) { + cache = mavl_create(cmp, NULL); + if (!cache) { + cw_log(LOG_ERR, "Can't initialize mod cache: %s", + strerror(errno)); return NULL; + } } struct cache_item s; - s.capwap=c->name; - s.bindings=b->name; + s.capwap = c->name; + s.bindings = b->name; - struct cache_item * i = mavl_get(cache,&s); - if (i){ - cw_dbg(DBG_INFO,"Using cached actions for %s,%s",c->name,b->name); + struct cache_item *i = mavl_get(cache, &s); + if (i) { + cw_dbg(DBG_INFO, "Using cached actions for %s,%s", c->name, b->name); return &(i->actions); } i = malloc(sizeof(struct cache_item)); - if (!i) + if (!i) { + cw_log(LOG_ERR, "Can't allocate memory for mod cache item %s", + strerror(errno)); return NULL; - - cw_dbg(DBG_INFO,"Loading actions for %s,%s",c->name,b->name); - memset (i,0,sizeof(struct cache_item)); - if (c){ - i->capwap=c->name; - c->register_actions(&(i->actions),MOD_MODE_CAPWAP); - } - if (b){ - i->bindings=b->name; - b->register_actions(&(i->actions),MOD_MODE_BINDINGS); } - mavl_add(cache,i); + cw_dbg(DBG_INFO, "Loading actions for %s,%s", c->name, b->name); + memset(i, 0, sizeof(struct cache_item)); + if (c) { + i->capwap = c->name; + c->register_actions(&(i->actions), MOD_MODE_CAPWAP); + } + if (b) { + i->bindings = b->name; + b->register_actions(&(i->actions), MOD_MODE_BINDINGS); + } + if (actions_registered_cb) + actions_registered_cb(c,b,&(i->actions)); + + mavl_add(cache, i); return &(i->actions); - } diff --git a/src/cw/mod.h b/src/cw/mod.h index 62a3ea2c..caedd4a0 100644 --- a/src/cw/mod.h +++ b/src/cw/mod.h @@ -56,8 +56,6 @@ struct mod_ac { /** Register actions */ int (*register_actions) (struct cw_actiondef * def,int mode); - - }; @@ -72,9 +70,17 @@ extern struct mod_ac mod_null; #define MOD_NULL (&mod_null) -struct cw_actiondef * mod_cache_add(struct mod_ac *c, struct mod_ac *b); +struct cw_actiondef * mod_cache_add(struct conn *conn,struct mod_ac *c, struct mod_ac *b); #define mod_init_config(mod,cfg) (mod->init_config ? mod->init_config(cfg):1) +void mod_set_actions_registered_cb(void (*fun) (struct mod_ac *, struct mod_ac *, struct cw_actiondef *)); + +extern int mod_caching; + +#define mod_set_caching(var) (mod_caching=var) +#define mod_get_caching() (mod_caching) + + #endif