r14359@Kushana: nickm | 2007-09-08 15:07:17 -0400

Add some generic skew-and-tolerance functions so we can handle time more sanely.


svn:r11406
This commit is contained in:
Nick Mathewson 2007-09-08 19:08:46 +00:00
parent eb3f24f027
commit 509bc3b4a0
3 changed files with 74 additions and 1 deletions

View File

@ -71,7 +71,7 @@ Things we'd like to do in 0.2.0.x:
- 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

View File

@ -1250,6 +1250,70 @@ parse_http_time(const char *date, struct tm *tm)
return 0;
}
/* =====
* 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
* ===== */

View File

@ -206,6 +206,15 @@ void format_local_iso_time(char *buf, time_t t);
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);