From 230b5907e242aa48bb8c8d09450fbd3767e9c24a Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 19 Jul 2019 14:50:25 +0200 Subject: [PATCH] Speed up reconnections on network interface changes. Catta informs us whenever an interface comes online or goes offline. If we detect that there are no online interfaces, immediately terminate all meta- connections. Otherwise, reset the ping timers and reconnection timers for outgoing connections. --- src/discovery.c | 13 ++++++++++++- src/meshlink.c | 15 +++++++++++++++ src/meshlink_internal.h | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/discovery.c b/src/discovery.c index 67a34949..95e5ed65 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -15,6 +15,7 @@ #include "logger.h" #include "node.h" #include "connection.h" +#include "xalloc.h" #define MESHLINK_MDNS_SERVICE_TYPE "_%s._tcp" #define MESHLINK_MDNS_NAME_KEY "name" @@ -60,7 +61,7 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup case CATTA_ENTRY_GROUP_UNCOMMITED: case CATTA_ENTRY_GROUP_REGISTERING: - ; + break; } pthread_mutex_unlock(&(mesh->mesh_mutex)); @@ -310,9 +311,17 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde case CATTA_BROWSER_NEW: catta_s_service_resolver_new(mesh->catta_server, interface_, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, discovery_resolve_callback, mesh); + pthread_mutex_lock(&mesh->mesh_mutex); + handle_network_change(mesh, ++mesh->catta_interfaces); + pthread_mutex_unlock(&mesh->mesh_mutex); break; case CATTA_BROWSER_REMOVE: + pthread_mutex_lock(&mesh->mesh_mutex); + handle_network_change(mesh, --mesh->catta_interfaces); + pthread_mutex_unlock(&mesh->mesh_mutex); + break; + case CATTA_BROWSER_ALL_FOR_NOW: case CATTA_BROWSER_CACHE_EXHAUSTED: break; @@ -500,4 +509,6 @@ void discovery_stop(meshlink_handle_t *mesh) { free(mesh->catta_servicetype); mesh->catta_servicetype = NULL; } + + mesh->catta_interfaces = 0; } diff --git a/src/meshlink.c b/src/meshlink.c index 302624fc..b1f5c918 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -3090,6 +3090,21 @@ end: #endif } +void handle_network_change(meshlink_handle_t *mesh, bool online) { + if(!mesh->connections) { + return; + } + + if(online) { + retry(mesh); + } else { + // We are off-line. Terminate all active connections. + for list_each(connection_t, c, mesh->connections) { + terminate_connection(mesh, c, false); + } + } +} + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); unsigned int seed; diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 84368ee8..b31c16eb 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -166,6 +166,7 @@ struct meshlink_handle { struct CattaSimplePoll *catta_poll; struct CattaSEntryGroup *catta_group; char *catta_servicetype; + unsigned int catta_interfaces; int netns; void *config_key; @@ -205,6 +206,7 @@ extern meshlink_log_level_t global_log_level; extern meshlink_log_cb_t global_log_cb; extern int check_port(meshlink_handle_t *mesh); extern void handle_duplicate_node(meshlink_handle_t *mesh, struct node_t *n); +extern void handle_network_change(meshlink_handle_t *mesh, bool online); /// Device class traits typedef struct { -- 2.39.2