[asterisk-commits] mjordan: branch 11 r417310 - in /branches/11: ./ channels/ channels/sip/inclu...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Jun 26 07:06:35 CDT 2014
Author: mjordan
Date: Thu Jun 26 07:06:22 2014
New Revision: 417310
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=417310
Log:
res_http_websocket: Close websocket correctly and use careful fwrite
When a client takes a long time to process information received from Asterisk,
a write operation using fwrite may fail to write all information. This causes
the underlying file stream to be in an unknown state, such that the socket
must be disconnected. Unfortunately, there are two problems with this in
Asterisk's existing websocket code:
1. Periodically, during the read loop, Asterisk must write to the connected
websocket to respond to pings. As such, Asterisk maintains a reference to
the session during the loop. When ast_http_websocket_write fails, it may
cause the session to decrement its ref count, but this in and of itself
does not break the read loop. The read loop's write, on the other hand,
does not break the loop if it fails. This causes the socket to get in a
'stuck' state, preventing the client from reconnecting to the server.
2. More importantly, however, is that the fwrite in ast_http_websocket_write
fails with a large volume of data when the client takes awhile to process
the information. When it does fail, it fails writing only a portion of
the bytes. With some debugging, it was shown that this was failing in a
similar fashion to ASTERISK-12767. Switching this over to ast_careful_fwrite
with a long enough timeout solved the problem.
ASTERISK-23917 #close
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/3624/
Modified:
branches/11/UPGRADE.txt
branches/11/channels/chan_sip.c
branches/11/channels/sip/include/sip.h
branches/11/configs/sip.conf.sample
branches/11/include/asterisk/http_websocket.h
branches/11/res/res_http_websocket.c
Modified: branches/11/UPGRADE.txt
URL: http://svnview.digium.com/svn/asterisk/branches/11/UPGRADE.txt?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/UPGRADE.txt (original)
+++ branches/11/UPGRADE.txt Thu Jun 26 07:06:22 2014
@@ -20,6 +20,13 @@
===
===========================================================
+from 11.10.0 to 11.11.0
+ - Added a compatibility option for chan_sip, 'websocket_write_timeout'.
+ When a websocket connection exists where Asterisk writes a substantial
+ amount of data to the connected client, and the connected client is slow
+ to process the received data, the socket may be disconnected. In such
+ cases, it may be necessary to adjust this value. Default is 100 ms.
+
from 11.10.0 to 11.10.1
- MixMonitor AMI actions now require users to have authorization classes.
* MixMonitor - system
Modified: branches/11/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/branches/11/channels/chan_sip.c?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/channels/chan_sip.c (original)
+++ branches/11/channels/chan_sip.c Thu Jun 26 07:06:22 2014
@@ -2578,6 +2578,10 @@
goto end;
}
+ if (ast_websocket_set_timeout(session, sip_cfg.websocket_write_timeout)) {
+ goto end;
+ }
+
while ((res = ast_wait_for_input(ast_websocket_fd(session), -1)) > 0) {
char *payload;
uint64_t payload_len;
@@ -32241,6 +32245,12 @@
ast_copy_string(default_parkinglot, v->value, sizeof(default_parkinglot));
} else if (!strcasecmp(v->name, "refer_addheaders")) {
global_refer_addheaders = ast_true(v->value);
+ } else if (!strcasecmp(v->name, "websocket_write_timeout")) {
+ if (sscanf(v->value, "%30d", &sip_cfg.websocket_write_timeout) != 1
+ || sip_cfg.websocket_write_timeout < 0) {
+ ast_log(LOG_WARNING, "'%s' is not a valid websocket_write_timeout value at line %d. Using default '%d'.\n", v->value, v->lineno, AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT);
+ sip_cfg.websocket_write_timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
+ }
}
}
Modified: branches/11/channels/sip/include/sip.h
URL: http://svnview.digium.com/svn/asterisk/branches/11/channels/sip/include/sip.h?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/channels/sip/include/sip.h (original)
+++ branches/11/channels/sip/include/sip.h Thu Jun 26 07:06:22 2014
@@ -778,6 +778,7 @@
struct ast_format_cap *caps; /*!< Supported codecs */
int tcp_enabled;
int default_max_forwards; /*!< Default max forwards (SIP Anti-loop) */
+ int websocket_write_timeout; /*!< Socket write timeout for websocket transports, in ms */
};
/*! \brief The SIP socket definition */
Modified: branches/11/configs/sip.conf.sample
URL: http://svnview.digium.com/svn/asterisk/branches/11/configs/sip.conf.sample?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/configs/sip.conf.sample (original)
+++ branches/11/configs/sip.conf.sample Thu Jun 26 07:06:22 2014
@@ -228,6 +228,12 @@
;tcpauthlimit = 100 ; tcpauthlimit specifies the maximum number of
; unauthenticated sessions that will be allowed
; to connect at any given time. (default: 100)
+
+;websocket_write_timeout = 100 ; Default write timeout to set on websocket transports.
+ ; This value may need to be adjusted for connections where
+ ; Asterisk must write a substantial amount of data and the
+ ; receiving clients are slow to process the received information.
+ ; Value is in milliseconds; default is 100 ms.
transport=udp ; Set the default transports. The order determines the primary default transport.
; If tcpenable=no and the transport set is tcp, we will fallback to UDP.
Modified: branches/11/include/asterisk/http_websocket.h
URL: http://svnview.digium.com/svn/asterisk/branches/11/include/asterisk/http_websocket.h?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/include/asterisk/http_websocket.h (original)
+++ branches/11/include/asterisk/http_websocket.h Thu Jun 26 07:06:22 2014
@@ -20,6 +20,9 @@
#define _ASTERISK_HTTP_WEBSOCKET_H
#include "asterisk/optional_api.h"
+
+/*! \brief Default websocket write timeout, in ms */
+#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT 100
/*!
* \file http_websocket.h
@@ -184,4 +187,14 @@
*/
AST_OPTIONAL_API(int, ast_websocket_set_nonblock, (struct ast_websocket *session), {return -1;});
+/*!
+ * \brief Set the timeout on a non-blocking WebSocket session.
+ *
+ * \since 11.11.0
+ *
+ * \retval 0 on success
+ * \retval -1 on failure
+ */
+AST_OPTIONAL_API(int, ast_websocket_set_timeout, (struct ast_websocket *session, int timeout), {return -1;});
+
#endif
Modified: branches/11/res/res_http_websocket.c
URL: http://svnview.digium.com/svn/asterisk/branches/11/res/res_http_websocket.c?view=diff&rev=417310&r1=417309&r2=417310
==============================================================================
--- branches/11/res/res_http_websocket.c (original)
+++ branches/11/res/res_http_websocket.c Thu Jun 26 07:06:22 2014
@@ -77,6 +77,7 @@
size_t payload_len; /*!< Length of the payload */
char *payload; /*!< Pointer to the payload */
size_t reconstruct; /*!< Number of bytes before a reconstructed payload will be returned and a new one started */
+ int timeout; /*!< The timeout for operations on the socket */
unsigned int secure:1; /*!< Bit to indicate that the transport is secure */
unsigned int closing:1; /*!< Bit to indicate that the session is in the process of being closed */
unsigned int close_sent:1; /*!< Bit to indicate that the session close opcode has been sent and no further data will be sent */
@@ -207,8 +208,9 @@
session->close_sent = 1;
ao2_lock(session);
- res = (fwrite(frame, 1, 4, session->f) == 4) ? 0 : -1;
+ res = ast_careful_fwrite(session->f, session->fd, frame, 4, session->timeout);
ao2_unlock(session);
+
return res;
}
@@ -251,12 +253,12 @@
return -1;
}
- if (fwrite(frame, 1, header_size, session->f) != header_size) {
+ if (ast_careful_fwrite(session->f, session->fd, frame, header_size, session->timeout)) {
ao2_unlock(session);
return -1;
}
- if (fwrite(payload, 1, actual_length, session->f) != actual_length) {
+ if (ast_careful_fwrite(session->f, session->fd, payload, actual_length, session->timeout)) {
ao2_unlock(session);
return -1;
}
@@ -314,6 +316,13 @@
if ((flags = fcntl(session->fd, F_SETFL, flags)) == -1) {
return -1;
}
+
+ return 0;
+}
+
+int AST_OPTIONAL_API_NAME(ast_websocket_set_timeout)(struct ast_websocket *session, int timeout)
+{
+ session->timeout = timeout;
return 0;
}
@@ -462,8 +471,10 @@
}
/* Per the RFC for PING we need to send back an opcode with the application data as received */
- if (*opcode == AST_WEBSOCKET_OPCODE_PING) {
- ast_websocket_write(session, AST_WEBSOCKET_OPCODE_PONG, *payload, *payload_len);
+ if ((*opcode == AST_WEBSOCKET_OPCODE_PING) && (ast_websocket_write(session, AST_WEBSOCKET_OPCODE_PONG, *payload, *payload_len))) {
+ *payload_len = 0;
+ ast_websocket_close(session, 1009);
+ return 0;
}
session->payload = new_payload;
@@ -613,6 +624,7 @@
ao2_ref(protocol_handler, -1);
return 0;
}
+ session->timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
combined = ast_alloca(combined_length);
snprintf(combined, combined_length, "%s%s", key, WEBSOCKET_GUID);
More information about the asterisk-commits
mailing list