From 1de5cdeab8387b23b566142f205ac9232a6f61a3 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sun, 2 May 2021 09:07:09 +0200 Subject: Codechange: [Network] Use std::string for the internal handling of company passwords --- src/network/network.cpp | 8 ++++---- src/network/network_client.cpp | 21 ++++++++++----------- src/network/network_client.h | 12 ++++++------ src/network/network_func.h | 2 +- src/network/network_internal.h | 2 +- src/network/network_server.cpp | 22 +++++++++------------- src/network/network_server.h | 2 +- 7 files changed, 32 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/network/network.cpp b/src/network/network.cpp index b77e0477c..61baa7617 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -172,13 +172,13 @@ const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *passw * @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++) { diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index b9bcfe1f2..675807dec 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -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 ClientNetworkGameSocketHandler::SendGamePassword(const char *p * 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 ClientNetworkGameSocketHandler::SendError(NetworkErrorCode err * 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 ClientNetworkGameSocketHandler::SendRCon(const char *pass, con * @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 ClientNetworkGameSocketHandler::Receive_SERVER_NEED_COMPANY_PA 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 ClientNetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet /* 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 *password, const char *command) * @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 action, DestType type, int dest, const * 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); } diff --git a/src/network/network_client.h b/src/network/network_client.h index ddc0e362c..c4dc4b1fd 100644 --- a/src/network/network_client.h +++ b/src/network/network_client.h @@ -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; diff --git a/src/network/network_func.h b/src/network/network_func.h index cb0ca3355..bedc9bc54 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -53,7 +53,7 @@ void NetworkUpdateClientInfo(ClientID client_id); 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); diff --git a/src/network/network_internal.h b/src/network/network_internal.h index 3456ab251..7412d9232 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -118,7 +118,7 @@ void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, 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); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 1a6012fb1..4d35af116 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -978,8 +978,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWOR 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 ServerNetworkGameSocketHandler::Receive_CLIENT_SET_PASSWORD(Pa 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 ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet *p) /* 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(ClientID client_id, const char *new_name) * @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()); } diff --git a/src/network/network_server.h b/src/network/network_server.h index fb4c4f63e..f8f58d1e3 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -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 */ -- cgit v1.2.3-54-g00ecf