Changeset - r25478:0c7266566a47
[Not reviewed]
master
0 6 0
rubidium42 - 4 years ago 2021-05-02 07:18:56
rubidium@openttd.org
Codechange: [Network] Pass passwords as std::string to the network code
6 files changed with 24 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -844,7 +844,7 @@ CommandCost CmdCompanyCtrl(TileIndex til
 
				assert(_local_company == COMPANY_SPECTATOR);
 
				SetLocalCompany(c->index);
 
				if (!_settings_client.network.default_company_pass.empty()) {
 
					NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass.c_str());
 
					NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
 
				}
 

	
 
				/* Now that we have a new company, broadcast our company settings to
src/console_cmds.cpp
Show inline comments
 
@@ -1737,7 +1737,7 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
 
	}
 

	
 
	CompanyID company_id;
 
	const char *password;
 
	std::string password;
 
	const char *errormsg;
 

	
 
	if (argc == 2) {
 
@@ -1759,10 +1759,10 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
 

	
 
	password = NetworkChangeCompanyPassword(company_id, password);
 

	
 
	if (StrEmpty(password)) {
 
	if (password.empty()) {
 
		IConsolePrintF(CC_WARNING, "Company password cleared");
 
	} else {
 
		IConsolePrintF(CC_WARNING, "Company password changed to: %s", password);
 
		IConsolePrintF(CC_WARNING, "Company password changed to: %s", password.c_str());
 
	}
 

	
 
	return true;
src/network/network.cpp
Show inline comments
 
@@ -152,9 +152,9 @@ byte NetworkSpectatorCount()
 
 * @param password The unhashed password we like to set ('*' or '' resets the password)
 
 * @return The password.
 
 */
 
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password)
 
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password)
 
{
 
	if (strcmp(password, "*") == 0) password = "";
 
	if (password.compare("*") == 0) password = "";
 

	
 
	if (_network_server) {
 
		NetworkServerSetCompanyPassword(company_id, password, false);
 
@@ -787,7 +787,7 @@ public:
 
 * @param join_company_password The password for the company.
 
 * @return Whether the join has started.
 
 */
 
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const char *join_server_password, const char *join_company_password)
 
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password, const std::string &join_company_password)
 
{
 
	CompanyID join_as = default_company;
 
	std::string resolved_connection_string = ParseGameConnectionString(connection_string, NETWORK_DEFAULT_PORT, &join_as).GetAddressAsString(false);
src/network/network_client.cpp
Show inline comments
 
@@ -1261,7 +1261,7 @@ void NetworkClient_Connected()
 
 * @param password The password.
 
 * @param command The command to execute.
 
 */
 
void NetworkClientSendRcon(const char *password, const char *command)
 
void NetworkClientSendRcon(const std::string &password, const char *command)
 
{
 
	MyClient::SendRCon(password, command);
 
}
src/network/network_func.h
Show inline comments
 
@@ -40,7 +40,7 @@ bool NetworkValidateClientName();
 
bool NetworkValidateClientName(std::string &client_name);
 
void NetworkUpdateClientName();
 
bool NetworkCompanyHasClients(CompanyID company);
 
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);
 
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
 
void NetworkReboot();
 
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
 
void NetworkGameLoop();
 
@@ -51,10 +51,10 @@ void NetworkPopulateCompanyStats(Network
 

	
 
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);
 
bool NetworkClientConnectGame(const std::string &connection_string, CompanyID default_company, const std::string &join_server_password = "", const std::string &join_company_password = "");
 
void NetworkClientJoinGame();
 
void NetworkClientRequestMove(CompanyID company, const std::string &pass = "");
 
void NetworkClientSendRcon(const char *password, const char *command);
 
void NetworkClientSendRcon(const std::string &password, const char *command);
 
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
 
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
 
bool NetworkCompanyIsPassworded(CompanyID company_id);
src/openttd.cpp
Show inline comments
 
@@ -400,23 +400,19 @@ void OpenBrowser(const char *url)
 

	
 
/** Callback structure of statements to be executed after the NewGRF scan. */
 
struct AfterNewGRFScan : NewGRFScanCallback {
 
	Year startyear;                    ///< The start year.
 
	uint32 generation_seed;            ///< Seed for the new game.
 
	std::string dedicated_host;        ///< Hostname for the dedicated server.
 
	uint16 dedicated_port;             ///< Port for the dedicated server.
 
	char *network_conn;                ///< Information about the server to connect to, or nullptr.
 
	const char *join_server_password;  ///< The password to join the server with.
 
	const char *join_company_password; ///< The password to join the company with.
 
	bool save_config;                  ///< The save config setting.
 
	Year startyear = INVALID_YEAR;              ///< The start year.
 
	uint32 generation_seed = GENERATE_NEW_SEED; ///< Seed for the new game.
 
	std::string dedicated_host;                 ///< Hostname for the dedicated server.
 
	uint16 dedicated_port = 0;                  ///< Port for the dedicated server.
 
	std::string connection_string;              ///< Information about the server to connect to
 
	std::string join_server_password;           ///< The password to join the server with.
 
	std::string join_company_password;          ///< The password to join the company with.
 
	bool save_config = true;                    ///< The save config setting.
 

	
 
	/**
 
	 * Create a new callback.
 
	 */
 
	AfterNewGRFScan() :
 
			startyear(INVALID_YEAR), generation_seed(GENERATE_NEW_SEED),
 
			dedicated_port(0), network_conn(nullptr),
 
			join_server_password(nullptr), join_company_password(nullptr),
 
			save_config(true)
 
	AfterNewGRFScan()
 
	{
 
		/* Visual C++ 2015 fails compiling this line (AfterNewGRFScan::generation_seed undefined symbol)
 
		 * if it's placed outside a member function, directly in the struct body. */
 
@@ -469,11 +465,11 @@ struct AfterNewGRFScan : NewGRFScanCallb
 
		/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
 
		if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();
 

	
 
		if (_network_available && network_conn != nullptr) {
 
		if (_network_available && !connection_string.empty()) {
 
			LoadIntroGame();
 
			_switch_mode = SM_NONE;
 

	
 
			NetworkClientConnectGame(network_conn, COMPANY_NEW_COMPANY, join_server_password, join_company_password);
 
			NetworkClientConnectGame(connection_string, COMPANY_NEW_COMPANY, join_server_password, join_company_password);
 
		}
 

	
 
		/* After the scan we're not used anymore. */
 
@@ -567,7 +563,7 @@ int openttd_main(int argc, char *argv[])
 
			break;
 
		case 'f': _dedicated_forks = true; break;
 
		case 'n':
 
			scanner->network_conn = mgo.opt; // optional IP parameter, nullptr if unset
 
			scanner->connection_string = mgo.opt; // optional IP:port#company parameter
 
			break;
 
		case 'l':
 
			debuglog_conn = mgo.opt;
 
@@ -875,7 +871,7 @@ static void MakeNewGameDone()
 
	/* We are the server, we start a new company (not dedicated),
 
	 * so set the default password *if* needed. */
 
	if (_network_server && !_settings_client.network.default_company_pass.empty()) {
 
		NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass.c_str());
 
		NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass);
 
	}
 

	
 
	if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
0 comments (0 inline, 0 general)