summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-01-19 16:47:40 +0000
committerrubidium <rubidium@openttd.org>2011-01-19 16:47:40 +0000
commit795a5283289473185afdedb3133155101e84b0fe (patch)
tree00845d6c66a5a9289173acc4b7e1cb78eeb36c0d /src/network
parente54a1f67213218f52d8e27bce3aeda5d7dc16ad9 (diff)
downloadopenttd-795a5283289473185afdedb3133155101e84b0fe.tar.xz
(svn r21854) -Codechange: refactor the password setting methods to make it possible to change the password of other companies (on the server)
Diffstat (limited to 'src/network')
-rw-r--r--src/network/network.cpp15
-rw-r--r--src/network/network_client.cpp8
-rw-r--r--src/network/network_func.h2
-rw-r--r--src/network/network_gui.cpp2
-rw-r--r--src/network/network_server.cpp29
-rw-r--r--src/network/network_server.h2
6 files changed, 29 insertions, 29 deletions
diff --git a/src/network/network.cpp b/src/network/network.cpp
index d29ac023c..0902fcdb0 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -151,18 +151,19 @@ byte NetworkSpectatorCount()
}
/**
- * Sets/resets company password
- * @param password new password, "" or "*" resets password
- * @return new password
+ * 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(const char *password)
+const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
{
if (strcmp(password, "*") == 0) password = "";
- if (!_network_server) {
- NetworkClientSetCompanyPassword(password);
+ if (_network_server) {
+ NetworkServerSetCompanyPassword(company_id, password, already_hashed);
} else {
- HashCurrentCompanyPassword(password);
+ NetworkClientSetCompanyPassword(password);
}
return password;
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index 0f6606352..15ad3c6d9 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -278,9 +278,9 @@ ClientNetworkGameSocketHandler * ClientNetworkGameSocketHandler::my_client = NUL
static uint32 last_ack_frame;
/** One bit of 'entropy' used to generate a salt for the company passwords. */
-uint32 _password_game_seed;
+static uint32 _password_game_seed;
/** The other bit of 'entropy' used to generate a salt for the company passwords. */
-char _password_server_id[NETWORK_SERVER_ID_LENGTH];
+static char _password_server_id[NETWORK_SERVER_ID_LENGTH];
/** Maximum number of companies of the currently joined server. */
static uint8 _network_server_max_companies;
@@ -1178,6 +1178,10 @@ void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const
MyClient::SendChat(action, type, dest, msg, data);
}
+/**
+ * Set/Reset company password on the client side.
+ * @param password Password to be set.
+ */
void NetworkClientSetCompanyPassword(const char *password)
{
MyClient::SendSetPassword(password);
diff --git a/src/network/network_func.h b/src/network/network_func.h
index 29d924ae4..f50406140 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -36,7 +36,7 @@ extern StringList _network_ban_list;
byte NetworkSpectatorCount();
void NetworkUpdateClientName();
bool NetworkCompanyHasClients(CompanyID company);
-const char *NetworkChangeCompanyPassword(const char *);
+const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
void NetworkReboot();
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
void NetworkGameLoop();
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index 6ec2f7b38..ccaf3661f 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -2311,7 +2311,7 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
snprintf(_settings_client.network.default_company_pass, lengthof(_settings_client.network.default_company_pass), "%s", this->edit_str_buf);
}
- NetworkChangeCompanyPassword(this->edit_str_buf);
+ NetworkChangeCompanyPassword(_local_company, this->edit_str_buf);
}
virtual void OnPaint()
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 83352f702..fcfd89dde 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -1287,10 +1287,7 @@ DEF_GAME_RECEIVE_COMMAND(Server, PACKET_CLIENT_SET_PASSWORD)
p->Recv_string(password, sizeof(password));
ci = this->GetInfo();
- if (Company::IsValidID(ci->client_playas)) {
- strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password));
- NetworkServerUpdateCompanyPassworded(ci->client_playas, !StrEmpty(_network_company_states[ci->client_playas].password));
- }
+ NetworkServerSetCompanyPassword(ci->client_playas, password);
return NETWORK_RECV_STATUS_OKAY;
}
@@ -1630,23 +1627,21 @@ bool NetworkServerChangeClientName(ClientID client_id, const char *new_name)
}
/**
- * Hash the current company password; used when the server 'company' sets his/her password.
- * @param password The password to hash.
+ * Set/Reset a company password on the server end.
+ * @param company_id ID of the company the password should be changed for.
+ * @param password The new password.
+ * @param already_hashed Is the given password already hashed?
*/
-void HashCurrentCompanyPassword(const char *password)
+void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed)
{
- uint32 password_game_seed;
- char password_server_id[NETWORK_SERVER_ID_LENGTH];
-
- password_game_seed = _settings_game.game_creation.generation_seed;
- strecpy(password_server_id, _settings_client.network.network_id, lastof(password_server_id));
+ if (!Company::IsValidHumanID(company_id)) return;
- const char *new_pw = GenerateCompanyPasswordHash(password, password_server_id, password_game_seed);
- strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
-
- if (_network_server) {
- NetworkServerUpdateCompanyPassworded(_local_company, !StrEmpty(_network_company_states[_local_company].password));
+ if (!already_hashed) {
+ password = GenerateCompanyPasswordHash(password, _settings_client.network.network_id, _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));
}
/* Handle the local command-queue */
diff --git a/src/network/network_server.h b/src/network/network_server.h
index cd087478b..1ea273ffa 100644
--- a/src/network/network_server.h
+++ b/src/network/network_server.h
@@ -118,7 +118,7 @@ public:
};
void NetworkServer_Tick(bool send_frame);
-void HashCurrentCompanyPassword(const char *password);
+void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
#define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
#define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)