Add DeviceStandby option to only enable the device when nodes are reachable.

This adds a new DeviceStandby option; when it is disabled (the default),
behavior is unchanged. If it is enabled, tinc-up will not be called during
tinc initialization, but will instead be deferred until the first node is
reachable, and it will be closed as soon as no nodes are reachable.

This is useful because it means the device won't be set up until we are fairly
sure there is something listening on the other side. This is more user-friendly,
as one can check on the status of the tinc network connection just by checking
the status of the network interface. Besides, it prevents the OS from thinking
it is connected to some network when it is in fact completely isolated.
This commit is contained in:
Etienne Dechamps 2014-06-22 10:48:34 +01:00
parent f0885b8d2f
commit bd451cfe15
4 changed files with 67 additions and 24 deletions

View file

@ -204,6 +204,9 @@ static void sssp_bfs(void) {
static void check_reachability(void) {
/* Check reachability status. */
int reachable_count = 0;
int became_reachable_count = 0;
int became_unreachable_count = 0;
for splay_each(node_t, n, node_tree) {
if(n->status.visited != n->status.reachable) {
n->status.reachable = !n->status.reachable;
@ -212,9 +215,13 @@ static void check_reachability(void) {
if(n->status.reachable) {
logger(DEBUG_TRAFFIC, LOG_DEBUG, "Node %s (%s) became reachable",
n->name, n->hostname);
if (n != myself)
became_reachable_count++;
} else {
logger(DEBUG_TRAFFIC, LOG_DEBUG, "Node %s (%s) became unreachable",
n->name, n->hostname);
if (n != myself)
became_unreachable_count++;
}
if(experimental && OPTION_VERSION(n->options) >= 2)
@ -277,6 +284,16 @@ static void check_reachability(void) {
}
}
}
if(n->status.reachable && n != myself)
reachable_count++;
}
if (device_standby) {
if (reachable_count == 0 && became_unreachable_count > 0)
device_disable();
else if (reachable_count == became_reachable_count)
device_enable();
}
}