2022-02-06 16:48:52 +01:00
|
|
|
//=============================================================================//
|
|
|
|
//
|
|
|
|
// Purpose: Net system utilities
|
|
|
|
//
|
|
|
|
//=============================================================================//
|
|
|
|
|
|
|
|
#include "core/stdafx.h"
|
|
|
|
#include "engine/net.h"
|
2022-04-02 02:48:54 +02:00
|
|
|
#ifndef NETCONSOLE
|
|
|
|
#include "core/logdef.h"
|
2022-04-09 16:16:40 +02:00
|
|
|
#include "tier1/cvar.h"
|
|
|
|
#include "vstdlib/completion.h"
|
2022-04-02 02:48:54 +02:00
|
|
|
#include "mathlib/color.h"
|
|
|
|
#include "engine/sys_utils.h"
|
|
|
|
#include "engine/net.h"
|
|
|
|
#include "engine/net_chan.h"
|
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
#include "engine/baseclient.h"
|
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
#endif // !NETCONSOLE
|
|
|
|
|
|
|
|
#ifndef NETCONSOLE
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: hook and log the receive datagram
|
2022-04-24 19:32:47 +02:00
|
|
|
// Input : iSocket -
|
|
|
|
// *pInpacket -
|
|
|
|
// bEncrypted -
|
|
|
|
// Output : true on success, false otherwise
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-24 19:32:47 +02:00
|
|
|
bool NET_ReceiveDatagram(int iSocket, netpacket_s* pInpacket, bool bEncrypted)
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
2022-04-27 18:22:08 +02:00
|
|
|
bool result = v_NET_ReceiveDatagram(iSocket, pInpacket, net_encryptionEnable->GetBool());
|
|
|
|
if (result && net_tracePayload->GetBool())
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
|
|
|
// Log received packet data.
|
2022-04-24 19:32:47 +02:00
|
|
|
HexDump("[+] NET_ReceiveDatagram", "netchan_packet_logger", &pInpacket->data[NULL], pInpacket->wiresize);
|
2022-04-02 02:48:54 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: hook and log the send datagram
|
2022-04-24 19:32:47 +02:00
|
|
|
// Input : s -
|
|
|
|
// *pPayload -
|
|
|
|
// iLenght -
|
|
|
|
// *pAdr -
|
2022-04-27 18:22:08 +02:00
|
|
|
// bEncrypt -
|
2022-04-24 19:32:47 +02:00
|
|
|
// Output : outgoing sequence number for this packet
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-27 18:22:08 +02:00
|
|
|
int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, v_netadr_t* pAdr, bool bEncrypt)
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
2022-04-27 18:22:08 +02:00
|
|
|
int result = v_NET_SendDatagram(s, pPayload, iLenght, pAdr, net_encryptionEnable->GetBool());
|
|
|
|
if (result && net_tracePayload->GetBool())
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
|
|
|
// Log transmitted packet data.
|
2022-04-24 19:32:47 +02:00
|
|
|
HexDump("[+] NET_SendDatagram", "netchan_packet_logger", pPayload, iLenght);
|
2022-04-02 02:48:54 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: sets the user specified encryption key
|
2022-04-10 19:59:34 +02:00
|
|
|
// Input : *svNetKey -
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-04-10 19:59:34 +02:00
|
|
|
void NET_SetKey(const string& svNetKey)
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
|
|
|
g_szNetKey.clear();
|
|
|
|
g_szNetKey = svNetKey;
|
|
|
|
|
|
|
|
DevMsg(eDLL_T::ENGINE, "______________________________________________________________\n");
|
|
|
|
DevMsg(eDLL_T::ENGINE, "] NET_KEY ----------------------------------------------------\n");
|
|
|
|
DevMsg(eDLL_T::ENGINE, "] BASE64: %s%s%s\n", g_svGreyB.c_str(), g_szNetKey.c_str(), g_svReset.c_str());
|
|
|
|
DevMsg(eDLL_T::ENGINE, "--------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
v_NET_SetKey(g_pNetKey, g_szNetKey.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: calculates and sets the encryption key
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NET_GenerateKey()
|
|
|
|
{
|
|
|
|
g_szNetKey.clear();
|
2022-04-27 18:22:08 +02:00
|
|
|
net_useRandomKey->SetValue(1);
|
2022-04-02 02:48:54 +02:00
|
|
|
|
|
|
|
BCRYPT_ALG_HANDLE hAlgorithm;
|
|
|
|
if (BCryptOpenAlgorithmProvider(&hAlgorithm, L"RNG", 0, 0) < 0)
|
|
|
|
{
|
|
|
|
Error(eDLL_T::ENGINE, "Failed to open rng algorithm\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned char pBuffer[0x10u];
|
|
|
|
if (BCryptGenRandom(hAlgorithm, pBuffer, 0x10u, 0) < 0)
|
|
|
|
{
|
|
|
|
Error(eDLL_T::ENGINE, "Failed to generate random data\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 0x10u; i++)
|
|
|
|
{
|
|
|
|
g_szNetKey += pBuffer[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
g_szNetKey = Base64Encode(g_szNetKey);
|
|
|
|
|
|
|
|
DevMsg(eDLL_T::ENGINE, "______________________________________________________________\n");
|
|
|
|
DevMsg(eDLL_T::ENGINE, "] NET_KEY ----------------------------------------------------\n");
|
|
|
|
DevMsg(eDLL_T::ENGINE, "] BASE64: %s%s%s\n", g_svGreyB.c_str(), g_szNetKey.c_str(), g_svReset.c_str());
|
|
|
|
DevMsg(eDLL_T::ENGINE, "--------------------------------------------------------------\n");
|
|
|
|
|
|
|
|
v_NET_SetKey(g_pNetKey, g_szNetKey.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: hook and log the client's signonstate to the console
|
2022-04-24 19:32:47 +02:00
|
|
|
// Input : *fmt -
|
|
|
|
// ... -
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void NET_PrintFunc(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
|
|
|
|
buf[sizeof(buf) - 1] = 0;
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
DevMsg(eDLL_T::CLIENT, "%s", buf);
|
|
|
|
}
|
|
|
|
|
2022-04-24 19:32:47 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: shutdown netchannel
|
|
|
|
// Input : *this -
|
|
|
|
// *szReason -
|
2022-05-12 01:20:19 +02:00
|
|
|
// bBadRep -
|
2022-04-24 19:32:47 +02:00
|
|
|
// bRemoveNow -
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-12 01:20:19 +02:00
|
|
|
void NET_Shutdown(void* thisptr, const char* szReason, uint8_t bBadRep, bool bRemoveNow)
|
2022-04-24 19:32:47 +02:00
|
|
|
{
|
|
|
|
_DownloadPlaylists_f_CompletionFunc(); // Re-load playlist from disk after getting disconnected from the server.
|
2022-05-12 01:20:19 +02:00
|
|
|
v_NET_Shutdown(thisptr, szReason, bBadRep, bRemoveNow);
|
2022-04-24 19:32:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: disconnect the client and shutdown netchannel
|
2022-04-24 19:32:47 +02:00
|
|
|
// Input : *pClient -
|
|
|
|
// nIndex -
|
|
|
|
// *szReason -
|
2022-05-12 01:20:19 +02:00
|
|
|
// bBadRep -
|
|
|
|
// bRemoveNow -
|
2022-04-02 02:48:54 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-12 01:20:19 +02:00
|
|
|
void NET_DisconnectClient(CBaseClient* pClient, int nIndex, const char* szReason, uint8_t bBadRep, bool bRemoveNow)
|
2022-04-02 02:48:54 +02:00
|
|
|
{
|
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
if (!pClient || std::strlen(szReason) == NULL || !pClient->GetNetChan())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:20:19 +02:00
|
|
|
v_NET_Shutdown(pClient->GetNetChan(), szReason, bBadRep, bRemoveNow); // Shutdown netchan.
|
2022-05-13 11:26:44 +02:00
|
|
|
pClient->SetNetChan(nullptr); // Null netchan.
|
|
|
|
CBaseClient_Clear(pClient); // Reset CClient instance for client.
|
|
|
|
g_bIsPersistenceVarSet[nIndex] = false; // Reset Persistence var.
|
2022-04-02 02:48:54 +02:00
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
}
|
|
|
|
#endif // !NETCONSOLE
|
2022-02-06 16:48:52 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: returns the WSA error code
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const char* NET_ErrorString(int iCode)
|
|
|
|
{
|
|
|
|
switch (iCode)
|
|
|
|
{
|
|
|
|
case WSAEINTR : return "WSAEINTR";
|
|
|
|
case WSAEBADF : return "WSAEBADF";
|
|
|
|
case WSAEACCES : return "WSAEACCES";
|
|
|
|
case WSAEDISCON : return "WSAEDISCON";
|
|
|
|
case WSAEFAULT : return "WSAEFAULT";
|
|
|
|
case WSAEINVAL : return "WSAEINVAL";
|
|
|
|
case WSAEMFILE : return "WSAEMFILE";
|
|
|
|
case WSAEWOULDBLOCK : return "WSAEWOULDBLOCK";
|
|
|
|
case WSAEINPROGRESS : return "WSAEINPROGRESS";
|
|
|
|
case WSAEALREADY : return "WSAEALREADY";
|
|
|
|
case WSAENOTSOCK : return "WSAENOTSOCK";
|
|
|
|
case WSAEDESTADDRREQ : return "WSAEDESTADDRREQ";
|
|
|
|
case WSAEMSGSIZE : return "WSAEMSGSIZE";
|
|
|
|
case WSAEPROTOTYPE : return "WSAEPROTOTYPE";
|
|
|
|
case WSAENOPROTOOPT : return "WSAENOPROTOOPT";
|
|
|
|
case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
|
|
|
|
case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
|
|
|
|
case WSAEOPNOTSUPP : return "WSAEOPNOTSUPP";
|
|
|
|
case WSAEPFNOSUPPORT : return "WSAEPFNOSUPPORT";
|
|
|
|
case WSAEAFNOSUPPORT : return "WSAEAFNOSUPPORT";
|
|
|
|
case WSAEADDRINUSE : return "WSAEADDRINUSE";
|
|
|
|
case WSAEADDRNOTAVAIL : return "WSAEADDRNOTAVAIL";
|
|
|
|
case WSAENETDOWN : return "WSAENETDOWN";
|
|
|
|
case WSAENETUNREACH : return "WSAENETUNREACH";
|
|
|
|
case WSAENETRESET : return "WSAENETRESET";
|
|
|
|
case WSAECONNABORTED : return "WSWSAECONNABORTEDAEINTR";
|
|
|
|
case WSAECONNRESET : return "WSAECONNRESET";
|
|
|
|
case WSAENOBUFS : return "WSAENOBUFS";
|
|
|
|
case WSAEISCONN : return "WSAEISCONN";
|
|
|
|
case WSAENOTCONN : return "WSAENOTCONN";
|
|
|
|
case WSAESHUTDOWN : return "WSAESHUTDOWN";
|
|
|
|
case WSAETOOMANYREFS : return "WSAETOOMANYREFS";
|
|
|
|
case WSAETIMEDOUT : return "WSAETIMEDOUT";
|
|
|
|
case WSAECONNREFUSED : return "WSAECONNREFUSED";
|
|
|
|
case WSAELOOP : return "WSAELOOP";
|
|
|
|
case WSAENAMETOOLONG : return "WSAENAMETOOLONG";
|
|
|
|
case WSAEHOSTDOWN : return "WSAEHOSTDOWN";
|
2022-02-14 02:33:13 +01:00
|
|
|
case WSAEPROCLIM : return "WSAEPROCLIM";
|
2022-02-06 16:48:52 +01:00
|
|
|
case WSASYSNOTREADY : return "WSASYSNOTREADY";
|
|
|
|
case WSAVERNOTSUPPORTED: return "WSAVERNOTSUPPORTED";
|
|
|
|
case WSANOTINITIALISED : return "WSANOTINITIALISED";
|
|
|
|
case WSAHOST_NOT_FOUND : return "WSAHOST_NOT_FOUND";
|
|
|
|
case WSATRY_AGAIN : return "WSATRY_AGAIN";
|
|
|
|
case WSANO_RECOVERY : return "WSANO_RECOVERY";
|
|
|
|
case WSANO_DATA : return "WSANO_DATA";
|
2022-04-02 02:48:54 +02:00
|
|
|
default : return "UNKNOWN_ERROR";
|
2022-02-06 16:48:52 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-02 02:48:54 +02:00
|
|
|
|
|
|
|
#ifndef NETCONSOLE
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void NET_Attach()
|
|
|
|
{
|
2022-04-27 18:22:08 +02:00
|
|
|
DetourAttach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram);
|
|
|
|
DetourAttach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram);
|
2022-04-02 02:48:54 +02:00
|
|
|
DetourAttach((LPVOID*)&v_NET_PrintFunc, &NET_PrintFunc);
|
|
|
|
#ifndef DEDICATED
|
2022-04-24 19:32:47 +02:00
|
|
|
DetourAttach((LPVOID*)&v_NET_Shutdown, &NET_Shutdown);
|
2022-04-02 02:48:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void NET_Detach()
|
|
|
|
{
|
2022-04-27 18:22:08 +02:00
|
|
|
DetourDetach((LPVOID*)&v_NET_ReceiveDatagram, &NET_ReceiveDatagram);
|
|
|
|
DetourDetach((LPVOID*)&v_NET_SendDatagram, &NET_SendDatagram);
|
2022-04-02 02:48:54 +02:00
|
|
|
DetourDetach((LPVOID*)&v_NET_PrintFunc, &NET_PrintFunc);
|
|
|
|
#ifndef DEDICATED
|
2022-04-24 19:32:47 +02:00
|
|
|
DetourDetach((LPVOID*)&v_NET_Shutdown, &NET_Shutdown);
|
2022-04-02 02:48:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2022-04-10 19:59:34 +02:00
|
|
|
string g_szNetKey = "WDNWLmJYQ2ZlM0VoTid3Yg==";
|
2022-04-18 03:35:08 +02:00
|
|
|
uintptr_t g_pNetKey = NULL;
|
2022-04-02 02:48:54 +02:00
|
|
|
#endif // !NETCONSOLE
|