Improved mod detection, also for bindings. detections are cached.

FossilOrigin-Name: bb2a4cd9c18aded4fa028aa8d385c811d7550a37ddaf3da17f90399315abf637
This commit is contained in:
7u83@mail.ru
2016-03-08 00:20:22 +00:00
parent 43401093ea
commit be07b0aa0a
12 changed files with 258 additions and 78 deletions

View File

@ -195,7 +195,6 @@ DTLSOBJS+=dtls_bio.o
CONNOBJS= conn_create.o \
conn_process_packet.o \
conn_process_packet2.o \
conn_q_add_packet.o \
conn_q_get_packet.o \
conn_q_recv_packet.o \
@ -261,7 +260,8 @@ CWACTION=action.o \
radio.o \
cw_out_radio_info.o \
capwap_items.o \
item.o
item.o \
mod.o
# cw_process_msg.o \
# item_strings.o \

View File

@ -117,7 +117,7 @@ struct cw_itemdef capwap_itemdefs[] = {
{CW_ITEM_AP_MODE_AND_TYPE,CW_ITEM_NONE,MBAG_WORD},
//{CW_ITEM_AP_MODE_AND_TYPE,CW_ITEM_NONE,MBAG_WORD},
/* Board Data */
{CW_ITEM_WTP_BOARD_DATA,CW_ITEM_NONE,MBAG_MBAG},

View File

@ -31,6 +31,7 @@
#include "sock.h"
#include "stravltree.h"
#include "mod.h"
int conn_send_msg(struct conn *conn, uint8_t * rawmsg);
@ -152,6 +153,78 @@ static int check_len(struct conn *conn, struct cw_action_in *a, uint8_t * data,
return 1;
}
static struct mod_ac * detect_mod(struct conn *conn, uint8_t * rawmsg, int len,
int elems_len, struct sockaddr *from, int mode)
{
if (conn->mods) {
struct mod_ac **mods = (struct mod_ac **) conn->mods;
int i;
for (i = 0; mods[i]; i++) {
if (mods[i]->detect) {
if (mods[i]->
detect(conn, rawmsg, len, elems_len, from, mode)) {
return mods[i];
}
}
}
}
return MOD_NULL;
}
static struct cw_actiondef * load_mods(struct conn *conn, uint8_t * rawmsg, int len,
int elems_len, struct sockaddr *from)
{
struct mod_ac * cmod = detect_mod(conn, rawmsg, len, elems_len, from, MOD_DETECT_CAPWAP);
if (cmod == MOD_NULL) {
cw_dbg(DBG_MSG_ERR, "Cant't find mod to handle connection from %s , discarding message",
sock_addr2str_p(from));
return NULL;
}
struct mod_ac * bmod = detect_mod(conn, rawmsg, len, elems_len, from, MOD_DETECT_BINDINGS);
cw_dbg(DBG_INFO,"Mods deteced: %s,%s",cmod->name,bmod->name);
struct cw_actiondef * ad = mod_cache_add(cmod,bmod);
return ad;
/*
if (bindins_mod) {
cw_dbg(DBG_INFO, "Using mod '%s' to handle CAWPAP for %s", mod->name,
sock_addr2str_p(from));
conn->detected=1;
}
else{
// errno = EAGAIN;
// return -1;
}
mod = detect_mod(conn, rawmsg, len, elems_len, from, MOD_DETECT_BINDINGS);
if (mod) {
cw_dbg(DBG_INFO, "Using bindings '%s' to handle %s", mod->name,
sock_addr2str_p(from));
conn->detected=1;
}
else{
cw_dbg(DBG_MSG_ERR, "Cant't detect bindings ... for %s",
sock_addr2str_p(from));
}
return 0;
*/
}
static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
struct sockaddr *from)
@ -198,30 +271,14 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
if (!conn->detected) {
if (conn->mods) {
struct mod_ac **mods = (struct mod_ac **) conn->mods;
int i;
for (i = 0; mods[i]; i++) {
if (mods[i]->detect) {
if (mods[i]->detect(conn, rawmsg, len, elems_len, from)) {
cw_dbg(DBG_INFO,
"Using mod '%s' to handle connection from %s",
mods[i]->name,
sock_addr2str(from));
break;
}
}
}
//struct mod_ac *mod;
struct cw_actiondef * ad = load_mods(conn, rawmsg, len, elems_len, from);
if (!ad) {
cw_log(LOG_ERR,"Eror");
errno=EAGAIN;
return -1;
}
}
if (!conn->detected) {
cw_dbg(DBG_MSG_ERR, "Cant't detect capwap, discarding message from %s",
sock_addr2str(from));
// errno = EAGAIN;
// return -1;
conn->actions = ad;
}
@ -284,7 +341,7 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
/* Create an avltree to catch the found mandatory elements */
conn->mand = stravltree_create();
int unrecognized=0;
int unrecognized = 0;
/* iterate through message elements */
cw_foreach_elem(elem, elems_ptr, elems_len) {
@ -307,7 +364,7 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
continue;
}
if (!check_len(conn,af,cw_get_elem_data(elem), elem_len,from)){
if (!check_len(conn, af, cw_get_elem_data(elem), elem_len, from)) {
continue;
}
@ -336,8 +393,9 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
}
if (unrecognized){
cw_dbg(DBG_RFC,"Message has %d unrecognized message elements.",unrecognized);
if (unrecognized) {
cw_dbg(DBG_RFC, "Message has %d unrecognized message elements.",
unrecognized);
if (!result_code) {
result_code = CW_RESULT_UNRECOGNIZED_MESSAGE_ELEMENT;
}

87
src/cw/mod.c Normal file
View File

@ -0,0 +1,87 @@
#include <string.h>
#include "action.h"
#include "mavl.h"
#include "dbg.h"
struct cache_item
{
const char *capwap;
const char *bindings;
struct cw_actiondef actions;
};
static struct mavl * cache = NULL;
static int mod_null_register_actions(struct cw_actiondef *def)
{
return 0;
}
struct mod_ac mod_null={
.name="none",
.register_actions=mod_null_register_actions
};
int cmp(const void *p1, const void *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)
return r;
return strcmp(c1->bindings,c2->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)
{
if (!cache){
cache = mavl_create(cmp,NULL);
if (!cache)
return NULL;
}
struct cache_item s;
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);
return &(i->actions);
}
i = malloc(sizeof(struct cache_item));
if (!i)
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));
}
if (b){
i->bindings=b->name;
b->register_actions(&(i->actions));
}
mavl_add(cache,i);
return &(i->actions);
}

View File

@ -28,23 +28,30 @@
struct cw_actiondef;
struct mod_ac
{
#define MOD_MAXMODS 8
enum {
MOD_DETECT_CAPWAP,
MOD_DETECT_BINDINGS
};
struct mod_ac {
/** Name of the mod */
const char *name;
/** Initializion method */
int (*init)();
int (*init) ();
/** Detect capwap
This function ifter receiving and disassembling a complete
This function is called after receiving and disassembling a complete
CAPWAP message. Either on Discovery Request or Join Request
**/
int (*detect)(struct conn *conn,const uint8_t *rawmsg, int rawlen,int elems_len, struct sockaddr *from);
int (*detect) (struct conn * conn, const uint8_t * rawmsg, int rawlen,
int elems_len, struct sockaddr * from, int mode);
/** used for private data */
void *data;
/** Register actions */
int (*register_actions)(struct cw_actiondef *def);
int (*register_actions) (struct cw_actiondef * def);
};
@ -55,5 +62,11 @@ struct mod_ac
#define mod_wtp mod_ac
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);
#endif