Bläddra i källkod

core: internal events API use a structure as parameter instead of void*

- opens the door for more flexibility in passing extra data to callbacks
Daniel-Constantin Mierla 8 år sedan
förälder
incheckning
4eb286bb65
8 ändrade filer med 69 tillägg och 44 borttagningar
  1. 4 1
      src/core/action.c
  2. 17 17
      src/core/events.c
  3. 6 2
      src/core/events.h
  4. 9 5
      src/core/forward.h
  5. 7 3
      src/core/receive.c
  6. 14 12
      src/core/stun.c
  7. 9 3
      src/core/tcp_read.c
  8. 3 1
      src/core/udp_server.c

+ 4 - 1
src/core/action.c

@@ -310,6 +310,8 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
 	struct rval_cache c1;
 	str s;
 	void *srevp[2];
+	sr_event_param_t evp = {0};
+
 	/* temporary storage space for a struct action.val[] working copy
 	 (needed to transform RVE intro STRING before calling module
 	   functions). [0] is not used (corresp. to the module export pointer),
@@ -330,7 +332,8 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
 	{
 		srevp[0] = (void*)a;
 		srevp[1] = (void*)msg;
-		sr_event_exec(SREV_CFG_RUN_ACTION, (void*)srevp);
+		evp.data = (void*)srevp;
+		sr_event_exec(SREV_CFG_RUN_ACTION, &evp);
 	}
 
 	ret=E_BUG;

+ 17 - 17
src/core/events.c

@@ -189,7 +189,7 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
 /**
  *
  */
