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
 
@@ -841,13 +841,13 @@ CommandCost CmdCompanyCtrl(TileIndex til
 

	
 
			/* This is the client (or non-dedicated server) who wants a new company */
 
			if (client_id == _network_own_client_id) {
 
				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
 
				 * all clients so everything is in sync */
 
				SyncCompanySettings();
 

	
src/console_cmds.cpp
Show inline comments
 
@@ -1734,13 +1734,13 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
 
		IConsoleHelp(helpmsg);
 
		IConsoleHelp("Use \"*\" to disable the password.");
 
		return true;
 
	}
 

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

	
 
	if (argc == 2) {
 
		company_id = _local_company;
 
		password = argv[1];
 
		errormsg = "You have to own a company to make use of this command.";
 
@@ -1756,16 +1756,16 @@ DEF_CONSOLE_CMD(ConCompanyPassword)
 
		IConsoleError(errormsg);
 
		return false;
 
	}
 

	
 
	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;
 
}
 

	
 
/* Content downloading only is available with ZLIB */
src/network/network.cpp
Show inline comments
 
@@ -149,15 +149,15 @@ byte NetworkSpectatorCount()
 
/**
 
 * Change the company password of a given company.
 
 * @param company_id ID of the company the password should be changed for.
 
 * @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);
 
	} else {
 
		NetworkClientSetCompanyPassword(password);
 
	}
 
@@ -784,13 +784,13 @@ public:
 
 * @param connection_string     The IP address, port and company number to join as.
 
 * @param default_company       The company number to join as when none is given.
 
 * @param join_server_password  The password for the server.
 
 * @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);
 

	
 
	if (!_network_available) return false;
 
	if (!NetworkValidateClientName()) return false;
src/network/network_client.cpp
Show inline comments
 
@@ -1258,13 +1258,13 @@ void NetworkClient_Connected()
 

	
 
/**
 
 * Send a remote console command.
 
 * @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);
 
}
 

	
 
/**
 
 * Notify the server of this client wanting to be moved to another company.
src/network/network_func.h
Show inline comments
 
@@ -37,27 +37,27 @@ extern StringList _network_ban_list;
 
byte NetworkSpectatorCount();
 
bool NetworkIsValidClientName(const std::string_view client_name);
 
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();
 
void NetworkBackgroundLoop();
 
std::string_view ParseFullConnectionString(const std::string &connection_string, uint16 &port, CompanyID *company_id = nullptr);
 
void NetworkStartDebugLog(const std::string &connection_string);
 
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
 

	
 
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);
 
bool NetworkMaxCompaniesReached();
 
bool NetworkMaxSpectatorsReached();
 
void NetworkPrintClients();
src/openttd.cpp
Show inline comments
 
@@ -397,29 +397,25 @@ void OpenBrowser(const char *url)
 
	extern void OSOpenBrowser(const char *url);
 
	OSOpenBrowser(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. */
 
		static_assert(sizeof(generation_seed) == sizeof(_settings_game.game_creation.generation_seed));
 
	}
 

	
 
@@ -466,17 +462,17 @@ struct AfterNewGRFScan : NewGRFScanCallb
 
		InitializeGUI();
 
		IConsoleCmdExec("exec scripts/autoexec.scr 0");
 

	
 
		/* 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. */
 
		delete this;
 
	}
 
};
 
@@ -564,13 +560,13 @@ int openttd_main(int argc, char *argv[])
 
			if (mgo.opt != nullptr) {
 
				scanner->dedicated_host = ParseFullConnectionString(mgo.opt, scanner->dedicated_port);
 
			}
 
			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;
 
			break;
 
		case 'p':
 
			scanner->join_server_password = mgo.opt;
 
@@ -872,13 +868,13 @@ static void MakeNewGameDone()
 
	InitializeRailGUI();
 
	InitializeRoadGUI();
 

	
 
	/* 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);
 

	
 
	CheckEngines();
 
	CheckIndustries();
0 comments (0 inline, 0 general)