]> git.meshlink.io Git - meshlink/commitdiff
Run Catta in the same network namespace as MeshLink.
authorGuus Sliepen <guus@meshlink.io>
Sat, 3 Aug 2019 20:54:34 +0000 (22:54 +0200)
committerGuus Sliepen <guus@meshlink.io>
Sun, 4 Aug 2019 10:23:08 +0000 (12:23 +0200)
src/discovery.c
src/meshlink.c

index 95e5ed65d86da5e75297fd0fa94a9dd8eb9e762f..076bddcfd08201db5163636d21f24d604b25cdbd 100644 (file)
@@ -39,8 +39,6 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup
        assert(mesh->catta_server != NULL);
        assert(mesh->catta_poll != NULL);
 
-       pthread_mutex_lock(&(mesh->mesh_mutex));
-
        /* Called whenever the entry group state changes */
        switch(state) {
        case CATTA_ENTRY_GROUP_ESTABLISHED:
@@ -63,8 +61,6 @@ static void discovery_entry_group_callback(CattaServer *server, CattaSEntryGroup
        case CATTA_ENTRY_GROUP_REGISTERING:
                break;
        }
-
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 
@@ -259,7 +255,6 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
 
                                        if(naddress.unknown.family != AF_UNKNOWN) {
                                                meshlink_hint_address(mesh, (meshlink_node_t *)node, (struct sockaddr *)&naddress);
-                                               pthread_mutex_lock(&(mesh->mesh_mutex));
 
                                                node_t *n = (node_t *)node;
 
@@ -275,7 +270,6 @@ static void discovery_resolve_callback(CattaSServiceResolver *resolver, CattaIfI
                                                        n->connection->last_ping_time = 0;
                                                }
 
-                                               pthread_mutex_unlock(&(mesh->mesh_mutex));
                                        } else {
                                                logger(mesh, MESHLINK_WARNING, "Could not resolve node %s to a known address family type.\n", node->name);
                                        }
@@ -328,18 +322,6 @@ static void discovery_browse_callback(CattaSServiceBrowser *browser, CattaIfInde
        }
 }
 
-static void *discovery_loop(void *userdata) {
-       meshlink_handle_t *mesh = userdata;
-
-       // asserts
-       assert(mesh != NULL);
-       assert(mesh->catta_poll != NULL);
-
-       catta_simple_poll_loop(mesh->catta_poll);
-
-       return NULL;
-}
-
 static void discovery_log_cb(CattaLogLevel level, const char *txt) {
        meshlink_log_level_t mlevel = MESHLINK_CRITICAL;
 
@@ -366,16 +348,9 @@ static void discovery_log_cb(CattaLogLevel level, const char *txt) {
        logger(NULL, mlevel, "%s\n", txt);
 }
 
-bool discovery_start(meshlink_handle_t *mesh) {
-       logger(mesh, MESHLINK_DEBUG, "discovery_start called\n");
-
-       // asserts
+static void *discovery_loop(void *userdata) {
+       meshlink_handle_t *mesh = userdata;
        assert(mesh != NULL);
-       assert(mesh->catta_poll == NULL);
-       assert(mesh->catta_server == NULL);
-       assert(mesh->catta_browser == NULL);
-       assert(mesh->discovery_threadstarted == false);
-       assert(mesh->catta_servicetype == NULL);
 
        // handle catta logs
        catta_set_log_function(discovery_log_cb);
@@ -430,16 +405,7 @@ bool discovery_start(meshlink_handle_t *mesh) {
                goto fail;
        }
 
-       // Start the discovery thread
-       if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) {
-               logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno));
-               memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread);
-               goto fail;
-       }
-
-       mesh->discovery_threadstarted = true;
-
-       return true;
+       catta_simple_poll_loop(mesh->catta_poll);
 
 fail:
 
@@ -448,6 +414,12 @@ fail:
                mesh->catta_browser = NULL;
        }
 
+       if(mesh->catta_group) {
+               catta_s_entry_group_reset(mesh->catta_group);
+               catta_s_entry_group_free(mesh->catta_group);
+               mesh->catta_group = NULL;
+       }
+
        if(mesh->catta_server != NULL) {
                catta_server_free(mesh->catta_server);
                mesh->catta_server = NULL;
@@ -463,7 +435,30 @@ fail:
                mesh->catta_servicetype = NULL;
        }
 
-       return false;
+       return NULL;
+}
+
+bool discovery_start(meshlink_handle_t *mesh) {
+       logger(mesh, MESHLINK_DEBUG, "discovery_start called\n");
+
+       // asserts
+       assert(mesh != NULL);
+       assert(mesh->catta_poll == NULL);
+       assert(mesh->catta_server == NULL);
+       assert(mesh->catta_browser == NULL);
+       assert(mesh->discovery_threadstarted == false);
+       assert(mesh->catta_servicetype == NULL);
+
+       // Start the discovery thread
+       if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) {
+               logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno));
+               memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread);
+               return false;
+       }
+
+       mesh->discovery_threadstarted = true;
+
+       return true;
 }
 
 void discovery_stop(meshlink_handle_t *mesh) {
@@ -483,32 +478,5 @@ void discovery_stop(meshlink_handle_t *mesh) {
                mesh->discovery_threadstarted = false;
        }
 
-       // Clean up resources
-       if(mesh->catta_browser != NULL) {
-               catta_s_service_browser_free(mesh->catta_browser);
-               mesh->catta_browser = NULL;
-       }
-
-       if(mesh->catta_group) {
-               catta_s_entry_group_reset(mesh->catta_group);
-               catta_s_entry_group_free(mesh->catta_group);
-               mesh->catta_group = NULL;
-       }
-
-       if(mesh->catta_server != NULL) {
-               catta_server_free(mesh->catta_server);
-               mesh->catta_server = NULL;
-       }
-
-       if(mesh->catta_poll != NULL) {
-               catta_simple_poll_free(mesh->catta_poll);
-               mesh->catta_poll = NULL;
-       }
-
-       if(mesh->catta_servicetype != NULL) {
-               free(mesh->catta_servicetype);
-               mesh->catta_servicetype = NULL;
-       }
-
        mesh->catta_interfaces = 0;
 }
index 4a4d3c65f007462df313bf092531b4a209913d4e..2c1253c79864d10b0cf567b233ffce4523a88191 100644 (file)
@@ -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);