2015-04-05 02:09:23 +02:00
|
|
|
/*
|
|
|
|
This file is part of libcapwap.
|
|
|
|
|
|
|
|
libcapwap 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 Implementation of methods for actionlists.
|
2015-04-07 07:42:36 +02:00
|
|
|
*/
|
2015-04-05 02:09:23 +02:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
#include "mbag.h"
|
2015-04-05 02:09:23 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/* ---------------------------------------------
|
|
|
|
* cw_actionlist_in stuff
|
|
|
|
*/
|
2016-02-28 13:41:45 +01:00
|
|
|
|
|
|
|
|
2018-03-02 13:36:03 +01:00
|
|
|
static int cw_action_in_cmp(const void *elem1, const void *elem2)
|
2015-04-05 02:09:23 +02:00
|
|
|
{
|
2015-04-07 07:42:36 +02:00
|
|
|
struct cw_action_in *e1 = (struct cw_action_in *) elem1;
|
|
|
|
struct cw_action_in *e2 = (struct cw_action_in *) elem2;
|
|
|
|
int r;
|
2015-04-05 02:09:23 +02:00
|
|
|
|
2015-04-12 19:19:29 +02:00
|
|
|
r = e1->capwap_state - e2->capwap_state;
|
2015-04-07 07:42:36 +02:00
|
|
|
if (r != 0)
|
2015-04-05 02:09:23 +02:00
|
|
|
return r;
|
|
|
|
|
2015-04-12 19:19:29 +02:00
|
|
|
r = e1->msg_id - e2->msg_id;
|
2015-04-07 07:42:36 +02:00
|
|
|
if (r != 0)
|
2015-04-05 02:09:23 +02:00
|
|
|
return r;
|
|
|
|
|
2015-04-12 19:19:29 +02:00
|
|
|
r = e1->elem_id - e2->elem_id;
|
2015-04-07 07:42:36 +02:00
|
|
|
if (r != 0)
|
2015-04-05 02:09:23 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
r = e1->vendor_id - e2->vendor_id;
|
2015-04-07 07:42:36 +02:00
|
|
|
if (r != 0)
|
2015-04-05 02:09:23 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
r = e1->proto - e2->proto;
|
2015-04-07 07:42:36 +02:00
|
|
|
if (r != 0)
|
2015-04-05 02:09:23 +02:00
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/**
|
|
|
|
* Create an action list for incomming messages
|
|
|
|
* @return the create list or NULL if an error has occured
|
|
|
|
*/
|
|
|
|
cw_actionlist_in_t cw_actionlist_in_create()
|
2015-04-20 21:28:22 +02:00
|
|
|
{
|
2016-03-28 02:31:07 +02:00
|
|
|
return mavl_create(cw_action_in_cmp, free);
|
2015-04-20 21:28:22 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/**
|
|
|
|
* Add an element to an "in-list"
|
|
|
|
* @param t action list to elem to
|
|
|
|
* @param a element to add
|
|
|
|
* @rturn a pointer to the added element
|
|
|
|
*/
|
2015-04-07 07:42:36 +02:00
|
|
|
cw_action_in_t *cw_actionlist_in_add(cw_actionlist_in_t t, struct cw_action_in * a)
|
2015-04-05 02:09:23 +02:00
|
|
|
{
|
2016-02-28 15:13:10 +01:00
|
|
|
int s = sizeof(struct cw_action_in);
|
|
|
|
|
|
|
|
void *r = mavl_replace_data(t, a, s);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
void *an = malloc(s);
|
|
|
|
if (!an)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(an, a, s);
|
|
|
|
return mavl_add(t, an);
|
2015-04-05 02:09:23 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/**
|
|
|
|
* Get an element from an actionlist_in
|
|
|
|
* @param t action list
|
|
|
|
* @param a element to search for
|
|
|
|
* @return the elemen or NULL if not found
|
|
|
|
*/
|
2015-04-07 07:42:36 +02:00
|
|
|
struct cw_action_in *cw_actionlist_in_get(cw_actionlist_in_t t, struct cw_action_in *a)
|
2015-04-05 02:09:23 +02:00
|
|
|
{
|
2015-04-07 07:42:36 +02:00
|
|
|
return avltree_get(t, a);
|
2015-04-05 02:09:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-28 15:13:10 +01:00
|
|
|
/**
|
|
|
|
* Register actions in an action list for incommin messages
|
|
|
|
* @param t action list, where messaes will be registered
|
|
|
|
* @param actions an array of actions to reggister
|
2016-03-28 02:31:07 +02:00
|
|
|
* @return the number of registred actions
|
2016-02-28 15:13:10 +01:00
|
|
|
*/
|
2015-04-07 07:42:36 +02:00
|
|
|
int cw_actionlist_in_register_actions(cw_actionlist_in_t t, cw_action_in_t * actions)
|
2015-04-05 02:09:23 +02:00
|
|
|
{
|
2015-04-29 12:05:32 +02:00
|
|
|
int n=0;
|
2015-04-07 07:42:36 +02:00
|
|
|
while (actions->capwap_state) {
|
|
|
|
cw_action_in_t *rc = cw_actionlist_in_add(t, actions);
|
2016-02-28 13:41:45 +01:00
|
|
|
if (!rc)
|
2015-04-07 07:42:36 +02:00
|
|
|
return 0;
|
2015-04-05 02:09:23 +02:00
|
|
|
actions++;
|
2015-04-29 12:05:32 +02:00
|
|
|
n++;
|
2015-04-05 02:09:23 +02:00
|
|
|
}
|
2015-04-29 12:05:32 +02:00
|
|
|
return n;
|
2015-04-05 02:09:23 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/* ---------------------------------------------
|
|
|
|
* cw_actionlist_out stuff
|
|
|
|
*/
|
2015-04-05 02:09:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-28 15:13:10 +01:00
|
|
|
struct outelem{
|
|
|
|
uint32_t msg_id;
|
|
|
|
mlist_t * mlist;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-28 13:41:45 +01:00
|
|
|
static int mout_cmp(void *elem1,void *elem2)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct cw_action_out *e1 = (struct cw_action_out *) elem1;
|
|
|
|
struct cw_action_out *e2 = (struct cw_action_out *) elem2;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = e1->msg_id - e2->msg_id;
|
|
|
|
if (r )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = e1->elem_id - e2->elem_id;
|
|
|
|
if (r )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
|
|
r = e1->vendor_id - e2->vendor_id;
|
|
|
|
if (r )
|
|
|
|
return r;
|
|
|
|
|
2016-03-11 23:01:04 +01:00
|
|
|
if (e1->item_id == e2->item_id)
|
2016-02-28 13:41:45 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (e1->item_id && e2->item_id)
|
|
|
|
return strcmp(e1->item_id,e2->item_id);
|
|
|
|
|
|
|
|
return 1;
|
2016-03-28 02:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 13:41:45 +01:00
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
|
|
|
|
struct outelem * cw_actionlist_mout_create(int msg_id)
|
|
|
|
{
|
|
|
|
struct outelem * o = malloc(sizeof(struct outelem));
|
|
|
|
if (!o)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
o->mlist= mlist_create(mout_cmp);
|
|
|
|
if (!o->mlist){
|
|
|
|
free(o);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
o->msg_id=msg_id;
|
|
|
|
return o;
|
2016-02-28 13:41:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
static struct outelem * cw_actionlist_out_get_outelem(cw_actionlist_out_t t, int msg_id)
|
|
|
|
{
|
|
|
|
struct outelem search;
|
|
|
|
search.msg_id=msg_id;
|
|
|
|
return mavl_get(t,&search);
|
|
|
|
}
|
2016-02-28 13:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/*
|
|
|
|
* Compare function for actionlist_out_t lists
|
|
|
|
*/
|
|
|
|
static int cw_action_out_cmp(const void *elem1, const void *elem2)
|
|
|
|
{
|
|
|
|
struct outelem *e1 = (struct outelem *) elem1;
|
|
|
|
struct outelem *e2 = (struct outelem *) elem2;
|
|
|
|
return e1->msg_id - e2->msg_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an element to an actionlist_out
|
|
|
|
* @param t action list
|
|
|
|
* @param a element to add
|
|
|
|
*/
|
|
|
|
cw_action_out_t *cw_actionlist_out_add(cw_actionlist_out_t t, struct cw_action_out * a)
|
|
|
|
{
|
|
|
|
struct outelem * o = cw_actionlist_out_get_outelem(t,a->msg_id);
|
|
|
|
|
|
|
|
|
|
|
|
if (!o){
|
|
|
|
o = cw_actionlist_mout_create(a->msg_id);
|
|
|
|
if (!o) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
mavl_add(t,o);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mlist_elem * e = mlist_replace(o->mlist,NULL,a);
|
|
|
|
if (!e)
|
|
|
|
e = mlist_append(o->mlist,a);
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register actions in an action list for incommin messages
|
|
|
|
* @param t action list, where messaes will be registered
|
|
|
|
* @param actions an array of actions to reggister
|
|
|
|
* @return the number of registred actions
|
|
|
|
*/
|
|
|
|
|
|
|
|
int cw_actionlist_out_register_actions(cw_actionlist_out_t t, cw_action_out_t * actions)
|
|
|
|
{
|
|
|
|
int n=0;
|
|
|
|
while (actions->msg_id != 0) {
|
|
|
|
cw_action_out_t *rc = cw_actionlist_out_add(t, actions);
|
|
|
|
if (rc == 0)
|
|
|
|
return 0;
|
|
|
|
actions++;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-28 13:41:45 +01:00
|
|
|
|
2015-04-05 02:09:23 +02:00
|
|
|
/**
|
|
|
|
* Add an action to an action
|
|
|
|
* @param t actionlist
|
|
|
|
* @param a action to add
|
|
|
|
* @param s size of element to add
|
|
|
|
* @return pointer to added element or NULL if an error has opccured
|
2015-04-07 07:42:36 +02:00
|
|
|
*/
|
|
|
|
void *cw_actionlist_add(struct avltree *t, void *a, size_t s)
|
2015-04-05 02:09:23 +02:00
|
|
|
{
|
2015-04-11 19:00:51 +02:00
|
|
|
|
2015-05-01 13:55:02 +02:00
|
|
|
void *r = mavl_replace_data(t, a, s); //sizeof(struct cw_action_in));
|
2015-04-28 10:20:50 +02:00
|
|
|
if (r) {
|
2015-04-05 03:33:22 +02:00
|
|
|
return r;
|
2015-04-28 10:20:50 +02:00
|
|
|
}
|
2015-04-07 07:42:36 +02:00
|
|
|
|
2015-04-11 19:00:51 +02:00
|
|
|
void *an = malloc(s);
|
2015-04-05 03:33:22 +02:00
|
|
|
if (!an)
|
2015-04-05 02:09:23 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-04-07 07:42:36 +02:00
|
|
|
memcpy(an, a, s);
|
|
|
|
return avltree_add(t, an);
|
2015-04-05 02:09:23 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action list for outgoing message lements
|
|
|
|
* @return the created action list or NULL if an erro has occured.
|
2015-04-07 07:42:36 +02:00
|
|
|
*/
|
2015-04-05 02:09:23 +02:00
|
|
|
cw_actionlist_out_t cw_actionlist_out_create()
|
|
|
|
{
|
2016-02-28 13:41:45 +01:00
|
|
|
return mavl_create(cw_action_out_cmp, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-05 02:09:23 +02:00
|
|
|
|
2016-02-28 15:13:10 +01:00
|
|
|
mlist_t * cw_actionlist_out_get(cw_actionlist_out_t t,int msg_id)
|
|
|
|
{
|
|
|
|
struct outelem *o = cw_actionlist_out_get_outelem(t,msg_id);
|
|
|
|
if (!o)
|
|
|
|
return NULL;
|
|
|
|
return o->mlist;
|
|
|
|
}
|
|
|
|
|
2016-02-28 13:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
/* misc stuff */
|
2015-04-05 02:09:23 +02:00
|
|
|
|
2016-02-28 13:41:45 +01:00
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
cw_action_fun_t cw_set_msg_end_callback(struct cw_actiondef *actions,
|
|
|
|
int capwap_state,int msg_id, cw_action_fun_t callback)
|
|
|
|
{
|
|
|
|
cw_action_in_t as;
|
|
|
|
as.capwap_state = capwap_state;
|
|
|
|
as.msg_id = msg_id;
|
|
|
|
as.vendor_id = 0;
|
|
|
|
as.elem_id = 0;
|
|
|
|
as.proto = 0;
|
|
|
|
|
|
|
|
cw_action_in_t *af;
|
2016-02-28 13:41:45 +01:00
|
|
|
|
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
af = cw_actionlist_in_get(actions->in, &as);
|
|
|
|
if (!af)
|
2015-04-07 07:42:36 +02:00
|
|
|
return NULL;
|
2015-04-05 20:27:17 +02:00
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
cw_action_fun_t old = af->end;
|
|
|
|
af->end =callback;
|
|
|
|
return old;
|
2015-04-20 21:28:22 +02:00
|
|
|
|
2016-03-28 02:31:07 +02:00
|
|
|
}
|
2015-04-20 21:28:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|