summaryrefslogtreecommitdiff
path: root/src/network/core/tcp_connect.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-04-27 11:51:00 +0200
committerPatric Stout <github@truebrain.nl>2021-07-16 19:50:29 +0200
commit8adade26ed0354e5357803cf19ea9839c2eb785c (patch)
tree08c73e20a16ea19ee1545de8df6c4b1095c08707 /src/network/core/tcp_connect.cpp
parent55eed246b842d372cd32784c1afcc904aef67f65 (diff)
downloadopenttd-8adade26ed0354e5357803cf19ea9839c2eb785c.tar.xz
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.
Diffstat (limited to 'src/network/core/tcp_connect.cpp')
-rw-r--r--src/network/core/tcp_connect.cpp12
1 files changed, 10 insertions, 2 deletions
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<TCPConnecter *> _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);