From: Guus Sliepen Date: Sat, 26 Apr 2014 07:38:55 +0000 (+0200) Subject: Remove global variable "now". X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=a20765791b2a40ddd58452e8f74b0737f1f1f28e Remove global variable "now". --- diff --git a/src/event.c b/src/event.c index 065f1b5e..85ea9bb8 100644 --- a/src/event.c +++ b/src/event.c @@ -27,7 +27,6 @@ #include "xalloc.h" event_loop_t *loop; -struct timeval now; static int io_compare(const io_t *a, const io_t *b) { return a->fd - b->fd; @@ -171,7 +170,6 @@ bool event_loop_run(event_loop_t *loop) { while(loop->running) { gettimeofday(&loop->now, NULL); - now = loop->now; struct timeval diff, *tv = NULL; while(loop->timeouts.head) { @@ -237,6 +235,7 @@ void event_loop_init(event_loop_t *loop) { loop->signals.compare = (splay_compare_t)signal_compare; loop->pipefd[0] = -1; loop->pipefd[1] = -1; + gettimeofday(&loop->now, NULL); } void event_loop_exit(event_loop_t *loop) { diff --git a/src/event.h b/src/event.h index 3d49fe2d..0993e24c 100644 --- a/src/event.h +++ b/src/event.h @@ -71,7 +71,6 @@ struct event_loop_t { }; extern event_loop_t *loop; -extern struct timeval now; extern void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags); extern void io_del(event_loop_t *loop, io_t *io); diff --git a/src/graph.c b/src/graph.c index 017e10dc..ef152a06 100644 --- a/src/graph.c +++ b/src/graph.c @@ -199,7 +199,7 @@ static void check_reachability(void) { for splay_each(node_t, n, mesh->nodes) { if(n->status.visited != n->status.reachable) { n->status.reachable = !n->status.reachable; - n->last_state_change = now.tv_sec; + n->last_state_change = mesh->loop.now.tv_sec; if(n->status.reachable) { logger(DEBUG_TRAFFIC, LOG_DEBUG, "Node %s (%s) became reachable", diff --git a/src/meshlink.c b/src/meshlink.c index 46025655..e6f44c0c 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -365,8 +365,6 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { } static void __attribute__((constructor)) meshlink_init(void) { - gettimeofday(&now, NULL); - srand(now.tv_sec + now.tv_usec); crypto_init(); } diff --git a/src/net.c b/src/net.c index 12f2f33d..878a2f99 100644 --- a/src/net.c +++ b/src/net.c @@ -124,11 +124,11 @@ void terminate_connection(connection_t *c, bool report) { */ static void timeout_handler(event_loop_t *loop, void *data) { for list_each(connection_t, c, mesh->connections) { - if(c->last_ping_time + mesh->pingtimeout <= now.tv_sec) { + if(c->last_ping_time + mesh->pingtimeout <= mesh->loop.now.tv_sec) { if(c->status.active) { if(c->status.pinged) { - logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)now.tv_sec - c->last_ping_time); - } else if(c->last_ping_time + mesh->pinginterval <= now.tv_sec) { + logger(DEBUG_CONNECTIONS, LOG_INFO, "%s (%s) didn't respond to PING in %ld seconds", c->name, c->hostname, (long)mesh->loop.now.tv_sec - c->last_ping_time); + } else if(c->last_ping_time + mesh->pinginterval <= mesh->loop.now.tv_sec) { send_ping(c); continue; } else { @@ -303,7 +303,7 @@ int reload_configuration(void) { } } - mesh->last_config_check = now.tv_sec; + mesh->last_config_check = mesh->loop.now.tv_sec; return 0; } diff --git a/src/net_packet.c b/src/net_packet.c index 63d6e4ea..0fd2cd58 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -316,7 +316,7 @@ static void send_sptps_packet(node_t *n, vpn_packet_t *origpkt) { logger(DEBUG_TRAFFIC, LOG_INFO, "No valid key known yet for %s (%s)", n->name, n->hostname); if(!n->status.waitingforkey) send_req_key(n); - else if(n->last_req_key + 10 < now.tv_sec) { + else if(n->last_req_key + 10 < mesh->loop.now.tv_sec) { logger(DEBUG_ALWAYS, LOG_DEBUG, "No key from %s after 10 seconds, restarting SPTPS", n->name); sptps_stop(&n->sptps); n->status.waitingforkey = false; @@ -604,7 +604,7 @@ static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) { continue; if(sockaddrcmp_noport(from, &e->address)) { - if(last_hard_try == now.tv_sec) + if(last_hard_try == mesh->loop.now.tv_sec) continue; hard = true; } @@ -617,9 +617,9 @@ static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) { } if(hard) - last_hard_try = now.tv_sec; + last_hard_try = mesh->loop.now.tv_sec; - last_hard_try = now.tv_sec; + last_hard_try = mesh->loop.now.tv_sec; return n; } diff --git a/src/net_setup.c b/src/net_setup.c index 896df432..ba17b099 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -322,7 +322,7 @@ bool setup_myself(void) { mesh->self->nexthop = mesh->self; mesh->self->via = mesh->self; mesh->self->status.reachable = true; - mesh->self->last_state_change = now.tv_sec; + mesh->self->last_state_change = mesh->loop.now.tv_sec; node_add(mesh->self); graph(); @@ -353,7 +353,7 @@ bool setup_myself(void) { /* Done. */ - mesh->last_config_check = now.tv_sec; + mesh->last_config_check = mesh->loop.now.tv_sec; return true; } diff --git a/src/net_socket.c b/src/net_socket.c index 589c4caa..84985587 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -250,7 +250,7 @@ void retry_outgoing(outgoing_t *outgoing) { void finish_connecting(connection_t *c) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Connected to %s (%s)", c->name, c->hostname); - c->last_ping_time = now.tv_sec; + c->last_ping_time = mesh->loop.now.tv_sec; c->status.connecting = false; send_id(c); @@ -460,7 +460,7 @@ begin: c->status.connecting = true; c->name = xstrdup(outgoing->name); c->outcompression = mesh->self->connection->outcompression; - c->last_ping_time = now.tv_sec; + c->last_ping_time = mesh->loop.now.tv_sec; connection_add(c); @@ -564,12 +564,12 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { static int samehost_burst; static int samehost_burst_time; - if(now.tv_sec - samehost_burst_time > samehost_burst) + if(mesh->loop.now.tv_sec - samehost_burst_time > samehost_burst) samehost_burst = 0; else - samehost_burst -= now.tv_sec - samehost_burst_time; + samehost_burst -= mesh->loop.now.tv_sec - samehost_burst_time; - samehost_burst_time = now.tv_sec; + samehost_burst_time = mesh->loop.now.tv_sec; samehost_burst++; if(samehost_burst > max_connection_burst) { @@ -585,12 +585,12 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { static int connection_burst; static int connection_burst_time; - if(now.tv_sec - connection_burst_time > connection_burst) + if(mesh->loop.now.tv_sec - connection_burst_time > connection_burst) connection_burst = 0; else - connection_burst -= now.tv_sec - connection_burst_time; + connection_burst -= mesh->loop.now.tv_sec - connection_burst_time; - connection_burst_time = now.tv_sec; + connection_burst_time = mesh->loop.now.tv_sec; connection_burst++; if(connection_burst >= max_connection_burst) { @@ -608,7 +608,7 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { c->address = sa; c->hostname = sockaddr2hostname(&sa); c->socket = fd; - c->last_ping_time = now.tv_sec; + c->last_ping_time = mesh->loop.now.tv_sec; logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname); diff --git a/src/protocol.c b/src/protocol.c index f8b827d6..c1d635d0 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -167,7 +167,7 @@ static void age_past_requests(event_loop_t *loop, void *data) { int left = 0, deleted = 0; for splay_each(past_request_t, p, past_request_tree) { - if(p->firstseen + mesh->pinginterval <= now.tv_sec) + if(p->firstseen + mesh->pinginterval <= mesh->loop.now.tv_sec) splay_delete_node(past_request_tree, node), deleted++; else left++; @@ -191,7 +191,7 @@ bool seen_request(const char *request) { } else { new = xmalloc(sizeof *new); new->request = xstrdup(request); - new->firstseen = now.tv_sec; + new->firstseen = mesh->loop.now.tv_sec; splay_insert(past_request_tree, new); timeout_add(&mesh->loop, &past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000}); return false; diff --git a/src/protocol_key.c b/src/protocol_key.c index 8ab1325c..887ccd0d 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -94,7 +94,7 @@ bool send_req_key(node_t *to) { sptps_stop(&to->sptps); to->status.validkey = false; to->status.waitingforkey = true; - to->last_req_key = now.tv_sec; + to->last_req_key = mesh->loop.now.tv_sec; to->incompression = mesh->self->incompression; return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record); } @@ -150,7 +150,7 @@ static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, in sptps_stop(&from->sptps); from->status.validkey = false; from->status.waitingforkey = true; - from->last_req_key = now.tv_sec; + from->last_req_key = mesh->loop.now.tv_sec; sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record); sptps_receive_data(&from->sptps, buf, len); return true; diff --git a/src/protocol_misc.c b/src/protocol_misc.c index caaa08f1..c95c9cb9 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -22,6 +22,7 @@ #include "conf.h" #include "connection.h" #include "logger.h" +#include "meshlink_internal.h" #include "meta.h" #include "net.h" #include "netutl.h" @@ -88,7 +89,7 @@ bool termreq_h(connection_t *c, const char *request) { bool send_ping(connection_t *c) { c->status.pinged = true; - c->last_ping_time = now.tv_sec; + c->last_ping_time = mesh->loop.now.tv_sec; return send_request(c, "%d", PING); } diff --git a/src/route.c b/src/route.c index 21559728..68873096 100644 --- a/src/route.c +++ b/src/route.c @@ -31,11 +31,11 @@ static bool ratelimit(int frequency) { static time_t lasttime = 0; static int count = 0; - if(lasttime == now.tv_sec) { + if(lasttime == mesh->loop.now.tv_sec) { if(count >= frequency) return true; } else { - lasttime = now.tv_sec; + lasttime = mesh->loop.now.tv_sec; count = 0; }