1 /* Copyright (c) 2009-2013, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #ifndef TOR_COMPAT_LIBEVENT_H
5 #define TOR_COMPAT_LIBEVENT_H
11 #ifdef USE_BUFFEREVENTS
13 struct ev_token_bucket_cfg
;
14 struct bufferevent_rate_limit_group
;
17 #ifdef HAVE_EVENT2_EVENT_H
18 #include <event2/util.h>
19 #elif !defined(EVUTIL_SOCKET_DEFINED)
20 #define EVUTIL_SOCKET_DEFINED
21 #define evutil_socket_t int
24 void configure_libevent_logging(void);
25 void suppress_libevent_log_msg(const char *msg
);
27 #ifdef HAVE_EVENT2_EVENT_H
28 #define tor_event_new event_new
29 #define tor_evtimer_new evtimer_new
30 #define tor_evsignal_new evsignal_new
31 #define tor_event_free event_free
32 #define tor_evdns_add_server_port(sock, tcp, cb, data) \
33 evdns_add_server_port_with_base(tor_libevent_get_base(), \
34 (sock),(tcp),(cb),(data));
37 struct event
*tor_event_new(struct event_base
* base
, evutil_socket_t sock
,
38 short what
, void (*cb
)(evutil_socket_t
, short, void *), void *arg
);
39 struct event
*tor_evtimer_new(struct event_base
* base
,
40 void (*cb
)(evutil_socket_t
, short, void *), void *arg
);
41 struct event
*tor_evsignal_new(struct event_base
* base
, int sig
,
42 void (*cb
)(evutil_socket_t
, short, void *), void *arg
);
43 void tor_event_free(struct event
*ev
);
44 #define tor_evdns_add_server_port evdns_add_server_port
47 typedef struct periodic_timer_t periodic_timer_t
;
49 periodic_timer_t
*periodic_timer_new(struct event_base
*base
,
50 const struct timeval
*tv
,
51 void (*cb
)(periodic_timer_t
*timer
, void *data
),
53 void periodic_timer_free(periodic_timer_t
*);
55 #ifdef HAVE_EVENT_BASE_LOOPEXIT
56 #define tor_event_base_loopexit event_base_loopexit
59 int tor_event_base_loopexit(struct event_base
*base
, struct timeval
*tv
);
62 /** Defines a configuration for using libevent with Tor: passed as an argument
63 * to tor_libevent_initialize() to describe how we want to set up. */
64 typedef struct tor_libevent_cfg
{
65 /** Flag: if true, disable IOCP (assuming that it could be enabled). */
67 /** How many CPUs should we use (relevant only with IOCP). */
69 /** How many milliseconds should we allow between updating bandwidth limits?
70 * (relevant only with bufferevents). */
74 void tor_libevent_initialize(tor_libevent_cfg
*cfg
);
75 struct event_base
*tor_libevent_get_base(void);
76 const char *tor_libevent_get_method(void);
77 void tor_check_libevent_version(const char *m
, int server
,
78 const char **badness_out
);
79 void tor_check_libevent_header_compatibility(void);
80 const char *tor_libevent_get_version_str(void);
81 const char *tor_libevent_get_header_version_str(void);
83 #ifdef USE_BUFFEREVENTS
84 const struct timeval
*tor_libevent_get_one_tick_timeout(void);
85 int tor_libevent_using_iocp_bufferevents(void);
86 int tor_set_bufferevent_rate_limit(struct bufferevent
*bev
,
87 struct ev_token_bucket_cfg
*cfg
);
88 int tor_add_bufferevent_to_rate_limit_group(struct bufferevent
*bev
,
89 struct bufferevent_rate_limit_group
*g
);
92 int tor_init_libevent_rng(void);
94 void tor_gettimeofday_cached(struct timeval
*tv
);
95 void tor_gettimeofday_cache_clear(void);
97 void tor_gettimeofday_cache_set(const struct timeval
*tv
);
99 void tor_gettimeofday_cached_monotonic(struct timeval
*tv
);