[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [obfsproxy/master] Tweak code.



commit b91353387ed9bbcf4f237aa4803bc54160f29fb3
Author: George Kadianakis <desnacked@xxxxxxxxxx>
Date:   Mon Feb 13 18:26:44 2012 +0200

    Tweak code.
    
    * Free the heartbeat periodic event on halt.
    
    * Turn status.c:connections to an unsigned int.
    
    * Delete connections_init() and inline its code in status_init(). It's
      already inlined in the rest of the code.
    
    * Wrap connections_clear() into descriptive macros.
    
    * Use 'day(s)', 'hour(s)', 'minute(s)', instead of 'days', 'hours',
      'minutes'.
    
    * Write some documentation.
    
    * Heartbeat should log on 'notice'.
---
 src/main.c   |    5 ++++-
 src/status.c |   46 ++++++++++++++++------------------------------
 src/status.h |    3 +++
 3 files changed, 23 insertions(+), 31 deletions(-)

diff --git a/src/main.c b/src/main.c
index 607a153..0d690b7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -271,10 +271,13 @@ obfsproxy_cleanup()
   evdns_base_free(get_evdns_base(), 0);
   event_free(sig_int);
   event_free(sig_term);
+  if (heartbeat)
+    event_free(heartbeat);
+
   event_base_free(get_event_base());
 
   cleanup_crypto();
-  status_cleanup();
+  status_connections_cleanup();
   close_obfsproxy_logfile();
 }
 
diff --git a/src/status.c b/src/status.c
index 80e1764..4fcb144 100644
--- a/src/status.c
+++ b/src/status.c
@@ -10,6 +10,7 @@
 
 #include "util.h"
 
+#include "status.h"
 #include "container.h"
 
 #include <string.h>
@@ -20,22 +21,15 @@
 #include <event2/listener.h>
 
 /** Count total connections. */
-static int connections = 0;
+static unsigned int connections = 0;
 
 /** Count connections from unique addresses. */
 static strmap_t *addresses = NULL;
 
-/** Initialize counting connections from unique addresses. */
-static void
-connections_init(void)
-{
-  if (!addresses)
-    addresses = strmap_new();
-}
-
-/** Clear connection counters. */
-static void
-connections_clear(int reinit)
+/** Clear connection counters. If <b>reinit</b> is false, don't
+    reinitialize the info-keeping structures. */
+void
+status_connections_clear(int reinit)
 {
   connections = 0;
   if (!addresses)
@@ -55,7 +49,7 @@ status_note_connection(const char *addrport)
   if ((p = strrchr(addr, ':'))) {
     *p = '\0';
   } else {
-    log_debug("Error in address %s: port expected.", addrport);
+    log_warn("Error in address %s: port expected.", addrport);
     free(addr);
     return;
   }
@@ -74,7 +68,7 @@ static time_t started = 0;
 static time_t last_reset_counters = 0;
 
 /** Reset status information this often. */
-#define RESET_COUNTERS 86400
+#define RESET_COUNTERS 86400 /* 86400 seconds == 24 hours */
 
 /** Initialize status information to print out heartbeat messages. */
 void
@@ -83,16 +77,8 @@ status_init(void)
   time_t now = time(NULL);
   started = now;
   last_reset_counters = now;
-  connections_init();
-}
-
-/** Free all memory used by printing heartbeat logs. */
-void
-status_cleanup(void)
-{
-  started = 0;
-  last_reset_counters = 0;
-  connections_clear(0);
+  if (!addresses)
+    addresses = strmap_new();
 }
 
 /** Log information about our uptime and the number of connections we saw.
@@ -106,18 +92,18 @@ status_log_heartbeat(void)
   int hours = (int)((secs - (days * 86400)) / 3600);
   int last_reset_hours = (int) (now - last_reset_counters) / 3600;
   int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
-  log_info("Heartbeat: obfsproxy's uptime is %ld days, %d hours, and "
-           "%d minutes.", days, hours, minutes);
+  log_notice("Heartbeat: obfsproxy's uptime is %ld day(s), %d hour(s), and "
+             "%d minute(s).", days, hours, minutes);
 
   /* Also log connection stats, if we are keeping notes. */
   if (strmap_size(addresses) > 0)
-    log_info("Heartbeat: During the last %d hours we saw %d connections "
-             "from %d unique addresses.",
-             last_reset_hours, connections, strmap_size(addresses));
+    log_notice("Heartbeat: During the last %d hour(s) we saw %u connection(s) "
+               "from %d unique address(es).",
+               last_reset_hours, connections, strmap_size(addresses));
 
   if (now - last_reset_counters >= RESET_COUNTERS) {
     log_info("Resetting connection counters.");
-    connections_clear(1);
+    status_connections_reinit();
     last_reset_counters += RESET_COUNTERS;
   }
 }
diff --git a/src/status.h b/src/status.h
index 54dfb9c..e556a5b 100644
--- a/src/status.h
+++ b/src/status.h
@@ -11,3 +11,6 @@ void status_init(void);
 void status_cleanup(void);
 void status_note_connection(const char *addrport);
 void status_log_heartbeat(void);
+void status_connections_clear(int reinit);
+#define status_connections_cleanup() status_connections_clear(0)
+#define status_connections_reinit() status_connections_clear(1)

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits