From 8a3e6d3fc4328e4b8e1216823bdbbde94bbaf46a Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 23 Oct 2022 20:57:44 +0200 Subject: [PATCH] Stabilize raw socket communication --- src/client/component/network.cpp | 72 ++++++++++++++++++-------------- src/client/game/symbols.hpp | 10 +++-- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/client/component/network.cpp b/src/client/component/network.cpp index ba3bb160..1bc4ac94 100644 --- a/src/client/component/network.cpp +++ b/src/client/component/network.cpp @@ -57,6 +57,39 @@ namespace network a.jmp(0x14134D14B_g); } + + bool socket_set_blocking(const SOCKET s, const bool blocking) + { + unsigned long mode = blocking ? 0 : 1; + return ioctlsocket(s, FIONBIO, &mode) == 0; + } + + void create_ip_socket() + { + auto& s = *game::ip_socket; + if (s) + { + return; + } + + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + socket_set_blocking(s, false); + + const auto address = htonl(INADDR_ANY); + const auto port = htons(28960); + + sockaddr_in server_addr{}; + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = address; + server_addr.sin_port = port; + + if (bind(s, reinterpret_cast(&server_addr), sizeof(server_addr)) == + SOCKET_ERROR) + { + throw std::runtime_error("Failed to bind socket"); + } + } } class component final : public component_interface @@ -70,9 +103,14 @@ namespace network utils::hook::nop(0x142333056_g, 5); // don't add sock to packet utils::hook::set(0x14233305E_g, 0); // don't add checksum to packet + utils::hook::nop(0x142332E43_g, 5); // don't read local net id utils::hook::set(0x142332E55_g, 0); // clear local net id utils::hook::jump(0x142332E72_g, 0x142332E8E_g); // skip checksum parsing + // Fix netadr types + utils::hook::set(0x142332DE5_g, game::NA_RAWIP); // raw socket type + utils::hook::set(0x142332E00_g, game::NA_IP); // dw socket type + // intercept command handling utils::hook::jump(0x14134D146_g, utils::hook::assemble(handle_command_stub)); @@ -83,48 +121,20 @@ namespace network { MessageBoxA(0, 0, 0, 0); - static auto& ip_socket = *(SOCKET*)0x157E77818_g; - if (!ip_socket) - { - ip_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - - const auto address = htonl(INADDR_ANY); - const auto port = htons(28960); - - sockaddr_in server_addr{}; - server_addr.sin_family = AF_INET; - server_addr.sin_addr.s_addr = address; - server_addr.sin_port = port; - - if (bind(ip_socket, reinterpret_cast(&server_addr), sizeof(server_addr)) == - SOCKET_ERROR) - { - throw std::runtime_error("Failed to bind socket"); - } - } + create_ip_socket(); std::string data = utils::string::va("\xFF\xFF\xFF\xFF" "getservers S1 %i full empty", 1); game::netadr_t addr{}; addr.type = game::NA_RAWIP; addr.port = 20810; - *(unsigned long*)&addr.ipv4.a = inet_addr("116.203.183.23"); + *reinterpret_cast(&addr.ipv4.a) = inet_addr("116.203.183.23"); addr.localNetID = game::NS_CLIENT1; - game::NET_SendPacket(game::NS_CLIENT1, (int)data.size(), data.data(), &addr); + game::NET_SendPacket(game::NS_CLIENT1, static_cast(data.size()), data.data(), &addr); } } }).detach();*/ } - - void pre_destroy() override - { - static auto& ip_socket = *(SOCKET*)0x157E77818_g; - if (ip_socket) - { - closesocket(ip_socket); - ip_socket = 0; - } - } }; } diff --git a/src/client/game/symbols.hpp b/src/client/game/symbols.hpp index 9fe9357c..aaf955a4 100644 --- a/src/client/game/symbols.hpp +++ b/src/client/game/symbols.hpp @@ -36,15 +36,17 @@ namespace game WEAK symbol Dvar_GetString{0x1422BFFF0_g}; WEAK symbol Dvar_SetFromStringByName{0x1422C7F60_g}; - // Variables - WEAK symbol cmd_functions{0x15689FF58_g}; - WEAK symbol sv_cmd_args{0x15689CE30_g}; - // Rendering WEAK symbol R_AddCmdDrawText{ 0x141CD98D0_g }; + // Variables + WEAK symbol cmd_functions{0x15689FF58_g}; + WEAK symbol sv_cmd_args{0x15689CE30_g}; + + WEAK symbol ip_socket{0x157E77818_g}; + namespace s_wcd { WEAK symbol codLogo{0x157E77A50_g};