Subject: [PATCH] - datacount

[PATCH] - datacount

From: Lubos Uhliarik <uhliarik_at_seznam.cz>
Date: Wed, 13 Aug 2014 15:10:49 +0200

Hello Daniel,

my colleague wrote you an email before about 9 months. Company in which
I'm working would like to cooperate with you on libssh2 development,
because we are using this lib in several projects.

We made a few patches, but we would like to move those patches to the
upstream version. In this email, I will try introduce you first patch,
which we would like to apply on the upstream version.

This first patch should count total amount of transferred data during
the connection to a server. For this purpose, two functions were
created:

libssh2_session_recv_data(LIBSSH2_SESSION *session) and
libssh2_session_recv_data(LIBSSH2_SESSION *session).

Better explanation of this PATCH sent my colleague Jiri. You can read
his email here:

http://www.libssh2.org/mail/libssh2-devel-archive-2013-11/0027.shtml

If there is any problem with applying this patch, please let me know and
I will try to fix those problems.

Regards,
Lubos

Signed-off-by: Lubos Uhliarik <uhliarik_at_seznam.cz>

---
diff -Naur libssh2-1.4.3_keepalive/include/libssh2.h
libssh2-1.4.3_datacount/include/libssh2.h
--- libssh2-1.4.3_keepalive/include/libssh2.h	2013-11-20
10:04:02.985780814 +0100
+++ libssh2-1.4.3_datacount/include/libssh2.h	2013-11-22
13:15:10.870131905 +0100
@@ -241,10 +241,10 @@
 /* I/O callbacks */
 #define LIBSSH2_RECV_FUNC(name)  ssize_t name(libssh2_socket_t socket,
\
                                               void *buffer, size_t
length, \
-                                              int flags, void
**abstract)
+                                              int flags, void
**abstract, LIBSSH2_SESSION *session)
 #define LIBSSH2_SEND_FUNC(name)  ssize_t name(libssh2_socket_t socket,
\
                                               const void *buffer,
size_t length,\
-                                              int flags, void
**abstract)
+                                              int flags, void
**abstract, LIBSSH2_SESSION *session)
 
 /* libssh2_session_callback_set() constants */
 #define LIBSSH2_CALLBACK_IGNORE             0
@@ -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
@@ -1159,6 +1159,12 @@
 LIBSSH2_API int libssh2_keepalive_send (LIBSSH2_SESSION *session,
                                         int *seconds_to_next);
 
+LIBSSH2_API long long 
+libssh2_session_recv_data(LIBSSH2_SESSION *session);
+
+LIBSSH2_API long long
+libssh2_session_send_data(LIBSSH2_SESSION *session);
+
 /* NOTE NOTE NOTE
    libssh2_trace() has no function in builds that aren't built with
debug
    enabled
diff -Naur libssh2-1.4.3_keepalive/src/libssh2_priv.h
libssh2-1.4.3_datacount/src/libssh2_priv.h
--- libssh2-1.4.3_keepalive/src/libssh2_priv.h	2013-11-14
10:15:24.000000000 +0100
+++ libssh2-1.4.3_datacount/src/libssh2_priv.h	2013-11-22
13:37:21.543860697 +0100
@@ -184,9 +184,9 @@
                       (channel), &(channel)->abstract)
 
 #define LIBSSH2_SEND_FD(session, fd, buffer, length, flags) \
-    session->send(fd, buffer, length, flags, &session->abstract)
+    session->send(fd, buffer, length, flags, &session->abstract,
session)
 #define LIBSSH2_RECV_FD(session, fd, buffer, length, flags) \
-    session->recv(fd, buffer, length, flags, &session->abstract)
+    session->recv(fd, buffer, length, flags, &session->abstract,
session)
 
 #define LIBSSH2_SEND(session, buffer, length, flags)  \
     LIBSSH2_SEND_FD(session, session->socket_fd, buffer, length, flags)
@@ -810,6 +810,8 @@
     int keepalive_want_reply;
     time_t keepalive_last_sent;
     unsigned int keepalive_count;
+    long long recv_data;
+    long long send_data;
 };
 
 /* session.state bits */
@@ -1001,9 +1003,9 @@
 #define SSH_OPEN_RESOURCE_SHORTAGE           4
 
 ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer,
-                      size_t length, int flags, void **abstract);
+                      size_t length, int flags, void **abstract,
LIBSSH2_SESSION *session);
 ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer,
-                      size_t length, int flags, void **abstract);
+                      size_t length, int flags, void **abstract,
LIBSSH2_SESSION *session);
 
 #define LIBSSH2_READ_TIMEOUT 60 /* generic timeout in seconds used when
                                    waiting for more data to arrive */
diff -Naur libssh2-1.4.3_keepalive/src/misc.c
libssh2-1.4.3_datacount/src/misc.c
--- libssh2-1.4.3_keepalive/src/misc.c	2011-08-25 19:59:47.000000000
+0200
+++ libssh2-1.4.3_datacount/src/misc.c	2013-11-22 11:25:39.000000000
+0100
@@ -94,7 +94,7 @@
  * Replacement for the standard recv, return -errno on failure.
  */
 ssize_t
-_libssh2_recv(libssh2_socket_t sock, void *buffer, size_t length, int
flags, void **abstract)
+_libssh2_recv(libssh2_socket_t sock, void *buffer, size_t length, int
flags, void **abstract, LIBSSH2_SESSION *session)
 {
     ssize_t rc = recv(sock, buffer, length, flags);
 #ifdef WIN32
@@ -117,6 +117,7 @@
             return -errno;
     }
 #endif
+    session->recv_data += rc;
     return rc;
 }
 
@@ -126,7 +127,7 @@
  */
 ssize_t
 _libssh2_send(libssh2_socket_t sock, const void *buffer, size_t length,
-              int flags, void **abstract)
+              int flags, void **abstract, LIBSSH2_SESSION *session)
 {
     ssize_t rc = send(sock, buffer, length, flags);
 #ifdef WIN32
@@ -143,6 +144,7 @@
     if (rc < 0 )
         return -errno;
 #endif
+    session->send_data += rc;
     return rc;
 }
 
diff -Naur libssh2-1.4.3_keepalive/src/session.c
libssh2-1.4.3_datacount/src/session.c
--- libssh2-1.4.3_keepalive/src/session.c	2012-07-25 01:03:27.000000000
+0200
+++ libssh2-1.4.3_datacount/src/session.c	2013-11-22 11:28:00.000000000
+0100
@@ -1749,3 +1749,20 @@
 
     return (const char *) session->remote.banner;
 }
+
+LIBSSH2_API long long 
+libssh2_session_recv_data(LIBSSH2_SESSION *session)
+{
+    long long recv;
+    recv = session->recv_data;
+    session->recv_data = 0;
+    return recv;
+}
+
+LIBSSH2_API long long
+libssh2_session_send_data(LIBSSH2_SESSION *session){
+    long long send;
+    send = session->send_data;
+    session->send_data = 0;
+    return send;
+}
\ No newline at end of file
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2014-08-13