Avoid calling time(NULL).
authorGuus Sliepen <guus@tinc-vpn.org>
Fri, 8 Mar 2013 13:11:15 +0000 (14:11 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Fri, 8 Mar 2013 13:11:15 +0000 (14:11 +0100)
In most cases we can use the cached time.

src/graph.c
src/net.c
src/net_packet.c
src/net_setup.c
src/net_socket.c
src/protocol.c
src/protocol_auth.c
src/protocol_key.c
src/protocol_misc.c
src/route.c
src/tincd.c

index 7079f93..4506379 100644 (file)
@@ -204,7 +204,7 @@ static void check_reachability(void) {
        for splay_each(node_t, n, node_tree) {
                if(n->status.visited != n->status.reachable) {
                        n->status.reachable = !n->status.reachable;
-                       n->last_state_change = time(NULL);
+                       n->last_state_change = now.tv_sec;
 
                        if(n->status.reachable) {
                                logger(DEBUG_TRAFFIC, LOG_DEBUG, "Node %s (%s) became reachable",
index 0d374e6..1487e81 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -406,7 +406,7 @@ int reload_configuration(void) {
                free(fname);
        }
 
-       last_config_check = time(NULL);
+       last_config_check = now.tv_sec;
 
        return 0;
 }
index 5fd5632..8a4cebd 100644 (file)
@@ -458,7 +458,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 < time(NULL)) {
+               else if(n->last_req_key + 10 < 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;
index a4fd3a4..488cb7c 100644 (file)
@@ -751,7 +751,7 @@ static bool setup_myself(void) {
        myself->nexthop = myself;
        myself->via = myself;
        myself->status.reachable = true;
-       myself->last_state_change = time(NULL);
+       myself->last_state_change = now.tv_sec;
        myself->status.sptps = experimental;
        node_add(myself);
 
@@ -958,7 +958,7 @@ static bool setup_myself(void) {
                return false;
        }
 
-       last_config_check = time(NULL);
+       last_config_check = now.tv_sec;
 
        return true;
 }
index 0ee3655..5332ed2 100644 (file)
@@ -294,7 +294,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 = time(NULL);
+       c->last_ping_time = now.tv_sec;
        c->status.connecting = false;
 
        send_id(c);
@@ -508,7 +508,7 @@ begin:
        c->outdigest = myself->connection->outdigest;
        c->outmaclength = myself->connection->outmaclength;
        c->outcompression = myself->connection->outcompression;
-       c->last_ping_time = time(NULL);
+       c->last_ping_time = now.tv_sec;
 
        connection_add(c);
 
@@ -571,7 +571,7 @@ void handle_new_meta_connection(void *data, int flags) {
        c->address = sa;
        c->hostname = sockaddr2hostname(&sa);
        c->socket = fd;
-       c->last_ping_time = time(NULL);
+       c->last_ping_time = now.tv_sec;
 
        logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname);
 
@@ -610,7 +610,7 @@ void handle_new_unix_connection(void *data, int flags) {
        c->address = sa;
        c->hostname = xstrdup("localhost port unix");
        c->socket = fd;
-       c->last_ping_time = time(NULL);
+       c->last_ping_time = now.tv_sec;
 
        logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection from %s", c->hostname);
 
index fc16ffe..ad0fa8d 100644 (file)
@@ -195,7 +195,7 @@ bool seen_request(const char *request) {
        } else {
                new = xmalloc(sizeof *new);
                new->request = xstrdup(request);
-               new->firstseen = time(NULL);
+               new->firstseen = now.tv_sec;
                splay_insert(past_request_tree, new);
                timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
                return false;
index f4a30a4..ba5db2e 100644 (file)
@@ -160,7 +160,7 @@ bool id_h(connection_t *c, const char *request) {
        if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
                c->status.control = true;
                c->allow_request = CONTROL;
-               c->last_ping_time = time(NULL) + 3600;
+               c->last_ping_time = now.tv_sec + 3600;
 
                free(c->name);
                c->name = xstrdup("<control>");
index 115845a..57377b2 100644 (file)
@@ -111,7 +111,7 @@ bool send_req_key(node_t *to) {
                sptps_stop(&to->sptps);
                to->status.validkey = false;
                to->status.waitingforkey = true;
-               to->last_req_key = time(NULL);
+               to->last_req_key = now.tv_sec;
                to->incompression = myself->incompression;
                return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
        }
@@ -169,7 +169,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 = time(NULL);
+                       from->last_req_key = now.tv_sec;
                        sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
                        sptps_receive_data(&from->sptps, buf, len);
                        return true;
index 7617091..a4ad73d 100644 (file)
@@ -89,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 = time(NULL);
+       c->last_ping_time = now.tv_sec;
 
        return send_request(c, "%d", PING);
 }
index 1896374..00ba4c0 100644 (file)
@@ -229,7 +229,7 @@ static void learn_mac(mac_t *address) {
 
                subnet = new_subnet();
                subnet->type = SUBNET_MAC;
-               subnet->expires = time(NULL) + macexpire;
+               subnet->expires = now.tv_sec + macexpire;
                subnet->net.mac.address = *address;
                subnet->weight = 10;
                subnet_add(myself, subnet);
@@ -244,7 +244,7 @@ static void learn_mac(mac_t *address) {
                timeout_add(&age_subnets_timeout, age_subnets, NULL, &(struct timeval){10, rand() % 100000});
        } else {
                if(subnet->expires)
-                       subnet->expires = time(NULL) + macexpire;
+                       subnet->expires = now.tv_sec + macexpire;
        }
 }
 
index 8412c8f..333a207 100644 (file)
@@ -346,7 +346,8 @@ int main(int argc, char **argv) {
 
        /* Slllluuuuuuurrrrp! */
 
-       srand(time(NULL));
+       gettimeofday(&now, NULL);
+       srand(now.tv_sec + now.tv_usec);
        crypto_init();
 
        if(!read_server_config())