]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Remove global variables.
[meshlink] / src / meshlink.c
index 302624fc5d0944cdd53934bd272a11d942ee8d65..7e2e20e5c358b08b4ad7a57789055671cca3d626 100644 (file)
@@ -599,7 +599,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
        free(mesh->self->name);
        mesh->name = name;
        mesh->self->name = xstrdup(name);
-       mesh->self->devclass = devclass;
+       mesh->self->devclass = devclass == DEV_CLASS_UNKNOWN ? mesh->devclass : devclass;
 
        // Initialize configuration directory
        if(!config_init(mesh, "current")) {
@@ -744,8 +744,9 @@ static bool recvline(meshlink_handle_t *mesh, size_t len) {
 
        return true;
 }
+
 static bool sendline(int fd, char *format, ...) {
-       static char buffer[4096];
+       char buffer[4096];
        char *p = buffer;
        int blen = 0;
        va_list ap;
@@ -1391,10 +1392,27 @@ static void *meshlink_main_loop(void *arg) {
 
        pthread_mutex_lock(&(mesh->mesh_mutex));
 
+#if HAVE_CATTA
+
+       if(mesh->discovery) {
+               discovery_start(mesh);
+       }
+
+#endif
+
        logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
        main_loop(mesh);
        logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
 
+#if HAVE_CATTA
+
+       // Stop discovery
+       if(mesh->discovery) {
+               discovery_stop(mesh);
+       }
+
+#endif
+
        pthread_mutex_unlock(&(mesh->mesh_mutex));
        return NULL;
 }
@@ -1453,14 +1471,6 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        mesh->threadstarted = true;
 
-#if HAVE_CATTA
-
-       if(mesh->discovery) {
-               discovery_start(mesh);
-       }
-
-#endif
-
        assert(mesh->self->ecdsa);
        assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
 
@@ -1478,15 +1488,6 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        pthread_mutex_lock(&(mesh->mesh_mutex));
        logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
 
-#if HAVE_CATTA
-
-       // Stop discovery
-       if(mesh->discovery) {
-               discovery_stop(mesh);
-       }
-
-#endif
-
        // Shut down the main thread
        event_loop_stop(&mesh->loop);
 
@@ -3090,6 +3091,16 @@ end:
 #endif
 }
 
+void handle_network_change(meshlink_handle_t *mesh, bool online) {
+       (void)online;
+
+       if(!mesh->connections) {
+               return;
+       }
+
+       retry(mesh);
+}
+
 static void __attribute__((constructor)) meshlink_init(void) {
        crypto_init();
        unsigned int seed;
@@ -3102,7 +3113,7 @@ static void __attribute__((destructor)) meshlink_exit(void) {
 }
 
 /// Device class traits
-dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = {
+const dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX + 1] = {
        { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE
        { .min_connects = 3, .max_connects = 100, .edge_weight = 3 },   // DEV_CLASS_STATIONARY
        { .min_connects = 3, .max_connects = 3, .edge_weight = 6 },             // DEV_CLASS_PORTABLE