]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix resource leaks found by cppcheck.
[meshlink] / src / meshlink.c
index c67be83087ee7f32f17afbeae5cb4c6f4c6954bb..68286d29046aa9e6477e012f5075f0a643f9e2d3 100644 (file)
@@ -39,6 +39,7 @@ typedef struct {
 #include "node.h"
 #include "protocol.h"
 #include "route.h"
+#include "sockaddr.h"
 #include "utils.h"
 #include "xalloc.h"
 #include "ed25519/sha512.h"
@@ -940,10 +941,17 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
-       pthread_mutex_lock(&(mesh->mesh_mutex));
        
        logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n");
 
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+
+       if(mesh->threadstarted) {
+               logger(mesh, MESHLINK_DEBUG, "thread was already running\n");
+               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               return true;
+       }
+
        if(mesh->listen_socket[0].tcp.fd < 0) {
                logger(mesh, MESHLINK_ERROR, "Listening socket not open\n");
                meshlink_errno = MESHLINK_ENETWORK;
@@ -964,6 +972,8 @@ bool meshlink_start(meshlink_handle_t *mesh) {
 
        // Start the main thread
 
+       event_loop_start(&mesh->loop);
+
        if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) {
                logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
                memset(&mesh->thread, 0, sizeof mesh->thread);
@@ -992,10 +1002,16 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        // Stop discovery
        discovery_stop(mesh);
 
-       // Shut down a listening socket to signal the main thread to shut down
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+       logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
+
+       // Shut down the main thread
+       event_loop_stop(&mesh->loop);
 
+       // Send ourselves a UDP packet to kick the event loop
        listen_socket_t *s = &mesh->listen_socket[0];
-       shutdown(s->tcp.fd, SHUT_RDWR);
+       if(sendto(s->udp.fd, "", 1, MSG_NOSIGNAL, &s->sa.sa, SALEN(s->sa.sa)) == -1)
+               logger(mesh, MESHLINK_ERROR, "Could not send a UDP packet to ourself");
 
        // Wait for the main thread to finish
        pthread_mutex_unlock(&(mesh->mesh_mutex));
@@ -1004,16 +1020,6 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 
        mesh->threadstarted = false;
 
-       // Fix the socket
-       
-       closesocket(s->tcp.fd);
-       io_del(&mesh->loop, &s->tcp);
-       s->tcp.fd = setup_listen_socket(&s->sa);
-       if(s->tcp.fd < 0)
-               logger(mesh, MESHLINK_ERROR, "Could not repair listenen socket!");
-       else
-               io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ);
-       
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
@@ -2036,7 +2042,6 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink
                *nmemb = result_size;
        } else {
                *nmemb = 0;
-               free(result);
                meshlink_errno = MESHLINK_ENOMEM;
        }