X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=f949c2c7da8df9acd9999418c79cf4a736635ada;hb=e40d5bf3a0e030105334046319f377efbf3f06c4;hp=d54fe30b1ae8f49500214036709f983b6a015ba3;hpb=c8af33c1b1b25dc5e9b42892e09dbb65fd5bd766;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index d54fe30b..f949c2c7 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,7 +407,7 @@ 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; @@ -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 @@ -3575,7 +3626,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. */ @@ -3880,6 +3936,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;