[sr-dev] git:master: modules/websocket: tidied up MI commands

Peter Dunkley peter.dunkley at crocodile-rcs.com
Wed Oct 2 02:32:13 CEST 2013


Module: sip-router
Branch: master
Commit: 6c7a0f3cdaee5ea0300d014f07e4d68f45c6e20b
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=6c7a0f3cdaee5ea0300d014f07e4d68f45c6e20b

Author: Peter Dunkley <peter.dunkley at crocodilertc.net>
Committer: Peter Dunkley <peter.dunkley at crocodilertc.net>
Date:   Wed Oct  2 01:29:45 2013 +0100

modules/websocket: tidied up MI commands

- Fixed leak in error situations
- Improved error responses

---

 modules/websocket/ws_conn.c  |   21 +++++++++++++++++----
 modules/websocket/ws_frame.c |   19 +++++++++++++++----
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/modules/websocket/ws_conn.c b/modules/websocket/ws_conn.c
index 27a9968..263b49f 100644
--- a/modules/websocket/ws_conn.c
+++ b/modules/websocket/ws_conn.c
@@ -440,10 +440,7 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 	int h, connections = 0, truncated = 0, order = 0, found = 0;
 	ws_connection_t *wsc;
 	struct mi_node *node = NULL;
-	struct mi_root *rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
-	
-	if (!rpl_tree)
-		return 0;
+	struct mi_root *rpl_tree;
 
 	node = cmd->node.kids;
 	if (node != NULL)
@@ -476,6 +473,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		}
 	}
 
+	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+	if (rpl_tree == NULL)
+		return 0;
+
 	WSCONN_LOCK;
 	if (order == 0)
 	{
@@ -485,7 +486,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 			while(wsc)
 			{
 				if ((found = add_node(rpl_tree, wsc)) < 0)
+				{
+					free_mi_tree(rpl_tree);
 					return 0;
+				}
 
 
 				connections += found;
@@ -508,7 +512,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		while (wsc)
 		{
 			if ((found = add_node(rpl_tree, wsc)) < 0)
+			{
+				free_mi_tree(rpl_tree);
 				return 0;
+			}
 
 			connections += found;
 			if (connections >= MAX_WS_CONNS_DUMP)
@@ -526,7 +533,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		while (wsc)
 		{
 			if ((found = add_node(rpl_tree, wsc)) < 0)
+			{
+				free_mi_tree(rpl_tree);
 				return 0;
+			}
 
 			connections += found;
 			if (connections >= MAX_WS_CONNS_DUMP)
@@ -544,7 +554,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 				"%d WebSocket connection%s found%s",
 				connections, connections == 1 ? "" : "s",
 				truncated == 1 ? "(truncated)" : "") == 0)
+	{
+		free_mi_tree(rpl_tree);
 		return 0;
+	}
 
 	return rpl_tree;
 }
diff --git a/modules/websocket/ws_frame.c b/modules/websocket/ws_frame.c
index 086cf79..503722e 100644
--- a/modules/websocket/ws_frame.c
+++ b/modules/websocket/ws_frame.c
@@ -125,6 +125,7 @@ static str str_status_too_many_params = str_init("Too many parameters");
 static str str_status_bad_param = str_init("Bad connection ID parameter");
 static str str_status_error_closing = str_init("Error closing connection");
 static str str_status_error_sending = str_init("Error sending frame");
+static str str_status_string_error = str_init("Error converting string to int");
 
 static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 {
@@ -726,7 +727,11 @@ struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
 
 	node = cmd->node.kids;
 	if (node == NULL)
-		return 0;
+	{
+		LM_WARN("no connection ID parameter\n");
+		return init_mi_tree(400, str_status_empty_param.s,
+					str_status_empty_param.len);
+	}
 	if (node->value.s == NULL || node->value.len == 0)
 	{
 		LM_WARN("empty connection ID parameter\n");
@@ -736,7 +741,8 @@ struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
 	if (str2int(&node->value, &id) < 0)
 	{
 		LM_ERR("converting string to int\n");
-		return 0;
+		return init_mi_tree(400, str_status_string_error.s,
+					str_status_string_error.len);
 	}
 	if (node->next != NULL)
 	{
@@ -772,7 +778,11 @@ static struct mi_root *mi_ping_pong(struct mi_root *cmd, void *param,
 
 	node = cmd->node.kids;
 	if (node == NULL)
-		return 0;
+	{
+		LM_WARN("no connection ID parameter\n");
+		return init_mi_tree(400, str_status_empty_param.s,
+					str_status_empty_param.len);
+	}
 	if (node->value.s == NULL || node->value.len == 0)
 	{
 		LM_WARN("empty connection ID parameter\n");
@@ -782,7 +792,8 @@ static struct mi_root *mi_ping_pong(struct mi_root *cmd, void *param,
 	if (str2int(&node->value, &id) < 0)
 	{
 		LM_ERR("converting string to int\n");
-		return 0;
+		return init_mi_tree(400, str_status_string_error.s,
+					str_status_string_error.len);
 	}
 	if (node->next != NULL)
 	{




More information about the sr-dev mailing list