2022-03-26 00:24:13 +01:00
|
|
|
//===============================================================================//
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//
|
|
|
|
//===============================================================================//
|
2022-05-20 11:52:19 +02:00
|
|
|
// client.cpp: implementation of the CClient class.
|
2022-06-16 18:04:43 +02:00
|
|
|
//
|
2022-03-26 00:24:13 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2021-12-25 22:36:38 +01:00
|
|
|
#include "core/stdafx.h"
|
2022-09-20 22:48:55 +02:00
|
|
|
#include "tier1/cvar.h"
|
2023-07-04 23:01:13 +02:00
|
|
|
#include "tier1/strtools.h"
|
2022-09-19 01:28:43 +02:00
|
|
|
#include "engine/server/server.h"
|
2022-05-20 11:52:19 +02:00
|
|
|
#include "engine/client/client.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2023-09-23 14:04:49 +02:00
|
|
|
// Absolute max string cmd length, any character past this will be NULLED.
|
|
|
|
#define STRINGCMD_MAX_LEN 512
|
2023-08-07 16:52:35 +02:00
|
|
|
|
2022-05-21 18:56:56 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: throw away any residual garbage in the channel
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
void CClient::Clear(void)
|
|
|
|
{
|
2022-09-22 00:08:49 +02:00
|
|
|
#ifndef CLIENT_DLL
|
2022-09-19 01:28:43 +02:00
|
|
|
g_ServerPlayer[GetUserID()].Reset(); // Reset ServerPlayer slot.
|
2022-09-22 00:08:49 +02:00
|
|
|
#endif // !CLIENT_DLL
|
2022-05-21 18:56:56 +02:00
|
|
|
v_CClient_Clear(this);
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: throw away any residual garbage in the channel
|
2022-09-19 01:28:43 +02:00
|
|
|
// Input : *pClient -
|
2022-03-26 00:24:13 +01:00
|
|
|
//---------------------------------------------------------------------------------
|
2022-09-19 01:28:43 +02:00
|
|
|
void CClient::VClear(CClient* pClient)
|
2022-05-21 18:56:56 +02:00
|
|
|
{
|
2023-07-17 17:38:21 +02:00
|
|
|
pClient->Clear();
|
2022-05-21 18:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: connect new client
|
|
|
|
// Input : *szName -
|
|
|
|
// *pNetChannel -
|
|
|
|
// bFakePlayer -
|
|
|
|
// *a5 -
|
|
|
|
// *szMessage -
|
|
|
|
// nMessageSize -
|
2022-09-09 19:47:31 +02:00
|
|
|
// Output : true if connection was successful, false otherwise
|
2022-05-21 18:56:56 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool CClient::Connect(const char* szName, void* pNetChannel, bool bFakePlayer, void* a5, char* szMessage, int nMessageSize)
|
2022-03-26 00:24:13 +01:00
|
|
|
{
|
2023-09-16 16:18:18 +02:00
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
g_ServerPlayer[GetUserID()].Reset(); // Reset ServerPlayer slot.
|
|
|
|
#endif // !CLIENT_DLL
|
2022-05-21 18:56:56 +02:00
|
|
|
return v_CClient_Connect(this, szName, pNetChannel, bFakePlayer, a5, szMessage, nMessageSize);
|
2022-03-26 00:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: connect new client
|
|
|
|
// Input : *pClient -
|
|
|
|
// *szName -
|
|
|
|
// *pNetChannel -
|
|
|
|
// bFakePlayer -
|
|
|
|
// *a5 -
|
|
|
|
// *szMessage -
|
|
|
|
// nMessageSize -
|
2022-09-09 19:47:31 +02:00
|
|
|
// Output : true if connection was successful, false otherwise
|
2021-12-25 22:36:38 +01:00
|
|
|
//---------------------------------------------------------------------------------
|
2022-05-21 18:56:56 +02:00
|
|
|
bool CClient::VConnect(CClient* pClient, const char* szName, void* pNetChannel, bool bFakePlayer, void* a5, char* szMessage, int nMessageSize)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2023-09-16 16:18:18 +02:00
|
|
|
return pClient->Connect(szName, pNetChannel, bFakePlayer, a5, szMessage, nMessageSize);;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 23:19:50 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: disconnect client
|
2022-09-19 01:28:43 +02:00
|
|
|
// Input : nRepLvl -
|
2022-09-18 23:19:50 +02:00
|
|
|
// *szReason -
|
|
|
|
// ... -
|
|
|
|
//---------------------------------------------------------------------------------
|
2022-09-19 01:28:43 +02:00
|
|
|
void CClient::Disconnect(const Reputation_t nRepLvl, const char* szReason, ...)
|
2022-09-18 23:19:50 +02:00
|
|
|
{
|
2022-09-19 01:28:43 +02:00
|
|
|
if (m_nSignonState != SIGNONSTATE::SIGNONSTATE_NONE)
|
|
|
|
{
|
|
|
|
char szBuf[1024];
|
|
|
|
{/////////////////////////////
|
2023-06-26 22:34:24 +02:00
|
|
|
va_list vArgs;
|
2022-09-19 01:28:43 +02:00
|
|
|
va_start(vArgs, szReason);
|
|
|
|
|
|
|
|
vsnprintf(szBuf, sizeof(szBuf), szReason, vArgs);
|
|
|
|
|
|
|
|
szBuf[sizeof(szBuf) - 1] = '\0';
|
|
|
|
va_end(vArgs);
|
|
|
|
}/////////////////////////////
|
|
|
|
v_CClient_Disconnect(this, nRepLvl, szBuf);
|
|
|
|
}
|
2022-09-18 23:19:50 +02:00
|
|
|
}
|
|
|
|
|
2023-05-15 17:19:18 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: activate player
|
|
|
|
// Input : *pClient -
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
void CClient::VActivatePlayer(CClient* pClient)
|
|
|
|
{
|
2023-07-16 13:47:07 +02:00
|
|
|
// Set the client instance to 'ready' before calling ActivatePlayer.
|
|
|
|
pClient->SetPersistenceState(PERSISTENCE::PERSISTENCE_READY);
|
|
|
|
v_CClient_ActivatePlayer(pClient);
|
2023-05-15 17:19:18 +02:00
|
|
|
|
2023-07-19 02:30:07 +02:00
|
|
|
#ifndef CLIENT_DLL
|
2023-07-17 17:08:37 +02:00
|
|
|
const CNetChan* pNetChan = pClient->GetNetChan();
|
2023-05-15 17:19:18 +02:00
|
|
|
|
2023-07-17 17:08:37 +02:00
|
|
|
if (pNetChan && sv_showconnecting->GetBool())
|
|
|
|
{
|
2023-08-21 19:12:29 +02:00
|
|
|
Msg(eDLL_T::SERVER, "Activated player #%d; channel %s(%s) ('%llu')\n",
|
2023-07-16 13:47:07 +02:00
|
|
|
pClient->GetUserID(), pNetChan->GetName(), pNetChan->GetAddress(), pClient->GetNucleusID());
|
|
|
|
}
|
2023-07-19 02:30:07 +02:00
|
|
|
#endif // !CLIENT_DLL
|
2023-05-15 17:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
2023-05-30 23:01:46 +02:00
|
|
|
// Purpose: send a net message with replay.
|
|
|
|
// set 'CNetMessage::m_nGroup' to 'NoReplay' to disable replay.
|
2023-05-15 17:19:18 +02:00
|
|
|
// Input : *pMsg -
|
|
|
|
// bLocal -
|
|
|
|
// bForceReliable -
|
|
|
|
// bVoice -
|
|
|
|
//---------------------------------------------------------------------------------
|
2023-05-30 23:01:46 +02:00
|
|
|
bool CClient::SendNetMsgEx(CNetMessage* pMsg, char bLocal, bool bForceReliable, bool bVoice)
|
2023-02-17 23:59:37 +01:00
|
|
|
{
|
2023-05-30 23:01:46 +02:00
|
|
|
if (!ShouldReplayMessage(pMsg))
|
|
|
|
{
|
|
|
|
// Don't copy the message into the replay buffer.
|
|
|
|
pMsg->m_nGroup = NetMessageGroup::NoReplay;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v_CClient_SendNetMsgEx(this, pMsg, bLocal, bForceReliable, bVoice);
|
2023-02-17 23:59:37 +01:00
|
|
|
}
|
|
|
|
|
2023-05-15 17:19:18 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: send a snapshot
|
|
|
|
// Input : *pClient -
|
|
|
|
// *pFrame -
|
|
|
|
// nTick -
|
|
|
|
// nTickAck -
|
|
|
|
//---------------------------------------------------------------------------------
|
2023-02-17 23:59:37 +01:00
|
|
|
void* CClient::VSendSnapshot(CClient* pClient, CClientFrame* pFrame, int nTick, int nTickAck)
|
|
|
|
{
|
|
|
|
return v_CClient_SendSnapshot(pClient, pFrame, nTick, nTickAck);
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:18:18 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: internal hook to 'CClient::SendNetMsgEx'
|
|
|
|
// Input : *pClient -
|
|
|
|
// *pMsg -
|
|
|
|
// bLocal -
|
|
|
|
// bForceReliable -
|
|
|
|
// bVoice -
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool CClient::VSendNetMsgEx(CClient* pClient, CNetMessage* pMsg, char bLocal, bool bForceReliable, bool bVoice)
|
|
|
|
{
|
|
|
|
return pClient->SendNetMsgEx(pMsg, bLocal, bForceReliable, bVoice);
|
|
|
|
}
|
|
|
|
|
2022-09-20 22:48:55 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
2023-09-16 12:18:32 +02:00
|
|
|
// Purpose: some versions of the binary have an optimization that shifts the 'this'
|
|
|
|
// pointer of the CClient structure by 8 bytes to avoid having to cache the vftable
|
|
|
|
// pointer if it never get used. Here we shift it back so it aligns again.
|
2022-09-20 22:48:55 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
2023-09-16 12:18:32 +02:00
|
|
|
CClient* AdjustShiftedThisPointer(CClient* shiftedPointer)
|
2022-09-20 22:48:55 +02:00
|
|
|
{
|
|
|
|
#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
|
2023-09-16 12:18:32 +02:00
|
|
|
return shiftedPointer;
|
2022-09-20 22:48:55 +02:00
|
|
|
#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
|
|
|
|
/* Original function called method "CClient::ExecuteStringCommand" with an optimization
|
|
|
|
* that shifted the 'this' pointer with 8 bytes.
|
|
|
|
* Since this has been inlined with "CClient::ProcessStringCmd" as of S2, the shifting
|
|
|
|
* happens directly to anything calling this function. */
|
2023-09-16 12:18:32 +02:00
|
|
|
char* pShifted = reinterpret_cast<char*>(shiftedPointer) - 8;
|
|
|
|
return reinterpret_cast<CClient*>(pShifted);
|
2022-09-20 22:48:55 +02:00
|
|
|
#endif // !GAMEDLL_S0 || !GAMEDLL_S1
|
2023-09-16 12:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
// Purpose: process string commands (kicking anyone attempting to DOS)
|
|
|
|
// Input : *pClient - (ADJ)
|
|
|
|
// *pMsg -
|
|
|
|
// Output : false if cmd should be passed to CServerGameClients
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
bool CClient::VProcessStringCmd(CClient* pClient, NET_StringCmd* pMsg)
|
|
|
|
{
|
|
|
|
#ifndef CLIENT_DLL
|
2023-09-23 14:04:49 +02:00
|
|
|
CClient* const pClient_Adj = AdjustShiftedThisPointer(pClient);
|
2023-08-08 19:25:12 +02:00
|
|
|
|
|
|
|
// Jettison the cmd if the client isn't active.
|
|
|
|
if (!pClient_Adj->IsActive())
|
|
|
|
return true;
|
|
|
|
|
2023-09-23 14:04:49 +02:00
|
|
|
const int nUserID = pClient_Adj->GetUserID();
|
|
|
|
ServerPlayer_t* const pSlot = &g_ServerPlayer[nUserID];
|
2023-04-30 01:26:37 +02:00
|
|
|
|
2023-09-23 14:04:49 +02:00
|
|
|
const double flStartTime = Plat_FloatTime();
|
|
|
|
const int nCmdQuotaLimit = sv_quota_stringCmdsPerSecond->GetInt();
|
2022-09-20 22:48:55 +02:00
|
|
|
|
2022-09-22 21:20:37 +02:00
|
|
|
if (!nCmdQuotaLimit)
|
2022-09-20 22:48:55 +02:00
|
|
|
return true;
|
|
|
|
|
2023-06-22 23:45:19 +02:00
|
|
|
const char* pCmd = pMsg->cmd;
|
|
|
|
// Just skip if the cmd pointer is null, we still check if the
|
|
|
|
// client sent too many commands and take appropriate actions.
|
|
|
|
// The internal function discards the command if it's null.
|
2023-08-07 16:52:35 +02:00
|
|
|
if (pCmd)
|
2023-06-22 23:45:19 +02:00
|
|
|
{
|
2023-09-23 14:04:49 +02:00
|
|
|
// There is an issue in CUtlBuffer::ParseToken() that causes it to read
|
|
|
|
// past its buffer; mostly seems to happen on 32bit, but a carefully
|
|
|
|
// crafted string should work on 64bit too). The fix is to just null
|
|
|
|
// everything past the maximum allowed length. The second 'theoretical'
|
|
|
|
// fix would be to properly fix CUtlBuffer::ParseToken() by computing
|
|
|
|
// the UTF8 character size each iteration and check if it still doesn't
|
|
|
|
// exceed bounds.
|
2023-08-07 16:52:35 +02:00
|
|
|
memset(&pMsg->buffer[STRINGCMD_MAX_LEN],
|
|
|
|
'\0', sizeof(pMsg->buffer) - (STRINGCMD_MAX_LEN));
|
|
|
|
|
|
|
|
if (!V_IsValidUTF8(pCmd))
|
|
|
|
{
|
|
|
|
Warning(eDLL_T::SERVER, "Removing client '%s' from slot #%i ('%llu' sent invalid string command!)\n",
|
|
|
|
pClient_Adj->GetNetChan()->GetAddress(), pClient_Adj->GetUserID(), pClient_Adj->GetNucleusID());
|
|
|
|
|
|
|
|
pClient_Adj->Disconnect(Reputation_t::REP_MARK_BAD, "#DISCONNECT_INVALID_STRINGCMD");
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-22 23:45:19 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 22:48:55 +02:00
|
|
|
if (flStartTime - pSlot->m_flStringCommandQuotaTimeStart >= 1.0)
|
|
|
|
{
|
|
|
|
pSlot->m_flStringCommandQuotaTimeStart = flStartTime;
|
|
|
|
pSlot->m_nStringCommandQuotaCount = 0;
|
|
|
|
}
|
|
|
|
++pSlot->m_nStringCommandQuotaCount;
|
|
|
|
|
2022-09-22 21:20:37 +02:00
|
|
|
if (pSlot->m_nStringCommandQuotaCount > nCmdQuotaLimit)
|
2022-09-20 22:48:55 +02:00
|
|
|
{
|
2023-06-22 23:45:19 +02:00
|
|
|
Warning(eDLL_T::SERVER, "Removing client '%s' from slot #%i ('%llu' exceeded string command quota!)\n",
|
|
|
|
pClient_Adj->GetNetChan()->GetAddress(), pClient_Adj->GetUserID(), pClient_Adj->GetNucleusID());
|
2022-09-20 22:48:55 +02:00
|
|
|
|
|
|
|
pClient_Adj->Disconnect(Reputation_t::REP_MARK_BAD, "#DISCONNECT_STRINGCMD_OVERFLOW");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
|
|
|
|
return v_CClient_ProcessStringCmd(pClient, pMsg);
|
|
|
|
}
|
|
|
|
|
2023-05-30 23:01:46 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
2023-09-16 16:18:18 +02:00
|
|
|
// Purpose: process set convar
|
|
|
|
// Input : *pClient - (ADJ)
|
2023-05-30 23:01:46 +02:00
|
|
|
// *pMsg -
|
2023-09-16 16:18:18 +02:00
|
|
|
// Output :
|
2023-05-30 23:01:46 +02:00
|
|
|
//---------------------------------------------------------------------------------
|
2023-09-16 16:18:18 +02:00
|
|
|
bool CClient::VProcessSetConVar(CClient* pClient, NET_SetConVar* pMsg)
|
2023-05-30 23:01:46 +02:00
|
|
|
{
|
2023-09-16 22:02:04 +02:00
|
|
|
#ifndef CLIENT_DLL
|
2023-09-16 16:18:18 +02:00
|
|
|
CClient* pAdj = AdjustShiftedThisPointer(pClient);
|
|
|
|
ServerPlayer_t* pSlot = &g_ServerPlayer[pAdj->GetUserID()];
|
|
|
|
|
|
|
|
// This loop never exceeds 255 iterations, NET_SetConVar::ReadFromBuffer(...)
|
|
|
|
// reads and inserts up to 255 entries in the vector (reads a byte for size).
|
|
|
|
FOR_EACH_VEC(pMsg->m_ConVars, i)
|
|
|
|
{
|
|
|
|
const NET_SetConVar::cvar_t& entry = pMsg->m_ConVars[i];
|
|
|
|
const char* const name = entry.name;
|
|
|
|
const char* const value = entry.value;
|
|
|
|
|
|
|
|
// Discard any ConVar change request if it contains funky characters.
|
|
|
|
bool bFunky = false;
|
|
|
|
for (const char* s = name; *s != '\0'; ++s)
|
|
|
|
{
|
|
|
|
if (!isalnum(*s) && *s != '_')
|
|
|
|
{
|
|
|
|
bFunky = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bFunky)
|
|
|
|
{
|
|
|
|
DevWarning(eDLL_T::SERVER, "Ignoring ConVar change request for variable '%s' from client '%s'; invalid characters in the variable name\n",
|
|
|
|
name, pAdj->GetClientName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The initial set of ConVars must contain all client ConVars that are
|
|
|
|
// flagged UserInfo. This is a simple fix to exploits that send bogus
|
|
|
|
// data later, and catches bugs, such as new UserInfo ConVars appearing
|
|
|
|
// later, which shouldn't happen.
|
|
|
|
if (pSlot->m_bInitialConVarsSet && !pAdj->m_ConVars->FindKey(name))
|
|
|
|
{
|
|
|
|
DevWarning(eDLL_T::SERVER, "UserInfo update from \"%s\" ignored: %s = %s\n",
|
|
|
|
pAdj->GetClientName(), name, value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pAdj->m_ConVars->SetString(name, value);
|
|
|
|
DevMsg(eDLL_T::SERVER, "UserInfo update from \"%s\": %s = %s\n", pAdj->GetClientName(), name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pSlot->m_bInitialConVarsSet = true;
|
|
|
|
pAdj->m_bConVarsChanged = true;
|
2023-09-16 22:02:04 +02:00
|
|
|
#endif // !CLIENT_DLL
|
2023-09-16 16:18:18 +02:00
|
|
|
|
|
|
|
return true;
|
2023-05-30 23:01:46 +02:00
|
|
|
}
|
2023-07-19 02:30:07 +02:00
|
|
|
|
|
|
|
void VClient::Attach(void) const
|
|
|
|
{
|
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_Clear, &CClient::VClear);
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_Connect, &CClient::VConnect);
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_ActivatePlayer, &CClient::VActivatePlayer);
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_SendNetMsgEx, &CClient::VSendNetMsgEx);
|
|
|
|
//DetourAttach((LPVOID*)&p_CClient_SendSnapshot, &CClient::VSendSnapshot);
|
2023-09-16 16:18:18 +02:00
|
|
|
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_ProcessStringCmd, &CClient::VProcessStringCmd);
|
|
|
|
DetourAttach((LPVOID*)&v_CClient_ProcessSetConVar, &CClient::VProcessSetConVar);
|
2023-07-19 02:30:07 +02:00
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
}
|
|
|
|
void VClient::Detach(void) const
|
|
|
|
{
|
|
|
|
#ifndef CLIENT_DLL
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_Clear, &CClient::VClear);
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_Connect, &CClient::VConnect);
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_ActivatePlayer, &CClient::VActivatePlayer);
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_SendNetMsgEx, &CClient::VSendNetMsgEx);
|
|
|
|
//DetourDetach((LPVOID*)&p_CClient_SendSnapshot, &CClient::VSendSnapshot);
|
2023-09-16 16:18:18 +02:00
|
|
|
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_ProcessStringCmd, &CClient::VProcessStringCmd);
|
|
|
|
DetourDetach((LPVOID*)&v_CClient_ProcessSetConVar, &CClient::VProcessSetConVar);
|
2023-07-19 02:30:07 +02:00
|
|
|
#endif // !CLIENT_DLL
|
|
|
|
}
|