Codechange: [Network] Use std::string for the internal handling of server passwords

This commit is contained in:
rubidium42 2021-05-02 09:10:09 +02:00 committed by rubidium42
parent 1de5cdeab8
commit 6db52d52d0
3 changed files with 7 additions and 9 deletions

View File

@ -385,7 +385,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::SendNewGRFsOk()
* Set the game password as requested. * Set the game password as requested.
* @param password The game password. * @param password The game password.
*/ */
NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const char *password) NetworkRecvStatus ClientNetworkGameSocketHandler::SendGamePassword(const std::string &password)
{ {
Packet *p = new Packet(PACKET_CLIENT_GAME_PASSWORD); Packet *p = new Packet(PACKET_CLIENT_GAME_PASSWORD);
p->Send_string(password); p->Send_string(password);
@ -799,9 +799,8 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_NEED_GAME_PASSW
if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET; if (this->status < STATUS_JOIN || this->status >= STATUS_AUTH_GAME) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
this->status = STATUS_AUTH_GAME; this->status = STATUS_AUTH_GAME;
const char *password = _network_join.server_password; if (!_network_join.server_password.empty()) {
if (!StrEmpty(password)) { return SendGamePassword(_network_join.server_password);
return SendGamePassword(password);
} }
ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD); ShowNetworkNeedPassword(NETWORK_GAME_PASSWORD);

View File

@ -90,7 +90,7 @@ public:
static NetworkRecvStatus SendQuit(); static NetworkRecvStatus SendQuit();
static NetworkRecvStatus SendAck(); static NetworkRecvStatus SendAck();
static NetworkRecvStatus SendGamePassword(const char *password); static NetworkRecvStatus SendGamePassword(const std::string &password);
static NetworkRecvStatus SendCompanyPassword(const std::string &password); static NetworkRecvStatus SendCompanyPassword(const std::string &password);
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data); static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data);
@ -114,10 +114,10 @@ void NetworkClientSetCompanyPassword(const std::string &password);
/** Information required to join a server. */ /** Information required to join a server. */
struct NetworkJoinInfo { struct NetworkJoinInfo {
NetworkJoinInfo() : company(COMPANY_SPECTATOR), server_password(nullptr) {} NetworkJoinInfo() : company(COMPANY_SPECTATOR) {}
std::string connection_string; ///< The address of the server to join. std::string connection_string; ///< The address of the server to join.
CompanyID company; ///< The company to join. CompanyID company; ///< The company to join.
const char *server_password; ///< The password of the server to join. std::string server_password; ///< The password of the server to join.
std::string company_password; ///< The password of the company to join. std::string company_password; ///< The password of the company to join.
}; };

View File

@ -953,8 +953,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(P
return this->SendError(NETWORK_ERROR_NOT_EXPECTED); return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
} }
char password[NETWORK_PASSWORD_LENGTH]; std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
p->Recv_string(password, sizeof(password));
/* Check game password. Allow joining if we cleared the password meanwhile */ /* Check game password. Allow joining if we cleared the password meanwhile */
if (!_settings_client.network.server_password.empty() && if (!_settings_client.network.server_password.empty() &&