aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat_libevent.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-02-26 09:51:30 -0500
committerNick Mathewson <nickm@torproject.org>2014-02-26 09:51:30 -0500
commit833d027778ba97020fb5ded1d94e4b21fbcab766 (patch)
treed7e3d9b859cc8da448e0620b8f95ca25f22c5733 /src/common/compat_libevent.c
parent79c234e0e3fa22d76029bd3b5e2c52072709cff3 (diff)
downloadtor-833d027778ba97020fb5ded1d94e4b21fbcab766.tar
tor-833d027778ba97020fb5ded1d94e4b21fbcab766.tar.gz
Monotonize the OOM-killer data timers
In a couple of places, to implement the OOM-circuit-killer defense against sniper attacks, we have counters to remember the age of cells or data chunks. These timers were based on wall clock time, which can move backwards, thus giving roll-over results for our age calculation. This commit creates a low-budget monotonic time, based on ratcheting gettimeofday(), so that even in the event of a time rollback, we don't do anything _really_ stupid. A future version of Tor should update this function to do something even less stupid here, like employ clock_gettime() or its kin.
Diffstat (limited to 'src/common/compat_libevent.c')
-rw-r--r--src/common/compat_libevent.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c
index 6655ca87d..77f8495e5 100644
--- a/src/common/compat_libevent.c
+++ b/src/common/compat_libevent.c
@@ -724,3 +724,33 @@ tor_gettimeofday_cache_clear(void)
}
#endif
+/**
+ * As tor_gettimeofday_cached, but can never move backwards in time.
+ *
+ * The returned value may diverge from wall-clock time, since wall-clock time
+ * can trivially be adjusted backwards, and this can't. Don't mix wall-clock
+ * time with these values in the same calculation.
+ *
+ * Depending on implementation, this function may or may not "smooth out" huge
+ * jumps forward in wall-clock time. It may or may not keep its results
+ * advancing forward (as opposed to stalling) if the wall-clock time goes
+ * backwards. The current implementation does neither of of these.
+ *
+ * This function is not thread-safe; do not call it outside the main thread.
+ *
+ * In future versions of Tor, this may return a time does not have its
+ * origin at the Unix epoch.
+ */
+void
+tor_gettimeofday_cached_monotonic(struct timeval *tv)
+{
+ struct timeval last_tv = { 0, 0 };
+
+ tor_gettimeofday_cached(tv);
+ if (timercmp(tv, &last_tv, <)) {
+ memcpy(tv, &last_tv, sizeof(struct timeval));
+ } else {
+ memcpy(&last_tv, tv, sizeof(struct timeval));
+ }
+}
+