]> git.meshlink.io Git - meshlink/blobdiff - src/net.c
Add support for AIO using filedescriptors.
[meshlink] / src / net.c
index a42d933bef822a375fea4c5b4e823d4f6553f683..1df0be0030b257f800ed23e12d9c89e757e0a175 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -393,7 +393,6 @@ static void periodic_handler(event_loop_t *loop, void *data) {
                logger(mesh, MESHLINK_DEBUG, "* min_connects = %d", min_connects);
                logger(mesh, MESHLINK_DEBUG, "* max_connects = %d", max_connects);
 
-
                // find the best one for initial connect
 
                if(cur_connects < min_connects) {
@@ -402,7 +401,7 @@ static void periodic_handler(event_loop_t *loop, void *data) {
                        for splay_each(node_t, n, mesh->nodes) {
                                logger(mesh, MESHLINK_DEBUG, "* %s->devclass = %d", n->name, n->devclass);
 
-                               if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) {
+                               if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
                                        splay_insert(nodes, n);
                                }
                        }
@@ -436,7 +435,7 @@ static void periodic_handler(event_loop_t *loop, void *data) {
                                        splay_tree_t *nodes = splay_alloc_tree(node_compare_lsc_desc, NULL);
 
                                        for splay_each(node_t, n, mesh->nodes) {
-                                               if(n != mesh->self && n->devclass == devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) {
+                                               if(n != mesh->self && n->devclass == devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
                                                        splay_insert(nodes, n);
                                                }
                                        }
@@ -467,7 +466,7 @@ static void periodic_handler(event_loop_t *loop, void *data) {
                        splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
 
                        for splay_each(node_t, n, mesh->nodes) {
-                               if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) {
+                               if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
                                        splay_insert(nodes, n);
                                }
                        }
@@ -486,7 +485,7 @@ static void periodic_handler(event_loop_t *loop, void *data) {
                // perform connect
 
                if(connect_to && !connect_to->connection) {
-                       connect_to->last_connect_try = time(NULL);
+                       connect_to->last_connect_try = mesh->loop.now.tv_sec;
                        logger(mesh, MESHLINK_DEBUG, "Autoconnect trying to connect to %s", connect_to->name);
 
                        /* check if there is already a connection attempt to this node */
@@ -625,13 +624,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