Hello againg!
After various problems with sending and receiving keepalive in nonblocking
state(typically: - send keepalive to server through
 libssh2_keepalive_send() function and server reply OR server send
keepalive.In both cases, the event is triggered (through epoll()) and we
must handle this event.) we've created another patch that added
libssh2_check_keepalive(LIBSSH2_SESSION * session) function and adds new
parameter to LIBSSH_SESSION - keepalive_count for storing count of received
keepalives.
Problem occurs when session receives keepalive reply. This packet is
decoded as SSH_MSG_REQUEST_FAILURE but in packet type switch in packet.c
file isn't handled as this type. to resolve this, we've added new case to
handle this packet which deletes and set packAdd_state to
libssh2_NB_state_idle.
I'm not sure if this last change may affect next session state. My question
is the same, as in my previous query with datacount patch, is this patch
correct and applicable?
diff -Naur libssh2-1.4.3/include/libssh2.h libssh2-1.4.3_keepalive/include/libssh2.h
--- libssh2-1.4.3/include/libssh2.h	2012-11-27 22:45:21.000000000 +0100
+++ libssh2-1.4.3_keepalive/include/libssh2.h	2013-11-20 10:04:02.985780814 +0100
@@ -329,9 +329,9 @@
 #define LIBSSH2_HOSTKEY_HASH_SHA1                           2
 
 /* Hostkey Types */
-#define LIBSSH2_HOSTKEY_TYPE_UNKNOWN			    0
-#define LIBSSH2_HOSTKEY_TYPE_RSA			    1
-#define LIBSSH2_HOSTKEY_TYPE_DSS			    2
+#define LIBSSH2_HOSTKEY_TYPE_UNKNOWN          0
+#define LIBSSH2_HOSTKEY_TYPE_RSA          1
+#define LIBSSH2_HOSTKEY_TYPE_DSS          2
 
 /* Disconnect Codes (defined by SSH protocol) */
 #define SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT          1
@@ -1128,6 +1128,8 @@
 LIBSSH2_API void
 libssh2_agent_free(LIBSSH2_AGENT *agent);
 
+LIBSSH2_API int
+libssh2_check_keepalive(LIBSSH2_SESSION * session);
 
 /*
  * libssh2_keepalive_config()
diff -Naur libssh2-1.4.3/src/keepalive.c libssh2-1.4.3_keepalive/src/keepalive.c
--- libssh2-1.4.3/src/keepalive.c	2010-12-05 00:17:14.000000000 +0100
+++ libssh2-1.4.3_keepalive/src/keepalive.c	2013-11-14 10:52:18.000000000 +0100
@@ -96,3 +96,22 @@
 
     return 0;
 }
+
+LIBSSH2_API int
+libssh2_check_keepalive(LIBSSH2_SESSION * session)
+{
+  int rc = 1;
+  
+  while (rc > 0) {
+        rc = _libssh2_transport_read(session);
+  }
+
+  if (rc < 0 && (rc != LIBSSH2_ERROR_EAGAIN)){
+      return _libssh2_error(session, rc, "transport read");
+        return rc;
+  }
+
+  rc = session->keepalive_count;
+  session->keepalive_count = 0;
+  return rc;
+}
diff -Naur libssh2-1.4.3/src/libssh2_priv.h libssh2-1.4.3_keepalive/src/libssh2_priv.h
--- libssh2-1.4.3/src/libssh2_priv.h	2012-10-08 14:54:30.000000000 +0200
+++ libssh2-1.4.3_keepalive/src/libssh2_priv.h	2013-11-14 10:15:24.000000000 +0100
@@ -809,6 +809,7 @@
     int keepalive_interval;
     int keepalive_want_reply;
     time_t keepalive_last_sent;
+    unsigned int keepalive_count;
 };
 
 /* session.state bits */
diff -Naur libssh2-1.4.3/src/packet.c libssh2-1.4.3_keepalive/src/packet.c
--- libssh2-1.4.3/src/packet.c	2012-05-14 22:42:29.000000000 +0200
+++ libssh2-1.4.3_keepalive/src/packet.c	2013-11-20 09:54:49.921846717 +0100
@@ -568,6 +568,15 @@
               ....      request-specific data follows
             */
 
+        case SSH_MSG_REQUEST_FAILURE:
+            _libssh2_debug(session,
+                LIBSSH2_TRACE_CONN,
+                "Received keepalive response");
+
+            LIBSSH2_FREE(session, data);
+            session->packAdd_state = libssh2_NB_state_idle;
+            return 0;
+
         case SSH_MSG_GLOBAL_REQUEST:
             if(datalen >= 5) {
                 uint32_t len =0;
@@ -579,14 +588,30 @@
                                    LIBSSH2_TRACE_CONN,
                                    "Received global request type %.*s (wr %X)",
                                    len, data + 5, want_reply);
-                }
+		        }
+                if (want_reply) {
+                    int ret = -1;
+                    char *keep_message = "keepalive@";
 
+                    ret = memcmp((data + 5), keep_message, 10);
+
+                    if(datalen >= 15) {
+                        if(ret == 0){
+                            session->keepalive_count++;
+                            _libssh2_debug(session,
+                                            LIBSSH2_TRACE_CONN,
+                                            "Received keepalive packet");
+                        }
+                    }
 
-                if (want_reply) {
                     unsigned char packet = SSH_MSG_REQUEST_FAILURE;
-                  libssh2_packet_add_jump_point5:
+                   libssh2_packet_add_jump_point5:
                     session->packAdd_state = libssh2_NB_state_jump5;
                     rc = _libssh2_transport_send(session, &packet, 1, NULL, 0);
+                    _libssh2_debug(session,
+                        LIBSSH2_TRACE_CONN,
+                        "KEEPALIVE SEND RC %i", rc);
+
                     if (rc == LIBSSH2_ERROR_EAGAIN)
                         return rc;
                 }
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

Reply via email to