iw4x-client/src/Components/Modules/Network.cpp

312 lines
7.7 KiB
C++
Raw Normal View History

#include "STDInclude.hpp"
2015-12-24 10:55:38 -05:00
namespace Components
{
std::string Network::SelectedPacket;
2016-02-12 16:23:41 -05:00
wink::signal<wink::slot<Network::CallbackRaw>> Network::StartupSignal;
std::map<std::string, wink::slot<Network::Callback>> Network::PacketHandlers;
2015-12-24 10:55:38 -05:00
Network::Address::Address(std::string addrString)
{
Game::NET_StringToAdr(addrString.data(), &this->address);
}
bool Network::Address::operator==(const Network::Address &obj)
{
return Game::NET_CompareAdr(this->address, obj.address);
}
void Network::Address::SetPort(unsigned short port)
{
2016-01-28 19:53:17 -05:00
this->address.port = htons(port);
}
2015-12-24 10:55:38 -05:00
unsigned short Network::Address::GetPort()
{
2016-01-28 19:53:17 -05:00
return ntohs(this->address.port);
2015-12-24 10:55:38 -05:00
}
2015-12-27 22:02:30 -05:00
void Network::Address::SetIP(DWORD ip)
{
2016-01-03 18:00:07 -05:00
this->address.ip.full = ip;
2015-12-27 22:02:30 -05:00
}
2016-01-03 18:00:07 -05:00
void Network::Address::SetIP(Game::netIP_t ip)
2015-12-27 22:02:30 -05:00
{
2016-01-03 18:00:07 -05:00
this->address.ip = ip;
}
Game::netIP_t Network::Address::GetIP()
{
return this->address.ip;
2015-12-27 22:02:30 -05:00
}
2016-01-03 13:28:47 -05:00
void Network::Address::SetType(Game::netadrtype_t type)
{
this->address.type = type;
}
Game::netadrtype_t Network::Address::GetType()
{
return this->address.type;
}
2015-12-24 10:55:38 -05:00
Game::netadr_t* Network::Address::Get()
{
return &this->address;
}
const char* Network::Address::GetString()
{
return Game::NET_AdrToString(this->address);
}
2016-01-03 18:00:07 -05:00
bool Network::Address::IsLocal()
{
// According to: https://en.wikipedia.org/wiki/Private_network
// 10.X.X.X
if (this->GetIP().bytes[0] == 10) return true;
// 192.168.X.X
if (this->GetIP().bytes[0] == 192 && this->GetIP().bytes[1] == 168) return true;
// 172.16.X.X - 172.31.X.X
if (this->GetIP().bytes[0] == 172 && (this->GetIP().bytes[1] >= 16) && (this->GetIP().bytes[1] < 32)) return true;
// TODO: Maybe check for matching localIPs and subnet mask
return false;
}
bool Network::Address::IsSelf()
{
if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback
if (this->GetPort() != (Dvar::Var("net_port").Get<int>() & 0xFFFF)) return false; // Port not equal
2016-01-24 13:58:13 -05:00
for (int i = 0; i < *Game::numIP; ++i)
2016-01-03 18:00:07 -05:00
{
if (this->GetIP().full == Game::localIP[i].full)
{
return true;
}
}
return false;
}
2016-02-15 14:28:01 -05:00
bool Network::Address::IsValid()
{
return (this->GetType() != Game::netadrtype_t::NA_BAD);
}
void Network::Address::Serialize(Proto::Network::Address* protoAddress)
{
protoAddress->set_ip(this->GetIP().full);
protoAddress->set_port(htons(this->GetPort()));
}
void Network::Address::Deserialize(const Proto::Network::Address& protoAddress)
{
this->SetIP(protoAddress.ip());
this->SetPort(ntohs(static_cast<uint16_t>(protoAddress.port())));
this->SetType(Game::netadrtype_t::NA_IP);
}
2015-12-24 10:55:38 -05:00
void Network::Handle(std::string packet, Network::Callback* callback)
2015-12-24 10:55:38 -05:00
{
Network::PacketHandlers[Utils::StrToLower(packet)] = callback;
}
2016-02-12 16:23:41 -05:00
void Network::OnStart(Network::CallbackRaw* callback)
{
Network::StartupSignal.connect(callback);
}
2016-02-10 11:18:45 -05:00
void Network::Send(Game::netsrc_t type, Network::Address target, std::string data)
2015-12-24 10:55:38 -05:00
{
2016-02-10 11:18:45 -05:00
// NET_OutOfBandPrint only supports non-binary data!
//Game::NET_OutOfBandPrint(type, *target.Get(), data.data());
std::string rawData;
rawData.append("\xFF\xFF\xFF\xFF", 4);
rawData.append(data);
//rawData.append("\0", 1);
Network::SendRaw(type, target, rawData);
2015-12-24 10:55:38 -05:00
}
2016-02-10 11:18:45 -05:00
void Network::Send(Network::Address target, std::string data)
2015-12-27 22:02:30 -05:00
{
Network::Send(Game::netsrc_t::NS_CLIENT, target, data);
}
2016-02-10 11:18:45 -05:00
void Network::SendRaw(Game::netsrc_t type, Network::Address target, std::string data)
{
2016-01-24 10:57:16 -05:00
// NET_OutOfBandData doesn't seem to work properly
//Game::NET_OutOfBandData(type, *target.Get(), data.data(), data.size());
2016-02-10 11:18:45 -05:00
Game::Sys_SendPacket(type, data.size(), data.data(), *target.Get());
}
2016-02-10 11:18:45 -05:00
void Network::SendRaw(Network::Address target, std::string data)
{
Network::SendRaw(Game::netsrc_t::NS_CLIENT, target, data);
}
2016-02-10 11:18:45 -05:00
void Network::SendCommand(Game::netsrc_t type, Network::Address target, std::string command, std::string data)
{
2016-02-10 14:11:34 -05:00
// Use space a separator (possible separators are '\n', ' ').
// Though, our handler only needs exactly 1 char as separator and doesn't which char it is
2016-02-10 11:18:45 -05:00
std::string packet;
packet.append(command);
packet.append(" ", 1);
packet.append(data);
Network::Send(type, target, packet);
}
void Network::SendCommand(Network::Address target, std::string command, std::string data)
{
Network::SendCommand(Game::netsrc_t::NS_CLIENT, target, command, data);
}
2016-01-03 13:28:47 -05:00
void Network::Broadcast(unsigned short port, std::string data)
{
Address target;
target.SetPort(port);
target.SetIP(INADDR_BROADCAST);
target.SetType(Game::netadrtype_t::NA_BROADCAST);
2016-02-10 11:18:45 -05:00
Network::Send(Game::netsrc_t::NS_CLIENT, target, data);
2016-01-03 13:28:47 -05:00
}
void Network::BroadcastRange(unsigned int min, unsigned int max, std::string data)
{
2016-01-24 13:58:13 -05:00
for (unsigned int i = min; i < max; ++i)
2016-01-03 13:28:47 -05:00
{
Network::Broadcast(static_cast<unsigned short>(i & 0xFFFF), data);
2016-01-03 13:28:47 -05:00
}
}
void Network::BroadcastAll(std::string data)
{
Network::BroadcastRange(100, 65536, data);
}
2015-12-24 10:55:38 -05:00
int Network::PacketInterceptionHandler(const char* packet)
{
2016-02-19 17:57:06 -05:00
// Packet rate limit.
static uint32_t packets = 0;
static int lastClean = 0;
if ((Game::Com_Milliseconds() - lastClean) > 1'000)
{
packets = 0;
lastClean = Game::Com_Milliseconds();
}
if ((++packets) > NETWORK_MAX_PACKETS_PER_SECOND)
{
return 1;
}
2016-01-04 04:54:31 -05:00
std::string packetCommand = packet;
auto pos = packetCommand.find_first_of("\\\n ");
if (pos != std::string::npos)
{
packetCommand = packetCommand.substr(0, pos);
}
packetCommand = Utils::StrToLower(packetCommand);
2015-12-24 10:55:38 -05:00
// Check if custom handler exists
2016-01-24 13:58:13 -05:00
for (auto i = Network::PacketHandlers.begin(); i != Network::PacketHandlers.end(); ++i)
2015-12-24 10:55:38 -05:00
{
2016-01-04 04:54:31 -05:00
if (Utils::StrToLower(i->first) == packetCommand)
2015-12-24 10:55:38 -05:00
{
Network::SelectedPacket = i->first;
return 0;
}
}
// No interception
return 1;
}
2016-02-10 14:11:34 -05:00
void Network::DeployPacket(Game::netadr_t* from, Game::msg_t* msg)
2015-12-24 10:55:38 -05:00
{
if (Network::PacketHandlers.find(Network::SelectedPacket) != Network::PacketHandlers.end())
{
2016-01-28 21:18:58 -05:00
std::string data;
2015-12-25 15:42:35 -05:00
size_t offset = Network::SelectedPacket.size() + 4 + 1;
2016-01-28 21:18:58 -05:00
if (static_cast<size_t>(msg->cursize) > offset)
{
data.append(msg->data + offset, msg->cursize - offset);
}
// Remove trailing 0x00 byte
2016-02-10 11:18:45 -05:00
// Actually, don't remove it, it might be part of the packet. Send correctly formatted packets instead!
//if (data.size() && !data[data.size() - 1]) data.pop_back();
Network::PacketHandlers[Network::SelectedPacket](from, data);
2015-12-24 10:55:38 -05:00
}
else
{
Logger::Print("Error: Network packet intercepted, but handler is missing!\n");
}
}
2016-02-12 16:23:41 -05:00
void Network::NetworkStart()
{
Network::StartupSignal();
}
void __declspec(naked) Network::NetworkStartStub()
{
__asm
{
mov eax, 64D900h
call eax
jmp Network::NetworkStart
}
}
2015-12-24 10:55:38 -05:00
void __declspec(naked) Network::DeployPacketStub()
{
__asm
{
2016-02-10 14:11:34 -05:00
lea eax, [esp + 0C54h]
push ebp // Command
push eax // Address pointer
2015-12-24 10:55:38 -05:00
call Network::DeployPacket
2016-02-10 14:11:34 -05:00
add esp, 8h
2015-12-24 10:55:38 -05:00
mov al, 1
2016-02-10 14:11:34 -05:00
pop edi
pop esi
pop ebp
pop ebx
2015-12-24 10:55:38 -05:00
add esp, 0C40h
retn
}
}
Network::Network()
{
2016-01-24 10:57:16 -05:00
Assert_Size(Game::netadr_t, 20);
2015-12-24 10:55:38 -05:00
// maximum size in NET_OutOfBandPrint
Utils::Hook::Set<DWORD>(0x4AEF08, 0x1FFFC);
Utils::Hook::Set<DWORD>(0x4AEFA3, 0x1FFFC);
// increase max port binding attempts from 10 to 100
Utils::Hook::Set<BYTE>(0x4FD48A, 100);
2016-02-12 16:23:41 -05:00
2016-01-28 11:30:32 -05:00
// Parse port as short in Net_AddrToString
Utils::Hook::Set<char*>(0x4698E3, "%u.%u.%u.%u:%hu");
2016-02-12 16:23:41 -05:00
// Install startup handler
Utils::Hook(0x4FD4D4, Network::NetworkStartStub, HOOK_JUMP).Install()->Quick();
2015-12-24 10:55:38 -05:00
// Install interception handler
Utils::Hook(0x5AA709, Network::PacketInterceptionHandler, HOOK_CALL).Install()->Quick();
// Install packet deploy hook
2016-02-11 18:36:52 -05:00
Utils::Hook::RedirectJump(0x5AA713, Network::DeployPacketStub);
2015-12-24 10:55:38 -05:00
}
Network::~Network()
{
Network::SelectedPacket.clear();
Network::PacketHandlers.clear();
2016-02-12 16:23:41 -05:00
Network::StartupSignal.clear();
2015-12-24 10:55:38 -05:00
}
}