GUACAMOLE-203: Implement keepalive config in SSH connection.

Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/8ab7e569
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/8ab7e569
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/8ab7e569

Branch: refs/heads/master
Commit: 8ab7e56972f4b7049a886cc6eb425ed2a3bdc277
Parents: f42f05a
Author: Nick Couchman <vn...@apache.org>
Authored: Wed May 31 08:00:09 2017 -0400
Committer: Nick Couchman <nick.couch...@yahoo.com>
Committed: Wed May 31 21:02:50 2017 -0400

----------------------------------------------------------------------
 src/protocols/ssh/ssh.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/8ab7e569/src/protocols/ssh/ssh.c
----------------------------------------------------------------------
diff --git a/src/protocols/ssh/ssh.c b/src/protocols/ssh/ssh.c
index 43e66f8..fc5e408 100644
--- a/src/protocols/ssh/ssh.c
+++ b/src/protocols/ssh/ssh.c
@@ -224,6 +224,11 @@ void* ssh_client_thread(void* data) {
         return NULL;
     }
 
+    /* Set keepalive configuration for session */
+    if (settings->server_alive_interval > 0) {
+        libssh2_keepalive_config(ssh_client->session->session, 1, 
settings->server_alive_interval);
+    }
+
     pthread_mutex_init(&ssh_client->term_channel_lock, NULL);
 
     /* Open channel for terminal */
@@ -318,11 +323,18 @@ void* ssh_client_thread(void* data) {
 
     /* While data available, write to terminal */
     int bytes_read = 0;
+    int timeout = 0;
     for (;;) {
 
         /* Track total amount of data read */
         int total_read = 0;
 
+        /* Set up return value for keepalives */
+        int alive = 0;
+
+        /* Timer for keepalives */
+        int sleep = 0;
+
         pthread_mutex_lock(&(ssh_client->term_channel_lock));
 
         /* Stop reading at EOF */
@@ -331,6 +343,16 @@ void* ssh_client_thread(void* data) {
             break;
         }
 
+        /* Send keepalive at configured interval */
+        if (settings->server_alive_interval > 0) {
+            alive = libssh2_keepalive_send(ssh_client->session->session, 
&timeout);
+            if (alive > 0)
+                break;
+            sleep = timeout * 1000;
+        }
+        else
+            sleep = 1000;
+
         /* Read terminal data */
         bytes_read = libssh2_channel_read(ssh_client->term_channel,
                 buffer, sizeof(buffer));
@@ -370,8 +392,8 @@ void* ssh_client_thread(void* data) {
                 .revents = 0,
             }};
 
-            /* Wait up to one second */
-            if (poll(fds, 1, 1000) < 0)
+            /* Wait up to computed sleep time */
+            if (poll(fds, 1, sleep) < 0)
                 break;
 
         }

Reply via email to