[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3 of 6 V1] Implement session socket byte counter


 include/libssh/counters.h |  23 +++++++++++++++++++++++
 include/libssh/session.h  |   4 ++++
 src/counters.c            |   8 ++++++++
 src/socket.c              |   4 ++++
 4 files changed, 39 insertions(+), 0 deletions(-)


# HG changeset patch
# User Audrius Butkevicius <audrius.butkevicius@xxxxxxxxxxxxxxxx>
# Date 1389803762 0
#      Wed Jan 15 16:36:02 2014 +0000
# Node ID 754710d2feba31833ecdd111b0a9948549bcbb5a
# Parent  c3b966140111f5829a791289b64e13795be1e162
Implement session socket byte counter

diff --git a/include/libssh/counters.h b/include/libssh/counters.h
--- a/include/libssh/counters.h
+++ b/include/libssh/counters.h
@@ -25,6 +25,7 @@
 #ifndef _SSH_COUNTERS_H_
 #define _SSH_COUNTERS_H_
 
+#include <libssh/libssh.h>
 #include <stdint.h>
 
 #ifdef __cplusplus
@@ -54,6 +55,28 @@
 
 typedef struct ssh_packet_counter_struct *ssh_packet_counter;
 
+ /**
+ * @brief Set the session data counters.
+ *
+ * This functions sets the counter structures to be used to calculate data
+ * which come in and go out through the session at various points in time.
+ *
+ * @code
+ * struct ssh_byte_counter_struct scounter = {
+ *     .in_bytes = 0,
+ *     .out_bytes = 0
+ * };
+ *
+ * ssh_set_session_counters(session, &scounter);
+ * @endcode
+ *
+ * @param  session      The session to set the counter structures.
+ *
+ * @param  scounter     The byte counter structure for data passed to sockets.
+ */
+LIBSSH_API void ssh_set_session_counters(ssh_session session,
+                                         ssh_bytes_counter scounter);
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/include/libssh/session.h b/include/libssh/session.h
--- a/include/libssh/session.h
+++ b/include/libssh/session.h
@@ -27,6 +27,7 @@
 #include "libssh/auth.h"
 #include "libssh/channels.h"
 #include "libssh/poll.h"
+#include "libssh/counters.h"
 
 /* These are the different states a SSH session can be into its life */
 enum ssh_session_state_e {
@@ -187,6 +188,9 @@
         char *gss_client_identity;
         int gss_delegate_creds;
     } opts;
+
+    /* counters */
+    ssh_bytes_counter socket_byte_counter;
 };
 
 /** @internal
diff --git a/src/counters.c b/src/counters.c
--- a/src/counters.c
+++ b/src/counters.c
@@ -22,3 +22,11 @@
  */
 
 #include "libssh/counters.h"
+#include "libssh/session.h"
+
+void ssh_set_session_counters(ssh_session session, ssh_bytes_counter scounter) {
+    if (session == NULL)
+        return;
+
+    session->socket_byte_counter = scounter;
+}
diff --git a/src/socket.c b/src/socket.c
--- a/src/socket.c
+++ b/src/socket.c
@@ -280,6 +280,8 @@
 			}
 		}
 		if(r>0){
+            if (s->session->socket_byte_counter)
+                s->session->socket_byte_counter->in_bytes += r;
 			/* Bufferize the data and then call the callback */
             r = buffer_add_data(s->in_buffer,buffer,r);
             if (r < 0) {
@@ -657,6 +659,8 @@
       return SSH_ERROR;
     }
     buffer_pass_bytes(s->out_buffer, w);
+    if (s->session->socket_byte_counter)
+        s->session->socket_byte_counter->out_bytes += w;
   }
 
   /* Is there some data pending? */

References:
[PATCH 0 of 6 V1] CountersAudrius Butkevicius <audrius.butkevicius@xxxxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org