Moved Callbacks to mods

This commit is contained in:
2022-09-01 10:24:40 +02:00
parent 2064f7dba1
commit 5ed35979cd
26 changed files with 3728 additions and 431 deletions

View File

@ -465,9 +465,9 @@ enum cw_reboot_failure_types {
/**
* The Missing AC List Result Code is sent by the
* WTP to the AC when the AC List is missing */
#define CW_RESULT_MISSING_AC_LIST 1
#define CAPWAP_RESULT_SUCCESS_NAT 2
#define CW_RESULT_JOIN_FAILURE 3
#define CW_RESULT_MISSING_AC_LIST 1
#define CAPWAP_RESULT_SUCCESS_NAT 2
#define CAPWAP_RESULT_JOIN_FAILURE 3
#define CW_RESULT_JOIN_RESOURCE_DEPLETION 4
#define CW_RESULT_JOIN_UNKNOWN_SOURCE 5
#define CW_RESULT_JOIN_INCORRECT_DATA 6
@ -491,7 +491,7 @@ enum cw_reboot_failure_types {
13 Configuration Failure (Unable to Apply Requested Configuration
- Service Not Provided)
*/
#define CAPWAP_RESULT_CONFIGURATION_FAILURE_SERVICE_NOT PROVIDED 13
#define CAPWAP_RESULT_CONFIGURATION_FAILURE_SERVICE_NOT_PROVIDED 13
/**
* Image Data Error (Invalid Checksum)
*/

View File

@ -637,11 +637,15 @@ struct cw_Cfg_entry *cw_cfg_iter_next(struct cw_Cfg_iter *cfi, const char *nnkey
}
}
d = strchr(e->key, '.');
if (e->key[bl]!='.' && e->key[bl]!='/')
return NULL;
/* d = strchr(e->key, '.');
if (d == NULL)
return NULL;
if (d - e->key != bl)
return NULL;
return NULL;*/
if (strncmp(cfi->base, e->key, bl) != 0)
return NULL;
@ -684,6 +688,14 @@ uint8_t cw_cfg_get_byte_l(cw_Cfg_t ** cfgs, char *key, uint8_t def)
return v.val.byte;
}
uint16_t cw_cfg_get_int(cw_Cfg_t * cfg, const char *key, int def)
{
const char *s = cw_cfg_get(cfg,key,NULL);
if (s==NULL)
return def;
return atoi(s);
}
uint16_t cw_cfg_get_word(cw_Cfg_t * cfg, const char *key, uint16_t def)
@ -708,7 +720,6 @@ uint16_t cw_cfg_get_word_l(cw_Cfg_t ** cfg, const char *key, uint16_t def)
/*
int cw_cfg_get_word(cw_Cfg_t * cfg, char *key, const char * def)
{

View File

@ -89,6 +89,7 @@ void cw_cfg_del(cw_Cfg_t * cfg, const char *key);
int cw_cfg_get_first_index(cw_Cfg_t * cfg, const char *key, int n);
int cw_cfg_get_first_index_l(cw_Cfg_t ** cfgs, const char *key, int n);
uint16_t cw_cfg_get_int(cw_Cfg_t * cfg, const char *key, int def);

View File

@ -24,11 +24,12 @@
#include "dtls.h"
#define MAX_MSG_CBS 5
struct msg_callback{
int type; /**< message type */
cw_MsgCallbackFun fun;
int type; /**< message type */
struct cw_MsgCb_data data[MAX_MSG_CBS];
int count;
};
int msg_callback_cmp(const void *v1,const void *v2)
@ -62,19 +63,55 @@ void cw_conn_init(struct cw_Conn * conn)
conn->remote_cfg = cw_cfg_create();
conn->local_cfg = cw_cfg_create();
conn->cfg_list[0]=NULL;
conn->remote_addr[0]=0;
}
int cw_conn_set_msg_cb(struct cw_Conn *conn, int type, cw_MsgCallbackFun fun)
/**
* Register a message callback function.
* @param conn the connection the registered cb functions belongs to
* @param type The associated msg type. Whenever a message of this type is
* received, the callback function will be called
* @param fun A pinter to the callback function
*
*/
int cw_conn_register_msg_cb(struct cw_Conn *conn, int type, cw_MsgCallbackFun fun)
{
struct msg_callback cb;
struct msg_callback cb, *result;
int exists;
cb.type = type;
cb.fun = fun;
mavl_insert(conn->msg_callbacks,&cb,&exists);
cb.data[0].fun = fun;
cb.data[0].parent = NULL;
cb.count = 0;
result = mavl_insert(conn->msg_callbacks,&cb,&exists);
cw_dbg(DBG_X, "Registering msg callback: %d %p",type,result);
if (result==NULL)
return 0;
if (exists){
if (result->count>=MAX_MSG_CBS){
cw_log(LOG_ERROR,"Too many msg callback registrations for msg id %d",type);
return 0;
}
result->count++;
result->data[result->count].fun=fun;
result->data[result->count].parent=&(result->data[result->count-1]);
cw_dbg(DBG_X,"The result exists: %p %p %d %p",result,&cb,result->count,
result->data[result->count].parent
);
}
return 1;
}
/*
cw_MsgCallbackFun cw_conn_get_msg_cb(struct cw_Conn *conn, int type)
{
struct msg_callback cb,*result;
@ -83,8 +120,22 @@ cw_MsgCallbackFun cw_conn_get_msg_cb(struct cw_Conn *conn, int type)
if (result == NULL)
return NULL;
return result->fun;
}*/
int cw_conn_run_msg_cbs(struct cw_Conn * conn, int type, struct cw_ElemHandlerParams *params)
{
struct msg_callback cb,*result;
cb.type=type;
result = mavl_get(conn->msg_callbacks,&cb);
if (result == NULL)
return -1;
return result->data[result->count].fun(params,&(result->data[result->count]));
}
/**
* Create a conn object
* @param sock a socket
@ -106,8 +157,10 @@ struct cw_Conn * cw_conn_create(int sock, struct sockaddr * addr, int qsize)
conn->sock=sock;
if (addr)
if (addr){
sock_copyaddr(&conn->addr,addr);
sock_addr2str_p(addr, conn->remote_addr);
}
conn->fragman = fragman_create();
@ -516,12 +569,12 @@ static int process_elements(struct cw_Conn *conn, uint8_t * rawmsg, int len,
message->postprocess(&params,elems_ptr, elems_len);
}
cw_MsgCallbackFun cb_fun = cw_conn_get_msg_cb(conn,message->type);
if (cb_fun != NULL){
result_code = cb_fun(&params,elems_ptr, elems_len);
}
else{
result_code = cw_conn_run_msg_cbs(conn,message->type,&params);
// cw_MsgCallbackFun cb_fun = cw_conn_get_msg_cb(conn,message->type);
if (result_code==-1){
cw_cfg_clear(conn->update_cfg);
result_code=0;
}
// conn->remote_cfg=params.cfg;

View File

@ -58,6 +58,8 @@ struct cw_action_in;
struct cw_Conn {
int sock;
struct sockaddr_storage addr;
char remote_addr[64]; /* Contains a printfable string, of the connections
peer address */
struct connlist * connlist;
@ -71,19 +73,19 @@ struct cw_Conn {
int recv_timeout;
cw_Cfg_t * global_cfg; /**< This should set the global cfg of the program
cw_Cfg_t * global_cfg; /**< This should set to the global cfg of the program
which is using this conn object.
Teh global_cfg has to be treated read-only. */
cw_Cfg_t * local_cfg; /**< local_cfg contains overrides for global_cfg
wich are related to this conn object. */
wich are related to this conniection. */
cw_Cfg_t * remote_cfg; /**< contains the configuration we now from the
device this conn object ist connected to.
Typically this is what we have got from discovery
response or join response in WTP mode.
And in AC mode this contains date receive from
configuration status request. */
And in AC mode this contains datia received by
configuration status and join request. */
cw_Cfg_t * update_cfg;
@ -311,7 +313,18 @@ int conn_send_msg(struct cw_Conn *conn, uint8_t * rawmsg);
void conn_clear_upd(struct cw_Conn*conn, int merge);
int cw_conn_set_msg_cb(struct cw_Conn *conn, int type, cw_MsgCallbackFun fun);
struct cw_MsgCb_data;
//typedef int (*cw_MsgCallbackFun)(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len,
// struct cw_MsgCb_data *d);
typedef int (*cw_MsgCallbackFun)(struct cw_ElemHandlerParams * params, struct cw_MsgCb_data *d);
struct cw_MsgCb_data{
cw_MsgCallbackFun fun;
struct cw_MsgCb_data * parent;
};
int cw_conn_register_msg_cb(struct cw_Conn *conn, int type, cw_MsgCallbackFun fun);
cw_MsgCallbackFun cw_conn_get_msg_cb(struct cw_Conn *conn, int type);
int cw_decode_element(struct cw_ElemHandlerParams *params, int proto,

View File

@ -3,9 +3,10 @@
#include "dbg.h"
#include "cw.h"
#include "cfg.h"
/**
* @brief Detect NAT after a join/discovery request
* @brief Detect NAT after a Join Request
* @param conn Connection object
* @retval 1 NAT detected
* @retval 0 no NAT was detected
@ -15,7 +16,8 @@ int cw_detect_nat(struct cw_ElemHandlerParams *params)
const char * remote_str;
char local_str[128];
remote_str = cw_cfg_get(params->cfg,"capwap-local-ip-address",NULL);
remote_str = cw_cfg_get(params->cfg,"capwap/local-ip-address",NULL);
if (remote_str == NULL){
cw_dbg(DBG_WARN,"Can't detect NAT. No local IP from peer received.");
return 0;
@ -29,9 +31,12 @@ int cw_detect_nat(struct cw_ElemHandlerParams *params)
/* if connected and sent address is the same, there is
* no NAT */
if (strcmp(remote_str,local_str)==0)
if (strcmp(remote_str,local_str)==0){
cw_dbg(DBG_INFO,"Connection from %s: no NAT detected.",local_str);
return 0;
}
/* otherwise ther must be something between AC and WTP */
cw_dbg(DBG_INFO,"Connection from %s: NAT detected.",local_str);
return 1;
}

View File

@ -133,7 +133,7 @@ static struct cw_StrListElem prefix[] = {
{DBG_RFC, " RFC - "},
{DBG_DTLS, "DTLS - "},
{DBG_DTLS_DETAIL, "DTLS - "},
{DBG_WARN, " Warning - "},
{DBG_WARN, "Warning - "},
{DBG_MOD, "Mod - "},
{DBG_MOD_DETAIL, "Mod - "},
{DBG_STATE, "STATEMACHINE - "},

View File

@ -27,6 +27,8 @@
#include "cw.h"
#include "capwap80211.h"
/**
* Format bytes as hex string.
* @param dst Destination buffer
@ -87,4 +89,18 @@ int format_hdr_flags(char *dst, uint8_t * th)
}
char * cw_format_radio_information(char * dst, int ri)
{
char *d = dst;
if (ri & CW_80211_RADIO_TYPE_A)
*d++='a';
if (ri & CW_80211_RADIO_TYPE_B)
*d++='b';
if (ri & CW_80211_RADIO_TYPE_G)
*d++='g';
if (ri & CW_80211_RADIO_TYPE_N)
*d++='n';
*d=0;
return dst;
}

View File

@ -112,6 +112,9 @@ int cw_format_pkt_hdr(char *dst, int incomming, uint8_t * packet, int len,
struct sockaddr *from);
int cw_format_version(char *s, const uint8_t * version, int len);
char * cw_format_radio_information(char * dst, int ri);
/**@}*/
#endif

View File

@ -56,7 +56,7 @@ struct cw_Mod {
int elems_len, struct sockaddr * from, int mode);
struct cw_MsgSet * (*register_messages)(struct cw_MsgSet * set, int mode);
struct cw_MsgSet * (*register_messages)(struct cw_MsgSet * set, int mode );
/**
* Handle returned by dlopen, if this module was loaded

View File

@ -405,7 +405,9 @@ struct cw_MsgData *cw_msgset_get_msgdata(struct cw_MsgSet *set, int type)
return NULL;
}
typedef int (*cw_MsgCallbackFun)(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len);
//typedef int (*cw_MsgCallbackFun)(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len);
//
/*
cw_MsgCallbackFun cw_msgset_set_postprocess(struct cw_MsgSet * set,int msg_id,
cw_MsgCallbackFun fun)
@ -420,5 +422,5 @@ cw_MsgCallbackFun cw_msgset_set_postprocess(struct cw_MsgSet * set,int msg_id,
return old_callback;
}
*/

View File

@ -146,9 +146,8 @@ struct cw_ElemHandler * cw_msgset_get_elemhandler(struct cw_MsgSet * set,
#define CW_MSGSET_POSTPROCESS 1
#define CW_MSGSET_PREPROCESS 2
typedef int (*cw_MsgCallbackFun)(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len);
cw_MsgCallbackFun cw_msgset_set_postprocess(struct cw_MsgSet * set,int msg_id,
cw_MsgCallbackFun fun);
//cw_MsgCallbackFun cw_msgset_set_postprocess(struct cw_MsgSet * set,int msg_id,
// cw_MsgCallbackFun fun);
#endif