Update Tor to use Libevent 2.0 APIs when available.

This patch adds a new compat_libevent.[ch] set of files, and moves our
Libevent compatibility and utilitity functions there.  We build them
into a separate .a so that nothing else in src/commmon depends on
Libevent (partially fixing bug 507).

Also, do not use our own built-in evdns copy when we have Libevent
2.0, whose evdns is finally good enough (thus fixing Bug 920).
This commit is contained in:
Nick Mathewson 2009-06-04 01:05:23 -04:00
parent cfce7d5dea
commit e5b88dc83f
15 changed files with 269 additions and 119 deletions

1
.gitignore vendored
View File

@ -111,6 +111,7 @@
/src/common/common_sha1.i
/src/common/libor.a
/src/common/libor-crypto.a
/src/common/libor-event.a
# /src/config/
/src/config/Makefile

View File

@ -273,15 +273,19 @@ save_CPPFLAGS="$CPPFLAGS"
LIBS="-levent $TOR_LIB_WS32 $LIBS"
LDFLAGS="$TOR_LDFLAGS_libevent $LDFLAGS"
CPPFLAGS="$TOR_CPPFLAGS_libevent $CPPFLAGS"
AC_CHECK_FUNCS(event_get_version event_get_method event_set_log_callback)
AC_CHECK_FUNCS(event_get_version event_get_method event_set_log_callback evdns_set_outgoing_bind_address)
AC_CHECK_MEMBERS([struct event.min_heap_idx], , ,
[#include <event.h>
])
AC_CHECK_HEADERS(event2/event.h event2/dns.h)
LIBS="$save_LIBS"
LDFLAGS="$save_LDFLAGS"
CPPFLAGS="$save_CPPFLAGS"
AM_CONDITIONAL(USE_EXTERNAL_EVDNS, test x$ac_cv_header_event2_dns_h = xyes)
dnl ------------------------------------------------------
dnl Where do you live, openssl? And how do we call you?

View File

@ -1,5 +1,5 @@
noinst_LIBRARIES = libor.a libor-crypto.a
noinst_LIBRARIES = libor.a libor-crypto.a libor-event.a
EXTRA_DIST = common_sha1.i
@ -14,8 +14,9 @@ endif
libor_a_SOURCES = address.c log.c util.c compat.c container.c mempool.c \
memarea.c util_codedigest.c $(libor_extra_source)
libor_crypto_a_SOURCES = crypto.c aes.c tortls.c torgzip.c
libor_event_a_SOURCES = compat_libevent.c
noinst_HEADERS = address.h log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h ht.h mempool.h memarea.h ciphers.inc
noinst_HEADERS = address.h log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h ht.h mempool.h memarea.h ciphers.inc compat_libevent.h
common_sha1.i: $(libor_SOURCES) $(libor_crypto_a_SOURCES) $(noinst_HEADERS)
if test "@SHA1SUM@" != none; then \

View File

@ -0,0 +1,153 @@
/* Copyright (c) 2009, The Tor Project, Inc. */
/* 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
* 2009, Libevent 2.0 is still in alpha, and we will have old versions of
* Libevent for the forseeable future.
**/
#include "orconfig.h"
#include "compat_libevent.h"
#include "compat.h"
#include "util.h"
#include "log.h"
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#endif
#ifdef HAVE_EVENT_SET_LOG_CALLBACK
/** 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. */
static void
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:
log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_MSG:
log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_WARN:
log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
break;
case _EVENT_LOG_ERR:
log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
break;
default:
log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
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;
}
#else
void
configure_libevent_logging(void)
{
}
void
suppress_libevent_log_msg(const char *msg)
{
(void)msg;
}
#endif
#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);
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, cb, arg);
}
/** Work-alike replacement for event_free() on pre-Libevent-2.0 systems. */
void
tor_event_free(struct event *ev)
{
event_del(ev);
tor_free(ev);
}
#endif
/** Global event base for use by the main thread. */
struct event_base *the_event_base = NULL;
/** Initialize the Libevent library and set up the event base. */
void
tor_libevent_initialize(void)
{
tor_assert(the_event_base == NULL);
#ifdef HAVE_EVENT2_EVENT_H
the_event_base = event_base_new();
#else
the_event_base = event_init();
#endif
}
/** Return the current Libevent event base that we're set up to use. */
struct event_base *
tor_libevent_get_base(void)
{
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);
#elif defined(HAVE_EVENT_GET_METHOD)
return event_get_method();
#else
return "<unknown>";
#endif
}

View File

