3942: Allow the antibot to get more insight into the network r=Learath2 a=heinrich5991

E.g. allow it to see the messages the server sends.

<!-- What is the motivation for the changes of this pull request -->

## Checklist

- [x] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [x] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: heinrich5991 <heinrich5991@gmail.com>
This commit is contained in:
bors[bot] 2021-07-01 14:55:12 +00:00 committed by GitHub
commit f23475665f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 15 deletions

View file

@ -6,7 +6,7 @@
enum
{
ANTIBOT_ABI_VERSION = 3,
ANTIBOT_ABI_VERSION = 4,
ANTIBOT_MSGFLAG_NONVITAL = 1,
ANTIBOT_MSGFLAG_FLUSH = 2,

View file

@ -29,7 +29,11 @@ ANTIBOTAPI void AntibotOnHookAttach(int ClientID, bool Player);
ANTIBOTAPI void AntibotOnEngineTick(void);
ANTIBOTAPI void AntibotOnEngineClientJoin(int ClientID, bool Sixup);
ANTIBOTAPI void AntibotOnEngineClientDrop(int ClientID, const char *pReason);
ANTIBOTAPI void AntibotOnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags);
// Returns true if the message shouldn't be processed by the server.
ANTIBOTAPI bool AntibotOnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags);
ANTIBOTAPI bool AntibotOnEngineServerMessage(int ClientID, const void *pData, int Size, int Flags);
// Returns true if the server should simulate receiving a client message.
ANTIBOTAPI bool AntibotOnEngineSimulateClientMessage(int *pClientID, void *pBuffer, int BufferSize, int *pOutSize, int *pFlags);
}
#endif // ANTIBOT_ANTIBOT_INTERFACE_H

View file

@ -35,5 +35,7 @@ void AntibotOnHookAttach(int /*ClientID*/, bool /*Player*/) {}
void AntibotOnEngineTick(void) {}
void AntibotOnEngineClientJoin(int /*ClientID*/, bool /*Sixup*/) {}
void AntibotOnEngineClientDrop(int /*ClientID*/, const char * /*pReason*/) {}
void AntibotOnEngineClientMessage(int /*ClientID*/, const void * /*pData*/, int /*Size*/, int /*Flags*/) {}
bool AntibotOnEngineClientMessage(int /*ClientID*/, const void * /*pData*/, int /*Size*/, int /*Flags*/) { return false; }
bool AntibotOnEngineServerMessage(int /*ClientID*/, const void * /*pData*/, int /*Size*/, int /*Flags*/) { return false; }
bool AntibotOnEngineSimulateClientMessage(int * /*pClientID*/, void * /*pBuffer*/, int /*BufferSize*/, int * /*pOutSize*/, int * /*pFlags*/) { return false; }
}

View file

@ -37,7 +37,9 @@ public:
virtual void OnEngineTick() = 0;
virtual void OnEngineClientJoin(int ClientID, bool Sixup) = 0;
virtual void OnEngineClientDrop(int ClientID, const char *pReason) = 0;
virtual void OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags) = 0;
virtual bool OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags) = 0;
virtual bool OnEngineServerMessage(int ClientID, const void *pData, int Size, int Flags) = 0;
virtual bool OnEngineSimulateClientMessage(int *pClientID, void *pBuffer, int BufferSize, int *pOutSize, int *pFlags) = 0;
virtual ~IEngineAntibot(){};
};

View file

