Stabilize raw socket communication

This commit is contained in:
momo5502 2022-10-23 20:57:44 +02:00
parent 250364903e
commit 8a3e6d3fc4
2 changed files with 47 additions and 35 deletions

View File

@ -57,6 +57,39 @@ namespace network
a.jmp(0x14134D14B_g); 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<sockaddr*>(&server_addr), sizeof(server_addr)) ==
SOCKET_ERROR)
{
throw std::runtime_error("Failed to bind socket");
}
}
} }
class component final : public component_interface 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::nop(0x142333056_g, 5); // don't add sock to packet
utils::hook::set<uint8_t>(0x14233305E_g, 0); // don't add checksum to packet utils::hook::set<uint8_t>(0x14233305E_g, 0); // don't add checksum to packet
utils::hook::nop(0x142332E43_g, 5); // don't read local net id
utils::hook::set<uint8_t>(0x142332E55_g, 0); // clear local net id utils::hook::set<uint8_t>(0x142332E55_g, 0); // clear local net id
utils::hook::jump(0x142332E72_g, 0x142332E8E_g); // skip checksum parsing utils::hook::jump(0x142332E72_g, 0x142332E8E_g); // skip checksum parsing
// Fix netadr types
utils::hook::set<uint32_t>(0x142332DE5_g, game::NA_RAWIP); // raw socket type
utils::hook::set<uint32_t>(0x142332E00_g, game::NA_IP); // dw socket type
// intercept command handling // intercept command handling
utils::hook::jump(0x14134D146_g, utils::hook::assemble(handle_command_stub)); utils::hook::jump(0x14134D146_g, utils::hook::assemble(handle_command_stub));
@ -83,48 +121,20 @@ namespace network
{ {
MessageBoxA(0, 0, 0, 0); MessageBoxA(0, 0, 0, 0);
static auto& ip_socket = *(SOCKET*)0x157E77818_g; create_ip_socket();
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<sockaddr*>(&server_addr), sizeof(server_addr)) ==
SOCKET_ERROR)
{
throw std::runtime_error("Failed to bind socket");
}
}
std::string data = utils::string::va("\xFF\xFF\xFF\xFF" "getservers S1 %i full empty", 1); std::string data = utils::string::va("\xFF\xFF\xFF\xFF" "getservers S1 %i full empty", 1);
game::netadr_t addr{}; game::netadr_t addr{};
addr.type = game::NA_RAWIP; addr.type = game::NA_RAWIP;
addr.port = 20810; addr.port = 20810;
*(unsigned long*)&addr.ipv4.a = inet_addr("116.203.183.23"); *reinterpret_cast<unsigned long*>(&addr.ipv4.a) = inet_addr("116.203.183.23");
addr.localNetID = game::NS_CLIENT1; 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<int>(data.size()), data.data(), &addr);
} }
} }
}).detach();*/ }).detach();*/
} }
void pre_destroy() override
{
static auto& ip_socket = *(SOCKET*)0x157E77818_g;
if (ip_socket)
{
closesocket(ip_socket);
ip_socket = 0;
}
}
}; };
} }

View File

@ -36,15 +36,17 @@ namespace game
WEAK symbol<const char*(const dvar_t* dvar)> Dvar_GetString{0x1422BFFF0_g}; WEAK symbol<const char*(const dvar_t* dvar)> Dvar_GetString{0x1422BFFF0_g};
WEAK symbol<void(const char *dvarName, const char *string, bool createIfMissing)> Dvar_SetFromStringByName{0x1422C7F60_g}; WEAK symbol<void(const char *dvarName, const char *string, bool createIfMissing)> Dvar_SetFromStringByName{0x1422C7F60_g};
// Variables
WEAK symbol<cmd_function_s> cmd_functions{0x15689FF58_g};
WEAK symbol<CmdArgs> sv_cmd_args{0x15689CE30_g};
// Rendering // Rendering
WEAK symbol<void(const char*, int, void*, float, float, float, float, float, float*, int)> R_AddCmdDrawText{ WEAK symbol<void(const char*, int, void*, float, float, float, float, float, float*, int)> R_AddCmdDrawText{
0x141CD98D0_g 0x141CD98D0_g
}; };
// Variables
WEAK symbol<cmd_function_s> cmd_functions{0x15689FF58_g};
WEAK symbol<CmdArgs> sv_cmd_args{0x15689CE30_g};
WEAK symbol<SOCKET> ip_socket{0x157E77818_g};
namespace s_wcd namespace s_wcd
{ {
WEAK symbol<HWND> codLogo{0x157E77A50_g}; WEAK symbol<HWND> codLogo{0x157E77A50_g};