@ -0,0 +1,39 @@
/* Copyright (c) 2009, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#ifndef _TOR_COMPAT_LIBEVENT_H
#define _TOR_COMPAT_LIBEVENT_H
#include "orconfig.h"
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#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_event_free event_free
#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);
void tor_event_free(struct event *ev);
#endif
void tor_libevent_initialize(void);
struct event_base *tor_libevent_get_base(void);
const char *tor_libevent_get_method(void);
#endif

View File

@ -36,8 +36,6 @@
#include "log.h"
#include "container.h"
#include <event.h>
#define TRUNCATED_STR "[...truncated]"
#define TRUNCATED_STR_LEN 14
@ -921,65 +919,6 @@ switch_logs_debug(void)
UNLOCK_LOGS();
}
#ifdef HAVE_EVENT_SET_LOG_CALLBACK
/** 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. */
static void
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:
log(LOG_DEBUG, LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_MSG:
log(LOG_INFO, LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_WARN:
log(LOG_WARN, LD_GENERAL, "Warning from libevent: %s", buf);
break;
case _EVENT_LOG_ERR:
log(LOG_ERR, LD_GENERAL, "Error from libevent: %s", buf);
break;
default:
log(LOG_WARN, LD_GENERAL, "Message [%d] from libevent: %s",
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;
}
#else
void
configure_libevent_logging(void)
{
}
void
suppress_libevent_log_msg(const char *msg)
{
(void)msg;
}
#endif
#if 0
static void
dump_log_info(logfile_t *lf)

View File

@ -134,8 +134,6 @@ void add_temp_log(int min_severity);
void close_temp_logs(void);
void rollback_log_changes(void);
void mark_logs_temp(void);
void configure_libevent_logging(void);
void suppress_libevent_log_msg(const char *msg);
void change_callback_log_severity(int loglevelMin, int loglevelMax,
log_callback cb);
void log_set_application_name(const char *name);

View File

@ -12,7 +12,13 @@ endif
EXTRA_DIST=ntmain.c or_sha1.i
tor_SOURCES = buffers.c circuitbuild.c circuitlist.c \
if USE_EXTERNAL_EVDNS
evdns_source=
else
evdns_source=eventdns.c
endif
COMMON_SRC = buffers.c circuitbuild.c circuitlist.c \
circuituse.c command.c config.c \
connection.c connection_edge.c connection_or.c control.c \
cpuworker.c directory.c dirserv.c dirvote.c \
@ -20,8 +26,9 @@ tor_SOURCES = buffers.c circuitbuild.c circuitlist.c \
networkstatus.c onion.c policies.c \
reasons.c relay.c rendcommon.c rendclient.c rendmid.c \
rendservice.c rephist.c router.c routerlist.c routerparse.c \
eventdns.c config_codedigest.c \
tor_main.c
$(evdns_source) config_codedigest.c
tor_SOURCES = $(COMMON_SRC) tor_main.c
AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
-DLOCALSTATEDIR="\"$(localstatedir)\"" \
@ -33,22 +40,15 @@ AM_CPPFLAGS = -DSHARE_DATADIR="\"$(datadir)\"" \
tor_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ @TOR_LDFLAGS_libevent@
tor_LDADD = ../common/libor.a ../common/libor-crypto.a \
-lz -levent -lssl -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
test_SOURCES = buffers.c circuitbuild.c circuitlist.c \
circuituse.c command.c config.c \
connection.c connection_edge.c connection_or.c control.c \
cpuworker.c directory.c dirserv.c dirvote.c \
dns.c dnsserv.c geoip.c hibernate.c main.c $(tor_platform_source) \
networkstatus.c onion.c policies.c \
reasons.c relay.c rendcommon.c rendclient.c rendmid.c \
rendservice.c rephist.c router.c routerlist.c routerparse.c \
eventdns.c config_codedigest.c \
test_data.c test.c
../common/libor-event.a \
-lz -levent -lssl -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
test_SOURCES = $(COMMON_SRC) test_data.c test.c
test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
test_LDADD = ../common/libor.a ../common/libor-crypto.a \
-lz -levent -lssl -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
../common/libor-event.a \
-lz -levent -lssl -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
noinst_HEADERS = or.h eventdns.h eventdns_tor.h micro-revision.i

View File

@ -4903,15 +4903,17 @@ init_libevent(void)
/* Your libevent is ancient. */
#endif
event_init();
tor_libevent_initialize();
suppress_libevent_log_msg(NULL);
#if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
/* Making this a NOTICE for now so we can link bugs to a libevent versions
* or methods better. */
log(LOG_NOTICE, LD_GENERAL,
"Initialized libevent version %s using method %s. Good.",
event_get_version(), event_get_method());
check_libevent_version(event_get_method(), get_options()->ORPort != 0);
event_get_version(), tor_libevent_get_method());
check_libevent_version(tor_libevent_get_method(),
get_options()->ORPort != 0);
#else
log(LOG_NOTICE, LD_GENERAL,
"Initialized old libevent (version 1.0b or earlier).");

View File

@ -13,7 +13,15 @@
#include "or.h"
#include "ht.h"
#ifdef HAVE_EVENT2_DNS_H
#include <event2/dns.h>
#include <event2/dns_compat.h>
#else
#include "eventdns.h"
#ifndef HAVE_EVDNS_SET_DEFAULT_OUTGOING_BIND_ADDRESS
#define HAVE_EVDNS_SET_DEFAULT_OUTGOING_BIND_ADDRESS
#endif
#endif
/** Longest hostname we're willing to resolve. */
#define MAX_ADDRESSLEN 256
@ -1108,6 +1116,7 @@ configure_nameservers(int force)
conf_fname = "/etc/resolv.conf";
#endif
#ifdef HAVE_EVDNS_SET_DEFAULT_OUTGOING_BIND_ADDRESS
if (options->OutboundBindAddress) {
tor_addr_t addr;
if (tor_addr_from_str(&addr, options->OutboundBindAddress) < 0) {
@ -1127,6 +1136,7 @@ configure_nameservers(int force)
}
}
}
#endif
if (options->ServerDNSRandomizeCase)
evdns_set_option("randomize-case:", "1", DNS_OPTIONS_ALL);
@ -1547,7 +1557,7 @@ dns_launch_wildcard_checks(void)
void
dns_launch_correctness_checks(void)
{
static struct event launch_event;
static struct event *launch_event = NULL;
struct timeval timeout;
if (!get_options()->ServerDNSDetectHijacking)
return;
@ -1555,10 +1565,11 @@ dns_launch_correctness_checks(void)
/* Wait a while before launching requests for test addresses, so we can
* get the results from checking for wildcarding. */
evtimer_set(&launch_event, launch_test_addresses, NULL);
if (! launch_event)
launch_event = tor_evtimer_new(NULL, launch_test_addresses, NULL);
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if (evtimer_add(&launch_event, &timeout)<0) {
if (evtimer_add(launch_event, &timeout)<0) {
log_warn(LD_BUG, "Couldn't add timer for checking for dns hijacking");
}
}

View File

@ -9,7 +9,14 @@
**/
#include "or.h"
#ifdef HAVE_EVENT2_DNS_H
#include <event2/dns.h>
#include <event2/dns_compat.h>
/* XXXX022 this implies we want an improved evdns */
#include <event2/dns_struct.h>
#else
#include "eventdns.h"
#endif
/** Helper function: called by evdns whenever the client sends a request to our
* DNSPort. We need to eventually answer the request <b>req</b>.

View File

@ -89,6 +89,7 @@
#include <stdarg.h>
#include "eventdns.h"
#ifdef WIN32
#include <windows.h>
#include <winsock2.h>

View File

@ -127,12 +127,10 @@ connection_add(connection_t *conn)
smartlist_add(connection_array, conn);
if (conn->s >= 0 || conn->linked) {
conn->read_event = tor_malloc_zero(sizeof(struct event));
conn->write_event = tor_malloc_zero(sizeof(struct event));
event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
conn_read_callback, conn);
event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
conn_write_callback, conn);
conn->read_event = tor_event_new(tor_libevent_get_base(),
conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
conn->write_event = tor_event_new(tor_libevent_get_base(),
conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
}
log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
@ -393,11 +391,11 @@ connection_start_reading_from_linked_conn(connection_t *conn)
smartlist_add(active_linked_connection_lst, conn);
if (!called_loop_once) {
/* This is the first event on the list; we won't be in LOOP_ONCE mode,
* so we need to make sure that the event_loop() actually exits at the
* end of its run through the current connections and
* lets us activate read events for linked connections. */
* so we need to make sure that the event_base_loop() actually exits at
* the end of its run through the current connections and lets us
* activate read events for linked connections. */
struct timeval tv = { 0, 0 };
event_loopexit(&tv);
event_base_loopexit(tor_libevent_get_base(), &tv);
}
} else {
tor_assert(smartlist_isin(active_linked_connection_lst, conn));
@ -1140,8 +1138,8 @@ second_elapsed_callback(int fd, short event, void *args)
(void)event;
(void)args;
if (!timeout_event) {
timeout_event = tor_malloc_zero(sizeof(struct event));
evtimer_set(timeout_event, second_elapsed_callback, NULL);
timeout_event = tor_evtimer_new(tor_libevent_get_base(),
second_elapsed_callback, NULL);
one_second.tv_sec = 1;
one_second.tv_usec = 0;
}
@ -1213,7 +1211,7 @@ second_elapsed_callback(int fd, short event, void *args)
}
#endif
if (evtimer_add(timeout_event, &one_second))
if (event_add(timeout_event, &one_second))
log_err(LD_NET,
"Error from libevent when setting one-second timeout event");
}
@ -1424,20 +1422,16 @@ do_main_loop(void)
/* poll until we have an event, or the second ends, or until we have
* some active linked connections to trigger events for. */
loop_result = event_loop(called_loop_once ? EVLOOP_ONCE : 0);
loop_result = event_base_loop(tor_libevent_get_base(),
called_loop_once ? EVLOOP_ONCE : 0);
/* let catch() handle things like ^c, and otherwise don't worry about it */
if (loop_result < 0) {
int e = tor_socket_errno(-1);
/* let the program survive things like ^z */
if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
#ifdef HAVE_EVENT_GET_METHOD
log_err(LD_NET,"libevent call with %s failed: %s [%d]",
event_get_method(), tor_socket_strerror(e), e);
#else
log_err(LD_NET,"libevent call failed: %s [%d]",
tor_socket_strerror(e), e);
#endif
tor_libevent_get_method(), tor_socket_strerror(e), e);
return -1;
#ifndef MS_WINDOWS
} else if (e == EINVAL) {
@ -1704,7 +1698,7 @@ handle_signals(int is_parent)
{
#ifndef MS_WINDOWS /* do signal stuff only on Unix */
int i;
static int signals[] = {
static const int signals[] = {
SIGINT, /* do a controlled slow shutdown */
SIGTERM, /* to terminate now */
SIGPIPE, /* otherwise SIGPIPE kills us */
@ -1716,12 +1710,13 @@ handle_signals(int is_parent)
#endif
SIGCHLD, /* handle dns/cpu workers that exit */
-1 };
static struct event signal_events[16]; /* bigger than it has to be. */
static struct event *signal_events[16]; /* bigger than it has to be. */
if (is_parent) {
for (i = 0; signals[i] >= 0; ++i) {
signal_set(&signal_events[i], signals[i], signal_callback,
(void*)(uintptr_t)signals[i]);
if (signal_add(&signal_events[i], NULL))
signal_events[i] = tor_evsignal_new(
tor_libevent_get_base(), signals[i], signal_callback,
(void*)(uintptr_t)signals[i]);
if (event_add(signal_events[i], NULL))
log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
signals[i]);
}

View File

@ -91,8 +91,7 @@
#include "util.h"
#include "torgzip.h"
#include "address.h"
#include <event.h>
#include "compat_libevent.h"
/* These signals are defined to help control_signal_act work.
*/
@ -922,8 +921,8 @@ typedef struct connection_t {
* connection. */
unsigned int linked_conn_is_closed:1;
int s; /**< Our socket; -1 if this connection is closed, or has no
* socket. */
/** Our socket; -1 if this connection is closed, or has no socket. */
evutil_socket_t s;
int conn_array_index; /**< Index into the global connection array. */
struct event *read_event; /**< Libevent event structure. */
struct event *write_event; /**< Libevent event structure. */

View File

@ -3,16 +3,16 @@ noinst_PROGRAMS = tor-checkkey
tor_resolve_SOURCES = tor-resolve.c
tor_resolve_LDFLAGS = @TOR_LDFLAGS_libevent@
tor_resolve_LDADD = ../common/libor.a -levent @TOR_LIB_WS32@
tor_resolve_LDADD = ../common/libor.a @TOR_LIB_WS32@
tor_gencert_SOURCES = tor-gencert.c
tor_gencert_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
tor_gencert_LDADD = ../common/libor.a ../common/libor-crypto.a \
-lz -lcrypto -levent @TOR_LIB_WS32@ @TOR_LIB_GDI@
-lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@
tor_checkkey_SOURCES = tor-checkkey.c
tor_checkkey_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
tor_checkkey_LDADD = ../common/libor.a ../common/libor-crypto.a \
-lz -lcrypto -levent @TOR_LIB_WS32@ @TOR_LIB_GDI@
-lz -lcrypto @TOR_LIB_WS32@ @TOR_LIB_GDI@