]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Use condition variables to wait for threads to finish initializing.
[meshlink] / src / meshlink.c
index 9b13dca1652d22658f282be6cfbfbce02c81357e..20dad9f43caf437d15155931f2d528b3e1a30e42 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;
@@ -1381,21 +1382,42 @@ static void *meshlink_main_loop(void *arg) {
 #ifdef HAVE_SETNS
 
                if(setns(mesh->netns, CLONE_NEWNET) != 0) {
+                       pthread_cond_signal(&mesh->cond);
                        return NULL;
                }
 
 #else
+                       pthread_cond_signal(&mesh->cond);
                return NULL;
 #endif // HAVE_SETNS
        }
 
+#if HAVE_CATTA
+
+       if(mesh->discovery) {
+               discovery_start(mesh);
+       }
+
+#endif
+
        pthread_mutex_lock(&(mesh->mesh_mutex));
 
        logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
+       pthread_cond_broadcast(&mesh->cond);
        main_loop(mesh);
        logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
 
        pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+#if HAVE_CATTA
+
+       // Stop discovery
+       if(mesh->discovery) {
+               discovery_stop(mesh);
+       }
+
+#endif
+
        return NULL;
 }
 
@@ -1412,6 +1434,9 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        pthread_mutex_lock(&(mesh->mesh_mutex));
 
+       assert(mesh->self->ecdsa);
+       assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
+
        if(mesh->threadstarted) {
                logger(mesh, MESHLINK_DEBUG, "thread was already running\n");
                pthread_mutex_unlock(&(mesh->mesh_mutex));
@@ -1451,20 +1476,9 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                return false;
        }
 
+       pthread_cond_wait(&mesh->cond, &mesh->mesh_mutex);
        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));
-
-
        pthread_mutex_unlock(&(mesh->mesh_mutex));
        return true;
 }
@@ -1478,15 +1492,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);
 
@@ -2310,7 +2315,15 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        //Before doing meshlink_join make sure we are not connected to another mesh
        if(mesh->threadstarted) {
-               logger(mesh, MESHLINK_DEBUG, "Already connected to a mesh\n");
+               logger(mesh, MESHLINK_ERROR, "Cannot join while started\n");
+               meshlink_errno = MESHLINK_EINVAL;
+               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               return false;
+       }
+
+       // Refuse to join a mesh if we are already part of one. We are part of one if we know at least one other node.
+       if(mesh->nodes->count > 1) {
+               logger(mesh, MESHLINK_ERROR, "Already part of an existing mesh\n");
                meshlink_errno = MESHLINK_EINVAL;
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
@@ -3082,6 +3095,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;
@@ -3094,7 +3117,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