Permalink
Browse files

Make ChallengeManager into a namespace

  • Loading branch information...
mbasaglia committed Feb 28, 2016
1 parent c9902aa commit 011593e5d61bf1a8943d7ea0c64e66754b265bf7
@@ -177,7 +177,7 @@ bool Message::Acceptable(std::string *invalid_reason) const
if ( secure == Secure::EncryptedChallenge )
{
if ( !ChallengeManager::Get().MatchString(remote, challenge) )
if ( !ChallengeManager::MatchString(remote, challenge) )
{
return invalid("Mismatched challenge");
}
@@ -88,8 +88,26 @@ bool Challenge::ValidString(const std::string& challenge)
return std::all_of(challenge.begin(), challenge.end(), Str::cisxdigit);
}
static std::deque<Challenge> challenges;
std::size_t ChallengeManager::MaxChallenges() const
/*
* Removes outdated challenges
* PRE: The caller has acquired a lock on mutex
*/
static void Cleanup()
{
auto now = Challenge::Clock::now();
challenges.erase(
std::remove_if( challenges.begin(), challenges.end(),
[&now]( const Challenge& challenge ) {
return !challenge.ValidAt( now );
}
),
challenges.end()
);
}
std::size_t ChallengeManager::MaxChallenges()
{
return cvar_server_challenge_count.Get();
}
@@ -103,8 +121,6 @@ std::string ChallengeManager::GenerateChallenge( const netadr_t& source )
void ChallengeManager::Push( const Challenge& challenge )
{
auto lock = Lock();
Cleanup();
if ( challenges.size() >= MaxChallenges() )
@@ -117,8 +133,6 @@ void ChallengeManager::Push( const Challenge& challenge )
bool ChallengeManager::Match( const Challenge& challenge, Challenge::Duration* ping )
{
auto lock = Lock();
Cleanup();
auto it = std::find_if( challenges.begin(), challenges.end(),
@@ -141,23 +155,9 @@ bool ChallengeManager::Match( const Challenge& challenge, Challenge::Duration* p
void ChallengeManager::Clear()
{
auto lock = Lock();
challenges.clear();
}
void ChallengeManager::Cleanup()
{
auto now = Challenge::Clock::now();
challenges.erase(
std::remove_if( challenges.begin(), challenges.end(),
[&now]( const Challenge& challenge ) {
return !challenge.ValidAt( now );
}
),
challenges.end()
);
}
bool ChallengeManager::MatchString( const netadr_t& source,
const std::string& challenge,
Challenge::Duration* ping )
@@ -135,16 +135,10 @@ class Challenge
};
class ChallengeManager
namespace ChallengeManager
{
public:
static ChallengeManager& Get()
{
static ChallengeManager singleton;
return singleton;
}
std::size_t MaxChallenges() const;
std::size_t MaxChallenges();
/*
* Generates a challenge for the given address and returns the challenge string
@@ -180,24 +174,6 @@ class ChallengeManager
const std::string& challenge,
Challenge::Duration* ping = nullptr);
private:
ChallengeManager() = default;
ChallengeManager( const ChallengeManager& ) = delete;
ChallengeManager& operator=( const ChallengeManager& ) = delete;
std::unique_lock<std::mutex> Lock()
{
return std::unique_lock<std::mutex>{mutex};
}
/*
* Removes outdated challenges
* PRE: The caller has acquired a lock on mutex
*/
void Cleanup();
std::mutex mutex;
std::deque<Challenge> challenges;
};
} // namespace ChallengeManager
#endif // CRYPTOCHALLENGE_H
@@ -47,7 +47,7 @@ void SV_GetChallenge( netadr_t from )
return;
}
auto challenge = ChallengeManager::Get().GenerateChallenge( from );
auto challenge = ChallengeManager::GenerateChallenge( from );
Net::OutOfBandPrint( netsrc_t::NS_SERVER, from, "challengeResponse %s\n", challenge );
}
@@ -110,7 +110,7 @@ void SV_DirectConnect( netadr_t from, const Cmd::Args& args )
{
// see if the challenge is valid (local clients don't need to challenge)
Challenge::Duration ping_duration;
if ( !ChallengeManager::Get().MatchString( from, challenge, &ping_duration ) )
if ( !ChallengeManager::MatchString( from, challenge, &ping_duration ) )
{
Net::OutOfBandPrint( netsrc_t::NS_SERVER, from, "print\n[err_dialog]No or bad challenge for address." );
return;
@@ -793,7 +793,7 @@ void SV_Shutdown( const char *finalmsg )
memset( &svs, 0, sizeof( svs ) );
svs.serverLoad = -1;
ChallengeManager::Get().Clear();
ChallengeManager::Clear();
Cvar_Set( "sv_running", "0" );
#ifndef BUILD_SERVER

0 comments on commit 011593e

Please sign in to comment.