some cleanup

This commit is contained in:
oy 2015-09-20 18:25:38 +02:00
parent cd83db08b8
commit 3086a422f8
4 changed files with 60 additions and 106 deletions

View file

@ -92,7 +92,7 @@ void CNetBase::SendPacketConnless(NETSOCKET Socket, const NETADDR *pAddr, TOKEN
{
unsigned char aBuffer[NET_MAX_PACKETSIZE];
dbg_assert(DataSize + 15 <= NET_MAX_PACKETSIZE, "packet data size too high");
dbg_assert(DataSize <= NET_MAX_PAYLOAD, "packet data size too high");
dbg_assert((Token&~NET_TOKEN_MASK) == 0, "token out of range");
dbg_assert((ResponseToken&~NET_TOKEN_MASK) == 0, "resp token out of range");
@ -100,10 +100,9 @@ void CNetBase::SendPacketConnless(NETSOCKET Socket, const NETADDR *pAddr, TOKEN
aBuffer[i++] = (Token>>12)&0xff; // token
aBuffer[i++] = (Token>>4)&0xff;
aBuffer[i++] = ((Token&0xf)<<4)
| NET_PACKETFLAG_CONNLESS // connless flag
| ((NET_PACKETVERSION&0x70)>>4); // version
aBuffer[i++] = ((NET_PACKETVERSION<<4)&0xf0)
| (ResponseToken>>16); // response token
| (NET_PACKETFLAG_CONNLESS&0xf); // connless flag
aBuffer[i++] = ((NET_PACKETVERSION&0x0f)<<4) // version
| ((ResponseToken>>16)&0xf); // response token
aBuffer[i++] = (ResponseToken>>8)&0xff;
aBuffer[i++] = ResponseToken&0xff;
@ -133,7 +132,7 @@ void CNetBase::SendPacket(NETSOCKET Socket, const NETADDR *pAddr, CNetPacketCons
// compress if not ctrl msg
if(!(pPacket->m_Flags&NET_PACKETFLAG_CONTROL))
CompressedSize = ms_Huffman.Compress(pPacket->m_aChunkData, pPacket->m_DataSize, &aBuffer[NET_PACKETHEADERSIZE], NET_MAX_PACKETSIZE - NET_PACKETHEADERSIZE - 1);
CompressedSize = ms_Huffman.Compress(pPacket->m_aChunkData, pPacket->m_DataSize, &aBuffer[NET_PACKETHEADERSIZE], NET_MAX_PAYLOAD);
// check if the compression was enabled, successful and good enough
if(CompressedSize > 0 && CompressedSize < pPacket->m_DataSize)
@ -158,7 +157,7 @@ void CNetBase::SendPacket(NETSOCKET Socket, const NETADDR *pAddr, CNetPacketCons
aBuffer[i++] = (pPacket->m_Token>>12)&0xff; // token
aBuffer[i++] = (pPacket->m_Token>>4)&0xff;
aBuffer[i++] = ((pPacket->m_Token<<4)&0xf0)
| (pPacket->m_Flags); // flags
| ((pPacket->m_Flags)&0xf); // flags
aBuffer[i++] = (pPacket->m_Ack>>2)&0xff; // ack
aBuffer[i++] = ((pPacket->m_Ack<<6)&0xc0)
| (pPacket->m_NumChunks&0x3f);
@ -208,15 +207,6 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
if(pPacket->m_Flags&NET_PACKETFLAG_CONNLESS)
{
pPacket->m_Flags = NET_PACKETFLAG_CONNLESS;
pPacket->m_Ack = 0;
pPacket->m_NumChunks = 0;
int Version = ((pBuffer[2]&0x07)<<4) | (pBuffer[3]>>4);
// xxxxxVVV VVVVxxxx
if(Version != NET_PACKETVERSION)
return -1;
if(Size < NET_PACKETHEADERSIZE_CONNLESS)
{
if(g_Config.m_Debug)
@ -224,6 +214,15 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
return -1;
}
pPacket->m_Flags = NET_PACKETFLAG_CONNLESS;
pPacket->m_Ack = 0;
pPacket->m_NumChunks = 0;
int Version = (pBuffer[3]>>4);
// VVVVxxxx
if(Version != NET_PACKETVERSION)
return -1;
pPacket->m_DataSize = Size - NET_PACKETHEADERSIZE_CONNLESS;
pPacket->m_ResponseToken = ((pBuffer[3]&0x0f)<<16) | (pBuffer[4]<<8) | pBuffer[5];
// xxxxRRRR RRRRRRRR RRRRRRRR
@ -231,13 +230,6 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
}
else
{
pPacket->m_Ack = (pBuffer[3]<<2) | ((pBuffer[4]&0xc0)>>6);
// AAAAAAAA AAxxxxxx
pPacket->m_NumChunks = pBuffer[4]&0x3f;
// xxNNNNNN
pPacket->m_DataSize = Size - NET_PACKETHEADERSIZE;
pPacket->m_ResponseToken = NET_TOKEN_NONE;
if(Size - NET_PACKETHEADERSIZE > NET_MAX_PAYLOAD)
{
if(g_Config.m_Debug)
@ -245,6 +237,14 @@ int CNetBase::UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct
return -1;
}
pPacket->m_Ack = (pBuffer[3]<<2) | ((pBuffer[4]&0xc0)>>6);
// AAAAAAAA AAxxxxxx
pPacket->m_NumChunks = pBuffer[4]&0x3f;
// xxNNNNNN
pPacket->m_DataSize = Size - NET_PACKETHEADERSIZE;
pPacket->m_ResponseToken = NET_TOKEN_NONE;
if(pPacket->m_Flags&NET_PACKETFLAG_COMPRESSION)
pPacket->m_DataSize = ms_Huffman.Decompress(&pBuffer[NET_PACKETHEADERSIZE], pPacket->m_DataSize, pPacket->m_aChunkData, sizeof(pPacket->m_aChunkData));
else
@ -309,7 +309,7 @@ void CNetBase::SendControlMsgWithToken(NETSOCKET Socket, const NETADDR *pAddr, T
dbg_assert((Token&~NET_TOKEN_MASK) == 0, "token out of range");
dbg_assert((MyToken&~NET_TOKEN_MASK) == 0, "resp token out of range");
char aToken[3];
unsigned char aToken[3];
aToken[0] = (MyToken>>16)&0xff;
aToken[1] = (MyToken>>8)&0xff;
aToken[2] = (MyToken)&0xff;

View file

@ -22,13 +22,13 @@ CURRENT:
packet header (CONNLESS):
unsigned char token[2]; // 16bit token
unsigned char token_flag_version; // 4bit token, 1bit flag, 3bit version
unsigned char token_flag; // 4bit token, 4bit flags
unsigned char version_responsetoken; // 4bit version, 4bit response token
unsigned char responsetoken[2]; // 16bit response token
// TTTTTTTT
// TTTTTTTT
// TTTTfvvv
// TTTTffff
// vvvvRRRR
// RRRRRRRR
// RRRRRRRR
@ -65,21 +65,39 @@ enum
{
NET_VERSION = 2,
NET_SEEDTIME = 10,
NET_MAX_PACKETSIZE = 1400,
NET_MAX_PAYLOAD = NET_MAX_PACKETSIZE-15,
NET_MAX_CHUNKHEADERSIZE = 3,
// packets
NET_PACKETHEADERSIZE = 5,
NET_PACKETHEADERSIZE_CONNLESS = NET_PACKETHEADERSIZE + 1,
NET_MAX_PACKETHEADERSIZE = NET_PACKETHEADERSIZE_CONNLESS,
NET_MAX_PACKETSIZE = 1400,
NET_MAX_PAYLOAD = NET_MAX_PACKETSIZE-NET_MAX_PACKETHEADERSIZE,
NET_PACKETVERSION=1,
NET_PACKETFLAG_CONTROL=1,
NET_PACKETFLAG_RESEND=2,
NET_PACKETFLAG_COMPRESSION=4,
NET_PACKETFLAG_CONNLESS=8,
// token
NET_SEEDTIME = 10,
NET_TOKENCACHE_SIZE = 16,
NET_TOKENCACHE_ADDRESSEXPIRY = NET_SEEDTIME/2,
NET_TOKENCACHE_PACKETEXPIRY = NET_TOKENCACHE_ADDRESSEXPIRY,
NET_MAX_CLIENTS = 16,
NET_MAX_CONSOLE_CLIENTS = 4,
NET_MAX_SEQUENCE = 1<<10,
NET_TOKEN_MAX = 0xfffff,
NET_TOKEN_NONE = NET_TOKEN_MAX,
NET_TOKEN_MASK = NET_TOKEN_MAX,
//
NET_MAX_CLIENTS = 16,
NET_MAX_CONSOLE_CLIENTS = 4,
NET_MAX_SEQUENCE = 1<<10,
NET_SEQUENCE_MASK = NET_MAX_SEQUENCE-1,
NET_CONNSTATE_OFFLINE=0,
@ -89,15 +107,6 @@ enum
NET_CONNSTATE_ONLINE=4,
NET_CONNSTATE_ERROR=5,
NET_PACKETVERSION=1,
NET_PACKETFLAG_CONTROL=1,
NET_PACKETFLAG_RESEND=2,
NET_PACKETFLAG_COMPRESSION=4,
NET_PACKETFLAG_CONNLESS=8,
// warning: the code assumes that the connless flag
// is the highest
NET_CHUNKFLAG_VITAL=1,
NET_CHUNKFLAG_RESEND=2,

View file

@ -4,7 +4,7 @@
#include "config.h"
#include "network.h"
#include <stdlib.h>
#include <stdlib.h> // rand
void CNetConnection::ResetStats()
{
@ -316,7 +316,6 @@ int CNetConnection::Feed(CNetPacketConstruct *pPacket, NETADDR *pAddr)
TOKEN Token = m_Token;
Reset();
m_State = NET_CONNSTATE_PENDING;
m_Token = Token;
m_PeerAddr = *pAddr;
m_PeerToken = pPacket->m_ResponseToken;
m_Token = Token;

View file

@ -118,6 +118,15 @@ int CNetServer::Recv(CNetChunk *pChunk, TOKEN *pResponseToken)
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data) == 0)
{
// check for bans
char aBuf[128];
if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf)))
{
// banned, reply with a message
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf)+1);
continue;
}
bool Found = false;
// try to find matching slot
for(int i = 0; i < MaxClients(); i++)
@ -150,15 +159,6 @@ int CNetServer::Recv(CNetChunk *pChunk, TOKEN *pResponseToken)
if(Found)
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, 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(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL)
@ -232,60 +232,6 @@ int CNetServer::Recv(CNetChunk *pChunk, TOKEN *pResponseToken)
*pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken;
return 1;
}
else
{
if(!Accept)
continue;
// TODO: check size here
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL && m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_CONNECT)
{
bool Found = false;
// only allow a specific number of players with the same ip
NETADDR ThisAddr = Addr, OtherAddr;
int FoundAddr = 1;
ThisAddr.port = 0;
for(int i = 0; i < MaxClients(); i++)
{
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE)
continue;
OtherAddr = *m_aSlots[i].m_Connection.PeerAddress();
OtherAddr.port = 0;
if(!net_addr_comp(&ThisAddr, &OtherAddr))
{
if(FoundAddr++ >= m_MaxClientsPerIP)
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP);
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, sizeof(aBuf));
return 0;
}
}
}
for(int i = 0; i < MaxClients(); i++)
{
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE)
{
Found = true;
m_aSlots[i].m_Connection.SetToken(m_RecvUnpacker.m_Data.m_Token);
m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr);
m_aSlots[i].m_Connection.SetToken(m_RecvUnpacker.m_Data.m_Token); // HACK!
if(m_pfnNewClient)
m_pfnNewClient(i, m_UserPtr);
break;
}
}
if(!Found)
{
const char FullMsg[] = "This server is full";
CNetBase::SendControlMsg(m_Socket, &Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg));
}
}
}
}
}
return 0;