2014-01-30 17:33:36 +00:00
|
|
|
/* =======================================================================
|
|
|
|
*
|
|
|
|
* Game side of server CMDs. At this time only the ipfilter.
|
|
|
|
*
|
|
|
|
* =======================================================================
|
|
|
|
*/
|
|
|
|
|
2011-10-11 11:40:43 +00:00
|
|
|
#include "header/local.h"
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
#define MAX_IPFILTERS 1024
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
/*
|
2014-01-28 16:28:57 +00:00
|
|
|
* ==============================================================================
|
|
|
|
*
|
|
|
|
* PACKET FILTERING
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* You can add or remove addresses from the filter list with:
|
|
|
|
*
|
|
|
|
* addip <ip>
|
|
|
|
* removeip <ip>
|
|
|
|
*
|
|
|
|
* The ip address is specified in dot format, and any unspecified digits will match
|
|
|
|
* any value, so you can specify an entire class C network with "addip 192.246.40".
|
|
|
|
*
|
|
|
|
* Removeip will only remove an address specified exactly the same way. You cannot
|
|
|
|
* addip a subnet, then removeip a single host.
|
|
|
|
*
|
|
|
|
* listip
|
|
|
|
* Prints the current list of filters.
|
|
|
|
*
|
|
|
|
* writeip
|
|
|
|
* Dumps "addip <ip>" commands to listip.cfg so it can be execed at a later date.
|
|
|
|
* The filter lists are not saved and restored by default, because I beleive it
|
|
|
|
* would cause too much confusion.
|
|
|
|
*
|
|
|
|
* filterban <0 or 1>
|
|
|
|
*
|
|
|
|
* If 1 (the default), then ip addresses matching the current list will be prohibited
|
|
|
|
* from entering the game. This is the default setting.
|
|
|
|
*
|
|
|
|
* If 0, then only addresses matching the list will be allowed. This lets you easily
|
|
|
|
* set up a private game, or a game that only allows players from your local network.
|
|
|
|
*
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
unsigned mask;
|
|
|
|
unsigned compare;
|
2009-03-12 20:03:41 +00:00
|
|
|
} ipfilter_t;
|
2014-02-22 12:20:22 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
ipfilter_t ipfilters[MAX_IPFILTERS];
|
|
|
|
int numipfilters;
|
2014-02-22 12:20:22 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
void
|
|
|
|
Svcmd_Test_f(void)
|
|
|
|
{
|
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Svcmd_Test_f()\n");
|
|
|
|
}
|
2014-02-22 12:20:22 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
qboolean
|
|
|
|
StringToFilter(char *s, ipfilter_t *f)
|
|
|
|
{
|
|
|
|
char num[128];
|
|
|
|
int i, j;
|
|
|
|
byte b[4];
|
|
|
|
byte m[4];
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
if (!s || !f)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
|
|
|
b[i] = 0;
|
|
|
|
m[i] = 0;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
if ((*s < '0') || (*s > '9'))
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Bad filter address: %s\n", s);
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
j = 0;
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
while (*s >= '0' && *s <= '9')
|
|
|
|
{
|
|
|
|
num[j++] = *s++;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
num[j] = 0;
|
|
|
|
b[i] = atoi(num);
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
if (b[i] != 0)
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
m[i] = 255;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
if (!*s)
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
break;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
s++;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2019-09-23 21:45:50 +00:00
|
|
|
/* PVS NOTE: maybe use memcpy here? */
|
2009-03-12 20:03:41 +00:00
|
|
|
f->mask = *(unsigned *)m;
|
|
|
|
f->compare = *(unsigned *)b;
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
qboolean
|
|
|
|
SV_FilterPacket(char *from)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
int i;
|
|
|
|
unsigned in;
|
2009-03-12 20:03:41 +00:00
|
|
|
byte m[4];
|
|
|
|
char *p;
|
2014-02-22 12:20:22 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
if (!from)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-22 12:20:22 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
i = 0;
|
|
|
|
p = from;
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
while (*p && i < 4)
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
m[i] = 0;
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
while (*p >= '0' && *p <= '9')
|
|
|
|
{
|
|
|
|
m[i] = m[i] * 10 + (*p - '0');
|
2009-03-12 20:03:41 +00:00
|
|
|
p++;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
if (!*p || (*p == ':'))
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
break;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
i++, p++;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2019-09-23 21:45:50 +00:00
|
|
|
/* PVS NOTE: maybe use memcpy here? */
|
2009-03-12 20:03:41 +00:00
|
|
|
in = *(unsigned *)m;
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
for (i = 0; i < numipfilters; i++)
|
|
|
|
{
|
|
|
|
if ((in & ipfilters[i].mask) == ipfilters[i].compare)
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
return (int)filterban->value;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
return (int)!filterban->value;
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
void
|
|
|
|
SVCmd_AddIP_f(void)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (gi.argc() < 3)
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Usage: addip <ip-mask>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
for (i = 0; i < numipfilters; i++)
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
if (ipfilters[i].compare == 0xffffffff)
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
|
|
|
break; /* free spot */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
if (i == numipfilters)
|
|
|
|
{
|
|
|
|
if (numipfilters == MAX_IPFILTERS)
|
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "IP filter list is full\n");
|
2009-03-12 20:03:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
numipfilters++;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
if (!StringToFilter(gi.argv(2), &ipfilters[i]))
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
ipfilters[i].compare = 0xffffffff;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
void
|
|
|
|
SVCmd_RemoveIP_f(void)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
ipfilter_t f;
|
|
|
|
int i, j;
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
if (gi.argc() < 3)
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Usage: sv removeip <ip-mask>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
if (!StringToFilter(gi.argv(2), &f))
|
|
|
|
{
|
2009-03-12 20:03:41 +00:00
|
|
|
return;
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
for (i = 0; i < numipfilters; i++)
|
|
|
|
{
|
|
|
|
if ((ipfilters[i].mask == f.mask) &&
|
|
|
|
(ipfilters[i].compare == f.compare))
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
for (j = i + 1; j < numipfilters; j++)
|
|
|
|
{
|
|
|
|
ipfilters[j - 1] = ipfilters[j];
|
|
|
|
}
|
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
numipfilters--;
|
2014-01-28 16:28:57 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Removed.\n");
|
2009-03-12 20:03:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Didn't find %s.\n", gi.argv(2));
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
void
|
|
|
|
SVCmd_ListIP_f(void)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
int i;
|
|
|
|
byte b[4];
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Filter list:\n");
|
|
|
|
|
|
|
|
for (i = 0; i < numipfilters; i++)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2019-09-23 21:45:50 +00:00
|
|
|
/* PVS NOTE: maybe use memcpy here? */
|
2009-03-12 20:03:41 +00:00
|
|
|
*(unsigned *)b = ipfilters[i].compare;
|
2014-01-28 16:28:57 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "%3i.%3i.%3i.%3i\n", b[0], b[1], b[2], b[3]);
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
void
|
|
|
|
SVCmd_WriteIP_f(void)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
FILE *f;
|
|
|
|
char name[MAX_OSPATH];
|
|
|
|
byte b[4];
|
|
|
|
int i;
|
|
|
|
cvar_t *game;
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
game = gi.cvar("game", "", 0);
|
|
|
|
|
|
|
|
if (!*game->string)
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
|
|
|
sprintf(name, "%s/listip.cfg", GAMEVERSION);
|
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
else
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
|
|
|
sprintf(name, "%s/listip.cfg", game->string);
|
|
|
|
}
|
|
|
|
|
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Writing %s.\n", name);
|
2009-03-12 20:03:41 +00:00
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
f = fopen(name, "wb");
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
if (!f)
|
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Couldn't open %s\n", name);
|
2009-03-12 20:03:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
2009-03-12 20:03:41 +00:00
|
|
|
fprintf(f, "set filterban %d\n", (int)filterban->value);
|
|
|
|
|
2014-01-28 16:28:57 +00:00
|
|
|
for (i = 0; i < numipfilters; i++)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2019-09-23 21:45:50 +00:00
|
|
|
/* PVS NOTE: maybe use memcpy here? */
|
2009-03-12 20:03:41 +00:00
|
|
|
*(unsigned *)b = ipfilters[i].compare;
|
2014-01-28 16:28:57 +00:00
|
|
|
fprintf(f, "sv addip %i.%i.%i.%i\n", b[0], b[1], b[2], b[3]);
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
fclose(f);
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-01-28 16:28:57 +00:00
|
|
|
* ServerCommand will be called when an "sv" command is issued.
|
|
|
|
* The game can issue gi.argc() / gi.argv() commands to get the
|
|
|
|
* rest of the parameters
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ServerCommand(void)
|
2009-03-12 20:03:41 +00:00
|
|
|
{
|
2014-01-28 16:28:57 +00:00
|
|
|
char *cmd;
|
2009-03-12 20:03:41 +00:00
|
|
|
|
|
|
|
cmd = gi.argv(1);
|
2014-01-28 16:28:57 +00:00
|
|
|
|
|
|
|
if (Q_stricmp(cmd, "test") == 0)
|
|
|
|
{
|
|
|
|
Svcmd_Test_f();
|
|
|
|
}
|
|
|
|
else if (Q_stricmp(cmd, "addip") == 0)
|
|
|
|
{
|
|
|
|
SVCmd_AddIP_f();
|
|
|
|
}
|
|
|
|
else if (Q_stricmp(cmd, "removeip") == 0)
|
|
|
|
{
|
|
|
|
SVCmd_RemoveIP_f();
|
|
|
|
}
|
|
|
|
else if (Q_stricmp(cmd, "listip") == 0)
|
|
|
|
{
|
|
|
|
SVCmd_ListIP_f();
|
|
|
|
}
|
|
|
|
else if (Q_stricmp(cmd, "writeip") == 0)
|
|
|
|
{
|
|
|
|
SVCmd_WriteIP_f();
|
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
else
|
2014-01-28 16:28:57 +00:00
|
|
|
{
|
|
|
|
gi.cprintf(NULL, PRINT_HIGH, "Unknown server command \"%s\"\n", cmd);
|
|
|
|
}
|
2009-03-12 20:03:41 +00:00
|
|
|
}
|