X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=c34b24c9643f4bde69e2904877502e78c01f2b86;hb=a5f139f0cef76bb2ef6d1bb888869cf17ca97f5d;hp=0fe7343c7ae6a789ab517f2973a6ab5920970c84;hpb=fcd0a7b8c0a823aaa4f9b885a408969b7dc16e00;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 0fe7343c..c34b24c9 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -344,6 +344,52 @@ static bool is_localaddr(sockaddr_t *sa) { } } +#ifdef HAVE_GETIFADDRS +struct getifaddrs_in_netns_params { + struct ifaddrs **ifa; + int netns; +}; + +#ifdef HAVE_SETNS +static void *getifaddrs_in_netns_thread(void *arg) { + struct getifaddrs_in_netns_params *params = arg; + + if(setns(params->netns, CLONE_NEWNET) == -1) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + if(getifaddrs(params->ifa) != 0) { + *params->ifa = NULL; + } + + return NULL; +} +#endif // HAVE_SETNS + +static int getifaddrs_in_netns(struct ifaddrs **ifa, int netns) { + if(netns == -1) { + return getifaddrs(ifa); + } + +#ifdef HAVE_SETNS + struct getifaddrs_in_netns_params params = {ifa, netns}; + pthread_t thr; + + if(pthread_create(&thr, NULL, getifaddrs_in_netns_thread, ¶ms) == 0) { + if(pthread_join(thr, NULL) != 0) { + abort(); + } + } + + return *params.ifa ? 0 : -1; +#else + return -1; +#endif // HAVE_SETNS + +} +#endif + char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) { (void)mesh; @@ -361,12 +407,12 @@ char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) if(!success) { struct ifaddrs *ifa = NULL; - getifaddrs(&ifa); + getifaddrs_in_netns(&ifa, mesh->netns); for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) { sockaddr_t *sa = (sockaddr_t *)ifap->ifa_addr; - if(sa->sa.sa_family != family) { + if(!sa || sa->sa.sa_family != family) { continue; } @@ -783,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; } @@ -1667,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 @@ -2009,6 +2060,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; @@ -2309,6 +2374,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; @@ -2371,6 +2440,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; @@ -2390,6 +2468,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; @@ -2532,11 +2632,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(); @@ -2556,6 +2652,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; @@ -3336,6 +3456,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); } } @@ -3359,6 +3483,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"); } @@ -3538,6 +3665,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); @@ -3575,7 +3705,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. */ @@ -3828,9 +3963,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; @@ -3880,6 +4012,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;