[sr-dev] git:pd/websocket: modules/websocket: Added connection close code and tidied up MI commands

Peter Dunkley peter.dunkley at crocodile-rcs.com
Thu Jun 21 14:51:50 CEST 2012


Module: sip-router
Branch: pd/websocket
Commit: 7d35945ec66caa286db66cd55ef56c78f29c434c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7d35945ec66caa286db66cd55ef56c78f29c434c

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Thu Jun 21 13:50:53 2012 +0100

modules/websocket: Added connection close code and tidied up MI commands

---

 modules/websocket/ws_frame.c |   14 ++++++++------
 modules/websocket/ws_mod.c   |   41 +++++++++++++++++++++++++++--------------
 2 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/modules/websocket/ws_frame.c b/modules/websocket/ws_frame.c
index fba01f5..4e36b59 100644
--- a/modules/websocket/ws_frame.c
+++ b/modules/websocket/ws_frame.c
@@ -202,7 +202,7 @@ static int decode_and_validate_ws_frame(ws_frame_t *frame)
 	return frame->opcode;
 }
 
-static int encode_and_send_ws_frame(ws_frame_t *frame)
+static int encode_and_send_ws_frame(ws_frame_t *frame, int conn_close)
 {
 	int pos = 0, extended_length;
 	unsigned int frame_length;
@@ -291,6 +291,8 @@ static int encode_and_send_ws_frame(ws_frame_t *frame)
 	memcpy(&send_buf[pos], frame->payload_data, frame->payload_len);
 
 	init_dst_from_rcv(&dst, &frame->tcpinfo->con->rcv);
+	if (conn_close) dst.send_flags.f |= SND_F_CON_CLOSE;
+
 	if (tcp_send(&dst, NULL, send_buf, frame_length) < 0)
 	{
 		LM_ERR("sending WebSocket frame\n");
@@ -335,7 +337,9 @@ static int handle_close(ws_frame_t *frame)
 
 	LM_INFO("Close: %hu %.*s\n", code, reason.len, reason.s); 
 
-	/* TODO: cleanly close TCP/TLS connection */
+	/* Close socket */
+	frame->tcpinfo->con->state = S_CONN_BAD;
+	frame->tcpinfo->con->timeout = get_ticks_raw();
 
 	return 0;
 }
@@ -347,7 +351,7 @@ static int handle_ping(ws_frame_t *frame)
 	frame->opcode = OPCODE_PONG;
 	frame->mask = 0;
 
-	encode_and_send_ws_frame(frame);
+	encode_and_send_ws_frame(frame, 0);
 
 	return 0;
 }
@@ -470,15 +474,13 @@ struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
 	frame.payload_data = data;
 	frame.tcpinfo = &tcpinfo;
 
-	if (encode_and_send_ws_frame(&frame) < 0)
+	if (encode_and_send_ws_frame(&frame, 1) < 0)
 	{
 		LM_ERR("sending WebSocket close\n");
 		pkg_free(data);
 		return init_mi_tree(500,"Sending WebSocket close", 23);
 	}
 
-	/* TODO: cleanly close TCP/TLS connection */
-
 	update_stat(ws_local_closed_connections, 1);
 	update_stat(ws_current_connections, -1);
 
diff --git a/modules/websocket/ws_mod.c b/modules/websocket/ws_mod.c
index 1bf1dc0..fb86069 100644
--- a/modules/websocket/ws_mod.c
+++ b/modules/websocket/ws_mod.c
@@ -91,12 +91,12 @@ static stat_export_t stats[] =
 
 static mi_export_t mi_cmds[] =
 {
-	{ "ws_close",   ws_mi_close,   0, 0, 0 },
-	{ "ws_disable", ws_mi_disable, 0, 0, 0 },
-	{ "ws_dump",	mi_dump,       0, 0, 0 },
-	{ "ws_enable",	ws_mi_enable,  0, 0, 0 },
-	{ "ws_ping",    ws_mi_ping,    0, 0, 0 },
-	{ "ws_pong",	ws_mi_pong,    0, 0, 0 },
+	{ "ws.close",   ws_mi_close,   0, 0, 0 },
+	{ "ws.disable", ws_mi_disable, 0, 0, 0 },
+	{ "ws.dump",	mi_dump,       0, 0, 0 },
+	{ "ws.enable",	ws_mi_enable,  0, 0, 0 },
+	{ "ws.ping",    ws_mi_ping,    0, 0, 0 },
+	{ "ws.pong",	ws_mi_pong,    0, 0, 0 },
 	{ 0, 0, 0, 0, 0 }
 };
 
@@ -181,6 +181,10 @@ static struct mi_root *mi_dump(struct mi_root *cmd, void *param)
 	char *src_proto, *dst_proto;
 	char src_ip[IP6_MAX_STR_SIZE + 1], dst_ip[IP6_MAX_STR_SIZE + 1];
 	struct tcp_connection *c;
+	struct mi_root *rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+
+	if (!rpl_tree)
+		return 0;
 
 	TCPCONN_LOCK;
 	for (h = 0; h < TCP_ID_HASH_SIZE; h++)
@@ -202,12 +206,18 @@ static struct mi_root *mi_dump(struct mi_root *cmd, void *param)
 				ip_addr2sbuf(&c->rcv.dst_ip, src_ip,
 						IP6_MAX_STR_SIZE);
 
-				LM_ERR("id - %d, "
-					"src - %s:%s:%hu, "
-					"dst - %s:%s:%hu\n",
-					c->id,
-					src_proto, src_ip, c->rcv.src_port,
-					dst_proto, dst_ip, c->rcv.dst_port);
+				if (addf_mi_node_child(&rpl_tree->node, 0, 0, 0,
+						"id - %d, "
+						"src - %s:%s:%hu, "
+						"dst - %s:%s:%hu",
+						c->id,
+						src_proto,
+						strlen(src_ip) ? src_ip : "*",
+						c->rcv.src_port,
+						dst_proto,
+						strlen(dst_ip) ? dst_ip : "*",
+						c->rcv.dst_port) == 0)
+					return 0;
 
 				connections++;
 			}
@@ -217,7 +227,10 @@ static struct mi_root *mi_dump(struct mi_root *cmd, void *param)
 	}
 	TCPCONN_UNLOCK;
 
-	LM_ERR("%d WebSocket connections found\n", connections);
+	if (addf_mi_node_child(&rpl_tree->node, 0, 0, 0,
+				"%d WebSocket connection%s found",
+				connections, connections == 1 ? "" : "s") == 0)
+		return 0;
 
-	return init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+	return rpl_tree;
 }




More information about the sr-dev mailing list