Changeset - r25924:31e3cdb1821b
[Not reviewed]
master
0 1 0
Patric Stout - 3 years ago 2021-08-23 17:38:02
truebrain@openttd.org
Fix #9490: [Network] a full server couldn't be queried either (#9508)

You can now still query a full server, as long as the maximum
amount of allowed connections isn't reached. This means that as
long as there are not 255 clients connected to a server, you can
always connect to query.
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/network/network_server.cpp
Show inline comments
 
@@ -212,193 +212,193 @@ ServerNetworkGameSocketHandler::ServerNe
 
	this->receive_limit = _settings_client.network.bytes_per_frame_burst;
 

	
 
	/* The Socket and Info pools need to be the same in size. After all,
 
	 * each Socket will be associated with at most one Info object. As
 
	 * such if the Socket was allocated the Info object can as well. */
 
	static_assert(NetworkClientSocketPool::MAX_SIZE == NetworkClientInfoPool::MAX_SIZE);
 
}
 

	
 
/**
 
 * Clear everything related to this client.
 
 */
 
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
 
{
 
	if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
 
	OrderBackup::ResetUser(this->client_id);
 

	
 
	if (this->savegame != nullptr) {
 
		this->savegame->Destroy();
 
		this->savegame = nullptr;
 
	}
 
}
 

	
 
Packet *ServerNetworkGameSocketHandler::ReceivePacket()
 
{
 
	/* Only allow receiving when we have some buffer free; this value
 
	 * can go negative, but eventually it will become positive again. */
 
	if (this->receive_limit <= 0) return nullptr;
 

	
 
	/* We can receive a packet, so try that and if needed account for
 
	 * the amount of received data. */
 
	Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
 
	if (p != nullptr) this->receive_limit -= p->Size();
 
	return p;
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
 
{
 
	assert(status != NETWORK_RECV_STATUS_OKAY);
 
	/*
 
	 * Sending a message just before leaving the game calls cs->SendPackets.
 
	 * This might invoke this function, which means that when we close the
 
	 * connection after cs->SendPackets we will close an already closed
 
	 * connection. This handles that case gracefully without having to make
 
	 * that code any more complex or more aware of the validity of the socket.
 
	 */
 
	if (this->sock == INVALID_SOCKET) return status;
 

	
 
	if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
 
		/* We did not receive a leave message from this client... */
 
		std::string client_name = this->GetClientName();
 

	
 
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
 

	
 
		/* Inform other clients of this... strange leaving ;) */
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
 
				new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
 
			}
 
		}
 
	}
 

	
 
	/* If we were transfering a map to this client, stop the savegame creation
 
	 * process and queue the next client to receive the map. */
 
	if (this->status == STATUS_MAP) {
 
		/* Ensure the saving of the game is stopped too. */
 
		this->savegame->Destroy();
 
		this->savegame = nullptr;
 

	
 
		this->CheckNextClientToSendMap(this);
 
	}
 

	
 
	NetworkAdminClientError(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
 
	Debug(net, 3, "Closed client connection {}", this->client_id);
 

	
 
	/* We just lost one client :( */
 
	if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--;
 
	extern byte _network_clients_connected;
 
	_network_clients_connected--;
 

	
 
	this->SendPackets(true);
 

	
 
	delete this->GetInfo();
 
	delete this;
 

	
 
	InvalidateWindowData(WC_CLIENT_LIST, 0);
 

	
 
	return status;
 
}
 

	
 
/**
 
 * Whether an connection is allowed or not at this moment.
 
 * @return true if the connection is allowed.
 
 */
 
/* static */ bool ServerNetworkGameSocketHandler::AllowConnection()
 
{
 
	extern byte _network_clients_connected;
 
	bool accept = _network_clients_connected < MAX_CLIENTS && _network_game_info.clients_on < _settings_client.network.max_clients;
 
	bool accept = _network_clients_connected < MAX_CLIENTS;
 

	
 
	/* We can't go over the MAX_CLIENTS limit here. However, the
 
	 * pool must have place for all clients and ourself. */
 
	static_assert(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENTS + 1);
 
	assert(!accept || ServerNetworkGameSocketHandler::CanAllocateItem());
 
	return accept;
 
}
 

	
 
