浏览代码

tcpops: framework to execute event_route[tcp:closed]

Armen Babikyan 9 年之前
父节点
当前提交
5856d98483
共有 3 个文件被更改,包括 52 次插入0 次删除
  1. 44 0
      modules/tcpops/tcpops.c
  2. 2 0
      modules/tcpops/tcpops.h
  3. 6 0
      modules/tcpops/tcpops_mod.c

+ 44 - 0
modules/tcpops/tcpops.c

@@ -31,6 +31,7 @@
 #include "../../globals.h"
 #include "../../globals.h"
 #include "../../pass_fd.h"
 #include "../../pass_fd.h"
 #include "../../timer.h"
 #include "../../timer.h"
+#include "../../sr_module.h"
 
 
 /**
 /**
  * gets the fd of the current message source connection
  * gets the fd of the current message source connection
@@ -185,3 +186,46 @@ int tcpops_set_connection_lifetime(struct tcp_connection* con, int time) {
 	LM_DBG("new connection lifetime for conid=%d: %d\n", con->id, con->timeout);
 	LM_DBG("new connection lifetime for conid=%d: %d\n", con->id, con->timeout);
 	return 1;
 	return 1;
 }
 }
+
+static void tcpops_tcp_closed_run_route(struct tcp_connection *con)
+{
+	int rt, backup_rt;
+	struct run_act_ctx ctx;
+	sip_msg_t *fmsg;
+	LM_DBG("tcp_closed_run_route event_route[tcp:closed]\n");
+
+	rt = route_get(&event_rt, "tcp:closed");
+	if (rt < 0 || event_rt.rlist[rt] == NULL)
+	{
+		LM_DBG("route does not exist");
+		return;
+	}
+
+	if (faked_msg_init() < 0)
+	{
+		LM_ERR("faked_msg_init() failed\n");
+		return;
+	}
+	fmsg = faked_msg_next();
+	fmsg->rcv = con->rcv;
+
+	backup_rt = get_route_type();
+	set_route_type(EVENT_ROUTE);
+	init_run_actions_ctx(&ctx);
+	run_top_route(event_rt.rlist[rt], fmsg, 0);
+	set_route_type(backup_rt);
+}
+
+int tcpops_handle_tcp_closed(void *data)
+{
+	tcp_event_info_t *tev = (tcp_event_info_t *) data;
+
+	if (tev == NULL || tev->con == NULL) {
+		LM_WARN("received bad TCP closed event\n");
+		return -1;
+	}
+
+	tcpops_tcp_closed_run_route(tev->con);
+
+	return 0;
+}

+ 2 - 0
modules/tcpops/tcpops.h

@@ -25,11 +25,13 @@
 #define TCP_KEEPALIVE_H_
 #define TCP_KEEPALIVE_H_
 
 
 #include "../../tcp_conn.h"
 #include "../../tcp_conn.h"
+#include "../../events.h"
 
 
 int tcpops_get_current_fd(int conid, int *fd);
 int tcpops_get_current_fd(int conid, int *fd);
 int tcpops_acquire_fd_from_tcpmain(int conid, int *fd);
 int tcpops_acquire_fd_from_tcpmain(int conid, int *fd);
 int tcpops_keepalive_enable(int fd, int idle, int count, int interval, int closefd);
 int tcpops_keepalive_enable(int fd, int idle, int count, int interval, int closefd);
 int tcpops_keepalive_disable(int fd, int closefd);
 int tcpops_keepalive_disable(int fd, int closefd);
 int tcpops_set_connection_lifetime(struct tcp_connection* con, int time);
 int tcpops_set_connection_lifetime(struct tcp_connection* con, int time);
+int tcpops_handle_tcp_closed(void *data);
 
 
 #endif /* TCP_KEEPALIVE_H_ */
 #endif /* TCP_KEEPALIVE_H_ */

+ 6 - 0
modules/tcpops/tcpops_mod.c

@@ -35,6 +35,7 @@
 #include "../../tcp_options.h"
 #include "../../tcp_options.h"
 #include "../../dprint.h"
 #include "../../dprint.h"
 #include "../../mod_fix.h"
 #include "../../mod_fix.h"
+#include "../../events.h"
 
 
 #include "tcpops.h"
 #include "tcpops.h"
 
 
@@ -101,6 +102,11 @@ static int mod_init(void)
 {
 {
 	LM_DBG("TCP keepalive module loaded.\n");
 	LM_DBG("TCP keepalive module loaded.\n");
 
 
+	if (sr_event_register_cb(SREV_TCP_CLOSED, tcpops_handle_tcp_closed) != 0) {
+		LM_ERR("problem registering tcpops_handle_tcp_closed call-back\n");
+		return -1;
+	}
+
 	return 0;
 	return 0;
 }
 }