#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;
while(loop->running) {
gettimeofday(&loop->now, NULL);
- now = loop->now;
struct timeval diff, *tv = NULL;
while(loop->timeouts.head) {
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) {
};
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);
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",
}
static void __attribute__((constructor)) meshlink_init(void) {
- gettimeofday(&now, NULL);
- srand(now.tv_sec + now.tv_usec);
crypto_init();
}
*/
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 {
}
}
- mesh->last_config_check = now.tv_sec;
+ mesh->last_config_check = mesh->loop.now.tv_sec;
return 0;
}
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;
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;
}
}
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;
}
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();
/* Done. */
- mesh->last_config_check = now.tv_sec;
+ mesh->last_config_check = mesh->loop.now.tv_sec;
return true;
}
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);
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);
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) {
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) {
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);
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++;
} 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;
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);
}
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;
#include "conf.h"
#include "connection.h"
#include "logger.h"
+#include "meshlink_internal.h"
#include "meta.h"
#include "net.h"
#include "netutl.h"
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);
}
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;
}