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:
|
|
|
|
|
2002-06-12 20:26:30 +00:00
|
|
|
eree Software Foundation, Inc.
|
2001-09-18 03:24:26 +00:00
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
/* 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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-10-11 20:47:17 +00:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
# include "winquake.h"
|
|
|
|
#endif
|
2001-09-18 06:47:02 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include "unistd.h"
|
|
|
|
#endif
|
2002-05-10 18:39:59 +00:00
|
|
|
#include <sys/types.h>
|
2001-09-18 06:47:02 +00:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2001-11-20 06:27:48 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2002-08-09 02:44:23 +00:00
|
|
|
# define model_t sun_model_t
|
2001-11-20 06:27:48 +00:00
|
|
|
# include <netinet/in.h>
|
2002-08-09 02:44:23 +00:00
|
|
|
# undef model_t
|
2001-11-20 06:27:48 +00:00
|
|
|
#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
|
2002-02-11 20:50:54 +00:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
2002-04-15 14:35:10 +00:00
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
# include <sys/uio.h>
|
|
|
|
#endif
|
2001-09-18 06:47:02 +00:00
|
|
|
|
2001-09-18 03:24:26 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-09-18 07:26:12 +00:00
|
|
|
#include <ctype.h>
|
2002-02-12 17:44:41 +00:00
|
|
|
#include <getopt.h>
|
2003-01-31 04:11:06 +00:00
|
|
|
#include <stdarg.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
|
|
|
|
2003-01-31 04:11:06 +00:00
|
|
|
static void __attribute__ ((format (printf, 1, 2)))
|
|
|
|
ma_log (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
time_t mytime = 0;
|
|
|
|
struct tm *local = NULL;
|
|
|
|
char stamp[1024];
|
|
|
|
|
|
|
|
mytime = time (NULL);
|
|
|
|
local = localtime (&mytime);
|
|
|
|
strftime (stamp, sizeof (stamp), "[%b %e %X] ", local);
|
|
|
|
fprintf (stdout, "%s", stamp);
|
|
|
|
va_start (args, fmt);
|
|
|
|
vfprintf (stdout, fmt, args);
|
|
|
|
va_end (args);
|
2003-01-31 04:49:31 +00:00
|
|
|
fflush (stdout);
|
2003-01-31 04:11:06 +00:00
|
|
|
}
|
|
|
|
|
2002-05-13 21:53:52 +00:00
|
|
|
#ifdef HAVE_IN_PKTINFO
|
2002-04-15 19:06:25 +00:00
|
|
|
static int
|
|
|
|
qf_sendmsg (int sock, void *data, size_t len, struct msghdr *msghdr)
|
|
|
|
{
|
|
|
|
struct iovec iovec = {data, len};
|
|
|
|
msghdr->msg_iov = &iovec;
|
|
|
|
msghdr->msg_iovlen = 1;
|
|
|
|
return sendmsg (sock, msghdr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qf_recvmsg (int sock, void *data, size_t len, struct msghdr *msghdr)
|
|
|
|
{
|
|
|
|
struct iovec iovec = {data, len};
|
|
|
|
msghdr->msg_iov = &iovec;
|
|
|
|
msghdr->msg_iovlen = 1;
|
|
|
|
return recvmsg (sock, msghdr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qf_initsocket (int sock)
|
|
|
|
{
|
|
|
|
int ip_pktinfo = 1;
|
|
|
|
if (setsockopt (sock, SOL_IP, IP_PKTINFO, &ip_pktinfo,
|
|
|
|
sizeof (ip_pktinfo)) == -1) {
|
|
|
|
perror ("setsockopt");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MSGHDR \
|
|
|
|
char ancillary[CMSG_SPACE (sizeof (struct in_pktinfo))]; \
|
|
|
|
struct sockaddr_in recvaddr; \
|
|
|
|
struct msghdr msghdr = { \
|
|
|
|
&recvaddr, \
|
|
|
|
sizeof (recvaddr), \
|
|
|
|
0, 0, \
|
|
|
|
ancillary, \
|
|
|
|
sizeof (ancillary), \
|
|
|
|
0 \
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct msghdr msghdr_t;
|
|
|
|
|
|
|
|
#else
|
|
|
|
static int
|
|
|
|
qf_sendmsg (int sock, void *data, size_t len, struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
return sendto (sock, data, len, 0, (struct sockaddr *)addr, sizeof (*addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qf_recvmsg (int sock, void *data, size_t len, struct sockaddr_in *addr)
|
|
|
|
{
|
2002-12-20 23:53:34 +00:00
|
|
|
#ifdef HAVE_SOCKLEN_T
|
|
|
|
socklen_t addr_len = sizeof (*addr);
|
|
|
|
#else
|
|
|
|
int addr_len = sizeof (*addr);
|
|
|
|
#endif
|
2002-04-15 19:06:25 +00:00
|
|
|
return recvfrom (sock, data, len, 0, (struct sockaddr *)addr, &addr_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qf_initsocket (int sock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MSGHDR struct sockaddr_in msghdr
|
|
|
|
#define recvaddr msghdr
|
|
|
|
|
|
|
|
typedef struct sockaddr_in msghdr_t;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-01-31 04:56:11 +00:00
|
|
|
#define MASTER_TIMEOUT 600
|
2001-09-18 08:58:51 +00:00
|
|
|
#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;
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-09-18 08:58:51 +00:00
|
|
|
QW_AddHeartbeat (server_t **servers_p, int slen,
|
2003-01-06 18:28:13 +00:00
|
|
|
struct sockaddr_in *addr, const char *buf, qboolean notimeout)
|
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
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Refreshed %s:%d (seq %d, players %d)\n",
|
2001-09-18 06:47:02 +00:00
|
|
|
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++)
|
2002-02-11 20:50:54 +00:00
|
|
|
memset (&newservers[i], 0, sizeof (newservers[i]));
|
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;
|
2002-02-11 20:50:54 +00:00
|
|
|
servers = *servers_p;
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
2001-09-18 06:47:02 +00:00
|
|
|
servers[freeslot].addr = *addr;
|
2002-02-11 20:50:54 +00:00
|
|
|
servers[freeslot].notimeout = notimeout;
|
2001-09-18 03:24:26 +00:00
|
|
|
#if 1
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Added %s:%d (seq %d, players %d)\n",
|
2001-09-18 06:47:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static 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
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Removed %s:%d (timeout)\n",
|
2001-09-18 06:47:02 +00:00
|
|
|
inet_ntoa (servers[i].addr.sin_addr),
|
|
|
|
ntohs (servers[i].addr.sin_port));
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static 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
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Removed %s:%d\n",
|
2001-09-18 06:47:02 +00:00
|
|
|
inet_ntoa (servers[i].addr.sin_addr),
|
|
|
|
ntohs (servers[i].addr.sin_port));
|
2001-09-18 03:24:26 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-04-15 19:06:25 +00:00
|
|
|
QW_SendHearts (int sock, msghdr_t *msghdr, 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;
|
2002-02-11 21:28:18 +00:00
|
|
|
out[0] = out[1] = out[2] = out[3] = 0xff; // connectionless packet
|
2002-02-11 21:23:28 +00:00
|
|
|
out[4] = M2C_MASTER_REPLY;
|
|
|
|
out[5] = '\n';
|
2001-09-18 06:47:02 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2002-04-15 19:06:25 +00:00
|
|
|
qf_sendmsg (sock, out, (count + 1) * 6, msghdr);
|
2001-09-18 06:47:02 +00:00
|
|
|
free (out);
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-04-15 19:06:25 +00:00
|
|
|
QW_Pong (int sock, msghdr_t *msghdr)
|
2001-09-18 06:47:02 +00:00
|
|
|
{
|
2002-04-15 19:06:25 +00:00
|
|
|
// connectionless packet
|
2001-09-18 06:47:02 +00:00
|
|
|
char data[6] = {0xFF, 0xFF, 0xFF, 0xFF, A2A_ACK, 0};
|
2003-01-31 04:11:06 +00:00
|
|
|
//ma_log ("Ping\n");
|
2002-04-15 19:06:25 +00:00
|
|
|
qf_sendmsg (sock, data, sizeof (data), msghdr);
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
2002-02-11 20:50:54 +00:00
|
|
|
|
|
|
|
int serverlen = SLIST_MULTIPLE;
|
|
|
|
server_t *servers;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static 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
|
|
|
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-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
|
|
|
|
2002-04-15 19:06:25 +00:00
|
|
|
if (qf_initsocket (sock) == -1)
|
|
|
|
return;
|
2002-04-14 03:44:16 +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
|
|
|
|
|
|
|
while (1) {
|
2002-04-15 18:13:17 +00:00
|
|
|
int size;
|
2001-09-18 06:47:02 +00:00
|
|
|
char buf[31];
|
2002-04-15 19:06:25 +00:00
|
|
|
MSGHDR;
|
2002-04-14 03:44:16 +00:00
|
|
|
|
2001-09-18 08:58:51 +00:00
|
|
|
buf[30] = '\0'; // a sentinal for string ops
|
2001-09-18 06:47:02 +00:00
|
|
|
|
2002-04-15 19:06:25 +00:00
|
|
|
size = qf_recvmsg (sock, buf, sizeof (buf) - 1, &msghdr);
|
2001-09-18 06:47:02 +00:00
|
|
|
if (size == -1) {
|
2002-04-15 19:06:25 +00:00
|
|
|
perror ("qf_recvmsg failed\n");
|
2001-09-18 06:47:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if 0
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Got %d bytes: 0x%x from %s\n", size, buf[0],
|
2001-09-18 06:47:02 +00:00
|
|
|
inet_ntoa (recvaddr.sin_addr));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("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:
|
2002-04-14 03:44:16 +00:00
|
|
|
QW_SendHearts (sock, &msghdr, servers, serverlen);
|
2001-09-18 06:47:02 +00:00
|
|
|
break;
|
|
|
|
case S2M_HEARTBEAT:
|
2001-09-18 08:58:51 +00:00
|
|
|
serverlen = QW_AddHeartbeat (&servers, serverlen,
|
2002-02-11 20:50:54 +00:00
|
|
|
&recvaddr, buf, false);
|
2001-09-18 06:47:02 +00:00
|
|
|
break;
|
|
|
|
case S2M_SHUTDOWN:
|
|
|
|
QW_HeartShutdown (&recvaddr, servers, serverlen);
|
|
|
|
break;
|
|
|
|
case A2A_PING:
|
2002-04-14 03:44:16 +00:00
|
|
|
QW_Pong (sock, &msghdr);
|
2001-09-18 06:47:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-01-31 04:11:06 +00:00
|
|
|
ma_log ("Unknown 0x%x\n", buf[0]);
|
2001-09-18 06:47:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-02-11 20:50:54 +00:00
|
|
|
static int
|
|
|
|
make_host_addr (const char *host, int port, struct sockaddr_in *host_addr)
|
|
|
|
{
|
|
|
|
struct hostent *host_ent;
|
|
|
|
|
|
|
|
host_ent = gethostbyname (host);
|
|
|
|
if (!host_ent)
|
|
|
|
return -1;
|
|
|
|
host_addr->sin_family = AF_INET;
|
|
|
|
memcpy (&host_addr->sin_addr, host_ent->h_addr_list[0],
|
|
|
|
sizeof (host_addr->sin_addr));
|
|
|
|
host_addr->sin_port = htons (port);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_hosts (const char *fname)
|
|
|
|
{
|
|
|
|
FILE *host_file;
|
|
|
|
int host_port;
|
|
|
|
char host_name[256];
|
2003-01-06 18:28:13 +00:00
|
|
|
static const char *fake_heartbeat = " ";
|
2002-02-11 20:50:54 +00:00
|
|
|
char *buf;
|
|
|
|
struct sockaddr_in host_addr;
|
|
|
|
|
|
|
|
host_file = fopen (fname, "rt");
|
|
|
|
if (!host_file) {
|
|
|
|
fprintf (stderr, "could not open `%s'\n", fname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (fgets (host_name, sizeof (host_name), host_file)) {
|
|
|
|
for (buf = host_name; *buf && !isspace ((byte)*buf) && *buf !=':';
|
|
|
|
buf++)
|
|
|
|
;
|
|
|
|
if (*buf)
|
|
|
|
*buf++ = 0;
|
|
|
|
if (*buf)
|
|
|
|
host_port = atoi (buf);
|
|
|
|
else
|
2002-02-11 21:28:18 +00:00
|
|
|
host_port = PORT_SERVER;
|
2002-02-11 20:50:54 +00:00
|
|
|
if (make_host_addr (host_name, host_port, &host_addr) == -1) {
|
|
|
|
fprintf (stderr, "could not resolve `%s', skipping", host_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
serverlen = QW_AddHeartbeat (&servers, serverlen, &host_addr,
|
|
|
|
fake_heartbeat, true);
|
|
|
|
}
|
|
|
|
fclose (host_file);
|
|
|
|
}
|
|
|
|
|
2002-02-13 17:58:30 +00:00
|
|
|
static int
|
|
|
|
net_init (void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int i;
|
|
|
|
WSADATA winsockdata;
|
|
|
|
|
|
|
|
i = WSAStartup (MAKEWORD (1, 1), &winsockdata);
|
|
|
|
if (i) {
|
|
|
|
printf ("Winsock initialization failed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
int c;
|
2001-09-18 03:24:26 +00:00
|
|
|
|
2002-02-13 17:58:30 +00:00
|
|
|
if (!net_init ())
|
|
|
|
return 1;
|
|
|
|
|
2002-02-11 20:50:54 +00:00
|
|
|
servers = calloc (sizeof (server_t), serverlen);
|
2002-02-12 17:44:41 +00:00
|
|
|
|
2002-04-15 18:13:17 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = htons (PORT_MASTER);
|
|
|
|
|
|
|
|
while ((c = getopt (argc, argv, "b:p:f:")) != -1) {
|
2002-02-11 20:50:54 +00:00
|
|
|
switch (c) {
|
2002-04-15 18:13:17 +00:00
|
|
|
case 'b':
|
|
|
|
c = inet_addr (optarg);
|
|
|
|
if (c == -1) {
|
|
|
|
fprintf (stderr, "bad bind address %s\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
addr.sin_addr.s_addr = c;
|
2002-02-11 20:50:54 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
read_hosts (optarg);
|
|
|
|
break;
|
2002-04-15 18:13:17 +00:00
|
|
|
case 'p':
|
|
|
|
addr.sin_port = htons (atoi (optarg));
|
|
|
|
break;
|
2001-09-18 03:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-12 17:44:41 +00:00
|
|
|
|
2001-09-18 06:47:02 +00:00
|
|
|
QW_Master (&addr);
|
2001-09-18 03:24:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|