From cebf395939b2e7d2bae7cb4ab28823bbedd7b86c Mon Sep 17 00:00:00 2001 From: hiker Date: Wed, 10 Feb 2016 08:28:15 +1100 Subject: [PATCH] Only allow the first connected client to control the server. --- .../protocols/server_lobby_room_protocol.cpp | 1 + src/network/stk_host.cpp | 14 ++++++-------- src/network/stk_peer.cpp | 1 + src/network/stk_peer.hpp | 9 ++++++++- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index f8391c113..35a1f608d 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -499,6 +499,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) m_setup->addPlayer(profile); peer->setPlayerProfile(profile); peer->setClientServerToken(token); + peer->setHostId(new_host_id); Log::verbose("ServerLobbyRoomProtocol", "New player."); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 5b6a2659c..1c2e3969a 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -494,6 +494,8 @@ int STKHost::mustStopListening() // -------------------------------------------------------------------- /** Returns true if this instance is allowed to control the server. + * Only the first connected client (i.e. the one with host id 1) is + * allowed to control the server. Only called from a client. */ bool STKHost::isAuthorisedToControl() const { @@ -501,15 +503,13 @@ bool STKHost::isAuthorisedToControl() const // stk logic), no peer is authorised. if(m_peers.size()==0) return false; - Server *server = ServersManager::get()->getJoinedServer(); - return NetworkConfig::get()->getMyAddress().getIP() == - server->getAddress().getIP(); - + return m_host_id == 1; } // isAuthorisedToControl // ---------------------------------------------------------------------------- /** Server-side check if the client sending a command is really authorised - * to do so. + * to do so. Only the first connected client (i.e. host id = 1) is allowed + * to control the server. * \param peer Peer sending the command. */ bool STKHost::isAuthorisedToControl(const STKPeer *peer) const @@ -518,9 +518,7 @@ bool STKHost::isAuthorisedToControl(const STKPeer *peer) const // stk logic), no peer is authorised. if(m_peers.size()==0) return false; - // FIXME peer has ip 0 - return true; - return peer->getAddress()==NetworkConfig::get()->getMyAddress().getIP(); + return peer->getHostId()==1; } // isAuthorisedToControl // ---------------------------------------------------------------------------- diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index e810da871..190b7ca17 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -32,6 +32,7 @@ STKPeer::STKPeer(ENetPeer *enet_peer) m_enet_peer = enet_peer; m_player_profile = NULL; m_client_server_token = 0; + m_host_id = 0; m_token_set = false; } // STKPeer diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index 3c9bc7b30..923e73cff 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -50,6 +50,8 @@ protected: /** True if the token for this peer has been set. */ bool m_token_set; + /** Host id of this peer. */ + int m_host_id; public: STKPeer(ENetPeer *enet_peer); virtual ~STKPeer(); @@ -89,7 +91,12 @@ public: // ------------------------------------------------------------------------ /** Returns if the token for this client is known. */ bool isClientServerTokenSet() const { return m_token_set; } - + // ------------------------------------------------------------------------ + /** Sets the host if of this peer. */ + void setHostId(int host_id) { m_host_id = host_id; } + // ------------------------------------------------------------------------ + /** Returns the host id of this peer. */ + int getHostId() const { return m_host_id; } }; // STKPeer #endif // STK_PEER_HPP