X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet.c;h=ddfd2cf09aa828abc5a861742e60172e33b8fddb;hb=64720408e45d283eaaa5091957d949fab43087ef;hp=a1cccbdf8e875471064f7d0b1367677814e75afe;hpb=9e588a7b6cb4fff70f223bab1ba3120acff1f18b;p=meshlink diff --git a/src/net.c b/src/net.c index a1cccbdf..ddfd2cf0 100644 --- a/src/net.c +++ b/src/net.c @@ -585,6 +585,10 @@ static void periodic_handler(event_loop_t *loop, void *data) { terminate_connection(mesh, disconnect_from->connection, disconnect_from->connection->status.active); } + // reduce timeout if we don't have enough connections + outgoings + if(cur_connects + mesh->outgoings->count < 3) { + timeout = 1; + } // done! @@ -621,13 +625,61 @@ void retry(meshlink_handle_t *mesh) { }); } - /* Check for outgoing connections that are in progress, and reset their ping timers */ +#ifdef HAVE_IFADDRS_H + struct ifaddrs *ifa = NULL; + getifaddrs(&ifa); +#endif + + /* For active connections, check if their addresses are still valid. + * If yes, reset their ping timers, otherwise terminate them. */ for list_each(connection_t, c, mesh->connections) { - if(c->outgoing && !c->node) { + if(!c->status.active) { + continue; + } + + if(!c->status.pinged) { c->last_ping_time = 0; } + +#ifdef HAVE_IFADDRS_H + + if(!ifa) { + continue; + } + + sockaddr_t sa; + socklen_t salen = sizeof(sa); + + if(getsockname(c->socket, &sa.sa, &salen)) { + continue; + } + + bool found = false; + + for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) { + if(ifap->ifa_addr && !sockaddrcmp_noport(&sa, (sockaddr_t *)ifap->ifa_addr)) { + found = true; + break; + } + + } + + if(!found) { + logger(mesh, MESHLINK_DEBUG, "Local address for connection to %s no longer valid, terminating", c->name); + terminate_connection(mesh, c, c->status.active); + } + +#endif + } + +#ifdef HAVE_IFADDRS_H + + if(ifa) { + freeifaddrs(ifa); } +#endif + /* Kick the ping timeout handler */ timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval) { 0, 0