X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=5b01ad2c1fda7b12abb823baa9c80da391177fb7;hb=3fe7713c4f9d16f192271bc4fa8a2ffda6bb28fd;hp=c4613a7019e6182929804a30a7963dc0e9602557;hpb=2bf1a40fa455564a4aac52349d70e9fedb163721;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index c4613a70..5b01ad2c 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -37,6 +37,7 @@ #include "ed25519/sha512.h" #include "discovery.h" #include "devtools.h" +#include "graph.h" #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 @@ -286,6 +287,21 @@ char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int fami return hostname; } +static bool is_localaddr(sockaddr_t *sa) { + switch(sa->sa.sa_family) { + case AF_INET: + return *(uint8_t *)(&sa->in.sin_addr.s_addr) == 127; + + case AF_INET6: { + uint16_t first = sa->in6.sin6_addr.s6_addr[0] << 8 | sa->in6.sin6_addr.s6_addr[1]; + return first == 0 || (first & 0xffc0) == 0xfe80; + } + + default: + return false; + } +} + char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) { (void)mesh; @@ -299,6 +315,34 @@ char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) success = getlocaladdrname("2606:2800:220:1:248:1893:25c8:1946", localaddr, sizeof(localaddr), mesh->netns); } +#ifdef HAVE_GETIFADDRS + + if(!success) { + struct ifaddrs *ifa = NULL; + getifaddrs(&ifa); + + for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) { + sockaddr_t *sa = (sockaddr_t *)ifap->ifa_addr; + + if(sa->sa.sa_family != family) { + continue; + } + + if(is_localaddr(sa)) { + continue; + } + + if(!getnameinfo(&sa->sa, SALEN(sa->sa), localaddr, sizeof(localaddr), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) { + success = true; + break; + } + } + + freeifaddrs(ifa); + } + +#endif + if(!success) { meshlink_errno = MESHLINK_ENETWORK; return NULL; @@ -667,6 +711,10 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len } } + /* Clear the reachability times, since we ourself have never seen these nodes yet */ + n->last_reachable = 0; + n->last_unreachable = 0; + if(!node_write_config(mesh, n)) { free_node(n); return false; @@ -719,7 +767,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint return finalize_join(mesh, msg, len); case 1: - logger(mesh, MESHLINK_DEBUG, "Invitation succesfully accepted.\n"); + logger(mesh, MESHLINK_DEBUG, "Invitation successfully accepted.\n"); shutdown(mesh->sock, SHUT_RDWR); mesh->success = true; break; @@ -1527,8 +1575,9 @@ bool meshlink_start(meshlink_handle_t *mesh) { pthread_cond_wait(&mesh->cond, &mesh->mutex); mesh->threadstarted = true; - mesh->self->last_reachable = time(NULL); - mesh->self->status.dirty = true; + + // Ensure we are considered reachable + graph(mesh); pthread_mutex_unlock(&mesh->mutex); return true; @@ -1543,11 +1592,6 @@ void meshlink_stop(meshlink_handle_t *mesh) { pthread_mutex_lock(&mesh->mutex); logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n"); - if(mesh->self) { - mesh->self->last_unreachable = time(NULL); - mesh->self->status.dirty = true; - } - // Shut down the main thread event_loop_stop(&mesh->loop); @@ -1587,6 +1631,11 @@ void meshlink_stop(meshlink_handle_t *mesh) { exit_outgoings(mesh); + // Ensure we are considered unreachable + if(mesh->nodes) { + graph(mesh); + } + // Try to write out any changed node config files, ignore errors at this point. if(mesh->nodes) { for splay_each(node_t, n, mesh->nodes) { @@ -2143,6 +2192,31 @@ meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_ return s; } +bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlink_node *node, time_t *last_reachable, time_t *last_unreachable) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + node_t *n = (node_t *)node; + bool reachable; + + pthread_mutex_lock(&mesh->mutex); + reachable = n->status.reachable && !n->status.blacklisted; + + if(last_reachable) { + *last_reachable = n->last_reachable; + } + + if(last_unreachable) { + *last_unreachable = n->last_unreachable; + } + + pthread_mutex_unlock(&mesh->mutex); + + return reachable; +} + bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) { if(!mesh || !data || !len || !signature || !siglen) { meshlink_errno = MESHLINK_EINVAL; @@ -2401,7 +2475,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c } // Ensure no other nodes know about this name - if(meshlink_get_node(mesh, name)) { + if(lookup_node(mesh, name)) { logger(mesh, MESHLINK_ERROR, "A node with name %s is already known!\n", name); meshlink_errno = MESHLINK_EEXIST; pthread_mutex_unlock(&mesh->mutex); @@ -2427,7 +2501,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c // If we changed our own host config file, write it out now if(mesh->self->status.dirty) { if(!node_write_config(mesh, mesh->self)) { - logger(mesh, MESHLINK_ERROR, "Could not write our own host conifg file!\n"); + logger(mesh, MESHLINK_ERROR, "Could not write our own host config file!\n"); pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2892,7 +2966,11 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { break; } - if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) { + /* Clear the reachability times, since we ourself have never seen these nodes yet */ + n->last_reachable = 0; + n->last_unreachable = 0; + + if(!node_write_config(mesh, n)) { free_node(n); return false; } @@ -2948,6 +3026,10 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) { n->mtuprobes = 0; n->status.udp_confirmed = false; + if(n->status.reachable) { + n->last_unreachable = mesh->loop.now.tv_sec; + } + /* Graph updates will suppress status updates for blacklisted nodes, so we need to * manually call the status callback if necessary. */ @@ -3019,6 +3101,7 @@ static bool whitelist(meshlink_handle_t *mesh, node_t *n) { n->status.blacklisted = false; if(n->status.reachable) { + n->last_reachable = mesh->loop.now.tv_sec; update_node_status(mesh, n); } @@ -3807,6 +3890,22 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla pthread_mutex_unlock(&mesh->mutex); } +void meshlink_set_dev_class_fast_retry_period(meshlink_handle_t *mesh, dev_class_t devclass, int fast_retry_period) { + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) { + meshlink_errno = EINVAL; + return; + } + + if(fast_retry_period < 0) { + meshlink_errno = EINVAL; + return; + } + + pthread_mutex_lock(&mesh->mutex); + mesh->dev_class_traits[devclass].fast_retry_period = fast_retry_period; + pthread_mutex_unlock(&mesh->mutex); +} + void handle_network_change(meshlink_handle_t *mesh, bool online) { (void)online;