[Asterisk-code-review] res http websocket: Forcefully terminate on write errors. (asterisk[certified/13.1])

Joshua Colp asteriskteam at digium.com
Wed Aug 12 05:15:22 CDT 2015


Joshua Colp has uploaded a new change for review.

  https://gerrit.asterisk.org/1075

Change subject: res_http_websocket: Forcefully terminate on write errors.
......................................................................

res_http_websocket: Forcefully terminate on write errors.

The res_http_websocket module will currently attempt to close
the WebSocket connection if fatal cases occur, such as when
attempting to write out data and being unable to. When the
fatal cases occur the code attempts to write a WebSocket close
frame out to have the remote side close the connection. If
writing this fails then the connection is not terminated.

This change forcefully terminates the connection if the
WebSocket is to be closed but is unable to send the close frame.

ASTERISK-25312 #close

Change-Id: I10973086671cc192a76424060d9ec8e688602845
---
M res/res_http_websocket.c
1 file changed, 28 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/1075/1

diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 4f42ef0..8319579 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -263,6 +263,17 @@
 
 	ao2_lock(session);
 	res = ast_careful_fwrite(session->f, session->fd, frame, 4, session->timeout);
+
+	/* If an error occurred when trying to close this connection explicitly terminate it now.
+	 * Doing so will cause the thread polling on it to wake up and terminate.
+	 */
+	if (res) {
+		fclose(session->f);
+		session->f = NULL;
+		ast_verb(2, "WebSocket connection %s '%s' forcefully closed due to fatal write error\n",
+			session->client ? "to" : "from", ast_sockaddr_stringify(&session->address));
+	}
+
 	ao2_unlock(session);
 	return res;
 }
@@ -417,6 +428,14 @@
 	size_t rlen;
 	int xlen = len;
 	char *rbuf = buf;
+
+	ao2_lock(session);
+	if (!session->f) {
+		ao2_unlock(session);
+		errno = ECONNABORTED;
+		return -1;
+	}
+
 	for (sanity = 10; sanity; sanity--) {
 		clearerr(session->f);
 		rlen = fread(rbuf, 1, xlen, session->f);
@@ -424,6 +443,7 @@
 			ast_log(LOG_ERROR, "Error reading from web socket: %s\n", strerror(errno));
 			*opcode = AST_WEBSOCKET_OPCODE_CLOSE;
 			session->closing = 1;
+			ao2_unlock(session);
 			return -1;
 		}
 		xlen = xlen - rlen;
@@ -435,6 +455,7 @@
 			ast_log(LOG_ERROR, "ast_wait_for_input returned err: %s\n", strerror(errno));
 			*opcode = AST_WEBSOCKET_OPCODE_CLOSE;
 			session->closing = 1;
+			ao2_unlock(session);
 			return -1;
 		}
 	}
@@ -442,8 +463,10 @@
 		ast_log(LOG_WARNING, "Websocket seems unresponsive, disconnecting ...\n");
 		*opcode = AST_WEBSOCKET_OPCODE_CLOSE;
 		session->closing = 1;
+		ao2_unlock(session);
 		return -1;
 	}
+	ao2_unlock(session);
 	return 0;
 }
 
@@ -460,7 +483,7 @@
 	*fragmented = 0;
 
 	if (ws_safe_read(session, &buf[0], MIN_WS_HDR_SZ, opcode)) {
-		return 0;
+		return -1;
 	}
 	frame_size += MIN_WS_HDR_SZ;
 
@@ -478,7 +501,7 @@
 		if (options_len) {
 			/* read the rest of the header options */
 			if (ws_safe_read(session, &buf[frame_size], options_len, opcode)) {
-				return 0;
+				return -1;
 			}
 			frame_size += options_len;
 		}
@@ -507,7 +530,7 @@
 		}
 
 		if (ws_safe_read(session, *payload, *payload_len, opcode)) {
-			return 0;
+			return -1;
 		}
 		/* If a mask is present unmask the payload */
 		if (mask_present) {
@@ -530,7 +553,7 @@
 					session->payload, session->payload_len, *payload_len);
 				*payload_len = 0;
 				ast_websocket_close(session, 1009);
-				return 0;
+				return -1;
 			}
 
 			session->payload = new_payload;
@@ -567,7 +590,7 @@
 		/* Make the payload available so the user can look at the reason code if they so desire */
 		if ((*payload_len) && (new_payload = ast_realloc(session->payload, *payload_len))) {
 			if (ws_safe_read(session, &buf[frame_size], (*payload_len), opcode)) {
-				return 0;
+				return -1;
 			}
 			session->payload = new_payload;
 			memcpy(session->payload, &buf[frame_size], *payload_len);

-- 
To view, visit https://gerrit.asterisk.org/1075
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10973086671cc192a76424060d9ec8e688602845
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: certified/13.1
Gerrit-Owner: Joshua Colp <jcolp at digium.com>



More information about the asterisk-code-review mailing list