Changeset - r25551:26df1bec7e96
[Not reviewed]
master
0 5 0
rubidium42 - 3 years ago 2021-05-24 10:13:54
rubidium@openttd.org
Fix: [Network] Prevent an empty server name to be set anywhere
5 files changed with 36 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/lang/english.txt
Show inline comments
 
@@ -2201,12 +2201,13 @@ STR_NETWORK_ERROR_LOSTCONNECTION        
 
STR_NETWORK_ERROR_SAVEGAMEERROR                                 :{WHITE}Could not load savegame
 
STR_NETWORK_ERROR_SERVER_START                                  :{WHITE}Could not start the server
 
STR_NETWORK_ERROR_CLIENT_START                                  :{WHITE}Could not connect
 
STR_NETWORK_ERROR_TIMEOUT                                       :{WHITE}Connection #{NUM} timed out
 
STR_NETWORK_ERROR_SERVER_ERROR                                  :{WHITE}A protocol error was detected and the connection was closed
 
STR_NETWORK_ERROR_BAD_PLAYER_NAME                               :{WHITE}Your player name has not been set. The name can be set at the top of the Multiplayer window
 
STR_NETWORK_ERROR_BAD_SERVER_NAME                               :{WHITE}Your server name has not been set. The name can be set at the top of the Multiplayer window
 
STR_NETWORK_ERROR_WRONG_REVISION                                :{WHITE}The revision of this client does not match the server's revision
 
STR_NETWORK_ERROR_WRONG_PASSWORD                                :{WHITE}Wrong password
 
STR_NETWORK_ERROR_SERVER_FULL                                   :{WHITE}The server is full
 
STR_NETWORK_ERROR_SERVER_BANNED                                 :{WHITE}You are banned from this server
 
STR_NETWORK_ERROR_KICKED                                        :{WHITE}You were kicked out of the game
 
STR_NETWORK_ERROR_KICK_MESSAGE                                  :{WHITE}Reason: {RAW_STRING}
src/network/network.cpp
Show inline comments
 
@@ -832,16 +832,12 @@ void NetworkClientJoinGame()
 

	
 
	new TCPClientConnecter(_network_join.connection_string);
 
}
 

	
 
static void NetworkInitGameInfo()
 
{
 
	if (_settings_client.network.server_name.empty()) {
 
		_settings_client.network.server_name = "Unnamed Server";
 
	}
 

	
 
	FillStaticNetworkServerGameInfo();
 
	/* The server is a client too */
 
	_network_game_info.clients_on = _network_dedicated ? 0 : 1;
 

	
 
	/* There should be always space for the server. */
 
	assert(NetworkClientInfo::CanAllocateItem());
 
@@ -849,26 +845,47 @@ static void NetworkInitGameInfo()
 
	ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
 

	
 
	ci->client_name = _settings_client.network.client_name;
 
}
 

	
 
/**
 
 * Trim the given server name in place, i.e. remove leading and trailing spaces.
 
 * After the trim check whether the server name is not empty.
 
 * When the server name is empty a GUI error message is shown telling the
 
 * user to set the servername and this function returns false.
 
 *
 
 * @param server_name The server name to validate. It will be trimmed of leading
 
 *                    and trailing spaces.
 
 * @return True iff the server name is valid.
 
 */
 
bool NetworkValidateServerName(std::string &server_name)
 
{
 
	StrTrimInPlace(server_name);
 
	if (!server_name.empty()) return true;
 

	
 
	ShowErrorMessage(STR_NETWORK_ERROR_BAD_SERVER_NAME, INVALID_STRING_ID, WL_ERROR);
 
	return false;
 
}
 

	
 
/**
 
 * Check whether the client and server name are set, for a dedicated server and if not set them to some default
 
 * value and tell the user to change this as soon as possible.
 
 * If the saved name is the default value, then the user is told to override  this value too.
 
 * This is only meant dedicated servers, as for the other servers the GUI ensures a name has been entered.
 
 */
 
static void CheckClientAndServerName()
 
{
 
	static const std::string fallback_client_name = "Unnamed Client";
 
	StrTrimInPlace(_settings_client.network.client_name);
 
	if (_settings_client.network.client_name.empty() || _settings_client.network.client_name.compare(fallback_client_name) == 0) {
 
		DEBUG(net, 1, "No \"client_name\" has been set, using \"%s\" instead. Please set this now using the \"name <new name>\" command", fallback_client_name.c_str());
 
		_settings_client.network.client_name = fallback_client_name;
 
	}
 

	
 
	static const std::string fallback_server_name = "Unnamed Server";
 
	StrTrimInPlace(_settings_client.network.server_name);
 
	if (_settings_client.network.server_name.empty() || _settings_client.network.server_name.compare(fallback_server_name) == 0) {
 
		DEBUG(net, 1, "No \"server_name\" has been set, using \"%s\" instead. Please set this now using the \"server_name <new name>\" command", fallback_server_name.c_str());
 
		_settings_client.network.server_name = fallback_server_name;
 
	}
 
}
 

	
src/network/network_func.h
Show inline comments
 
