Remove MAXBANS

Needs proper stress testing but seems to work.

# Conflicts:
#	src/i_tcp.c
This commit is contained in:
toaster 2022-06-14 16:45:21 +01:00
parent 8b7aab97c6
commit 77e67d86bc
2 changed files with 65 additions and 27 deletions

View file

@ -3125,7 +3125,7 @@ static void Command_Ban(void)
{ {
if (COM_Argc() < 2) if (COM_Argc() < 2)
{ {
CONS_Printf(M_GetText("Ban <playername/playernum> <reason>: ban and kick a player\n")); CONS_Printf(M_GetText("ban <playername/playernum> <reason>: ban and kick a player\n"));
return; return;
} }
@ -3149,7 +3149,7 @@ static void Command_Ban(void)
if (server && I_Ban && !I_Ban(node)) // only the server is allowed to do this right now if (server && I_Ban && !I_Ban(node)) // only the server is allowed to do this right now
{ {
CONS_Alert(CONS_WARNING, M_GetText("Too many bans! Geez, that's a lot of people you're excluding...\n")); CONS_Alert(CONS_WARNING, M_GetText("Ban failed. Invalid node?\n"));
WRITEUINT8(p, KICK_MSG_GO_AWAY); WRITEUINT8(p, KICK_MSG_GO_AWAY);
SendNetXCmd(XD_KICK, &buf, 2); SendNetXCmd(XD_KICK, &buf, 2);
} }

View file

@ -174,8 +174,6 @@ static UINT8 UPNP_support = TRUE;
#endif // !NONET #endif // !NONET
#define MAXBANS 100
#include "i_system.h" #include "i_system.h"
#include "i_net.h" #include "i_net.h"
#include "d_net.h" #include "d_net.h"
@ -183,6 +181,7 @@ static UINT8 UPNP_support = TRUE;
#include "i_tcp.h" #include "i_tcp.h"
#include "m_argv.h" #include "m_argv.h"
#include "stun.h" #include "stun.h"
#include "z_zone.h"
#include "doomstat.h" #include "doomstat.h"
@ -239,11 +238,13 @@ static mysockaddr_t clientaddress[MAXNETNODES+1];
static mysockaddr_t broadcastaddress[MAXNETNODES+1]; static mysockaddr_t broadcastaddress[MAXNETNODES+1];
static size_t broadcastaddresses = 0; static size_t broadcastaddresses = 0;
static boolean nodeconnected[MAXNETNODES+1]; static boolean nodeconnected[MAXNETNODES+1];
static mysockaddr_t banned[MAXBANS]; static mysockaddr_t *banned;
static UINT8 bannedmask[MAXBANS]; static UINT8 *bannedmask;
#endif #endif
static size_t numbans = 0; static size_t numbans = 0;
static size_t banned_size = 0;
static boolean SOCK_bannednode[MAXNETNODES+1]; /// \note do we really need the +1? static boolean SOCK_bannednode[MAXNETNODES+1]; /// \note do we really need the +1?
static boolean init_tcp_driver = false; static boolean init_tcp_driver = false;
@ -1395,6 +1396,39 @@ static boolean SOCK_OpenSocket(void)
#endif #endif
} }
static void AddBannedIndex(void)
{
if (numbans >= banned_size)
{
if (banned_size == 0)
{
banned_size = 8;
}
else
{
banned_size *= 2;
}
banned = Z_ReallocAlign(
(void*) banned,
sizeof(mysockaddr_t) * banned_size,
PU_STATIC,
NULL,
sizeof(mysockaddr_t) * 8
);
bannedmask = Z_ReallocAlign(
(void*) banned,
sizeof(UINT8) * banned_size,
PU_STATIC,
NULL,
sizeof(UINT8) * 8
);
}
numbans++;
}
static boolean SOCK_Ban(INT32 node) static boolean SOCK_Ban(INT32 node)
{ {
if (node > MAXNETNODES) if (node > MAXNETNODES)
@ -1402,23 +1436,23 @@ static boolean SOCK_Ban(INT32 node)
#ifdef NONET #ifdef NONET
return false; return false;
#else #else
if (numbans == MAXBANS)
return false;
M_Memcpy(&banned[numbans], &clientaddress[node], sizeof (mysockaddr_t)); AddBannedIndex();
if (banned[numbans].any.sa_family == AF_INET)
M_Memcpy(&banned[numbans-1], &clientaddress[node], sizeof (mysockaddr_t));
if (banned[numbans-1].any.sa_family == AF_INET)
{ {
banned[numbans].ip4.sin_port = 0; banned[numbans-1].ip4.sin_port = 0;
bannedmask[numbans] = 32; bannedmask[numbans-1] = 32;
} }
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
else if (banned[numbans].any.sa_family == AF_INET6) else if (banned[numbans-1].any.sa_family == AF_INET6)
{ {
banned[numbans].ip6.sin6_port = 0; banned[numbans-1].ip6.sin6_port = 0;
bannedmask[numbans] = 128; bannedmask[numbans-1] = 128;
} }
#endif #endif
numbans++;
return true; return true;
#endif #endif
} }
@ -1433,7 +1467,7 @@ static boolean SOCK_SetBanAddress(const char *address, const char *mask)
struct my_addrinfo *ai, *runp, hints; struct my_addrinfo *ai, *runp, hints;
int gaie; int gaie;
if (numbans == MAXBANS || !address) if (!address)
return false; return false;
memset(&hints, 0x00, sizeof(hints)); memset(&hints, 0x00, sizeof(hints));
@ -1448,26 +1482,27 @@ static boolean SOCK_SetBanAddress(const char *address, const char *mask)
runp = ai; runp = ai;
while(runp != NULL && numbans != MAXBANS) while (runp != NULL)
{ {
memcpy(&banned[numbans], runp->ai_addr, runp->ai_addrlen); AddBannedIndex();
memcpy(&banned[numbans-1], runp->ai_addr, runp->ai_addrlen);
if (mask) if (mask)
bannedmask[numbans] = (UINT8)atoi(mask); bannedmask[numbans-1] = (UINT8)atoi(mask);
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
else if (runp->ai_family == AF_INET6) else if (runp->ai_family == AF_INET6)
bannedmask[numbans] = 128; bannedmask[numbans-1] = 128;
#endif #endif
else else
bannedmask[numbans] = 32; bannedmask[numbans-1] = 32;
if (bannedmask[numbans] > 32 && runp->ai_family == AF_INET) if (bannedmask[numbans-1] > 32 && runp->ai_family == AF_INET)
bannedmask[numbans] = 32; bannedmask[numbans-1] = 32;
#ifdef HAVE_IPV6 #ifdef HAVE_IPV6
else if (bannedmask[numbans] > 128 && runp->ai_family == AF_INET6) else if (bannedmask[numbans-1] > 128 && runp->ai_family == AF_INET6)
bannedmask[numbans] = 128; bannedmask[numbans-1] = 128;
#endif #endif
numbans++;
runp = runp->ai_next; runp = runp->ai_next;
} }
@ -1480,6 +1515,9 @@ static boolean SOCK_SetBanAddress(const char *address, const char *mask)
static void SOCK_ClearBans(void) static void SOCK_ClearBans(void)
{ {
numbans = 0; numbans = 0;
banned_size = 0;
banned = NULL;
bannedmask = NULL;
} }
boolean I_InitTcpNetwork(void) boolean I_InitTcpNetwork(void)