summaryrefslogtreecommitdiff
path: root/res/res_pjsip_transport_websocket.c
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2014-06-26 12:21:14 +0000
committerMatthew Jordan <mjordan@digium.com>2014-06-26 12:21:14 +0000
commit365ae7523b45f18abb1418f498561cc2c8cbf680 (patch)
tree2d1ce4e889fedf5885299baef55a16df464f7a21 /res/res_pjsip_transport_websocket.c
parentd171e0b2e96ca1cc2cf6c53cdd9d5a3c876be91b (diff)
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. Note that this version of the patch, unlike r417310 in Asterisk 11, exposes configuration options beyond just chan_sip's sip.conf. Configuration options to configure the write timeout have also been added to pjsip.conf and ari.conf. #ASTERISK-23917 #close Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/3624/ ........ Merged revisions 417310 from http://svn.asterisk.org/svn/asterisk/branches/11 ........ Merged revisions 417311 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@417317 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_pjsip_transport_websocket.c')
-rw-r--r--res/res_pjsip_transport_websocket.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/res/res_pjsip_transport_websocket.c b/res/res_pjsip_transport_websocket.c
index 22962dab0..bae120a19 100644
--- a/res/res_pjsip_transport_websocket.c
+++ b/res/res_pjsip_transport_websocket.c
@@ -207,6 +207,37 @@ static int transport_read(void *data)
return (read_data->payload_len == recvd) ? 0 : -1;
}
+static int get_write_timeout(void)
+{
+ int write_timeout = -1;
+ struct ao2_container *transports;
+
+ transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_ALL, NULL);
+
+ if (transports) {
+ struct ao2_iterator it_transports = ao2_iterator_init(transports, 0);
+ struct ast_sip_transport *transport;
+
+ for (; (transport = ao2_iterator_next(&it_transports)); ao2_cleanup(transport)) {
+ if (transport->type != AST_TRANSPORT_WS && transport->type != AST_TRANSPORT_WSS) {
+ continue;
+ }
+ ast_debug(5, "Found %s transport with write timeout: %d\n",
+ transport->type == AST_TRANSPORT_WS ? "WS" : "WSS",
+ transport->write_timeout);
+ write_timeout = MAX(write_timeout, transport->write_timeout);
+ }
+ ao2_cleanup(transports);
+ }
+
+ if (write_timeout < 0) {
+ write_timeout = AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT;
+ }
+
+ ast_debug(1, "Write timeout for WS/WSS transports: %d\n", write_timeout);
+ return write_timeout;
+}
+
/*!
\brief WebSocket connection handler.
*/
@@ -222,6 +253,11 @@ static void websocket_cb(struct ast_websocket *session, struct ast_variable *par
return;
}
+ if (ast_websocket_set_timeout(session, get_write_timeout())) {
+ ast_websocket_unref(session);
+ return;
+ }
+
if (!(serializer = ast_sip_create_serializer())) {
ast_websocket_unref(session);
return;