Commit 265e40b4 authored by Sebastian Hahn's avatar Sebastian Hahn
Browse files

Raise libevent dependency to 2.0.10-stable or newer

Only some very ancient distributions don't ship with Libevent 2 anymore,
even the oldest supported Ubuntu LTS version has it. This allows us to
get rid of a lot of compat code.
parent ec6ea662
......@@ -176,7 +176,7 @@ coverage-html-full: all
genhtml --branch-coverage -o "$(HTML_COVER_DIR)" "$(HTML_COVER_DIR)/lcov.info"
# Avoid strlcpy.c, strlcat.c, aes.c, OpenBSD_malloc_Linux.c, sha256.c,
# eventdns.[hc], tinytest*.[ch]
# tinytest*.[ch]
check-spaces:
$(top_srcdir)/scripts/maint/checkSpace.pl -C \
$(top_srcdir)/src/common/*.[ch] \
......
......@@ -481,7 +481,7 @@ TOR_SEARCH_LIBRARY(libevent, $trylibeventdir, [-levent $STATIC_LIBEVENT_FLAGS $T
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <event.h>], [
#include <event2/event.h>], [
#ifdef _WIN32
#include <winsock2.h>
#endif
......@@ -500,21 +500,10 @@ save_CPPFLAGS="$CPPFLAGS"
LIBS="-levent $STATIC_LIBEVENT_FLAGS $TOR_LIB_WS32 $LIBS"
LDFLAGS="$TOR_LDFLAGS_libevent $LDFLAGS"
CPPFLAGS="$TOR_CPPFLAGS_libevent $CPPFLAGS"
AC_CHECK_FUNCS([event_get_version_number \
evutil_secure_rng_set_urandom_device_file \
evutil_secure_rng_init \
])
AC_CHECK_MEMBERS([struct event.min_heap_idx], , ,
[#include <event.h>
])
AC_CHECK_HEADERS(event2/event.h event2/dns.h event2/bufferevent_ssl.h)
AC_CHECK_FUNCS([evutil_secure_rng_set_urandom_device_file])
LIBS="$STATIC_LIBEVENT_FLAGS $TOR_LIB_WS32 $save_LIBS"
AM_CONDITIONAL(USE_EXTERNAL_EVDNS,
test "x$ac_cv_header_event2_dns_h" = "xyes")
if test "$enable_static_libevent" = "yes"; then
if test "$tor_cv_library_libevent_dir" = "(system)"; then
AC_MSG_ERROR("You must specify an explicit --with-libevent-dir=x option when using --enable-static-libevent")
......@@ -541,32 +530,34 @@ LIBS="$save_LIBS"
LDFLAGS="$save_LDFLAGS"
CPPFLAGS="$save_CPPFLAGS"
dnl This isn't the best test for Libevent 2.0.3-alpha. Once it's released,
dnl we can do much better.
if test "$enable_bufferevents" = "yes"; then
if test "$ac_cv_header_event2_bufferevent_ssl_h" != "yes"; then
AC_MSG_ERROR([You've asked for bufferevent support, but you're using a version of Libevent without SSL support. This won't work. We need Libevent 2.0.8-rc or later, and you don't seem to even have Libevent 2.0.3-alpha.])
else
CPPFLAGS="$CPPFLAGS $TOR_CPPFLAGS_libevent"
# Check for the right version. First see if version detection works.
AC_MSG_CHECKING([whether we can detect the Libevent version])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
dnl Check that libevent is at least at version 2.0.10, the first stable
dnl release of its series
CPPFLAGS="$CPPFLAGS $TOR_CPPFLAGS_libevent"
AC_MSG_CHECKING([whether Libevent is new enough])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <event2/event.h>
#if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 10
#if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 0x02000a00
#error
int x = y(zz);
#else
int x = 1;
#endif
])], [event_version_number_works=yes; AC_MSG_RESULT([yes]) ],
[event_version_number_works=no; AC_MSG_RESULT([no])])
if test "$event_version_number_works" != "yes"; then
AC_MSG_WARN([Version detection on Libevent seems broken. Your Libevent installation is probably screwed up or very old.])
else
AC_MSG_CHECKING([whether Libevent is new enough for bufferevents])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
])], [ AC_MSG_RESULT([yes]) ],
[ AC_MSG_RESULT([no])
AC_MSG_ERROR([Libevent is not new enough. We require 2.0.10-stable or later]) ] )
LIBS="$save_LIBS"
LDFLAGS="$save_LDFLAGS"
CPPFLAGS="$save_CPPFLAGS"
dnl bufferents require version 2.0.13
if test "$enable_bufferevents" = "yes"; then
AC_CHECK_HEADERS(event2/bufferevent_ssl.h)
CPPFLAGS="$CPPFLAGS $TOR_CPPFLAGS_libevent"
AC_MSG_CHECKING([whether Libevent is new enough for bufferevents])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <event2/event.h>
#if !defined(LIBEVENT_VERSION_NUMBER) || LIBEVENT_VERSION_NUMBER < 0x02000d00
#error
......@@ -577,8 +568,6 @@ int x = 1;
])], [ AC_MSG_RESULT([yes]) ],
[ AC_MSG_RESULT([no])
AC_MSG_ERROR([Libevent does not seem new enough to support bufferevents. We require 2.0.13-stable or later]) ] )
fi
fi
fi
LIBS="$save_LIBS"
......
......@@ -33,8 +33,6 @@
# files that we've snarfed in from somebody else, whose C we do no intend
# to document for them.
SKIP_FILES = [ "OpenBSD_malloc_Linux.c",
"eventdns.c",
"eventdns.h",
"strlcat.c",
"strlcpy.c",
"sha256.c",
......
......@@ -19,15 +19,11 @@
#include "util.h"
#include "torlog.h"
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#include <event2/thread.h>
#ifdef USE_BUFFEREVENTS
#include <event2/bufferevent.h>
#endif
#else
#include <event.h>
#endif
/** A string which, if it appears in a libevent log, should be ignored. */
static const char *suppress_msg = NULL;
......@@ -69,6 +65,7 @@ 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)
......@@ -76,44 +73,6 @@ 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);
if (! base)
base = tor_libevent_get_base();
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)
{
return tor_event_new(base, sig, EV_SIGNAL|EV_PERSIST, cb, arg);
}
/** Work-alike replacement for event_free() on pre-Libevent-2.0 systems,
* except tolerate tor_event_free(NULL). */
void
tor_event_free(struct event *ev)
{
if (ev == NULL)
return;
event_del(ev);
tor_free(ev);
}
#else
/* Wrapper for event_free() that tolerates tor_event_free(NULL) */
void
tor_event_free(struct event *ev)
......@@ -122,7 +81,6 @@ tor_event_free(struct event *ev)
return;
event_free(ev);
}
#endif
/** Global event base for use by the main thread. */
static struct event_base *the_event_base = NULL;
......@@ -158,7 +116,6 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
/* some paths below don't use torcfg, so avoid unused variable warnings */
(void)torcfg;
#ifdef HAVE_EVENT2_EVENT_H
{
int attempts = 0;
int using_threads;
......@@ -190,16 +147,12 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK);
}
#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
#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
the_event_base = event_base_new_with_config(cfg);
......@@ -223,9 +176,6 @@ tor_libevent_initialize(tor_libevent_cfg *torcfg)
goto retry;
}
}
#else
the_event_base = event_init();
#endif
if (!the_event_base) {
/* LCOV_EXCL_START */
......@@ -255,62 +205,7 @@ tor_libevent_get_base, (void))
const char *
tor_libevent_get_method(void)
{
#ifdef HAVE_EVENT2_EVENT_H
return event_base_get_method(the_event_base);
#else
return event_get_method();
#endif
}
/** 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. */
STATIC le_version_t
tor_decode_libevent_version(const char *v)
{
unsigned major, minor, patchlevel;
char c, e, extra;
int fields;
/* Try the new preferred "1.4.11-stable" format.
* Also accept "1.4.14b-stable". */
fields = tor_sscanf(v, "%u.%u.%u%c%c", &major, &minor, &patchlevel, &c, &e);
if (fields == 3 ||
((fields == 4 || fields == 5 ) && (c == '-' || c == '_')) ||
(fields == 5 && TOR_ISALPHA(c) && (e == '-' || e == '_'))) {
return V(major,minor,patchlevel);
}
/* Try the old "1.3e" format. */
fields = tor_sscanf(v, "%u.%u%c%c", &major, &minor, &c, &extra);
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.*/
STATIC int
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
......@@ -321,101 +216,14 @@ 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
/** 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. */
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. */
#if defined(HEADER_VERSION)
/* 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;
tor_log(verybad ? LOG_WARN : LOG_NOTICE,
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.");
}
#else
/* 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
return LIBEVENT_VERSION;
}
/*
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. */
......@@ -424,11 +232,6 @@ struct periodic_timer_t {
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. */
......@@ -438,10 +241,6 @@ 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);
}
......@@ -459,16 +258,13 @@ periodic_timer_new(struct event_base *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,
if (!(timer->ev = tor_event_new(base, -1, EV_PERSIST,
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
event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/
return timer;
}
......@@ -539,7 +335,6 @@ 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;
......@@ -547,7 +342,6 @@ tor_init_libevent_rng(void)
crypto_rand(buf, 32);
evutil_secure_rng_add_bytes(buf, 32);
evutil_secure_rng_get_bytes(buf, sizeof(buf));
#endif
return rv;
}
......
......@@ -15,32 +15,17 @@ struct ev_token_bucket_cfg;
struct bufferevent_rate_limit_group;
#endif
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/util.h>
#elif !defined(EVUTIL_SOCKET_DEFINED)
#define EVUTIL_SOCKET_DEFINED
#define evutil_socket_t int
#endif
void configure_libevent_logging(void);
void suppress_libevent_log_msg(const char *msg);
#ifdef HAVE_EVENT2_EVENT_H
#define tor_event_new event_new
#define tor_evtimer_new evtimer_new
#define tor_evsignal_new evsignal_new
#define tor_evdns_add_server_port(sock, tcp, cb, data) \
evdns_add_server_port_with_base(tor_libevent_get_base(), \
(sock),(tcp),(cb),(data));
#else
struct event *tor_event_new(struct event_base * base, evutil_socket_t sock,
short what, void (*cb)(evutil_socket_t, short, void *), void *arg);
struct event *tor_evtimer_new(struct event_base * base,
void (*cb)(evutil_socket_t, short, void *), void *arg);
struct event *tor_evsignal_new(struct event_base * base, int sig,
void (*cb)(evutil_socket_t, short, void *), void *arg);
#define tor_evdns_add_server_port evdns_add_server_port
#endif
void tor_event_free(struct event *ev);
......@@ -99,9 +84,7 @@ void tor_gettimeofday_cached_monotonic(struct timeval *tv);
byte is unused.
This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
2.0.1 or later. For versions of Libevent before 1.4.0, which followed the
format of "1.0, 1.0a, 1.0b", we define 1.0 to be equivalent to 1.0.0, 1.0a
to be equivalent to 1.0.1, and so on.
2.0.1 or later.
*/
typedef uint32_t le_version_t;
......@@ -109,23 +92,10 @@ typedef uint32_t le_version_t;
/** Macros: returns the number of a libevent version as a le_version_t */
#define V(major, minor, patch) \
(((major) << 24) | ((minor) << 16) | ((patch) << 8))
#define V_OLD(major, minor, patch) \
V((major), (minor), (patch)-'a'+1)
/** @} */
/** Represetns a version of libevent so old we can't figure out what version
* it is. */
#define LE_OLD V(0,0,0)
/** Represents a version of libevent so weird we can't figure out what version
* it is. */
#define LE_OTHER V(0,0,99)
STATIC void
libevent_logging_callback(int severity, const char *msg);
STATIC le_version_t
tor_decode_libevent_version(const char *v);
STATIC int
le_versions_compatibility(le_version_t v);
#endif
#endif
......
......@@ -10,11 +10,7 @@
#include "util.h"
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
......@@ -164,11 +160,7 @@ tor_validate_process_specifier(const char *process_spec,
}
/* XXXX we should use periodic_timer_new() for this stuff */
#ifdef HAVE_EVENT2_EVENT_H
#define PERIODIC_TIMER_FLAGS EV_PERSIST
#else
#define PERIODIC_TIMER_FLAGS (0)
#endif
/* DOCDOC poll_interval_tv */
static struct timeval poll_interval_tv = {15, 0};
......@@ -331,10 +323,6 @@ tor_process_monitor_poll_cb(evutil_socket_t unused1, short unused2,
if (its_dead_jim) {
procmon->cb(procmon->cb_arg);
#ifndef HAVE_EVENT2_EVENT_H
} else {
evtimer_add(procmon->e, &poll_interval_tv);
#endif
}
}
#endif
......
......@@ -38,11 +38,7 @@
#include "torlog.h"
#include "util.h"
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#endif
struct timeout_cb {
timer_cb_fn_t cb;
......
......@@ -11,13 +11,6 @@ strlcpy.c
for strcat and strcpy. These are nonstandard, and some libc
implementations refuse to add them for religious reasons.
eventdns.[ch]
A fork of Libevent's DNS implementation, used by Tor when Libevent
2.0 or later is not available. Once Libevent 2.0 is required, we
should throw this away; it has diverged from evdns.[ch], and is
no longer easily mergeable.
ht.h
An implementation of a hash table in the style of Niels Provos's
......
This diff is collapsed.
/*
* The original DNS code is due to Adam Langley with heavy
* modifications by Nick Mathewson. Adam put his DNS software in the
* public domain. You can find his original copyright below. Please,
* aware that the code as part of libevent is governed by the 3-clause
* BSD license above.
*
* This software is Public Domain. To view a copy of the public domain dedication,
* visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
* Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
*
* I ask and expect, but do not require, that all derivative works contain an
* attribution similar to:
* Parts developed by Adam Langley <agl@imperialviolet.org>
*
* You may wish to replace the word "Parts" with something else depending on
* the amount of original code.
*
* (Derivative works does not include programs which link against, run or include
* the source verbatim in their source distributions)
*/
/*
* Welcome, gentle reader
*
* Async DNS lookups are really a whole lot harder than they should be,
* mostly stemming from the fact that the libc resolver has never been
* very good at them. Before you use this library you should see if libc
* can do the job for you with the modern async call getaddrinfo_a
* (see http://www.imperialviolet.org/page25.html#e498). Otherwise,
* please continue.
*
* This code is based on libevent and you must call event_init before
* any of the APIs in this file. You must also seed the OpenSSL random
* source if you are using OpenSSL for ids (see below).
*
* This library is designed to be included and shipped with your source
* code. You statically link with it. You should also test for the
* existence of strtok_r and define HAVE_STRTOK_R if you have it.
*
* The DNS protocol requires a good source of id numbers and these
* numbers should be unpredictable for spoofing reasons. There are
* three methods for generating them here and you must define exactly
* one of them. In increasing order of preference:
*
* DNS_USE_GETTIMEOFDAY_FOR_ID:
* Using the bottom 16 bits of the usec result from gettimeofday. This
* is a pretty poor solution but should work anywhere.
* DNS_USE_CPU_CLOCK_FOR_ID:
* Using the bottom 16 bits of the nsec result from the CPU's time
* counter. This is better, but may not work everywhere. Requires
* POSIX realtime support and you'll need to link against -lrt on
* glibc systems at least.
* DNS_USE_OPENSSL_FOR_ID:
* Uses the OpenSSL RAND_bytes call to generate the data. You must
* have seeded the pool before making any calls to this library.
*
* The library keeps track of the state of nameservers and will avoid
* them when they go down. Otherwise it will round robin between them.
*
* Quick start guide:
* #include "evdns.h"
* void callback(int result, char type, int count, int ttl,
* void *addresses, void *arg);