X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=e1cf7979631e0f94b9ebf3f8f820afeb5a66197d;hb=57f6341e91373198ad2a4099015f2988795d8a3f;hp=6e7cda60a3f71fd1a94424c754af48a1288ae0c5;hpb=32d723c40d6fa959779f35973411185a320529d1;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 6e7cda60..e1cf7979 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] = xstrdup(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; @@ -1171,7 +1161,6 @@ static void *setup_network_in_netns_thread(void *arg) { } bool success = setup_network(mesh); - add_local_addresses(mesh); return success ? arg : NULL; } #endif // HAVE_SETNS @@ -1544,7 +1533,6 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { #endif // HAVE_SETNS } else { success = setup_network(mesh); - add_local_addresses(mesh); } if(!success) { @@ -2020,6 +2008,8 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const return false; } + logger(mesh, MESHLINK_DEBUG, "Adding packet of %zu bytes to packet queue", len); + // Notify event loop signal_trigger(&mesh->loop, &mesh->datafromapp); @@ -2029,17 +2019,16 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void meshlink_send_from_queue(event_loop_t *loop, void *data) { (void)loop; meshlink_handle_t *mesh = data; - vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue); - if(!packet) { - return; - } + logger(mesh, MESHLINK_DEBUG, "Flushing the packet queue"); - mesh->self->in_packets++; - mesh->self->in_bytes += packet->len; - route(mesh, mesh->self, packet); - - free(packet); + for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) { + logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len); + mesh->self->in_packets++; + mesh->self->in_bytes += packet->len; + route(mesh, mesh->self, packet); + free(packet); + } } ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) { @@ -2467,11 +2456,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 +2541,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; }