From ff8d520972efa4cf427791144ff7799163fdcafd Mon Sep 17 00:00:00 2001
From: 7u83 <7u83@mail.ru>
Date: Sat, 13 Aug 2022 11:48:49 +0200
Subject: [PATCH] New msg_callbacks implemented and tested
---
src/ac/wtpman.c | 6 +--
src/cw/conn.c | 22 ++++++++++-
src/cw/conn.h | 5 +++
src/cw/conn_init.c | 74 ++++++++++++++++++++++++++++++++++++
src/cw/conn_process_packet.c | 5 +++
5 files changed, 108 insertions(+), 4 deletions(-)
create mode 100644 src/cw/conn_init.c
diff --git a/src/ac/wtpman.c b/src/ac/wtpman.c
index 80e00234..c3482152 100644
--- a/src/ac/wtpman.c
+++ b/src/ac/wtpman.c
@@ -622,9 +622,7 @@ void wtpman_destroy(struct wtpman *wtpman)
static void discovery_cb(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len)
{
- struct wtpman * wtpman = (struct wtpman *)params->conn->data;
cw_dbg(DBG_X,"Discovery->Callback");
- wtpman->pdiscovery(params,elems_ptr,elems_len);
}
static void join_cb(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr, int elems_len)
@@ -648,7 +646,6 @@ static void update_cb(struct cw_ElemHandlerParams * params, uint8_t * elems_ptr,
static setup_complete(struct cw_Conn *conn)
{
struct wtpman * wtpman = (struct wtpman *)conn->data;
- wtpman->pdiscovery = cw_msgset_set_postprocess(conn->msgset,CAPWAP_MSG_DISCOVERY_REQUEST,discovery_cb);
wtpman->pjoin = cw_msgset_set_postprocess(conn->msgset,CAPWAP_MSG_JOIN_REQUEST,join_cb);
wtpman->pupdate = cw_msgset_set_postprocess(conn->msgset,CAPWAP_MSG_CONFIGURATION_STATUS_REQUEST,update_cb);
cw_dbg(DBG_X,"SETUP COMPLETE");
@@ -715,6 +712,9 @@ struct wtpman *wtpman_create(int socklistindex, struct sockaddr *srcaddr,
sock_copyaddr(&wtpman->conn->data_addr,
(struct sockaddr *) &wtpman->conn->addr);
+ cw_conn_set_msg_cb(wtpman->conn,
+ CAPWAP_MSG_DISCOVERY_REQUEST,
+ discovery_cb);
// wtpman->conn->mods = conf_mods;
diff --git a/src/cw/conn.c b/src/cw/conn.c
index 9c92664b..dfe8194d 100644
--- a/src/cw/conn.c
+++ b/src/cw/conn.c
@@ -163,7 +163,7 @@ int cw_assemble_message(struct cw_Conn *conn, uint8_t * rawout)
struct msg_callback{
int type; /**< message type */
- int (*fun)();
+ cw_MsgCallbackFun fun;
};
int msg_callback_cmp(const void *v1,const void *v2)
@@ -194,6 +194,26 @@ void cw_conn_init(struct cw_Conn * conn)
conn->msg_callbacks = mavl_create(msg_callback_cmp,NULL,sizeof(struct msg_callback));
}
+int cw_conn_set_msg_cb(struct cw_Conn *conn, int type, cw_MsgCallbackFun fun)
+{
+ struct msg_callback cb;
+ int exists;
+
+ cb.type = type;
+ cb.fun = fun;
+ mavl_insert(conn->msg_callbacks,&cb,&exists);
+}
+
+cw_MsgCallbackFun cw_conn_get_msg_cb(struct cw_Conn *conn, int type)
+{
+ struct msg_callback cb,*result;
+ cb.type=type;
+ result = mavl_get(conn->msg_callbacks,&cb);
+ if (result == NULL)
+ return NULL;
+ return result->fun;
+}
+
/**
* Create a conn object
* @param sock a socket
diff --git a/src/cw/conn.h b/src/cw/conn.h
index 38b29454..60173b96 100644
--- a/src/cw/conn.h
+++ b/src/cw/conn.h
@@ -313,4 +313,9 @@ 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);
+cw_MsgCallbackFun cw_conn_get_msg_cb(struct cw_Conn *conn, int type);
+
+
#endif /* __CONN_H */
diff --git a/src/cw/conn_init.c b/src/cw/conn_init.c
new file mode 100644
index 00000000..f2357086
--- /dev/null
+++ b/src/cw/conn_init.c
@@ -0,0 +1,74 @@
+/*
+ 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 .
+
+*/
+
+
+/**
+ * @file
+ * @brief conn_init
+ */
+
+#include
+
+
+#include "conn.h"
+#include "capwap.h"
+#include "cw.h"
+
+/*
+static int write_header(struct cw_ElemHandler * handler, uint8_t * dst, int len)
+{
+ if (handler->vendor)
+ return len + cw_put_elem_vendor_hdr(dst, handler->vendor, handler->id, len);
+
+ return len + cw_put_elem_hdr(dst, handler->id, len);
+}
+
+static int header_len(struct cw_ElemHandler * handler)
+{
+ return handler->vendor ? 10 : 4;
+}
+
+*/
+
+/**
+ * Basic initialization of a conn object
+ * @param conn conn object to initialize
+ */
+void conn_init(struct cw_Conn * conn)
+{
+ memset(conn,0,sizeof(struct cw_Conn ));
+ conn->retransmit_interval=CAPWAP_RETRANSMIT_INTERVAL;
+ conn->max_retransmit=CAPWAP_MAX_RETRANSMIT;
+ conn->wait_dtls=CAPWAP_WAIT_DTLS;
+ conn->wait_join=CAPWAP_WAIT_JOIN;
+ conn->mtu_discovery=1;
+// conn->capwap_mode = 0;
+ conn->strict_capwap=1;
+
+
+
+ conn->process_packet=conn_process_packet;
+ conn->process_message=process_message;
+/*
+ conn->write_header = write_header;
+ conn->header_len = header_len;
+*/
+
+}
+
+
diff --git a/src/cw/conn_process_packet.c b/src/cw/conn_process_packet.c
index 8c7ef723..ac3ec1fd 100644
--- a/src/cw/conn_process_packet.c
+++ b/src/cw/conn_process_packet.c
@@ -405,6 +405,11 @@ static int process_elements(struct cw_Conn *conn, uint8_t * rawmsg, int len,
" *** Remote CFG dump ***", "CFG:", " *** End of remote CFG dump");
*/
+ cw_MsgCallbackFun cb_fun = cw_conn_get_msg_cb(conn,message->type);
+ if (cb_fun != NULL){
+ cb_fun(¶ms,elems_ptr, elems_len);
+ }
+
if (message->postprocess) {
// message->postprocess(conn);