From 8adade26ed0354e5357803cf19ea9839c2eb785c Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Tue, 27 Apr 2021 11:51:00 +0200 Subject: Feature: allow the use of STUN to connect client and server together This method doesn't require port-forwarding to be used, and works for most common NAT routers in home setups. But, for sure it doesn't work for all setups, and not everyone will be able to use this. --- src/network/core/tcp_connect.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/network/core/tcp_connect.cpp') diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index 6db2500f3..29e9048d9 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -29,8 +29,9 @@ static std::vector _tcp_connecters; * @param default_port If not indicated in connection_string, what port to use. * @param bind_address The local bind address to use. Defaults to letting the OS find one. */ -TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_port, NetworkAddress bind_address) : - bind_address(bind_address) +TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_port, NetworkAddress bind_address, int family) : + bind_address(bind_address), + family(family) { this->connection_string = NormalizeConnectionString(connection_string, default_port); @@ -99,6 +100,10 @@ void TCPConnecter::Connect(addrinfo *address) return; } + if (!SetReusePort(sock)) { + Debug(net, 0, "Setting reuse-port mode failed: {}", NetworkError::GetLast().AsString()); + } + if (this->bind_address.GetPort() > 0) { if (bind(sock, (const sockaddr *)this->bind_address.GetAddress(), this->bind_address.GetAddressLength()) != 0) { Debug(net, 1, "Could not bind socket on {}: {}", this->bind_address.GetAddressAsString(), NetworkError::GetLast().AsString()); @@ -170,6 +175,9 @@ void TCPConnecter::OnResolved(addrinfo *ai) /* Convert the addrinfo into NetworkAddresses. */ for (addrinfo *runp = ai; runp != nullptr; runp = runp->ai_next) { + /* Skip entries if the family is set and it is not matching. */ + if (this->family != AF_UNSPEC && this->family != runp->ai_family) continue; + if (resort) { if (runp->ai_family == AF_INET6) { addresses_ipv6.emplace_back(runp); -- cgit v1.2.3-54-g00ecf