From a925463fdfc8eb7eb64432f8eef850ee4a7173d2 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 18 Jul 2021 21:08:51 -0400 Subject: [PATCH] Print Error Message If RakNet Fails To Start --- VERSION | 2 +- docs/CHANGELOG.md | 3 ++ libreborn/include/libreborn/libreborn.h | 2 +- libreborn/include/libreborn/minecraft.h | 18 ++++++++++++ libreborn/src/libreborn.c | 4 +-- mods/src/misc/misc.c | 38 ++++++++++++++++++++++++- mods/src/server/server.cpp | 2 +- 7 files changed, 63 insertions(+), 6 deletions(-) diff --git a/VERSION b/VERSION index 7d2ed7c7..cd57a8b9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.4 +2.1.5 diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f6307c5c..db43bc34 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +**2.1.5** +* Print Error Message If RakNet Fails To Start + **2.1.4** * Fix RakNet::RakString Security Bug diff --git a/libreborn/include/libreborn/libreborn.h b/libreborn/include/libreborn/libreborn.h index 35ae28f8..6852a8ad 100644 --- a/libreborn/include/libreborn/libreborn.h +++ b/libreborn/include/libreborn/libreborn.h @@ -23,7 +23,7 @@ void _overwrite_calls(const char *file, int line, void *start, void *target); void _overwrite(const char *file, int line, void *start, void *target); #define overwrite(start, target) _overwrite(__FILE__, __LINE__, start, target); -void _patch(const char *file, int line, void *start, unsigned char patch[]); +void _patch(const char *file, int line, void *start, unsigned char patch[4]); #define patch(start, patch) _patch(__FILE__, __LINE__, start, patch); void _patch_address(const char *file, int line, void *start, void *target); diff --git a/libreborn/include/libreborn/minecraft.h b/libreborn/include/libreborn/minecraft.h index a6c57e5f..f2b43648 100644 --- a/libreborn/include/libreborn/minecraft.h +++ b/libreborn/include/libreborn/minecraft.h @@ -389,6 +389,24 @@ static uint32_t RakNetInstance_peer_property_offset = 0x4; // RakNet::RakPeer * // RakNet::RakPeer +typedef enum { + RAKNET_STARTED = 0, + RAKNET_ALREADY_STARTED, + INVALID_SOCKET_DESCRIPTORS, + INVALID_MAX_CONNECTIONS, + SOCKET_FAMILY_NOT_SUPPORTED, + SOCKET_PORT_ALREADY_IN_USE, + SOCKET_FAILED_TO_BIND, + SOCKET_FAILED_TEST_SEND, + PORT_CANNOT_BE_ZERO, + FAILED_TO_CREATE_NETWORK_THREAD, + COULD_NOT_GENERATE_GUID, + STARTUP_OTHER_FAILURE +} RakNet_StartupResult; +typedef RakNet_StartupResult (*RakNet_RakPeer_Startup_t)(unsigned char *rak_peer, unsigned short maxConnections, unsigned char *socketDescriptors, uint32_t socketDescriptorCount, int32_t threadPriority); +static RakNet_RakPeer_Startup_t RakNet_RakPeer_Startup = (RakNet_RakPeer_Startup_t) 0xe1654; +static void *RakNet_RakPeer_Startup_vtable_addr = (void *) 0x135438; + typedef struct RakNet_SystemAddress (*RakNet_RakPeer_GetSystemAddressFromGuid_t)(unsigned char *rak_peer, struct RakNet_RakNetGUID guid); static uint32_t RakNet_RakPeer_GetSystemAddressFromGuid_vtable_offset = 0xd0; diff --git a/libreborn/src/libreborn.c b/libreborn/src/libreborn.c index e9a0471a..78402ae0 100644 --- a/libreborn/src/libreborn.c +++ b/libreborn/src/libreborn.c @@ -142,7 +142,7 @@ void _overwrite(const char *file, int line, void *start, void *target) { #endif // Patch Instruction -void _patch(const char *file, int line, void *start, unsigned char patch[]) { +void _patch(const char *file, int line, void *start, unsigned char patch[4]) { if (((uint32_t) start) % 4 != 0) { ERR("%s", "Invalid Address"); } @@ -171,6 +171,6 @@ void _patch(const char *file, int line, void *start, unsigned char patch[]) { // Patch Address void _patch_address(const char *file, int line, void *start, void *target) { uint32_t addr = (uint32_t) target; - unsigned char patch_data[4] = {addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff}; + unsigned char *patch_data = (unsigned char *) &addr; _patch(file, line, start, patch_data); } diff --git a/mods/src/misc/misc.c b/mods/src/misc/misc.c index 38d69bac..cb2f6f13 100644 --- a/mods/src/misc/misc.c +++ b/mods/src/misc/misc.c @@ -74,6 +74,39 @@ static unsigned char *RakNet_RakString_injection(unsigned char *rak_string, cons return (*RakNet_RakString)(rak_string, "%s", format); } +// Print Error Message If RakNet Startup Fails +static char *RAKNET_ERROR_NAMES[] = { + "Success", + "Already Started", + "Invalid Socket Descriptors", + "Invalid Max Connections", + "Socket Family Not Supported", + "Part Already In Use", + "Failed To Bind Port", + "Failed Test Send", + "Port Cannot Be 0", + "Failed To Create Network Thread", + "Couldn't Generate GUID", + "Unknown" +}; +#ifdef MCPI_SERVER_MODE +#define PRINT_RAKNET_STARTUP_FAILURE ERR +#else +#define PRINT_RAKNET_STARTUP_FAILURE WARN +#endif +static RakNet_StartupResult RakNetInstance_host_RakNet_RakPeer_Startup_injection(unsigned char *rak_peer, unsigned short maxConnections, unsigned char *socketDescriptors, uint32_t socketDescriptorCount, int32_t threadPriority) { + // Call Original Method + RakNet_StartupResult result = (*RakNet_RakPeer_Startup)(rak_peer, maxConnections, socketDescriptors, socketDescriptorCount, threadPriority); + + // Print Error + if (result != RAKNET_STARTED) { + PRINT_RAKNET_STARTUP_FAILURE("Failed To Start RakNet: %s", RAKNET_ERROR_NAMES[result]); + } + + // Return + return result; +} + // Init void init_misc() { if (feature_has("Remove Invalid Item Background", 0)) { @@ -93,7 +126,10 @@ void init_misc() { patch_address(LoginPacket_read_vtable_addr, (void *) LoginPacket_read_injection); // Fix RakNet::RakString Security Bug - overwrite_calls((void *) RakNet_RakString, RakNet_RakString_injection); + overwrite_calls((void *) RakNet_RakString, (void *) RakNet_RakString_injection); + + // Print Error Message If RakNet Startup Fails + overwrite_call((void *) 0x73778, (void *) RakNetInstance_host_RakNet_RakPeer_Startup_injection); // Init C++ _init_misc_cpp(); diff --git a/mods/src/server/server.cpp b/mods/src/server/server.cpp index ba881ecf..ae187a4c 100644 --- a/mods/src/server/server.cpp +++ b/mods/src/server/server.cpp @@ -83,8 +83,8 @@ static void start_world(unsigned char *minecraft) { if (!only_generate) { // Open Port int port = get_server_properties().get_int("port", DEFAULT_PORT); - (*Minecraft_hostMultiplayer)(minecraft, port); INFO("Listening On: %i", port); + (*Minecraft_hostMultiplayer)(minecraft, port); } // Open ProgressScreen