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_Flags = Header.m_Flags;
pChunk->m_DataSize = Header.m_Size; pChunk->m_DataSize = Header.m_Size;
pChunk->m_pData = pData; pChunk->m_pData = pData;
pChunk->m_ResponseToken = NET_TOKEN_NONE;
return 1; return 1;
} }
} }
@ -248,6 +247,8 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
} }
else else
{ {
// new-style packet
// check size first // check size first
if(Size < NET_PACKETHEADERSIZE_LEGACY + 1) 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_DataSize = Size - 15;
pPacket->m_ResponseToken = (pBuffer[11]<<24) | (pBuffer[12]<<16) | (pBuffer[13]<<8) | pBuffer[14]; 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 else
@ -335,6 +336,20 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
return -1; 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 // log the data
if(ms_DataLogRecv) if(ms_DataLogRecv)
{ {

View file

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

View file

@ -20,6 +20,8 @@ bool CNetClient::Open(NETADDR BindAddr, int Flags)
m_TokenManager.Init(Socket); m_TokenManager.Init(Socket);
m_Flags = Flags;
return true; return true;
} }
@ -57,7 +59,7 @@ int CNetClient::ResetErrorString()
return 0; return 0;
} }
int CNetClient::Recv(CNetChunk *pChunk) int CNetClient::Recv(CNetChunk *pChunk, unsigned int *pResponseToken, int *pVersion)
{ {
while(1) 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(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; 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_ClientID = -1;
pChunk->m_Address = Addr; pChunk->m_Address = Addr;
pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize;
pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; 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; 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; return 0;
} }
int CNetClient::Send(CNetChunk *pChunk) int CNetClient::Send(CNetChunk *pChunk, unsigned int Token, int Version)
{ {
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS) if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{ {
@ -106,7 +126,7 @@ int CNetClient::Send(CNetChunk *pChunk)
} }
// send connectionless packet // 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 else
{ {

View file

@ -41,8 +41,7 @@ void CNetConnection::SetToken(unsigned int Token)
unsigned int CNetConnection::GenerateToken(const NETADDR *pPeerAddr) unsigned int CNetConnection::GenerateToken(const NETADDR *pPeerAddr)
{ {
return ((rand() & 0xff) << 24) | ((rand() & 0xff) << 16) return ((rand() & 0xffff) << 16) | (rand() & 0xffff);
| ((rand() & 0xff) << 8) | (rand() & 0xff);
} }
const char *CNetConnection::ErrorString() 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++) for(int i = 0; i < NET_MAX_CLIENTS; i++)
m_aSlots[i].m_Connection.Init(m_Socket, true); m_aSlots[i].m_Connection.Init(m_Socket, true);
m_Flags = Flags;
return true; return true;
} }
@ -93,7 +95,7 @@ int CNetServer::Update()
/* /*
TODO: chopp up this function into smaller working parts 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) while(1)
{ {
@ -112,23 +114,52 @@ int CNetServer::Recv(CNetChunk *pChunk)
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data) == 0) 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]; char aBuf[128];
if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf))) if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf)))
{ {
// banned, reply with a message // 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; 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_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; 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_ClientID = -1;
pChunk->m_Address = Addr; pChunk->m_Address = Addr;
pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize;
pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; 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; return 1;
} }
else else
@ -169,7 +200,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
{ {
char aBuf[128]; char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP); 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; return 0;
} }
} }
@ -190,22 +221,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
if(!Found) if(!Found)
{ {
const char FullMsg[] = "This server is full"; const char FullMsg[] = "This server is full";
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg)); CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_Version, m_RecvUnpacker.m_Data.m_ResponseToken, 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);
}
} }
} }
} }
@ -215,7 +231,7 @@ int CNetServer::Recv(CNetChunk *pChunk)
return 0; return 0;
} }
int CNetServer::Send(CNetChunk *pChunk) int CNetServer::Send(CNetChunk *pChunk, unsigned int Token, int Version)
{ {
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS) if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
{ {
@ -226,7 +242,7 @@ int CNetServer::Send(CNetChunk *pChunk)
} }
// send connectionless packet // 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 else
{ {

View file

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