/** Send the packets for the server sockets. */
 
/* static */ void ServerNetworkGameSocketHandler::Send()
 
{
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->writable) {
 
			if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) {
 
				/* This client is in the middle of a map-send, call the function for that */
 
				cs->SendMap();
 
			}
 
		}
 
	}
 
}
 

	
 
static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
 

	
 
/***********
 
 * Sending functions
 
 *   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientSocket *cs
 
 ************/
 

	
 
/**
 
 * Send the client information about a client.
 
 * @param ci The client to send information about.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientInfo *ci)
 
{
 
	if (ci->client_id != INVALID_CLIENT_ID) {
 
		Packet *p = new Packet(PACKET_SERVER_CLIENT_INFO);
 
		p->Send_uint32(ci->client_id);
 
		p->Send_uint8 (ci->client_playas);
 
		p->Send_string(ci->client_name);
 

	
 
		this->SendPacket(p);
 
	}
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send the client information about the server. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_GAME_INFO, TCP_MTU);
 
	SerializeNetworkGameInfo(p, GetCurrentNetworkServerGameInfo());
 

	
 
	this->SendPacket(p);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send an error to the client, and close its connection.
 
 * @param error The error to disconnect for.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_ERROR);
 

	
 
	p->Send_uint8(error);
 
	if (!reason.empty()) p->Send_string(reason);
 
	this->SendPacket(p);
 

	
 
	StringID strid = GetNetworkErrorMsg(error);
 

	
 
	/* Only send when the current client was in game */
 
	if (this->status > STATUS_AUTHORIZED) {
 
		std::string client_name = this->GetClientName();
 

	
 
		Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
 

	
 
		if (error == NETWORK_ERROR_KICKED && !reason.empty()) {
 
			NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
 
		} else {
 
			NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
 
		}
 

	
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
 
				/* Some errors we filter to a more general error. Clients don't have to know the real
 
				 *  reason a joining failed. */
 
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
 
					error = NETWORK_ERROR_ILLEGAL_PACKET;
 
				}
 
				new_cs->SendErrorQuit(this->client_id, error);
 
			}
 
		}
 

	
 
		NetworkAdminClientError(this->client_id, error);
 
	} else {
 
@@ -712,192 +712,197 @@ NetworkRecvStatus ServerNetworkGameSocke
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGame()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_NEWGAME);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send the result of a console action.
 
 * @param colour The colour of the result.
 
 * @param command The command that was executed.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendRConResult(uint16 colour, const std::string &command)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_RCON);
 

	
 
	p->Send_uint16(colour);
 
	p->Send_string(command);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Tell that a client moved to another company.
 
 * @param client_id The client that moved.
 
 * @param company_id The company the client moved to.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendMove(ClientID client_id, CompanyID company_id)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_MOVE);
 

	
 
	p->Send_uint32(client_id);
 
	p->Send_uint8(company_id);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send an update about the company password states. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyUpdate()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_COMPANY_UPDATE);
 

	
 
	p->Send_uint16(_network_company_passworded);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send an update about the max company/spectator counts. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendConfigUpdate()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_CONFIG_UPDATE);
 

	
 
	p->Send_uint8(_settings_client.network.max_companies);
 
	p->Send_string(_settings_client.network.server_name);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/***********
 
 * Receiving functions
 
 *   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientSocket *cs, Packet *p
 
 ************/
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO(Packet *p)
 