@@ -35,12 +35,13 @@ extern StringList _network_host_list;
 
extern StringList _network_ban_list;
 

	
 
byte NetworkSpectatorCount();
 
bool NetworkIsValidClientName(const std::string_view client_name);
 
bool NetworkValidateClientName();
 
bool NetworkValidateClientName(std::string &client_name);
 
bool NetworkValidateServerName(std::string &server_name);
 
void NetworkUpdateClientName(const std::string &client_name);
 
bool NetworkCompanyHasClients(CompanyID company);
 
std::string NetworkChangeCompanyPassword(CompanyID company_id, std::string password);
 
void NetworkReboot();
 
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
 
void NetworkGameLoop();
src/network/network_gui.cpp
Show inline comments
 
@@ -1092,31 +1092,35 @@ struct NetworkStartServerWindow : public
 
				this->widget_id = WID_NSS_SPECTATORS_TXT;
 
				SetDParam(0, _settings_client.network.max_spectators);
 
				ShowQueryString(STR_JUST_INT, STR_NETWORK_START_SERVER_NUMBER_OF_SPECTATORS, 4, this, CS_NUMERAL, QSF_NONE);
 
				break;
 

	
 
			case WID_NSS_GENERATE_GAME: // Start game
 
				if (!CheckServerName()) return;
 
				_is_network_server = true;
 
				if (_ctrl_pressed) {
 
					StartNewGameWithoutGUI(GENERATE_NEW_SEED);
 
				} else {
 
					ShowGenerateLandscape();
 
				}
 
				break;
 

	
 
			case WID_NSS_LOAD_GAME:
 
				if (!CheckServerName()) return;
 
				_is_network_server = true;
 
				ShowSaveLoadDialog(FT_SAVEGAME, SLO_LOAD);
 
				break;
 

	
 
			case WID_NSS_PLAY_SCENARIO:
 
				if (!CheckServerName()) return;
 
				_is_network_server = true;
 
				ShowSaveLoadDialog(FT_SCENARIO, SLO_LOAD);
 
				break;
 

	
 
			case WID_NSS_PLAY_HEIGHTMAP:
 
				if (!CheckServerName()) return;
 
				_is_network_server = true;
 
				ShowSaveLoadDialog(FT_HEIGHTMAP,SLO_LOAD);
 
				break;
 
		}
 
	}
 

	
 
@@ -1130,17 +1134,19 @@ struct NetworkStartServerWindow : public
 
				NOT_REACHED();
 
		}
 

	
 
		this->SetDirty();
 
	}
 

	
 
	void OnEditboxChanged(int wid) override
 
	bool CheckServerName()
 
	{
 
		if (wid == WID_NSS_GAMENAME) {
 
			_settings_client.network.server_name = this->name_editbox.text.buf;
 
		}
 
		std::string str = this->name_editbox.text.buf;
 
		if (!NetworkValidateServerName(str)) return false;
 

	
 
		SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), str);
 
		return true;
 
	}
 

	
 
	void OnTimeout() override
 
	{
 
		static const int raise_widgets[] = {WID_NSS_CLIENTS_BTND, WID_NSS_CLIENTS_BTNU, WID_NSS_COMPANIES_BTND, WID_NSS_COMPANIES_BTNU, WID_NSS_SPECTATORS_BTND, WID_NSS_SPECTATORS_BTNU, WIDGET_LIST_END};
 
		for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
 
@@ -2196,22 +2202,19 @@ public:
 
		switch (this->query_widget) {
 
			default: NOT_REACHED();
 

	
 
			case WID_CL_SERVER_NAME_EDIT: {
 
				if (!_network_server) break;
 

	
 
				SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), StrEmpty(str) ? "Unnamed Server" : str);
 
				SetSettingValue(GetSettingFromName("network.server_name")->AsStringSetting(), str);
 
				this->InvalidateData();
 
				break;
 
			}
 

	
 
			case WID_CL_CLIENT_NAME_EDIT: {
 
				std::string client_name(str);
 
				if (!NetworkValidateClientName(client_name)) break;
 

	
 
				SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), client_name);
 
				SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), str);
 
				this->InvalidateData();
 
				break;
 
			}
 

	
 
			case WID_CL_COMPANY_JOIN:
 
				NetworkClientRequestMove(this->join_company, str);
src/table/settings.ini
Show inline comments
 
@@ -3956,12 +3956,13 @@ def      = nullptr
 
var      = network.server_name
 
type     = SLE_STR
 
length   = NETWORK_NAME_LENGTH
 
flags    = SLF_NOT_IN_SAVE | SLF_NO_NETWORK_SYNC
 
guiflags = SGF_NETWORK_ONLY
 
def      = nullptr
 
pre_cb   = NetworkValidateServerName
 
post_cb  = [](auto) { UpdateClientConfigValues(); }
 
cat      = SC_BASIC
 

	
 
[SDTC_SSTR]
 
var      = network.connect_to_ip
 
type     = SLE_STR
0 comments (0 inline, 0 general)