From a8a2c94ab13eff07ad38a34bc0abfc7f9b304963 Mon Sep 17 00:00:00 2001 From: Northn Date: Sat, 27 Jul 2024 00:16:21 +0400 Subject: [PATCH 1/5] Fix SAMP data encryption --- SAMPRakNet.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SAMPRakNet.cpp b/SAMPRakNet.cpp index 7e8d1ee..c31a4d3 100644 --- a/SAMPRakNet.cpp +++ b/SAMPRakNet.cpp @@ -591,9 +591,9 @@ SAMPRakNet:: // Alternate the mask every byte. cur = (uint8_t)src[i]; checksum ^= cur & 0xAA; + cur = key[cur]; if (i & 1) cur ^= port; - cur = key[cur]; buffer_[i + 1] = cur; } buffer_[0] = checksum; From 40b26d46a20ff8fab0cc722991e58e5a7007c5bc Mon Sep 17 00:00:00 2001 From: Northn Date: Sat, 27 Jul 2024 00:17:03 +0400 Subject: [PATCH 2/5] Remove unused include --- SAMPRakNet.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/SAMPRakNet.hpp b/SAMPRakNet.hpp index 91fe47e..bf92c93 100644 --- a/SAMPRakNet.hpp +++ b/SAMPRakNet.hpp @@ -33,8 +33,6 @@ typedef int SOCKET; #define LOCALHOST (0x0100007fu) -#include - class SAMPRakNet { public: From 7e074415f4da35d96233d1b36d31be75969282d6 Mon Sep 17 00:00:00 2001 From: Northn Date: Sat, 27 Jul 2024 15:17:02 +0400 Subject: [PATCH 3/5] Add client-side support * Connection to server is fully working, backward compatibility with server & original client kept working too * Roughly removed server OMP-SDK support to let client handle without it * Enable RAKNET_BUILD_FOR_CLIENT to use --- CMakeLists.txt | 8 +- Include/raknet/NetworkTypes.h | 3 + Include/raknet/RakPeer.h | 10 +- Include/raknet/RakServer.h | 2 + Include/raknet/RakServerInterface.h | 2 + SAMPRakNet.cpp | 107 ++++++++++++++++++ SAMPRakNet.hpp | 31 ++++++ Source/RakPeer.cpp | 164 +++++++++++++++++++++++++++- Source/ReliabilityLayer.cpp | 14 +++ Source/SocketLayer.cpp | 10 ++ Source/rakserver.cpp | 2 + 11 files changed, 350 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94c66ef..44bea19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +option(RAKNET_BUILD_FOR_CLIENT "Build RakNet without server-side functionality" NO) + if(WIN32) add_definitions( -D_CRT_SECURE_NO_WARNINGS @@ -50,7 +52,11 @@ target_include_directories(raknet ) if (WIN32) - target_link_libraries(raknet Ws2_32 OMP-SDK) + target_link_libraries(raknet Ws2_32) +endif() + +if (RAKNET_BUILD_FOR_CLIENT) + target_compile_definitions(raknet PUBLIC -DBUILD_FOR_CLIENT) else() target_link_libraries(raknet OMP-SDK) endif() diff --git a/Include/raknet/NetworkTypes.h b/Include/raknet/NetworkTypes.h index 866a8b3..9670623 100644 --- a/Include/raknet/NetworkTypes.h +++ b/Include/raknet/NetworkTypes.h @@ -20,7 +20,10 @@ #include "RakNetDefines.h" #include "Export.h" +#ifndef BUILD_FOR_CLIENT #include "../../SDK/include/types.hpp" +#endif +#include /// Forward declaration namespace RakNet diff --git a/Include/raknet/RakPeer.h b/Include/raknet/RakPeer.h index 4c3439c..ffa7373 100644 --- a/Include/raknet/RakPeer.h +++ b/Include/raknet/RakPeer.h @@ -18,6 +18,8 @@ #ifndef __RAK_PEER_H #define __RAK_PEER_H +#include + #include "Export.h" #include "RakPeerInterface.h" #include "ReliabilityLayer.h" @@ -508,7 +510,9 @@ namespace RakNet bool setAESKey; /// true if security is enabled. RPCMap rpcMap; /// Mapping of RPC calls to single byte integers to save transmission bandwidth. enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET, CONNECTED} connectMode; +#ifndef BUILD_FOR_CLIENT SAMPRakNet::RemoteSystemData sampData; +#endif bool isLogon; }; @@ -529,7 +533,9 @@ namespace RakNet friend void* UpdateNetworkLoop( void* arguments ); #endif +#ifndef BUILD_FOR_CLIENT friend bool __stdcall ProcessBan(RakPeer* rakPeer, PlayerID playerId, const char* data, const int length); +#endif // This is done to provide custom RPC handling when in a blocking RPC Packet* ReceiveIgnoreRPC( void ); @@ -543,11 +549,13 @@ namespace RakNet /// \return 0 if none RemoteSystemStruct *GetRemoteSystemFromPlayerID( const PlayerID playerID, bool calledFromNetworkThread, bool onlyActive) const; ///Parse out a connection request packet +#ifndef BUILD_FOR_CLIENT void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, PlayerID playerId, const char *data, int byteSize); bool ParseConnectionAuthPacket(RakPeer::RemoteSystemStruct* remoteSystem, PlayerID playerId, unsigned char* data, int byteSize); ///When we get a connection request from an ip / port, accept it unless full void OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, unsigned char *AESKey, bool setAESKey ); void AcceptConnectionRequest(RakPeer::RemoteSystemStruct* remoteSystem); +#endif ///Send a reliable disconnect packet to this player and disconnect them when it is delivered void NotifyAndFlagForDisconnect( const PlayerID playerId, bool performImmediate, unsigned char orderingChannel ); ///Returns how many remote systems initiated a connection to us @@ -602,7 +610,7 @@ namespace RakNet /// open.mp addition: /// Let's create an array of player indexes using PlayerIDs - FlatHashMap playerIndexes; + std::unordered_map playerIndexes; /// This is an array of pointers to RemoteSystemStruct /// This allows us to preallocate the list when starting, so we don't have to allocate or delete at runtime. diff --git a/Include/raknet/RakServer.h b/Include/raknet/RakServer.h index d84b7d7..b7a650b 100644 --- a/Include/raknet/RakServer.h +++ b/Include/raknet/RakServer.h @@ -435,8 +435,10 @@ namespace RakNet /// \sa RakNetStatistics.h RakNetStatisticsStruct * GetStatistics( const PlayerID playerId ) override; +#ifndef BUILD_FOR_CLIENT /// Return the SAMPRakNet RemoteSystemData for a player ID virtual SAMPRakNet::RemoteSystemData GetSAMPDataFromPlayerID(const PlayerID playerId) override; +#endif /// Get Remote System data for a player from their ID virtual RakPeer::RemoteSystemStruct* GetRemoteSystemFromPlayerID(const PlayerID playerId) override; diff --git a/Include/raknet/RakServerInterface.h b/Include/raknet/RakServerInterface.h index 6142145..e4054e1 100644 --- a/Include/raknet/RakServerInterface.h +++ b/Include/raknet/RakServerInterface.h @@ -429,8 +429,10 @@ namespace RakNet /// \sa RakNetStatistics.h virtual RakNetStatisticsStruct * GetStatistics( const PlayerID playerId )=0; +#ifndef BUILD_FOR_CLIENT /// Get SAMP data for a player from their ID virtual SAMPRakNet::RemoteSystemData GetSAMPDataFromPlayerID(const PlayerID playerId) = 0; +#endif /// Get Remote System data for a player from their ID virtual RakPeer::RemoteSystemStruct* GetRemoteSystemFromPlayerID(const PlayerID playerId) = 0; diff --git a/SAMPRakNet.cpp b/SAMPRakNet.cpp index c31a4d3..0175a1a 100644 --- a/SAMPRakNet.cpp +++ b/SAMPRakNet.cpp @@ -9,10 +9,19 @@ #include uint8_t SAMPRakNet::buffer_[MAXIMUM_MTU_SIZE]; +#ifdef BUILD_FOR_CLIENT +char SAMPRakNet::authkeyBuffer_[AUTHKEY_RESPONSE_LEN]; +bool SAMPRakNet::connectAsNpc_ = false; +#endif +#ifndef BUILD_FOR_CLIENT uint32_t SAMPRakNet::token_; +#endif uint16_t SAMPRakNet::portNumber = 7777; +#ifndef BUILD_FOR_CLIENT Query* SAMPRakNet::query_ = nullptr; +#endif unsigned int SAMPRakNet::timeout_ = 10000; +#ifndef BUILD_FOR_CLIENT unsigned int SAMPRakNet::minConnectionTime_ = 0; unsigned int SAMPRakNet::messagesLimit_ = 500; unsigned int SAMPRakNet::messageHoleLimit_ = 3000; @@ -22,6 +31,7 @@ bool SAMPRakNet::logCookies_ = false; ICore* SAMPRakNet::core_ = nullptr; FlatHashSet SAMPRakNet::incomingConnections_; RakNet::RakNetTime SAMPRakNet::gracePeriod_ = 0; +#endif uint16_t SAMPRakNet:: @@ -600,6 +610,102 @@ SAMPRakNet:: return buffer_; } +#ifdef BUILD_FOR_CLIENT +inline uint8_t transformAuthSha1(const uint8_t value, const uint8_t xorValue) +{ + static const uint8_t authHashTransformTable[] = { + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80, + 0x08, 0x06, 0x00, 0x00, 0x00, 0xE4, 0xB5, 0xB7, 0x0A, 0x00, 0x00, 0x00, + 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0B, 0x13, 0x00, 0x00, 0x0B, + 0x13, 0x01, 0x00, 0x9A, 0x9C, 0x18, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, + 0x4D, 0x41, 0x00, 0x00, 0xB1, 0x8E, 0x7C, 0xFB, 0x51, 0x93, 0x00, 0x00, + 0x00, 0x20, 0x63, 0x48, 0x52, 0x4D, 0x00, 0x00, 0x7A, 0x25, 0x00, 0x00, + 0x80, 0x83, 0x00, 0x00, 0xF9, 0xFF, 0x00, 0x00, 0x80, 0xE9, 0x00, 0x00, + 0x75, 0x30, 0x00, 0x00 + }; + + uint8_t result = value; + + for (const uint8_t &entry : authHashTransformTable) { + result = result ^ entry ^ xorValue; + } + + return result; +} + +inline const char * uint8ToHex(const uint8_t num) { + static uint8_t buffer[2]; + + buffer[0] = (num >> 4) & 0xF; + buffer[1] = num & 0xF; + + for (uint8_t &entry : buffer) { + uint8_t result = entry + '0'; + + if (result > '9') + result = entry + '7'; + + entry = result; + } + + return (char*)buffer; +} + +char* +SAMPRakNet:: + PrepareAuthkeyResponse(const char* initialKey) +{ + static const uint8_t code_from_CAnimManager_AddAnimation[] = + { + 0xFF, 0x25, 0x34, 0x39, // gta_sa.exe + 0x4D3AA0 + 0x4D, 0x00, 0x90, 0x90, // gta_sa.exe + 0x4D3AA4 + 0x90, 0x90, 0x56, 0x57, // gta_sa.exe + 0x4D3AAC + 0x50, 0x8B, 0x44, 0x24, // gta_sa.exe + 0x4D3AA8 + 0x14, 0x8D, 0x0C, 0x80 // gta_sa.exe + 0x4D3AB0 + }; + + RakNet::CSHA1 sha1; + sha1.Update( (unsigned char*) initialKey, strlen(initialKey) ); + sha1.Final(); + + auto sha1_finalized = (uint32_t*)sha1.GetHash(); + + uint32_t sha1_digits[5]; + for (int i = 0; i < 5; i++) { + uint32_t digit = sha1_finalized[i]; + // Flipping bytes order + digit = ((digit & 0xFF) << 24) | ((digit & 0xFF00) << 8) | + ((digit >> 8) & 0xFF00) | ((digit >> 24) & 0xFF); + sha1_digits[i] = digit; + } + + auto sha1_digits_u8 = (uint8_t*)&sha1_digits; + + static const uint8_t xorValues[] = { + 0x2F, 0x45, 0x6F, 0xDB + }; + + for (int i = 0; i < 20; i++) { + uint8_t xorVal = xorValues[i / 5]; + sha1_digits_u8[i] = transformAuthSha1(sha1_digits_u8[i], xorVal); + sha1_digits_u8[i] ^= code_from_CAnimManager_AddAnimation[i]; + } + + for (int i = 0; i < 40;) { + uint8_t hash = sha1_digits_u8[i / 2]; + const char* hashHex = uint8ToHex(hash); + authkeyBuffer_[i] = hashHex[0]; + i++; + authkeyBuffer_[i] = hashHex[1]; + i++; + } + + return authkeyBuffer_; +} +#endif + +#ifndef BUILD_FOR_CLIENT void SAMPRakNet:: HandleQuery(SOCKET instance, int outsize, const sockaddr_in& client, char const* buf, int insize) { @@ -967,3 +1073,4 @@ bool SAMPRakNet::OnConnectionRequest( return true; } +#endif diff --git a/SAMPRakNet.hpp b/SAMPRakNet.hpp index bf92c93..8a581ea 100644 --- a/SAMPRakNet.hpp +++ b/SAMPRakNet.hpp @@ -24,7 +24,13 @@ typedef int SOCKET; #define MAX_AUTH_RESPONSE_LEN (64) +#ifdef BUILD_FOR_CLIENT +#define AUTHKEY_RESPONSE_LEN (40) +#endif + +#ifndef BUILD_FOR_CLIENT #include "../../Server/Components/LegacyNetwork/Query/query.hpp" +#endif #include "Include/raknet/NetworkTypes.h" #include "Include/raknet/GetTime.h" @@ -36,6 +42,7 @@ typedef int SOCKET; class SAMPRakNet { public: +#ifndef BUILD_FOR_CLIENT enum AuthType { AuthType_Invalid, AuthType_Player, @@ -55,6 +62,7 @@ class SAMPRakNet core_ = core; srand(time(nullptr)); } +#endif static uint8_t * Decrypt(uint8_t const * src, int len); static uint8_t * Encrypt(uint8_t const * src, int len); @@ -62,6 +70,11 @@ class SAMPRakNet static uint16_t GetPort(); static void SetPort(uint16_t value); +#ifdef BUILD_FOR_CLIENT + static char * PrepareAuthkeyResponse(const char* initialKey); +#endif + +#ifndef BUILD_FOR_CLIENT static uint32_t GetToken() { return token_; } static void SeedToken() { token_ = rand(); } @@ -72,10 +85,17 @@ class SAMPRakNet static void SeedCookie(); static uint16_t GetCookie(unsigned int address); +#endif static void SetTimeout(unsigned int timeout) { timeout_ = timeout; } static unsigned int GetTimeout() { return timeout_; } +#ifdef BUILD_FOR_CLIENT + static void SetConnectionAsNpc(bool enabled) { connectAsNpc_ = enabled; } + static bool ShouldConnectAsNpc() { return connectAsNpc_; } +#endif + +#ifndef BUILD_FOR_CLIENT static void SetQuery(Query* query) { query_ = query; } static void SetLogCookies(bool log) { logCookies_ = log; } @@ -121,13 +141,23 @@ class SAMPRakNet RakNet::RakNetTime& minConnectionTick, RakNet::RakNetTime& minConnectionLogTick ); +#endif private: static uint8_t buffer_[MAXIMUM_MTU_SIZE]; +#ifdef BUILD_FOR_CLIENT + static char authkeyBuffer_[AUTHKEY_RESPONSE_LEN]; + static bool connectAsNpc_; +#endif +#ifndef BUILD_FOR_CLIENT static uint32_t token_; +#endif static uint16_t portNumber; +#ifndef BUILD_FOR_CLIENT static Query *query_; +#endif static unsigned int timeout_; +#ifndef BUILD_FOR_CLIENT static bool logCookies_; static unsigned int minConnectionTime_; static unsigned int messagesLimit_; @@ -137,4 +167,5 @@ class SAMPRakNet static ICore* core_; static FlatHashSet incomingConnections_; static RakNet::RakNetTime gracePeriod_; +#endif }; diff --git a/Source/RakPeer.cpp b/Source/RakPeer.cpp index ea39294..a4c1acb 100644 --- a/Source/RakPeer.cpp +++ b/Source/RakPeer.cpp @@ -2684,6 +2684,7 @@ RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromPlayerID( const PlayerI return 0; } +#ifndef BUILD_FOR_CLIENT // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, PlayerID playerId, const char *data, int byteSize ) { @@ -2805,6 +2806,7 @@ void RakPeer::OnConnectionRequest( RakPeer::RemoteSystemStruct *remoteSystem, un } } } +#endif // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::NotifyAndFlagForDisconnect(const PlayerID playerId, bool performImmediate, unsigned char orderingChannel) @@ -3524,8 +3526,9 @@ void RakPeer::CloseConnectionInternal( const PlayerID target, bool sendDisconnec // Found the index to stop remoteSystem->isActive = false; --activePeersCount; - +#ifndef BUILD_FOR_CLIENT SAMPRakNet::SetRequestingConnection(target.binaryAddress, false); +#endif // Reserve this reliability layer for ourselves //remoteSystemList[ i ].playerId = UNASSIGNED_PLAYER_ID; @@ -3906,6 +3909,7 @@ namespace RakNet } +#ifndef BUILD_FOR_CLIENT bool __stdcall ProcessBan(RakPeer* rakPeer, PlayerID playerId, const char* data, const int length) { if (rakPeer->IsBanned(rakPeer->PlayerIDToDottedIP(playerId))) @@ -3926,6 +3930,7 @@ namespace RakNet return false; } +#endif // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #ifdef _WIN32 @@ -3934,8 +3939,10 @@ namespace RakNet void ProcessNetworkPacket( const unsigned int binaryAddress, const unsigned short port, const char *data, const int length, RakPeer *rakPeer ) #endif { +#ifndef BUILD_FOR_CLIENT static RakNetTime minConnectionTick; static RakNetTime minConnectionLogTick; +#endif Packet *packet; PlayerID playerId; @@ -3944,6 +3951,7 @@ namespace RakNet playerId.binaryAddress = binaryAddress; playerId.port = port; +#ifndef BUILD_FOR_CLIENT const bool needsBanCheck = (data[0] == ID_OPEN_CONNECTION_REQUEST || data[0] == ID_OPEN_CONNECTION_REPLY || data[0] == ID_CONNECTION_ATTEMPT_FAILED); #if !defined(_COMPATIBILITY_1) @@ -3952,6 +3960,7 @@ namespace RakNet return; } #endif +#endif // We didn't check this datagram to see if it came from a connected system or not yet. // Therefore, this datagram must be under 17 bits - otherwise it may be normal network traffic as the min size for a raknet send is 17 bits @@ -4102,20 +4111,25 @@ namespace RakNet // Therefore, this datagram must be under 17 bits - otherwise it may be normal network traffic as the min size for a raknet send is 17 bits else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST && length == sizeof(unsigned char)*3) { +#ifndef BUILD_FOR_CLIENT if (!SAMPRakNet::OnConnectionRequest(rakPeer->connectionSocket, playerId, data, minConnectionTick, minConnectionLogTick)) { return; } +#endif for (i=0; i < rakPeer->messageHandlerList.Size(); i++) rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, playerId); // If this guy is already connected and they initiated the connection, ignore the connection request RakPeer::RemoteSystemStruct *rss = rakPeer->GetRemoteSystemFromPlayerID( playerId, true, true ); +#ifndef BUILD_FOR_CLIENT static unsigned int s_uiLastProcessedBinaryAddr = 0; static unsigned int s_uiLastProcessedConnTick = 0; +#endif if (rss==0 || rss->weInitiatedTheConnection==true) { +#ifndef BUILD_FOR_CLIENT if (rakPeer->GetNumberOfUnverifiedInstances(binaryAddress) > 30) { SAMPRakNet::GetCore()->printLn("Blocking %s due to a 'server full' attack (1)", playerId.ToString()); @@ -4131,6 +4145,7 @@ namespace RakNet rakPeer->AddToBanList(rakPeer->PlayerIDToDottedIP(playerId), 120000); return; } +#endif @@ -4141,8 +4156,10 @@ namespace RakNet unsigned char c[2]; if (rss) // If this guy is already connected remote system will be 0 { +#ifndef BUILD_FOR_CLIENT s_uiLastProcessedBinaryAddr = playerId.binaryAddress; s_uiLastProcessedConnTick = RakNet::GetTime(); +#endif c[0] = ID_OPEN_CONNECTION_REPLY; } else @@ -4154,10 +4171,12 @@ namespace RakNet rakPeer->messageHandlerList[i]->OnDirectSocketSend((char*)&c, 16, playerId); SocketLayer::Instance()->SendTo( rakPeer->connectionSocket, (char*)&c, 2, playerId.binaryAddress, playerId.port ); +#ifndef BUILD_FOR_CLIENT if (rss) { SAMPRakNet::SetRequestingConnection(binaryAddress, true); } +#endif return; } @@ -4180,6 +4199,74 @@ namespace RakNet } } +#ifdef BUILD_FOR_CLIENT + // SAMP clientside connection cookie verification + else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_COOKIE && length == sizeof(unsigned char)*3) + { + for (i=0; i < rakPeer->messageHandlerList.Size(); i++) + rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, playerId); + + unsigned char c[3]; + + c[0] = ID_OPEN_CONNECTION_REQUEST; + *(uint16_t*)&c[1] = (*(uint16_t*)&data[1]) ^ 0x6969; + + unsigned i; + for (i=0; i < rakPeer->messageHandlerList.Size(); i++) + rakPeer->messageHandlerList[i]->OnDirectSocketSend((char*)&c, 24, playerId); + + SocketLayer::Instance()->SendTo( rakPeer->connectionSocket, (char*)&c, 3, playerId.binaryAddress, playerId.port ); + return; + } + else if ( + ((unsigned char)(data)[0] == ID_CONNECTION_BANNED || (unsigned char)(data)[0] == ID_NO_FREE_INCOMING_CONNECTIONS) + && length <= sizeof(unsigned char)*2 + ) + { + // Remove the connection attempt from the buffered commands + RakPeer::RequestedConnectionStruct *rcsFirst, *rcs; + rcsFirst = rakPeer->requestedConnectionList.ReadLock(); + rcs=rcsFirst; + bool connectionAttemptCancelled=false; + while (rcs) + { + if (rcs->actionToTake==RakPeer::RequestedConnectionStruct::CONNECT && rcs->playerId==playerId) + { + connectionAttemptCancelled=true; + if (rcs==rcsFirst) + { + rakPeer->requestedConnectionList.ReadUnlock(); + rcsFirst=rakPeer->requestedConnectionList.ReadLock(); + rcs=rcsFirst; + } + else + { + // Hole in the middle + rcs->playerId=UNASSIGNED_PLAYER_ID; + rcs=rakPeer->requestedConnectionList.ReadLock(); + } + + continue; + } + + rcs=rakPeer->requestedConnectionList.ReadLock(); + } + + if (rcsFirst) + rakPeer->requestedConnectionList.CancelReadLock(rcsFirst); + + if (connectionAttemptCancelled) + { + // Tell user of connection attempt failed + packet=AllocPacket(sizeof( char )); + packet->data[ 0 ] = (unsigned char)(data)[0]; // Attempted a connection and couldn't + packet->bitSize = ( sizeof( char ) * 8); + packet->playerId = playerId; + packet->playerIndex = 65535; + rakPeer->AddPacketToProducer(packet); + } + } +#endif // See if this datagram came from a connected system remoteSystem = rakPeer->GetRemoteSystemFromPlayerID( playerId, true, true ); @@ -4219,6 +4306,7 @@ namespace RakNet } } +#ifndef BUILD_FOR_CLIENT if (shouldBanPeer && playerId.binaryAddress != LOCALHOST && GetTime() > SAMPRakNet::GetGracePeriod()) { const char* playerIp = rakPeer->PlayerIDToDottedIP(playerId); @@ -4226,13 +4314,16 @@ namespace RakNet rakPeer->AddToBanList(playerIp, banTime); rakPeer->CloseConnectionInternal(playerId, false, true, 0); } +#endif } else { +#ifndef BUILD_FOR_CLIENT if (ProcessBan(rakPeer, playerId, data, length)) { return; } +#endif for (i=0; i < rakPeer->messageHandlerList.Size(); i++) rakPeer->messageHandlerList[i]->OnDirectSocketReceive(data, length*8, playerId); @@ -4342,7 +4433,11 @@ namespace RakNet if ( errorCode != 0 && endThreads == false ) { #ifdef _DO_PRINTF +#ifndef BUILD_FOR_CLIENT SAMPRakNet::GetCore()->printLn( "Server RecvFrom critical failure!" ); +#else + printf( "Server RecvFrom critical failure!\n" ); +#endif #endif // Some kind of critical error // peer->isRecvfromThreadActive=false; @@ -4565,10 +4660,12 @@ namespace RakNet // } // else connection shutting down, don't bother telling the user +#ifndef BUILD_FOR_CLIENT #ifdef _DO_PRINTF const char* ipPort = playerId.ToString(true); SAMPRakNet::GetCore()->printLn("Connection dropped for player %s", ipPort); #endif +#endif CloseConnectionInternal( playerId, false, true, 0 ); continue; } @@ -4603,6 +4700,7 @@ namespace RakNet // Ping this guy if it is time to do so if ( remoteSystem->connectMode==RemoteSystemStruct::CONNECTED ) { +#ifndef BUILD_FOR_CLIENT // Taken from SA-MP 0.3.7 changes if (!remoteSystem->isLogon) { @@ -4615,6 +4713,7 @@ namespace RakNet continue; } } +#endif if (timeMS > remoteSystem->nextPingTime && (occasionalPing || remoteSystem->lowestPing == (unsigned short)-1)) { @@ -4679,9 +4778,12 @@ namespace RakNet { if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) { +#ifndef BUILD_FOR_CLIENT ParseConnectionRequestPacket(remoteSystem, playerId, (const char*)data, byteSize); +#endif delete [] data; } +#ifndef BUILD_FOR_CLIENT else if ((unsigned char)(data)[0] != ID_AUTH_KEY || !ParseConnectionAuthPacket(remoteSystem, playerId, data, byteSize)) { if ((unsigned char)(data)[0] == ID_RPC && remoteSystem->sampData.unverifiedRPCs++ < MAX_UNVERIFIED_RPCS) @@ -4713,6 +4815,18 @@ namespace RakNet delete[] data; } } +#else // !BUILD_FOR_CLIENT + else + { + CloseConnectionInternal(playerId, false, true, 0); + +#if !defined(_COMPATIBILITY_1) + AddToBanList(PlayerIDToDottedIP(playerId), remoteSystem->reliabilityLayer.GetTimeoutTime()); +#endif + if (data) + delete[] data; + } +#endif } else { @@ -4722,8 +4836,10 @@ namespace RakNet { // 04/27/06 This is wrong. With cross connections, we can both have initiated the connection are in state REQUESTED_CONNECTION // 04/28/06 Downgrading connections from connected will close the connection due to security at ((remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED && time > remoteSystem->connectionTime && time - remoteSystem->connectionTime > 10000)) +#ifndef BUILD_FOR_CLIENT if (remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED) ParseConnectionRequestPacket(remoteSystem, playerId, (const char*)data, byteSize); +#endif delete [] data; } else if ( (unsigned char) data[ 0 ] == ID_NEW_INCOMING_CONNECTION && byteSize == sizeof(unsigned char)+sizeof(unsigned int)+sizeof(unsigned short) ) @@ -4738,7 +4854,9 @@ namespace RakNet playerId==myPlayerId) // local system connect { +#ifndef BUILD_FOR_CLIENT SAMPRakNet::SetRequestingConnection(playerId.binaryAddress, false); +#endif remoteSystem->connectMode=RemoteSystemStruct::CONNECTED; PingInternal( playerId, true ); @@ -4934,7 +5052,9 @@ namespace RakNet AESKey[ i ] = data[ 1 + i ] ^ ( ( unsigned char* ) ( message ) ) [ i ]; // Connect this player assuming we have open slots +#ifndef BUILD_FOR_CLIENT OnConnectionRequest( remoteSystem, AESKey, true ); +#endif } delete [] data; } @@ -5039,12 +5159,54 @@ namespace RakNet // Tell the remote system the connection failed NotifyAndFlagForDisconnect(playerId, true, 0); #ifdef _DO_PRINTF +#ifndef BUILD_FOR_CLIENT SAMPRakNet::GetCore()->printLn( "Error: Got a connection accept when we didn't request the connection." ); +#else + printf( "Error: Got a connection accept when we didn't request the connection.\n" ); +#endif #endif if (data) delete [] data; } } +#ifdef BUILD_FOR_CLIENT + else if ( (unsigned char)(data)[0] == ID_AUTH_KEY && byteSize >= sizeof(unsigned char)*4 ) + { + RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); + inBitStream.IgnoreBits(8); + + unsigned char initialAuthKeyLen; + inBitStream.Read(initialAuthKeyLen); + + // Prebuilt server initial authkeys usually have length between 14-16 characters + // But, to support wider range and keep backward compatibility with original SAMP, + // We should make it possible to keep up to 256 chars + char initialAuthKey[256]; + if (initialAuthKeyLen > 1 && inBitStream.Read((char*)initialAuthKey, initialAuthKeyLen)) + { + // Server could send NULL terminator in the middle of string (why?) + // But send larger initialAuthKeyLen than the actual string length + // Original SAMP backward compatibility, so will use strlen() in PrepareAuthkeyResponse instead of initialAuthKeyLen + initialAuthKey[initialAuthKeyLen] = NULL; + + RakNet::BitStream outBitStream(sizeof(unsigned char)+sizeof(unsigned char)+AUTHKEY_RESPONSE_LEN); + outBitStream.Write((unsigned char)ID_AUTH_KEY); + if (!SAMPRakNet::ShouldConnectAsNpc()) + { + const char* response = SAMPRakNet::PrepareAuthkeyResponse(initialAuthKey); + outBitStream.Write((unsigned char)AUTHKEY_RESPONSE_LEN); + outBitStream.Write(response, AUTHKEY_RESPONSE_LEN); + } + else + { + // Server requires length & string including NULL terminator in case of NPC + outBitStream.Write((unsigned char)4); + outBitStream.Write("NPC", 4); + } + SendImmediate( (char*)outBitStream.GetData(), outBitStream.GetNumberOfBitsUsed(), SYSTEM_PRIORITY, RELIABLE, 0, playerId, false, false, RakNet::GetTime() ); + } + } +#endif else { if (data[0]>=(unsigned char)ID_RPC) diff --git a/Source/ReliabilityLayer.cpp b/Source/ReliabilityLayer.cpp index 0922088..651ca20 100644 --- a/Source/ReliabilityLayer.cpp +++ b/Source/ReliabilityLayer.cpp @@ -346,9 +346,11 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = false; //int numberOfAcksInFrame = 0; +#ifndef BUILD_FOR_CLIENT unsigned int acksLimit = 0; unsigned int messageHoleLimit = 0; unsigned int messagesLimit = 0; +#endif RakNetTimeNS time; bool indexFound; int count, size; @@ -379,9 +381,11 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe RakNet::BitStream socketData( (unsigned char*) buffer, length, false ); // Convert the incoming data to a bitstream for easy parsing time = RakNet::GetTimeNS(); +#ifndef BUILD_FOR_CLIENT acksLimit = SAMPRakNet::GetAcksLimit(); messageHoleLimit = SAMPRakNet::GetMessageHoleLimit(); messagesLimit = SAMPRakNet::GetMessagesLimit(); +#endif DataStructures::RangeList incomingAcks; socketData.Read(hasAcks); @@ -400,6 +404,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.acknowlegementsReceived += incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex; +#ifndef BUILD_FOR_CLIENT if (incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex > messageHoleLimit) { unsigned int receivedAcks = incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex; @@ -409,6 +414,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = true; return 1; } +#endif for (messageNumber=incomingAcks.ranges[i].minIndex; messageNumber >= incomingAcks.ranges[i].minIndex && messageNumber <= incomingAcks.ranges[i].maxIndex; messageNumber++) { @@ -451,6 +457,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.perFrameAcksLimitCounter = 0; } +#ifndef BUILD_FOR_CLIENT if (statistics.perSecondAcksLimitCounter > acksLimit) { const char * ipPort = playerId.ToString(true); @@ -459,6 +466,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = true; return 1; } +#endif } } @@ -590,6 +598,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.perSecondMessagesLimitCounter = statistics.perFrameMessagesLimitCounter; statistics.perFrameMessagesLimitCounter = 0; } +#ifndef BUILD_FOR_CLIENT if (statistics.perSecondMessagesLimitCounter > messagesLimit) { const char* ipPort = playerId.ToString(true); @@ -598,6 +607,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = true; return 1; } +#endif statistics.messagesReceived++; if (time - statistics.lastRecvMsgProcess > 1000000) @@ -800,6 +810,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe // RakAssert(waitingForOrderedPacketReadIndex[ internalPacket->orderingChannel ] < internalPacket->orderingIndex); statistics.orderedMessagesOutOfOrder++; +#ifndef BUILD_FOR_CLIENT if (statistics.orderedMessagesOutOfOrder > messageHoleLimit) { const char* ipPort = playerId.ToString(true); @@ -808,6 +819,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = true; return 1; } +#endif // This is a newer ordered packet than we are waiting for. Store it for future use AddToOrderingList( internalPacket ); @@ -1944,7 +1956,9 @@ InternalPacket* ReliabilityLayer::CreateInternalPacketFromBitStream( RakNet::Bit return 0; } +#ifndef BUILD_FOR_CLIENT SAMPRakNet::GetCore()->logLn(LogLevel::Warning, "dropping a split packet from client"); +#endif internalPacketPool.ReleasePointer(internalPacket); } else diff --git a/Source/SocketLayer.cpp b/Source/SocketLayer.cpp index 896c8ea..f6424dd 100644 --- a/Source/SocketLayer.cpp +++ b/Source/SocketLayer.cpp @@ -368,14 +368,17 @@ int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode ) if ( len != SOCKET_ERROR ) { +#ifndef BUILD_FOR_CLIENT if (len > 10 && data[0] == 'S' && data[1] == 'A' && data[2] == 'M' && data[3] == 'P') { SAMPRakNet::HandleQuery(s, len2, sa, data, len); return 1; } +#endif unsigned short portnum; portnum = ntohs( sa.sin_port ); +#ifndef BUILD_FOR_CLIENT uint8_t* decrypted = SAMPRakNet::Decrypt((uint8_t*)data, len); if (decrypted) { ProcessNetworkPacket(sa.sin_addr.s_addr, portnum, (char*)decrypted, len - 1, rakPeer); @@ -385,6 +388,9 @@ int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode ) uint8_t* const addr = reinterpret_cast(&sa.sin_addr.s_addr); SAMPRakNet::GetCore()->printLn("Dropping bad packet from %u.%u.%u.%u:%u!", addr[0], addr[1], addr[2], addr[3], sa.sin_port); } +#endif +#else // BUILD_FOR_CLIENT + ProcessNetworkPacket(sa.sin_addr.s_addr, portnum, data, len, rakPeer); #endif return 1; } @@ -451,6 +457,10 @@ int SocketLayer::SendTo( SOCKET s, const char *data, int length, unsigned int bi sa.sin_addr.s_addr = binaryAddress; sa.sin_family = AF_INET; +#ifdef BUILD_FOR_CLIENT + data = (const char*)SAMPRakNet::Encrypt((uint8_t*)data, length); + length++; +#endif do { // TODO - use WSASendTo which is faster. diff --git a/Source/rakserver.cpp b/Source/rakserver.cpp index 51b9b92..f34e5c6 100644 --- a/Source/rakserver.cpp +++ b/Source/rakserver.cpp @@ -459,6 +459,7 @@ RakPeer::RemoteSystemStruct* RakServer::GetRemoteSystemFromPlayerID(const Player return RakPeer::GetRemoteSystemFromPlayerID(playerId, false, false); } +#ifndef BUILD_FOR_CLIENT SAMPRakNet::RemoteSystemData RakServer::GetSAMPDataFromPlayerID(const PlayerID playerId) { RemoteSystemStruct* remoteSystem = RakPeer::GetRemoteSystemFromPlayerID(playerId, false, false); @@ -468,6 +469,7 @@ SAMPRakNet::RemoteSystemData RakServer::GetSAMPDataFromPlayerID(const PlayerID p return remoteSystem->sampData; } +#endif #ifdef _MSC_VER #pragma warning( pop ) From cc740dc356dda52f73c5dfa8362ec476808a86a4 Mon Sep 17 00:00:00 2001 From: Northn Date: Sat, 27 Jul 2024 23:36:05 +0400 Subject: [PATCH 4/5] BUILD_FOR_CLIENT -> RAKNET_BUILD_FOR_CLIENT --- CMakeLists.txt | 2 +- Include/raknet/NetworkTypes.h | 2 +- Include/raknet/RakPeer.h | 6 ++-- Include/raknet/RakServer.h | 2 +- Include/raknet/RakServerInterface.h | 2 +- SAMPRakNet.cpp | 12 ++++---- SAMPRakNet.hpp | 22 ++++++------- Source/RakPeer.cpp | 48 ++++++++++++++--------------- Source/ReliabilityLayer.cpp | 14 ++++----- Source/SocketLayer.cpp | 8 ++--- Source/rakserver.cpp | 2 +- 11 files changed, 60 insertions(+), 60 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44bea19..189c2b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,7 @@ if (WIN32) endif() if (RAKNET_BUILD_FOR_CLIENT) - target_compile_definitions(raknet PUBLIC -DBUILD_FOR_CLIENT) + target_compile_definitions(raknet PUBLIC -DRAKNET_BUILD_FOR_CLIENT) else() target_link_libraries(raknet OMP-SDK) endif() diff --git a/Include/raknet/NetworkTypes.h b/Include/raknet/NetworkTypes.h index 9670623..1878e9c 100644 --- a/Include/raknet/NetworkTypes.h +++ b/Include/raknet/NetworkTypes.h @@ -20,7 +20,7 @@ #include "RakNetDefines.h" #include "Export.h" -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT #include "../../SDK/include/types.hpp" #endif #include diff --git a/Include/raknet/RakPeer.h b/Include/raknet/RakPeer.h index ffa7373..c630b10 100644 --- a/Include/raknet/RakPeer.h +++ b/Include/raknet/RakPeer.h @@ -510,7 +510,7 @@ namespace RakNet bool setAESKey; /// true if security is enabled. RPCMap rpcMap; /// Mapping of RPC calls to single byte integers to save transmission bandwidth. enum ConnectMode {NO_ACTION, DISCONNECT_ASAP, DISCONNECT_ASAP_SILENTLY, DISCONNECT_ON_NO_ACK, REQUESTED_CONNECTION, HANDLING_CONNECTION_REQUEST, UNVERIFIED_SENDER, SET_ENCRYPTION_ON_MULTIPLE_16_BYTE_PACKET, CONNECTED} connectMode; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::RemoteSystemData sampData; #endif bool isLogon; @@ -533,7 +533,7 @@ namespace RakNet friend void* UpdateNetworkLoop( void* arguments ); #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT friend bool __stdcall ProcessBan(RakPeer* rakPeer, PlayerID playerId, const char* data, const int length); #endif @@ -549,7 +549,7 @@ namespace RakNet /// \return 0 if none RemoteSystemStruct *GetRemoteSystemFromPlayerID( const PlayerID playerID, bool calledFromNetworkThread, bool onlyActive) const; ///Parse out a connection request packet -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT void ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, PlayerID playerId, const char *data, int byteSize); bool ParseConnectionAuthPacket(RakPeer::RemoteSystemStruct* remoteSystem, PlayerID playerId, unsigned char* data, int byteSize); ///When we get a connection request from an ip / port, accept it unless full diff --git a/Include/raknet/RakServer.h b/Include/raknet/RakServer.h index b7a650b..3279c93 100644 --- a/Include/raknet/RakServer.h +++ b/Include/raknet/RakServer.h @@ -435,7 +435,7 @@ namespace RakNet /// \sa RakNetStatistics.h RakNetStatisticsStruct * GetStatistics( const PlayerID playerId ) override; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT /// Return the SAMPRakNet RemoteSystemData for a player ID virtual SAMPRakNet::RemoteSystemData GetSAMPDataFromPlayerID(const PlayerID playerId) override; #endif diff --git a/Include/raknet/RakServerInterface.h b/Include/raknet/RakServerInterface.h index e4054e1..cb487dd 100644 --- a/Include/raknet/RakServerInterface.h +++ b/Include/raknet/RakServerInterface.h @@ -429,7 +429,7 @@ namespace RakNet /// \sa RakNetStatistics.h virtual RakNetStatisticsStruct * GetStatistics( const PlayerID playerId )=0; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT /// Get SAMP data for a player from their ID virtual SAMPRakNet::RemoteSystemData GetSAMPDataFromPlayerID(const PlayerID playerId) = 0; #endif diff --git a/SAMPRakNet.cpp b/SAMPRakNet.cpp index 0175a1a..98127e6 100644 --- a/SAMPRakNet.cpp +++ b/SAMPRakNet.cpp @@ -9,19 +9,19 @@ #include uint8_t SAMPRakNet::buffer_[MAXIMUM_MTU_SIZE]; -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT char SAMPRakNet::authkeyBuffer_[AUTHKEY_RESPONSE_LEN]; bool SAMPRakNet::connectAsNpc_ = false; #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT uint32_t SAMPRakNet::token_; #endif uint16_t SAMPRakNet::portNumber = 7777; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT Query* SAMPRakNet::query_ = nullptr; #endif unsigned int SAMPRakNet::timeout_ = 10000; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT unsigned int SAMPRakNet::minConnectionTime_ = 0; unsigned int SAMPRakNet::messagesLimit_ = 500; unsigned int SAMPRakNet::messageHoleLimit_ = 3000; @@ -610,7 +610,7 @@ SAMPRakNet:: return buffer_; } -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT inline uint8_t transformAuthSha1(const uint8_t value, const uint8_t xorValue) { static const uint8_t authHashTransformTable[] = { @@ -705,7 +705,7 @@ SAMPRakNet:: } #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT void SAMPRakNet:: HandleQuery(SOCKET instance, int outsize, const sockaddr_in& client, char const* buf, int insize) { diff --git a/SAMPRakNet.hpp b/SAMPRakNet.hpp index 8a581ea..ddb8c4e 100644 --- a/SAMPRakNet.hpp +++ b/SAMPRakNet.hpp @@ -24,11 +24,11 @@ typedef int SOCKET; #define MAX_AUTH_RESPONSE_LEN (64) -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT #define AUTHKEY_RESPONSE_LEN (40) #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT #include "../../Server/Components/LegacyNetwork/Query/query.hpp" #endif @@ -42,7 +42,7 @@ typedef int SOCKET; class SAMPRakNet { public: -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT enum AuthType { AuthType_Invalid, AuthType_Player, @@ -70,11 +70,11 @@ class SAMPRakNet static uint16_t GetPort(); static void SetPort(uint16_t value); -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT static char * PrepareAuthkeyResponse(const char* initialKey); #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static uint32_t GetToken() { return token_; } static void SeedToken() { token_ = rand(); } @@ -90,12 +90,12 @@ class SAMPRakNet static void SetTimeout(unsigned int timeout) { timeout_ = timeout; } static unsigned int GetTimeout() { return timeout_; } -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT static void SetConnectionAsNpc(bool enabled) { connectAsNpc_ = enabled; } static bool ShouldConnectAsNpc() { return connectAsNpc_; } #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static void SetQuery(Query* query) { query_ = query; } static void SetLogCookies(bool log) { logCookies_ = log; } @@ -145,19 +145,19 @@ class SAMPRakNet private: static uint8_t buffer_[MAXIMUM_MTU_SIZE]; -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT static char authkeyBuffer_[AUTHKEY_RESPONSE_LEN]; static bool connectAsNpc_; #endif -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static uint32_t token_; #endif static uint16_t portNumber; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static Query *query_; #endif static unsigned int timeout_; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static bool logCookies_; static unsigned int minConnectionTime_; static unsigned int messagesLimit_; diff --git a/Source/RakPeer.cpp b/Source/RakPeer.cpp index a4c1acb..9e9b8d6 100644 --- a/Source/RakPeer.cpp +++ b/Source/RakPeer.cpp @@ -2684,7 +2684,7 @@ RakPeer::RemoteSystemStruct *RakPeer::GetRemoteSystemFromPlayerID( const PlayerI return 0; } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void RakPeer::ParseConnectionRequestPacket( RakPeer::RemoteSystemStruct *remoteSystem, PlayerID playerId, const char *data, int byteSize ) { @@ -3526,7 +3526,7 @@ void RakPeer::CloseConnectionInternal( const PlayerID target, bool sendDisconnec // Found the index to stop remoteSystem->isActive = false; --activePeersCount; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::SetRequestingConnection(target.binaryAddress, false); #endif @@ -3909,7 +3909,7 @@ namespace RakNet } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT bool __stdcall ProcessBan(RakPeer* rakPeer, PlayerID playerId, const char* data, const int length) { if (rakPeer->IsBanned(rakPeer->PlayerIDToDottedIP(playerId))) @@ -3939,7 +3939,7 @@ namespace RakNet void ProcessNetworkPacket( const unsigned int binaryAddress, const unsigned short port, const char *data, const int length, RakPeer *rakPeer ) #endif { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static RakNetTime minConnectionTick; static RakNetTime minConnectionLogTick; #endif @@ -3951,7 +3951,7 @@ namespace RakNet playerId.binaryAddress = binaryAddress; playerId.port = port; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT const bool needsBanCheck = (data[0] == ID_OPEN_CONNECTION_REQUEST || data[0] == ID_OPEN_CONNECTION_REPLY || data[0] == ID_CONNECTION_ATTEMPT_FAILED); #if !defined(_COMPATIBILITY_1) @@ -4111,7 +4111,7 @@ namespace RakNet // Therefore, this datagram must be under 17 bits - otherwise it may be normal network traffic as the min size for a raknet send is 17 bits else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_REQUEST && length == sizeof(unsigned char)*3) { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (!SAMPRakNet::OnConnectionRequest(rakPeer->connectionSocket, playerId, data, minConnectionTick, minConnectionLogTick)) { return; @@ -4123,13 +4123,13 @@ namespace RakNet // If this guy is already connected and they initiated the connection, ignore the connection request RakPeer::RemoteSystemStruct *rss = rakPeer->GetRemoteSystemFromPlayerID( playerId, true, true ); -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT static unsigned int s_uiLastProcessedBinaryAddr = 0; static unsigned int s_uiLastProcessedConnTick = 0; #endif if (rss==0 || rss->weInitiatedTheConnection==true) { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (rakPeer->GetNumberOfUnverifiedInstances(binaryAddress) > 30) { SAMPRakNet::GetCore()->printLn("Blocking %s due to a 'server full' attack (1)", playerId.ToString()); @@ -4156,7 +4156,7 @@ namespace RakNet unsigned char c[2]; if (rss) // If this guy is already connected remote system will be 0 { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT s_uiLastProcessedBinaryAddr = playerId.binaryAddress; s_uiLastProcessedConnTick = RakNet::GetTime(); #endif @@ -4171,7 +4171,7 @@ namespace RakNet rakPeer->messageHandlerList[i]->OnDirectSocketSend((char*)&c, 16, playerId); SocketLayer::Instance()->SendTo( rakPeer->connectionSocket, (char*)&c, 2, playerId.binaryAddress, playerId.port ); -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (rss) { SAMPRakNet::SetRequestingConnection(binaryAddress, true); @@ -4199,7 +4199,7 @@ namespace RakNet } } -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT // SAMP clientside connection cookie verification else if ((unsigned char)(data)[0] == ID_OPEN_CONNECTION_COOKIE && length == sizeof(unsigned char)*3) { @@ -4306,7 +4306,7 @@ namespace RakNet } } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (shouldBanPeer && playerId.binaryAddress != LOCALHOST && GetTime() > SAMPRakNet::GetGracePeriod()) { const char* playerIp = rakPeer->PlayerIDToDottedIP(playerId); @@ -4318,7 +4318,7 @@ namespace RakNet } else { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (ProcessBan(rakPeer, playerId, data, length)) { return; @@ -4433,7 +4433,7 @@ namespace RakNet if ( errorCode != 0 && endThreads == false ) { #ifdef _DO_PRINTF -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::GetCore()->printLn( "Server RecvFrom critical failure!" ); #else printf( "Server RecvFrom critical failure!\n" ); @@ -4660,7 +4660,7 @@ namespace RakNet // } // else connection shutting down, don't bother telling the user -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT #ifdef _DO_PRINTF const char* ipPort = playerId.ToString(true); SAMPRakNet::GetCore()->printLn("Connection dropped for player %s", ipPort); @@ -4700,7 +4700,7 @@ namespace RakNet // Ping this guy if it is time to do so if ( remoteSystem->connectMode==RemoteSystemStruct::CONNECTED ) { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT // Taken from SA-MP 0.3.7 changes if (!remoteSystem->isLogon) { @@ -4778,12 +4778,12 @@ namespace RakNet { if ( (unsigned char)(data)[0] == ID_CONNECTION_REQUEST ) { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT ParseConnectionRequestPacket(remoteSystem, playerId, (const char*)data, byteSize); #endif delete [] data; } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT else if ((unsigned char)(data)[0] != ID_AUTH_KEY || !ParseConnectionAuthPacket(remoteSystem, playerId, data, byteSize)) { if ((unsigned char)(data)[0] == ID_RPC && remoteSystem->sampData.unverifiedRPCs++ < MAX_UNVERIFIED_RPCS) @@ -4815,7 +4815,7 @@ namespace RakNet delete[] data; } } -#else // !BUILD_FOR_CLIENT +#else // !RAKNET_BUILD_FOR_CLIENT else { CloseConnectionInternal(playerId, false, true, 0); @@ -4836,7 +4836,7 @@ namespace RakNet { // 04/27/06 This is wrong. With cross connections, we can both have initiated the connection are in state REQUESTED_CONNECTION // 04/28/06 Downgrading connections from connected will close the connection due to security at ((remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED && time > remoteSystem->connectionTime && time - remoteSystem->connectionTime > 10000)) -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (remoteSystem->connectMode!=RemoteSystemStruct::CONNECTED) ParseConnectionRequestPacket(remoteSystem, playerId, (const char*)data, byteSize); #endif @@ -4854,7 +4854,7 @@ namespace RakNet playerId==myPlayerId) // local system connect { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::SetRequestingConnection(playerId.binaryAddress, false); #endif @@ -5052,7 +5052,7 @@ namespace RakNet AESKey[ i ] = data[ 1 + i ] ^ ( ( unsigned char* ) ( message ) ) [ i ]; // Connect this player assuming we have open slots -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT OnConnectionRequest( remoteSystem, AESKey, true ); #endif } @@ -5159,7 +5159,7 @@ namespace RakNet // Tell the remote system the connection failed NotifyAndFlagForDisconnect(playerId, true, 0); #ifdef _DO_PRINTF -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::GetCore()->printLn( "Error: Got a connection accept when we didn't request the connection." ); #else printf( "Error: Got a connection accept when we didn't request the connection.\n" ); @@ -5169,7 +5169,7 @@ namespace RakNet delete [] data; } } -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT else if ( (unsigned char)(data)[0] == ID_AUTH_KEY && byteSize >= sizeof(unsigned char)*4 ) { RakNet::BitStream inBitStream( (unsigned char *) data, byteSize, false ); diff --git a/Source/ReliabilityLayer.cpp b/Source/ReliabilityLayer.cpp index 651ca20..bd7b9f3 100644 --- a/Source/ReliabilityLayer.cpp +++ b/Source/ReliabilityLayer.cpp @@ -346,7 +346,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe shouldBanPeer = false; //int numberOfAcksInFrame = 0; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT unsigned int acksLimit = 0; unsigned int messageHoleLimit = 0; unsigned int messagesLimit = 0; @@ -381,7 +381,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe RakNet::BitStream socketData( (unsigned char*) buffer, length, false ); // Convert the incoming data to a bitstream for easy parsing time = RakNet::GetTimeNS(); -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT acksLimit = SAMPRakNet::GetAcksLimit(); messageHoleLimit = SAMPRakNet::GetMessageHoleLimit(); messagesLimit = SAMPRakNet::GetMessagesLimit(); @@ -404,7 +404,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.acknowlegementsReceived += incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex > messageHoleLimit) { unsigned int receivedAcks = incomingAcks.ranges[i].maxIndex - incomingAcks.ranges[i].minIndex; @@ -457,7 +457,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.perFrameAcksLimitCounter = 0; } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (statistics.perSecondAcksLimitCounter > acksLimit) { const char * ipPort = playerId.ToString(true); @@ -598,7 +598,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe statistics.perSecondMessagesLimitCounter = statistics.perFrameMessagesLimitCounter; statistics.perFrameMessagesLimitCounter = 0; } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (statistics.perSecondMessagesLimitCounter > messagesLimit) { const char* ipPort = playerId.ToString(true); @@ -810,7 +810,7 @@ bool ReliabilityLayer::HandleSocketReceiveFromConnectedPlayer( const char *buffe // RakAssert(waitingForOrderedPacketReadIndex[ internalPacket->orderingChannel ] < internalPacket->orderingIndex); statistics.orderedMessagesOutOfOrder++; -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (statistics.orderedMessagesOutOfOrder > messageHoleLimit) { const char* ipPort = playerId.ToString(true); @@ -1956,7 +1956,7 @@ InternalPacket* ReliabilityLayer::CreateInternalPacketFromBitStream( RakNet::Bit return 0; } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::GetCore()->logLn(LogLevel::Warning, "dropping a split packet from client"); #endif internalPacketPool.ReleasePointer(internalPacket); diff --git a/Source/SocketLayer.cpp b/Source/SocketLayer.cpp index f6424dd..4166f5b 100644 --- a/Source/SocketLayer.cpp +++ b/Source/SocketLayer.cpp @@ -368,7 +368,7 @@ int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode ) if ( len != SOCKET_ERROR ) { -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT if (len > 10 && data[0] == 'S' && data[1] == 'A' && data[2] == 'M' && data[3] == 'P') { SAMPRakNet::HandleQuery(s, len2, sa, data, len); @@ -378,7 +378,7 @@ int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode ) unsigned short portnum; portnum = ntohs( sa.sin_port ); -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT uint8_t* decrypted = SAMPRakNet::Decrypt((uint8_t*)data, len); if (decrypted) { ProcessNetworkPacket(sa.sin_addr.s_addr, portnum, (char*)decrypted, len - 1, rakPeer); @@ -389,7 +389,7 @@ int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode ) SAMPRakNet::GetCore()->printLn("Dropping bad packet from %u.%u.%u.%u:%u!", addr[0], addr[1], addr[2], addr[3], sa.sin_port); } #endif -#else // BUILD_FOR_CLIENT +#else // RAKNET_BUILD_FOR_CLIENT ProcessNetworkPacket(sa.sin_addr.s_addr, portnum, data, len, rakPeer); #endif return 1; @@ -457,7 +457,7 @@ int SocketLayer::SendTo( SOCKET s, const char *data, int length, unsigned int bi sa.sin_addr.s_addr = binaryAddress; sa.sin_family = AF_INET; -#ifdef BUILD_FOR_CLIENT +#ifdef RAKNET_BUILD_FOR_CLIENT data = (const char*)SAMPRakNet::Encrypt((uint8_t*)data, length); length++; #endif diff --git a/Source/rakserver.cpp b/Source/rakserver.cpp index f34e5c6..6fff9cd 100644 --- a/Source/rakserver.cpp +++ b/Source/rakserver.cpp @@ -459,7 +459,7 @@ RakPeer::RemoteSystemStruct* RakServer::GetRemoteSystemFromPlayerID(const Player return RakPeer::GetRemoteSystemFromPlayerID(playerId, false, false); } -#ifndef BUILD_FOR_CLIENT +#ifndef RAKNET_BUILD_FOR_CLIENT SAMPRakNet::RemoteSystemData RakServer::GetSAMPDataFromPlayerID(const PlayerID playerId) { RemoteSystemStruct* remoteSystem = RakPeer::GetRemoteSystemFromPlayerID(playerId, false, false); From 23dd5ba63fe6e4d466bfc53c5c07ef9954be015d Mon Sep 17 00:00:00 2001 From: Northn Date: Fri, 29 Aug 2025 02:32:18 +0400 Subject: [PATCH 5/5] Removed unused include in RakPeer --- Include/raknet/RakPeer.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/Include/raknet/RakPeer.h b/Include/raknet/RakPeer.h index 684668f..d9009a0 100644 --- a/Include/raknet/RakPeer.h +++ b/Include/raknet/RakPeer.h @@ -18,8 +18,6 @@ #ifndef __RAK_PEER_H #define __RAK_PEER_H -#include - #include "Export.h" #include "RakPeerInterface.h" #include "ReliabilityLayer.h"