[Asterisk-code-review] res_http_websocket: Use mask for client messages (asterisk[master])

Nickolay V. Shmyrev asteriskteam at digium.com
Tue Jun 2 18:27:44 CDT 2020


Nickolay V. Shmyrev has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/14453 )


Change subject: res_http_websocket:  Use mask for client messages
......................................................................

res_http_websocket:  Use mask for client messages

According to websocket protocol specification
https://tools.ietf.org/html/rfc6455#section-5.1 websocket clients MUST
use masking when sending data. Implement zero mask for interoperability
with websocket servers.

ASTERISK-28914 #close
Reported-by: Nickolay Shmyrev <nshmyrev at alphacephei.com>

Change-Id: I9649e294f35489ae852a4bbb309ae32ef2a0689e
---
M res/res_http_websocket.c
1 file changed, 22 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/53/14453/1

diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 63fccdd..4599985 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -287,7 +287,8 @@
 int AST_OPTIONAL_API_NAME(ast_websocket_close)(struct ast_websocket *session, uint16_t reason)
 {
 	enum ast_websocket_opcode opcode = AST_WEBSOCKET_OPCODE_CLOSE;
-	char frame[4] = { 0, }; /* The header is 2 bytes and the reason code takes up another 2 bytes */
+	char frame[8] = { 0, };
+	int header_size, frame_size;
 	int res;
 
 	if (session->close_sent) {
@@ -297,21 +298,29 @@
 	frame[0] = opcode | 0x80;
 	frame[1] = 2; /* The reason code is always 2 bytes */
 
+	if (session->client) {
+		frame[1] |= 0x80;
+		header_size = 6;
+	} else {
+		header_size = 2;
+	}
+	frame_size = header_size + 2;
+
 	/* If no reason has been specified assume 1000 which is normal closure */
-	put_unaligned_uint16(&frame[2], htons(reason ? reason : 1000));
+	put_unaligned_uint16(&frame[header_size], htons(reason ? reason : 1000));
 
 	session->closing = 1;
 	session->close_sent = 1;
 
 	ao2_lock(session);
 	ast_iostream_set_timeout_inactivity(session->stream, session->timeout);
-	res = ast_iostream_write(session->stream, frame, sizeof(frame));
+	res = ast_iostream_write(session->stream, frame, frame_size);
 	ast_iostream_set_timeout_disable(session->stream);
 
 	/* 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 != sizeof(frame)) {
+	if (res != frame_size) {
 		ast_iostream_close(session->stream);
 		session->stream = NULL;
 		ast_verb(2, "WebSocket connection %s '%s' forcefully closed due to fatal write error\n",
@@ -364,6 +373,10 @@
 		header_size += 8;
 	}
 
+	if (session->client) {
+		header_size += 4;
+	}
+
 	frame_size = header_size + payload_size;
 
 	frame = ast_alloca(frame_size + 1);
@@ -372,6 +385,11 @@
 	frame[0] = opcode | 0x80;
 	frame[1] = length;
 
+	/* We introduce mask but keep mask zero for now */
+	if (session->client) {
+		frame[1] |= 0x80;
+	}
+
 	/* Use the additional available bytes to store the length */
 	if (length == 126) {
 		put_unaligned_uint16(&frame[2], htons(payload_size));

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14453
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I9649e294f35489ae852a4bbb309ae32ef2a0689e
Gerrit-Change-Number: 14453
Gerrit-PatchSet: 1
Gerrit-Owner: Nickolay V. Shmyrev <nshmyrev at alphacephei.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200602/b2030836/attachment-0001.html>


More information about the asterisk-code-review mailing list