Skip to content

Commit

Permalink
Added more debug messages
Browse files Browse the repository at this point in the history
Fixed compilation error on Linux
  • Loading branch information
danielga committed Dec 13, 2020
1 parent 75a80ee commit 7004436
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions source/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#if defined DEBUG

#include <dbg.h>
#include <Color.h>

#define _DebugMsg( ... ) Msg( __VA_ARGS__ )
#define _DebugWarning( ... ) ConColorMsg( 1, global::__yellow, __VA_ARGS__ )
Expand Down
71 changes: 55 additions & 16 deletions source/netfilter/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ namespace netfilter
static IVEngineServer *engine_server = nullptr;
static IFileSystem *filesystem = nullptr;

inline const char *IPToString( const in_addr &addr )
{
static char buffer[16] = { };
const char *str =
inet_ntop( AF_INET, const_cast<in_addr *>( &addr ), buffer, sizeof( buffer ) );
if( str == nullptr )
return "unknown";

return str;
}

static void BuildStaticReplyInfo( )
{
reply_info.game_desc = gamedll->GetGameDescription( );
Expand Down Expand Up @@ -348,32 +359,26 @@ namespace netfilter
sizeof( from )
);

_DebugWarning( "[ServerSecure] Handled %s info request using cache\n", IPToString( from.sin_addr ) );

return PacketType::Invalid; // we've handled it
}

inline PacketType HandleInfoQuery( const sockaddr_in &from )
{
const uint32_t time = static_cast<uint32_t>( Plat_FloatTime( ) );
if( !client_manager.CheckIPRate( from.sin_addr.s_addr, time ) )
{
_DebugWarning( "[ServerSecure] Client %s hit rate limit\n", IPToString( from.sin_addr ) );
return PacketType::Invalid;
}

if( info_cache_enabled )
return SendInfoCache( from, time );

return PacketType::Good;
}

inline const char *IPToString( const in_addr &addr )
{
static char buffer[16] = { };
const char *str =
inet_ntop( AF_INET, const_cast<in_addr *>( &addr ), buffer, sizeof( buffer ) );
if( str == nullptr )
return "unknown";

return str;
}

static PacketType ClassifyPacket( const uint8_t *data, int32_t len, const sockaddr_in &from )
{
if( len == 0 )
Expand All @@ -389,7 +394,7 @@ namespace netfilter
if( len < 5 )
return PacketType::Good;

int32_t channel = *reinterpret_cast<const int32_t *>( data );
const int32_t channel = *reinterpret_cast<const int32_t *>( data );
if( channel == -2 )
{
_DebugWarning(
Expand All @@ -404,7 +409,7 @@ namespace netfilter
if( channel != -1 )
return PacketType::Good;

uint8_t type = *( data + 4 );
const uint8_t type = *( data + 4 );
if( packet_validation_enabled )
{
switch( type )
Expand Down Expand Up @@ -438,12 +443,35 @@ namespace netfilter
return PacketType::Good;

case 'T': // server info request (A2S_INFO)
return ( len == 25 || len == 1200 ) && strncmp( reinterpret_cast<const char *>( data + 5 ), "Source Engine Query", 19 ) == 0 ?
PacketType::Info : PacketType::Invalid;
if( ( len != 25 && len != 1200 ) || strncmp( reinterpret_cast<const char *>( data + 5 ), "Source Engine Query", 19 ) != 0 )
{
_DebugWarning(
"[ServerSecure] Bad OOB! len: %d, channel: 0x%X, type: %c from %s\n",
len,
channel,
type,
IPToString( from.sin_addr )
);
return PacketType::Invalid;
}

return PacketType::Info;

case 'U': // player info request (A2S_PLAYER)
case 'V': // rules request (A2S_RULES)
return ( len == 9 || len == 1200 ) ? PacketType::Good : PacketType::Invalid;
if( len != 9 && len != 1200 )
{
_DebugWarning(
"[ServerSecure] Bad OOB! len: %d, channel: 0x%X, type: %c from %s\n",
len,
channel,
type,
IPToString( from.sin_addr )
);
return PacketType::Invalid;
}

return PacketType::Good;

case 'q': // connection handshake init
case 'k': // steam auth packet
Expand Down Expand Up @@ -560,6 +588,7 @@ namespace netfilter
return -1;

const ssize_t len = trampoline( s, buf, buflen, flags, from, fromlen );
_DebugWarning( "[ServerSecure] Called recvfrom on socket %d and received %d bytes\n", s, len );
if( len == -1 )
return -1;

Expand All @@ -578,6 +607,8 @@ namespace netfilter
if( !IsAddressAllowed( infrom ) )
return -1;

_DebugWarning( "[ServerSecure] Address %s was allowed\n", IPToString( infrom.sin_addr ) );

PacketType type = ClassifyPacket( buffer, len, infrom );
if( type == PacketType::Info )
type = HandleInfoQuery( infrom );
Expand All @@ -596,9 +627,12 @@ namespace netfilter
{
if( s != game_socket )
{
_DebugWarning( "[ServerSecure] recvfrom detour called with socket %d, passing through\n", s );
auto trampoline = recvfrom_hook.GetTrampoline<recvfrom_t>( );
return trampoline != nullptr ? trampoline( s, buf, buflen, flags, from, fromlen ) : -1;
}

_DebugWarning( "[ServerSecure] recvfrom detour called with socket %d, detouring\n", s );

packet_t p;
const bool has_packet = PopPacketFromQueue( p );
Expand All @@ -622,6 +656,7 @@ namespace netfilter
{
if( IsPacketQueueFull( ) )
{
_DebugWarning( "[ServerSecure] Packet queue is full, sleeping for 100ms\n" );
ThreadSleep( 100 );
continue;
}
Expand All @@ -634,6 +669,8 @@ namespace netfilter
if( res == -1 || !FD_ISSET( game_socket, &readables ) )
continue;

_DebugWarning( "[ServerSecure] Select passed\n" );

packet_t p;
p.buffer.resize( threaded_socket_max_buffer );
const ssize_t len = ReceiveAndAnalyzePacket(
Expand All @@ -647,6 +684,8 @@ namespace netfilter
if( len == -1 )
continue;

_DebugWarning( "[ServerSecure] Pushing packet to queue\n" );

p.buffer.resize( static_cast<size_t>( len ) );

PushPacketToQueue( std::move( p ) );
Expand Down

0 comments on commit 7004436

Please sign in to comment.