From 323c10d34b6000fd349398c468568b9900f75a2a Mon Sep 17 00:00:00 2001
From: Andrey Volk <andywolk@gmail.com>
Date: Sat, 9 May 2020 20:08:58 +0400
Subject: [PATCH] [sofia-sip] Timed out or not established wss should be
 destroyed immediately and avoid keep-alive functionality.

---
 libs/sofia-sip/.update                              |  2 +-
 libs/sofia-sip/libsofia-sip-ua/tport/tport.c        | 13 ++++++++++---
 .../sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c | 12 ++++++++++--
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/libs/sofia-sip/.update b/libs/sofia-sip/.update
index bbf71612f8..e6af7a2165 100644
--- a/libs/sofia-sip/.update
+++ b/libs/sofia-sip/.update
@@ -1 +1 @@
-Thu Apr 30 14:02:03 UTC 2020
+Sat May 09 16:04:04 UTC 2020
diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/tport.c b/libs/sofia-sip/libsofia-sip-ua/tport/tport.c
index 9eca15f77a..980df15c19 100644
--- a/libs/sofia-sip/libsofia-sip-ua/tport/tport.c
+++ b/libs/sofia-sip/libsofia-sip-ua/tport/tport.c
@@ -2266,6 +2266,8 @@ int tport_set_secondary_timer(tport_t *self)
     return 0;
 
   if (tport_is_closed(self)) {
+
+again:
     if (self->tp_refs == 0) {
       SU_DEBUG_7(("tport(%p): set timer at %u ms because %s\n",
 				  (void *)self, 0, "zap"));
@@ -2291,9 +2293,14 @@ int tport_set_secondary_timer(tport_t *self)
     }
   }
 
-  if (self->tp_pri->pri_vtable->vtp_next_secondary_timer)
-    self->tp_pri->pri_vtable->
-      vtp_next_secondary_timer(self, &target, &why);
+  if (self->tp_pri->pri_vtable->vtp_next_secondary_timer) {
+    if (self->tp_pri->pri_vtable->
+      vtp_next_secondary_timer(self, &target, &why) == -1) {
+      if (tport_is_closed(self)) {
+        goto again;
+      }
+    }
+  }
 
   if (su_time_cmp(target, infinity)) {
     SU_DEBUG_7(("tport(%p): set timer at %ld ms because %s\n",
diff --git a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
index 7e1dc76456..618ac8bd2f 100644
--- a/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
+++ b/libs/sofia-sip/libsofia-sip-ua/tport/tport_type_ws.c
@@ -635,6 +635,8 @@ int tport_ws_next_timer(tport_t *self,
 		SU_DEBUG_7(("%s(%p): %s to " TPN_FORMAT "%s\n",
 					__func__, (void *)self,
 					(punt == 2 ? "Timeout establishing SSL" : "Error establishing SSL"), TPN_ARGS(self->tp_name), ""));
+		if (wstp->ws.secure)
+			return -1;
 	}
 
 
@@ -646,7 +648,13 @@ int tport_ws_next_timer(tport_t *self,
 /** WS timer. */
 void tport_ws_timer(tport_t *self, su_time_t now)
 {
-  tport_recv_timeout_timer(self, now);
-  tport_keepalive_timer(self, now);
+  tport_ws_t *wstp = (tport_ws_t *)self;
+
+  if (!strcmp("wss", self->tp_protoname) && !wstp->ws.secure_established) {
+    tport_close(self);
+  } else {
+    tport_recv_timeout_timer(self, now);
+    tport_keepalive_timer(self, now);
+  }
   tport_base_timer(self, now);
 }