]> git.meshlink.io Git - meshlink/commitdiff
Speed up reconnections on network interface changes.
authorGuus Sliepen <guus@meshlink.io>
Fri, 19 Jul 2019 12:50:25 +0000 (14:50 +0200)
committerGuus Sliepen <guus@meshlink.io>
Fri, 19 Jul 2019 12:51:10 +0000 (14:51 +0200)
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
src/meshlink.c
src/meshlink_internal.h

index 67a349499c9c4a0162a5252aa38797f0e4775602..95e5ed65d86da5e75297fd0fa94a9dd8eb9e762f 100644 (file)
@@ -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;
 }
index 302624fc5d0944cdd53934bd272a11d942ee8d65..b1f5c918fa68dd9d1d29e09a710c60d73b3bd51e 100644 (file)
@@ -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;
index 84368ee8cac938881897fc1dcc29b12600568128..b31c16eb1cc3b92fef8bd1373f9c07573a11e2d9 100644 (file)
@@ -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 {