From: Guus Sliepen Date: Fri, 14 Jan 2011 19:28:52 +0000 (+0100) Subject: Use tinc 1.0's event infrastructure to handle timeouts. X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=6c987fcbc5a1e2394dcbcd999d265cb9826be82b;p=tinc Use tinc 1.0's event infrastructure to handle timeouts. This removes the necessity for libevent. --- diff --git a/src/control.c b/src/control.c index b8e5204b..73d79166 100644 --- a/src/control.c +++ b/src/control.c @@ -54,7 +54,7 @@ bool control_h(connection_t *c, char *request) { switch (type) { case REQ_STOP: - event_loopexit(NULL); + abort(); return control_ok(c, REQ_STOP); case REQ_DUMP_NODES: diff --git a/src/net.c b/src/net.c index dbff75b9..b34ff380 100644 --- a/src/net.c +++ b/src/net.c @@ -157,7 +157,8 @@ void terminate_connection(connection_t *c, bool report) { end does not reply in time, we consider them dead and close the connection. */ -static void timeout_handler(int fd, short events, void *event) { +static void timeout_handler(void *arg) { + event_t *event = arg; splay_node_t *node, *next; connection_t *c; time_t now = time(NULL); @@ -197,7 +198,7 @@ static void timeout_handler(int fd, short events, void *event) { if(rand() % 3 == 0) { logger(LOG_ERR, "Shutting down, check configuration of all nodes for duplicate Names!"); - event_loopexit(NULL); + abort(); return; } @@ -205,7 +206,8 @@ static void timeout_handler(int fd, short events, void *event) { contradicting_del_edge = 0; } - event_add(event, &(struct timeval){pingtimeout, 0}); + event->time = now + pingtimeout; + event_add(event); } void handle_meta_connection_data(void *data) { @@ -240,7 +242,7 @@ void handle_meta_connection_data(void *data) { static void sigterm_handler(int signal, short events, void *data) { logger(LOG_NOTICE, "Got %s signal", strsignal(signal)); - event_loopexit(NULL); + exit(0); } static void sighup_handler(int signal, short events, void *data) { @@ -262,7 +264,7 @@ int reload_configuration(void) { if(!read_server_config()) { logger(LOG_ERR, "Unable to reread configuration file, exitting."); - event_loopexit(NULL); + abort(); return EINVAL; } @@ -334,8 +336,7 @@ void retry(void) { c = node->data; if(c->outgoing && !c->node) { - if(timeout_initialized(&c->outgoing->ev)) - event_del(&c->outgoing->ev); + event_del(&c->outgoing->ev); if(c->status.connecting) close(c->socket); c->outgoing->timeout = 0; @@ -349,34 +350,31 @@ void retry(void) { */ int main_loop(void) { struct event timeout_event; - struct event sighup_event; - struct event sigterm_event; - struct event sigquit_event; - timeout_set(&timeout_event, timeout_handler, &timeout_event); - event_add(&timeout_event, &(struct timeval){pingtimeout, 0}); + timeout_event.time = time(NULL) + pingtimeout; + timeout_event.handler = timeout_handler; + timeout_event.data = &timeout_event; + + event_add(&timeout_event); #ifdef SIGHUP - signal_set(&sighup_event, SIGHUP, sighup_handler, NULL); - signal_add(&sighup_event, NULL); + signal(SIGHUP, sighup_handler); #endif #ifdef SIGTERM - signal_set(&sigterm_event, SIGTERM, sigterm_handler, NULL); - signal_add(&sigterm_event, NULL); + signal(SIGTERM, sigterm_handler); #endif #ifdef SIGQUIT - signal_set(&sigquit_event, SIGQUIT, sigterm_handler, NULL); - signal_add(&sigquit_event, NULL); + signal(SIGQUIT, sigterm_handler); #endif - if(event_loop(0) < 0) { - logger(LOG_ERR, "Error while waiting for input: %s", strerror(errno)); - return 1; + while(true) { + sleep(1); + struct event *event; + while((event = get_expired_event())) { + event->handler(event->data); + } } - signal_del(&sighup_event); - signal_del(&sigterm_event); - signal_del(&sigquit_event); event_del(&timeout_event); return 0; diff --git a/src/net.h b/src/net.h index 68cca02e..d4523fca 100644 --- a/src/net.h +++ b/src/net.h @@ -21,6 +21,7 @@ #ifndef __TINC_NET_H__ #define __TINC_NET_H__ +#include "event.h" #include "ipv6.h" #include "cipher.h" #include "digest.h" diff --git a/src/net_packet.c b/src/net_packet.c index 269f314c..2856c616 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -72,7 +72,7 @@ unsigned replaywin = 16; // mtuprobes == 32: send 1 burst, sleep pingtimeout second // mtuprobes == 33: no response from other side, restart PMTU discovery process -static void send_mtu_probe_handler(int fd, short events, void *data) { +static void send_mtu_probe_handler(void *data) { node_t *n = data; vpn_packet_t packet; int len, i; @@ -136,13 +136,15 @@ static void send_mtu_probe_handler(int fd, short events, void *data) { } end: - event_add(&n->mtuevent, &(struct timeval){timeout, 0}); + n->mtuevent.time = time(NULL) + timeout; + event_add(&n->mtuevent); } void send_mtu_probe(node_t *n) { - if(!timeout_initialized(&n->mtuevent)) - timeout_set(&n->mtuevent, send_mtu_probe_handler, n); - send_mtu_probe_handler(0, 0, n); + event_del(&n->mtuevent); + n->mtuevent.handler = send_mtu_probe_handler; + n->mtuevent.data = n; + send_mtu_probe_handler(n); } void mtu_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { diff --git a/src/net_setup.c b/src/net_setup.c index 73d4f775..38788f0a 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -139,20 +139,17 @@ bool read_rsa_private_key() { static struct event keyexpire_event; -static void keyexpire_handler(int fd, short events, void *data) { +static void keyexpire_handler(void *data) { regenerate_key(); } void regenerate_key() { - if(timeout_initialized(&keyexpire_event)) { - ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys"); - event_del(&keyexpire_event); - send_key_changed(broadcast, myself); - } else { - timeout_set(&keyexpire_event, keyexpire_handler, NULL); - } - - event_add(&keyexpire_event, &(struct timeval){keylifetime, 0}); + ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys"); + event_del(&keyexpire_event); + send_key_changed(broadcast, myself); + keyexpire_event.time = time(NULL) + keylifetime; + keyexpire_event.handler = keyexpire_handler; + event_add(&keyexpire_event); } /* @@ -460,16 +457,6 @@ bool setup_myself(void) { if(!setup_device()) return false; - if(device_fd >= 0) { - event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL); - - if (event_add(&device_ev, NULL) < 0) { - logger(LOG_ERR, "event_add failed: %s", strerror(errno)); - close_device(); - return false; - } - } - /* Run tinc-up script to further initialize the tap interface */ xasprintf(&envp[0], "NETNAME=%s", netname ? : ""); xasprintf(&envp[1], "DEVICE=%s", device ? : ""); diff --git a/src/net_socket.c b/src/net_socket.c index 8a733896..28b60679 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -285,7 +285,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { return nfd; } /* int setup_vpn_in_socket */ -static void retry_outgoing_handler(int fd, short events, void *data) { +static void retry_outgoing_handler(void *data) { setup_outgoing_connection(data); } @@ -295,8 +295,9 @@ void retry_outgoing(outgoing_t *outgoing) { if(outgoing->timeout > maxtimeout) outgoing->timeout = maxtimeout; - timeout_set(&outgoing->ev, retry_outgoing_handler, outgoing); - event_add(&outgoing->ev, &(struct timeval){outgoing->timeout, 0}); + outgoing->ev.handler = retry_outgoing_handler; + outgoing->ev.time = time(NULL) + outgoing->timeout; + event_add(&outgoing->ev); ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Trying to re-establish outgoing connection in %d seconds", diff --git a/src/node.h b/src/node.h index 4eb216f4..36c8253e 100644 --- a/src/node.h +++ b/src/node.h @@ -25,6 +25,7 @@ #include "cipher.h" #include "connection.h" #include "digest.h" +#include "event.h" #include "list.h" #include "subnet.h" diff --git a/src/process.c b/src/process.c index d588a3fd..c1ad81fa 100644 --- a/src/process.c +++ b/src/process.c @@ -166,7 +166,7 @@ DWORD WINAPI controlhandler(DWORD request, DWORD type, LPVOID boe, LPVOID bah) { return ERROR_CALL_NOT_IMPLEMENTED; } - event_loopexit(NULL); + abort(); status.dwWaitHint = 30000; status.dwCurrentState = SERVICE_STOP_PENDING; SetServiceStatus(statushandle, &status); diff --git a/src/protocol.c b/src/protocol.c index e866e316..023f9697 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -190,12 +190,13 @@ bool seen_request(char *request) { new->request = xstrdup(request); new->firstseen = time(NULL); splay_insert(past_request_tree, new); - event_add(&past_request_event, &(struct timeval){10, 0}); + past_request_event.time = time(NULL) + 10; + event_add(&past_request_event); return false; } } -void age_past_requests(int fd, short events, void *data) { +void age_past_requests(void *data) { splay_node_t *node, *next; past_request_t *p; int left = 0, deleted = 0; @@ -215,14 +216,16 @@ void age_past_requests(int fd, short events, void *data) { ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left); - if(left) - event_add(&past_request_event, &(struct timeval){10, 0}); + if(left) { + past_request_event.time = time(NULL) + 10; + event_add(&past_request_event); + } } void init_requests(void) { past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request); - timeout_set(&past_request_event, age_past_requests, NULL); + past_request_event.handler = age_past_requests; } void exit_requests(void) { diff --git a/src/route.c b/src/route.c index 9897d0d7..23bf38fd 100644 --- a/src/route.c +++ b/src/route.c @@ -180,7 +180,7 @@ static void swap_mac_addresses(vpn_packet_t *packet) { memcpy(&packet->data[6], &tmp, sizeof tmp); } -static void age_subnets(int fd, short events, void *data) { +static void age_subnets(void *data) { subnet_t *s; connection_t *c; splay_node_t *node, *next, *node2; @@ -210,8 +210,10 @@ static void age_subnets(int fd, short events, void *data) { } } - if(left) - event_add(&age_subnets_event, &(struct timeval){10, 0}); + if(left) { + age_subnets_event.time = time(NULL) + 10; + event_add(&age_subnets_event); + } } static void learn_mac(mac_t *address) { @@ -244,9 +246,9 @@ static void learn_mac(mac_t *address) { send_add_subnet(c, subnet); } - if(!timeout_initialized(&age_subnets_event)) - timeout_set(&age_subnets_event, age_subnets, NULL); - event_add(&age_subnets_event, &(struct timeval){10, 0}); + age_subnets_event.handler = age_subnets; + age_subnets_event.time = time(NULL) + 10; + event_add(&age_subnets_event); } else { if(subnet->expires) subnet->expires = time(NULL) + macexpire; diff --git a/src/tincd.c b/src/tincd.c index c4750da2..aab250ae 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -381,13 +381,9 @@ int main(int argc, char **argv) { openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR); - if(!event_init()) { - logger(LOG_ERR, "Error initializing libevent!"); - return 1; - } - g_argv = argv; + init_events(); init_configuration(&config_tree); /* Slllluuuuuuurrrrp! */ @@ -495,6 +491,7 @@ end: crypto_exit(); exit_configuration(&config_tree); + exit_events(); free_names(); return status;