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

Re: Guard selection time and expiry



On Tue, Jan 19, 2010 at 11:25:25AM -0500, Paul Syverson wrote:
> Pick a random timestamp during the last four weeks and an expiry [...]
> 60 days [...] after the timestamp.)

I like this one. I'm going to go with it. As a bonus, it means we can
cut more code.

Thanks,
--Roger

diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 7eafeb3..458df02 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -78,7 +78,6 @@ static int count_acceptable_routers(smartlist_t *routers);
 static int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
 
 static void entry_guards_changed(void);
-static time_t start_of_month(time_t when);
 
 /** Make a note that we're running unit tests (rather than running Tor
  * itself), so we avoid clobbering our state file. */
@@ -3022,7 +3021,9 @@ add_an_entry_guard(routerinfo_t *chosen, int reset_status)
   log_info(LD_CIRC, "Chose '%s' as new entry guard.", router->nickname);
   strlcpy(entry->nickname, router->nickname, sizeof(entry->nickname));
   memcpy(entry->identity, router->cache_info.identity_digest, DIGEST_LEN);
-  entry->chosen_on_date = start_of_month(time(NULL));
+  /* Choose expiry time smudged over the past month. For details, see
+   * http://archives.seul.org/or/dev/Jan-2010/msg00004.html */
+  entry->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
   entry->chosen_by_version = tor_strdup(VERSION);
   if (chosen) /* prepend */
     smartlist_insert(entry_guards, 0, entry);
@@ -3074,7 +3075,7 @@ static int
 remove_obsolete_entry_guards(void)
 {
   int changed = 0, i;
-  time_t this_month = start_of_month(time(NULL));
+  time_t now = time(NULL);
 
   for (i = 0; i < smartlist_len(entry_guards); ++i) {
     entry_guard_t *entry = smartlist_get(entry_guards, i);
@@ -3094,9 +3095,8 @@ remove_obsolete_entry_guards(void)
                 !tor_version_as_new_as(ver, "0.2.0.6-alpha"))) {
       msg = "was selected without regard for guard bandwidth";
       version_is_bad = 1;
-    } else if (entry->chosen_on_date + 3600*24*35 < this_month) {
-      /* It's been more than a month, and probably more like two since
-       * chosen_on_date is clipped to the beginning of its month. */
+    } else if (entry->chosen_on_date + 3600*24*60 < now) {
+      /* It's been 2 months since the date listed in our state file. */
       msg = "was selected several months ago";
       date_is_bad = 1;
     }
@@ -3594,19 +3594,6 @@ choose_random_entry(cpath_build_state_t *state)
   return r;
 }
 
-/** Helper: Return the start of the month containing <b>time</b>. */
-static time_t
-start_of_month(time_t now)
-{
-  struct tm tm;
-  tor_gmtime_r(&now, &tm);
-  tm.tm_sec = 0;
-  tm.tm_min = 0;
-  tm.tm_hour = 0;
-  tm.tm_mday = 1;
-  return tor_timegm(&tm);
-}
-
 /** Parse <b>state</b> and learn about the entry guards it describes.
  * If <b>set</b> is true, and there are no errors, replace the global
  * entry_list with what we find.
@@ -3715,7 +3702,7 @@ entry_guards_parse_state(or_state_t *state, int set, char 
      } else {
        if (state_version) {
          e->chosen_by_version = tor_strdup(state_version);
-         e->chosen_on_date = start_of_month(time(NULL));
+         e->chosen_on_date = time(NULL) - crypto_rand_int(3600*24*30);
        }
      }
    });