]> git.meshlink.io Git - meshlink/commitdiff
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 7079f93efc5e98513541b47de14d823211874b30..45063795893e7789b586f4dd61577915c7aa98af 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 0d374e6e739de3522bdfdc73317d2eb61f979926..1487e8182b7dd928b1a55bfd8d476fa584bc613e 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 5fd563258d4a3fa2f05dc8a94f3ce5dffeb4b085..8a4cebd0f2c47543dc33b7653a925365483d4091 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 a4fd3a4beffc8b8c710145db94fd4b4357f33e2b..488cb7c649c44bf81d9ff456cfba173a4f69a215 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 0ee365538be3c8248b721d8a70c934b0d85b7b6d..5332ed20326702c8de68bf6ce22b6df2ea560924 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 fc16ffe7c08b66166e114094daab6dd814e78a29..ad0fa8dda20033649f5c4a48cff203420833729a 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 f4a30a412071854530df028aad559d632171d568..ba5db2ea2e743f28d904ab480b6e41f8615dd691 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 115845a0e206777e13df93aba2455feb4a5e3636..57377b2fb22e55ec14b2486a1873007d6e0c0641 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 7617091e5eac3711c505e2d84ce0d150ca0b3966..a4ad73d6531c12a31d6a552e906efd812c8d800f 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 189637424bf7e4f79fcf324140036ded91e833f8..00ba4c05b19a7a33f5ed978270ed3b24f99b8a7a 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 8412c8f7cbe5875a8a66167cf267134db13d034f..333a207a9913ee57469ad48ace992f012f85f547 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())