{
 
	return this->SendGameInfo();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED(Packet *p)
 
{
 
	if (this->status != STATUS_NEWGRFS_CHECK) {
 
		/* Illegal call, return error and ignore the packet */
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	NetworkClientInfo *ci = this->GetInfo();
 

	
 
	/* We now want a password from the client else we do not allow them in! */
 
	if (!_settings_client.network.server_password.empty()) {
 
		return this->SendNeedGamePassword();
 
	}
 

	
 
	if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) {
 
		return this->SendNeedCompanyPassword();
 
	}
 

	
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
 
{
 
	if (this->status != STATUS_INACTIVE) {
 
		/* Illegal call, return error and ignore the packet */
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	if (_network_game_info.clients_on >= _settings_client.network.max_clients) {
 
		/* Turns out we are full. Inform the user about this. */
 
		return this->SendError(NETWORK_ERROR_FULL);
 
	}
 

	
 
	std::string client_revision = p->Recv_string(NETWORK_REVISION_LENGTH);
 
	uint32 newgrf_version = p->Recv_uint32();
 

	
 
	/* Check if the client has revision control enabled */
 
	if (!IsNetworkCompatibleVersion(client_revision) || _openttd_newgrf_version != newgrf_version) {
 
		/* Different revisions!! */
 
		return this->SendError(NETWORK_ERROR_WRONG_REVISION);
 
	}
 

	
 
	std::string client_name = p->Recv_string(NETWORK_CLIENT_NAME_LENGTH);
 
	CompanyID playas = (Owner)p->Recv_uint8();
 

	
 
	if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
 

	
 
	/* join another company does not affect these values */
 
	switch (playas) {
 
		case COMPANY_NEW_COMPANY: // New company
 
			if (Company::GetNumItems() >= _settings_client.network.max_companies) {
 
				return this->SendError(NETWORK_ERROR_FULL);
 
			}
 
			break;
 
		case COMPANY_SPECTATOR: // Spectator
 
			break;
 
		default: // Join another company (companies 1-8 (index 0-7))
 
			if (!Company::IsValidHumanID(playas)) {
 
				return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
 
			}
 
			break;
 
	}
 

	
 
	if (!NetworkIsValidClientName(client_name)) {
 
		/* An invalid client name was given. However, the client ensures the name
 
		 * is valid before it is sent over the network, so something went horribly
 
		 * wrong. This is probably someone trying to troll us. */
 
		return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
 
	}
 

	
 
	if (!NetworkMakeClientNameUnique(client_name)) { // Change name if duplicate
 
		/* We could not create a name for this client */
 
		return this->SendError(NETWORK_ERROR_NAME_IN_USE);
 
	}
 

	
 
	assert(NetworkClientInfo::CanAllocateItem());
 
	NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
 
	this->SetInfo(ci);
 
	ci->join_date = _date;
 
	ci->client_name = client_name;
 
	ci->client_playas = playas;
 
	Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:02x}", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
 

	
 
	/* Make sure companies to which people try to join are not autocleaned */
 
	if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
 

	
 
	this->status = STATUS_NEWGRFS_CHECK;
 

	
 
	if (_grfconfig == nullptr) {
 
		/* Behave as if we received PACKET_CLIENT_NEWGRFS_CHECKED */
 
		return this->Receive_CLIENT_NEWGRFS_CHECKED(nullptr);
 
	}
 

	
 
	return this->SendNewGRFCheck();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(Packet *p)
 
{
 
	if (this->status != STATUS_AUTH_GAME) {
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
	/* Check game password. Allow joining if we cleared the password meanwhile */
 
	if (!_settings_client.network.server_password.empty() &&
 
			_settings_client.network.server_password.compare(password) != 0) {
 
		/* Password is invalid */
 
		return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
 
	}
 

	
 
	const NetworkClientInfo *ci = this->GetInfo();
 
	if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) {
 
		return this->SendNeedCompanyPassword();
 
	}
 

	
 
	/* Valid password, allow user */
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD(Packet *p)
 
{
 
	if (this->status != STATUS_AUTH_COMPANY) {
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
	/* Check company password. Allow joining if we cleared the password meanwhile.
0 comments (0 inline, 0 general)