From 967378ae9812052078ceae7059be3c6ba6ff4dbe Mon Sep 17 00:00:00 2001 From: rna88 Date: Thu, 15 Mar 2018 20:12:33 -0700 Subject: [PATCH 1/3] SInPacket now tracks decryption/decompression failure. Previously there was no way to tell if an incoming packet was incorrectly formatted. The new function SInPacket.isValid() can be called after decryption/decompression to determine if the packet wasn't decrypted and/or decompressed properly. * Added "valid" attribute to SInPacket. * Decryption routine unsets "valid" if buffer is not divisible by 16. * Decompression routine unsets "valid" on receiving zlib error code. --- include/SPacket.h | 4 ++++ source/SPacket.cpp | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/SPacket.h b/include/SPacket.h index 6d9d1e9..b6a2548 100644 --- a/include/SPacket.h +++ b/include/SPacket.h @@ -154,10 +154,14 @@ class SInPacket /// Returns the size in bytes of the packet. u32 getSize(); + /// Returns true if the packet decompresses/decrypts successfully. + bool isValid(); + private: u32 pos; core::array buff; u16 playerid; + bool valid; }; } // Close Net Namespace diff --git a/source/SPacket.cpp b/source/SPacket.cpp index 55bd1c0..9d46a5a 100644 --- a/source/SPacket.cpp +++ b/source/SPacket.cpp @@ -225,7 +225,7 @@ void SOutPacket::decryptPacket(const c8 key[16]) buff = tmpbuff; } -SInPacket::SInPacket(const c8* buff, const u32 size) : pos(0), playerid(0) +SInPacket::SInPacket(const c8* buff, const u32 size) : pos(0), playerid(0), valid(true) { SInPacket::buff.set_used(size); memcpy(SInPacket::buff.pointer(), buff, size); @@ -435,10 +435,19 @@ void SInPacket::deCompressPacket() newBuff.set_used(newSize); uLongf destLen = newSize; - uncompress((Bytef*)newBuff.pointer(), &destLen, (Bytef*)buff.pointer() + 4, buff.size() - 4); - newBuff.set_used(destLen); - - buff = newBuff; + int ret = uncompress((Bytef*)newBuff.pointer(), &destLen, (Bytef*)buff.pointer() + 4, buff.size() - 4); + + if (ret != Z_OK) + { + valid = false; + newBuff.set_used(0); + newBuff.clear(); + } + else + { + newBuff.set_used(destLen); + buff = newBuff; + } } void SInPacket::encryptPacket(const c8 key[16]) @@ -457,6 +466,12 @@ void SInPacket::encryptPacket(const c8 key[16]) void SInPacket::decryptPacket(const c8 key[16]) { + if (buff.size() % 16 != 0) + { + valid = false; + return; + } + CEncryption::SetEncryptionKey((u8*)&key[0]); const u32 newSize = buff.size(); core::array tmpbuff; @@ -473,5 +488,10 @@ u32 SInPacket::getSize() return buff.size(); } +bool SInPacket::isValid() +{ + return valid; +} + } // Close Net Namespace } // Close Irr namespace From 8e5d81f91384d56c538e649c2d1a2a8e8858aaff Mon Sep 17 00:00:00 2001 From: Otto Naderer Date: Tue, 7 Jun 2022 22:01:26 +0200 Subject: [PATCH 2/3] cmake op for native irrlicht --- CMakeLists.txt | 19 ++++++++++++++++--- include/SPacket.h | 4 ++++ source/SPacket.cpp | 6 ++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2236f4b..990bcff 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.10) +option(OP_COMPILE_WITH_IRRLICHT "compile irrnet with native irrlicht datatypes" ON) + set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -10,7 +12,6 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/lib") set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) #irrnet stuff -add_definitions(-DCOMPILE_WITH_IRRLICHT) set(IRRNET_TARGET irrnet) set(IRRNET_SRC_DIR "${CMAKE_CURRENT_LIST_DIR}/source") set(IRRNET_INC_DIR "${CMAKE_CURRENT_LIST_DIR}/include") @@ -27,15 +28,27 @@ set(ZLIB_DIR "${CMAKE_CURRENT_LIST_DIR}/zlib") set(ZLIB_INC_DIR ${ZLIB_DIR}/include) add_subdirectory(${ZLIB_DIR} EXCLUDE_FROM_ALL) +#irrlicht stuff +set(IRRLICHT_INC_DIR /usr/include/irrlicht) + project(irrnet) -include_directories(/usr/include/irrlicht) - add_library(${IRRNET_TARGET} STATIC + ${IRRNET_INC_DIR}/CEncryption.h + ${IRRNET_INC_DIR}/CNetManager.h + ${IRRNET_INC_DIR}/INetManager.h + ${IRRNET_INC_DIR}/irrNet.h + ${IRRNET_INC_DIR}/SPacket.h ${IRRNET_SRC_DIR}/CNetManager.cpp ${IRRNET_SRC_DIR}/SPacket.cpp ) +if (OP_COMPILE_WITH_IRRLICHT) + message(STATUS "compile with native irrlicht") + add_definitions(-DCOMPILE_WITH_IRRLICHT) + target_include_directories(${IRRNET_TARGET} PUBLIC ${IRRLICHT_INC_DIR}) +endif() + target_include_directories(${IRRNET_TARGET} PUBLIC ${IRRNET_INC_DIR} ${ENET_INC_DIR} ${ZLIB_INC_DIR}) diff --git a/include/SPacket.h b/include/SPacket.h index e4d4725..303c0a3 100644 --- a/include/SPacket.h +++ b/include/SPacket.h @@ -49,8 +49,10 @@ class SOutPacket SOutPacket& operator << (const f64 data); /// Adds data to the packet. SOutPacket& operator << (const core::vector3df& data); +#ifdef COMPILE_WITH_IRRLICHT /// Adds a quaternion to the packet SOutPacket& operator << (const core::quaternion&); +#endif /// Adds data to the packet. SOutPacket& operator << (const c8* string); /// Adds data to the packet. @@ -116,8 +118,10 @@ class SInPacket void operator >> (f64& data); /// Gets the next item in the packet based on the size of the variable. void operator >> (core::vector3df& data); +#ifdef COMPILE_WITH_IRRLICHT /// Gets the next item in the packet based on the size of the variable. void operator >> (core::quaternion& data); +#endif /// Gets the next item in the packet based on the size of the variable. void operator >> (char* string); /// Gets the next item in the packet based on the size of the variable. diff --git a/source/SPacket.cpp b/source/SPacket.cpp index e9eaa26..83ebd64 100644 --- a/source/SPacket.cpp +++ b/source/SPacket.cpp @@ -106,13 +106,14 @@ SOutPacket& SOutPacket::operator << (const core::vector3df& data) memcpy(buff.pointer() + buff.size() - 12, &data.X, 12); return *this; } - +#ifdef COMPILE_WITH_IRRLICHT SOutPacket& SOutPacket::operator << (const core::quaternion& data) { enlargeBuffer(16); memcpy(buff.pointer() + buff.size() - 16, &data.X, 16); return *this; } +#endif SOutPacket& SOutPacket::operator << (const c8* string) { @@ -304,12 +305,13 @@ void SInPacket::operator >> (core::vector3df &data) memcpy(&data.X,getData()+pos,12); pos+=12; } - +#ifdef COMPILE_WITH_IRRLICHT void SInPacket::operator >> (core::quaternion &data) { memcpy(&data.X,getData()+pos,16); pos+=16; } +#endif void SInPacket::operator >> (char *string) { From 0a39403f8d7212c3ac50a48fc52380219e57f4f6 Mon Sep 17 00:00:00 2001 From: Otto Naderer Date: Wed, 8 Jun 2022 00:38:01 +0200 Subject: [PATCH 3/3] fixes to cmake cfg windows --- CMakeLists.txt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 990bcff..cfc55a5 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,11 +25,14 @@ add_subdirectory(${ENET_DIR} EXCLUDE_FROM_ALL) #zlib stuff set(ZLIB_TARGET zlibstatic) set(ZLIB_DIR "${CMAKE_CURRENT_LIST_DIR}/zlib") -set(ZLIB_INC_DIR ${ZLIB_DIR}/include) +set(ZLIB_INC_DIR ${ZLIB_DIR}) +set(ZLIB_CONF_DIR ${CMAKE_CURRENT_BINARY_DIR}/zlib) add_subdirectory(${ZLIB_DIR} EXCLUDE_FROM_ALL) #irrlicht stuff -set(IRRLICHT_INC_DIR /usr/include/irrlicht) +if (NOT IRRLICHT_INC_DIR) + set(IRRLICHT_INC_DIR /usr/include/irrlicht) +endif() project(irrnet) @@ -47,8 +50,13 @@ add_library(${IRRNET_TARGET} STATIC if (OP_COMPILE_WITH_IRRLICHT) message(STATUS "compile with native irrlicht") add_definitions(-DCOMPILE_WITH_IRRLICHT) + message(STATUS "irrlicht dir: " ${IRRLICHT_INC_DIR}) target_include_directories(${IRRNET_TARGET} PUBLIC ${IRRLICHT_INC_DIR}) endif() -target_include_directories(${IRRNET_TARGET} PUBLIC ${IRRNET_INC_DIR} ${ENET_INC_DIR} ${ZLIB_INC_DIR}) +target_link_libraries(${IRRNET_TARGET} ${ZLIB_TARGET} ${ENET_TARGET}) + +target_include_directories(${IRRNET_TARGET} PUBLIC ${IRRNET_INC_DIR} ${ENET_INC_DIR} ${ZLIB_INC_DIR}) +message(STATUS "conf dir" ${ZLIB_CONF_DIR}) +target_include_directories(${IRRNET_TARGET} PRIVATE ${ZLIB_CONF_DIR})