From 5dec7459d9920c930bdfb3ac0f2a3740a6247ec0 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 13 Aug 2017 14:53:51 +0200 Subject: [PATCH] Allow Catta to be disabled. Normally, we want to enable Catta, however for testing it produces a lot of unwanted network packets and log messages. --- src/meshlink++.h | 10 ++++++++++ src/meshlink.c | 30 ++++++++++++++++++++++++++++-- src/meshlink.h | 10 ++++++++++ src/meshlink_internal.h | 1 + test/basicpp.cpp | 6 ++++++ test/channels-fork.c | 4 ++++ test/channels.c | 2 ++ test/import-export.c | 5 +++++ test/invite-join.c | 5 +++++ 9 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/meshlink++.h b/src/meshlink++.h index f1c415a9..b012bb4a 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -558,6 +558,16 @@ public: return meshlink_channel_send(handle, channel, data, len); } + /// Enable or disable zeroconf discovery of local peers + /** This controls whether zeroconf discovery using the Catta library will be + * enabled to search for peers on the local network. By default, it is enabled. + * + * @param enable Set to true to enable discovery, false to disable. + */ + void enable_discovery(bool enable = true) { + meshlink_enable_discovery(handle, enable); + } + private: // non-copyable: mesh(const mesh&) /* TODO: C++11: = delete */; diff --git a/src/meshlink.c b/src/meshlink.c index 4a8cef20..6a2c99da 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1009,7 +1009,8 @@ bool meshlink_start(meshlink_handle_t *mesh) { mesh->threadstarted=true; - discovery_start(mesh); + if(mesh->discovery) + discovery_start(mesh); pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; @@ -1025,7 +1026,8 @@ void meshlink_stop(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n"); // Stop discovery - discovery_stop(mesh); + if(mesh->discovery) + discovery_stop(mesh); // Shut down the main thread event_loop_stop(&mesh->loop); @@ -2322,6 +2324,30 @@ void update_node_status(meshlink_handle_t *mesh, node_t *n) { mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable); } +void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) { + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + pthread_mutex_lock(&mesh->mesh_mutex); + + if(mesh->discovery == enable) + goto end; + + if(mesh->threadstarted) { + if(enable) + discovery_start(mesh); + else + discovery_stop(mesh); + } + + mesh->discovery = enable; + +end: + pthread_mutex_unlock(&mesh->mesh_mutex); +} + static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); unsigned int seed; diff --git a/src/meshlink.h b/src/meshlink.h index 30491b00..a768d435 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -832,6 +832,16 @@ extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node */ extern meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink_edge_t **edges, size_t *nmemb); +/// Enable or disable zeroconf discovery of local peers + +/** This controls whether zeroconf discovery using the Catta library will be + * enabled to search for peers on the local network. By default, it is enabled. + * + * @param mesh A handle which represents an instance of MeshLink. + * @param enable Set to true to enable discovery, false to disable. + */ +extern void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable); + #ifdef __cplusplus } #endif diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index fa5e60ea..492e2b2b 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -113,6 +113,7 @@ struct meshlink_handle { char *proxypass; proxytype_t proxytype; + bool discovery; // Whether Catta is enabled or not bool localdiscovery; sockaddr_t localdiscovery_address; diff --git a/test/basicpp.cpp b/test/basicpp.cpp index 02d4cb61..ed2815a1 100644 --- a/test/basicpp.cpp +++ b/test/basicpp.cpp @@ -25,6 +25,10 @@ int main(int argc, char *argv[]) { return 1; } + // Disable local discovery. + + mesh.enable_discovery(false); + // Start and stop the mesh. if(!mesh.start()) { @@ -66,6 +70,8 @@ int main(int argc, char *argv[]) { // Start and stop the mesh. + mesh.enable_discovery(false); + if(!mesh.start()) { cerr << "Foo could not start a third time\n"; return 1; diff --git a/test/channels-fork.c b/test/channels-fork.c index 7fdd5b8e..a95efbe2 100644 --- a/test/channels-fork.c +++ b/test/channels-fork.c @@ -58,6 +58,8 @@ int main1(int rfd, int wfd) { return 1; } + meshlink_enable_discovery(mesh1, false); + meshlink_add_address(mesh1, "localhost"); char *data = meshlink_export(mesh1); @@ -142,6 +144,8 @@ int main2(int rfd, int wfd) { return 1; } + meshlink_enable_discovery(mesh2, false); + char *data = meshlink_export(mesh2); if(!data) { fprintf(stderr, "Bar could not export its configuration\n"); diff --git a/test/channels.c b/test/channels.c index c6d9fa8f..99a97a33 100644 --- a/test/channels.c +++ b/test/channels.c @@ -87,6 +87,8 @@ int main(int argc, char *argv[]) { return 1; } + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb); meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb); diff --git a/test/import-export.c b/test/import-export.c index 22cd80bb..411a7a21 100644 --- a/test/import-export.c +++ b/test/import-export.c @@ -27,6 +27,11 @@ int main(int argc, char *argv[]) { return 1; } + // Disable local discovery + + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); + // Import and export both side's data meshlink_add_address(mesh1, "localhost"); diff --git a/test/invite-join.c b/test/invite-join.c index 41b62bd4..d20ad37d 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -27,6 +27,11 @@ int main(int argc, char *argv[]) { return 1; } + // Disable local discovery. + + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); + // Start the first instance and have it generate an invitation. meshlink_set_node_status_cb(mesh1, status_cb); -- 2.39.5