]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Prevent meshlink_join() when the joining node is already part of a mesh.
[meshlink] / src / meshlink.c
index c5dc33aca3a091a6a2793266cb5aa58369bc6a70..302624fc5d0944cdd53934bd272a11d942ee8d65 100644 (file)
@@ -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;
@@ -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;