2004-02-20 02:39:34 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2004 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/console.h"
|
2004-02-21 02:31:22 +00:00
|
|
|
#include "QF/dstring.h"
|
2004-02-20 02:39:34 +00:00
|
|
|
#include "QF/hash.h"
|
2004-02-27 00:10:46 +00:00
|
|
|
#include "QF/idparse.h"
|
2004-02-21 02:31:22 +00:00
|
|
|
#include "QF/info.h"
|
|
|
|
#include "QF/msg.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/va.h"
|
2004-02-20 02:39:34 +00:00
|
|
|
|
2004-02-21 02:31:22 +00:00
|
|
|
#include "qw/protocol.h"
|
|
|
|
|
|
|
|
#include "connection.h"
|
2004-02-27 00:10:46 +00:00
|
|
|
#include "qtv.h"
|
2004-02-20 02:39:34 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
2005-05-02 04:09:15 +00:00
|
|
|
static hashtab_t *server_hash;
|
|
|
|
static server_t *servers;
|
2004-02-20 02:39:34 +00:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
server_get_key (void *sv, void *unused)
|
|
|
|
{
|
|
|
|
return ((server_t *) sv)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_free (void *_sv, void *unused)
|
|
|
|
{
|
2004-02-21 02:31:22 +00:00
|
|
|
server_t *sv = (server_t *) _sv;
|
2005-05-02 04:09:15 +00:00
|
|
|
server_t **s;
|
|
|
|
|
|
|
|
static byte final[] = {qtv_stringcmd, 'd', 'r', 'o', 'p', 0};
|
2004-02-21 02:31:22 +00:00
|
|
|
|
2004-02-27 00:10:46 +00:00
|
|
|
if (sv->connected)
|
|
|
|
Netchan_Transmit (&sv->netchan, sizeof (final), final);
|
2004-02-21 02:31:22 +00:00
|
|
|
Connection_Del (sv->con);
|
2005-05-02 04:09:15 +00:00
|
|
|
|
|
|
|
for (s = &servers; *s; s = &(*s)->next) {
|
|
|
|
if (*s == sv) {
|
|
|
|
*s = sv->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
server_compare (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp ((*(server_t **) a)->name, (*(server_t **) b)->name);
|
|
|
|
}
|
|
|
|
|
2005-05-02 04:09:15 +00:00
|
|
|
static void
|
|
|
|
qtv_server_data (server_t *sv)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
sv->ver = MSG_ReadLong (net_message);
|
|
|
|
sv->spawncount = MSG_ReadLong (net_message);
|
|
|
|
sv->gamedir = strdup (MSG_ReadString (net_message));
|
|
|
|
|
|
|
|
sv->message = strdup (MSG_ReadString (net_message));
|
|
|
|
sv->movevars.gravity = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.stopspeed = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.maxspeed = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.spectatormaxspeed = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.accelerate = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.airaccelerate = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.wateraccelerate = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.friction = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.waterfriction = MSG_ReadFloat (net_message);
|
|
|
|
sv->movevars.entgravity = MSG_ReadFloat (net_message);
|
|
|
|
|
|
|
|
sv->cdtrack = MSG_ReadByte (net_message);
|
|
|
|
sv->sounds = MSG_ReadByte (net_message);
|
|
|
|
|
|
|
|
COM_TokenizeString (MSG_ReadString (net_message), qtv_args);
|
|
|
|
cmd_args = qtv_args;
|
|
|
|
sv->info = Info_ParseString (Cmd_Argv (1), MAX_SERVERINFO_STRING, 0);
|
|
|
|
|
|
|
|
str = Info_ValueForKey (sv->info, "hostname");
|
|
|
|
if (strcmp (str, "unnamed"))
|
|
|
|
qtv_printf ("%s: %s\n", sv->name, str);
|
|
|
|
str = Info_ValueForKey (sv->info, "*version");
|
|
|
|
qtv_printf ("%s: QW %s\n", sv->name, str);
|
|
|
|
str = Info_ValueForKey (sv->info, "*qf_version");
|
|
|
|
if (str[0])
|
|
|
|
qtv_printf ("%s: QuakeForge %s\n", sv->name, str);
|
|
|
|
qtv_printf ("%s: gamedir: %s\n", sv->name, sv->gamedir);
|
|
|
|
str = Info_ValueForKey (sv->info, "map");
|
|
|
|
qtv_printf ("%s: (%s) %s\n", sv->name, str, sv->message);
|
|
|
|
}
|
|
|
|
|
2004-02-21 02:31:22 +00:00
|
|
|
static void
|
|
|
|
server_handler (connection_t *con, void *object)
|
|
|
|
{
|
|
|
|
server_t *sv = (server_t *) object;
|
|
|
|
|
2004-02-27 00:10:46 +00:00
|
|
|
if (*(int *) net_message->message->data == -1)
|
|
|
|
return;
|
|
|
|
if (!Netchan_Process (&sv->netchan))
|
|
|
|
return;
|
2004-03-05 23:42:15 +00:00
|
|
|
while (1) {
|
|
|
|
int cmd;
|
|
|
|
if (net_message->badread) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cmd = MSG_ReadByte (net_message);
|
|
|
|
if (cmd == -1) {
|
|
|
|
net_message->readcount++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (cmd) {
|
|
|
|
default:
|
|
|
|
qtv_printf ("Illegible server message: %d\n", cmd);
|
|
|
|
goto bail;
|
2005-05-02 04:09:15 +00:00
|
|
|
case qtv_disconnect:
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("%s: disconnected\n", sv->name);
|
|
|
|
break;
|
2005-05-02 04:09:15 +00:00
|
|
|
case qtv_serverdata:
|
|
|
|
qtv_server_data (sv);
|
|
|
|
break;
|
2004-03-05 23:42:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bail:
|
2005-05-02 04:09:15 +00:00
|
|
|
return;
|
2004-02-21 02:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *
|
|
|
|
expect_packet (qmsg_t *msg, int type)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
int seq;
|
|
|
|
|
|
|
|
MSG_BeginReading (net_message);
|
|
|
|
seq = MSG_ReadLong (net_message);
|
|
|
|
if (seq != -1) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("unexpected connected packet\n");
|
2004-02-21 02:31:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
str = MSG_ReadString (net_message);
|
2004-02-27 00:10:46 +00:00
|
|
|
if (str[0] == A2C_PRINT) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("%s", str + 1);
|
2004-02-27 00:10:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-02-21 02:31:22 +00:00
|
|
|
if (str[0] != type) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("unexpected connectionless packet type: %s\n", str);
|
2004-02-21 02:31:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_connect (connection_t *con, void *object)
|
|
|
|
{
|
|
|
|
server_t *sv = (server_t *) object;
|
|
|
|
sizebuf_t *msg = &sv->netchan.message;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
if (!(str = expect_packet (net_message, S2C_CONNECTION)))
|
|
|
|
return;
|
|
|
|
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("connection from %s\n", sv->name);
|
2004-02-21 02:31:22 +00:00
|
|
|
Netchan_Setup (&sv->netchan, con->address, sv->qport, NC_SEND_QPORT);
|
|
|
|
sv->netchan.outgoing_sequence = 1;
|
2004-02-27 00:10:46 +00:00
|
|
|
sv->connected = 1;
|
2005-05-02 04:09:15 +00:00
|
|
|
MSG_WriteByte (msg, qtv_stringcmd);
|
2004-02-21 02:31:22 +00:00
|
|
|
MSG_WriteString (msg, "new");
|
|
|
|
Netchan_Transmit (&sv->netchan, 0, 0);
|
|
|
|
con->handler = server_handler;
|
2005-05-02 04:09:15 +00:00
|
|
|
|
|
|
|
sv->next_run = realtime;
|
2004-02-21 02:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_challenge (connection_t *con, void *object)
|
|
|
|
{
|
|
|
|
server_t *sv = (server_t *) object;
|
2004-02-27 00:10:46 +00:00
|
|
|
const char *str, *qtv = 0;
|
|
|
|
int challenge, i;
|
2004-02-21 02:31:22 +00:00
|
|
|
dstring_t *data;
|
|
|
|
|
|
|
|
if (!(str = expect_packet (net_message, S2C_CHALLENGE)))
|
|
|
|
return;
|
|
|
|
|
2004-02-27 00:10:46 +00:00
|
|
|
COM_TokenizeString (str + 1, qtv_args);
|
|
|
|
cmd_args = qtv_args;
|
|
|
|
challenge = atoi (Cmd_Argv (0));
|
|
|
|
|
|
|
|
for (i = 1; i < Cmd_Argc (); i++) {
|
|
|
|
str = Cmd_Argv (i);
|
|
|
|
if (!strcmp ("QF", str)) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("QuakeForge server detected\n");
|
2004-02-27 00:10:46 +00:00
|
|
|
} else if (!strcmp ("qtv", str)) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("QTV capable server\n");
|
2004-02-27 00:10:46 +00:00
|
|
|
qtv = str;
|
|
|
|
} else {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("%s\n", str);
|
2004-02-27 00:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!qtv) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("%s can't handle qtv.\n", sv->name);
|
2005-05-02 04:09:15 +00:00
|
|
|
Hash_Del (server_hash, sv->name);
|
|
|
|
Hash_Free (server_hash, sv);
|
2004-02-27 00:10:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-21 02:31:22 +00:00
|
|
|
|
|
|
|
data = dstring_new ();
|
2004-02-27 00:10:46 +00:00
|
|
|
dsprintf (data, "%c%c%c%cconnect %s %i %i \"%s\"\n", 255, 255, 255, 255,
|
|
|
|
qtv, sv->qport, challenge,
|
2004-02-21 02:31:22 +00:00
|
|
|
Info_MakeString (sv->info, 0));
|
|
|
|
Netchan_SendPacket (strlen (data->str), data->str, net_from);
|
|
|
|
dstring_delete (data);
|
|
|
|
con->handler = server_connect;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_getchallenge (connection_t *con, server_t *sv)
|
|
|
|
{
|
|
|
|
static const char *getchallenge = "\377\377\377\377getchallenge\n";
|
|
|
|
|
|
|
|
Netchan_SendPacket (strlen (getchallenge), (void *) getchallenge, sv->adr);
|
|
|
|
}
|
|
|
|
|
2004-02-20 02:39:34 +00:00
|
|
|
static void
|
|
|
|
sv_new_f (void)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
const char *address;
|
|
|
|
server_t *sv;
|
2004-02-21 02:31:22 +00:00
|
|
|
netadr_t adr;
|
2004-02-20 02:39:34 +00:00
|
|
|
|
|
|
|
if (Cmd_Argc () != 3) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("Usage: sv_new <name> <address>\n");
|
2004-02-20 02:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
name = Cmd_Argv (1);
|
2005-05-02 04:09:15 +00:00
|
|
|
if (Hash_Find (server_hash, name)) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("sv_new: %s already exists\n", name);
|
2004-02-20 02:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
address = Cmd_Argv (2);
|
2004-02-21 02:31:22 +00:00
|
|
|
if (!NET_StringToAdr (address, &adr)) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("Bad server address\n");
|
2004-02-21 02:31:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!adr.port)
|
|
|
|
adr.port = BigShort (27500);
|
|
|
|
|
2004-02-20 02:39:34 +00:00
|
|
|
sv = calloc (1, sizeof (server_t));
|
2005-05-02 04:09:15 +00:00
|
|
|
sv->next = servers;
|
|
|
|
servers = sv;
|
2004-02-20 02:39:34 +00:00
|
|
|
sv->name = strdup (name);
|
|
|
|
sv->address = strdup (address);
|
2004-02-21 02:31:22 +00:00
|
|
|
sv->adr = adr;
|
|
|
|
sv->qport = qport->int_val;
|
|
|
|
sv->info = Info_ParseString ("", MAX_INFO_STRING, 0);
|
|
|
|
Info_SetValueForStarKey (sv->info, "*ver",
|
|
|
|
va ("%s QTV %s", QW_VERSION, VERSION), 0);
|
|
|
|
Info_SetValueForStarKey (sv->info, "*qsg_version", QW_QSG_VERSION, 0);
|
|
|
|
Info_SetValueForKey (sv->info, "name", "QTV Proxy", 0);
|
2005-05-02 04:09:15 +00:00
|
|
|
Hash_Add (server_hash, sv);
|
2004-02-21 02:31:22 +00:00
|
|
|
|
|
|
|
sv->con = Connection_Add (&adr, sv, server_challenge);
|
|
|
|
|
|
|
|
server_getchallenge (sv->con, sv);
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sv_del_f (void)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
server_t *sv;
|
|
|
|
|
|
|
|
if (Cmd_Argc () != 2) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("Usage: sv_del <name>\n");
|
2004-02-20 02:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
name = Cmd_Argv (1);
|
2005-05-02 04:09:15 +00:00
|
|
|
if (!(sv = Hash_Del (server_hash, name))) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("sv_new: %s unkown\n", name);
|
2004-02-20 02:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-02 04:09:15 +00:00
|
|
|
Hash_Free (server_hash, sv);
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sv_list_f (void)
|
|
|
|
{
|
|
|
|
server_t **list, **l, *sv;
|
|
|
|
int count;
|
|
|
|
|
2005-05-02 04:09:15 +00:00
|
|
|
for (l = &servers, count = 0; *l; l = &(*l)->next)
|
2004-02-20 02:39:34 +00:00
|
|
|
count++;
|
|
|
|
if (!count) {
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("no servers\n");
|
2004-02-20 02:39:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-02 04:09:15 +00:00
|
|
|
list = malloc (count * sizeof (server_t **));
|
|
|
|
for (l = &servers, count = 0; *l; l = &(*l)->next)
|
|
|
|
list[count] = *l;
|
2004-02-20 02:39:34 +00:00
|
|
|
qsort (list, count, sizeof (*list), server_compare);
|
|
|
|
for (l = list; *l; l++) {
|
|
|
|
sv = *l;
|
2004-03-05 23:42:15 +00:00
|
|
|
qtv_printf ("%-20s %s(%s)\n", sv->name, sv->address,
|
2004-02-21 02:31:22 +00:00
|
|
|
NET_AdrToString (sv->adr));
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
2005-05-02 04:09:15 +00:00
|
|
|
free (list);
|
2004-02-20 02:39:34 +00:00
|
|
|
}
|
|
|
|
|
2004-02-21 02:31:22 +00:00
|
|
|
static void
|
|
|
|
server_shutdown (void)
|
|
|
|
{
|
2005-05-02 04:09:15 +00:00
|
|
|
Hash_FlushTable (server_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
server_run (server_t *sv)
|
|
|
|
{
|
|
|
|
static byte msg[] = {qtv_nop};
|
|
|
|
Netchan_Transmit (&sv->netchan, sizeof (msg), msg);
|
|
|
|
sv->next_run = realtime + 0.03;
|
2004-02-21 02:31:22 +00:00
|
|
|
}
|
|
|
|
|
2004-02-20 02:39:34 +00:00
|
|
|
void
|
|
|
|
Server_Init (void)
|
|
|
|
{
|
2004-02-21 02:31:22 +00:00
|
|
|
Sys_RegisterShutdown (server_shutdown);
|
2005-05-02 04:09:15 +00:00
|
|
|
server_hash = Hash_NewTable (61, server_get_key, server_free, 0);
|
2004-02-20 02:39:34 +00:00
|
|
|
Cmd_AddCommand ("sv_new", sv_new_f, "Add a new server");
|
|
|
|
Cmd_AddCommand ("sv_del", sv_del_f, "Remove an existing server");
|
|
|
|
Cmd_AddCommand ("sv_list", sv_list_f, "List available servers");
|
|
|
|
}
|
2005-05-02 04:09:15 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Server_Frame (void)
|
|
|
|
{
|
|
|
|
server_t *sv;
|
|
|
|
|
|
|
|
for (sv = servers; sv; sv = sv->next) {
|
|
|
|
if (sv->next_run && sv->next_run <= realtime) {
|
|
|
|
sv->next_run = 0;
|
|
|
|
server_run (sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|