[or-cvs] r11406: Add some generic skew-and-tolerance functions so we can hand (in tor/trunk: . doc src/common)

nickm at seul.org nickm at seul.org
Sat Sep 8 19:08:46 UTC 2007


Author: nickm
Date: 2007-09-08 15:08:46 -0400 (Sat, 08 Sep 2007)
New Revision: 11406

Modified:
   tor/trunk/
   tor/trunk/doc/TODO
   tor/trunk/src/common/util.c
   tor/trunk/src/common/util.h
Log:
 r14359 at Kushana:  nickm | 2007-09-08 15:07:17 -0400
 Add some generic skew-and-tolerance functions so we can handle time more sanely.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14359] on c95137ef-5f19-0410-b913-86e773d04f59

Modified: tor/trunk/doc/TODO
===================================================================
--- tor/trunk/doc/TODO	2007-09-08 19:08:39 UTC (rev 11405)
+++ tor/trunk/doc/TODO	2007-09-08 19:08:46 UTC (rev 11406)
@@ -71,7 +71,7 @@
         - Dump certificates with the wrong time.  Or just warn?
         - Warn authority ops when their certs are nearly invalid.
         - When checking a consensus, make sure that its times are plausible.
-        - Add a function that will eventually tell us about our clock skew.
+        o Add a function that will eventually tell us about our clock skew.
           For now, just require that authorities not be skewed.
       - Start caching consensus documents once authorities make them
       - Start downloading and using consensus documents once caches serve them

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2007-09-08 19:08:39 UTC (rev 11405)
+++ tor/trunk/src/common/util.c	2007-09-08 19:08:46 UTC (rev 11406)
@@ -1251,6 +1251,70 @@
 }
 
 /* =====
+ * Fuzzy time
+ * ===== */
+
+/* In a perfect world, everybody would run ntp, and ntp would be perfect, so
+ * if we wanted to know "Is the current time before time X?" we could just say
+ * "time(NULL) < X".
+ *
+ * But unfortunately, many users are running Tor in an imperfect world, on
+ * even more imperfect computers.  Hence, we need to track time oddly.  We
+ * model the user's computer as being "skewed" from accurate time by
+ * -<b>ftime_skew</b> seconds, such that our best guess of the current time is
+ * time(NULL)+ftime_skew.  We also assume that our measurements of time may
+ * have up to <b>ftime_slop</b> seconds of inaccuracy; hence, the 
+ * measurements;
+ */
+static int ftime_skew = 0;
+static int ftime_slop = 60;
+void
+ftime_set_maximum_sloppiness(int seconds)
+{
+  tor_assert(seconds >= 0);
+  ftime_slop = seconds;
+}
+void
+ftime_set_estimated_skew(int seconds)
+{
+  ftime_skew = seconds;
+}
+#if 0
+void
+ftime_get_window(time_t now, ftime_t *ft_out)
+{
+  ft_out->earliest = now + ftime_skew - ftime_slop;
+  ft_out->latest =  now + ftime_skew + ftime_slop;
+}
+#endif
+int
+ftime_maybe_after(time_t now, time_t when)
+{
+  /* It may be after when iff the latest possible current time is after when. */
+  return (now + ftime_skew + ftime_slop) >= when;
+}
+int
+ftime_maybe_before(time_t now, time_t when)
+{
+  /* It may be before when iff the earliest possible current time is before. */
+  return (now + ftime_skew - ftime_slop) < when;
+}
+int
+ftime_definitely_after(time_t now, time_t when)
+{
+  /* It is definitely after when if the earliest time it could be is still
+   * after when. */
+  return (now + ftime_skew - ftime_slop) >= when;
+}
+int
+ftime_definitely_before(time_t now, time_t when)
+{
+  /* It is definitely before when if the latest time it could be is still
+   * before when. */
+  return (now + ftime_skew + ftime_slop) < when;
+}
+
+/* =====
  * File helpers
  * ===== */
 

Modified: tor/trunk/src/common/util.h
===================================================================
--- tor/trunk/src/common/util.h	2007-09-08 19:08:39 UTC (rev 11405)
+++ tor/trunk/src/common/util.h	2007-09-08 19:08:46 UTC (rev 11406)
@@ -206,6 +206,15 @@
 void format_iso_time(char *buf, time_t t);
 int parse_iso_time(const char *buf, time_t *t);
 int parse_http_time(const char *buf, struct tm *tm);
+/* Fuzzy time. */
+void ftime_set_maximum_sloppiness(int seconds);
+void ftime_set_estimated_skew(int seconds);
+/* typedef struct ftime_t { time_t earliest; time_t latest; } ftime_t; */
+/* void ftime_get_window(time_t now, ftime_t *ft_out); */
+int ftime_maybe_after(time_t now, time_t when);
+int ftime_maybe_before(time_t now, time_t when);
+int ftime_definitely_after(time_t now, time_t when);
+int ftime_definitely_before(time_t now, time_t when);
 
 /* File helpers */
 int write_all(int fd, const char *buf, size_t count, int isSocket);



More information about the tor-commits mailing list