2016-02-27 18:48:19 +01:00
|
|
|
/* Copyright (c) 2009-2016, The Tor Project, Inc. */
|
2009-06-04 07:05:23 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file compat_libevent.c
|
|
|
|
* \brief Wrappers to handle porting between different versions of libevent.
|
|
|
|
*
|
|
|
|
* In an ideal world, we'd just use Libevent 2.0 from now on. But as of June
|
2012-06-05 01:51:00 +02:00
|
|
|
* 2012, Libevent 1.4 is still all over, and some poor souls are stuck on
|
|
|
|
* Libevent 1.3e. */
|
2009-06-04 07:05:23 +02:00
|
|
|
|
|
|
|
#include "orconfig.h"
|
2010-04-19 22:41:25 +02:00
|
|
|
#include "compat.h"
|
2015-09-15 17:20:44 +02:00
|
|
|
#define COMPAT_LIBEVENT_PRIVATE
|
2009-06-04 07:05:23 +02:00
|
|
|
#include "compat_libevent.h"
|
|
|
|
|
2014-04-16 20:54:39 +02:00
|
|
|
#include "crypto.h"
|
|
|
|
|
2009-06-04 07:05:23 +02:00
|
|
|
#include "util.h"
|
2010-07-10 03:52:20 +02:00
|
|
|
#include "torlog.h"
|
2009-06-04 07:05:23 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
|
|
|
#include <event2/event.h>
|
2011-08-17 20:44:16 +02:00
|
|
|
#include <event2/thread.h>
|
2011-08-24 23:09:56 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#endif
|
2009-06-04 07:05:23 +02:00
|
|
|
#else
|
|
|
|
#include <event.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** A string which, if it appears in a libevent log, should be ignored. */
|
|
|
|
static const char *suppress_msg = NULL;
|
|
|
|
/** Callback function passed to event_set_log() so we can intercept
|
|
|
|
* log messages from libevent. */
|
2015-09-15 17:20:44 +02:00
|
|
|
STATIC void
|
2009-06-04 07:05:23 +02:00
|
|
|
libevent_logging_callback(int severity, const char *msg)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
size_t n;
|
|
|
|
if (suppress_msg && strstr(msg, suppress_msg))
|
|
|
|
return;
|
|
|
|
n = strlcpy(buf, msg, sizeof(buf));
|
|
|
|
if (n && n < sizeof(buf) && buf[n-1] == '\n') {
|
|
|
|
buf[n-1] = '\0';
|
|
|
|
}
|
|
|
|
switch (severity) {
|
|
|
|
case _EVENT_LOG_DEBUG:
|
2013-02-01 21:43:37 +01:00
|
|
|
log_debug(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
|
2009-06-04 07:05:23 +02:00
|
|
|
break;
|
|
|
|
case _EVENT_LOG_MSG:
|
2013-02-01 21:43:37 +01:00
|
|
|
log_info(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
|
2009-06-04 07:05:23 +02:00
|
|
|
break;
|
|
|
|
case _EVENT_LOG_WARN:
|
2013-02-01 21:43:37 +01:00
|
|
|
log_warn(LD_NOCB|LD_GENERAL, "Warning from libevent: %s", buf);
|
2009-06-04 07:05:23 +02:00
|
|
|
break;
|
|
|
|
case _EVENT_LOG_ERR:
|
2013-02-01 21:43:37 +01:00
|
|
|
log_err(LD_NOCB|LD_GENERAL, "Error from libevent: %s", buf);
|
2009-06-04 07:05:23 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-02-01 21:43:37 +01:00
|
|
|
log_warn(LD_NOCB|LD_GENERAL, "Message [%d] from libevent: %s",
|
2009-06-04 07:05:23 +02:00
|
|
|
severity, buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** Set hook to intercept log messages from libevent. */
|
|
|
|
void
|
|
|
|
configure_libevent_logging(void)
|
|
|
|
{
|
|
|
|
event_set_log_callback(libevent_logging_callback);
|
|
|
|
}
|
|
|
|
/** Ignore any libevent log message that contains <b>msg</b>. */
|
|
|
|
void
|
|
|
|
suppress_libevent_log_msg(const char *msg)
|
|
|
|
{
|
|
|
|
suppress_msg = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef HAVE_EVENT2_EVENT_H
|
|
|
|
/** Work-alike replacement for event_new() on pre-Libevent-2.0 systems. */
|
|
|
|
struct event *
|
|
|
|
tor_event_new(struct event_base *base, int sock, short what,
|
|
|
|
void (*cb)(int, short, void *), void *arg)
|
|
|
|
{
|
|
|
|
struct event *e = tor_malloc_zero(sizeof(struct event));
|
|
|
|
event_set(e, sock, what, cb, arg);
|
2009-06-16 17:30:08 +02:00
|
|
|
if (! base)
|
|
|
|
base = tor_libevent_get_base();
|
2009-06-04 07:05:23 +02:00
|
|
|
event_base_set(base, e);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
/** Work-alike replacement for evtimer_new() on pre-Libevent-2.0 systems. */
|
|
|
|
struct event *
|
|
|
|
tor_evtimer_new(struct event_base *base,
|
|
|
|
void (*cb)(int, short, void *), void *arg)
|
|
|
|
{
|
|
|
|
return tor_event_new(base, -1, 0, cb, arg);
|
|
|
|
}
|
|
|
|
/** Work-alike replacement for evsignal_new() on pre-Libevent-2.0 systems. */
|
|
|
|
struct event *
|
|
|
|
tor_evsignal_new(struct event_base * base, int sig,
|
|
|
|
void (*cb)(int, short, void *), void *arg)
|
|
|
|
{
|
2009-06-18 16:07:26 +02:00
|
|
|
return tor_event_new(base, sig, EV_SIGNAL|EV_PERSIST, cb, arg);
|
2009-06-04 07:05:23 +02:00
|
|
|
}
|
2014-08-29 17:33:05 +02:00
|
|
|
/** Work-alike replacement for event_free() on pre-Libevent-2.0 systems,
|
|
|
|
* except tolerate tor_event_free(NULL). */
|
2009-06-04 07:05:23 +02:00
|
|
|
void
|
|
|
|
tor_event_free(struct event *ev)
|
|
|
|
{
|
2014-08-29 17:33:05 +02:00
|
|
|
if (ev == NULL)
|
|
|
|
return;
|
2009-06-04 07:05:23 +02:00
|
|
|
event_del(ev);
|
|
|
|
tor_free(ev);
|
|
|
|
}
|
2014-08-29 17:33:05 +02:00
|
|
|
#else
|
|
|
|
/* Wrapper for event_free() that tolerates tor_event_free(NULL) */
|
|
|
|
void
|
|
|
|
tor_event_free(struct event *ev)
|
|
|
|
{
|
|
|
|
if (ev == NULL)
|
|
|
|
return;
|
|
|
|
event_free(ev);
|
|
|
|
}
|
2009-06-04 07:05:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Global event base for use by the main thread. */
|
|
|
|
struct event_base *the_event_base = NULL;
|
|
|
|
|
2009-06-04 09:16:26 +02:00
|
|
|
/* This is what passes for version detection on OSX. We set
|
|
|
|
* MACOSX_KQUEUE_IS_BROKEN to true iff we're on a version of OSX before
|
|
|
|
* 10.4.0 (aka 1040). */
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
|
|
|
|
#define MACOSX_KQUEUE_IS_BROKEN \
|
|
|
|
(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1040)
|
|
|
|
#else
|
|
|
|
#define MACOSX_KQUEUE_IS_BROKEN 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-18 21:08:49 +02:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
static int using_iocp_bufferevents = 0;
|
2011-09-08 04:00:48 +02:00
|
|
|
static void tor_libevent_set_tick_timeout(int msec_per_tick);
|
2011-08-18 21:08:49 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
tor_libevent_using_iocp_bufferevents(void)
|
|
|
|
{
|
|
|
|
return using_iocp_bufferevents;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-04 07:05:23 +02:00
|
|
|
/** Initialize the Libevent library and set up the event base. */
|
|
|
|
void
|
2010-09-28 20:01:45 +02:00
|
|
|
tor_libevent_initialize(tor_libevent_cfg *torcfg)
|
2009-06-04 07:05:23 +02:00
|
|
|
{
|
|
|
|
tor_assert(the_event_base == NULL);
|
2010-09-29 08:50:46 +02:00
|
|
|
/* some paths below don't use torcfg, so avoid unused variable warnings */
|
|
|
|
(void)torcfg;
|
2009-06-04 09:16:26 +02:00
|
|
|
|
2009-06-04 07:05:23 +02:00
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
2010-09-28 20:01:45 +02:00
|
|
|
{
|
2011-11-15 00:12:29 +01:00
|
|
|
int attempts = 0;
|
|
|
|
int using_threads;
|
|
|
|
struct event_config *cfg;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
++attempts;
|
|
|
|
using_threads = 0;
|
|
|
|
cfg = event_config_new();
|
2011-11-14 23:53:45 +01:00
|
|
|
tor_assert(cfg);
|
2010-09-28 20:01:45 +02:00
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
|
2011-08-17 20:44:16 +02:00
|
|
|
if (! torcfg->disable_iocp) {
|
|
|
|
evthread_use_windows_threads();
|
2010-09-28 20:01:45 +02:00
|
|
|
event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP);
|
2011-08-18 21:08:49 +02:00
|
|
|
using_iocp_bufferevents = 1;
|
2011-11-15 00:12:29 +01:00
|
|
|
using_threads = 1;
|
|
|
|
} else {
|
|
|
|
using_iocp_bufferevents = 0;
|
2011-08-17 20:44:16 +02:00
|
|
|
}
|
2014-09-26 15:40:30 +02:00
|
|
|
#elif defined(__COVERITY__)
|
|
|
|
/* Avoid a 'dead code' warning below. */
|
|
|
|
using_threads = ! torcfg->disable_iocp;
|
2010-09-28 20:01:45 +02:00
|
|
|
#endif
|
|
|
|
|
2011-11-15 00:12:29 +01:00
|
|
|
if (!using_threads) {
|
|
|
|
/* Telling Libevent not to try to turn locking on can avoid a needless
|
|
|
|
* socketpair() attempt. */
|
|
|
|
event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK);
|
|
|
|
}
|
|
|
|
|
2010-09-28 20:01:45 +02:00
|
|
|
#if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,0,7)
|
|
|
|
if (torcfg->num_cpus > 0)
|
|
|
|
event_config_set_num_cpus_hint(cfg, torcfg->num_cpus);
|
|
|
|
#endif
|
|
|
|
|
2010-12-16 19:35:09 +01:00
|
|
|
#if LIBEVENT_VERSION_NUMBER >= V(2,0,9)
|
|
|
|
/* We can enable changelist support with epoll, since we don't give
|
|
|
|
* Libevent any dup'd fds. This lets us avoid some syscalls. */
|
|
|
|
event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
|
|
|
|
#endif
|
|
|
|
|
2010-09-28 20:01:45 +02:00
|
|
|
the_event_base = event_base_new_with_config(cfg);
|
2010-10-15 23:14:04 +02:00
|
|
|
|
|
|
|
event_config_free(cfg);
|
2011-11-15 00:12:29 +01:00
|
|
|
|
|
|
|
if (using_threads && the_event_base == NULL && attempts < 2) {
|
|
|
|
/* This could be a socketpair() failure, which can happen sometimes on
|
|
|
|
* windows boxes with obnoxious firewall rules. Downgrade and try
|
|
|
|
* again. */
|
2012-01-31 16:59:42 +01:00
|
|
|
#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
|
2011-11-15 00:12:29 +01:00
|
|
|
if (torcfg->disable_iocp == 0) {
|
2011-11-24 09:20:51 +01:00
|
|
|
log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again "
|
|
|
|
"with IOCP disabled.");
|
2011-11-15 00:12:29 +01:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again.");
|
|
|
|
}
|
|
|
|
|
|
|
|
torcfg->disable_iocp = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
2010-09-28 20:01:45 +02:00
|
|
|
}
|
2009-06-04 07:05:23 +02:00
|
|
|
#else
|
|
|
|
the_event_base = event_init();
|
|
|
|
#endif
|
2009-06-04 09:16:26 +02:00
|
|
|
|
2011-11-14 23:53:45 +01:00
|
|
|
if (!the_event_base) {
|
|
|
|
log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-06-04 09:16:26 +02:00
|
|
|
|
|
|
|
/* Making this a NOTICE for now so we can link bugs to a libevent versions
|
|
|
|
* or methods better. */
|
2013-02-01 21:43:37 +01:00
|
|
|
log_info(LD_GENERAL,
|
2009-06-04 09:16:26 +02:00
|
|
|
"Initialized libevent version %s using method %s. Good.",
|
|
|
|
event_get_version(), tor_libevent_get_method());
|
2011-09-08 04:00:48 +02:00
|
|
|
|
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
tor_libevent_set_tick_timeout(torcfg->msec_per_tick);
|
|
|
|
#endif
|
2009-06-04 07:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the current Libevent event base that we're set up to use. */
|
2014-01-24 05:54:50 +01:00
|
|
|
MOCK_IMPL(struct event_base *,
|
|
|
|
tor_libevent_get_base, (void))
|
2009-06-04 07:05:23 +02:00
|
|
|
{
|
2016-02-04 18:37:00 +01:00
|
|
|
tor_assert(the_event_base != NULL);
|
2009-06-04 07:05:23 +02:00
|
|
|
return the_event_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the name of the Libevent backend we're using. */
|
|
|
|
const char *
|
|
|
|
tor_libevent_get_method(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
|
|
|
return event_base_get_method(the_event_base);
|
|
|
|
#else
|
2015-03-12 21:59:05 +01:00
|
|
|
return event_get_method();
|
2009-06-04 07:05:23 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-16 23:07:55 +01:00
|
|
|
/** Return the le_version_t for the version of libevent specified in the
|
|
|
|
* string <b>v</b>. If the version is very new or uses an unrecognized
|
|
|
|
* version, format, return LE_OTHER. */
|
2015-09-15 17:20:44 +02:00
|
|
|
STATIC le_version_t
|
2009-06-04 09:16:26 +02:00
|
|
|
tor_decode_libevent_version(const char *v)
|
|
|
|
{
|
|
|
|
unsigned major, minor, patchlevel;
|
2010-07-26 06:40:44 +02:00
|
|
|
char c, e, extra;
|
2009-06-04 09:16:26 +02:00
|
|
|
int fields;
|
|
|
|
|
2010-07-26 06:40:44 +02:00
|
|
|
/* Try the new preferred "1.4.11-stable" format.
|
|
|
|
* Also accept "1.4.14b-stable". */
|
2011-07-01 17:26:30 +02:00
|
|
|
fields = tor_sscanf(v, "%u.%u.%u%c%c", &major, &minor, &patchlevel, &c, &e);
|
2009-06-04 09:16:26 +02:00
|
|
|
if (fields == 3 ||
|
2010-07-26 06:40:44 +02:00
|
|
|
((fields == 4 || fields == 5 ) && (c == '-' || c == '_')) ||
|
|
|
|
(fields == 5 && TOR_ISALPHA(c) && (e == '-' || e == '_'))) {
|
2009-06-04 09:16:26 +02:00
|
|
|
return V(major,minor,patchlevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try the old "1.3e" format. */
|
2011-07-01 17:26:30 +02:00
|
|
|
fields = tor_sscanf(v, "%u.%u%c%c", &major, &minor, &c, &extra);
|
2009-06-04 09:16:26 +02:00
|
|
|
if (fields == 3 && TOR_ISALPHA(c)) {
|
|
|
|
return V_OLD(major, minor, c);
|
|
|
|
} else if (fields == 2) {
|
|
|
|
return V(major, minor, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return LE_OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return an integer representing the binary interface of a Libevent library.
|
|
|
|
* Two different versions with different numbers are sure not to be binary
|
|
|
|
* compatible. Two different versions with the same numbers have a decent
|
|
|
|
* chance of binary compatibility.*/
|
2015-09-15 17:20:44 +02:00
|
|
|
STATIC int
|
2009-06-04 09:16:26 +02:00
|
|
|
le_versions_compatibility(le_version_t v)
|
|
|
|
{
|
|
|
|
if (v == LE_OTHER)
|
|
|
|
return 0;
|
|
|
|
if (v < V_OLD(1,0,'c'))
|
|
|
|
return 1;
|
|
|
|
else if (v < V(1,4,0))
|
|
|
|
return 2;
|
|
|
|
else if (v < V(1,4,99))
|
|
|
|
return 3;
|
|
|
|
else if (v < V(2,0,1))
|
|
|
|
return 4;
|
|
|
|
else /* Everything 2.0 and later should be compatible. */
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a string representation of the version of the currently running
|
|
|
|
* version of Libevent. */
|
|
|
|
const char *
|
|
|
|
tor_libevent_get_version_str(void)
|
|
|
|
{
|
|
|
|
return event_get_version();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(LIBEVENT_VERSION)
|
|
|
|
#define HEADER_VERSION LIBEVENT_VERSION
|
|
|
|
#elif defined(_EVENT_VERSION)
|
|
|
|
#define HEADER_VERSION _EVENT_VERSION
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 18:38:01 +02:00
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
|
2009-06-04 09:16:26 +02:00
|
|
|
/** 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. */
|
|
|
|
void
|
|
|
|
tor_check_libevent_header_compatibility(void)
|
|
|
|
{
|
|
|
|
(void) le_versions_compatibility;
|
|
|
|
(void) tor_decode_libevent_version;
|
|
|
|
|
|
|
|
/* In libevent versions before 2.0, it's hard to keep binary compatibility
|
|
|
|
* between upgrades, and unpleasant to detect when the version we compiled
|
|
|
|
* against is unlike the version we have linked against. Here's how. */
|
2015-03-12 21:59:05 +01:00
|
|
|
#if defined(HEADER_VERSION)
|
2009-06-04 09:16:26 +02:00
|
|
|
/* We have a header-file version and a function-call version. Easy. */
|
|
|
|
if (strcmp(HEADER_VERSION, event_get_version())) {
|
|
|
|
le_version_t v1, v2;
|
|
|
|
int compat1 = -1, compat2 = -1;
|
|
|
|
int verybad;
|
|
|
|
v1 = tor_decode_libevent_version(HEADER_VERSION);
|
|
|
|
v2 = tor_decode_libevent_version(event_get_version());
|
|
|
|
compat1 = le_versions_compatibility(v1);
|
|
|
|
compat2 = le_versions_compatibility(v2);
|
|
|
|
|
|
|
|
verybad = compat1 != compat2;
|
|
|
|
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(verybad ? LOG_WARN : LOG_NOTICE,
|
2009-06-04 09:16:26 +02:00
|
|
|
LD_GENERAL, "We were compiled with headers from version %s "
|
|
|
|
"of Libevent, but we're using a Libevent library that says it's "
|
|
|
|
"version %s.", HEADER_VERSION, event_get_version());
|
|
|
|
if (verybad)
|
|
|
|
log_warn(LD_GENERAL, "This will almost certainly make Tor crash.");
|
|
|
|
else
|
|
|
|
log_info(LD_GENERAL, "I think these versions are binary-compatible.");
|
|
|
|
}
|
2015-03-12 21:59:05 +01:00
|
|
|
#else
|
2009-06-04 09:16:26 +02:00
|
|
|
/* event_get_version but no _EVENT_VERSION. We might be in 1.4.0-beta or
|
|
|
|
earlier, where that's normal. To see whether we were compiled with an
|
|
|
|
earlier version, let's see whether the struct event defines MIN_HEAP_IDX.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_STRUCT_EVENT_MIN_HEAP_IDX
|
|
|
|
/* The header files are 1.4.0-beta or later. If the version is not
|
|
|
|
* 1.4.0-beta, we are incompatible. */
|
|
|
|
{
|
|
|
|
if (strcmp(event_get_version(), "1.4.0-beta")) {
|
|
|
|
log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
|
|
|
|
"Libevent 1.4.0-beta header files, whereas you have linked "
|
|
|
|
"against Libevent %s. This will probably make Tor crash.",
|
|
|
|
event_get_version());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Our headers are 1.3e or earlier. If the library version is not 1.4.x or
|
|
|
|
later, we're probably fine. */
|
|
|
|
{
|
|
|
|
const char *v = event_get_version();
|
|
|
|
if ((v[0] == '1' && v[2] == '.' && v[3] > '3') || v[0] > '1') {
|
|
|
|
log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
|
|
|
|
"Libevent header file from 1.3e or earlier, whereas you have "
|
|
|
|
"linked against Libevent %s. This will probably make Tor "
|
|
|
|
"crash.", event_get_version());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Your libevent is ancient. */
|
|
|
|
#endif
|
|
|
|
}
|
2009-06-12 21:09:09 +02:00
|
|
|
|
2010-06-25 21:31:46 +02:00
|
|
|
/*
|
|
|
|
If possible, we're going to try to use Libevent's periodic timer support,
|
|
|
|
since it does a pretty good job of making sure that periodic events get
|
|
|
|
called exactly M seconds apart, rather than starting each one exactly M
|
|
|
|
seconds after the time that the last one was run.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
|
|
|
#define HAVE_PERIODIC
|
|
|
|
#define PERIODIC_FLAGS EV_PERSIST
|
|
|
|
#else
|
|
|
|
#define PERIODIC_FLAGS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Represents a timer that's run every N microseconds by Libevent. */
|
|
|
|
struct periodic_timer_t {
|
|
|
|
/** Underlying event used to implement this periodic event. */
|
|
|
|
struct event *ev;
|
|
|
|
/** The callback we'll be invoking whenever the event triggers */
|
|
|
|
void (*cb)(struct periodic_timer_t *, void *);
|
|
|
|
/** User-supplied data for the callback */
|
|
|
|
void *data;
|
|
|
|
#ifndef HAVE_PERIODIC
|
|
|
|
/** If Libevent doesn't know how to invoke events every N microseconds,
|
|
|
|
* we'll need to remember the timeout interval here. */
|
|
|
|
struct timeval tv;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Libevent callback to implement a periodic event. */
|
|
|
|
static void
|
|
|
|
periodic_timer_cb(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
|
|
|
periodic_timer_t *timer = arg;
|
|
|
|
(void) what;
|
|
|
|
(void) fd;
|
|
|
|
#ifndef HAVE_PERIODIC
|
|
|
|
/** reschedule the event as needed. */
|
|
|
|
event_add(timer->ev, &timer->tv);
|
|
|
|
#endif
|
|
|
|
timer->cb(timer, timer->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create and schedule a new timer that will run every <b>tv</b> in
|
|
|
|
* the event loop of <b>base</b>. When the timer fires, it will
|
|
|
|
* run the timer in <b>cb</b> with the user-supplied data in <b>data</b>. */
|
|
|
|
periodic_timer_t *
|
|
|
|
periodic_timer_new(struct event_base *base,
|
|
|
|
const struct timeval *tv,
|
|
|
|
void (*cb)(periodic_timer_t *timer, void *data),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
periodic_timer_t *timer;
|
|
|
|
tor_assert(base);
|
|
|
|
tor_assert(tv);
|
|
|
|
tor_assert(cb);
|
|
|
|
timer = tor_malloc_zero(sizeof(periodic_timer_t));
|
|
|
|
if (!(timer->ev = tor_event_new(base, -1, PERIODIC_FLAGS,
|
|
|
|
periodic_timer_cb, timer))) {
|
|
|
|
tor_free(timer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
timer->cb = cb;
|
|
|
|
timer->data = data;
|
|
|
|
#ifndef HAVE_PERIODIC
|
|
|
|
memcpy(&timer->tv, tv, sizeof(struct timeval));
|
|
|
|
#endif
|
2010-06-25 22:14:21 +02:00
|
|
|
event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/
|
2010-06-25 21:31:46 +02:00
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Stop and free a periodic timer */
|
|
|
|
void
|
|
|
|
periodic_timer_free(periodic_timer_t *timer)
|
|
|
|
{
|
|
|
|
if (!timer)
|
|
|
|
return;
|
|
|
|
tor_event_free(timer->ev);
|
|
|
|
tor_free(timer);
|
|
|
|
}
|
2010-07-25 13:24:31 +02:00
|
|
|
|
2010-02-22 19:59:34 +01:00
|
|
|
#ifdef USE_BUFFEREVENTS
|
|
|
|
static const struct timeval *one_tick = NULL;
|
|
|
|
/**
|
2010-04-04 03:34:42 +02:00
|
|
|
* Return a special timeout to be passed whenever libevent's O(1) timeout
|
|
|
|
* implementation should be used. Only use this when the timer is supposed
|
2011-09-08 04:00:48 +02:00
|
|
|
* to fire after msec_per_tick ticks have elapsed.
|
2010-02-22 19:59:34 +01:00
|
|
|
*/
|
2010-09-27 18:41:41 +02:00
|
|
|
const struct timeval *
|
|
|
|
tor_libevent_get_one_tick_timeout(void)
|
2010-02-22 19:59:34 +01:00
|
|
|
{
|
2011-09-08 04:00:48 +02:00
|
|
|
tor_assert(one_tick);
|
2010-02-22 19:59:34 +01:00
|
|
|
return one_tick;
|
|
|
|
}
|
2011-08-24 23:09:56 +02:00
|
|
|
|
2011-09-08 04:00:48 +02:00
|
|
|
/** Initialize the common timeout that we'll use to refill the buckets every
|
|
|
|
* time a tick elapses. */
|
|
|
|
static void
|
|
|
|
tor_libevent_set_tick_timeout(int msec_per_tick)
|
|
|
|
{
|
|
|
|
struct event_base *base = tor_libevent_get_base();
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tor_assert(! one_tick);
|
|
|
|
tv.tv_sec = msec_per_tick / 1000;
|
|
|
|
tv.tv_usec = (msec_per_tick % 1000) * 1000;
|
|
|
|
one_tick = event_base_init_common_timeout(base, &tv);
|
|
|
|
}
|
|
|
|
|
2011-08-24 23:09:56 +02:00
|
|
|
static struct bufferevent *
|
|
|
|
tor_get_root_bufferevent(struct bufferevent *bev)
|
|
|
|
{
|
|
|
|
struct bufferevent *u;
|
|
|
|
while ((u = bufferevent_get_underlying(bev)) != NULL)
|
|
|
|
bev = u;
|
|
|
|
return bev;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tor_set_bufferevent_rate_limit(struct bufferevent *bev,
|
|
|
|
struct ev_token_bucket_cfg *cfg)
|
|
|
|
{
|
|
|
|
return bufferevent_set_rate_limit(tor_get_root_bufferevent(bev), cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tor_add_bufferevent_to_rate_limit_group(struct bufferevent *bev,
|
|
|
|
struct bufferevent_rate_limit_group *g)
|
|
|
|
{
|
|
|
|
return bufferevent_add_to_rate_limit_group(tor_get_root_bufferevent(bev), g);
|
|
|
|
}
|
2010-02-22 19:59:34 +01:00
|
|
|
#endif
|
|
|
|
|
2014-04-16 20:54:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-09 18:47:24 +01:00
|
|
|
#if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,1,1) \
|
|
|
|
&& !defined(TOR_UNIT_TESTS)
|
2012-06-15 16:31:34 +02:00
|
|
|
void
|
|
|
|
tor_gettimeofday_cached(struct timeval *tv)
|
|
|
|
{
|
|
|
|
event_base_gettimeofday_cached(the_event_base, tv);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
tor_gettimeofday_cache_clear(void)
|
|
|
|
{
|
|
|
|
event_base_update_cache_time(the_event_base);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/** Cache the current hi-res time; the cache gets reset when libevent
|
|
|
|
* calls us. */
|
|
|
|
static struct timeval cached_time_hires = {0, 0};
|
|
|
|
|
|
|
|
/** Return a fairly recent view of the current time. */
|
|
|
|
void
|
|
|
|
tor_gettimeofday_cached(struct timeval *tv)
|
|
|
|
{
|
|
|
|
if (cached_time_hires.tv_sec == 0) {
|
|
|
|
tor_gettimeofday(&cached_time_hires);
|
|
|
|
}
|
|
|
|
*tv = cached_time_hires;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reset the cached view of the current time, so that the next time we try
|
|
|
|
* to learn it, we will get an up-to-date value. */
|
|
|
|
void
|
|
|
|
tor_gettimeofday_cache_clear(void)
|
|
|
|
{
|
|
|
|
cached_time_hires.tv_sec = 0;
|
|
|
|
}
|
2014-01-09 18:47:24 +01:00
|
|
|
|
|
|
|
#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
|
2012-06-15 16:31:34 +02:00
|
|
|
#endif
|
2012-06-15 22:12:24 +02:00
|
|
|
|
2014-02-26 15:51:30 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
2014-11-12 19:28:07 +01:00
|
|
|
if (timercmp(tv, &last_tv, OP_LT)) {
|
2014-02-26 15:51:30 +01:00
|
|
|
memcpy(tv, &last_tv, sizeof(struct timeval));
|
|
|
|
} else {
|
|
|
|
memcpy(&last_tv, tv, sizeof(struct timeval));
|
|
|
|
}
|
|
|
|
}
|
2015-10-02 13:13:58 +02:00
|
|
|
|