2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2009-10-27 14:38:53 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
#include <engine/console.h>
|
2011-04-09 06:41:31 +00:00
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
#include "netban.h"
|
|
|
|
#include "network.h"
|
2009-10-27 14:38:53 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
bool CNetServer::Open(NETADDR BindAddr, CNetBan *pNetBan, int MaxClients, int MaxClientsPerIP, int Flags)
|
2009-10-27 14:38:53 +00:00
|
|
|
{
|
|
|
|
// zero out the whole structure
|
|
|
|
mem_zero(this, sizeof(*this));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
// open socket
|
|
|
|
m_Socket = net_udp_create(BindAddr);
|
2011-03-28 18:11:28 +00:00
|
|
|
if(!m_Socket.type)
|
2009-10-27 14:38:53 +00:00
|
|
|
return false;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
m_pNetBan = pNetBan;
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
// clamp clients
|
|
|
|
m_MaxClients = MaxClients;
|
|
|
|
if(m_MaxClients > NET_MAX_CLIENTS)
|
|
|
|
m_MaxClients = NET_MAX_CLIENTS;
|
|
|
|
if(m_MaxClients < 1)
|
|
|
|
m_MaxClients = 1;
|
2010-06-03 12:48:32 +00:00
|
|
|
|
|
|
|
m_MaxClientsPerIP = MaxClientsPerIP;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
for(int i = 0; i < NET_MAX_CLIENTS; i++)
|
2012-04-20 19:39:49 +00:00
|
|
|
m_aSlots[i].m_Connection.Init(m_Socket, true);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetServer::SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser)
|
|
|
|
{
|
|
|
|
m_pfnNewClient = pfnNewClient;
|
|
|
|
m_pfnDelClient = pfnDelClient;
|
|
|
|
m_UserPtr = pUser;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetServer::Close()
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: implement me
|
2009-10-27 14:38:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetServer::Drop(int ClientID, const char *pReason)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: insert lots of checks here
|
2011-05-04 23:43:27 +00:00
|
|
|
/*NETADDR Addr = ClientAddr(ClientID);
|
2009-10-27 14:38:53 +00:00
|
|
|
|
2011-05-04 23:43:27 +00:00
|
|
|
dbg_msg("net_server", "client dropped. cid=%d ip=%d.%d.%d.%d reason=\"%s\"",
|
2009-10-27 14:38:53 +00:00
|
|
|
ClientID,
|
|
|
|
Addr.ip[0], Addr.ip[1], Addr.ip[2], Addr.ip[3],
|
|
|
|
pReason
|
2010-08-17 22:06:00 +00:00
|
|
|
);*/
|
|
|
|
if(m_pfnDelClient)
|
|
|
|
m_pfnDelClient(ClientID, pReason, m_UserPtr);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
m_aSlots[ClientID].m_Connection.Disconnect(pReason);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetServer::Update()
|
|
|
|
{
|
2014-01-13 16:00:49 +00:00
|
|
|
//int64 Now = time_get();
|
2009-10-27 14:38:53 +00:00
|
|
|
for(int i = 0; i < MaxClients(); i++)
|
|
|
|
{
|
|
|
|
m_aSlots[i].m_Connection.Update();
|
|
|
|
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ERROR)
|
2013-09-09 19:53:13 +00:00
|
|
|
{
|
2014-01-16 14:11:42 +00:00
|
|
|
if (Now - m_aSlots[i].m_Connection.ConnectTime() < time_freq() / 5 && NetBan())
|
|
|
|
NetBan()->BanAddr(ClientAddr(i), 60, "Too many connections");
|
|
|
|
else
|
2014-01-12 15:32:06 +00:00
|
|
|
Drop(i, m_aSlots[i].m_Connection.ErrorString());
|
2013-09-09 19:53:13 +00:00
|
|
|
}
|
2009-10-27 14:38:53 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: chopp up this function into smaller working parts
|
|
|
|
*/
|
|
|
|
int CNetServer::Recv(CNetChunk *pChunk)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
NETADDR Addr;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// check for a chunk
|
2009-10-27 14:38:53 +00:00
|
|
|
if(m_RecvUnpacker.FetchChunk(pChunk))
|
|
|
|
return 1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: empty the recvinfo
|
2009-10-27 14:38:53 +00:00
|
|
|
int Bytes = net_udp_recv(m_Socket, &Addr, m_RecvUnpacker.m_aBuffer, NET_MAX_PACKETSIZE);
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// no more packets for now
|
2009-10-27 14:38:53 +00:00
|
|
|
if(Bytes <= 0)
|
|
|
|
break;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2013-07-31 03:36:52 +00:00
|
|
|
// check if we just should drop the packet
|
|
|
|
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);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Found = false;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2013-07-31 03:36:52 +00:00
|
|
|
if(CNetBase::UnpackPacket(m_RecvUnpacker.m_aBuffer, Bytes, &m_RecvUnpacker.m_Data) == 0)
|
|
|
|
{
|
2009-10-27 14:38:53 +00:00
|
|
|
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)
|
|
|
|
{
|
|
|
|
pChunk->m_Flags = NETSENDFLAG_CONNLESS;
|
|
|
|
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;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
2011-04-13 18:37:12 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// TODO: check size here
|
2009-10-27 14:38:53 +00:00
|
|
|
if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL && m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_CONNECT)
|
|
|
|
{
|
2013-07-31 03:36:52 +00:00
|
|
|
Found = false;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// check if we already got this client
|
2009-10-27 14:38:53 +00:00
|
|
|
for(int i = 0; i < MaxClients(); i++)
|
|
|
|
{
|
|
|
|
if(m_aSlots[i].m_Connection.State() != NET_CONNSTATE_OFFLINE &&
|
2011-12-29 22:36:53 +00:00
|
|
|
net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr) == 0)
|
2009-10-27 14:38:53 +00:00
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
Found = true; // silent ignore.. we got this client already
|
2009-10-27 14:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// client that wants to connect
|
2009-10-27 14:38:53 +00:00
|
|
|
if(!Found)
|
|
|
|
{
|
2010-06-03 12:48:32 +00:00
|
|
|
// 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;
|
|
|
|
|
2011-12-29 22:36:53 +00:00
|
|
|
OtherAddr = *m_aSlots[i].m_Connection.PeerAddress();
|
2010-06-03 12:48:32 +00:00
|
|
|
OtherAddr.port = 0;
|
|
|
|
if(!net_addr_comp(&ThisAddr, &OtherAddr))
|
|
|
|
{
|
|
|
|
if(FoundAddr++ >= m_MaxClientsPerIP)
|
|
|
|
{
|
|
|
|
char aBuf[128];
|
2010-10-09 18:19:58 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP);
|
2010-06-03 12:48:32 +00:00
|
|
|
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, aBuf, sizeof(aBuf));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
for(int i = 0; i < MaxClients(); i++)
|
|
|
|
{
|
|
|
|
if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE)
|
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
Found = true;
|
2009-10-27 14:38:53 +00:00
|
|
|
m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr);
|
|
|
|
if(m_pfnNewClient)
|
|
|
|
m_pfnNewClient(i, m_UserPtr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
if(!Found)
|
|
|
|
{
|
2010-10-09 18:19:58 +00:00
|
|
|
const char FullMsg[] = "This server is full";
|
2009-10-27 14:38:53 +00:00
|
|
|
CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// normal packet, find matching slot
|
2009-10-27 14:38:53 +00:00
|
|
|
for(int i = 0; i < MaxClients(); i++)
|
|
|
|
{
|
2011-12-29 22:36:53 +00:00
|
|
|
if(net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr) == 0)
|
2009-10-27 14:38:53 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CNetServer::Send(CNetChunk *pChunk)
|
|
|
|
{
|
|
|
|
if(pChunk->m_DataSize >= NET_MAX_PAYLOAD)
|
|
|
|
{
|
|
|
|
dbg_msg("netserver", "packet payload too big. %d. dropping packet", pChunk->m_DataSize);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
if(pChunk->m_Flags&NETSENDFLAG_CONNLESS)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
// send connectionless packet
|
2009-10-27 14:38:53 +00:00
|
|
|
CNetBase::SendPacketConnless(m_Socket, &pChunk->m_Address, pChunk->m_pData, pChunk->m_DataSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int Flags = 0;
|
|
|
|
dbg_assert(pChunk->m_ClientID >= 0, "errornous client id");
|
|
|
|
dbg_assert(pChunk->m_ClientID < MaxClients(), "errornous client id");
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
if(pChunk->m_Flags&NETSENDFLAG_VITAL)
|
|
|
|
Flags = NET_CHUNKFLAG_VITAL;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_aSlots[pChunk->m_ClientID].m_Connection.QueueChunk(Flags, pChunk->m_DataSize, pChunk->m_pData) == 0)
|
|
|
|
{
|
|
|
|
if(pChunk->m_Flags&NETSENDFLAG_FLUSH)
|
|
|
|
m_aSlots[pChunk->m_ClientID].m_Connection.Flush();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-17 01:27:28 +00:00
|
|
|
//Drop(pChunk->m_ClientID, "Error sending data");
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2009-10-27 14:38:53 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-03 12:48:32 +00:00
|
|
|
void CNetServer::SetMaxClientsPerIP(int Max)
|
|
|
|
{
|
|
|
|
// clamp
|
|
|
|
if(Max < 1)
|
|
|
|
Max = 1;
|
|
|
|
else if(Max > NET_MAX_CLIENTS)
|
|
|
|
Max = NET_MAX_CLIENTS;
|
|
|
|
|
|
|
|
m_MaxClientsPerIP = Max;
|
|
|
|
}
|