From bb96e77b20e9e4b9ff1e8827f2614732d3fd117c Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 28 Sep 2017 00:27:50 +0100 Subject: [PATCH] Fixed some compilation warnings and code formatting. --- source/filecheck.cpp | 276 ++++--- source/filecheck.hpp | 6 +- source/main.cpp | 95 ++- source/main.hpp | 13 +- source/netfilter/core.cpp | 1424 +++++++++++++++++++------------------ source/netfilter/core.hpp | 6 +- 6 files changed, 915 insertions(+), 905 deletions(-) diff --git a/source/filecheck.cpp b/source/filecheck.cpp index 5f547cd..c14bf62 100644 --- a/source/filecheck.cpp +++ b/source/filecheck.cpp @@ -14,189 +14,187 @@ namespace filecheck { + enum ValidationMode + { + ValidationModeNone, + ValidationModeFixed, + ValidationModeLua + }; -enum ValidationMode -{ - ValidationModeNone, - ValidationModeFixed, - ValidationModeLua -}; - -typedef bool ( *CNetChan__IsValidFileForTransfer_t )( const char *filepath ); + typedef bool( *CNetChan__IsValidFileForTransfer_t )( const char *filepath ); #if defined SYSTEM_WINDOWS -static const char CNetChan__IsValidFileForTransfer_sig[] = - "\x55\x8B\xEC\x53\x8B\x5D\x08\x85\xDB\x0F\x84\x2A\x2A\x2A\x2A\x80\x3B"; -static const size_t CNetChan__IsValidFileForTransfer_siglen = - sizeof( CNetChan__IsValidFileForTransfer_sig ) - 1; + static const char CNetChan__IsValidFileForTransfer_sig[] = + "\x55\x8B\xEC\x53\x8B\x5D\x08\x85\xDB\x0F\x84\x2A\x2A\x2A\x2A\x80\x3B"; + static const size_t CNetChan__IsValidFileForTransfer_siglen = + sizeof( CNetChan__IsValidFileForTransfer_sig ) - 1; #elif defined SYSTEM_LINUX -static const char CNetChan__IsValidFileForTransfer_sig[] = - "@_ZN8CNetChan22IsValidFileForTransferEPKc"; -static const size_t CNetChan__IsValidFileForTransfer_siglen = 0; + static const char CNetChan__IsValidFileForTransfer_sig[] = + "@_ZN8CNetChan22IsValidFileForTransferEPKc"; + static const size_t CNetChan__IsValidFileForTransfer_siglen = 0; #elif defined SYSTEM_MACOSX -static const char CNetChan__IsValidFileForTransfer_sig[] = - "@__ZN8CNetChan22IsValidFileForTransferEPKc"; -static const size_t CNetChan__IsValidFileForTransfer_siglen = 0; + static const char CNetChan__IsValidFileForTransfer_sig[] = + "@__ZN8CNetChan22IsValidFileForTransferEPKc"; + static const size_t CNetChan__IsValidFileForTransfer_siglen = 0; #endif -static CNetChan__IsValidFileForTransfer_t CNetChan__IsValidFileForTransfer_original = nullptr; -static const char file_hook_name[] = "IsValidFileForTransfer"; -static const char downloads_dir[] = "downloads" CORRECT_PATH_SEPARATOR_S; -static ValidationMode validation_mode = ValidationModeNone; -static GarrysMod::Lua::ILuaInterface *lua_interface = nullptr; -static INetworkStringTable *downloads = nullptr; -static Detouring::Hook hook; + static CNetChan__IsValidFileForTransfer_t CNetChan__IsValidFileForTransfer_original = nullptr; + static const char file_hook_name[] = "IsValidFileForTransfer"; + static const char downloads_dir[] = "downloads" CORRECT_PATH_SEPARATOR_S; + static ValidationMode validation_mode = ValidationModeNone; + static GarrysMod::Lua::ILuaInterface *lua_interface = nullptr; + static INetworkStringTable *downloads = nullptr; + static Detouring::Hook hook; -inline bool SetFileDetourStatus( ValidationMode mode ) -{ - if( mode != ValidationModeNone ? hook.Enable( ) : hook.Disable( ) ) + inline bool SetFileDetourStatus( ValidationMode mode ) { - validation_mode = mode; - return true; - } - - return false; -} - -LUA_FUNCTION_STATIC( EnableFileValidation ) -{ - if( LUA->Top( ) < 1 ) - LUA->ArgError( 1, "boolean or number expected, got nil" ); + if( mode != ValidationModeNone ? hook.Enable( ) : hook.Disable( ) ) + { + validation_mode = mode; + return true; + } - ValidationMode mode = ValidationModeFixed; - if( LUA->IsType( 1, GarrysMod::Lua::Type::BOOL ) ) - { - mode = LUA->GetBool( 1 ) ? ValidationModeFixed : ValidationModeNone; + return false; } - else if( LUA->IsType( 1, GarrysMod::Lua::Type::NUMBER ) ) - { - int32_t num = static_cast( LUA->GetNumber( 1 ) ); - if( num < 0 || num > 2 ) - LUA->ArgError( 1, "invalid mode value, must be 0, 1 or 2" ); - mode = static_cast( num ); - } - else + LUA_FUNCTION_STATIC( EnableFileValidation ) { - LUA->ArgError( 1, "boolean or number expected" ); - } + if( LUA->Top( ) < 1 ) + LUA->ArgError( 1, "boolean or number expected, got nil" ); - LUA->PushBool( SetFileDetourStatus( mode ) ); - return 1; -} + ValidationMode mode = ValidationModeFixed; + if( LUA->IsType( 1, GarrysMod::Lua::Type::BOOL ) ) + { + mode = LUA->GetBool( 1 ) ? ValidationModeFixed : ValidationModeNone; + } + else if( LUA->IsType( 1, GarrysMod::Lua::Type::NUMBER ) ) + { + int32_t num = static_cast( LUA->GetNumber( 1 ) ); + if( num < 0 || num > 2 ) + LUA->ArgError( 1, "invalid mode value, must be 0, 1 or 2" ); -inline bool Call( const char *filepath ) -{ - return hook.GetTrampoline( )( filepath ); -} + mode = static_cast( num ); + } + else + { + LUA->ArgError( 1, "boolean or number expected" ); + } -inline bool BlockDownload( const char *filepath ) -{ - DebugWarning( "[ServerSecure] Blocking download of \"%s\"\n", filepath ); - return false; -} + LUA->PushBool( SetFileDetourStatus( mode ) ); + return 1; + } -static bool CNetChan_IsValidFileForTransfer_detour( const char *filepath ) -{ - if( filepath == nullptr ) - return BlockDownload( - "[ServerSecure] Invalid file to download (string pointer was NULL)\n" - ); - - std::string nicefile( filepath ); - if( nicefile.empty( ) ) - return BlockDownload( - "[ServerSecure] Invalid file to download (path length was 0)\n" - ); - - if( validation_mode == ValidationModeLua ) + inline bool Call( const char *filepath ) { - if( !LuaHelpers::PushHookRun( lua_interface, file_hook_name ) ) - return Call( filepath ); + return hook.GetTrampoline( )( filepath ); + } - lua_interface->PushString( filepath ); + inline bool BlockDownload( const char *filepath ) + { + DebugWarning( "[ServerSecure] Blocking download of \"%s\"\n", filepath ); + return false; + } - bool valid = true; - if( LuaHelpers::CallHookRun( lua_interface, 1, 1 ) ) + static bool CNetChan_IsValidFileForTransfer_detour( const char *filepath ) + { + if( filepath == nullptr ) + return BlockDownload( + "[ServerSecure] Invalid file to download (string pointer was NULL)\n" + ); + + std::string nicefile( filepath ); + if( nicefile.empty( ) ) + return BlockDownload( + "[ServerSecure] Invalid file to download (path length was 0)\n" + ); + + if( validation_mode == ValidationModeLua ) { - if( lua_interface->IsType( -1, GarrysMod::Lua::Type::BOOL ) ) - valid = lua_interface->GetBool( -1 ); + if( !LuaHelpers::PushHookRun( lua_interface, file_hook_name ) ) + return Call( filepath ); - lua_interface->Pop( 1 ); - } + lua_interface->PushString( filepath ); - return valid; - } + bool valid = true; + if( LuaHelpers::CallHookRun( lua_interface, 1, 1 ) ) + { + if( lua_interface->IsType( -1, GarrysMod::Lua::Type::BOOL ) ) + valid = lua_interface->GetBool( -1 ); - if( !V_RemoveDotSlashes( &nicefile[0] ) ) - return BlockDownload( filepath ); + lua_interface->Pop( 1 ); + } - nicefile.resize( std::strlen( nicefile.c_str( ) ) ); - filepath = nicefile.c_str( ); + return valid; + } - DebugWarning( "[ServerSecure] Checking file \"%s\"\n", filepath ); + if( !V_RemoveDotSlashes( &nicefile[0] ) ) + return BlockDownload( filepath ); - if( !Call( filepath ) ) - return BlockDownload( filepath ); + nicefile.resize( std::strlen( nicefile.c_str( ) ) ); + filepath = nicefile.c_str( ); - int32_t index = downloads->FindStringIndex( filepath ); - if( index != INVALID_STRING_INDEX ) - return true; + DebugWarning( "[ServerSecure] Checking file \"%s\"\n", filepath ); - if( nicefile.size( ) == 22 && - std::strncmp( filepath, downloads_dir, 10 ) == 0 && - std::strncmp( filepath + nicefile.size( ) - 4, ".dat", 4 ) == 0 ) - return true; + if( !Call( filepath ) ) + return BlockDownload( filepath ); - return BlockDownload( filepath ); -} + int32_t index = downloads->FindStringIndex( filepath ); + if( index != INVALID_STRING_INDEX ) + return true; -void Initialize( GarrysMod::Lua::ILuaBase *LUA ) -{ - lua_interface = static_cast( LUA ); + if( nicefile.size( ) == 22 && + std::strncmp( filepath, downloads_dir, 10 ) == 0 && + std::strncmp( filepath + nicefile.size( ) - 4, ".dat", 4 ) == 0 ) + return true; - { - SymbolFinder symfinder; - - CNetChan__IsValidFileForTransfer_original = - reinterpret_cast( symfinder.ResolveOnBinary( - global::engine_binary.c_str( ), - CNetChan__IsValidFileForTransfer_sig, - CNetChan__IsValidFileForTransfer_siglen - ) ); + return BlockDownload( filepath ); } - if( CNetChan__IsValidFileForTransfer_original == nullptr ) - LUA->ThrowError( "unable to find CNetChan::IsValidFileForTransfer" ); + void Initialize( GarrysMod::Lua::ILuaBase *LUA ) + { + lua_interface = static_cast( LUA ); - if( !hook.Create( reinterpret_cast( CNetChan__IsValidFileForTransfer_original ), - reinterpret_cast( &CNetChan_IsValidFileForTransfer_detour ) ) ) - LUA->ThrowError( "unable to create detour for CNetChan::IsValidFileForTransfer" ); + { + SymbolFinder symfinder; + + CNetChan__IsValidFileForTransfer_original = + reinterpret_cast( symfinder.ResolveOnBinary( + global::engine_binary.c_str( ), + CNetChan__IsValidFileForTransfer_sig, + CNetChan__IsValidFileForTransfer_siglen + ) ); + } - INetworkStringTableContainer *networkstringtable = - global::engine_loader.GetInterface( - INTERFACENAME_NETWORKSTRINGTABLESERVER - ); - if( networkstringtable == nullptr ) - LUA->ThrowError( "unable to get INetworkStringTableContainer" ); + if( CNetChan__IsValidFileForTransfer_original == nullptr ) + LUA->ThrowError( "unable to find CNetChan::IsValidFileForTransfer" ); - downloads = networkstringtable->FindTable( "downloadables" ); - if( downloads == nullptr ) - LUA->ThrowError( "missing \"downloadables\" string table" ); + if( !hook.Create( reinterpret_cast( CNetChan__IsValidFileForTransfer_original ), + reinterpret_cast( &CNetChan_IsValidFileForTransfer_detour ) ) ) + LUA->ThrowError( "unable to create detour for CNetChan::IsValidFileForTransfer" ); - LUA->PushCFunction( EnableFileValidation ); - LUA->SetField( -2, "EnableFileValidation" ); -} + INetworkStringTableContainer *networkstringtable = + global::engine_loader.GetInterface( + INTERFACENAME_NETWORKSTRINGTABLESERVER + ); + if( networkstringtable == nullptr ) + LUA->ThrowError( "unable to get INetworkStringTableContainer" ); -void Deinitialize( GarrysMod::Lua::ILuaBase * ) -{ - hook.Disable( ); -} + downloads = networkstringtable->FindTable( "downloadables" ); + if( downloads == nullptr ) + LUA->ThrowError( "missing \"downloadables\" string table" ); + + LUA->PushCFunction( EnableFileValidation ); + LUA->SetField( -2, "EnableFileValidation" ); + } + void Deinitialize( GarrysMod::Lua::ILuaBase * ) + { + hook.Disable( ); + } } diff --git a/source/filecheck.hpp b/source/filecheck.hpp index a33c06d..0cc22d2 100644 --- a/source/filecheck.hpp +++ b/source/filecheck.hpp @@ -12,8 +12,6 @@ namespace GarrysMod namespace filecheck { - -void Initialize( GarrysMod::Lua::ILuaBase *LUA ); -void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ); - + void Initialize( GarrysMod::Lua::ILuaBase *LUA ); + void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ); } diff --git a/source/main.cpp b/source/main.cpp index 3a6004b..9a9f71b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,83 +11,82 @@ namespace global #if defined SYSTEM_WINDOWS -static const char IServer_sig[] = - "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xD8\x6D\x24\x83\x4D\xEC\x10"; -static const size_t IServer_siglen = sizeof( IServer_sig ) - 1; + static const char IServer_sig[] = + "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xD8\x6D\x24\x83\x4D\xEC\x10"; + static const size_t IServer_siglen = sizeof( IServer_sig ) - 1; #elif defined SYSTEM_LINUX -static const char IServer_sig[] = "@sv"; -static const size_t IServer_siglen = 0; + static const char IServer_sig[] = "@sv"; + static const size_t IServer_siglen = 0; #elif defined SYSTEM_MACOSX -static const char IServer_sig[] = "@_sv"; -static const size_t IServer_siglen = 0; + static const char IServer_sig[] = "@_sv"; + static const size_t IServer_siglen = 0; #endif -SourceSDK::FactoryLoader engine_loader( "engine", false, true, "bin/" ); -std::string engine_binary = Helpers::GetBinaryFileName( "engine", false, true, "bin/" ); -IServer *server = nullptr; + SourceSDK::FactoryLoader engine_loader( "engine", false, true, "bin/" ); + std::string engine_binary = Helpers::GetBinaryFileName( "engine", false, true, "bin/" ); + IServer *server = nullptr; -LUA_FUNCTION_STATIC( GetClientCount ) -{ - LUA->PushNumber( server->GetClientCount( ) ); - return 1; -} + LUA_FUNCTION_STATIC( GetClientCount ) + { + LUA->PushNumber( server->GetClientCount( ) ); + return 1; + } -static void PreInitialize( GarrysMod::Lua::ILuaBase *LUA ) -{ + static void PreInitialize( GarrysMod::Lua::ILuaBase *LUA ) { - SymbolFinder symfinder; + { + SymbolFinder symfinder; - server = + server = #if defined SYSTEM_POSIX - reinterpret_cast + reinterpret_cast #else - *reinterpret_cast + *reinterpret_cast #endif - ( symfinder.ResolveOnBinary( - engine_binary.c_str( ), - IServer_sig, - IServer_siglen - ) ); - } - - if( server == nullptr ) - LUA->ThrowError( "failed to locate IServer" ); + ( symfinder.ResolveOnBinary( + engine_binary.c_str( ), + IServer_sig, + IServer_siglen + ) ); + } - LUA->CreateTable( ); + if( server == nullptr ) + LUA->ThrowError( "failed to locate IServer" ); - LUA->PushString( "serversecure 1.5.16" ); - LUA->SetField( -2, "Version" ); + LUA->CreateTable( ); - // version num follows LuaJIT style, xxyyzz - LUA->PushNumber( 10516 ); - LUA->SetField( -2, "VersionNum" ); + LUA->PushString( "serversecure 1.5.17" ); + LUA->SetField( -2, "Version" ); - LUA->PushCFunction( GetClientCount ); - LUA->SetField( -2, "GetClientCount" ); -} + // version num follows LuaJIT style, xxyyzz + LUA->PushNumber( 10517 ); + LUA->SetField( -2, "VersionNum" ); -static void Initialize( GarrysMod::Lua::ILuaBase *LUA ) -{ - LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "serversecure" ); -} + LUA->PushCFunction( GetClientCount ); + LUA->SetField( -2, "GetClientCount" ); + } -static void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ) -{ - LUA->PushNil( ); - LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "serversecure" ); -} + static void Initialize( GarrysMod::Lua::ILuaBase *LUA ) + { + LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "serversecure" ); + } + static void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ) + { + LUA->PushNil( ); + LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "serversecure" ); + } } GMOD_MODULE_OPEN( ) diff --git a/source/main.hpp b/source/main.hpp index 84bc13d..044976d 100644 --- a/source/main.hpp +++ b/source/main.hpp @@ -13,8 +13,8 @@ #else -#define DebugMsg( arg, ... ) (void)arg -#define DebugWarning( arg, ... ) (void)arg +#define DebugMsg( arg, ... ) (void)( arg, __VA_ARGS__ ) +#define DebugWarning( arg, ... ) (void)( arg, __VA_ARGS__ ) #endif @@ -22,14 +22,13 @@ class IServer; namespace global { - -extern SourceSDK::FactoryLoader engine_loader; -extern std::string engine_binary; -extern IServer *server; + extern SourceSDK::FactoryLoader engine_loader; + extern std::string engine_binary; + extern IServer *server; #if defined DEBUG -static Color __yellow( 255, 255, 0, 255 ); + static Color __yellow( 255, 255, 0, 255 ); #endif diff --git a/source/netfilter/core.cpp b/source/netfilter/core.cpp index d8dd6c2..74e0263 100644 --- a/source/netfilter/core.cpp +++ b/source/netfilter/core.cpp @@ -23,6 +23,7 @@ #define WIN32_LEAN_AND_MEAN #include +#include #include #include @@ -98,357 +99,369 @@ namespace netfilter #endif -typedef int32_t ( *Hook_recvfrom_t )( - int32_t s, - char *buf, - int32_t buflen, - int32_t flags, - sockaddr *from, - int32_t *fromlen -); + typedef int32_t( *Hook_recvfrom_t )( + int32_t s, + char *buf, + int32_t buflen, + int32_t flags, + sockaddr *from, + int32_t *fromlen + ); -struct packet_t -{ - packet_t( ) : - address( ), - address_size( sizeof( address ) ) - { } + struct packet_t + { + packet_t( ) : + address( ), + address_size( sizeof( address ) ) + { } + + sockaddr_in address; + int32_t address_size; + std::vector buffer; + }; - sockaddr_in address; - int32_t address_size; - std::vector buffer; -}; + struct netsocket_t + { + int32_t nPort; + bool bListening; + int32_t hUDP; + int32_t hTCP; + }; -struct netsocket_t -{ - int32_t nPort; - bool bListening; - int32_t hUDP; - int32_t hTCP; -}; + struct reply_info_t + { + std::string game_dir; + std::string game_version; + std::string game_desc; + int32_t max_clients; + int32_t udp_port; + std::string tags; + }; -struct reply_info_t -{ - std::string game_dir; - std::string game_version; - std::string game_desc; - int32_t max_clients; - int32_t udp_port; - std::string tags; -}; - -enum PacketType -{ - PacketTypeInvalid = -1, - PacketTypeGood, - PacketTypeInfo -}; + enum PacketType + { + PacketTypeInvalid = -1, + PacketTypeGood, + PacketTypeInfo + }; -class CSteamGameServerAPIContext -{ -public: - ISteamClient *m_pSteamClient; - ISteamGameServer *m_pSteamGameServer; - ISteamUtils *m_pSteamGameServerUtils; - ISteamNetworking *m_pSteamGameServerNetworking; - ISteamGameServerStats *m_pSteamGameServerStats; - ISteamHTTP *m_pSteamHTTP; - ISteamInventory *m_pSteamInventory; - ISteamUGC *m_pSteamUGC; - ISteamApps *m_pSteamApps; -}; - -typedef CUtlVector netsockets_t; + class CSteamGameServerAPIContext + { + public: + ISteamClient *m_pSteamClient; + ISteamGameServer *m_pSteamGameServer; + ISteamUtils *m_pSteamGameServerUtils; + ISteamNetworking *m_pSteamGameServerNetworking; + ISteamGameServerStats *m_pSteamGameServerStats; + ISteamHTTP *m_pSteamHTTP; + ISteamInventory *m_pSteamInventory; + ISteamUGC *m_pSteamUGC; + ISteamApps *m_pSteamApps; + }; + + typedef CUtlVector netsockets_t; #if defined SYSTEM_WINDOWS -static const char SteamGameServerAPIContext_sym[] = - "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x6A\x00\x68\x2A\x2A\x2A\x2A\xFF\x55\x08\x83\xC4\x08\xA3"; -static const size_t SteamGameServerAPIContext_symlen = - sizeof( SteamGameServerAPIContext_sym ) - 1; + static const char SteamGameServerAPIContext_sym[] = + "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x6A\x00\x68\x2A\x2A\x2A\x2A\xFF\x55\x08\x83\xC4\x08\xA3"; + static const size_t SteamGameServerAPIContext_symlen = + sizeof( SteamGameServerAPIContext_sym ) - 1; -static const char FileSystemFactory_sym[] = - "\x55\x8B\xEC\x68\x2A\x2A\x2A\x2A\xFF\x75\x08\xE8"; -static const size_t FileSystemFactory_symlen = sizeof( FileSystemFactory_sym ) - 1; + static const char FileSystemFactory_sym[] = + "\x55\x8B\xEC\x68\x2A\x2A\x2A\x2A\xFF\x75\x08\xE8"; + static const size_t FileSystemFactory_symlen = sizeof( FileSystemFactory_sym ) - 1; -static const char g_pFullFileSystem_sym[] = "@g_pFullFileSystem"; -static const size_t g_pFullFileSystem_symlen = 0; + static const char g_pFullFileSystem_sym[] = "@g_pFullFileSystem"; + static const size_t g_pFullFileSystem_symlen = 0; -static const char net_sockets_sig[] = - "\x2A\x2A\x2A\x2A\x80\x7E\x04\x00\x0F\x84\x2A\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\xC7\x45\xF8\x10"; -static size_t net_sockets_siglen = sizeof( net_sockets_sig ) - 1; + static const char net_sockets_sig[] = + "\x2A\x2A\x2A\x2A\x80\x7E\x04\x00\x0F\x84\x2A\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\xC7\x45\xF8\x10"; + static size_t net_sockets_siglen = sizeof( net_sockets_sig ) - 1; -static const char operating_system_char = 'w'; + static const char operating_system_char = 'w'; #elif defined SYSTEM_LINUX -static const char SteamGameServerAPIContext_sym[] = "@_ZL27s_SteamGameServerAPIContext"; -static const size_t SteamGameServerAPIContext_symlen = 0; + static const char SteamGameServerAPIContext_sym[] = "@_ZL27s_SteamGameServerAPIContext"; + static const size_t SteamGameServerAPIContext_symlen = 0; -static const char FileSystemFactory_sym[] = "@_Z17FileSystemFactoryPKcPi"; -static const size_t FileSystemFactory_symlen = 0; + static const char FileSystemFactory_sym[] = "@_Z17FileSystemFactoryPKcPi"; + static const size_t FileSystemFactory_symlen = 0; -static const char g_pFullFileSystem_sym[] = "@g_pFullFileSystem"; -static const size_t g_pFullFileSystem_symlen = 0; + static const char g_pFullFileSystem_sym[] = "@g_pFullFileSystem"; + static const size_t g_pFullFileSystem_symlen = 0; -static const char net_sockets_sig[] = "@_ZL11net_sockets"; -static const size_t net_sockets_siglen = 0; + static const char net_sockets_sig[] = "@_ZL11net_sockets"; + static const size_t net_sockets_siglen = 0; -static const char operating_system_char = 'l'; + static const char operating_system_char = 'l'; -typedef int SOCKET; + typedef int SOCKET; -static const SOCKET INVALID_SOCKET = -1; + static const SOCKET INVALID_SOCKET = -1; #elif defined SYSTEM_MACOSX -static const char SteamGameServerAPIContext_sym[] = "@__ZL27s_SteamGameServerAPIContext"; -static const size_t SteamGameServerAPIContext_symlen = 0; + static const char SteamGameServerAPIContext_sym[] = "@__ZL27s_SteamGameServerAPIContext"; + static const size_t SteamGameServerAPIContext_symlen = 0; -static const char FileSystemFactory_sym[] = "@__Z17FileSystemFactoryPKcPi"; -static const size_t FileSystemFactory_symlen = 0; + static const char FileSystemFactory_sym[] = "@__Z17FileSystemFactoryPKcPi"; + static const size_t FileSystemFactory_symlen = 0; -static const char g_pFullFileSystem_sym[] = "@_g_pFullFileSystem"; -static const size_t g_pFullFileSystem_symlen = 0; + static const char g_pFullFileSystem_sym[] = "@_g_pFullFileSystem"; + static const size_t g_pFullFileSystem_symlen = 0; -static const char net_sockets_sig[] = "@__ZL11net_sockets"; -static const size_t net_sockets_siglen = 0; + static const char net_sockets_sig[] = "@__ZL11net_sockets"; + static const size_t net_sockets_siglen = 0; -static const char operating_system_char = 'm'; + static const char operating_system_char = 'm'; -typedef int SOCKET; + typedef int SOCKET; -static const SOCKET INVALID_SOCKET = -1; + static const SOCKET INVALID_SOCKET = -1; #endif -static std::string server_binary = - Helpers::GetBinaryFileName( "server", false, true, "garrysmod/bin/" ); -static CSteamGameServerAPIContext *gameserver_context = nullptr; + static std::string server_binary = + Helpers::GetBinaryFileName( "server", false, true, "garrysmod/bin/" ); + static CSteamGameServerAPIContext *gameserver_context = nullptr; -static SourceSDK::FactoryLoader icvar_loader( "vstdlib", true, IS_SERVERSIDE, "bin/" ); -static ConVar *sv_visiblemaxplayers = nullptr; + static SourceSDK::FactoryLoader icvar_loader( "vstdlib", true, IS_SERVERSIDE, "bin/" ); + static ConVar *sv_visiblemaxplayers = nullptr; -static std::string dedicated_binary = - Helpers::GetBinaryFileName( "dedicated", false, true, "bin/" ); -static SourceSDK::FactoryLoader server_loader( "server", false, true, "garrysmod/bin/" ); + static std::string dedicated_binary = + Helpers::GetBinaryFileName( "dedicated", false, true, "bin/" ); + static SourceSDK::FactoryLoader server_loader( "server", false, true, "garrysmod/bin/" ); -static Hook_recvfrom_t Hook_recvfrom = VCRHook_recvfrom; -static SOCKET game_socket = INVALID_SOCKET; + static Hook_recvfrom_t Hook_recvfrom = VCRHook_recvfrom; + static SOCKET game_socket = INVALID_SOCKET; -static bool packet_validation_enabled = false; + static bool packet_validation_enabled = false; -static bool firewall_whitelist_enabled = false; -static set_uint32 firewall_whitelist; + static bool firewall_whitelist_enabled = false; + static set_uint32 firewall_whitelist; -static bool firewall_blacklist_enabled = false; -static set_uint32 firewall_blacklist; + static bool firewall_blacklist_enabled = false; + static set_uint32 firewall_blacklist; -static const size_t threaded_socket_max_queue = 1000; -static AtomicBool threaded_socket_enabled( false ); -static AtomicBool threaded_socket_execute( true ); -static ThreadHandle_t threaded_socket_handle = nullptr; -static std::queue threaded_socket_queue; -static CThreadFastMutex threaded_socket_mutex; + static const size_t threaded_socket_max_queue = 1000; + static AtomicBool threaded_socket_enabled( false ); + static AtomicBool threaded_socket_execute( true ); + static ThreadHandle_t threaded_socket_handle = nullptr; + static std::queue threaded_socket_queue; + static CThreadFastMutex threaded_socket_mutex; -static const char *default_game_version = "16.12.01"; -static const uint8_t default_proto_version = 17; -static bool info_cache_enabled = false; -static reply_info_t reply_info; -static char info_cache_buffer[1024] = { 0 }; -static bf_write info_cache_packet( info_cache_buffer, sizeof( info_cache_buffer ) ); -static uint32_t info_cache_last_update = 0; -static uint32_t info_cache_time = 5; + static const char *default_game_version = "16.12.01"; + static const uint8_t default_proto_version = 17; + static bool info_cache_enabled = false; + static reply_info_t reply_info; + static char info_cache_buffer[1024] = { 0 }; + static bf_write info_cache_packet( info_cache_buffer, sizeof( info_cache_buffer ) ); + static uint32_t info_cache_last_update = 0; + static uint32_t info_cache_time = 5; -static ClientManager client_manager; + static ClientManager client_manager; -static const size_t packet_sampling_max_queue = 50; -static AtomicBool packet_sampling_enabled( false ); -static std::deque packet_sampling_queue; -static CThreadFastMutex packet_sampling_mutex; + static const size_t packet_sampling_max_queue = 50; + static AtomicBool packet_sampling_enabled( false ); + static std::deque packet_sampling_queue; + static CThreadFastMutex packet_sampling_mutex; -static CGlobalVars *globalvars = nullptr; -static IServerGameDLL *gamedll = nullptr; -static IVEngineServer *engine_server = nullptr; -static IFileSystem *filesystem = nullptr; - -static void BuildStaticReplyInfo( ) -{ - reply_info.game_desc = gamedll->GetGameDescription( ); + static CGlobalVars *globalvars = nullptr; + static IServerGameDLL *gamedll = nullptr; + static IVEngineServer *engine_server = nullptr; + static IFileSystem *filesystem = nullptr; + static void BuildStaticReplyInfo( ) { - reply_info.game_dir.resize( 256 ); - engine_server->GetGameDir( &reply_info.game_dir[0], reply_info.game_dir.size( ) ); - reply_info.game_dir.resize( strlen( reply_info.game_dir.c_str( ) ) ); + reply_info.game_desc = gamedll->GetGameDescription( ); - size_t pos = reply_info.game_dir.find_last_of( "\\/" ); - if( pos != reply_info.game_dir.npos ) - reply_info.game_dir.erase( 0, pos + 1 ); - } + { + reply_info.game_dir.resize( 256 ); + engine_server->GetGameDir( &reply_info.game_dir[0], reply_info.game_dir.size( ) ); + reply_info.game_dir.resize( strlen( reply_info.game_dir.c_str( ) ) ); - reply_info.max_clients = global::server->GetMaxClients( ); + size_t pos = reply_info.game_dir.find_last_of( "\\/" ); + if( pos != reply_info.game_dir.npos ) + reply_info.game_dir.erase( 0, pos + 1 ); + } - reply_info.udp_port = global::server->GetUDPPort( ); + reply_info.max_clients = global::server->GetMaxClients( ); - { - const IGamemodeSystem::Information &gamemode = - static_cast( filesystem )->Gamemodes( )->Active( ); + reply_info.udp_port = global::server->GetUDPPort( ); - reply_info.tags = " gm:"; - reply_info.tags += gamemode.name; + { + const IGamemodeSystem::Information &gamemode = + static_cast( filesystem )->Gamemodes( )->Active( ); + + reply_info.tags = " gm:"; + reply_info.tags += gamemode.name; + + if( !gamemode.workshopid.empty( ) ) + { + reply_info.tags += " gmws:"; + reply_info.tags += gamemode.workshopid; + } + } - if( !gamemode.workshopid.empty( ) ) { - reply_info.tags += " gmws:"; - reply_info.tags += gamemode.workshopid; + FileHandle_t file = filesystem->Open( "steam.inf", "r", "GAME" ); + if( file == nullptr ) + { + reply_info.game_version = default_game_version; + DebugWarning( "[ServerSecure] Error opening steam.inf\n" ); + return; + } + + char buff[256] = { 0 }; + bool failed = filesystem->ReadLine( buff, sizeof( buff ), file ) == nullptr; + filesystem->Close( file ); + if( failed ) + { + reply_info.game_version = default_game_version; + DebugWarning( "[ServerSecure] Failed reading steam.inf\n" ); + return; + } + + reply_info.game_version = &buff[13]; + + size_t pos = reply_info.game_version.find_first_of( "\r\n" ); + if( pos != reply_info.game_version.npos ) + reply_info.game_version.erase( pos ); } } + // maybe divide into low priority and high priority data? + // low priority would be VAC protection status for example + // updated on a much bigger period + static void BuildReplyInfo( ) { - FileHandle_t file = filesystem->Open( "steam.inf", "r", "GAME" ); - if( file == nullptr ) - { - reply_info.game_version = default_game_version; - DebugWarning( "[ServerSecure] Error opening steam.inf\n" ); - return; - } + info_cache_packet.Reset( ); + + info_cache_packet.WriteLong( -1 ); // connectionless packet header + info_cache_packet.WriteByte( 'I' ); // packet type is always 'I' + info_cache_packet.WriteByte( default_proto_version ); + info_cache_packet.WriteString( global::server->GetName( ) ); + info_cache_packet.WriteString( global::server->GetMapName( ) ); + info_cache_packet.WriteString( reply_info.game_dir.c_str( ) ); + info_cache_packet.WriteString( reply_info.game_desc.c_str( ) ); + + int32_t appid = engine_server->GetAppID( ); + info_cache_packet.WriteShort( appid ); + + info_cache_packet.WriteByte( global::server->GetNumClients( ) ); + int32_t maxplayers = + sv_visiblemaxplayers != nullptr ? sv_visiblemaxplayers->GetInt( ) : -1; + if( maxplayers <= 0 || maxplayers > reply_info.max_clients ) + maxplayers = reply_info.max_clients; + info_cache_packet.WriteByte( maxplayers ); + info_cache_packet.WriteByte( global::server->GetNumFakeClients( ) ); + info_cache_packet.WriteByte( 'd' ); // dedicated server identifier + info_cache_packet.WriteByte( operating_system_char ); + info_cache_packet.WriteByte( global::server->GetPassword( ) != nullptr ? 1 : 0 ); + // if vac protected, it activates itself some time after startup + ISteamGameServer *steamGS = gameserver_context != nullptr ? + gameserver_context->m_pSteamGameServer : nullptr; + info_cache_packet.WriteByte( steamGS != nullptr ? steamGS->BSecure( ) : false ); + info_cache_packet.WriteString( reply_info.game_version.c_str( ) ); + + const CSteamID *sid = engine_server->GetGameServerSteamID( ); + uint64_t steamid = 0; + if( sid != nullptr ) + steamid = sid->ConvertToUint64( ); + + bool notags = reply_info.tags.empty( ); + // 0x80 - port number is present + // 0x10 - server steamid is present + // 0x20 - tags are present + // 0x01 - game long appid is present + info_cache_packet.WriteByte( 0x80 | 0x10 | ( notags ? 0x00 : 0x20 ) | 0x01 ); + info_cache_packet.WriteShort( reply_info.udp_port ); + info_cache_packet.WriteLongLong( steamid ); + if( !notags ) + info_cache_packet.WriteString( reply_info.tags.c_str( ) ); + info_cache_packet.WriteLongLong( appid ); + } - char buff[256] = { 0 }; - bool failed = filesystem->ReadLine( buff, sizeof( buff ), file ) == nullptr; - filesystem->Close( file ); - if( failed ) + inline PacketType SendInfoCache( const sockaddr_in &from, uint32_t time ) + { + if( time - info_cache_last_update >= info_cache_time ) { - reply_info.game_version = default_game_version; - DebugWarning( "[ServerSecure] Failed reading steam.inf\n" ); - return; + BuildReplyInfo( ); + info_cache_last_update = time; } - reply_info.game_version = &buff[13]; + sendto( + game_socket, + reinterpret_cast( info_cache_packet.GetData( ) ), + info_cache_packet.GetNumBytesWritten( ), + 0, + reinterpret_cast( &from ), + sizeof( from ) + ); - size_t pos = reply_info.game_version.find_first_of( "\r\n" ); - if( pos != reply_info.game_version.npos ) - reply_info.game_version.erase( pos ); + return PacketTypeInvalid; // we've handled it } -} -// maybe divide into low priority and high priority data? -// low priority would be VAC protection status for example -// updated on a much bigger period -static void BuildReplyInfo( ) -{ - info_cache_packet.Reset( ); - - info_cache_packet.WriteLong( -1 ); // connectionless packet header - info_cache_packet.WriteByte( 'I' ); // packet type is always 'I' - info_cache_packet.WriteByte( default_proto_version ); - info_cache_packet.WriteString( global::server->GetName( ) ); - info_cache_packet.WriteString( global::server->GetMapName( ) ); - info_cache_packet.WriteString( reply_info.game_dir.c_str( ) ); - info_cache_packet.WriteString( reply_info.game_desc.c_str( ) ); - - int32_t appid = engine_server->GetAppID( ); - info_cache_packet.WriteShort( appid ); - - info_cache_packet.WriteByte( global::server->GetNumClients( ) ); - int32_t maxplayers = sv_visiblemaxplayers != nullptr ? sv_visiblemaxplayers->GetInt( ) : -1; - if( maxplayers <= 0 || maxplayers > reply_info.max_clients ) - maxplayers = reply_info.max_clients; - info_cache_packet.WriteByte( maxplayers ); - info_cache_packet.WriteByte( global::server->GetNumFakeClients( ) ); - info_cache_packet.WriteByte( 'd' ); // dedicated server identifier - info_cache_packet.WriteByte( operating_system_char ); - info_cache_packet.WriteByte( global::server->GetPassword( ) != nullptr ? 1 : 0 ); - // if vac protected, it activates itself some time after startup - ISteamGameServer *steamGS = gameserver_context != nullptr ? - gameserver_context->m_pSteamGameServer : nullptr; - info_cache_packet.WriteByte( steamGS != nullptr ? steamGS->BSecure( ) : false ); - info_cache_packet.WriteString( reply_info.game_version.c_str( ) ); - - const CSteamID *sid = engine_server->GetGameServerSteamID( ); - uint64_t steamid = 0; - if( sid != nullptr ) - steamid = sid->ConvertToUint64( ); - - bool notags = reply_info.tags.empty( ); - // 0x80 - port number is present - // 0x10 - server steamid is present - // 0x20 - tags are present - // 0x01 - game long appid is present - info_cache_packet.WriteByte( 0x80 | 0x10 | ( notags ? 0x00 : 0x20 ) | 0x01 ); - info_cache_packet.WriteShort( reply_info.udp_port ); - info_cache_packet.WriteLongLong( steamid ); - if ( !notags ) - info_cache_packet.WriteString( reply_info.tags.c_str( ) ); - info_cache_packet.WriteLongLong( appid ); -} - -inline PacketType SendInfoCache( const sockaddr_in &from, uint32_t time ) -{ - if( time - info_cache_last_update >= info_cache_time ) + inline PacketType HandleInfoQuery( const sockaddr_in &from ) { - BuildReplyInfo( ); - info_cache_last_update = time; - } + uint32_t time = static_cast( globalvars->realtime ); + if( !client_manager.CheckIPRate( from.sin_addr.s_addr, time ) ) + return PacketTypeInvalid; - sendto( - game_socket, - reinterpret_cast( info_cache_packet.GetData( ) ), - info_cache_packet.GetNumBytesWritten( ), - 0, - reinterpret_cast( &from ), - sizeof( from ) - ); - - return PacketTypeInvalid; // we've handled it -} + if( info_cache_enabled ) + return SendInfoCache( from, time ); -inline PacketType HandleInfoQuery( const sockaddr_in &from ) -{ - uint32_t time = static_cast( globalvars->realtime ); - if( !client_manager.CheckIPRate( from.sin_addr.s_addr, time ) ) - return PacketTypeInvalid; + return PacketTypeGood; + } - if( info_cache_enabled ) - return SendInfoCache( from, time ); + inline const char *IPToString( const in_addr &addr ) + { + static char buffer[16] = { }; + const char *str = + inet_ntop( AF_INET, const_cast( &addr ), buffer, sizeof( buffer ) ); + if( str == nullptr ) + return "unknown"; - return PacketTypeGood; -} + return str; + } -static PacketType ClassifyPacket( const char *data, int32_t len, const sockaddr_in &from ) -{ - if( len == 0 ) + static PacketType ClassifyPacket( const char *data, int32_t len, const sockaddr_in &from ) { - DebugWarning( - "[ServerSecure] Bad OOB! len: %d from %s\n", - len, - inet_ntoa( from.sin_addr ) - ); - return PacketTypeInvalid; - } + if( len == 0 ) + { + DebugWarning( + "[ServerSecure] Bad OOB! len: %d from %s\n", + len, + IPToString( from.sin_addr ) + ); + return PacketTypeInvalid; + } - if( len < 5 ) - return PacketTypeGood; + if( len < 5 ) + return PacketTypeGood; - int32_t channel = *reinterpret_cast( data ); - if( channel == -2 ) - { - DebugWarning( - "[ServerSecure] Bad OOB! len: %d, channel: 0x%X from %s\n", - len, - channel, - inet_ntoa( from.sin_addr ) - ); - return PacketTypeInvalid; - } + int32_t channel = *reinterpret_cast( data ); + if( channel == -2 ) + { + DebugWarning( + "[ServerSecure] Bad OOB! len: %d, channel: 0x%X from %s\n", + len, + channel, + IPToString( from.sin_addr ) + ); + return PacketTypeInvalid; + } - if( channel != -1 ) - return PacketTypeGood; + if( channel != -1 ) + return PacketTypeGood; - uint8_t type = *reinterpret_cast( data + 4 ); - if( packet_validation_enabled ) - { - switch( type ) + uint8_t type = *reinterpret_cast( data + 4 ); + if( packet_validation_enabled ) { + switch( type ) + { case 'W': // server challenge request case 's': // master server challenge if( len > 100 ) @@ -458,7 +471,7 @@ static PacketType ClassifyPacket( const char *data, int32_t len, const sockaddr_ len, channel, type, - inet_ntoa( from.sin_addr ) + IPToString( from.sin_addr ) ); return PacketTypeInvalid; } @@ -470,7 +483,7 @@ static PacketType ClassifyPacket( const char *data, int32_t len, const sockaddr_ len, channel, type, - inet_ntoa( from.sin_addr ) + IPToString( from.sin_addr ) ); return PacketTypeInvalid; } @@ -492,555 +505,560 @@ static PacketType ClassifyPacket( const char *data, int32_t len, const sockaddr_ len, channel, type, - inet_ntoa( from.sin_addr ) + IPToString( from.sin_addr ) ); return PacketTypeGood; + } + + DebugWarning( + "[ServerSecure] Bad OOB! len: %d, channel: 0x%X, type: %c from %s\n", + len, + channel, + type, + IPToString( from.sin_addr ) + ); + return PacketTypeInvalid; } - DebugWarning( - "[ServerSecure] Bad OOB! len: %d, channel: 0x%X, type: %c from %s\n", - len, - channel, - type, - inet_ntoa( from.sin_addr ) - ); - return PacketTypeInvalid; + return type == 'T' ? PacketTypeInfo : PacketTypeGood; } - return type == 'T' ? PacketTypeInfo : PacketTypeGood; -} - -inline bool IsAddressAllowed( const sockaddr_in &addr ) -{ - return - ( - !firewall_whitelist_enabled || - firewall_whitelist.find( addr.sin_addr.s_addr ) != firewall_whitelist.end( ) - ) && - ( - !firewall_blacklist_enabled || - firewall_blacklist.find( addr.sin_addr.s_addr ) == firewall_blacklist.end( ) - ); -} + inline bool IsAddressAllowed( const sockaddr_in &addr ) + { + return + ( + !firewall_whitelist_enabled || + firewall_whitelist.find( addr.sin_addr.s_addr ) != firewall_whitelist.end( ) + ) && + ( + !firewall_blacklist_enabled || + firewall_blacklist.find( addr.sin_addr.s_addr ) == firewall_blacklist.end( ) + ); + } -inline int32_t HandleNetError( int32_t value ) -{ - if( value == -1 ) + inline int32_t HandleNetError( int32_t value ) + { + if( value == -1 ) #if defined SYSTEM_WINDOWS - WSASetLastError( WSAEWOULDBLOCK ); + WSASetLastError( WSAEWOULDBLOCK ); #elif defined SYSTEM_POSIX - errno = EWOULDBLOCK; + errno = EWOULDBLOCK; #endif - return value; -} - -inline packet_t GetQueuedPacket( ) -{ - AUTO_LOCK( threaded_socket_mutex ); - packet_t p = threaded_socket_queue.front( ); - threaded_socket_queue.pop( ); - return p; -} + return value; + } -static int32_t ReceiveAndAnalyzePacket( - int32_t s, - char *buf, - int32_t buflen, - int32_t flags, - sockaddr *from, - int32_t *fromlen -) -{ - sockaddr_in &infrom = *reinterpret_cast( from ); - int32_t len = Hook_recvfrom( s, buf, buflen, flags, from, fromlen ); - if( len == -1 ) - return -1; + inline packet_t GetQueuedPacket( ) + { + AUTO_LOCK( threaded_socket_mutex ); + packet_t p = threaded_socket_queue.front( ); + threaded_socket_queue.pop( ); + return p; + } - if( packet_sampling_enabled ) + static int32_t ReceiveAndAnalyzePacket( + int32_t s, + char *buf, + int32_t buflen, + int32_t flags, + sockaddr *from, + int32_t *fromlen + ) { - packet_t p; - memcpy( &p.address, from, *fromlen ); - p.address_size = *fromlen; - p.buffer.assign( buf, buf + len ); + sockaddr_in &infrom = *reinterpret_cast( from ); + int32_t len = Hook_recvfrom( s, buf, buflen, flags, from, fromlen ); + if( len == -1 ) + return -1; - AUTO_LOCK( packet_sampling_mutex ); + if( packet_sampling_enabled ) + { + packet_t p; + memcpy( &p.address, from, *fromlen ); + p.address_size = *fromlen; + p.buffer.assign( buf, buf + len ); - // there should only be packet_sampling_max_queue packets on the queue - // at the moment of this check - if( packet_sampling_queue.size( ) >= packet_sampling_max_queue ) - packet_sampling_queue.pop_front( ); + AUTO_LOCK( packet_sampling_mutex ); - packet_sampling_queue.push_back( p ); - } + // there should only be packet_sampling_max_queue packets on the queue + // at the moment of this check + if( packet_sampling_queue.size( ) >= packet_sampling_max_queue ) + packet_sampling_queue.pop_front( ); - if( !IsAddressAllowed( infrom ) ) - return -1; + packet_sampling_queue.push_back( p ); + } - PacketType type = ClassifyPacket( buf, len, infrom ); - if( type == PacketTypeInfo ) - type = HandleInfoQuery( infrom ); + if( !IsAddressAllowed( infrom ) ) + return -1; - if( type == PacketTypeInvalid ) - return -1; + PacketType type = ClassifyPacket( buf, len, infrom ); + if( type == PacketTypeInfo ) + type = HandleInfoQuery( infrom ); - return len; -} + if( type == PacketTypeInvalid ) + return -1; -inline bool IsPacketQueueEmpty( ) -{ - AUTO_LOCK( threaded_socket_mutex ); - return threaded_socket_queue.empty( ); -} + return len; + } -static int32_t Hook_recvfrom_detour( - int32_t s, - char *buf, - int32_t buflen, - int32_t flags, - sockaddr *from, - int32_t *fromlen -) -{ - bool queue_empty = IsPacketQueueEmpty( ); - if( !threaded_socket_enabled && queue_empty ) - return HandleNetError( ReceiveAndAnalyzePacket( s, buf, buflen, flags, from, fromlen ) ); + inline bool IsPacketQueueEmpty( ) + { + AUTO_LOCK( threaded_socket_mutex ); + return threaded_socket_queue.empty( ); + } - if( queue_empty ) - return HandleNetError( -1 ); + static int32_t Hook_recvfrom_detour( + int32_t s, + char *buf, + int32_t buflen, + int32_t flags, + sockaddr *from, + int32_t *fromlen + ) + { + bool queue_empty = IsPacketQueueEmpty( ); + if( !threaded_socket_enabled && queue_empty ) + return HandleNetError( + ReceiveAndAnalyzePacket( s, buf, buflen, flags, from, fromlen ) + ); - packet_t p = GetQueuedPacket( ); - int32_t len = static_cast( p.buffer.size( ) ); - if( len > buflen ) - len = buflen; + if( queue_empty ) + return HandleNetError( -1 ); - size_t addrlen = static_cast( *fromlen ); - if( addrlen > sizeof( p.address ) ) - addrlen = sizeof( p.address ); + packet_t p = GetQueuedPacket( ); + int32_t len = static_cast( p.buffer.size( ) ); + if( len > buflen ) + len = buflen; - memcpy( buf, &p.buffer[0], len ); - memcpy( from, &p.address, addrlen ); - *fromlen = p.address_size; + size_t addrlen = static_cast( *fromlen ); + if( addrlen > sizeof( p.address ) ) + addrlen = sizeof( p.address ); - return len; -} + memcpy( buf, &p.buffer[0], len ); + memcpy( from, &p.address, addrlen ); + *fromlen = p.address_size; -inline bool IsPacketQueueFull( ) -{ - AUTO_LOCK( threaded_socket_mutex ); - return threaded_socket_queue.size( ) >= threaded_socket_max_queue; -} + return len; + } -inline void PushPacketToQueue( const packet_t &p ) -{ - AUTO_LOCK( threaded_socket_mutex ); - threaded_socket_queue.push( p ); -} + inline bool IsPacketQueueFull( ) + { + AUTO_LOCK( threaded_socket_mutex ); + return threaded_socket_queue.size( ) >= threaded_socket_max_queue; + } -static uint32_t PacketReceiverThread( void * ) -{ - timeval ms100 = { 0, 100000 }; - char tempbuf[65535] = { 0 }; - fd_set readables; + inline void PushPacketToQueue( const packet_t &p ) + { + AUTO_LOCK( threaded_socket_mutex ); + threaded_socket_queue.push( p ); + } - while( threaded_socket_execute ) + static uint32_t PacketReceiverThread( void * ) { - if( !threaded_socket_enabled || IsPacketQueueFull( ) ) - // testing for maximum queue size, this is a very cheap "fix" - // the socket itself has a queue too but will start dropping packets when full - { - ThreadSleep( 100 ); - continue; - } + timeval ms100 = { 0, 100000 }; + char tempbuf[65535] = { 0 }; + fd_set readables; - FD_ZERO( &readables ); - FD_SET( game_socket, &readables ); - int res = select( game_socket + 1, &readables, nullptr, nullptr, &ms100 ); - ms100.tv_usec = 100000; - if( res == -1 || !FD_ISSET( game_socket, &readables ) ) - continue; + while( threaded_socket_execute ) + { + if( !threaded_socket_enabled || IsPacketQueueFull( ) ) + // testing for maximum queue size, this is a very cheap "fix" + // the socket itself has a queue too but will start dropping packets when full + { + ThreadSleep( 100 ); + continue; + } + + FD_ZERO( &readables ); + FD_SET( game_socket, &readables ); + int res = select( game_socket + 1, &readables, nullptr, nullptr, &ms100 ); + ms100.tv_usec = 100000; + if( res == -1 || !FD_ISSET( game_socket, &readables ) ) + continue; + + packet_t p; + int32_t len = ReceiveAndAnalyzePacket( + game_socket, + tempbuf, + sizeof( tempbuf ), + 0, + reinterpret_cast( &p.address ), + &p.address_size + ); + if( len == -1 ) + continue; - packet_t p; - int32_t len = ReceiveAndAnalyzePacket( - game_socket, - tempbuf, - sizeof( tempbuf ), - 0, - reinterpret_cast( &p.address ), - &p.address_size - ); - if( len == -1 ) - continue; + p.buffer.assign( tempbuf, tempbuf + len ); - p.buffer.assign( tempbuf, tempbuf + len ); + PushPacketToQueue( p ); + } - PushPacketToQueue( p ); + return 0; } - return 0; -} - -inline void SetReceiveDetourStatus( bool enabled ) -{ - if( enabled ) - VCRHook_recvfrom = Hook_recvfrom_detour; - else if( !firewall_whitelist_enabled && - !firewall_blacklist_enabled && - !packet_validation_enabled && - !threaded_socket_enabled ) - VCRHook_recvfrom = Hook_recvfrom; -} - -LUA_FUNCTION_STATIC( EnableFirewallWhitelist ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - firewall_whitelist_enabled = LUA->GetBool( 1 ); - SetReceiveDetourStatus( firewall_whitelist_enabled ); - return 0; -} - -// Whitelisted IPs bytes need to be in network order (big endian) -LUA_FUNCTION_STATIC( AddWhitelistIP ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - firewall_whitelist.insert( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + inline void SetReceiveDetourStatus( bool enabled ) + { + if( enabled ) + VCRHook_recvfrom = Hook_recvfrom_detour; + else if( !firewall_whitelist_enabled && + !firewall_blacklist_enabled && + !packet_validation_enabled && + !threaded_socket_enabled ) + VCRHook_recvfrom = Hook_recvfrom; + } -LUA_FUNCTION_STATIC( RemoveWhitelistIP ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - firewall_whitelist.erase( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( EnableFirewallWhitelist ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + firewall_whitelist_enabled = LUA->GetBool( 1 ); + SetReceiveDetourStatus( firewall_whitelist_enabled ); + return 0; + } -LUA_FUNCTION_STATIC( ResetWhitelist ) -{ - set_uint32( ).swap( firewall_whitelist ); - return 0; -} + // Whitelisted IPs bytes need to be in network order (big endian) + LUA_FUNCTION_STATIC( AddWhitelistIP ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + firewall_whitelist.insert( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } -LUA_FUNCTION_STATIC( EnableFirewallBlacklist ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - firewall_blacklist_enabled = LUA->GetBool( 1 ); - SetReceiveDetourStatus( firewall_blacklist_enabled ); - return 0; -} + LUA_FUNCTION_STATIC( RemoveWhitelistIP ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + firewall_whitelist.erase( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } -// Blacklisted IPs bytes need to be in network order (big endian) -LUA_FUNCTION_STATIC( AddBlacklistIP ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - firewall_blacklist.insert( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( ResetWhitelist ) + { + set_uint32( ).swap( firewall_whitelist ); + return 0; + } -LUA_FUNCTION_STATIC( RemoveBlacklistIP ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - firewall_blacklist.erase( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( EnableFirewallBlacklist ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + firewall_blacklist_enabled = LUA->GetBool( 1 ); + SetReceiveDetourStatus( firewall_blacklist_enabled ); + return 0; + } -LUA_FUNCTION_STATIC( ResetBlacklist ) -{ - set_uint32( ).swap( firewall_blacklist ); - return 0; -} + // Blacklisted IPs bytes need to be in network order (big endian) + LUA_FUNCTION_STATIC( AddBlacklistIP ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + firewall_blacklist.insert( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } -LUA_FUNCTION_STATIC( EnablePacketValidation ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - packet_validation_enabled = LUA->GetBool( 1 ); - SetReceiveDetourStatus( packet_validation_enabled ); - return 0; -} + LUA_FUNCTION_STATIC( RemoveBlacklistIP ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + firewall_blacklist.erase( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } -LUA_FUNCTION_STATIC( EnableThreadedSocket ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - threaded_socket_enabled = LUA->GetBool( 1 ); - SetReceiveDetourStatus( threaded_socket_enabled ); - return 0; -} + LUA_FUNCTION_STATIC( ResetBlacklist ) + { + set_uint32( ).swap( firewall_blacklist ); + return 0; + } -LUA_FUNCTION_STATIC( EnableInfoCache ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - info_cache_enabled = LUA->GetBool( 1 ); - return 0; -} + LUA_FUNCTION_STATIC( EnablePacketValidation ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + packet_validation_enabled = LUA->GetBool( 1 ); + SetReceiveDetourStatus( packet_validation_enabled ); + return 0; + } -LUA_FUNCTION_STATIC( SetInfoCacheTime ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - info_cache_time = static_cast( LUA->GetNumber( 1 ) ); - return 0; -} + LUA_FUNCTION_STATIC( EnableThreadedSocket ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + threaded_socket_enabled = LUA->GetBool( 1 ); + SetReceiveDetourStatus( threaded_socket_enabled ); + return 0; + } -LUA_FUNCTION_STATIC( RefreshInfoCache ) -{ - BuildStaticReplyInfo( ); - BuildReplyInfo( ); - return 0; -} + LUA_FUNCTION_STATIC( EnableInfoCache ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + info_cache_enabled = LUA->GetBool( 1 ); + return 0; + } -LUA_FUNCTION_STATIC( EnableQueryLimiter ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); - client_manager.SetState( LUA->GetBool( 1 ) ); - return 0; -} + LUA_FUNCTION_STATIC( SetInfoCacheTime ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + info_cache_time = static_cast( LUA->GetNumber( 1 ) ); + return 0; + } -LUA_FUNCTION_STATIC( SetMaxQueriesWindow ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - client_manager.SetMaxQueriesWindow( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( RefreshInfoCache ) + { + BuildStaticReplyInfo( ); + BuildReplyInfo( ); + return 0; + } -LUA_FUNCTION_STATIC( SetMaxQueriesPerSecond ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - client_manager.SetMaxQueriesPerSecond( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( EnableQueryLimiter ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + client_manager.SetState( LUA->GetBool( 1 ) ); + return 0; + } -LUA_FUNCTION_STATIC( SetGlobalMaxQueriesPerSecond ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); - client_manager.SetGlobalMaxQueriesPerSecond( static_cast( LUA->GetNumber( 1 ) ) ); - return 0; -} + LUA_FUNCTION_STATIC( SetMaxQueriesWindow ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + client_manager.SetMaxQueriesWindow( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } -LUA_FUNCTION_STATIC( EnablePacketSampling ) -{ - LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); + LUA_FUNCTION_STATIC( SetMaxQueriesPerSecond ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + client_manager.SetMaxQueriesPerSecond( static_cast( LUA->GetNumber( 1 ) ) ); + return 0; + } - packet_sampling_enabled = LUA->GetBool( 1 ); - if( !packet_sampling_enabled ) + LUA_FUNCTION_STATIC( SetGlobalMaxQueriesPerSecond ) { - AUTO_LOCK( packet_sampling_mutex ); - packet_sampling_queue.clear( ); + LUA->CheckType( 1, GarrysMod::Lua::Type::NUMBER ); + client_manager.SetGlobalMaxQueriesPerSecond( + static_cast( LUA->GetNumber( 1 ) ) + ); + return 0; } - return 0; -} + LUA_FUNCTION_STATIC( EnablePacketSampling ) + { + LUA->CheckType( 1, GarrysMod::Lua::Type::BOOL ); -inline packet_t GetSamplePacket( ) -{ - AUTO_LOCK( packet_sampling_mutex ); + packet_sampling_enabled = LUA->GetBool( 1 ); + if( !packet_sampling_enabled ) + { + AUTO_LOCK( packet_sampling_mutex ); + packet_sampling_queue.clear( ); + } - if( packet_sampling_queue.empty( ) ) - return packet_t( ); + return 0; + } - packet_t p = packet_sampling_queue.front( ); - packet_sampling_queue.pop_front( ); - return p; -} + inline packet_t GetSamplePacket( ) + { + AUTO_LOCK( packet_sampling_mutex ); -LUA_FUNCTION_STATIC( GetSamplePacket ) -{ - packet_t p = GetSamplePacket( ); - if( p.address.sin_addr.s_addr == 0 ) - return 0; + if( packet_sampling_queue.empty( ) ) + return packet_t( ); - LUA->PushNumber( p.address.sin_addr.s_addr ); - LUA->PushNumber( p.address.sin_port ); - LUA->PushString( &p.buffer[0], p.buffer.size( ) ); - return 3; -} + packet_t p = packet_sampling_queue.front( ); + packet_sampling_queue.pop_front( ); + return p; + } -void Initialize( GarrysMod::Lua::ILuaBase *LUA ) -{ - if( !server_loader.IsValid( ) ) - LUA->ThrowError( "unable to get server factory" ); + LUA_FUNCTION_STATIC( GetSamplePacket ) + { + packet_t p = GetSamplePacket( ); + if( p.address.sin_addr.s_addr == 0 ) + return 0; + + LUA->PushNumber( p.address.sin_addr.s_addr ); + LUA->PushNumber( p.address.sin_port ); + LUA->PushString( &p.buffer[0], p.buffer.size( ) ); + return 3; + } - ICvar *icvar = icvar_loader.GetInterface( CVAR_INTERFACE_VERSION ); - if( icvar != nullptr ) - sv_visiblemaxplayers = icvar->FindVar( "sv_visiblemaxplayers" ); + void Initialize( GarrysMod::Lua::ILuaBase *LUA ) + { + if( !server_loader.IsValid( ) ) + LUA->ThrowError( "unable to get server factory" ); - gamedll = server_loader.GetInterface( INTERFACEVERSION_SERVERGAMEDLL ); - if( gamedll == nullptr ) - LUA->ThrowError( "failed to load required IServerGameDLL interface" ); + ICvar *icvar = icvar_loader.GetInterface( CVAR_INTERFACE_VERSION ); + if( icvar != nullptr ) + sv_visiblemaxplayers = icvar->FindVar( "sv_visiblemaxplayers" ); - engine_server = global::engine_loader.GetInterface( - INTERFACEVERSION_VENGINESERVER - ); - if( engine_server == nullptr ) - LUA->ThrowError( "failed to load required IVEngineServer interface" ); + gamedll = server_loader.GetInterface( INTERFACEVERSION_SERVERGAMEDLL ); + if( gamedll == nullptr ) + LUA->ThrowError( "failed to load required IServerGameDLL interface" ); - IPlayerInfoManager *playerinfo = server_loader.GetInterface( - INTERFACEVERSION_PLAYERINFOMANAGER - ); - if( playerinfo == nullptr ) - LUA->ThrowError( "failed to load required IPlayerInfoManager interface" ); + engine_server = global::engine_loader.GetInterface( + INTERFACEVERSION_VENGINESERVER + ); + if( engine_server == nullptr ) + LUA->ThrowError( "failed to load required IVEngineServer interface" ); - globalvars = playerinfo->GetGlobalVars( ); - if( globalvars == nullptr ) - LUA->ThrowError( "failed to load required CGlobalVars interface" ); + IPlayerInfoManager *playerinfo = server_loader.GetInterface( + INTERFACEVERSION_PLAYERINFOMANAGER + ); + if( playerinfo == nullptr ) + LUA->ThrowError( "failed to load required IPlayerInfoManager interface" ); - netsockets_t *net_sockets = nullptr; + globalvars = playerinfo->GetGlobalVars( ); + if( globalvars == nullptr ) + LUA->ThrowError( "failed to load required CGlobalVars interface" ); - { - SymbolFinder symfinder; + netsockets_t *net_sockets = nullptr; - CreateInterfaceFn factory = reinterpret_cast( symfinder.ResolveOnBinary( - dedicated_binary.c_str( ), FileSystemFactory_sym, FileSystemFactory_symlen - ) ); - if( factory == nullptr ) - { - IFileSystem **filesystem_ptr = reinterpret_cast( symfinder.ResolveOnBinary( - dedicated_binary.c_str( ), g_pFullFileSystem_sym, g_pFullFileSystem_symlen - ) ); - filesystem = filesystem_ptr != nullptr ? *filesystem_ptr : nullptr; - } - else { - filesystem = - static_cast( factory( FILESYSTEM_INTERFACE_VERSION, nullptr ) ); - } - - net_sockets = + SymbolFinder symfinder; + + CreateInterfaceFn factory = + reinterpret_cast( symfinder.ResolveOnBinary( + dedicated_binary.c_str( ), FileSystemFactory_sym, FileSystemFactory_symlen + ) ); + if( factory == nullptr ) + { + IFileSystem **filesystem_ptr = + reinterpret_cast( symfinder.ResolveOnBinary( + dedicated_binary.c_str( ), g_pFullFileSystem_sym, g_pFullFileSystem_symlen + ) ); + filesystem = filesystem_ptr != nullptr ? *filesystem_ptr : nullptr; + } + else + { + filesystem = + static_cast( factory( FILESYSTEM_INTERFACE_VERSION, nullptr ) ); + } + + net_sockets = #if defined SYSTEM_POSIX - reinterpret_cast + reinterpret_cast #else - *reinterpret_cast + *reinterpret_cast #endif - ( symfinder.ResolveOnBinary( - global::engine_binary.c_str( ), - net_sockets_sig, - net_sockets_siglen - ) ); + ( symfinder.ResolveOnBinary( + global::engine_binary.c_str( ), + net_sockets_sig, + net_sockets_siglen + ) ); #if defined SYSTEM_WINDOWS - CSteamGameServerAPIContext **gameserver_context_pointer = - reinterpret_cast( symfinder.ResolveOnBinary( - server_binary.c_str( ), - SteamGameServerAPIContext_sym, - SteamGameServerAPIContext_symlen - ) ); - if( gameserver_context_pointer == nullptr ) - LUA->ThrowError( - "Failed to load required CSteamGameServerAPIContext interface pointer." - ); + CSteamGameServerAPIContext **gameserver_context_pointer = + reinterpret_cast( symfinder.ResolveOnBinary( + server_binary.c_str( ), + SteamGameServerAPIContext_sym, + SteamGameServerAPIContext_symlen + ) ); + if( gameserver_context_pointer == nullptr ) + LUA->ThrowError( + "Failed to load required CSteamGameServerAPIContext interface pointer." + ); - gameserver_context = *gameserver_context_pointer; + gameserver_context = *gameserver_context_pointer; #else - gameserver_context = - reinterpret_cast( symfinder.ResolveOnBinary( - server_binary.c_str( ), - SteamGameServerAPIContext_sym, - SteamGameServerAPIContext_symlen - ) ); + gameserver_context = + reinterpret_cast( symfinder.ResolveOnBinary( + server_binary.c_str( ), + SteamGameServerAPIContext_sym, + SteamGameServerAPIContext_symlen + ) ); #endif - if( gameserver_context == nullptr ) - LUA->ThrowError( "Failed to load required CSteamGameServerAPIContext interface." ); - } - - if( filesystem == nullptr ) - LUA->ThrowError( "failed to initialize IFileSystem" ); + if( gameserver_context == nullptr ) + LUA->ThrowError( "Failed to load required CSteamGameServerAPIContext interface." ); + } - if( net_sockets == nullptr ) - LUA->ThrowError( "got an invalid pointer to net_sockets" ); + if( filesystem == nullptr ) + LUA->ThrowError( "failed to initialize IFileSystem" ); - game_socket = net_sockets->Element( 1 ).hUDP; - if( game_socket == INVALID_SOCKET ) - LUA->ThrowError( "got an invalid server socket" ); + if( net_sockets == nullptr ) + LUA->ThrowError( "got an invalid pointer to net_sockets" ); - threaded_socket_execute = true; - threaded_socket_handle = CreateSimpleThread( PacketReceiverThread, nullptr ); - if( threaded_socket_handle == nullptr ) - LUA->ThrowError( "unable to create thread" ); + game_socket = net_sockets->Element( 1 ).hUDP; + if( game_socket == INVALID_SOCKET ) + LUA->ThrowError( "got an invalid server socket" ); - BuildStaticReplyInfo( ); + threaded_socket_execute = true; + threaded_socket_handle = CreateSimpleThread( PacketReceiverThread, nullptr ); + if( threaded_socket_handle == nullptr ) + LUA->ThrowError( "unable to create thread" ); - LUA->PushCFunction( EnableFirewallWhitelist ); - LUA->SetField( -2, "EnableFirewallWhitelist" ); + BuildStaticReplyInfo( ); - LUA->PushCFunction( AddWhitelistIP ); - LUA->SetField( -2, "AddWhitelistIP" ); + LUA->PushCFunction( EnableFirewallWhitelist ); + LUA->SetField( -2, "EnableFirewallWhitelist" ); - LUA->PushCFunction( RemoveWhitelistIP ); - LUA->SetField( -2, "RemoveWhitelistIP" ); + LUA->PushCFunction( AddWhitelistIP ); + LUA->SetField( -2, "AddWhitelistIP" ); - LUA->PushCFunction( ResetWhitelist ); - LUA->SetField( -2, "ResetWhitelist" ); + LUA->PushCFunction( RemoveWhitelistIP ); + LUA->SetField( -2, "RemoveWhitelistIP" ); - LUA->PushCFunction( EnableFirewallBlacklist ); - LUA->SetField( -2, "EnableFirewallBlacklist" ); + LUA->PushCFunction( ResetWhitelist ); + LUA->SetField( -2, "ResetWhitelist" ); - LUA->PushCFunction( AddBlacklistIP ); - LUA->SetField( -2, "AddBlacklistIP" ); + LUA->PushCFunction( EnableFirewallBlacklist ); + LUA->SetField( -2, "EnableFirewallBlacklist" ); - LUA->PushCFunction( RemoveBlacklistIP ); - LUA->SetField( -2, "RemoveBlacklistIP" ); + LUA->PushCFunction( AddBlacklistIP ); + LUA->SetField( -2, "AddBlacklistIP" ); - LUA->PushCFunction( ResetBlacklist ); - LUA->SetField( -2, "ResetBlacklist" ); + LUA->PushCFunction( RemoveBlacklistIP ); + LUA->SetField( -2, "RemoveBlacklistIP" ); - LUA->PushCFunction( EnablePacketValidation ); - LUA->SetField( -2, "EnablePacketValidation" ); + LUA->PushCFunction( ResetBlacklist ); + LUA->SetField( -2, "ResetBlacklist" ); - LUA->PushCFunction( EnableThreadedSocket ); - LUA->SetField( -2, "EnableThreadedSocket" ); + LUA->PushCFunction( EnablePacketValidation ); + LUA->SetField( -2, "EnablePacketValidation" ); - LUA->PushCFunction( EnableInfoCache ); - LUA->SetField( -2, "EnableInfoCache" ); + LUA->PushCFunction( EnableThreadedSocket ); + LUA->SetField( -2, "EnableThreadedSocket" ); - LUA->PushCFunction( SetInfoCacheTime ); - LUA->SetField( -2, "SetInfoCacheTime" ); + LUA->PushCFunction( EnableInfoCache ); + LUA->SetField( -2, "EnableInfoCache" ); - LUA->PushCFunction( RefreshInfoCache ); - LUA->SetField( -2, "RefreshInfoCache" ); + LUA->PushCFunction( SetInfoCacheTime ); + LUA->SetField( -2, "SetInfoCacheTime" ); - LUA->PushCFunction( EnableQueryLimiter ); - LUA->SetField( -2, "EnableQueryLimiter" ); + LUA->PushCFunction( RefreshInfoCache ); + LUA->SetField( -2, "RefreshInfoCache" ); - LUA->PushCFunction( SetMaxQueriesWindow ); - LUA->SetField( -2, "SetMaxQueriesWindow" ); + LUA->PushCFunction( EnableQueryLimiter ); + LUA->SetField( -2, "EnableQueryLimiter" ); - LUA->PushCFunction( SetMaxQueriesPerSecond ); - LUA->SetField( -2, "SetMaxQueriesPerSecond" ); + LUA->PushCFunction( SetMaxQueriesWindow ); + LUA->SetField( -2, "SetMaxQueriesWindow" ); - LUA->PushCFunction( SetGlobalMaxQueriesPerSecond ); - LUA->SetField( -2, "SetGlobalMaxQueriesPerSecond" ); + LUA->PushCFunction( SetMaxQueriesPerSecond ); + LUA->SetField( -2, "SetMaxQueriesPerSecond" ); - LUA->PushCFunction( EnablePacketSampling ); - LUA->SetField( -2, "EnablePacketSampling" ); + LUA->PushCFunction( SetGlobalMaxQueriesPerSecond ); + LUA->SetField( -2, "SetGlobalMaxQueriesPerSecond" ); - LUA->PushCFunction( GetSamplePacket ); - LUA->SetField( -2, "GetSamplePacket" ); -} + LUA->PushCFunction( EnablePacketSampling ); + LUA->SetField( -2, "EnablePacketSampling" ); -void Deinitialize( GarrysMod::Lua::ILuaBase * ) -{ - if( threaded_socket_handle != nullptr ) - { - threaded_socket_execute = false; - ThreadJoin( threaded_socket_handle ); - ReleaseThreadHandle( threaded_socket_handle ); - threaded_socket_handle = nullptr; + LUA->PushCFunction( GetSamplePacket ); + LUA->SetField( -2, "GetSamplePacket" ); } - VCRHook_recvfrom = Hook_recvfrom; -} + void Deinitialize( GarrysMod::Lua::ILuaBase * ) + { + if( threaded_socket_handle != nullptr ) + { + threaded_socket_execute = false; + ThreadJoin( threaded_socket_handle ); + ReleaseThreadHandle( threaded_socket_handle ); + threaded_socket_handle = nullptr; + } + VCRHook_recvfrom = Hook_recvfrom; + } } diff --git a/source/netfilter/core.hpp b/source/netfilter/core.hpp index e35a223..0bb351f 100644 --- a/source/netfilter/core.hpp +++ b/source/netfilter/core.hpp @@ -12,8 +12,6 @@ namespace GarrysMod namespace netfilter { - -void Initialize( GarrysMod::Lua::ILuaBase *LUA ); -void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ); - + void Initialize( GarrysMod::Lua::ILuaBase *LUA ); + void Deinitialize( GarrysMod::Lua::ILuaBase *LUA ); }