]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Add missing mutex locks.
[meshlink] / src / meshlink.c
index a9e1b551df18362848fc68a7b5e1f7a0f1192850..0826487674f2467d24c8d201968e8e815b1a702b 100644 (file)
@@ -514,7 +514,7 @@ static bool try_bind(int port) {
        return true;
 }
 
-int check_port(meshlink_handle_t *mesh) {
+static int check_port(meshlink_handle_t *mesh) {
        for(int i = 0; i < 1000; i++) {
                int port = 0x1000 + (rand() & 0x7fff);
 
@@ -1196,7 +1196,8 @@ meshlink_handle_t *meshlink_open_encrypted(const char *confbase, const char *nam
        }
 
        /* Create a temporary struct on the stack, to avoid allocating and freeing one. */
-       meshlink_open_params_t params = {NULL};
+       meshlink_open_params_t params;
+       memset(&params, 0, sizeof(params));
 
        params.confbase = (char *)confbase;
        params.name = (char *)name;
@@ -1213,7 +1214,8 @@ meshlink_handle_t *meshlink_open_encrypted(const char *confbase, const char *nam
 
 meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) {
        /* Create a temporary struct on the stack, to avoid allocating and freeing one. */
-       meshlink_open_params_t params = {NULL};
+       meshlink_open_params_t params;
+       memset(&params, 0, sizeof(params));
 
        params.name = (char *)name;
        params.appname = (char *)appname;
@@ -1670,6 +1672,17 @@ void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_c
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
+void meshlink_set_node_pmtu_cb(meshlink_handle_t *mesh, meshlink_node_pmtu_cb_t cb) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+       mesh->node_pmtu_cb = cb;
+       pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
 void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb) {
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -2127,7 +2140,13 @@ int meshlink_get_port(meshlink_handle_t *mesh) {
                return -1;
        }
 
-       return atoi(mesh->myport);
+       int port;
+
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+       port = atoi(mesh->myport);
+       pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+       return port;
 }
 
 bool meshlink_set_port(meshlink_handle_t *mesh, int port) {
@@ -2298,7 +2317,8 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c
         * Note: make sure we only add config files of nodes that are in the core mesh or the same submesh,
         * and are not blacklisted.
         */
-       config_t configs[5] = {NULL};
+       config_t configs[5];
+       memset(configs, 0, sizeof(configs));
        int count = 0;
 
        if(config_read(mesh, "current", mesh->self->name, &configs[count], mesh->config_key)) {
@@ -2809,6 +2829,10 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
        node_write_config(mesh, n);
        config_sync(mesh, "current");
 
+       if(n->status.reachable) {
+               update_node_status(mesh, n);
+       }
+
        pthread_mutex_unlock(&(mesh->mesh_mutex));
        return;
 }
@@ -3033,9 +3057,15 @@ static void channel_poll(struct utcp_connection *connection, size_t len) {
 }
 
 void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) {
-       (void)mesh;
+       if(!mesh || !channel) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       pthread_mutex_lock(&mesh->mesh_mutex);
        channel->poll_cb = cb;
        utcp_set_poll_cb(channel->c, (cb || channel->aio_send) ? channel_poll : NULL);
+       pthread_mutex_unlock(&mesh->mesh_mutex);
 }
 
 void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) {
@@ -3093,6 +3123,8 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
                return NULL;
        }
 
+       pthread_mutex_lock(&mesh->mesh_mutex);
+
        node_t *n = (node_t *)node;
 
        if(!n->utcp) {
@@ -3101,12 +3133,14 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
 
                if(!n->utcp) {
                        meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
+                       pthread_mutex_unlock(&mesh->mesh_mutex);
                        return NULL;
                }
        }
 
        if(n->status.blacklisted) {
                logger(mesh, MESHLINK_ERROR, "Cannot open a channel with blacklisted node\n");
+               pthread_mutex_unlock(&mesh->mesh_mutex);
                return NULL;
        }
 
@@ -3115,6 +3149,8 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n
        channel->receive_cb = cb;
        channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags);
 
+       pthread_mutex_unlock(&mesh->mesh_mutex);
+
        if(!channel->c) {
                meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL;
                free(channel);
@@ -3134,7 +3170,9 @@ void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *chan
                return;
        }
 
+       pthread_mutex_lock(&mesh->mesh_mutex);
        utcp_shutdown(channel->c, direction);
+       pthread_mutex_unlock(&mesh->mesh_mutex);
 }
 
 void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) {
@@ -3143,6 +3181,8 @@ void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel
                return;
        }
 
+       pthread_mutex_lock(&mesh->mesh_mutex);
+
        utcp_close(channel->c);
 
        /* Clean up any outstanding AIO buffers. */
@@ -3158,6 +3198,8 @@ void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel
                free(aio);
        }
 
+       pthread_mutex_unlock(&mesh->mesh_mutex);
+
        free(channel);
 }
 
@@ -3376,6 +3418,16 @@ void update_node_status(meshlink_handle_t *mesh, node_t *n) {
        if(mesh->node_status_cb) {
                mesh->node_status_cb(mesh, (meshlink_node_t *)n, n->status.reachable && !n->status.blacklisted);
        }
+
+       if(mesh->node_pmtu_cb) {
+               mesh->node_pmtu_cb(mesh, (meshlink_node_t *)n, n->minmtu);
+       }
+}
+
+void update_node_pmtu(meshlink_handle_t *mesh, node_t *n) {
+       if(mesh->node_pmtu_cb && !n->status.blacklisted) {
+               mesh->node_pmtu_cb(mesh, (meshlink_node_t *)n, n->minmtu);
+       }
 }
 
 void handle_duplicate_node(meshlink_handle_t *mesh, node_t *n) {