mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-11 05:33:47 +01:00
Refactor tor_event_base_once to do what we actually want
This version avoids the timeout system entirely, gives a nicer interface, and lets us manage allocation explicitly.
This commit is contained in:
parent
e5f2f10844
commit
7920ea55b8
@ -558,13 +558,48 @@ tor_check_libevent_header_compatibility(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Wrapper around libevent's event_base_once(). Sets a
|
typedef struct runnable_t {
|
||||||
* timeout-triggered event with no associated file descriptor. */
|
struct event *ev;
|
||||||
int
|
void (*cb)(void *arg);
|
||||||
tor_event_base_once(void (*cb)(evutil_socket_t, short, void *),
|
void *arg;
|
||||||
void *arg, struct timeval *timer)
|
} runnable_t;
|
||||||
|
|
||||||
|
/** Callback for tor_run_in_libevent_loop */
|
||||||
|
static void
|
||||||
|
run_runnable_cb(evutil_socket_t s, short what, void *arg)
|
||||||
{
|
{
|
||||||
return event_base_once(tor_libevent_get_base(), -1, EV_TIMEOUT, cb, arg, timer);
|
runnable_t *r = arg;
|
||||||
|
void (*cb)(void *) = r->cb;
|
||||||
|
void *cb_arg = r->arg;
|
||||||
|
(void)what;
|
||||||
|
(void)s;
|
||||||
|
event_free(r->ev);
|
||||||
|
tor_free(r);
|
||||||
|
|
||||||
|
cb(cb_arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cause cb(arg) to run later on this iteration of the libevent loop, or in
|
||||||
|
* the next iteration of the libevent loop. This is useful for when you're
|
||||||
|
* deep inside a no-reentrant code and there's some function you want to call
|
||||||
|
* without worrying about whether it might cause reeentrant invocation.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
tor_run_in_libevent_loop(void (*cb)(void *arg), void *arg)
|
||||||
|
{
|
||||||
|
runnable_t *r = tor_malloc(sizeof(runnable_t));
|
||||||
|
r->cb = cb;
|
||||||
|
r->arg = arg;
|
||||||
|
r->ev = tor_event_new(tor_libevent_get_base(), -1, EV_TIMEOUT,
|
||||||
|
run_runnable_cb, r);
|
||||||
|
if (!r->ev) {
|
||||||
|
tor_free(r);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* Make the event active immediately. */
|
||||||
|
event_active(r->ev, EV_TIMEOUT, 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -46,8 +46,7 @@ void tor_event_free(struct event *ev);
|
|||||||
|
|
||||||
typedef struct periodic_timer_t periodic_timer_t;
|
typedef struct periodic_timer_t periodic_timer_t;
|
||||||
|
|
||||||
int tor_event_base_once(void (*cb)(evutil_socket_t, short, void *),
|
int tor_run_in_libevent_loop(void (*cb)(void *arg), void *arg);
|
||||||
void *arg, struct timeval *timer);
|
|
||||||
|
|
||||||
periodic_timer_t *periodic_timer_new(struct event_base *base,
|
periodic_timer_t *periodic_timer_new(struct event_base *base,
|
||||||
const struct timeval *tv,
|
const struct timeval *tv,
|
||||||
|
@ -160,7 +160,7 @@ struct tor_tls_t {
|
|||||||
|
|
||||||
/** Callback to invoke whenever a client tries to renegotiate more
|
/** Callback to invoke whenever a client tries to renegotiate more
|
||||||
than once. */
|
than once. */
|
||||||
void (*excess_renegotiations_callback)(evutil_socket_t, short, void *);
|
void (*excess_renegotiations_callback)(void *);
|
||||||
|
|
||||||
/** Argument to pass to negotiated_callback. */
|
/** Argument to pass to negotiated_callback. */
|
||||||
void *callback_arg;
|
void *callback_arg;
|
||||||
@ -1326,10 +1326,8 @@ tor_tls_got_client_hello(tor_tls_t *tls)
|
|||||||
callback, so we set a libevent timer that triggers in the next
|
callback, so we set a libevent timer that triggers in the next
|
||||||
event loop and closes the connection. */
|
event loop and closes the connection. */
|
||||||
|
|
||||||
struct timeval zero_seconds_timer = {0,0};
|
if (tor_run_in_libevent_loop(tls->excess_renegotiations_callback,
|
||||||
|
tls->callback_arg) < 0) {
|
||||||
if (tor_event_base_once(tls->excess_renegotiations_callback,
|
|
||||||
tls->callback_arg, &zero_seconds_timer) < 0) {
|
|
||||||
log_warn(LD_GENERAL, "Didn't manage to set a renegotiation limiting callback.");
|
log_warn(LD_GENERAL, "Didn't manage to set a renegotiation limiting callback.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1557,7 +1555,7 @@ tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
|
|||||||
void
|
void
|
||||||
tor_tls_set_renegotiate_callbacks(tor_tls_t *tls,
|
tor_tls_set_renegotiate_callbacks(tor_tls_t *tls,
|
||||||
void (*cb)(tor_tls_t *, void *arg),
|
void (*cb)(tor_tls_t *, void *arg),
|
||||||
void (*cb2)(evutil_socket_t, short, void *),
|
void (*cb2)(void *),
|
||||||
void *arg)
|
void *arg)
|
||||||
{
|
{
|
||||||
tls->negotiated_callback = cb;
|
tls->negotiated_callback = cb;
|
||||||
|
@ -63,7 +63,7 @@ tor_tls_t *tor_tls_new(int sock, int is_server);
|
|||||||
void tor_tls_set_logged_address(tor_tls_t *tls, const char *address);
|
void tor_tls_set_logged_address(tor_tls_t *tls, const char *address);
|
||||||
void tor_tls_set_renegotiate_callbacks(tor_tls_t *tls,
|
void tor_tls_set_renegotiate_callbacks(tor_tls_t *tls,
|
||||||
void (*cb)(tor_tls_t *, void *arg),
|
void (*cb)(tor_tls_t *, void *arg),
|
||||||
void (*cb2)(evutil_socket_t, short, void *),
|
void (*cb2)(void *),
|
||||||
void *arg);
|
void *arg);
|
||||||
int tor_tls_is_server(tor_tls_t *tls);
|
int tor_tls_is_server(tor_tls_t *tls);
|
||||||
void tor_tls_free(tor_tls_t *tls);
|
void tor_tls_free(tor_tls_t *tls);
|
||||||
|
@ -3647,8 +3647,8 @@ tor_get_exit_code(const process_handle_t *process_handle,
|
|||||||
/* Process has not exited */
|
/* Process has not exited */
|
||||||
return PROCESS_EXIT_RUNNING;
|
return PROCESS_EXIT_RUNNING;
|
||||||
} else if (retval != process_handle->pid) {
|
} else if (retval != process_handle->pid) {
|
||||||
log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s", process_handle->pid,
|
log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s",
|
||||||
strerror(errno));
|
process_handle->pid, strerror(errno));
|
||||||
return PROCESS_EXIT_ERROR;
|
return PROCESS_EXIT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1157,11 +1157,9 @@ connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
|
|||||||
* tor_tls_got_client_hello() when the server receives excess
|
* tor_tls_got_client_hello() when the server receives excess
|
||||||
* renegotiation attempts; probably indicating a DoS. */
|
* renegotiation attempts; probably indicating a DoS. */
|
||||||
static void
|
static void
|
||||||
connection_or_close_connection_cb(evutil_socket_t fd, short what, void *_conn)
|
connection_or_close_connection_cb(void *_conn)
|
||||||
{
|
{
|
||||||
or_connection_t *conn = _conn;
|
or_connection_t *conn = _conn;
|
||||||
(void) what;
|
|
||||||
(void) fd;
|
|
||||||
|
|
||||||
connection_stop_reading(TO_CONN(conn));
|
connection_stop_reading(TO_CONN(conn));
|
||||||
connection_mark_for_close(TO_CONN(conn));
|
connection_mark_for_close(TO_CONN(conn));
|
||||||
|
Loading…
Reference in New Issue
Block a user