aboutsummaryrefslogtreecommitdiff
path: root/src/or
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2009-06-04 01:05:23 -0400
committerNick Mathewson <nickm@torproject.org>2009-06-12 14:27:52 -0400
commite5b88dc83fb44622f2b0f9c0c242907d1c02311f (patch)
tree4a3932831da9df966b74ddf59b16aaa2445edb12 /src/or
parentcfce7d5deab7839c0ac67c3777e70a2264176ca0 (diff)
downloadtor-e5b88dc83fb44622f2b0f9c0c242907d1c02311f.tar.gz
tor-e5b88dc83fb44622f2b0f9c0c242907d1c02311f.zip
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).
Diffstat (limited to 'src/or')
-rw-r--r--src/or/Makefile.am30
-rw-r--r--src/or/config.c8
-rw-r--r--src/or/dns.c17
-rw-r--r--src/or/dnsserv.c7
-rw-r--r--src/or/eventdns.c1
-rw-r--r--src/or/main.c45
-rw-r--r--src/or/or.h7
7 files changed, 65 insertions, 50 deletions
diff --git a/src/or/Makefile.am b/src/or/Makefile.am
index 11923fbd35..c967a8846f 100644
--- a/src/or/Makefile.am
+++ b/src/or/Makefile.am
@@ -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
diff --git a/src/or/config.c b/src/or/config.c
index 9d5fb3b120..e5f27f9acd 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -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).");
diff --git a/src/or/dns.c b/src/or/dns.c
index b8dd460889..ba34b406b9 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -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");
}
}
diff --git a/src/or/dnsserv.c b/src/or/dnsserv.c
index 6020f9a781..08cf653093 100644
--- a/src/or/dnsserv.c
+++ b/src/or/dnsserv.c
@@ -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>.
diff --git a/src/or/eventdns.c b/src/or/eventdns.c
index 34c16248dd..00e63b5212 100644
--- a/src/or/eventdns.c
+++ b/src/or/eventdns.c
@@ -89,6 +89,7 @@
#include <stdarg.h>
#include "eventdns.h"
+
#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
diff --git a/src/or/main.c b/src/or/main.c
index ee1ff3437b..1b716f93a5 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -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]);
}
diff --git a/src/or/or.h b/src/or/or.h
index 2fa60264f5..59425fa7cb 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -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. */