From 4c30004cb6dc23616d7295b0ce631f066e7f1f82 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 8 Mar 2013 14:11:15 +0100 Subject: [PATCH] Avoid calling time(NULL). In most cases we can use the cached time. --- src/graph.c | 2 +- src/net.c | 2 +- src/net_packet.c | 2 +- src/net_setup.c | 4 ++-- src/net_socket.c | 8 ++++---- src/protocol.c | 2 +- src/protocol_auth.c | 2 +- src/protocol_key.c | 4 ++-- src/protocol_misc.c | 2 +- src/route.c | 4 ++-- src/tincd.c | 3 ++- 11 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/graph.c b/src/graph.c index 7079f93e..45063795 100644 --- a/src/graph.c +++ b/src/graph.c @@ -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", diff --git a/src/net.c b/src/net.c index 0d374e6e..1487e818 100644 --- 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; } diff --git a/src/net_packet.c b/src/net_packet.c index 5fd56325..8a4cebd0 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -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; diff --git a/src/net_setup.c b/src/net_setup.c index a4fd3a4b..488cb7c6 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -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; } diff --git a/src/net_socket.c b/src/net_socket.c index 0ee36553..5332ed20 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -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); diff --git a/src/protocol.c b/src/protocol.c index fc16ffe7..ad0fa8dd 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -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; diff --git a/src/protocol_auth.c b/src/protocol_auth.c index f4a30a41..ba5db2ea 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -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(""); diff --git a/src/protocol_key.c b/src/protocol_key.c index 115845a0..57377b2f 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -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; diff --git a/src/protocol_misc.c b/src/protocol_misc.c index 7617091e..a4ad73d6 100644 --- a/src/protocol_misc.c +++ b/src/protocol_misc.c @@ -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); } diff --git a/src/route.c b/src/route.c index 18963742..00ba4c05 100644 --- a/src/route.c +++ b/src/route.c @@ -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; } } diff --git a/src/tincd.c b/src/tincd.c index 8412c8f7..333a207a 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -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()) -- 2.20.1