websocket: Consider pending SSL data when waiting for socket input

When TLS is in use, checking the readiness of the underlying FD is insufficient
for determining if there is data available to be read. So before polling the
FD, check if there is any buffered data in the TLS layer and use that first.

ASTERISK-28562 #close
Reported by: Robert Sutton

Change-Id: I95fcb3e2004700d5cf8e5ee04943f0115b15e10d
This commit is contained in:
Sean Bright
2019-11-26 14:24:10 -05:00
parent 3818759e9c
commit 47ba42f4a0
5 changed files with 51 additions and 4 deletions

View File

@@ -427,6 +427,11 @@ int AST_OPTIONAL_API_NAME(ast_websocket_fd)(struct ast_websocket *session)
return session->closing ? -1 : ast_iostream_get_fd(session->stream);
}
int AST_OPTIONAL_API_NAME(ast_websocket_wait_for_input)(struct ast_websocket *session, int timeout)
{
return session->closing ? -1 : ast_iostream_wait_for_input(session->stream, timeout);
}
struct ast_sockaddr * AST_OPTIONAL_API_NAME(ast_websocket_remote_address)(struct ast_websocket *session)
{
return &session->remote_address;
@@ -545,8 +550,8 @@ static inline int ws_safe_read(struct ast_websocket *session, char *buf, size_t
break;
}
}
if (ast_wait_for_input(ast_iostream_get_fd(session->stream), 1000) < 0) {
ast_log(LOG_ERROR, "ast_wait_for_input returned err: %s\n", strerror(errno));
if (ast_iostream_wait_for_input(session->stream, 1000) < 0) {
ast_log(LOG_ERROR, "ast_iostream_wait_for_input returned err: %s\n", strerror(errno));
*opcode = AST_WEBSOCKET_OPCODE_CLOSE;
session->closing = 1;
ao2_unlock(session);
@@ -974,7 +979,7 @@ static void websocket_echo_callback(struct ast_websocket *session, struct ast_va
goto end;
}
while ((res = ast_wait_for_input(ast_websocket_fd(session), -1)) > 0) {
while ((res = ast_websocket_wait_for_input(session, -1)) > 0) {
char *payload;
uint64_t payload_len;
enum ast_websocket_opcode opcode;