quakeforge/hw/source/master.c

560 lines
11 KiB
C
Raw Normal View History

2003-02-10 21:06:30 +00:00
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2003-02-12 17:15:03 +00:00
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
2003-02-10 21:06:30 +00:00
#include "QF/cbuf.h"
#include "QF/cmd.h"
#include "QF/console.h"
#include "QF/cvar.h"
#include "QF/idparse.h"
#include "QF/msg.h"
#include "QF/plugin.h"
#include "QF/qargs.h"
2003-02-12 17:15:03 +00:00
#include "QF/qendian.h"
2003-02-10 21:06:30 +00:00
#include "QF/sizebuf.h"
#include "QF/sys.h"
2003-02-12 17:15:03 +00:00
2003-02-12 17:46:48 +00:00
#include "compat.h"
#include "netchan.h"
2003-02-10 21:06:30 +00:00
2003-02-12 17:46:48 +00:00
#define SV_TIMEOUT 450
2003-02-12 17:15:03 +00:00
#define PORT_MASTER 26900
#define PORT_SERVER 26950
2003-02-12 17:15:03 +00:00
// M = master, S = server, C = client, A = any
// the second character will allways be \n if the message isn't a single
// byte long (?? not true anymore?)
2003-02-12 17:15:03 +00:00
#define S2C_CHALLENGE 'c'
#define A2A_PING 'k'
2003-02-10 21:06:30 +00:00
2003-02-12 17:15:03 +00:00
#define S2M_HEARTBEAT 'a' // + serverinfo + userlist + fraglist
#define S2M_SHUTDOWN 'C'
2003-02-10 21:06:30 +00:00
typedef struct filter_s {
netadr_t from;
netadr_t to;
struct filter_s *next;
struct filter_s *previous;
} filter_t;
2003-02-12 17:15:03 +00:00
typedef struct server_s {
netadr_t ip;
struct server_s *next;
struct server_s *previous;
double timeout;
} server_t;
2003-02-12 18:36:14 +00:00
static cvar_t *sv_console_plugin;
2003-02-12 17:15:03 +00:00
SERVER_PLUGIN_PROTOS
static plugin_list_t server_plugin_list[] = {
SERVER_PLUGIN_LIST
};
qboolean is_server = true;
static cbuf_t *mst_cbuf;
2003-02-12 18:36:14 +00:00
static server_t *sv_list = NULL;
static filter_t *filter_list = NULL;
2003-02-10 21:06:30 +00:00
static void
FL_Remove (filter_t * filter)
{
if (filter->previous)
filter->previous->next = filter->next;
if (filter->next)
filter->next->previous = filter->previous;
filter->next = NULL;
filter->previous = NULL;
if (filter_list == filter)
filter_list = NULL;
}
static void
FL_Clear (void)
{
filter_t *filter;
for (filter = filter_list; filter;) {
if (filter) {
filter_t *next = filter->next;
FL_Remove (filter);
free (filter);
filter = next;
}
}
filter_list = NULL;
}
static filter_t *
FL_New (netadr_t *adr1, netadr_t *adr2)
{
filter_t *filter;
filter = (filter_t *) calloc (1, sizeof (filter_t));
if (adr1)
2003-02-12 17:15:03 +00:00
filter->from = *adr1;
2003-02-10 21:06:30 +00:00
if (adr2)
2003-02-12 17:15:03 +00:00
filter->to = *adr2;
2003-02-10 21:06:30 +00:00
return filter;
}
static void
FL_Add (filter_t * filter)
{
filter->next = filter_list;
filter->previous = NULL;
if (filter_list)
filter_list->previous = filter;
filter_list = filter;
}
static filter_t *
FL_Find (netadr_t adr)
{
filter_t *filter;
for (filter = filter_list; filter; filter = filter->next) {
if (NET_CompareBaseAdr (filter->from, adr))
return filter;
}
return NULL;
}
2003-02-12 17:15:03 +00:00
static void
2003-02-12 18:36:14 +00:00
Filter (void)
2003-02-12 17:15:03 +00:00
{
int hold_port;
netadr_t filter_adr;
filter_t *filter;
hold_port = net_from.port;
NET_StringToAdr ("127.0.0.1:26950", &filter_adr);
if (NET_CompareBaseAdr (net_from, filter_adr)) {
NET_StringToAdr ("0.0.0.0:26950", &filter_adr);
if (!NET_CompareBaseAdr (net_local_adr, filter_adr)) {
net_from = net_local_adr;
net_from.port = hold_port;
}
return;
}
// if no compare with filter list
if ((filter = FL_Find (net_from))) {
net_from = filter->to;
net_from.port = hold_port;
}
}
static void
SVL_Remove (server_t *sv)
{
if (sv_list == sv)
sv_list = sv->next;
if (sv->previous)
sv->previous->next = sv->next;
if (sv->next)
sv->next->previous = sv->previous;
sv->next = NULL;
sv->previous = NULL;
}
static void
2003-02-10 21:06:30 +00:00
SVL_Clear (void)
{
server_t *sv;
for (sv = sv_list; sv;) {
if (sv) {
server_t *next = sv->next;
SVL_Remove (sv);
free (sv);
sv = next;
}
2003-02-12 17:15:03 +00:00
}
sv_list = NULL;
2003-02-10 21:06:30 +00:00
}
2003-02-12 17:15:03 +00:00
static server_t *
2003-02-10 21:06:30 +00:00
SVL_New (netadr_t *adr)
{
server_t *sv;
2003-02-10 21:06:30 +00:00
sv = (server_t *) calloc (1, sizeof (server_t));
if (adr)
2003-02-12 17:15:03 +00:00
sv->ip = *adr;
2003-02-10 21:06:30 +00:00
return sv;
}
2003-02-12 17:15:03 +00:00
static void
2003-02-10 21:06:30 +00:00
SVL_Add (server_t *sv)
{
sv->next = sv_list;
sv->previous = NULL;
if (sv_list)
sv_list->previous = sv;
sv_list = sv;
}
2003-02-12 17:15:03 +00:00
static server_t *
2003-02-10 21:06:30 +00:00
SVL_Find (netadr_t adr)
{
server_t *sv;
for (sv = sv_list; sv; sv = sv->next) {
if (NET_CompareAdr (sv->ip, adr))
return sv;
}
return NULL;
}
static void
SV_InitNet (void)
{
2003-02-12 17:15:03 +00:00
const char *str;
int port, p;
2003-02-12 17:15:03 +00:00
QFile *filters;
2003-02-10 21:06:30 +00:00
port = PORT_MASTER;
p = COM_CheckParm ("-port");
if (p && p < com_argc) {
port = atoi (com_argv[p + 1]);
Sys_Printf ("Port: %i\n", port);
2003-02-10 21:06:30 +00:00
}
NET_Init (port);
// Add filters
2003-02-12 17:15:03 +00:00
if ((filters = Qopen ("filters.ini", "rt"))) {
while ((str = Qgetline (filters))) {
2003-02-10 21:06:30 +00:00
Cbuf_AddText (mst_cbuf, "filter add ");
Cbuf_AddText (mst_cbuf, str);
Cbuf_AddText (mst_cbuf, "\n");
}
2003-02-12 17:15:03 +00:00
Qclose (filters);
2003-02-10 21:06:30 +00:00
}
}
static void
AnalysePacket (void)
{
2003-02-12 17:46:48 +00:00
byte buf[16];
byte *p, *data;
int i, size, rsize;
Sys_Printf ("%s >> unknown packet:\n", NET_AdrToString (net_from));
2003-02-12 17:46:48 +00:00
data = net_message->message->data;
size = net_message->message->cursize;
for (p = data; (rsize = min (size - (p - data), 16)); p += rsize) {
Sys_Printf ("%04X:", (unsigned) (p - data));
2003-02-12 17:46:48 +00:00
memcpy (buf, p, rsize);
for (i = 0; i < rsize; i++) {
Sys_Printf (" %02X", buf[i]);
2003-02-12 17:46:48 +00:00
if (buf[i] < ' ' || buf [i] > '~')
buf[i] = '.';
}
Sys_Printf ("%*.*s\n", 1 + (16 - rsize) * 3 + rsize, rsize, buf);
2003-02-10 21:06:30 +00:00
}
}
static void
Mst_SendList (void)
{
byte buf[MAX_DATAGRAM];
sizebuf_t msg;
server_t *sv;
short int sv_num = 0;
2003-02-10 21:06:30 +00:00
msg.data = buf;
msg.maxsize = sizeof (buf);
msg.cursize = 0;
msg.allowoverflow = true;
msg.overflowed = false;
// number of servers:
for (sv = sv_list; sv; sv = sv->next)
sv_num++;
MSG_WriteByte (&msg, 255);
MSG_WriteByte (&msg, 255);
MSG_WriteByte (&msg, 255);
MSG_WriteByte (&msg, 255);
MSG_WriteByte (&msg, 255);
MSG_WriteByte (&msg, 'd');
MSG_WriteByte (&msg, '\n');
if (sv_num > 0)
for (sv = sv_list; sv; sv = sv->next) {
MSG_WriteByte (&msg, sv->ip.ip[0]);
MSG_WriteByte (&msg, sv->ip.ip[1]);
MSG_WriteByte (&msg, sv->ip.ip[2]);
MSG_WriteByte (&msg, sv->ip.ip[3]);
MSG_WriteShort (&msg, sv->ip.port);
}
NET_SendPacket (msg.cursize, msg.data, net_from);
}
static void
Mst_Packet (void)
{
char msg;
server_t *sv;
2003-02-13 00:06:24 +00:00
// Filter ();
2003-02-10 21:06:30 +00:00
msg = net_message->message->data[1];
if (msg == A2A_PING) {
2003-02-12 18:36:14 +00:00
Filter ();
Sys_Printf ("%s >> A2A_PING\n", NET_AdrToString (net_from));
2003-02-10 21:06:30 +00:00
if (!(sv = SVL_Find (net_from))) {
sv = SVL_New (&net_from);
SVL_Add (sv);
}
sv->timeout = Sys_DoubleTime ();
} else if (msg == S2M_HEARTBEAT) {
2003-02-12 18:36:14 +00:00
Filter ();
Sys_Printf ("%s >> S2M_HEARTBEAT\n", NET_AdrToString (net_from));
2003-02-10 21:06:30 +00:00
if (!(sv = SVL_Find (net_from))) {
sv = SVL_New (&net_from);
SVL_Add (sv);
}
sv->timeout = Sys_DoubleTime ();
} else if (msg == S2M_SHUTDOWN) {
2003-02-12 18:36:14 +00:00
Filter ();
Sys_Printf ("%s >> S2M_SHUTDOWN\n", NET_AdrToString (net_from));
2003-02-10 21:06:30 +00:00
if ((sv = SVL_Find (net_from))) {
SVL_Remove (sv);
free (sv);
}
2003-02-12 17:15:03 +00:00
} else if (msg == S2C_CHALLENGE) {
Sys_Printf ("%s >> ", NET_AdrToString (net_from));
Sys_Printf ("Gamespy server list request\n");
2003-02-10 21:06:30 +00:00
Mst_SendList ();
} else {
2003-02-10 21:06:30 +00:00
byte *p;
p = net_message->message->data;
if (p[0] == 0 && p[1] == 'y') {
Sys_Printf ("%s >> ", NET_AdrToString (net_from));
Sys_Printf ("Pingtool server list request\n");
2003-02-10 21:06:30 +00:00
Mst_SendList ();
} else {
2003-02-10 21:06:30 +00:00
AnalysePacket ();
}
}
}
2003-02-12 17:15:03 +00:00
static void
2003-02-10 21:06:30 +00:00
SV_ReadPackets (void)
{
while (NET_GetPacket ()) {
Mst_Packet ();
}
}
static void
2003-02-12 18:36:14 +00:00
FilterAdd (int arg)
2003-02-10 21:06:30 +00:00
{
filter_t *filter;
netadr_t to, from;
2003-02-12 17:15:03 +00:00
if (Cmd_Argc () - arg != 2) {
Sys_Printf ("Invalid command parameters. "
"Usage:\nfilter add x.x.x.x:port x.x.x.x:port\n\n");
2003-02-10 21:06:30 +00:00
return;
}
2003-02-12 17:15:03 +00:00
NET_StringToAdr (Cmd_Argv (arg), &from);
NET_StringToAdr (Cmd_Argv (arg + 1), &to);
2003-02-10 21:06:30 +00:00
if (to.port == 0)
from.port = BigShort (PORT_SERVER);
if (from.port == 0)
from.port = BigShort (PORT_SERVER);
if (!(filter = FL_Find (from))) {
Sys_Printf ("Added filter %s\t\t%s\n", Cmd_Argv (arg),
2003-02-12 17:15:03 +00:00
Cmd_Argv (arg + 1));
2003-02-10 21:06:30 +00:00
filter = FL_New (&from, &to);
FL_Add (filter);
} else
Sys_Printf ("%s already defined\n\n", Cmd_Argv (arg));
2003-02-10 21:06:30 +00:00
}
static void
2003-02-12 18:36:14 +00:00
FilterRemove (int arg)
2003-02-10 21:06:30 +00:00
{
filter_t *filter;
netadr_t from;
2003-02-12 17:15:03 +00:00
if (Cmd_Argc () - arg != 1) {
Sys_Printf ("Invalid command parameters. Usage:\n"
"filter remove x.x.x.x:port\n\n");
2003-02-10 21:06:30 +00:00
return;
}
2003-02-12 17:15:03 +00:00
NET_StringToAdr (Cmd_Argv (arg), &from);
2003-02-10 21:06:30 +00:00
if ((filter = FL_Find (from))) {
Sys_Printf ("Removed %s\n\n", Cmd_Argv (arg));
2003-02-10 21:06:30 +00:00
FL_Remove (filter);
free (filter);
} else
Sys_Printf ("Cannot find %s\n\n", Cmd_Argv (arg));
2003-02-10 21:06:30 +00:00
}
static void
2003-02-12 18:36:14 +00:00
FilterList (void)
2003-02-10 21:06:30 +00:00
{
filter_t *filter;
for (filter = filter_list; filter; filter = filter->next) {
Sys_Printf ("%s", NET_AdrToString (filter->from));
Sys_Printf ("\t\t%s\n", NET_AdrToString (filter->to));
2003-02-10 21:06:30 +00:00
}
if (filter_list == NULL)
Sys_Printf ("No filter\n");
Sys_Printf ("\n");
2003-02-10 21:06:30 +00:00
}
static void
2003-02-12 18:36:14 +00:00
FilterClear (void)
2003-02-10 21:06:30 +00:00
{
Sys_Printf ("Removed all filters\n\n");
2003-02-10 21:06:30 +00:00
FL_Clear ();
}
2003-02-12 17:15:03 +00:00
static void
2003-02-12 18:36:14 +00:00
Filter_f (void)
2003-02-10 21:06:30 +00:00
{
if (!strcmp (Cmd_Argv (1), "add"))
2003-02-12 18:36:14 +00:00
FilterAdd (2);
2003-02-10 21:06:30 +00:00
else if (!strcmp (Cmd_Argv (1), "remove"))
2003-02-12 18:36:14 +00:00
FilterRemove (2);
2003-02-10 21:06:30 +00:00
else if (!strcmp (Cmd_Argv (1), "clear"))
2003-02-12 18:36:14 +00:00
FilterClear ();
2003-02-10 21:06:30 +00:00
else if (Cmd_Argc () == 3) {
2003-02-12 18:36:14 +00:00
FilterAdd (1);
} else if (Cmd_Argc () == 2) {
2003-02-12 18:36:14 +00:00
FilterRemove (1);
} else
2003-02-12 18:36:14 +00:00
FilterList ();
2003-02-10 21:06:30 +00:00
}
static void
SV_WriteFilterList (void)
{
2003-02-12 17:15:03 +00:00
QFile *filters;
2003-02-10 21:06:30 +00:00
2003-02-12 17:15:03 +00:00
if ((filters = Qopen ("filters.ini", "wt"))) {
filter_t *filter;
2003-02-10 21:06:30 +00:00
if (filter_list == NULL) {
2003-02-12 17:15:03 +00:00
Qclose (filters);
2003-02-10 21:06:30 +00:00
return;
}
for (filter = filter_list; filter; filter = filter->next) {
2003-02-12 17:15:03 +00:00
Qprintf (filters, "%s", NET_AdrToString (filter->from));
Qprintf (filters, " %s\n", NET_AdrToString (filter->to));
2003-02-10 21:06:30 +00:00
}
2003-02-12 17:15:03 +00:00
Qclose (filters);
2003-02-10 21:06:30 +00:00
}
}
2003-02-12 17:15:03 +00:00
static void
2003-02-10 21:06:30 +00:00
SV_Shutdown (void)
{
NET_Shutdown ();
// write filter list
SV_WriteFilterList ();
Con_Shutdown ();
2003-02-10 21:06:30 +00:00
}
static void
SV_TimeOut (void)
{
// Remove listed severs that havnt sent a heartbeat for some time
double time = Sys_DoubleTime ();
server_t *sv;
server_t *next;
if (sv_list == NULL)
return;
2003-02-10 21:06:30 +00:00
for (sv = sv_list; sv;) {
if (sv->timeout + SV_TIMEOUT < time) {
next = sv->next;
Sys_Printf ("%s timed out\n", NET_AdrToString (sv->ip));
2003-02-10 21:06:30 +00:00
SVL_Remove (sv);
free (sv);
sv = next;
} else
2003-02-10 21:06:30 +00:00
sv = sv->next;
}
}
2003-02-12 17:15:03 +00:00
static void
SV_Frame (void)
2003-02-10 21:06:30 +00:00
{
Sys_CheckInput (1, net_socket);
Con_ProcessInput ();
2003-02-10 21:06:30 +00:00
Cbuf_Execute_Stack (mst_cbuf);
SV_TimeOut ();
SV_ReadPackets ();
}
static void
MST_Quit_f (void)
{
Sys_Printf ("HW master shutdown\n");
Sys_Quit ();
}
2003-02-10 21:06:30 +00:00
int
main (int argc, const char **argv)
{
COM_InitArgv (argc, argv);
mst_cbuf = Cbuf_New (&id_interp);
Sys_RegisterShutdown (SV_Shutdown);
Cvar_Init_Hash ();
Cmd_Init_Hash ();
Cvar_Init ();
Sys_Init_Cvars ();
Sys_Init ();
Cmd_Init ();
Cmd_AddCommand ("quit", MST_Quit_f, "Shut down the master server");
2003-02-12 17:15:03 +00:00
Cmd_AddCommand ("clear", SVL_Clear, "Clear the server list");
2003-02-12 18:36:14 +00:00
Cmd_AddCommand ("filter", Filter_f, "Manipulate filtering");
Cmd_StuffCmds (mst_cbuf);
Cbuf_Execute_Sets (mst_cbuf);
2003-02-10 21:06:30 +00:00
PI_Init ();
sv_console_plugin = Cvar_Get ("sv_console_plugin", "server",
CVAR_ROM, 0, "Plugin used for the console");
PI_RegisterPlugins (server_plugin_list);
Con_Init (sv_console_plugin->string);
if (con_module)
con_module->data->console->cbuf = mst_cbuf;
con_list_print = Sys_Printf;
SV_InitNet ();
Sys_Printf ("Exe: " __TIME__ " " __DATE__ "\n");
Sys_Printf ("======== HW master initialized ========\n\n");
2003-02-10 21:06:30 +00:00
while (1) {
SV_Frame ();
}
return 0;
}