2001-09-18 03:24:26 +00:00
|
|
|
/*
|
|
|
|
master.c
|
|
|
|
|
|
|
|
Quakeworld master server
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-09-18 03:24:26 +00:00
|
|
|
|
|
|
|
/* afternoon hack (james was here.)
|
|
|
|
FIXME: I coded this outside of the QF tree so it has non-quake
|
|
|
|
stuff like malloc and things... ie, it's POSIX, not quake. */
|
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include "unistd.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2001-11-20 06:27:48 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
2001-09-18 06:47:02 +00:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
2001-09-25 06:18:30 +00:00
|
|
|
#ifdef HAVE_WINSOCK_H
|
|
|
|
# include <winsock.h>
|
|
|
|
#endif
|
2001-09-18 06:47:02 +00:00
|
|
|
|
2001-09-18 03:24:26 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-09-18 07:26:12 +00:00
|
|
|
#include <ctype.h>
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
#include "protocol.h"
|
2001-09-18 03:24:26 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
#define S2C_CHALLENGE 'c'
|
|
|
|
#define S2M_HEARTBEAT 'a'
|
|
|
|
#define S2M_SHUTDOWN 'C'
|
|
|
|
#endif
|
2001-09-18 08:58:51 +00:00
|
|
|
|
|
|
|
#define MASTER_TIMEOUT 300
|
|
|
|
#define SLIST_MULTIPLE 1
|
2001-09-18 03:24:26 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2001-10-20 08:20:27 +00:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
time_t updated;
|
|
|
|
qboolean notimeout;
|
2001-09-18 03:24:26 +00:00
|
|
|
} server_t;
|
|
|
|
|
|
|
|
int
|
2001-09-18 08:58:51 +00:00
|
|
|
QW_AddHeartbeat (server_t **servers_p, int slen,
|
|
|
|
struct sockaddr_in *addr, char *buf)
|
2001-09-18 03:24:26 +00:00
|
|
|
{
|
2001-09-18 06:47:02 +00:00
|
|
|
server_t *servers = *servers_p;
|
|
|
|
int freeslot = -1;
|
2001-09-18 03:24:26 +00:00
|
|
|
int i;
|
2001-09-18 06:47:02 +00:00
|
|
|
int sequence, players;
|
2001-09-18 08:58:51 +00:00
|
|
|
char *c;
|
2001-09-18 06:47:02 +00:00
|
|
|
|
|
|
|
sequence = atoi (buf + 2);
|
2001-09-18 08:58:51 +00:00
|
|
|
c = strchr (buf + 2, '\n');
|
|
|
|
players = c ? atoi (c + 1) : 0;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
for (i = 0; i < slen; i++) {
|
2001-09-18 08:58:51 +00:00
|
|
|
if (servers[i].updated
|
|
|
|
&& servers[i].addr.sin_addr.s_addr == addr->sin_addr.s_addr
|
2001-09-18 06:47:02 +00:00
|
|
|
&& servers[i].addr.sin_port == addr->sin_port) {
|
|
|
|
time (&servers[i].updated);
|
2001-09-18 03:24:26 +00:00
|
|
|
#if 1
|
2001-09-18 06:47:02 +00:00
|
|
|
printf ("Refreshed %s:%d (seq %d, players %d)\n",
|
|
|
|
inet_ntoa (servers[i].addr.sin_addr),
|
|
|
|
ntohs (servers[i].addr.sin_port),
|
|
|
|
sequence, players);
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
|
|
|
return slen;
|
2001-09-18 06:47:02 +00:00
|
|
|
} else if (freeslot == -1 && servers[i].updated == 0) {
|
|
|
|
freeslot = i;
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
if (freeslot == -1) {
|
2001-09-18 03:24:26 +00:00
|
|
|
server_t *newservers;
|
2001-09-18 08:58:51 +00:00
|
|
|
newservers = realloc (servers, sizeof (server_t)
|
|
|
|
* (slen + SLIST_MULTIPLE));
|
2001-09-18 06:47:02 +00:00
|
|
|
if (!newservers) {
|
2001-09-18 08:58:51 +00:00
|
|
|
printf ("realloc failed\n");
|
2001-09-18 03:24:26 +00:00
|
|
|
return slen; // boo.
|
|
|
|
}
|
2001-09-18 08:58:51 +00:00
|
|
|
|
|
|
|
for (i = slen; i < slen + SLIST_MULTIPLE; i++)
|
2001-09-18 06:47:02 +00:00
|
|
|
newservers[i].updated = 0;
|
2001-09-18 08:58:51 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
freeslot = slen;
|
2001-09-18 08:58:51 +00:00
|
|
|
slen += SLIST_MULTIPLE;
|
2001-09-18 06:47:02 +00:00
|
|
|
*servers_p = newservers;
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
servers[freeslot].addr = *addr;
|
2001-10-20 08:20:27 +00:00
|
|
|
servers[freeslot].notimeout = false;
|
2001-09-18 03:24:26 +00:00
|
|
|
#if 1
|
2001-09-18 06:47:02 +00:00
|
|
|
printf ("Added %s:%d (seq %d, players %d)\n",
|
|
|
|
inet_ntoa (servers[freeslot].addr.sin_addr),
|
|
|
|
ntohs (servers[freeslot].addr.sin_port),
|
|
|
|
sequence, players);
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
2001-09-18 06:47:02 +00:00
|
|
|
time (&(servers[freeslot].updated));
|
2001-09-18 03:24:26 +00:00
|
|
|
return slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-18 06:47:02 +00:00
|
|
|
QW_TimeoutHearts (server_t *servers, int slen)
|
2001-09-18 03:24:26 +00:00
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
int i;
|
2001-09-18 06:47:02 +00:00
|
|
|
time (&t);
|
|
|
|
for (i = 0; i < slen; i++) {
|
|
|
|
if (servers[i].updated != 0
|
2001-10-20 08:20:27 +00:00
|
|
|
&& servers[i].updated < t - MASTER_TIMEOUT
|
|
|
|
&& !servers[i].notimeout) {
|
2001-09-18 06:47:02 +00:00
|
|
|
servers[i].updated = 0;
|
2001-09-18 03:24:26 +00:00
|
|
|
#if 1
|
2001-09-18 06:47:02 +00:00
|
|
|
printf ("Removed %s:%d (timeout)\n",
|
|
|
|
inet_ntoa (servers[i].addr.sin_addr),
|
|
|
|
ntohs (servers[i].addr.sin_port));
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
void
|
2001-09-18 08:58:51 +00:00
|
|
|
QW_HeartShutdown (struct sockaddr_in *addr, server_t *servers,
|
|
|
|
int slen)
|
|
|
|
{
|
2001-09-18 03:24:26 +00:00
|
|
|
int i;
|
2001-09-18 06:47:02 +00:00
|
|
|
for (i = 0; i < slen; i++) {
|
|
|
|
if (servers[i].addr.sin_addr.s_addr == addr->sin_addr.s_addr
|
2001-09-18 03:24:26 +00:00
|
|
|
&& servers[i].addr.sin_port == addr->sin_port) {
|
2001-09-18 06:47:02 +00:00
|
|
|
servers[i].updated = 0;
|
2001-09-18 03:24:26 +00:00
|
|
|
#if 1
|
2001-09-18 06:47:02 +00:00
|
|
|
printf ("Removed %s:%d\n",
|
|
|
|
inet_ntoa (servers[i].addr.sin_addr),
|
|
|
|
ntohs (servers[i].addr.sin_port));
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-18 08:58:51 +00:00
|
|
|
QW_SendHearts (int sock, struct sockaddr_in *addr, server_t *servers,
|
|
|
|
int serverlen)
|
2001-09-18 03:24:26 +00:00
|
|
|
{
|
|
|
|
unsigned char *out;
|
2001-09-18 06:47:02 +00:00
|
|
|
int count, cpos;
|
2001-09-18 03:24:26 +00:00
|
|
|
int i;
|
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
count = 0;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 08:58:51 +00:00
|
|
|
for (i = 0; i < serverlen; i++)
|
2001-09-18 06:47:02 +00:00
|
|
|
if (servers[i].updated != 0)
|
2001-09-18 03:24:26 +00:00
|
|
|
count++;
|
2001-09-18 08:58:51 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
out = malloc ((count + 1) * 6);
|
2001-09-18 08:58:51 +00:00
|
|
|
if (!out) {
|
|
|
|
printf ("malloc failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
cpos = 1;
|
|
|
|
out[0] = out[1] = out[2] = out[3] = 0xff;
|
|
|
|
out[4] = 0x64;
|
|
|
|
out[5] = 0x0a;
|
|
|
|
|
|
|
|
for (i = 0; i < serverlen; i++) {
|
|
|
|
if (servers[i].updated != 0) {
|
|
|
|
unsigned char *p = out + (cpos * 6);
|
2001-09-18 08:58:51 +00:00
|
|
|
p[0] = ((unsigned char *) &servers[i].addr.sin_addr.s_addr)[0];
|
|
|
|
p[1] = ((unsigned char *) &servers[i].addr.sin_addr.s_addr)[1];
|
|
|
|
p[2] = ((unsigned char *) &servers[i].addr.sin_addr.s_addr)[2];
|
|
|
|
p[3] = ((unsigned char *) &servers[i].addr.sin_addr.s_addr)[3];
|
2001-09-18 06:47:02 +00:00
|
|
|
p[4] = (unsigned char) (ntohs(servers[i].addr.sin_port) >> 8);
|
|
|
|
p[5] = (unsigned char) (ntohs(servers[i].addr.sin_port) & 0xFF);
|
2001-09-18 03:24:26 +00:00
|
|
|
++cpos;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 08:58:51 +00:00
|
|
|
sendto (sock, out, (count + 1) * 6, 0, (struct sockaddr *) addr,
|
|
|
|
sizeof (struct sockaddr_in));
|
2001-09-18 06:47:02 +00:00
|
|
|
free (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QW_Pong (int sock, struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
char data[6] = {0xFF, 0xFF, 0xFF, 0xFF, A2A_ACK, 0};
|
2001-09-18 08:58:51 +00:00
|
|
|
printf ("Ping\n");
|
2001-09-18 06:47:02 +00:00
|
|
|
sendto (sock, data, sizeof (data), 0,
|
2001-09-18 08:58:51 +00:00
|
|
|
(struct sockaddr *) addr, sizeof (struct sockaddr_in));
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2001-09-18 06:47:02 +00:00
|
|
|
QW_Master (struct sockaddr_in *addr)
|
2001-09-18 03:24:26 +00:00
|
|
|
{
|
|
|
|
int sock;
|
2001-09-18 08:58:51 +00:00
|
|
|
server_t *servers;
|
|
|
|
int serverlen = SLIST_MULTIPLE;
|
2001-09-18 03:24:26 +00:00
|
|
|
int i;
|
2001-10-21 00:48:38 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA winsockdata;
|
|
|
|
#endif
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 08:58:51 +00:00
|
|
|
servers = malloc (sizeof (server_t) * serverlen);
|
|
|
|
if (!servers) {
|
|
|
|
printf ("initial malloc failed\n");
|
2001-09-18 06:47:02 +00:00
|
|
|
return;
|
2001-09-18 08:58:51 +00:00
|
|
|
}
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 08:58:51 +00:00
|
|
|
for (i = 0; i < serverlen; i++)
|
2001-09-18 06:47:02 +00:00
|
|
|
servers[i].updated = 0;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-10-21 00:48:38 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
i = WSAStartup (MAKEWORD (1, 1), &winsockdata);
|
|
|
|
if (i) {
|
|
|
|
printf ("Winsock initialization failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-20 08:20:27 +00:00
|
|
|
sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2001-09-18 08:58:51 +00:00
|
|
|
if (sock < 0) {
|
|
|
|
printf ("socket failed\n");
|
2001-09-18 03:24:26 +00:00
|
|
|
return;
|
2001-09-18 08:58:51 +00:00
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
|
2001-09-18 08:58:51 +00:00
|
|
|
if (bind (sock, (struct sockaddr *) addr, sizeof (struct sockaddr_in))) {
|
|
|
|
printf ("bind failed\n");
|
2001-09-18 03:24:26 +00:00
|
|
|
return;
|
2001-09-18 08:58:51 +00:00
|
|
|
}
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
listen (sock, 4); // probably don't need this.
|
2001-09-18 03:24:26 +00:00
|
|
|
|
|
|
|
while (1) {
|
2001-09-18 06:47:02 +00:00
|
|
|
char buf[31];
|
2001-09-18 03:24:26 +00:00
|
|
|
struct sockaddr_in recvaddr;
|
2001-09-18 06:47:02 +00:00
|
|
|
int recvsize = sizeof (struct sockaddr_in);
|
|
|
|
int size;
|
2001-09-18 08:58:51 +00:00
|
|
|
buf[30] = '\0'; // a sentinal for string ops
|
2001-09-18 06:47:02 +00:00
|
|
|
|
|
|
|
size = recvfrom (sock, buf, sizeof (buf) - 1, 0,
|
2001-09-18 08:58:51 +00:00
|
|
|
(struct sockaddr *) &recvaddr, &recvsize);
|
2001-09-18 06:47:02 +00:00
|
|
|
if (size == -1) {
|
|
|
|
printf ("recvfrom failed\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
printf ("Got %d bytes: 0x%x from %s\n", size, buf[0],
|
|
|
|
inet_ntoa (recvaddr.sin_addr));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2001-09-18 07:26:12 +00:00
|
|
|
printf ("Message Contents: '");
|
2001-09-18 06:47:02 +00:00
|
|
|
{ // so that 'j' isn't unused when commented out
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < size; j++)
|
2001-09-18 07:26:12 +00:00
|
|
|
if (isprint (buf[j]))
|
|
|
|
printf ("%c", buf[j]);
|
2001-09-18 08:58:51 +00:00
|
|
|
else
|
2001-09-18 07:26:12 +00:00
|
|
|
switch (buf[j]) {
|
|
|
|
case '\n': printf ("\\n"); break;
|
|
|
|
case '\0': printf ("\\0"); break;
|
|
|
|
default: printf ("(%02x)", buf[j]);
|
2001-09-18 08:58:51 +00:00
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
}
|
2001-09-18 07:26:12 +00:00
|
|
|
printf ("'\n");
|
2001-09-18 06:47:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
QW_TimeoutHearts (servers, serverlen);
|
2001-09-18 03:24:26 +00:00
|
|
|
switch (buf[0]) {
|
2001-09-18 06:47:02 +00:00
|
|
|
case S2C_CHALLENGE:
|
|
|
|
QW_SendHearts (sock, &recvaddr, servers, serverlen);
|
|
|
|
break;
|
|
|
|
case S2M_HEARTBEAT:
|
2001-09-18 08:58:51 +00:00
|
|
|
serverlen = QW_AddHeartbeat (&servers, serverlen,
|
|
|
|
&recvaddr, buf);
|
2001-09-18 06:47:02 +00:00
|
|
|
break;
|
|
|
|
case S2M_SHUTDOWN:
|
|
|
|
QW_HeartShutdown (&recvaddr, servers, serverlen);
|
|
|
|
break;
|
|
|
|
case A2A_PING:
|
|
|
|
QW_Pong (sock, &recvaddr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf ("Unknown 0x%x\n", buf[0]);
|
|
|
|
break;
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
free (servers);
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-18 06:47:02 +00:00
|
|
|
main (int argc, char **argv)
|
2001-09-18 03:24:26 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
2001-09-25 06:18:30 +00:00
|
|
|
short port = htons (27000);
|
|
|
|
#ifndef WIN32 //FIXME
|
|
|
|
int c;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
while ((c = getopt (argc, argv, "p:")) != -1) {
|
|
|
|
if (c == 'p') {
|
2001-09-25 06:18:30 +00:00
|
|
|
port = htons (atoi (optarg));
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2001-09-25 06:18:30 +00:00
|
|
|
#endif
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = port;
|
2001-09-18 06:47:02 +00:00
|
|
|
QW_Master (&addr);
|
2001-09-18 03:24:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|