From 1b5cf1bfb8099406cf64999b7971d9c77dff1035 Mon Sep 17 00:00:00 2001 From: Kai Philipp Date: Tue, 19 Jul 2016 14:01:13 +0200 Subject: [PATCH] guest IP-check and number of guest in lobby check combined now in one function --- src/net/common/serverlobbythread.cpp | 12 +++------ src/net/common/sessionmanager.cpp | 40 ++++++++++------------------ src/net/sessionmanager.h | 3 +-- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 084f1d71..72451256 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -65,7 +65,6 @@ #define SERVER_MAX_NUM_LOBBY_SESSIONS 512 // Maximum number of idle users in lobby. #define SERVER_MAX_NUM_TOTAL_SESSIONS 2000 // Total maximum of sessions, fitting a 2048 handle limit -#define SERVER_MAX_GUEST_USERS 100 // LG: Maximum number of guests users allowed #define SERVER_SAVE_STATISTICS_INTERVAL_SEC 60 #define SERVER_CHECK_SESSION_TIMEOUTS_INTERVAL_MSEC 500 @@ -1051,7 +1050,7 @@ ServerLobbyThread::HandleNetPacketInit(boost::shared_ptr session, c bool validGuest = false; // productive: if (initMessage.login() == InitMessage::guestLogin) { // debug: if (initMessage.login() == InitMessage::unauthenticatedLogin) { - if (initMessage.login() == InitMessage::guestLogin) { + if (initMessage.login() == InitMessage::unauthenticatedLogin) { playerName = initMessage.nickname(); // Verify guest player name. if (playerName.length() > sizeof(SERVER_GUEST_PLAYER_NAME - 1) @@ -1061,12 +1060,9 @@ ServerLobbyThread::HandleNetPacketInit(boost::shared_ptr session, c validGuest = true; noAuth = true; } - // check if a guest session with same ip is already connected - decline if true - if(m_sessionManager.IsGuestConnectedMultiple(session->GetClientAddr())) { - SessionError(session, ERR_NET_SERVER_FULL); - return; - } - if (m_sessionManager.GetGuestsCount() + m_gameSessionManager.GetGuestsCount() > SERVER_MAX_GUEST_USERS) { + // check if a guest session in lobby with same ip is already connected and + // if number of lobby guests >= SERVER_MAX_GUEST_USERS_LOBBY + if(!m_sessionManager.IsGuestAllowedToConnect(session->GetClientAddr())) { SessionError(session, ERR_NET_SERVER_FULL); return; } diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index a41fa0cc..5c68ed8f 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -34,8 +34,11 @@ #include #include +#include // debug - remove me + using namespace std; +#define SERVER_MAX_GUEST_USERS_LOBBY 50 // LG: Maximum number of guests users in lobby allowed SessionManager::SessionManager() { @@ -248,23 +251,25 @@ SessionManager::IsClientAddressConnected(const std::string &clientAddress) const } bool -SessionManager::IsGuestConnectedMultiple(const std::string &clientAddress) const +SessionManager::IsGuestAllowedToConnect(const std::string &clientAddress) const { - bool retVal = false; + bool retVal = true; boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); SessionMap::const_iterator i = m_sessionMap.begin(); SessionMap::const_iterator end = m_sessionMap.end(); + int num = 0; while (i != end) { boost::shared_ptr tmpPlayer(i->second->GetPlayerData()); - // productive: - //if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_GUEST && i->second->GetClientAddr() == clientAddress){ - // debug: - // if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_NORMAL && i->second->GetClientAddr() == clientAddress){ - if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_GUEST && i->second->GetClientAddr() == clientAddress) { - retVal = true; - break; + if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_GUEST){ + num++; + if(i->second->GetClientAddr() == clientAddress || num >= SERVER_MAX_GUEST_USERS_LOBBY) { + // guest has same ip as another guest in lobby or + // number of guests in lobby >= SERVER_MAX_GUEST_USERS_LOBBY => not allowed + retVal = false; + break; + } } ++i; } @@ -433,20 +438,3 @@ SessionManager::SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr< } } -unsigned -SessionManager::GetGuestsCount() const -{ - unsigned counter = 0; - boost::recursive_mutex::scoped_lock lock(m_sessionMapMutex); - - SessionMap::const_iterator i = m_sessionMap.begin(); - SessionMap::const_iterator end = m_sessionMap.end(); - - while (i != end) { - if (i->second && i->second->GetPlayerData() && - i->second->GetPlayerData()->GetRights() == PLAYER_RIGHTS_GUEST) - ++counter; - ++i; - } - return counter; -} diff --git a/src/net/sessionmanager.h b/src/net/sessionmanager.h index da983a7a..36f7fb4f 100644 --- a/src/net/sessionmanager.h +++ b/src/net/sessionmanager.h @@ -64,7 +64,7 @@ public: bool IsPlayerConnected(const std::string &playerName) const; bool IsPlayerConnected(unsigned uniqueId) const; bool IsClientAddressConnected(const std::string &clientAddress) const; - bool IsGuestConnectedMultiple(const std::string &clientAddress) const; + bool IsGuestAllowedToConnect(const std::string &clientAddress) const; void ForEach(boost::function)> func); @@ -75,7 +75,6 @@ public: unsigned GetRawSessionCount() const; unsigned GetSessionCountWithState(int state) const; bool HasSessionWithState(int state) const; - unsigned GetGuestsCount() const; void SendToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state); void SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr packet, int state);