2016-03-13 09:45:56 +01:00
|
|
|
/*
|
|
|
|
This file is part of actube.
|
|
|
|
|
|
|
|
actube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
libcapwap is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Functions for mods
|
|
|
|
*/
|
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
#include <string.h>
|
2016-03-27 04:48:36 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
#include "mavl.h"
|
|
|
|
#include "dbg.h"
|
2016-03-27 04:48:36 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void (*actions_registered_cb) (struct mod_ac * capwap, struct mod_ac * bindings,
|
|
|
|
struct cw_actiondef * actions) = NULL;
|
|
|
|
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
|
|
|
|
int mod_caching = 1;
|
|
|
|
|
|
|
|
void mod_set_actions_registered_cb(void (*fun)
|
|
|
|
(struct mod_ac *, struct mod_ac *, struct cw_actiondef *))
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2016-03-27 04:48:36 +02:00
|
|
|
actions_registered_cb = fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct cache_item {
|
2016-03-08 01:20:22 +01:00
|
|
|
const char *capwap;
|
|
|
|
const char *bindings;
|
|
|
|
struct cw_actiondef actions;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
static struct mavl *cache = NULL;
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
static int mod_null_register_actions(struct cw_actiondef *def, int mode)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-13 09:45:56 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-27 04:48:36 +02:00
|
|
|
* mod_null is a dummy mod
|
2016-03-13 09:45:56 +01:00
|
|
|
*/
|
2016-03-27 04:48:36 +02:00
|
|
|
struct mod_ac mod_null = {
|
|
|
|
.name = "none",
|
|
|
|
.register_actions = mod_null_register_actions
|
2016-03-08 01:20:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-13 09:45:56 +01:00
|
|
|
static int cmp(const void *p1, const void *p2)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2016-03-27 04:48:36 +02:00
|
|
|
struct cache_item *c1 = (struct cache_item *) p1;
|
|
|
|
struct cache_item *c2 = (struct cache_item *) p2;
|
2016-03-08 01:20:22 +01:00
|
|
|
|
|
|
|
int r;
|
2016-03-27 04:48:36 +02:00
|
|
|
r = strcmp(c1->capwap, c2->capwap);
|
|
|
|
if (r != 0)
|
2016-03-08 01:20:22 +01:00
|
|
|
return r;
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
return strcmp(c1->bindings, c2->bindings);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
struct cw_actiondef *mod_cache_get(const char *capwap, const char *bindings)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
|
|
|
|
struct cw_actiondef *mod_cache_add(struct conn *conn, struct mod_ac *c, struct mod_ac *b)
|
2016-03-08 01:20:22 +01:00
|
|
|
{
|
2016-03-27 04:48:36 +02:00
|
|
|
if (!cache) {
|
|
|
|
cache = mavl_create(cmp, NULL);
|
|
|
|
if (!cache) {
|
|
|
|
cw_log(LOG_ERR, "Can't initialize mod cache: %s",
|
|
|
|
strerror(errno));
|
2016-03-08 01:20:22 +01:00
|
|
|
return NULL;
|
2016-03-27 04:48:36 +02:00
|
|
|
}
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct cache_item s;
|
2016-03-27 04:48:36 +02:00
|
|
|
s.capwap = c->name;
|
|
|
|
s.bindings = b->name;
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
struct cache_item *i = mavl_get(cache, &s);
|
|
|
|
if (i) {
|
|
|
|
cw_dbg(DBG_INFO, "Using cached actions for %s,%s", c->name, b->name);
|
2016-03-08 01:20:22 +01:00
|
|
|
return &(i->actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
i = malloc(sizeof(struct cache_item));
|
2016-03-27 04:48:36 +02:00
|
|
|
if (!i) {
|
|
|
|
cw_log(LOG_ERR, "Can't allocate memory for mod cache item %s",
|
|
|
|
strerror(errno));
|
2016-03-08 01:20:22 +01:00
|
|
|
return NULL;
|
2016-03-27 04:48:36 +02:00
|
|
|
}
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
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);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
2016-03-27 04:48:36 +02:00
|
|
|
if (b) {
|
|
|
|
i->bindings = b->name;
|
|
|
|
b->register_actions(&(i->actions), MOD_MODE_BINDINGS);
|
2016-03-08 01:20:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
if (actions_registered_cb)
|
|
|
|
actions_registered_cb(c,b,&(i->actions));
|
2016-03-08 01:20:22 +01:00
|
|
|
|
2016-03-27 04:48:36 +02:00
|
|
|
mavl_add(cache, i);
|
2016-03-08 01:20:22 +01:00
|
|
|
return &(i->actions);
|
|
|
|
}
|