aboutsummaryrefslogtreecommitdiff
path: root/src/common/compat_libevent.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/compat_libevent.c')
-rw-r--r--src/common/compat_libevent.c70
1 files changed, 69 insertions, 1 deletions
diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c
index 200a7c65f..74b54bb85 100644
--- a/src/common/compat_libevent.c
+++ b/src/common/compat_libevent.c
@@ -13,6 +13,8 @@
#include "compat.h"
#include "compat_libevent.h"
+#include "crypto.h"
+
#include "util.h"
#include "torlog.h"
@@ -415,6 +417,14 @@ tor_check_libevent_version(const char *m, int server,
#define HEADER_VERSION _EVENT_VERSION
#endif
+/** Return a string representation of the version of Libevent that was used
+* at compilation time. */
+const char *
+tor_libevent_get_header_version_str(void)
+{
+ return HEADER_VERSION;
+}
+
/** See whether the headers we were built against differ from the library we
* linked against so much that we're likely to crash. If so, warn the
* user. */
@@ -618,7 +628,25 @@ tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
}
#endif
-#if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,1,1)
+int
+tor_init_libevent_rng(void)
+{
+ int rv = 0;
+#ifdef HAVE_EVUTIL_SECURE_RNG_INIT
+ char buf[256];
+ if (evutil_secure_rng_init() < 0) {
+ rv = -1;
+ }
+ /* Older libevent -- manually initialize the RNG */
+ crypto_rand(buf, 32);
+ evutil_secure_rng_add_bytes(buf, 32);
+ evutil_secure_rng_get_bytes(buf, sizeof(buf));
+#endif
+ return rv;
+}
+
+#if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,1,1) \
+ && !defined(TOR_UNIT_TESTS)
void
tor_gettimeofday_cached(struct timeval *tv)
{
@@ -651,5 +679,45 @@ tor_gettimeofday_cache_clear(void)
{
cached_time_hires.tv_sec = 0;
}
+
+#ifdef TOR_UNIT_TESTS
+/** For testing: force-update the cached time to a given value. */
+void
+tor_gettimeofday_cache_set(const struct timeval *tv)
+{
+ tor_assert(tv);
+ memcpy(&cached_time_hires, tv, sizeof(*tv));
+}
+#endif
#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));
+ }
+}
+