diff options
author | Patric Stout <truebrain@openttd.org> | 2021-04-28 10:55:35 +0200 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-07-16 19:50:29 +0200 |
commit | 55eed246b842d372cd32784c1afcc904aef67f65 (patch) | |
tree | ee202c98ed1a2abf177f0502fc04a86edcbb05d3 /src/network/core | |
parent | 695e1493c9cfb81b942213a7a23488b0ccfaae57 (diff) | |
download | openttd-55eed246b842d372cd32784c1afcc904aef67f65.tar.xz |
Codechange: allow Connect() to bind to a local address
Diffstat (limited to 'src/network/core')
-rw-r--r-- | src/network/core/tcp.h | 3 | ||||
-rw-r--r-- | src/network/core/tcp_connect.cpp | 17 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index bbd0bc2a9..b4b4398de 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -98,6 +98,7 @@ private: std::chrono::steady_clock::time_point last_attempt; ///< Time we last tried to connect. std::string connection_string; ///< Current address we are connecting to (before resolving). + NetworkAddress bind_address; ///< Address we're binding to, if any. void Resolve(); void OnResolved(addrinfo *ai); @@ -113,7 +114,7 @@ private: public: TCPConnecter() {}; - TCPConnecter(const std::string &connection_string, uint16 default_port); + TCPConnecter(const std::string &connection_string, uint16 default_port, NetworkAddress bind_address = {}); virtual ~TCPConnecter(); /** diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp index 0e8e2e125..6db2500f3 100644 --- a/src/network/core/tcp_connect.cpp +++ b/src/network/core/tcp_connect.cpp @@ -24,10 +24,13 @@ static std::vector<TCPConnecter *> _tcp_connecters; /** - * Create a new connecter for the given address - * @param connection_string the address to connect to + * Create a new connecter for the given address. + * @param connection_string The address to connect to. + * @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) +TCPConnecter::TCPConnecter(const std::string &connection_string, uint16 default_port, NetworkAddress bind_address) : + bind_address(bind_address) { this->connection_string = NormalizeConnectionString(connection_string, default_port); @@ -96,6 +99,14 @@ void TCPConnecter::Connect(addrinfo *address) return; } + 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()); + closesocket(sock); + return; + } + } + if (!SetNoDelay(sock)) { Debug(net, 1, "Setting TCP_NODELAY failed: {}", NetworkError::GetLast().AsString()); } |