@ -156,7 +156,7 @@ void CAntibot::OnEngineClientDrop(int ClientID, const char *pReason)
Update();
AntibotOnEngineClientDrop(ClientID, pReason);
}
void CAntibot::OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags)
bool CAntibot::OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags)
{
Update();
int AntibotFlags = 0;
@ -164,7 +164,31 @@ void CAntibot::OnEngineClientMessage(int ClientID, const void *pData, int Size,
{
AntibotFlags |= ANTIBOT_MSGFLAG_NONVITAL;
}
AntibotOnEngineClientMessage(ClientID, pData, Size, Flags);
return AntibotOnEngineClientMessage(ClientID, pData, Size, Flags);
}
bool CAntibot::OnEngineServerMessage(int ClientID, const void *pData, int Size, int Flags)
{
Update();
int AntibotFlags = 0;
if((Flags & MSGFLAG_VITAL) == 0)
{
AntibotFlags |= ANTIBOT_MSGFLAG_NONVITAL;
}
return AntibotOnEngineServerMessage(ClientID, pData, Size, Flags);
}
bool CAntibot::OnEngineSimulateClientMessage(int *pClientID, void *pBuffer, int BufferSize, int *pOutSize, int *pFlags)
{
int AntibotFlags = 0;
bool Result = AntibotOnEngineSimulateClientMessage(pClientID, pBuffer, BufferSize, pOutSize, &AntibotFlags);
if(Result)
{
*pFlags = 0;
if((AntibotFlags & ANTIBOT_MSGFLAG_NONVITAL) == 0)
{
*pFlags |= MSGFLAG_VITAL;
}
}
return Result;
}
#else
CAntibot::CAntibot() :
@ -209,7 +233,9 @@ void CAntibot::OnHookAttach(int ClientID, bool Player) {}
void CAntibot::OnEngineTick() {}
void CAntibot::OnEngineClientJoin(int ClientID, bool Sixup) {}
void CAntibot::OnEngineClientDrop(int ClientID, const char *pReason) {}
void CAntibot::OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags) {}
bool CAntibot::OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags) { return false; }
bool CAntibot::OnEngineServerMessage(int ClientID, const void *pData, int Size, int Flags) { return false; }
bool CAntibot::OnEngineSimulateClientMessage(int *pClientID, void *pBuffer, int BufferSize, int *pOutSize, int *pFlags) { return false; }
#endif
IEngineAntibot *CreateEngineAntibot()

View file

@ -33,7 +33,9 @@ public:
virtual void OnEngineTick();
virtual void OnEngineClientJoin(int ClientID, bool Sixup);
virtual void OnEngineClientDrop(int ClientID, const char *pReason);
virtual void OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags);
virtual bool OnEngineClientMessage(int ClientID, const void *pData, int Size, int Flags);
virtual bool OnEngineServerMessage(int ClientID, const void *pData, int Size, int Flags);
virtual bool OnEngineSimulateClientMessage(int *pClientID, void *pBuffer, int BufferSize, int *pOutSize, int *pFlags);
// Game
virtual void RoundStart(class IGameServer *pGameServer);

View file

@ -778,6 +778,10 @@ int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientID)
Packet.m_pData = pPack->Data();
Packet.m_DataSize = pPack->Size();
Packet.m_ClientID = i;
if(Antibot()->OnEngineServerMessage(i, Packet.m_pData, Packet.m_DataSize, Flags))
{
continue;
}
m_NetServer.Send(&Packet);
}
}
@ -793,6 +797,11 @@ int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientID)
Packet.m_pData = Pack.Data();
Packet.m_DataSize = Pack.Size();
if(Antibot()->OnEngineServerMessage(ClientID, Packet.m_pData, Packet.m_DataSize, Flags))
{
return 0;
}
if(!(Flags & MSGFLAG_NORECORD))
{
m_aDemoRecorder[ClientID].RecordMessage(Pack.Data(), Pack.Size());
@ -1319,13 +1328,6 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)
Unpacker.Reset(pPacket->m_pData, pPacket->m_DataSize);
CMsgPacker Packer(NETMSG_EX, true);
int GameFlags = 0;
if(pPacket->m_Flags & NET_CHUNKFLAG_VITAL)
{
GameFlags |= MSGFLAG_VITAL;
}
Antibot()->OnEngineClientMessage(ClientID, pPacket->m_pData, pPacket->m_DataSize, GameFlags);
// unpack msgid and system flag
int Msg;
bool Sys;
@ -2244,10 +2246,36 @@ void CServer::PumpNetwork(bool PacketWaiting)
}
else
{
int GameFlags = 0;
if(Packet.m_Flags & NET_CHUNKFLAG_VITAL)
{
GameFlags |= MSGFLAG_VITAL;
}
if(Antibot()->OnEngineClientMessage(Packet.m_ClientID, Packet.m_pData, Packet.m_DataSize, GameFlags))
{
continue;
}
ProcessClientPacket(&Packet);
}
}
}
{
unsigned char aBuffer[NET_MAX_PAYLOAD];
int Flags;
CNetChunk Packet;
mem_zero(&Packet, sizeof(Packet));
Packet.m_pData = aBuffer;
while(Antibot()->OnEngineSimulateClientMessage(&Packet.m_ClientID, aBuffer, sizeof(aBuffer), &Packet.m_DataSize, &Flags))
{
Packet.m_Flags = 0;
if(Flags & MSGFLAG_VITAL)
{
Packet.m_Flags |= NET_CHUNKFLAG_VITAL;
}
ProcessClientPacket(&Packet);
}
}
m_ServerBan.Update();
m_Econ.Update();