X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=src%2Fmeshlink.c;h=9f3b6ecd2094a89c92e68c0e9e62537b55a04518;hp=9f9b771dc5589598df24ff18a7a06048199b161a;hb=b70b090a28ff4cbdce29a3ad030f7d51ce9079b2;hpb=db8e6e4221c974314fd8b05d79dbc94152f01108 diff --git a/src/meshlink.c b/src/meshlink.c index 9f9b771d..9f3b6ecd 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -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] = colon + 1; + if(slash) { + *slash = 0; + port[n] = xstrdup(slash + 1); } n++; @@ -482,17 +482,17 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) { } } - for(int i = 0; i < 4; i++) { + for(int i = 0; i < n; i++) { // Ensure we always have a port number if(hostname[i] && !port[i]) { port[i] = xstrdup(mesh->myport); } } - remove_duplicate_hostnames(hostname, port, 4); + remove_duplicate_hostnames(hostname, port, n); // Resolve the hostnames - for(int i = 0; i < 4; i++) { + for(int i = 0; i < n; i++) { if(!hostname[i]) { continue; } @@ -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 @@ -554,10 +556,10 @@ static char *get_my_hostname(meshlink_handle_t *mesh, uint32_t flags) { } // Remove duplicates again, since IPv4 and IPv6 addresses might map to the same hostname - remove_duplicate_hostnames(hostname, port, 4); + remove_duplicate_hostnames(hostname, port, n); // Concatenate all unique address to the hostport string - for(int i = 0; i < 4; i++) { + for(int i = 0; i < n; i++) { if(!hostname[i]) { continue; } @@ -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; }