Changed CNetClient and CNetServer to work with CNetTokenManager

Conflicts:
	src/engine/shared/network_client.cpp
This commit is contained in:
heinrich5991 2012-03-31 11:43:41 +02:00 committed by oy
parent 2ba3cf859f
commit 66fcf9308c
6 changed files with 113 additions and 58 deletions

View file

@ -82,7 +82,6 @@ int CNetRecvUnpacker::FetchChunk(CNetChunk *pChunk)
pChunk->m_Flags = Header.m_Flags;
pChunk->m_DataSize = Header.m_Size;
pChunk->m_pData = pData;
pChunk->m_ResponseToken = NET_TOKEN_NONE;
return 1;
}
}
@ -248,6 +247,8 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
}
else
{
// new-style packet
// check size first
if(Size < NET_PACKETHEADERSIZE_LEGACY + 1)
{
@ -303,7 +304,7 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
pPacket->m_DataSize = Size - 15;
pPacket->m_ResponseToken = (pBuffer[11]<<24) | (pBuffer[12]<<16) | (pBuffer[13]<<8) | pBuffer[14];
mem_copy(pPacket->m_aChunkData, &pBuffer[NET_PACKETHEADERSIZE], pPacket->m_DataSize);
mem_copy(pPacket->m_aChunkData, &pBuffer[15], pPacket->m_DataSize);
}
}
else
@ -335,6 +336,20 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
return -1;
}
// set the response token (a bit hacky because this function doesn't know about control packets)
if(pPacket->m_Version == NET_PACKETVERSION && (pPacket->m_Flags&NET_PACKETFLAG_CONTROL))
{
if(pPacket->m_DataSize >= 5) // control byte + token
{
if(pPacket->m_aChunkData[0] == NET_CTRLMSG_CONNECT
|| pPacket->m_aChunkData[0] == NET_CTRLMSG_TOKEN)
{
pPacket->m_ResponseToken = (pPacket->m_aChunkData[1]<<24) | (pPacket->m_aChunkData[2]<<16)
| (pPacket->m_aChunkData[3]<<8) | pPacket->m_aChunkData[4];
}
}
}
// log the data
if(ms_DataLogRecv)
{

View file

@ -37,9 +37,11 @@ CURRENT:
enum
{
NETFLAG_ALLOWSTATELESS=1,
NETFLAG_ALLOWOLDSTYLE=2,
NETSENDFLAG_VITAL=1,
NETSENDFLAG_CONNLESS=2,
NETSENDFLAG_FLUSH=4,
NETSENDFLAG_STATELESS=8, // for connless packets only
NETSTATE_OFFLINE=0,
NETSTATE_CONNECTING,
@ -107,7 +109,6 @@ struct CNetChunk
// 0 on the client means the server
int m_ClientID;
NETADDR m_Address; // only used when cid == -1
unsigned int m_ResponseToken; // only used when cid == -1
int m_Flags;
int m_DataSize;
const void *m_pData;
@ -157,14 +158,14 @@ public:
void GenerateSeed();
void ProcessTokenMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket);
int ProcessMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket, bool Notify);
bool CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken);
bool ConnectionToken(unsigned int Token);
static unsigned int GenerateToken(const NETADDR *pAddr, int64 Seed);
bool CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken, bool Notify);
unsigned int GenerateToken(const NETADDR *pAddr);
private:
static unsigned int GenerateToken(const NETADDR *pAddr, int64 Seed);
NETSOCKET m_Socket;
int64 m_Seed;
@ -317,6 +318,7 @@ class CNetServer
CNetTokenManager m_TokenManager;
int m_Flags;
public:
int SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser);
@ -324,9 +326,9 @@ public:
bool Open(NETADDR BindAddr, class CNetBan *pNetBan, int MaxClients, int MaxClientsPerIP, int Flags);
int Close();
//
int Recv(CNetChunk *pChunk);
int Send(CNetChunk *pChunk);
// the token and version parameter are only used for connless packets
int Recv(CNetChunk *pChunk, unsigned int *pResponseToken = 0, int *pVersion = 0);
int Send(CNetChunk *pChunk, unsigned int Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION);
int Update();
//
@ -390,7 +392,9 @@ class CNetClient
{
CNetConnection m_Connection;
CNetRecvUnpacker m_RecvUnpacker;
CNetTokenManager m_TokenManager;
NETSOCKET m_Socket;
int m_Flags;
public:
// openness
bool Open(NETADDR BindAddr, int Flags);
@ -401,8 +405,8 @@ public:
int Connect(NETADDR *Addr);
// communication
int Recv(CNetChunk *Chunk);
int Send(CNetChunk *Chunk);
int Recv(CNetChunk *pChunk, unsigned int *pResponseToken = 0, int *pVersion = 0);
int Send(CNetChunk *pChunk, unsigned int Token = NET_TOKEN_NONE, int Version = NET_PACKETVERSION);
// pumping
int Update();

View file

@ -20,6 +20,8 @@ bool CNetClient::Open(NETADDR BindAddr, int Flags)
m_TokenManager.Init(Socket);
m_Flags = Flags;
return true;
}
@ -57,7 +59,7 @@ int CNetClient::ResetErrorString()
return 0;
}
int CNetClient::Recv(CNetChunk *pChunk)
int CNetClient::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVersion)
{
while(1)
{
@ -75,27 +77,45 @@ int CNetClient::Recv(CNetChunk *pChunk)
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data) == 0)
{
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
if(net_addr_comp(m_Connection.PeerAddress(), &Addr) == 0)
{
if(m_Connection.State() != NET_CONNSTATE_OFFLINE && m_Connection.State() != NET_CONNSTATE_ERROR && m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr))
m_RecvUnpacker.Start(&Addr, &m_Connection, 0);
}
else if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
{
if(!(m_Flags&NETFLAG_ALLOWOLDSTYLE) && m_RecvUnpacker.m_Data.m_Version != NET_PACKETVERSION)
continue;
int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data, false);
if(!Accept)
continue;
pChunk->m_Flags = NETSENDFLAG_CONNLESS;
if(Accept < 0)
{
if(!(m_Flags&NETFLAG_ALLOWSTATELESS))
continue;
pChunk->m_Flags |= NETSENDFLAG_STATELESS;
}
pChunk->m_ClientID = -1;
pChunk->m_Address = Addr;
pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize;
pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData;
if(pVersion)
*pVersion = m_RecvUnpacker.m_Data.m_Version;
if(pResponseToken)
*pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken;
return 1;
}
else
{
if(m_Connection.State() != NET_CONNSTATE_OFFLINE && m_Connection.State() != NET_CONNSTATE_ERROR && net_addr_comp(m_Connection.PeerAddress(), &Addr) == 0
&& m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr))
m_RecvUnpacker.Start(&Addr, &m_Connection, 0);
}
}
}
return 0;
}
int CNetClient::Send(CNetChunk *pChunk)
int CNetClient::Send(CNetChunk *pChunk, unsigned int Token, int Version)
{
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{
@ -106,7 +126,7 @@ int CNetClient::Send(CNetChunk *pChunk)
}
// send connectionless packet
CNetBase::SendPacketConnless(m_Socket, &pChunk->m_Address, pChunk->m_pData, pChunk->m_DataSize);
CNetBase::SendPacketConnless(m_Socket, &pChunk->m_Address, Version, Token, m_TokenManager.GenerateToken(&pChunk->m_Address), pChunk->m_pData, pChunk->m_DataSize);
}
else
{

View file

@ -41,8 +41,7 @@ void CNetConnection::SetToken(unsigned int Token)
unsigned int CNetConnection::GenerateToken(const NETADDR *pPeerAddr)
{
return ((rand() & 0xff) << 24) | ((rand() & 0xff) << 16)
| ((rand() & 0xff) << 8) | (rand() & 0xff);
return ((rand() & 0xffff) << 16) | (rand() & 0xffff);
}
const char *CNetConnection::ErrorString()

View file

@ -34,6 +34,8 @@ bool CNetServer::Open(NETADDR BindAddr, CNetBan *pNetBan, int MaxClients, int Ma
for(int i = 0; i < NET_MAX_CLIENTS; i++)
m_aSlots[i].m_Connection.Init(m_Socket, true);
m_Flags = Flags;
return true;
}
@ -93,7 +95,7 @@ int CNetServer::Update()
/*
TODO: chopp up this function into smaller working parts
*/
int CNetServer::Recv(CNetChunk *pChunk)
int CNetServer::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVersion)
{
while(1)
{
@ -112,23 +114,52 @@ int CNetServer::Recv(CNetChunk *pChunk)
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data) == 0)
{
// check if we just should drop the packet
// try to find matching slot
for(int i = 0; i < MaxClients(); i++)
{
if(net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr) == 0)
{
if(m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr))
{
if(m_RecvUnpacker.m_Data.m_DataSize)
m_RecvUnpacker.Start(&Addr, &m_aSlots[i].m_Connection, i);
}
continue;
}
}
// no matching slot, check for bans
char aBuf[128];
if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf)))
{
// banned, reply with a message
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf)+1);
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_Version, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf)+1);
continue;
}
int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data, true);
if(!Accept)
continue;
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
{
if(!(m_Flags&NETFLAG_ALLOWOLDSTYLE) && m_RecvUnpacker.m_Data.m_Version != NET_PACKETVERSION)
continue;
pChunk->m_Flags = NETSENDFLAG_CONNLESS;
if(Accept < 0)
{
if(!(m_Flags&NETFLAG_ALLOWSTATELESS))
continue;
pChunk->m_Flags |= NETSENDFLAG_STATELESS;
}
pChunk->m_ClientID = -1;
pChunk->m_Address = Addr;
pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize;
pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData;
pChunk->m_ResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken;
if(pVersion)
*pVersion = m_RecvUnpacker.m_Data.m_Version;
if(pResponseToken)
*pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken;
return 1;
}
else
@ -169,7 +200,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP);
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, aBuf, sizeof(aBuf));
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_Version, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, sizeof(aBuf));
return 0;
}
}
@ -190,22 +221,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
if(!Found)
{
const char FullMsg[] = "This server is full";
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg));
}
}
}
else
{
// normal packet, find matching slot
for(int i = 0; i < MaxClients(); i++)
{
if(net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr) == 0)
{
if(m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr))
{
if(m_RecvUnpacker.m_Data.m_DataSize)
m_RecvUnpacker.Start(&Addr, &m_aSlots[i].m_Connection, i);
}
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_Version, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg));
}
}
}
@ -215,7 +231,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
return 0;
}
int CNetServer::Send(CNetChunk *pChunk)
int CNetServer::Send(CNetChunk *pChunk, unsigned int Token, int Version)
{
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{
@ -226,7 +242,7 @@ int CNetServer::Send(CNetChunk *pChunk)
}
// send connectionless packet
CNetBase::SendPacketConnless(m_Socket, &pChunk->m_Address, pChunk->m_pData, pChunk->m_DataSize);
CNetBase::SendPacketConnless(m_Socket, &pChunk->m_Address, Version, Token, m_TokenManager.GenerateToken(&pChunk->m_Address), pChunk->m_pData, pChunk->m_DataSize);
}
else
{

View file

@ -29,9 +29,9 @@ void CNetTokenManager::Init(NETSOCKET Socket)
GenerateSeed();
}
int CNetTokenManager::ProcessMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket)
int CNetTokenManager::ProcessMessage(const NETADDR *pAddr, const CNetPacketConstruct *pPacket, bool Notify)
{
if(pPacket->m_Token != NET_TOKEN_NONE && !CheckToken(pAddr, pPacket->m_Token))
if(pPacket->m_Token != NET_TOKEN_NONE && !CheckToken(pAddr, pPacket->m_Token, pPacket->m_ResponseToken, Notify))
return 0; // wrong token, silent ignore
bool Verified = pPacket->m_Token != NET_TOKEN_NONE;
@ -53,12 +53,7 @@ int CNetTokenManager::ProcessMessage(const NETADDR *pAddr, const CNetPacketConst
return 0; // everything is fine, token exchange complete
// client requesting token
unsigned int RemoteToken = (pPacket->m_aChunkData[0]<<24)
| (pPacket->m_aChunkData[1]<<16)
| (pPacket->m_aChunkData[2]<<8)
| (pPacket->m_aChunkData[3]);
CNetBase::SendToken(m_Socket, (NETADDR *)pAddr, GenerateToken(pAddr, m_Seed), RemoteToken);
CNetBase::SendToken(m_Socket, (NETADDR *)pAddr, GenerateToken(pAddr, m_Seed), pPacket->m_ResponseToken);
return 0; // no need to process NET_CTRLMSG_TOKEN further
}
@ -75,6 +70,11 @@ void CNetTokenManager::GenerateSeed()
}
}
unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr)
{
return GenerateToken(pAddr, m_Seed);
}
unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr, int64 Seed)
{
char aBuf[sizeof(NETADDR) + sizeof(int64)];
@ -89,7 +89,7 @@ unsigned int CNetTokenManager::GenerateToken(const NETADDR *pAddr, int64 Seed)
return Result;
}
bool CNetTokenManager::CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken)
bool CNetTokenManager::CheckToken(const NETADDR *pAddr, unsigned int Token, unsigned int ResponseToken, bool Notify)
{
unsigned int CurrentToken = GenerateToken(pAddr, m_Seed);
if(CurrentToken == Token)
@ -97,7 +97,8 @@ bool CNetTokenManager::CheckToken(const NETADDR *pAddr, unsigned int Token, unsi
if(GenerateToken(pAddr, m_PrevSeed) == Token)
{
CNetBase::SendToken(m_Socket, (NETADDR *)pAddr, CurrentToken, ResponseToken); // notify the peer about the new token
if(Notify)
CNetBase::SendToken(m_Socket, (NETADDR *)pAddr, CurrentToken, ResponseToken); // notify the peer about the new token
return true;
}