]> git.meshlink.io Git - meshlink/commitdiff
Remove global variable "now".
authorGuus Sliepen <guus@meshlink.io>
Sat, 26 Apr 2014 07:38:55 +0000 (09:38 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sat, 26 Apr 2014 07:38:55 +0000 (09:38 +0200)
12 files changed:
src/event.c
src/event.h
src/graph.c
src/meshlink.c
src/net.c
src/net_packet.c
src/net_setup.c
src/net_socket.c
src/protocol.c
src/protocol_key.c
src/protocol_misc.c
src/route.c

index 065f1b5eaf5a52710f8c9412ddccd03b656e8f90..85ea9bb8c776b4cf319534b2761adcc23add84e2 100644 (file)
@@ -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) {
index 3d49fe2d9b825e74e41d9c581b3b4891fd993290..0993e24c1a52964d7e61532c318eb1b777647ed6 100644 (file)
@@ -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);
index 017e10dc4ba6fd17f0aad6bf901b0d3d31d4ce2d..ef152a0677e2c057559cde151eed69fb1fbdd713 100644 (file)
@@ -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",
index 460256551ee7198ff2e83fc4fb9db7507d4e7d74..e6f44c0ce69a513f88c5736342dc33da2e83671d 100644 (file)
@@ -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();
 }
 
index 12f2f33d7c48e921ced4558ef77b3e423dd5dee9..878a2f99b5e0de984d34762bb0775db0650ec18d 100644 (file)
--- 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;
 }
index 63d6e4eaf6f2f7aad8ec3593cd617fd1ef1e983a..0fd2cd58982284bc80cbec5d79923634077d89e3 100644 (file)
@@ -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;
 }
 
index 896df4327269c73bd9696a891ecbca3c65d054c7..ba17b099abcf80a06d09893edf02fc2d05b70469 100644 (file)
@@ -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;
 }
index 589c4caa9b5424bb3113d61b6b59e0b124313e55..84985587a7658791f85f6982967cb42a769fb79d 100644 (file)
@@ -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);
 
index f8b827d60ca8277ef1b77fe6ac28a2a9720e6f9a..c1d635d04627627b553aaad71de509311e347aed 100644 (file)
@@ -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;
index 8ab1325c23d701982a29a7163e871869e9d7757c..887ccd0d9f394ed6a09e8f14a46dbbb9812e1d77 100644 (file)
@@ -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;
index caaa08f11d0beb61a557c7e6e307e03e040294d6..c95c9cb92028bfdd279a1d9d4cd76728f85e02b3 100644 (file)
@@ -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);
 }
index 21559728eecae0e261fdf374dc2d0825ba4dc3fd..68873096e76afde29429ab0f76e74806f56b06bf 100644 (file)
@@ -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;
        }