Permalink
Browse files

Minor style changes

  • Loading branch information...
mbasaglia committed Feb 27, 2016
1 parent 8eb0659 commit c9902aa95de4a3cb9dcfb177181db9f4401958b4
@@ -92,6 +92,13 @@ namespace Str {
return true;
}
int GetHex(char ch) {
return ch > '9' ?
( ch >= 'a' ? ch - 'a' + 10 : ch - 'A' + 10 )
: ch - '0'
;
}
std::string ToUpper(Str::StringRef text) {
std::string res;
res.reserve(text.size());
@@ -246,13 +246,7 @@ namespace Str {
* Converts a hexadecimal character to the value of the digit it represents.
* Pre: cisxdigit(ch)
*/
inline CONSTEXPR_FUNCTION int GetHex(char ch)
{
return ch > '9' ?
( ch >= 'a' ? ch - 'a' + 10 : ch - 'A' + 10 )
: ch - '0'
;
}
int GetHex(char ch);
std::string ToUpper(Str::StringRef text);
std::string ToLower(Str::StringRef text);
@@ -1431,17 +1431,17 @@ void CL_Connect_f()
static void CL_RconSend(const Rcon::Message &message)
{
std::string invalid_reason;
if ( message.valid(&invalid_reason) )
if ( message.Valid(&invalid_reason) )
{
message.send();
message.Send();
}
else
{
Log::Notice("Invalid rcon message: %s\n", invalid_reason.c_str());
}
}
struct RconChallengeQueue
class RconChallengeQueue
{
public:
struct Request
@@ -2155,7 +2155,8 @@ void CL_CheckForResend()
Com_sprintf( data, sizeof(data), "connect %s", Cmd_QuoteString( info ) );
Net::OutOfBandData( netsrc_t::NS_CLIENT, clc.serverAddress, (byte*)data, strlen( data ) );
Net::OutOfBandData( netsrc_t::NS_CLIENT, clc.serverAddress,
reinterpret_cast<byte*>( data ), strlen( data ) );
// the most current userinfo has been sent, so watch for any
// newer changes to userinfo variables
cvar_modifiedFlags &= ~CVAR_USERINFO;
@@ -2684,7 +2685,7 @@ void CL_ConnectionlessPacket( netadr_t from, msg_t *msg )
{
return;
}
// start sending challenge repsonse instead of challenge request packets
// start sending challenge response instead of challenge request packets
clc.challenge = args.Argv(1);
cls.state = connstate_t::CA_CHALLENGING;
clc.connectPacketCount = 0;
@@ -2789,7 +2790,7 @@ void CL_ConnectionlessPacket( netadr_t from, msg_t *msg )
return;
}
Log::Debug( "Unknown connectionless packet command.\n" );
Log::Debug( "Unknown connectionless packet command." );
}
/*
@@ -104,16 +104,14 @@ namespace Encoding {
*/
Data HexEncode( const Data& input )
{
std::ostringstream stream;
stream.setf(std::ios::hex, std::ios::basefield);
stream.fill('0');
for ( auto ch : input )
Data output;
output.reserve(input.size()*2);
for ( auto byte : input )
{
stream.width(2);
stream << int( ch );
output.push_back((byte & 0xF0) >> 4);
output.push_back(byte & 0xF);
}
auto string = stream.str();
return Data(string.begin(), string.end());
return output;
}
/*
@@ -268,7 +266,7 @@ bool Aes256Decrypt( Data cypher_text, const Data& key, Data& output )
{
if ( key.size() != AES256_KEY_SIZE || cypher_text.size() % AES_BLOCK_SIZE )
{
return true;
return false;
}
aes256_ctx ctx;
@@ -91,7 +91,7 @@ namespace Crypto {
void AddPadding( Data& target, std::size_t block_size = 8 );
/*
* Encrypts using the AES256 algorthim
* Encrypts using the AES256 algorithm
* PRE: Key is 256-bit (32 octects) long
* POST: output contains the encrypted data
* Notes: plain_text will be padded using the PKCS#7 algorthm,
@@ -101,7 +101,7 @@ namespace Crypto {
bool Aes256Encrypt( Data plain_text, const Data& key, Data& output );
/*
* Encrypts using the AES256 algorthim
* Encrypts using the AES256 algorithm
* PRE: Key is 256-bit (32 octects) long,
* cypher_text.size() is an integer multiple of the AES block size
* POST: output contains the decrypted data
@@ -44,7 +44,7 @@ void NetadrToSockadr( const netadr_t *a, struct sockaddr *s );
namespace Net {
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte *format, std::size_t len )
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte *data, std::size_t len )
{
if ( len == 0 )
{
@@ -54,7 +54,7 @@ void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte *format, std::size_
std::basic_string<byte> message;
message.reserve(OOBHeader().size() + len);
message.append(OOBHeader().begin(), OOBHeader().end());
message.append(format, len);
message.append(data, len);
msg_t mbuf;
mbuf.data = &message[0];
@@ -91,15 +91,15 @@ std::string AddressToString( const netadr_t& address, bool with_port )
{
result = '[' + result + ']';
}
result += ':' + std::to_string(ntohs(ExtractPort(address)));
result += ':' + std::to_string(ntohs(GetPort(address)));
}
return result;
}
return "";
}
unsigned short ExtractPort(const netadr_t& address)
unsigned short GetPort(const netadr_t& address)
{
if ( address.type == netadrtype_t::NA_IP_DUAL )
{
@@ -48,7 +48,7 @@ void OutOfBandPrint( netsrc_t net_socket, const netadr_t& adr, Str::StringRef fo
/*
* Sends huffman-compressed data
*/
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte* format, std::size_t len );
void OutOfBandData( netsrc_t sock, const netadr_t& adr, byte* data, std::size_t len );
/*
* Converts an address to its string representation
@@ -58,7 +58,7 @@ std::string AddressToString( const netadr_t& address, bool with_port = false );
/*
* Returns port, port4 or port6 depending on the address type
*/
unsigned short ExtractPort(const netadr_t& address);
unsigned short GetPort(const netadr_t& address);
} // namespace Net
@@ -48,7 +48,7 @@ Cvar::Range<Cvar::Cvar<int>> cvar_server_secure(
"How secure the Rcon protocol should be: "
"0: Allow unencrypted rcon, "
"1: Require encryption, "
"2: Require encryption and challege check",
"2: Require encryption and challenge check",
Cvar::NONE,
0,
0,
@@ -64,51 +64,51 @@ Cvar::Cvar<std::string> cvar_client_destination(
Message::Message( const netadr_t& remote, std::string command,
Secure secure, std::string password, std::string challenge )
: secure_(secure),
challenge_(std::move(challenge)),
command_(std::move(command)),
password_(std::move(password)),
remote_(remote)
: secure(secure),
challenge(std::move(challenge)),
command(std::move(command)),
password(std::move(password)),
remote(remote)
{}
Message::Message( std::string error_message )
: error_(std::move(error_message))
: error(std::move(error_message))
{}
const std::string& Message::command() const
const std::string& Message::Command() const
{
return command_;
return command;
}
void Message::send() const
void Message::Send() const
{
if ( secure_ == Secure::Unencrypted )
if ( secure == Secure::Unencrypted )
{
Net::OutOfBandPrint(netsrc_t::NS_CLIENT, remote_, "rcon %s %s", password_, command_);
Net::OutOfBandPrint(netsrc_t::NS_CLIENT, remote, "rcon %s %s", password, command);
return;
}
std::string method = "PLAIN";
Crypto::Data key = Crypto::Hash::Sha256(Crypto::String(password_));
std::string plaintext = command_;
Crypto::Data key = Crypto::Hash::Sha256(Crypto::String(password));
std::string plaintext = command;
if ( secure_ == Secure::EncryptedChallenge )
if ( secure == Secure::EncryptedChallenge )
{
method = "CHALLENGE";
plaintext = challenge_ + ' ' + plaintext;
plaintext = challenge + ' ' + plaintext;
}
Crypto::Data cypher;
if ( Crypto::Aes256Encrypt(Crypto::String(plaintext), key, cypher) )
{
Net::OutOfBandPrint(netsrc_t::NS_CLIENT, remote_, "srcon %s %s",
Net::OutOfBandPrint(netsrc_t::NS_CLIENT, remote, "srcon %s %s",
method,
Crypto::String(Crypto::Encoding::Base64Encode(cypher))
);
}
}
bool Message::valid(std::string *invalid_reason) const
bool Message::Valid(std::string *invalid_reason) const
{
auto invalid = [invalid_reason](const char* reason)
{
@@ -117,27 +117,27 @@ bool Message::valid(std::string *invalid_reason) const
return false;
};
if ( !error_.empty() )
if ( !error.empty() )
{
return invalid(error_.c_str());
return invalid(error.c_str());
}
if ( secure_ < Secure::Unencrypted || secure_ > Secure::EncryptedChallenge )
if ( secure < Secure::Unencrypted || secure > Secure::EncryptedChallenge )
{
return invalid("Unknown secure protocol");
}
if ( password_.empty() )
if ( password.empty() )
{
return invalid("Missing password");
}
if ( command_.empty() )
if ( command.empty() )
{
return invalid("Missing command");
}
if ( secure_ == Secure::EncryptedChallenge && challenge_.empty() )
if ( secure == Secure::EncryptedChallenge && challenge.empty() )
{
return invalid("Missing challenge");
}
@@ -146,7 +146,7 @@ bool Message::valid(std::string *invalid_reason) const
}
bool Message::acceptable(std::string *invalid_reason) const
bool Message::Acceptable(std::string *invalid_reason) const
{
auto invalid = [invalid_reason](const char* reason)
{
@@ -155,12 +155,12 @@ bool Message::acceptable(std::string *invalid_reason) const
return false;
};
if ( !valid(invalid_reason) )
if ( !Valid(invalid_reason) )
{
return false;
}
if ( secure_ < Secure(cvar_server_secure.Get()) )
if ( secure < Secure(cvar_server_secure.Get()) )
{
return invalid("Weak security");
}
@@ -170,14 +170,14 @@ bool Message::acceptable(std::string *invalid_reason) const
return invalid("No rcon.server.password set on the server.");
}
if ( password_ != cvar_server_password.Get() )
if ( password != cvar_server_password.Get() )
{
return invalid("Bad password");
}
if ( secure_ == Secure::EncryptedChallenge )
if ( secure == Secure::EncryptedChallenge )
{
if ( !ChallengeManager::Get().MatchString(remote_, challenge_) )
if ( !ChallengeManager::Get().MatchString(remote, challenge) )
{
return invalid("Mismatched challenge");
}
@@ -186,7 +186,7 @@ bool Message::acceptable(std::string *invalid_reason) const
return true;
}
Message Message::decode(const netadr_t& remote, const Cmd::Args& args)
Message Message::Decode(const netadr_t& remote, const Cmd::Args& args)
{
if ( args.size() < 3 || (args[0] != "rcon" && args[0] != "srcon") )
{
@@ -59,36 +59,36 @@ class Message
/*
* Checks whether the message is valid (fits its own constraints)
*/
bool valid(std::string *invalid_reason = nullptr) const;
bool Valid(std::string *invalid_reason = nullptr) const;
/*
* Checks whether the message is acceptable by the server,
* it must be valid and match the rcon settings and challenges.
*/
bool acceptable(std::string *invalid_reason = nullptr) const;
bool Acceptable(std::string *invalid_reason = nullptr) const;
/*
* Sends the message to the remote server
*/
void send() const;
void Send() const;
/*
* Command to be executed
*/
const std::string& command() const;
const std::string& Command() const;
/*
* Decodes the arguments of an out of band message received by the server
*/
static Message decode(const netadr_t& remote, const Cmd::Args& args);
static Message Decode(const netadr_t& remote, const Cmd::Args& args);
private:
Secure secure_;
std::string challenge_;
std::string command_;
std::string password_;
netadr_t remote_;
std::string error_;
Secure secure;
std::string challenge;
std::string command;
std::string password;
netadr_t remote;
std::string error;
};
extern Cvar::Cvar<std::string> cvar_server_password;
Oops, something went wrong.

0 comments on commit c9902aa

Please sign in to comment.