Changeset - r25406:017a9f8d3ec9
[Not reviewed]
master
0 2 0
Patric Stout - 3 years ago 2021-05-08 09:57:41
truebrain@openttd.org
Fix f7e390bd: getpeername() doesn't work on closed sockets (#9213)
2 files changed with 13 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/network/core/tcp.h
Show inline comments
 
@@ -15,8 +15,9 @@
 
#include "address.h"
 
#include "packet.h"
 

	
 
#include <atomic>
 
#include <chrono>
 
#include <atomic>
 
#include <map>
 

	
 
/** The states of sending the packets. */
 
enum SendPacketsState {
 
@@ -66,6 +67,7 @@ class TCPConnecter {
 
private:
 
	addrinfo *ai = nullptr;                             ///< getaddrinfo() allocated linked-list of resolved addresses.
 
	std::vector<addrinfo *> addresses;                  ///< Addresses we can connect to.
 
	std::map<SOCKET, NetworkAddress> sock_to_address;   ///< Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
 
	size_t current_address = 0;                         ///< Current index in addresses we are trying.
 

	
 
	std::vector<SOCKET> sockets;                        ///< Pending connect() attempts.
src/network/core/tcp_connect.cpp
Show inline comments
 
@@ -40,8 +40,10 @@ TCPConnecter::TCPConnecter(const std::st
 
TCPConnecter::~TCPConnecter()
 
{
 
	for (const auto &socket : this->sockets) {
 
		close(socket);
 
		closesocket(socket);
 
	}
 
	this->sockets.clear();
 
	this->sock_to_address.clear();
 

	
 
	freeaddrinfo(this->ai);
 
}
 
@@ -72,6 +74,7 @@ void TCPConnecter::Connect(addrinfo *add
 
		return;
 
	}
 

	
 
	this->sock_to_address[sock] = network_address;
 
	this->sockets.push_back(sock);
 
}
 

	
 
@@ -245,6 +248,9 @@ bool TCPConnecter::CheckActivity()
 
		for (const auto &socket : this->sockets) {
 
			closesocket(socket);
 
		}
 
		this->sockets.clear();
 
		this->sock_to_address.clear();
 

	
 
		this->OnFailure();
 
		return true;
 
	}
 
@@ -253,8 +259,9 @@ bool TCPConnecter::CheckActivity()
 
	for (auto it = this->sockets.begin(); it != this->sockets.end(); /* nothing */) {
 
		NetworkError socket_error = GetSocketError(*it);
 
		if (socket_error.HasError()) {
 
			DEBUG(net, 1, "Could not connect to %s: %s", NetworkAddress::GetPeerName(*it).c_str(), socket_error.AsString());
 
			DEBUG(net, 1, "Could not connect to %s: %s", this->sock_to_address[*it].GetAddressAsString().c_str(), socket_error.AsString());
 
			closesocket(*it);
 
			this->sock_to_address.erase(*it);
 
			it = this->sockets.erase(it);
 
		} else {
 
			it++;
 
@@ -280,6 +287,7 @@ bool TCPConnecter::CheckActivity()
 
		} else {
 
			closesocket(*it);
 
		}
 
		this->sock_to_address.erase(*it);
 
		it = this->sockets.erase(it);
 
	}
 
	assert(connected_socket != INVALID_SOCKET);
0 comments (0 inline, 0 general)