X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet.c;h=8cb7ed739f6a2e793f7643b23b8c4c779f8d3c53;hb=1be0c284c7c8d34c2dd6c2160ce49aeae468e867;hp=4f05789cfd355bab5e51ec68f592472f87dde5c7;hpb=5baecfd11be67bb80aab6c482e0b0ac98b267cca;p=tinc diff --git a/src/net.c b/src/net.c index 4f05789c..8cb7ed73 100644 --- a/src/net.c +++ b/src/net.c @@ -94,29 +94,31 @@ void purge(void) { void terminate_connection(connection_t *c, bool report) { logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Closing connection with %s (%s)", c->name, c->hostname); - if(c->node && c->node->connection == c) - c->node->connection = NULL; + if(c->node) { + if(c->node->connection == c) + c->node->connection = NULL; - if(c->edge) { - if(report && !tunnelserver) - send_del_edge(everyone, c->edge); + if(c->edge) { + if(report && !tunnelserver) + send_del_edge(everyone, c->edge); - edge_del(c->edge); - c->edge = NULL; + edge_del(c->edge); + c->edge = NULL; - /* Run MST and SSSP algorithms */ + /* Run MST and SSSP algorithms */ - graph(); + graph(); - /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ + /* If the node is not reachable anymore but we remember it had an edge to us, clean it up */ - if(report && !c->node->status.reachable) { - edge_t *e; - e = lookup_edge(c->node, myself); - if(e) { - if(!tunnelserver) - send_del_edge(everyone, e); - edge_del(e); + if(report && !c->node->status.reachable) { + edge_t *e; + e = lookup_edge(c->node, myself); + if(e) { + if(!tunnelserver) + send_del_edge(everyone, e); + edge_del(e); + } } } } @@ -149,7 +151,7 @@ static void timeout_handler(void *data) { bool close_all_connections = false; /* - timeout_hanlder will start after 30 seconds from start of tincd + timeout_handler will start after 30 seconds from start of tincd hold information about the elapsed time since last time the handler has been run */ @@ -177,6 +179,7 @@ static void timeout_handler(void *data) { last_periodic_run_time = now; for list_each(connection_t, c, connection_list) { + // control connections (eg. tinc ctl) do not have any timeout if(c->status.control) continue; @@ -186,26 +189,34 @@ static void timeout_handler(void *data) { continue; } - if(c->last_ping_time + pingtimeout <= now.tv_sec) { - if(c->edge) { - try_tx(c->node, false); - 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 + pinginterval <= now.tv_sec) { - send_ping(c); - continue; - } else { - continue; - } - } else { - if(c->status.connecting) - logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname); - else - logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname); - } + // Bail out early if we haven't reached the ping timeout for this node yet + if(c->last_ping_time + pingtimeout > now.tv_sec) + continue; + + // timeout during connection establishing + if(!c->edge) { + if(c->status.connecting) + logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout while connecting to %s (%s)", c->name, c->hostname); + else + logger(DEBUG_CONNECTIONS, LOG_WARNING, "Timeout from %s (%s) during authentication", c->name, c->hostname); + terminate_connection(c, c->edge); + continue; + } + + // helps in UDP holepunching + try_tx(c->node, false); + + // timeout during ping + 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)); + terminate_connection(c, c->edge); + continue; } + // check whether we need to send a new ping + if(c->last_ping_time + pinginterval <= now.tv_sec) + send_ping(c); } timeout_set(data, &(struct timeval){1, rand() % 100000});