]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Have try_bind() reuse the setup_*_listen_socket() functions.
[meshlink] / src / meshlink.c
index b9cc18771d22ef5f2c01eba18b38937f3619a4e8..9f3b6ecd2094a89c92e68c0e9e62537b55a04518 100644 (file)
@@ -442,11 +442,11 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) {
        if(mesh->invitation_addresses) {
                for list_each(char, combo, mesh->invitation_addresses) {
                        hostname[n] = xstrdup(combo);
-                       char *colon = strchr(hostname[n], ':');
+                       char *slash = strrchr(hostname[n], '/');
 
-                       if(colon) {
-                               *colon = 0;
-                               port[n] = xstrdup(colon + 1);
+                       if(slash) {
+                               *slash = 0;
+                               port[n] = xstrdup(slash + 1);
                        }
 
                        n++;
@@ -489,7 +489,7 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) {
                }
        }
 
-       remove_duplicate_hostnames(hostname, port, 4);
+       remove_duplicate_hostnames(hostname, port, n);
 
        // Resolve the hostnames
        for(int i = 0; i < n; i++) {
@@ -510,8 +510,10 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) {
                        continue;
                }
 
-               // Remember the address
-               node_add_recent_address(mesh, mesh->self, (sockaddr_t *)ai_in->ai_addr);
+               // Remember the address(es)
+               for(struct addrinfo *aip = ai_in; aip; aip = aip->ai_next) {
+                       node_add_recent_address(mesh, mesh->self, (sockaddr_t *)aip->ai_addr);
+               }
 
                if(flags & MESHLINK_INVITE_NUMERIC) {
                        // We don't need to do any further conversion
@@ -575,7 +577,7 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) {
        return hostport;
 }
 
-static bool try_bind(int port) {
+static bool try_bind(meshlink_handle_t *mesh, int port) {
        struct addrinfo *ai = NULL;
        struct addrinfo hint = {
                .ai_flags = AI_PASSIVE,
@@ -596,16 +598,9 @@ static bool try_bind(int port) {
        for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
                /* Try to bind to TCP. */
 
-               int tcp_fd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
+               int tcp_fd = setup_tcp_listen_socket(mesh, aip);
 
                if(tcp_fd == -1) {
-                       continue;
-               }
-
-               int result = bind(tcp_fd, aip->ai_addr, aip->ai_addrlen);
-               closesocket(tcp_fd);
-
-               if(result) {
                        if(errno == EADDRINUSE) {
                                /* If this port is in use for any address family, avoid it. */
                                success = false;
@@ -617,21 +612,16 @@ static bool try_bind(int port) {
 
                /* If TCP worked, then we require that UDP works as well. */
 
-               int udp_fd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
+               int udp_fd = setup_udp_listen_socket(mesh, aip);
 
                if(udp_fd == -1) {
+                       closesocket(tcp_fd);
                        success = false;
                        break;
                }
 
-               result = bind(udp_fd, aip->ai_addr, aip->ai_addrlen);
+               closesocket(tcp_fd);
                closesocket(udp_fd);
-
-               if(result) {
-                       success = false;
-                       break;
-               }
-
                success = true;
        }
 
@@ -643,7 +633,7 @@ int check_port(meshlink_handle_t *mesh) {
        for(int i = 0; i < 1000; i++) {
                int port = 0x1000 + prng(mesh, 0x8000);
 
-               if(try_bind(port)) {
+               if(try_bind(mesh, port)) {
                        free(mesh->myport);
                        xasprintf(&mesh->myport, "%d", port);
                        return port;
@@ -2467,11 +2457,7 @@ bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *a
        char *combo;
 
        if(port) {
-               if(strchr(address, ':')) {
-                       xasprintf(&combo, "[%s]:%s", address, port);
-               } else {
-                       xasprintf(&combo, "%s:%s", address, port);
-               }
+               xasprintf(&combo, "%s/%s", address, port);
        } else {
                combo = xstrdup(address);
        }
@@ -2556,7 +2542,7 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) {
                return true;
        }
 
-       if(!try_bind(port)) {
+       if(!try_bind(mesh, port)) {
                meshlink_errno = MESHLINK_ENETWORK;
                return false;
        }