]> git.meshlink.io Git - meshlink/commitdiff
Fix logic in try_bind().
authorGuus Sliepen <guus@meshlink.io>
Tue, 25 Feb 2020 19:39:48 +0000 (20:39 +0100)
committerGuus Sliepen <guus@meshlink.io>
Tue, 25 Feb 2020 19:39:48 +0000 (20:39 +0100)
Fix the check for successful socket creation. Also make sure we only
return success if we can bind to IPv4 and IPv6, but ignore other
network protocols.

src/meshlink.c

index fbacfd4a4131a813d8f2119a11472303a00f4886..6fd89daded547d5ad2ad6045d55540fc2cfd4a17 100644 (file)
@@ -537,26 +537,33 @@ static bool try_bind(int port) {
                return false;
        }
 
-       //while(ai) {
+       bool success = false;
+
        for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
+               if(!(aip->ai_family == AF_INET || aip->ai_family == AF_INET6)) {
+                       continue;
+               }
+
                int fd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
 
-               if(!fd) {
-                       freeaddrinfo(ai);
-                       return false;
+               if(fd == -1) {
+                       success = false;
+                       break;
                }
 
                int result = bind(fd, aip->ai_addr, aip->ai_addrlen);
                closesocket(fd);
 
                if(result) {
-                       freeaddrinfo(ai);
-                       return false;
+                       success = false;
+                       break;
+               } else {
+                       success = true;
                }
        }
 
        freeaddrinfo(ai);
-       return true;
+       return success;
 }
 
 static int check_port(meshlink_handle_t *mesh) {