From 98283116fac58053ebd282772b0311a0b417cef7 Mon Sep 17 00:00:00 2001 From: rubidium42 Date: Sun, 2 May 2021 08:27:06 +0200 Subject: Codechange: [Network] Make company state password std::string --- src/console_cmds.cpp | 2 +- src/network/network.cpp | 4 ++-- src/network/network_server.cpp | 26 +++++++++++++------------- src/network/network_type.h | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 0756f7266..82638a5cb 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1635,7 +1635,7 @@ DEF_CONSOLE_CMD(ConCompanies) if (c->is_ai) { password_state = "AI"; } else if (_network_server) { - password_state = StrEmpty(_network_company_states[c->index].password) ? "unprotected" : "protected"; + password_state = _network_company_states[c->index].password.empty() ? "unprotected" : "protected"; } char colour[512]; diff --git a/src/network/network.cpp b/src/network/network.cpp index a66da42bc..b77e0477c 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -601,7 +601,7 @@ void NetworkClose(bool close_admins) NetworkFreeLocalCommandQueue(); - free(_network_company_states); + delete[] _network_company_states; _network_company_states = nullptr; InitializeNetworkPools(close_admins); @@ -896,7 +896,7 @@ bool NetworkServerStart() DEBUG(net, 5, "Starting listeners for incoming server queries"); NetworkUDPServerListen(); - _network_company_states = CallocT(MAX_COMPANIES); + _network_company_states = new NetworkCompanyState[MAX_COMPANIES]; _network_server = true; _networking = true; _frame_counter = 0; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index f2a8a5230..1a6012fb1 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -863,7 +863,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED return this->SendNeedGamePassword(); } - if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) { + if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) { return this->SendNeedCompanyPassword(); } @@ -964,7 +964,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(P } const NetworkClientInfo *ci = this->GetInfo(); - if (Company::IsValidID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) { + if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) { return this->SendNeedCompanyPassword(); } @@ -985,8 +985,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWOR * Also, check the company is still valid - client could be moved to spectators * in the middle of the authorization process */ CompanyID playas = this->GetInfo()->client_playas; - if (Company::IsValidID(playas) && !StrEmpty(_network_company_states[playas].password) && - strcmp(password, _network_company_states[playas].password) != 0) { + if (Company::IsValidID(playas) && !_network_company_states[playas].password.empty() && + _network_company_states[playas].password.compare(password) != 0) { /* Password is invalid */ return this->SendError(NETWORK_ERROR_WRONG_PASSWORD); } @@ -1467,13 +1467,13 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet *p) if (company_id != COMPANY_SPECTATOR && !Company::IsValidHumanID(company_id)) return NETWORK_RECV_STATUS_OKAY; /* Check if we require a password for this company */ - if (company_id != COMPANY_SPECTATOR && !StrEmpty(_network_company_states[company_id].password)) { + 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)); /* Incorrect password sent, return! */ - if (strcmp(password, _network_company_states[company_id].password) != 0) { + if (_network_company_states[company_id].password.compare(password) != 0) { DEBUG(net, 2, "Wrong password from client-id #%d for company #%d", this->client_id, company_id + 1); return NETWORK_RECV_STATUS_OKAY; } @@ -1523,7 +1523,7 @@ void NetworkSocketHandler::SendCompanyInformation(Packet *p, const Company *c, c p->Send_uint16(c->old_economy[0].performance_history); /* Send 1 if there is a password for the company else send 0 */ - p->Send_bool (!StrEmpty(_network_company_states[c->index].password)); + p->Send_bool (!_network_company_states[c->index].password.empty()); for (uint i = 0; i < NETWORK_VEH_END; i++) { p->Send_uint16(stats->num_vehicle[i]); @@ -1660,15 +1660,15 @@ static void NetworkAutoCleanCompanies() _network_company_states[c->index].months_empty++; /* Is the company empty for autoclean_unprotected-months, and is there no protection? */ - if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_states[c->index].password)) { + if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && _network_company_states[c->index].password.empty()) { /* Shut the company down */ DoCommandP(0, CCA_DELETE | c->index << 16 | CRR_AUTOCLEAN << 24, 0, CMD_COMPANY_CTRL); IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no password", c->index + 1); } /* Is the company empty for autoclean_protected-months, and there is a protection? */ - if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_states[c->index].password)) { + if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !_network_company_states[c->index].password.empty()) { /* Unprotect the company */ - _network_company_states[c->index].password[0] = '\0'; + _network_company_states[c->index].password.clear(); IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1); _network_company_states[c->index].months_empty = 0; NetworkServerUpdateCompanyPassworded(c->index, false); @@ -1765,8 +1765,8 @@ void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id.c_str(), _settings_game.game_creation.generation_seed); } - strecpy(_network_company_states[company_id].password, password, lastof(_network_company_states[company_id].password)); - NetworkServerUpdateCompanyPassworded(company_id, !StrEmpty(_network_company_states[company_id].password)); + _network_company_states[company_id].password = password; + NetworkServerUpdateCompanyPassworded(company_id, !_network_company_states[company_id].password.empty()); } /** @@ -2188,7 +2188,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci) if (!_network_server) return; _network_company_states[c->index].months_empty = 0; - _network_company_states[c->index].password[0] = '\0'; + _network_company_states[c->index].password.clear(); NetworkServerUpdateCompanyPassworded(c->index, false); if (ci != nullptr) { diff --git a/src/network/network_type.h b/src/network/network_type.h index 4dcdd8c03..fb9953695 100644 --- a/src/network/network_type.h +++ b/src/network/network_type.h @@ -62,8 +62,8 @@ struct NetworkCompanyStats { /** Some state information of a company, especially for servers */ struct NetworkCompanyState { - char password[NETWORK_PASSWORD_LENGTH]; ///< The password for the company - uint16 months_empty; ///< How many months the company is empty + std::string password; ///< The password for the company + uint16 months_empty; ///< How many months the company is empty }; struct NetworkClientInfo; -- cgit v1.2.3-70-g09d2