- much formatting cleanups

- made it use #if HAVE_FOO_H like the rest of QF
- a couple bug fixes
- cleaned up what it prints, including adding sequence numbers and
  player counts in the heartbeats
This commit is contained in:
Adam Olsen 2001-09-18 06:47:02 +00:00
parent b79ff6378d
commit 915674733c

View file

@ -29,16 +29,26 @@
FIXME: I coded this outside of the QF tree so it has non-quake 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. */ stuff like malloc and things... ie, it's POSIX, not quake. */
#include <sys/socket.h> #ifdef HAVE_CONFIG_H
#include <arpa/inet.h> # include "config.h"
#endif
#ifdef HAVE_UNISTD_H
# include "unistd.h"
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <protocol.h> #include "protocol.h"
#if 0 #if 0
#define S2C_CHALLENGE 'c' #define S2C_CHALLENGE 'c'
@ -55,78 +65,86 @@ typedef struct {
} server_t; } server_t;
int int
QW_AddHeartbeat(server_t **servers_p, int slen, struct sockaddr_in *addr) QW_AddHeartbeat (server_t **servers_p, int slen, struct sockaddr_in *addr, char *buf)
{ {
server_t *servers=*servers_p; server_t *servers = *servers_p;
int freeslot=-1; int freeslot = -1;
int i; int i;
int sequence, players;
for (i=0;i<slen;i++) { sequence = atoi (buf + 2);
if (servers[i].addr.sin_addr.s_addr==addr->sin_addr.s_addr players = atoi (buf + strlen (buf + 2) + 1);
&& servers[i].addr.sin_port == addr->sin_port ) {
time(&servers[i].updated); for (i = 0; i < slen; i++) {
if (servers[i].addr.sin_addr.s_addr == addr->sin_addr.s_addr
&& servers[i].addr.sin_port == addr->sin_port) {
time (&servers[i].updated);
#if 1 #if 1
printf("Refreshed %s:%d\n", printf ("Refreshed %s:%d (seq %d, players %d)\n",
inet_ntoa(servers[i].addr.sin_addr), inet_ntoa (servers[i].addr.sin_addr),
ntohs(servers[i].addr.sin_port)); ntohs (servers[i].addr.sin_port),
sequence, players);
#endif #endif
return slen; return slen;
} else if (freeslot==-1 && servers[i].updated==0) { } else if (freeslot == -1 && servers[i].updated == 0) {
freeslot=i; freeslot = i;
} }
} }
if (freeslot==-1) { if (freeslot == -1) {
server_t *newservers; server_t *newservers;
newservers=(server_t*)realloc(servers,sizeof(server_t)*(slen+50)); newservers = realloc (servers, sizeof (server_t) * (slen + 50));
if (newservers==NULL) { if (!newservers) {
return slen; // boo. return slen; // boo.
} }
for (i=slen;i<slen+50;i++) { for (i = slen; i < slen + 50; i++) {
newservers[i].updated=0; newservers[i].updated = 0;
} }
memcpy(newservers,servers,slen*sizeof(server_t)); memcpy (newservers, servers, slen * sizeof (server_t));
freeslot=slen; freeslot = slen;
slen+=50; slen += 50;
*servers_p=newservers; *servers_p = newservers;
} }
servers[freeslot].addr=*addr; servers[freeslot].addr = *addr;
#if 1 #if 1
printf("Added %s:%d\n", printf ("Added %s:%d (seq %d, players %d)\n",
inet_ntoa(servers[freeslot].addr.sin_addr), inet_ntoa (servers[freeslot].addr.sin_addr),
ntohs(servers[freeslot].addr.sin_port)); ntohs (servers[freeslot].addr.sin_port),
sequence, players);
#endif #endif
time(&(servers[freeslot].updated)); time (&(servers[freeslot].updated));
return slen; return slen;
} }
void void
QW_TimeoutHearts(server_t *servers, int slen) QW_TimeoutHearts (server_t *servers, int slen)
{ {
time_t t; time_t t;
int i; int i;
time(&t); time (&t);
for (i=0;i<slen;i++) { for (i = 0; i < slen; i++) {
if (servers[i].updated!=0 && servers[i].updated < t - MASTER_TIMEOUT) { if (servers[i].updated != 0
servers[i].updated=0; && servers[i].updated < t - MASTER_TIMEOUT) {
servers[i].updated = 0;
#if 1 #if 1
printf("Removed %s:%d (timeout)\n", printf ("Removed %s:%d (timeout)\n",
inet_ntoa(servers[i].addr.sin_addr), inet_ntoa (servers[i].addr.sin_addr),
ntohs(servers[i].addr.sin_port)); ntohs (servers[i].addr.sin_port));
#endif #endif
} }
} }
} }
void QW_HeartShutdown(struct sockaddr_in *addr,server_t *servers, int slen) { void
QW_HeartShutdown (struct sockaddr_in *addr,server_t *servers, int slen) {
int i; int i;
for (i=0;i<slen;i++) { for (i = 0; i < slen; i++) {
if (servers[i].addr.sin_addr.s_addr==addr->sin_addr.s_addr if (servers[i].addr.sin_addr.s_addr == addr->sin_addr.s_addr
&& servers[i].addr.sin_port == addr->sin_port) { && servers[i].addr.sin_port == addr->sin_port) {
servers[i].updated=0; servers[i].updated = 0;
#if 1 #if 1
printf("Removed %s:%d\n", printf ("Removed %s:%d\n",
inet_ntoa(servers[i].addr.sin_addr), inet_ntoa (servers[i].addr.sin_addr),
ntohs(servers[i].addr.sin_port)); ntohs (servers[i].addr.sin_port));
#endif #endif
return; return;
} }
@ -134,103 +152,142 @@ void QW_HeartShutdown(struct sockaddr_in *addr,server_t *servers, int slen) {
} }
void void
QW_SendHearts(int sock,struct sockaddr_in *addr, QW_SendHearts (int sock, struct sockaddr_in *addr,
server_t *servers, int serverlen) server_t *servers, int serverlen)
{ {
unsigned char *out; unsigned char *out;
int count,cpos; int count, cpos;
int i; int i;
count=0; count = 0;
for (i=0;i<serverlen;i++) { for (i = 0; i < serverlen; i++) {
if (servers[i].updated!=0) if (servers[i].updated != 0)
count++; count++;
} }
out=(unsigned char *)malloc((count+1)*6*sizeof(unsigned char)); out = malloc ((count + 1) * 6);
cpos=1; cpos = 1;
out[0]=out[1]=out[2]=out[3]=0xff; out[0] = out[1] = out[2] = out[3] = 0xff;
out[4]=0x64; out[4] = 0x64;
out[5]=0x0a; out[5] = 0x0a;
for (i=0;i<serverlen;i++) { for (i = 0; i < serverlen; i++) {
if (servers[i].updated!=0) { if (servers[i].updated != 0) {
unsigned char *p=out+(cpos*6); unsigned char *p = out + (cpos * 6);
p[0]=((unsigned char*)&servers[i].addr.sin_addr.s_addr)[0]; 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[1] = ((unsigned char*) &servers[i].addr.sin_addr.s_addr)[1];
p[2]=((unsigned char*)&servers[i].addr.sin_addr.s_addr)[2]; p[2] = ((unsigned char*) &servers[i].addr.sin_addr.s_addr)[2];
p[3]=((unsigned char*)&servers[i].addr.sin_addr.s_addr)[3]; p[3] = ((unsigned char*) &servers[i].addr.sin_addr.s_addr)[3];
p[4]=(unsigned char)(ntohs(servers[i].addr.sin_port) >> 8); p[4] = (unsigned char) (ntohs(servers[i].addr.sin_port) >> 8);
p[5]=(unsigned char)(ntohs(servers[i].addr.sin_port) & 0xFF); p[5] = (unsigned char) (ntohs(servers[i].addr.sin_port) & 0xFF);
++cpos; ++cpos;
} }
} }
sendto(sock,out,count*6*sizeof(unsigned char),0, sendto (sock, out, (count + 1) * 6, 0,
(struct sockaddr*)addr,sizeof(struct sockaddr_in)); (struct sockaddr*) addr, sizeof (struct sockaddr_in));
free(out); free (out);
}
void
QW_Pong (int sock, struct sockaddr_in *addr)
{
char data[6] = {0xFF, 0xFF, 0xFF, 0xFF, A2A_ACK, 0};
printf ("ping\n");
sendto (sock, data, sizeof (data), 0,
(struct sockaddr*) addr, sizeof (struct sockaddr_in));
} }
void void
QW_Master(struct sockaddr_in *addr) QW_Master (struct sockaddr_in *addr)
{ {
int sock; int sock;
server_t *servers=(server_t*)malloc(sizeof(server_t)*50); server_t *servers = malloc (sizeof (server_t) * 50);
int serverlen=50; int serverlen = 50;
int i; int i;
if (servers==NULL) return; if (servers == NULL)
for (i=0;i<50;i++) {
servers[i].updated=0;
}
sock=socket(AF_INET,SOCK_DGRAM,0);
if (sock<0) {
return; return;
for (i = 0; i < 50; i++) {
servers[i].updated = 0;
} }
if (bind(sock,(struct sockaddr*)addr,sizeof(struct sockaddr_in))!=0)
sock = socket (AF_INET,SOCK_DGRAM, 0);
if (sock < 0)
return;
if (bind (sock, (struct sockaddr*)addr, sizeof (struct sockaddr_in)))
return; return;
listen(sock,4); // probably don't need this. listen (sock, 4); // probably don't need this.
while (1) { while (1) {
char buf[3]; char buf[31];
struct sockaddr_in recvaddr; struct sockaddr_in recvaddr;
int recvsize; int recvsize = sizeof (struct sockaddr_in);
recvfrom(sock,buf,3,0,(struct sockaddr*)&recvaddr,&recvsize); int size;
printf("Got 0x%x\n",buf[0]);
QW_TimeoutHearts(servers,serverlen); size = recvfrom (sock, buf, sizeof (buf) - 1, 0,
(struct sockaddr*) &recvaddr, &recvsize);
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
printf ("Contents:");
{ // so that 'j' isn't unused when commented out
int j;
for (j = 0; j < size; j++)
printf (" %02x", buf[j]);
}
printf ("\n");
buf[size] = '\0';
printf ("in ascii: '%s'\n", buf);
#endif
QW_TimeoutHearts (servers, serverlen);
switch (buf[0]) { switch (buf[0]) {
case S2C_CHALLENGE: case S2C_CHALLENGE:
QW_SendHearts(sock,&recvaddr,servers,serverlen); QW_SendHearts (sock, &recvaddr, servers, serverlen);
break; break;
case S2M_HEARTBEAT: case S2M_HEARTBEAT:
serverlen=QW_AddHeartbeat(&servers,serverlen,&recvaddr); serverlen = QW_AddHeartbeat (&servers, serverlen, &recvaddr, buf);
break; break;
case S2M_SHUTDOWN: case S2M_SHUTDOWN:
QW_HeartShutdown(&recvaddr,servers,serverlen); QW_HeartShutdown (&recvaddr, servers, serverlen);
break; break;
case A2A_PING:
QW_Pong (sock, &recvaddr);
break;
default:
printf ("Unknown 0x%x\n", buf[0]);
break;
} }
} }
free(servers); free (servers);
} }
int int
main(int argc, char **argv) main (int argc, char **argv)
{ {
int c; int c;
struct sockaddr_in addr; struct sockaddr_in addr;
addr.sin_family=AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(27000); addr.sin_port = htons (27000);
while ((c=getopt(argc,argv,"p:"))!=-1) { while ((c = getopt (argc, argv, "p:")) != -1) {
if (c=='p') { if (c == 'p') {
addr.sin_port = htons(atoi(optarg)); addr.sin_port = htons (atoi (optarg));
} }
} }
QW_Master(&addr); QW_Master (&addr);
return 0; return 0;
} }