X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=2c1253c79864d10b0cf567b233ffce4523a88191;hb=5aa1e39a0ff8304e24151d3a69a68737084269e2;hp=9b13dca1652d22658f282be6cfbfbce02c81357e;hpb=5f3e39a3b08c7d42d23a18a3e5e950e099bc8c57;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 9b13dca1..2c1253c7 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -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;