]> git.meshlink.io Git - meshlink/commitdiff
Use bind() to check if a local address is still valid.
authorGuus Sliepen <guus@meshlink.io>
Thu, 6 Feb 2020 20:34:43 +0000 (21:34 +0100)
committerGuus Sliepen <guus@meshlink.io>
Sat, 8 Feb 2020 14:00:39 +0000 (15:00 +0100)
Some platforms don't support getifaddrs(). We use this to check if the
local address of a socket is still available on any network interface.
Instead, try to bind() a new socket to the same address (but port 0) as
existing sockets. If it returns EADDRNOTAVAIL, we know that this address
is no longer valid.

src/net.c

index 95790ac1d287c84ed4f11e8cd19019288b2549b3..304c62683b4e5faa2c50a3f675ad47051258ee57 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -639,11 +639,6 @@ void retry(meshlink_handle_t *mesh) {
                });
        }
 
-#if defined(HAVE_IFADDRS_H) && defined(HAVE_GETIFADDRS) && defined(HAVE_FREEIFADDRS)
-       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) {
@@ -655,12 +650,6 @@ void retry(meshlink_handle_t *mesh) {
                        c->last_ping_time = 0;
                }
 
-#if defined(HAVE_IFADDRS_H) && defined(HAVE_GETIFADDRS) && defined(HAVE_FREEIFADDRS)
-
-               if(!ifa) {
-                       continue;
-               }
-
                sockaddr_t sa;
                socklen_t salen = sizeof(sa);
 
@@ -668,32 +657,33 @@ void retry(meshlink_handle_t *mesh) {
                        continue;
                }
 
-               bool found = false;
+               switch(sa.sa.sa_family) {
+               case AF_INET:
+                       sa.in.sin_port = 0;
+                       break;
 
-               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;
-                       }
+               case AF_INET6:
+                       sa.in6.sin6_port = 0;
+                       break;
+
+               default:
+                       continue;
+               }
+
+               int sock = socket(sa.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
 
+               if(sock != -1) {
+                       continue;
                }
 
-               if(!found) {
+               if(bind(sock, &sa.sa, salen) && errno == EADDRNOTAVAIL) {
                        logger(mesh, MESHLINK_DEBUG, "Local address for connection to %s no longer valid, terminating", c->name);
                        terminate_connection(mesh, c, c->status.active);
                }
 
-#endif
+               closesocket(sock);
        }
 
-#if defined(HAVE_IFADDRS_H) && defined(HAVE_GETIFADDRS) && defined(HAVE_FREEIFADDRS)
-
-       if(ifa) {
-               freeifaddrs(ifa);
-       }
-
-#endif
-
        /* Kick the ping timeout handler */
        timeout_set(&mesh->loop, &mesh->pingtimer, &(struct timeval) {
                0, 0