Remove MAXBANS

Needs proper stress testing but seems to work.
This commit is contained in:
Sally Coolatta 2022-06-06 16:28:50 -04:00 committed by toaster
parent f571e559d4
commit 118eb0b3dd
2 changed files with 65 additions and 27 deletions

View file

@ -2524,7 +2524,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;
} }
@ -2548,7 +2548,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

@ -138,8 +138,6 @@
#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"
@ -147,6 +145,7 @@
#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"
@ -196,12 +195,14 @@
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;
static const INT32 hole_punch_magic = MSBF_LONG (0x52eb11); static const INT32 hole_punch_magic = MSBF_LONG (0x52eb11);
#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;
@ -1423,6 +1424,39 @@ static boolean SOCK_OpenSocket(void)
#endif #endif
} }
static void AddBannedIndex(void)
{
if (numbans >= banned_size)
{
if (banned_size == 0)
{
banned_size = 128;
}
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)
@ -1430,23 +1464,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
} }
@ -1461,7 +1495,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));
@ -1476,26 +1510,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;
} }
@ -1508,6 +1543,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)