Changeset - r25475:0426c207464e
[Not reviewed]
master
0 7 0
rubidium42 - 3 years ago 2021-05-02 07:07:09
rubidium@openttd.org
Codechange: [Network] Use std::string for the internal handling of company passwords
7 files changed with 32 insertions and 37 deletions:
0 comments (0 inline, 0 general)
src/network/network.cpp
Show inline comments
 
@@ -172,13 +172,13 @@ const char *NetworkChangeCompanyPassword
 
 * @param password_game_seed Game seed.
 
 * @return The hashed password.
 
 */
 
const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed)
 
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed)
 
{
 
	if (StrEmpty(password)) return password;
 
	if (password.empty()) return password;
 

	
 
	char salted_password[NETWORK_SERVER_ID_LENGTH];
 
	size_t password_length = strlen(password);
 
	size_t password_server_id_length = strlen(password_server_id);
 
	size_t password_length = password.size();
 
	size_t password_server_id_length = password_server_id.size();
 

	
 
	/* Add the game seed and the server's ID as the salt. */
 
	for (uint i = 0; i < NETWORK_SERVER_ID_LENGTH - 1; i++) {
src/network/network_client.cpp
Show inline comments
 
@@ -319,7 +319,7 @@ static uint32 last_ack_frame;
 
/** One bit of 'entropy' used to generate a salt for the company passwords. */
 
static uint32 _password_game_seed;
 
/** The other bit of 'entropy' used to generate a salt for the company passwords. */
 
static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
 
static std::string _password_server_id;
 

	
 
/** Maximum number of companies of the currently joined server. */
 
static uint8 _network_server_max_companies;
 
@@ -397,7 +397,7 @@ NetworkRecvStatus ClientNetworkGameSocke
 
 * Set the company password as requested.
 
 * @param password The company password.
 
 */
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const char *password)
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendCompanyPassword(const std::string &password)
 
{
 
	Packet *p = new Packet(PACKET_CLIENT_COMPANY_PASSWORD);
 
	p->Send_string(GenerateCompanyPasswordHash(password, _password_server_id, _password_game_seed));
 
@@ -478,7 +478,7 @@ NetworkRecvStatus ClientNetworkGameSocke
 
 * Tell the server that we like to change the password of the company.
 
 * @param password The new password.
 
 */
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const char *password)
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendSetPassword(const std::string &password)
 
{
 
	Packet *p = new Packet(PACKET_CLIENT_SET_PASSWORD);
 

	
 
@@ -530,7 +530,7 @@ NetworkRecvStatus ClientNetworkGameSocke
 
 * @param company The company to move to.
 
 * @param password The password of the company to move to.
 
 */
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const char *password)
 
NetworkRecvStatus ClientNetworkGameSocketHandler::SendMove(CompanyID company, const std::string &password)
 
{
 
	Packet *p = new Packet(PACKET_CLIENT_MOVE);
 
	p->Send_uint8(company);
 
@@ -815,12 +815,11 @@ NetworkRecvStatus ClientNetworkGameSocke
 
	this->status = STATUS_AUTH_COMPANY;
 

	
 
	_password_game_seed = p->Recv_uint32();
 
	p->Recv_string(_password_server_id, sizeof(_password_server_id));
 
	_password_server_id = p->Recv_string(NETWORK_SERVER_ID_LENGTH);
 
	if (this->HasClientQuit()) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 

	
 
	const char *password = _network_join.company_password;
 
	if (!StrEmpty(password)) {
 
		return SendCompanyPassword(password);
 
	if (!_network_join.company_password.empty()) {
 
		return SendCompanyPassword(_network_join.company_password);
 
	}
 

	
 
	ShowNetworkNeedPassword(NETWORK_COMPANY_PASSWORD);
 
@@ -837,7 +836,7 @@ NetworkRecvStatus ClientNetworkGameSocke
 

	
 
	/* Initialize the password hash salting variables, even if they were previously. */
 
	_password_game_seed = p->Recv_uint32();
 
	p->Recv_string(_password_server_id, sizeof(_password_server_id));
 
	_password_server_id = p->Recv_string(NETWORK_SERVER_ID_LENGTH);
 

	
 
	/* Start receiving the map */
 
	return SendGetMap();
 
@@ -1274,7 +1273,7 @@ void NetworkClientSendRcon(const char *p
 
 * @param pass the password, is only checked on the server end if a password is needed.
 
 * @return void
 
 */
 
void NetworkClientRequestMove(CompanyID company_id, const char *pass)
 
void NetworkClientRequestMove(CompanyID company_id, const std::string &pass)
 
{
 
	MyClient::SendMove(company_id, pass);
 
}
 
@@ -1396,7 +1395,7 @@ void NetworkClientSendChat(NetworkAction
 
 * Set/Reset company password on the client side.
 
 * @param password Password to be set.
 
 */
 
void NetworkClientSetCompanyPassword(const char *password)
 
void NetworkClientSetCompanyPassword(const std::string &password)
 
{
 
	MyClient::SendSetPassword(password);
 
}
src/network/network_client.h
Show inline comments
 
@@ -91,13 +91,13 @@ public:
 
	static NetworkRecvStatus SendAck();
 

	
 
	static NetworkRecvStatus SendGamePassword(const char *password);
 
	static NetworkRecvStatus SendCompanyPassword(const char *password);
 
	static NetworkRecvStatus SendCompanyPassword(const std::string &password);
 

	
 
	static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
 
	static NetworkRecvStatus SendSetPassword(const char *password);
 
	static NetworkRecvStatus SendSetPassword(const std::string &password);
 
	static NetworkRecvStatus SendSetName(const char *name);
 
	static NetworkRecvStatus SendRCon(const char *password, const char *command);
 
	static NetworkRecvStatus SendMove(CompanyID company, const char *password);
 
	static NetworkRecvStatus SendMove(CompanyID company, const std::string &password);
 

	
 
	static bool IsConnected();
 

	
 
@@ -110,15 +110,15 @@ public:
 
typedef ClientNetworkGameSocketHandler MyClient;
 

	
 
void NetworkClient_Connected();
 
void NetworkClientSetCompanyPassword(const char *password);
 
void NetworkClientSetCompanyPassword(const std::string &password);
 

	
 
/** Information required to join a server. */
 
struct NetworkJoinInfo {
 
	NetworkJoinInfo() : company(COMPANY_SPECTATOR), server_password(nullptr), company_password(nullptr) {}
 
	NetworkJoinInfo() : company(COMPANY_SPECTATOR), server_password(nullptr) {}
 
	std::string connection_string; ///< The address of the server to join.
 
	CompanyID company;             ///< The company to join.
 
	const char *server_password;   ///< The password of the server to join.
 
	const char *company_password;  ///< The password of the company to join.
 
	std::string company_password;  ///< The password of the company to join.
 
};
 

	
 
extern NetworkJoinInfo _network_join;
src/network/network_func.h
Show inline comments
 
@@ -53,7 +53,7 @@ void NetworkUpdateClientInfo(ClientID cl
 
void NetworkClientsToSpectators(CompanyID cid);
 
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password = nullptr, const char *join_company_password = nullptr);
 
void NetworkClientJoinGame();
 
void NetworkClientRequestMove(CompanyID company, const char *pass = "");
 
void NetworkClientRequestMove(CompanyID company, const std::string &pass = "");
 
void NetworkClientSendRcon(const char *password, const char *command);
 
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
 
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
src/network/network_internal.h
Show inline comments
 
@@ -118,7 +118,7 @@ void NetworkTextMessage(NetworkAction ac
 
uint NetworkCalculateLag(const NetworkClientSocket *cs);
 
StringID GetNetworkErrorMsg(NetworkErrorCode err);
 
bool NetworkFindName(char *new_name, const char *last);
 
const char *GenerateCompanyPasswordHash(const char *password, const char *password_server_id, uint32 password_game_seed);
 
std::string GenerateCompanyPasswordHash(const std::string &password, const std::string &password_server_id, uint32 password_game_seed);
 

	
 
NetworkAddress ParseConnectionString(const std::string &connection_string, uint16 default_port);
 
std::string NormalizeConnectionString(const std::string &connection_string, uint16 default_port);
src/network/network_server.cpp
Show inline comments
 
@@ -978,8 +978,7 @@ NetworkRecvStatus ServerNetworkGameSocke
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	char password[NETWORK_PASSWORD_LENGTH];
 
	p->Recv_string(password, sizeof(password));
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
	/* Check company password. Allow joining if we cleared the password meanwhile.
 
	 * Also, check the company is still valid - client could be moved to spectators
 
@@ -1389,11 +1388,8 @@ NetworkRecvStatus ServerNetworkGameSocke
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	char password[NETWORK_PASSWORD_LENGTH];
 
	const NetworkClientInfo *ci;
 

	
 
	p->Recv_string(password, sizeof(password));
 
	ci = this->GetInfo();
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 
	const NetworkClientInfo *ci = this->GetInfo();
 

	
 
	NetworkServerSetCompanyPassword(ci->client_playas, password);
 
	return NETWORK_RECV_STATUS_OKAY;
 
@@ -1469,8 +1465,7 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	/* Check if we require a password for this company */
 
	if (company_id != COMPANY_SPECTATOR && !_network_company_states[company_id].password.empty()) {
 
		/* we need a password from the client - should be in this packet */
 
		char password[NETWORK_PASSWORD_LENGTH];
 
		p->Recv_string(password, sizeof(password));
 
		std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
		/* Incorrect password sent, return! */
 
		if (_network_company_states[company_id].password.compare(password) != 0) {
 
@@ -1757,15 +1752,16 @@ bool NetworkServerChangeClientName(Clien
 
 * @param password The new password.
 
 * @param already_hashed Is the given password already hashed?
 
 */
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed)
 
{
 
	if (!Company::IsValidHumanID(company_id)) return;
 

	
 
	if (!already_hashed) {
 
		password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id.c_str(), _settings_game.game_creation.generation_seed);
 
	if (already_hashed) {
 
		_network_company_states[company_id].password = password;
 
	} else {
 
		_network_company_states[company_id].password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _settings_game.game_creation.generation_seed);
 
	}
 

	
 
	_network_company_states[company_id].password = password;
 
	NetworkServerUpdateCompanyPassworded(company_id, !_network_company_states[company_id].password.empty());
 
}
 

	
src/network/network_server.h
Show inline comments
 
@@ -121,7 +121,7 @@ public:
 
};
 

	
 
void NetworkServer_Tick(bool send_frame);
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed = true);
 
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
 

	
 
#endif /* NETWORK_SERVER_H */
0 comments (0 inline, 0 general)