]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Allow the application to provide channel send/receive buffers.
[meshlink] / src / meshlink.c
index 7492c46bda6a0ce7716fc27278b58db108735500..1540df37344ace42cc87c70cc07160401fd914e7 100644 (file)
@@ -391,7 +391,10 @@ static int getifaddrs_in_netns(struct ifaddrs **ifa, int netns) {
 #endif
 
 char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) {
-       (void)mesh;
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
 
        // Determine address of the local interface used for outgoing connections.
        char localaddr[NI_MAXHOST];
@@ -2060,6 +2063,20 @@ void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb)
        pthread_mutex_unlock(&mesh->mutex);
 }
 
+void meshlink_set_blacklisted_cb(struct meshlink_handle *mesh, meshlink_blacklisted_cb_t cb) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       mesh->blacklisted_cb = cb;
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
 static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) {
        meshlink_packethdr_t *hdr;
 
@@ -2360,6 +2377,10 @@ static bool search_node_by_dev_class(const node_t *node, const void *condition)
        return false;
 }
 
+static bool search_node_by_blacklisted(const node_t *node, const void *condition) {
+       return *(bool *)condition == node->status.blacklisted;
+}
+
 static bool search_node_by_submesh(const node_t *node, const void *condition) {
        if(condition == node->submesh) {
                return true;
@@ -2422,6 +2443,15 @@ meshlink_node_t **meshlink_get_all_nodes_by_last_reachable(meshlink_handle_t *me
        return meshlink_get_all_nodes_by_condition(mesh, &range, nodes, nmemb, search_node_by_last_reachable);
 }
 
+meshlink_node_t **meshlink_get_all_nodes_by_blacklisted(meshlink_handle_t *mesh, bool blacklisted, meshlink_node_t **nodes, size_t *nmemb) {
+       if(!mesh || !nmemb) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
+
+       return meshlink_get_all_nodes_by_condition(mesh, &blacklisted, nodes, nmemb, search_node_by_blacklisted);
+}
+
 dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node) {
        if(!mesh || !node) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -2441,6 +2471,28 @@ dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t
        return devclass;
 }
 
+bool meshlink_get_node_blacklisted(meshlink_handle_t *mesh, meshlink_node_t *node) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+       }
+
+       if(!node) {
+               return mesh->default_blacklist;
+       }
+
+       bool blacklisted;
+
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       blacklisted = ((node_t *)node)->status.blacklisted;
+
+       pthread_mutex_unlock(&mesh->mutex);
+
+       return blacklisted;
+}
+
 meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_node_t *node) {
        if(!mesh || !node) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -2603,6 +2655,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;
@@ -3383,6 +3459,10 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) {
         */
        for list_each(connection_t, c, mesh->connections) {
                if(c->node == n) {
+                       if(c->status.active) {
+                               send_error(mesh, c, BLACKLISTED, "blacklisted");
+                       }
+
                        shutdown(c->socket, SHUT_RDWR);
                }
        }
@@ -3406,6 +3486,9 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) {
                mesh->node_status_cb(mesh, (meshlink_node_t *)n, false);
        }
 
+       /* Remove any outstanding invitations */
+       invitation_purge_node(mesh, n->name);
+
        return node_write_config(mesh, n) && config_sync(mesh, "current");
 }
 
@@ -3585,6 +3668,9 @@ bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) {
                return false;
        }
 
+       /* Delete any pending invitations */
+       invitation_purge_node(mesh, n->name);
+
        /* Delete the node struct and any remaining edges referencing this node */
        node_del(mesh, n);
 
@@ -3780,7 +3866,7 @@ static void channel_retransmit(struct utcp_connection *utcp_connection) {
        node_t *n = utcp_connection->utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
 
-       if(n->mtuprobes == 31) {
+       if(n->mtuprobes == 31 && n->mtutimeout.cb) {
                timeout_set(&mesh->loop, &n->mtutimeout, &(struct timespec) {
                        0, 0
                });
@@ -3880,9 +3966,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;
@@ -3972,9 +4055,15 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac
 }
 
 void meshlink_set_channel_sndbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) {
-       (void)mesh;
+       meshlink_set_channel_sndbuf_storage(mesh, channel, NULL, size);
+}
 
-       if(!channel) {
+void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) {
+       meshlink_set_channel_rcvbuf_storage(mesh, channel, NULL, size);
+}
+
+void meshlink_set_channel_sndbuf_storage(meshlink_handle_t *mesh, meshlink_channel_t *channel, void *buf, size_t size) {
+       if(!mesh || !channel) {
                meshlink_errno = MESHLINK_EINVAL;
                return;
        }
@@ -3983,14 +4072,26 @@ void meshlink_set_channel_sndbuf(meshlink_handle_t *mesh, meshlink_channel_t *ch
                abort();
        }
 
-       utcp_set_sndbuf(channel->c, size);
+       utcp_set_sndbuf(channel->c, buf, size);
        pthread_mutex_unlock(&mesh->mutex);
 }
 
-void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) {
-       (void)mesh;
+void meshlink_set_channel_rcvbuf_storage(meshlink_handle_t *mesh, meshlink_channel_t *channel, void *buf, size_t size) {
+       if(!mesh || !channel) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
 
-       if(!channel) {
+       if(pthread_mutex_lock(&mesh->mutex) != 0) {
+               abort();
+       }
+
+       utcp_set_rcvbuf(channel->c, buf, size);
+       pthread_mutex_unlock(&mesh->mutex);
+}
+
+void meshlink_set_channel_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint32_t flags) {
+       if(!mesh || !channel) {
                meshlink_errno = MESHLINK_EINVAL;
                return;
        }
@@ -3999,7 +4100,7 @@ void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *ch
                abort();
        }
 
-       utcp_set_rcvbuf(channel->c, size);
+       utcp_set_flags(channel->c, flags);
        pthread_mutex_unlock(&mesh->mutex);
 }