]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Fix cornercases closing channels.
[meshlink] / src / meshlink.c
index 340b6041cfee81d7f7ef641ec55794dae6c1e5f0..a8ed1fad3d20a0d39db9ad5f9c7a67763d904e6f 100644 (file)
@@ -829,7 +829,7 @@ static bool finalize_join(join_state_t *state, const void *buf, uint16_t len) {
        }
 
        /* Ensure the configuration directory metadata is on disk */
-       if(!config_sync(mesh, "current") || !sync_path(mesh->confbase)) {
+       if(!config_sync(mesh, "current") || (mesh->confbase && !sync_path(mesh->confbase))) {
                return false;
        }
 
@@ -1713,6 +1713,11 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                return false;
        }
 
+       // Reset node connection timers
+       for splay_each(node_t, n, mesh->nodes) {
+               n->last_connect_try = 0;
+       }
+
        // TODO: open listening sockets first
 
        //Check that a valid name is set
@@ -2578,11 +2583,7 @@ bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *no
 
        char *canonical_address;
 
-       if(port) {
-               xasprintf(&canonical_address, "%s %s", address, port);
-       } else {
-               canonical_address = xstrdup(address);
-       }
+       xasprintf(&canonical_address, "%s %s", address, port ? port : mesh->myport);
 
        if(pthread_mutex_lock(&mesh->mutex) != 0) {
                abort();
@@ -2602,6 +2603,30 @@ bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *no
        return config_sync(mesh, "current");
 }
 
+bool meshlink_clear_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node) {
+       if(!mesh || !node) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return false;
+       }
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       node_t *n = (node_t *)node;
+       free(n->canonical_address);
+       n->canonical_address = NULL;
+
+       if(!node_write_config(mesh, n)) {
+               pthread_mutex_unlock(&mesh->mutex);
+               return false;
+       }
+
+       pthread_mutex_unlock(&mesh->mutex);
+
+       return config_sync(mesh, "current");
+}
+
 bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *address, const char *port) {
        if(!mesh || !address) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -3621,7 +3646,12 @@ static bool channel_pre_accept(struct utcp *utcp, uint16_t port) {
        (void)port;
        node_t *n = utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
-       return mesh->channel_accept_cb;
+
+       if(mesh->channel_accept_cb && mesh->channel_listen_cb) {
+               return mesh->channel_listen_cb(mesh, (meshlink_node_t *)n, port);
+       } else {
+               return mesh->channel_accept_cb;
+       }
 }
 
 /* Finish one AIO buffer, return true if the channel is still open. */
@@ -3874,9 +3904,6 @@ static void channel_poll(struct utcp_connection *connection, size_t len) {
                }
 
                if(sent != (ssize_t)todo) {
-                       /* We should never get a partial send at this point */
-                       assert(sent <= 0);
-
                        /* Sending failed, abort all outstanding AIO buffers and send a poll callback. */
                        if(!aio_abort(mesh, channel, &channel->aio_send)) {
                                return;
@@ -3926,6 +3953,21 @@ void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *c
        pthread_mutex_unlock(&mesh->mutex);
 }
 
+void meshlink_set_channel_listen_cb(meshlink_handle_t *mesh, meshlink_channel_listen_cb_t cb) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       mesh->channel_listen_cb = cb;
+
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
 void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) {
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;