-int sr_event_exec(int type, void *data)
+int sr_event_exec(int type, sr_event_param_t *evp)
 {
 	int ret;
 	int i;
@@ -201,14 +201,14 @@ int sr_event_exec(int type, void *data)
 				if(unlikely(_sr_events_list.net_data_in[0]!=0))
 				{
 #ifdef EXTRA_DEBUG
-					p = (str*)data;
+					p = (str*)evp->data;
 					LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
 							"%.*s\n+++++\n", p->len, p->s);
 #endif /* EXTRA_DEBUG */
 					ret = 0;
 					for(i=0; i<SREV_CB_LIST_SIZE
 							&& _sr_events_list.net_data_in[i]; i++) {
-						ret |= _sr_events_list.net_data_in[i](data);
+						ret |= _sr_events_list.net_data_in[i](evp);
 					}
 #ifdef EXTRA_DEBUG
 					LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
@@ -228,7 +228,7 @@ int sr_event_exec(int type, void *data)
 					ret = 0;
 					for(i=0; i<SREV_CB_LIST_SIZE; i++) {
 						if(_sr_events_list.net_data_out[i]) {
-							ret |= _sr_events_list.net_data_out[i](data);
+							ret |= _sr_events_list.net_data_out[i](evp);
 						}
 					}
 #ifdef EXTRA_DEBUG
@@ -241,80 +241,80 @@ int sr_event_exec(int type, void *data)
 		case SREV_CORE_STATS:
 				if(unlikely(_sr_events_list.core_stats!=0))
 				{
-					ret = _sr_events_list.core_stats(data);
+					ret = _sr_events_list.core_stats(evp);
 					return ret;
 				} else return 1;
 			break;
 		case SREV_CFG_RUN_ACTION:
 				if(unlikely(_sr_events_list.run_action!=0))
 				{
-					ret = _sr_events_list.run_action(data);
+					ret = _sr_events_list.run_action(evp);
 					return ret;
 				} else return 1;
 		case SREV_PKG_UPDATE_STATS:
 				if(unlikely(_sr_events_list.pkg_update_stats!=0))
 				{
-					ret = _sr_events_list.pkg_update_stats(data);
+					ret = _sr_events_list.pkg_update_stats(evp);
 					return ret;
 				} else return 1;
 		case SREV_NET_DGRAM_IN:
 				if(unlikely(_sr_events_list.net_dgram_in!=0))
 				{
-					ret = _sr_events_list.net_dgram_in(data);
+					ret = _sr_events_list.net_dgram_in(evp);
 					return ret;
 				} else return 1;
 		case SREV_TCP_HTTP_100C:
 				if(unlikely(_sr_events_list.tcp_http_100c!=0))
 				{
-					ret = _sr_events_list.tcp_http_100c(data);
+					ret = _sr_events_list.tcp_http_100c(evp);
 					return ret;
 				} else return 1;
 		case SREV_TCP_MSRP_FRAME:
 				if(unlikely(_sr_events_list.tcp_msrp_frame!=0))
 				{
-					ret = _sr_events_list.tcp_msrp_frame(data);
+					ret = _sr_events_list.tcp_msrp_frame(evp);
 					return ret;
 				} else return 1;
 		case SREV_TCP_WS_FRAME_IN:
 				if(unlikely(_sr_events_list.tcp_ws_frame_in!=0))
 				{
-					ret = _sr_events_list.tcp_ws_frame_in(data);
+					ret = _sr_events_list.tcp_ws_frame_in(evp);
 					return ret;
 				} else return 1;
 		case SREV_TCP_WS_FRAME_OUT:
 				if(unlikely(_sr_events_list.tcp_ws_frame_out!=0))
 				{
-					ret = _sr_events_list.tcp_ws_frame_out(data);
+					ret = _sr_events_list.tcp_ws_frame_out(evp);
 					return ret;
 				} else return 1;
 		case SREV_STUN_IN:
 				if(unlikely(_sr_events_list.stun_in!=0))
 				{
-					ret = _sr_events_list.stun_in(data);
+					ret = _sr_events_list.stun_in(evp);
 					return ret;
 				} else return 1;
 		case SREV_RCV_NOSIP:
 				if(unlikely(_sr_events_list.rcv_nosip!=0))
 				{
-					ret = _sr_events_list.rcv_nosip(data);
+					ret = _sr_events_list.rcv_nosip(evp);
 					return ret;
 				} else return 1;
 		case SREV_TCP_CLOSED:
 				if(unlikely(_sr_events_list.tcp_closed!=0))
 				{
-					ret = _sr_events_list.tcp_closed(data);
+					ret = _sr_events_list.tcp_closed(evp);
 					return ret;
 				} else return 1;
 		case SREV_NET_DATA_RECV:
 				if(unlikely(_sr_events_list.net_data_recv!=0))
 				{
-					ret = _sr_events_list.net_data_recv(data);
+					ret = _sr_events_list.net_data_recv(evp);
 					return ret;
 				} else return 1;
 		case SREV_NET_DATA_SEND:
 				if(unlikely(_sr_events_list.net_data_send!=0))
 				{
-					ret = _sr_events_list.net_data_send(data);
+					ret = _sr_events_list.net_data_send(evp);
 					return ret;
 				} else return 1;
 		default:

+ 6 - 2
src/core/events.h

@@ -40,7 +40,11 @@
 
 #define SREV_CB_LIST_SIZE	3
 
-typedef int (*sr_event_cb_f)(void *data);
+typedef struct sr_event_param {
+	void *data;
+} sr_event_param_t;
+
+typedef int (*sr_event_cb_f)(sr_event_param_t *evp);
 
 typedef struct sr_event_cb {
 	sr_event_cb_f net_data_in[SREV_CB_LIST_SIZE];
@@ -62,7 +66,7 @@ typedef struct sr_event_cb {
 
 void sr_event_cb_init(void);
 int sr_event_register_cb(int type, sr_event_cb_f f);
-int sr_event_exec(int type, void *data);
+int sr_event_exec(int type, sr_event_param_t *evp);
 int sr_event_enabled(int type);
 
 

+ 9 - 5
src/core/forward.h

@@ -13,8 +13,8 @@
  * 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 this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 /*!
@@ -121,6 +121,7 @@ static inline int msg_send_buffer(struct dest_info* dst, char* buf, int len,
 	struct dest_info new_dst;
 	str outb;
 	sr_net_info_t netinfo;
+	sr_event_param_t evp = {0};
 
 #ifdef USE_TCP 
 	int port;
@@ -135,7 +136,8 @@ static inline int msg_send_buffer(struct dest_info* dst, char* buf, int len,
 	outb.s = buf;
 	outb.len = len;
 	if(!(flags&1)) {
-		sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
+		evp.data = (void*)&outb;
+		sr_event_exec(SREV_NET_DATA_OUT, &evp);
 	}
 
 	if(outb.s==NULL) {
@@ -193,7 +195,8 @@ static inline int msg_send_buffer(struct dest_info* dst, char* buf, int len,
 		wsev.buf = outb.s;
 		wsev.len = outb.len;
 		wsev.id = con->id;
-		ret = sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
+		evp.data = (void *)&wsev;
+		ret = sr_event_exec(SREV_TCP_WS_FRAME_OUT, &evp);
 		tcpconn_put(con);
 		goto done;
 	}
@@ -320,7 +323,8 @@ done:
 		netinfo.data.s = outb.s;
 		netinfo.data.len = outb.len;
 		netinfo.dst = dst;
-		sr_event_exec(SREV_NET_DATA_SEND, (void*)&netinfo);
+		evp.data = (void*)&netinfo;
+		sr_event_exec(SREV_NET_DATA_SEND, &evp);
 	}
 
 	if(outb.s != buf)

+ 7 - 3
src/core/receive.c

@@ -133,6 +133,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 	str inb;
 	sr_net_info_t netinfo;
 	sr_kemi_eng_t *keng = NULL;
+	sr_event_param_t evp = {0};
 
 	if(sr_event_enabled(SREV_NET_DATA_RECV)) {
 		if(sip_check_fline(buf, len)==0) {
@@ -140,13 +141,15 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 			netinfo.data.s = buf;
 			netinfo.data.len = len;
 			netinfo.rcv = rcv_info;
-			sr_event_exec(SREV_NET_DATA_RECV, (void*)&netinfo);
+			evp.data = (void*)&netinfo;
+			sr_event_exec(SREV_NET_DATA_RECV, &evp);
 		}
 	}
 
 	inb.s = buf;
 	inb.len = len;
-	sr_event_exec(SREV_NET_DATA_IN, (void*)&inb);
+	evp.data = (void*)&inb;
+	sr_event_exec(SREV_NET_DATA_IN, &evp);
 	len = inb.len;
 
 	msg=pkg_malloc(sizeof(struct sip_msg));
@@ -174,7 +177,8 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
 	if(likely(sr_msg_time==1)) msg_set_time(msg);
 
 	if (parse_msg(buf,len, msg)!=0){
-		if((ret=sr_event_exec(SREV_RCV_NOSIP, (void*)msg))<NONSIP_MSG_DROP) {
+		evp.data = (void*)msg;
+		if((ret=sr_event_exec(SREV_RCV_NOSIP, &evp))<NONSIP_MSG_DROP) {
 			LOG(cfg_get(core, core_cfg, corelog),
 				"core parsing of SIP message failed (%s:%d/%d)\n",
 				ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,

+ 14 - 12
src/core/stun.c

@@ -13,8 +13,8 @@
  * 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 this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
@@ -32,20 +32,22 @@
 
 int stun_process_msg(char* buf, unsigned int len, struct receive_info* ri)
 {
-        int ret;
+	int ret;
 	stun_event_info_t sev;
+	sr_event_param_t evp = {0};
 
-        ret = 0;
-        LM_DBG("STUN Message: [[>>>\n%.*s<<<]]\n", len, buf);
-        if(likely(sr_event_enabled(SREV_STUN_IN))) {
+	ret = 0;
+	LM_DBG("STUN Message: [[>>>\n%.*s<<<]]\n", len, buf);
+	if(likely(sr_event_enabled(SREV_STUN_IN))) {
 		memset(&sev, 0, sizeof(stun_event_info_t));
 		sev.buf = buf;
 		sev.len = len;
 		sev.rcv = ri;
-                ret = sr_event_exec(SREV_STUN_IN, (void *) &sev);
-        } else {
-                LM_DBG("no callback registering for handling STUN -"
-			" dropping!\n");
-        }
-        return ret;
+		evp.data = (void *)&sev;
+		ret = sr_event_exec(SREV_STUN_IN, &evp);
+	} else {
+		LM_DBG("no callback registering for handling STUN -"
+				" dropping!\n");
+	}
+	return ret;
 }

+ 9 - 3
src/core/tcp_read.c

@@ -190,6 +190,7 @@ static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_rea
 {
 	int ret;
 	tcp_closed_event_info_t tev;
+	sr_event_param_t evp = {0};
 
 	ret = 0;
 	LM_DBG("TCP closed event creation triggered\n");
@@ -197,7 +198,8 @@ static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_rea
 		memset(&tev, 0, sizeof(tcp_closed_event_info_t));
 		tev.reason = reason;
 		tev.con = con;
-		ret = sr_event_exec(SREV_TCP_CLOSED, (void*)(&tev));
+		evp.data = (void*)(&tev);
+		ret = sr_event_exec(SREV_TCP_CLOSED, &evp);
 	} else {
 		LM_DBG("no callback registering for handling TCP closed event - dropping!\n");
 	}
@@ -1052,6 +1054,7 @@ int msrp_process_msg(char* tcpbuf, unsigned int len,
 {
 	int ret;
 	tcp_event_info_t tev;
+	sr_event_param_t evp = {0};
 
 	ret = 0;
 	LM_DBG("MSRP Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
@@ -1062,7 +1065,8 @@ int msrp_process_msg(char* tcpbuf, unsigned int len,
 		tev.len = len;
 		tev.rcv = rcv_info;
 		tev.con = con;
-		ret = sr_event_exec(SREV_TCP_MSRP_FRAME, (void*)(&tev));
+		evp.data = (void*)(&tev);
+		ret = sr_event_exec(SREV_TCP_MSRP_FRAME, &evp);
 	} else {
 		LM_DBG("no callback registering for handling MSRP - dropping!\n");
 	}
@@ -1178,6 +1182,7 @@ static int ws_process_msg(char* tcpbuf, unsigned int len,
 {
 	int ret;
 	tcp_event_info_t tev;
+	sr_event_param_t evp = {0};
 
 	ret = 0;
 	LM_DBG("WebSocket Message: [[>>>\n%.*s<<<]]\n", len, tcpbuf);
@@ -1188,7 +1193,8 @@ static int ws_process_msg(char* tcpbuf, unsigned int len,
 		tev.len = len;
 		tev.rcv = rcv_info;
 		tev.con = con;
-		ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, (void*)(&tev));
+		evp.data = (void*)(&tev);
+		ret = sr_event_exec(SREV_TCP_WS_FRAME_IN, &evp);
 	} else {
 		LM_DBG("no callback registering for handling WebSockets - dropping!\n");
 	}

+ 3 - 1
src/core/udp_server.c

@@ -422,6 +422,7 @@ int udp_rcv_loop()
 	union sockaddr_union* from;
 	unsigned int fromlen;
 	struct receive_info ri;
+	sr_event_param_t evp = {0};
 
 
 	from=(union sockaddr_union*) pkg_malloc(sizeof(union sockaddr_union));
@@ -473,7 +474,8 @@ int udp_rcv_loop()
 			sredp[0] = (void*)buf;
 			sredp[1] = (void*)(&len);
 			sredp[2] = (void*)(&ri);
-			if(sr_event_exec(SREV_NET_DGRAM_IN, (void*)sredp)<0) {
+			evp.data = (void*)sredp;
+			if(sr_event_exec(SREV_NET_DGRAM_IN, &evp)<0) {
 				/* data handled by callback - continue to next packet */
 				continue;
 			}