]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Correctly set device class after joining if the server didn't specify one.
[meshlink] / src / meshlink.c
index c5dc33aca3a091a6a2793266cb5aa58369bc6a70..c42eb7690543b77037284a22ff678237ff3d9631 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")) {
@@ -1057,8 +1057,8 @@ bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const
        return true;
 }
 
-bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const char *new_key, size_t new_keylen) {
-       if(!mesh || !new_key || !new_keylen || !*new_key) {
+bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, size_t new_keylen) {
+       if(!mesh || !new_key || !new_keylen) {
                logger(mesh, MESHLINK_ERROR, "Invalid arguments given!\n");
                meshlink_errno = MESHLINK_EINVAL;
                return false;
@@ -1391,10 +1391,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 +1470,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 +1487,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 +2310,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 +3090,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;