X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=src%2Fmeshlink.c;h=0843cc0c4826d0d3a800904f28e4c1c51704c528;hp=d54fe30b1ae8f49500214036709f983b6a015ba3;hb=d1b4e07e76555ecf47faf36f38a62878969be6d7;hpb=c8af33c1b1b25dc5e9b42892e09dbb65fd5bd766 diff --git a/src/meshlink.c b/src/meshlink.c index d54fe30b..0843cc0c 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