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

[or-cvs] r18287: {tor} patch from matt to implement 'getinfo status/clients-seen' (in tor/trunk: doc/spec src/or)



Author: arma
Date: 2009-01-28 01:50:36 -0500 (Wed, 28 Jan 2009)
New Revision: 18287

Modified:
   tor/trunk/doc/spec/control-spec.txt
   tor/trunk/src/or/control.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/router.c
Log:
patch from matt to implement 'getinfo status/clients-seen'


Modified: tor/trunk/doc/spec/control-spec.txt
===================================================================
--- tor/trunk/doc/spec/control-spec.txt	2009-01-28 06:46:14 UTC (rev 18286)
+++ tor/trunk/doc/spec/control-spec.txt	2009-01-28 06:50:36 UTC (rev 18287)
@@ -581,6 +581,11 @@
     "status/version/current"
       Status of the current version. One of: new, old, unrecommended,
       recommended, new in series, obsolete.
+    "status/clients-seen"
+      A summary of which countries we've seen clients from recently,
+      formatted the same as the CLIENTS_SEEN status event described in
+      Section 4.1.14. This GETINFO option is currently available only
+      for bridge relays.
 
   Examples:
      C: GETINFO version desc/name/moria1

Modified: tor/trunk/src/or/control.c
===================================================================
--- tor/trunk/src/or/control.c	2009-01-28 06:46:14 UTC (rev 18286)
+++ tor/trunk/src/or/control.c	2009-01-28 06:50:36 UTC (rev 18287)
@@ -1837,6 +1837,25 @@
         log_warn(LD_GENERAL, "%s is deprecated; it no longer gives useful "
                  "information", question);
       }
+    } else if (!strcmp(question, "status/clients-seen")) {
+      char geoip_start[ISO_TIME_LEN+1];
+      char *geoip_summary;
+      smartlist_t *sl;
+
+      geoip_summary = extrainfo_get_client_geoip_summary(time(NULL));
+      if (!geoip_summary)
+        return -1;
+      format_iso_time(geoip_start, geoip_get_history_start());
+
+      sl = smartlist_create();
+      smartlist_add(sl, (char *)"TimeStarted=\"");
+      smartlist_add(sl, geoip_start);
+      smartlist_add(sl, (char *)"\" CountrySummary=");
+      smartlist_add(sl, geoip_summary);
+      *answer = smartlist_join_strings(sl, "", 0, 0);
+
+      tor_free(geoip_summary);
+      smartlist_free(sl);
     } else {
       return 0;
     }
@@ -1937,6 +1956,8 @@
       "circuits."),
   DOC("status/bootstrap-phase",
       "The last bootstrap phase status event that Tor sent."),
+  DOC("status/clients-seen",
+      "Breakdown of client countries seen by a bridge."),
   DOC("status/version/recommended", "List of currently recommended versions."),
   DOC("status/version/current", "Status of the current version."),
   DOC("status/version/num-versioning", "Number of versioning authorities."),
@@ -3931,7 +3952,7 @@
 control_event_clients_seen(const char *timestarted, const char *countries)
 {
   send_control_event(EVENT_CLIENTS_SEEN, 0,
-    "650 CLIENTS_SEEN Timestarted=\"%s\" CountrySummary=%s\r\n",
+    "650 CLIENTS_SEEN TimeStarted=\"%s\" CountrySummary=%s\r\n",
     timestarted, countries);
 }
 

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2009-01-28 06:46:14 UTC (rev 18286)
+++ tor/trunk/src/or/or.h	2009-01-28 06:50:36 UTC (rev 18287)
@@ -4233,6 +4233,7 @@
                                  crypto_pk_env_t *ident_key);
 int extrainfo_dump_to_string(char *s, size_t maxlen, extrainfo_t *extrainfo,
                              crypto_pk_env_t *ident_key);
+char *extrainfo_get_client_geoip_summary(time_t);
 int is_legal_nickname(const char *s);
 int is_legal_nickname_or_hexdigest(const char *s);
 int is_legal_hexdigest(const char *s);

Modified: tor/trunk/src/or/router.c
===================================================================
--- tor/trunk/src/or/router.c	2009-01-28 06:46:14 UTC (rev 18286)
+++ tor/trunk/src/or/router.c	2009-01-28 06:50:36 UTC (rev 18287)
@@ -1863,19 +1863,7 @@
     return -1;
 
   if (should_record_bridge_info(options)) {
-    static time_t last_purged_at = 0;
-    char *geoip_summary;
-    time_t now = time(NULL);
-    int geoip_purge_interval = 48*60*60;
-#ifdef ENABLE_GEOIP_STATS
-    if (get_options()->DirRecordUsageByCountry)
-      geoip_purge_interval = get_options()->DirRecordUsageRetainIPs;
-#endif
-    if (now > last_purged_at+geoip_purge_interval) {
-      geoip_remove_old_clients(now-geoip_purge_interval);
-      last_purged_at = now;
-    }
-    geoip_summary = geoip_get_client_history(time(NULL), GEOIP_CLIENT_CONNECT);
+    char *geoip_summary = extrainfo_get_client_geoip_summary(time(NULL));
     if (geoip_summary) {
       char geoip_start[ISO_TIME_LEN+1];
       format_iso_time(geoip_start, geoip_get_history_start());
@@ -1918,6 +1906,27 @@
   return (int)strlen(s)+1;
 }
 
+/** Return a newly allocated comma-separated string containing entries for all
+ * the countries from which we've seen enough clients connect over the
+ * previous 48 hours. The entry format is cc=num where num is the number of
+ * IPs we've seen connecting from that country, and cc is a lowercased
+ * country code. Returns NULL if we don't want to export geoip data yet. */
+char *
+extrainfo_get_client_geoip_summary(time_t now)
+{
+  static time_t last_purged_at = 0;
+  int geoip_purge_interval = 48*60*60;
+#ifdef ENABLE_GEOIP_STATS
+  if (get_options()->DirRecordUsageByCountry)
+    geoip_purge_interval = get_options()->DirRecordUsageRetainIPs;
+#endif
+  if (now > last_purged_at+geoip_purge_interval) {
+    geoip_remove_old_clients(now-geoip_purge_interval);
+    last_purged_at = now;
+  }
+  return geoip_get_client_history(now, GEOIP_CLIENT_CONNECT);
+}
+
 /** Return true iff <b>s</b> is a legally valid server nickname. */
 int
 is_legal_nickname(const char *s)