-
Notifications
You must be signed in to change notification settings - Fork 1
/
metaserver.c
772 lines (649 loc) · 16.7 KB
/
metaserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* mostly borrwed from Nick Trown (COW) */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <pwd.h>
#include <string.h>
#include <ctype.h>
#ifdef hpux
#include <time.h>
#else /* hpux */
#include <sys/time.h>
#endif
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "netrek.h"
#define KEYS 5
#define F_CHAOS 'C'
#define F_PARADISE 'P'
#define F_HOCKEY 'H'
#define F_BRONCO 'B'
#define F_DOGFIGHT 'F'
#define F_INL_HOME 'h'
#define F_INL_AWAY 'a'
#define META_CACHE ".metacache"
#define NO_CACHE 0
#define READ_CACHE 1
#define READ_OLD_CACHE 2
#define MAX_SERVERS 48
#ifdef FORKNETREK
#define BUTTONS 3
#else
#define BUTTONS 2
#endif
struct servers {
char address[BUFSIZ];
int port;
int time;
int players;
int status;
char flag;
int RSA_client;
};
static char *metaserver = "metaserver.netrek.org";
static int metaport = 3521;
static struct servers *serverlist;
static int num_servers;
static W_Window playerWin;
static int quit_meta;
static char *keystrings[KEYS] =
{"OPEN:", "Wait queue:", "Nobody playing", "* Timed out", "* Garbag"};
#define KEY_OPEN 0
#define KEY_WAITQ 1
#define KEY_NOBODY 2
#define KEY_NORESP 3 /* usually metaserver lying */
#define KEY_GARBAGE 4 /* usually metaserver lying */
#define KEY_UNKNOWN 5 /* special case */
#if __STDC__ || defined(__cplusplus)
#define P_(s) s
#else
#define P_(s) ()
#endif
static int getnumber P_((char *string, int start));
static char *flagstring P_((char f));
static void readmeta P_((FILE *fsock, int cache_read));
static FILE *check_cache P_((int *last_cache));
static void parsemeta P_((void));
static void metawindow P_((void));
static void metarefresh P_((int i));
static void metaaction P_((char *metafile, W_Event *data));
static void metadone P_((void));
static void metainput P_((char *metafile));
static void handle_metafile P_((char *metafile));
static void show_playerlist P_((int sock));
#undef P_
#ifdef FORKNETREK
static void refresh();
static void reaper();
#endif
static int
getnumber(string, start)
char *string;
int start;
{
char temp[BUFSIZ];
int c;
int tc = 0;
for (c = start; c <= strlen(string); c++) {
if ((string[c] >= '0') && (string[c] <= '9')) {
temp[tc++] = string[c];
} else if (tc > 0) {
temp[tc] = '\0';
return (atoi(temp));
}
}
return 0;
}
static char *
flagstring(f)
char f;
{
static char buf[3];
switch(f){
case F_HOCKEY:
return "Hockey";
case F_BRONCO:
return "Standard";
case F_DOGFIGHT:
return "Dogfight";
case F_CHAOS:
return "Chaos";
case F_INL_HOME:
return "INL home";
case F_INL_AWAY:
return "INL away";
default:
buf[0] = f;
buf[1] = 0;
return buf;
}
}
/*
* The connection to the metaserver is by Andy McFadden. This calls the
* metaserver and parses the output into something useful
*/
int
open_port(host, port, verbose)
char *host;
int port;
int verbose;
{
struct sockaddr_in addr;
struct hostent *hp;
int sock;
/* Connect to the metaserver */
/* get numeric form */
if ((addr.sin_addr.s_addr = inet_addr(host)) == -1) {
if ((hp = gethostbyname(host)) == NULL) {
if (verbose)
fprintf(stderr, "netrek: unknown host '%s'\n", host);
return (-1);
} else {
addr.sin_addr.s_addr = *(long *) hp->h_addr;
}
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
if (verbose)
perror("socket");
return (-1);
}
if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) < 0) {
if (verbose){
perror("connect");
}
fprintf(stderr, "%s: not listening or network failure.\n", host);
close(sock);
return (-1);
}
return (sock);
}
void
read_sock(sock)
int sock;
{
char line[BUFSIZ];
FILE *fsock;
/* let internal buffer code handle the line splitting */
fsock = fdopen(sock, "r");
while (fgets(line, BUFSIZ - 1, fsock)) {
printf("%s", line);
}
fclose(fsock);
}
static void
readmeta(fsock, cache_read)
FILE *fsock;
int cache_read;
{
register int i;
char *numstr;
char line[BUFSIZ];
char misc[BUFSIZ];
FILE *fo = NULL;
if(cache_read == NO_CACHE){
char *home = getenv("HOME");
/* we're reading the metaserver, write out cache */
if(home){
sprintf(misc, "%s/%s", home, META_CACHE);
fo = fopen(misc, "w");
}
}
while (fgets(line, BUFSIZ - 1, fsock)) {
if(fo)
fprintf(fo, "%s", line);
if (sscanf(line, "-h %s -p %d %d %[^\n]", /* parse line */
serverlist[num_servers].address,
&serverlist[num_servers].port,
&serverlist[num_servers].time,
misc) == 4) {
serverlist[num_servers].status = -1;
/*
* I don't have it checking the servers with nobody playing because
* the menu window would then be too large to fit on a 1024 x 768
* window. :( - NBT
*/
if(cache_read == READ_OLD_CACHE)
serverlist[num_servers].status = KEY_NORESP;
else for (i = 0; i < KEYS; i++) {
if ((numstr = strstr(misc, keystrings[i]))) {
serverlist[num_servers].status = i;
serverlist[num_servers].players = getnumber(numstr, 0);
break;
}
}
serverlist[num_servers].flag = misc[strlen(misc) - 1];
if (serverlist[num_servers].flag == F_PARADISE)
/* can't handle paradise servers */
serverlist[num_servers].status = -1;
if (strrchr(misc + 15, 'R'))
serverlist[num_servers].RSA_client = 1;
else
serverlist[num_servers].RSA_client = 0;
#if 0 /* concept of default server and you don't play anywhere else
is contrary to netrek philosophy :) */
/* check default */
if (strcmpi(serverlist[num_servers].address, serverName) == 0
&& serverlist[num_servers].port == xtrekPort){
serverlist[num_servers].def = 1;
def_found = 1;
}
#endif
switch (serverlist[num_servers].status) {
case KEY_OPEN:
case KEY_WAITQ:
case KEY_NOBODY:
case KEY_NORESP:
case KEY_GARBAGE:
num_servers++; /* It's valid */
break;
default:
/* assuming that KEY_UNKNOWN does indeed mean somethings
wrong with the server */
break;
}
if(num_servers == MAX_SERVERS-1){
fprintf(stderr, "Too many servers (> %d)\n", MAX_SERVERS);
fprintf(stderr, "Client needs fixed.n");
break;
}
}
}
if(fo)
fclose(fo);
fclose(fsock);
}
static FILE *
check_cache(last_cache)
int *last_cache;
{
struct stat mstat;
char metacache[BUFSIZ];
char *home = getenv("HOME");
if(intDefault("metaCacheDisable", 0))
return NULL;
if(!home)
return NULL;
sprintf(metacache, "%s/%s", home, META_CACHE);
if(stat(metacache, &mstat) < 0)
return NULL;
if(!mstat.st_size)
return NULL;
*last_cache = time(NULL) - mstat.st_mtime;
return fopen(metacache, "r");
}
static void
parsemeta()
{
int sock;
int last_cache = 0;
FILE *fi;
fi = check_cache(&last_cache);
/* read in last 5 minutes */
if(fi && last_cache < 300){
/* use the cached value */
printf("netrek: using cached value from last 5 minutes ...\n");
readmeta(fi, READ_CACHE);
}
else if ((sock = open_port(metaserver, metaport, 1)) > 0) {
if(fi)
fclose(fi);
/* let internal buffer code handle the line splitting */
fi = fdopen(sock, "r");
readmeta(fi, NO_CACHE);
} else if(fi){
/* metaserver screwed so use the cached value but blank out the player
values, we don't know what they are */
printf("netrek: can't connect to metaserver %s at port %d\n",
metaserver, metaport);
printf(" using old cached value ...\n");
readmeta(fi, READ_OLD_CACHE);
}
else{
/* gotta fail */
fprintf(stderr, "netrek: can't connect to metaserver %s at port %d\n",
metaserver, metaport);
exit(0);
}
}
/* Show the meta server menu window */
static void
metawindow()
{
register int i,l;
char *s;
if(!metaWin){
metaWin = W_MakeMenu("MetaServer List", 0, 0, 72, num_servers + BUTTONS,
NULL, 2);
W_DefineArrowCursor(metaWin);
}
else{
W_ReinitMenu(metaWin, 72, num_servers + BUTTONS);
W_ResizeMenu(metaWin, 72, num_servers + BUTTONS);
}
s = "Button 1 -- CONNECT Button 3 -- PLAYER LIST";
l = strlen(s);
W_WriteText(metaWin, (72-l)/2, 0, W_Yellow, s, l, W_RegularFont);
for (i = 0; i < num_servers; i++)
metarefresh(i);
#ifdef FORKNETREK
/* Add refresh option */
s = "R E F R E S H";
l = strlen(s);
W_WriteText(metaWin, (72-l)/2, num_servers+BUTTONS-2, W_Green, s, l,
W_RegularFont);
#endif
/* Add quit option */
s = "Q U I T";
l = strlen(s);
W_WriteText(metaWin, (72-l)/2, num_servers+BUTTONS-1, W_Yellow, s, l,
W_RegularFont);
/* Map window */
W_MapWindow(metaWin);
}
/* Refresh item i */
static void
metarefresh(i)
int i;
{
char buf[BUFSIZ];
int color = textColor;
switch (serverlist[i].status) {
case KEY_OPEN: /* open */
case KEY_WAITQ: /* wait q */
color = W_Cyan;
sprintf(buf, "%-40s %12s %-5d %s",
serverlist[i].address,
keystrings[serverlist[i].status],
serverlist[i].players,
flagstring(serverlist[i].flag));
break;
case KEY_NOBODY:
sprintf(buf, "%-40s %12s %s",
serverlist[i].address,
"Empty",
flagstring(serverlist[i].flag));
break;
case KEY_NORESP:
case KEY_GARBAGE:
case KEY_UNKNOWN:
default:
sprintf(buf, "%-40s %12s %s",
serverlist[i].address,
"Unknown",
flagstring(serverlist[i].flag));
break;
}
W_WriteText(metaWin, 0, i+1, color, buf, strlen(buf), W_RegularFont);
}
/* Check selection to see if was valid. If it was then we have a winner! */
static void
metaaction(metafile, data)
char *metafile;
W_Event *data;
{
int sock;
#ifdef TREKHOPD
extern int use_trekhopd;
#endif
int use_observer_port = 0;
if ((data->y >= 0) && (data->y < num_servers)) {
xtrekPort = serverlist[data->y].port;
serverName = strdup(serverlist[data->y].address);/* leak xxx */
#ifdef RSA
RSA_Client = serverlist[data->y].RSA_client;
#endif
serverAlias = getalias(serverName);
switch(data->key){
case W_RBUTTON:
W_DefineWaitCursor(metaWin);
W_Flush();
if((sock = open_port(serverName, xtrekPort-1, 1)) < 0) {
W_DefineArrowCursor(metaWin);
break;
}
#ifdef nodef
printf("\n%s =========================\n",
strcap(serverName)/* distress.c */);
read_sock(sock);
#endif
show_playerlist(sock);
/* close(sock); */
W_DefineArrowCursor(metaWin);
break;
case W_MBUTTON:
use_observer_port = 1;
/* fall through */
case W_LBUTTON:
W_DefineWaitCursor(metaWin);
W_Flush();
#ifdef TREKHOPD
if(use_trekhopd) goto metadone_label; /* argh */
#endif
if ((sock = open_port(serverName, xtrekPort, 1)) < 0) {
serverlist[data->y].status = KEY_NORESP;
metarefresh(data->y);
close(sock);
W_DefineArrowCursor(metaWin);
} else {
close(sock);
#ifdef TREKHOPD
metadone_label:;
#endif
metadone();
#ifdef FORKNETREK
if(fork() == 0){
char *args[32];
int count = 0;
/* recommended in comp.windows.x faq */
W_ChildCloseDisplay();
/* get out of this process group so ^C on metawindow
doesn't kill everything spawned */
setpgid(0, 0);
args[count++] = program;
if(use_observer_port)
xtrekPort++;
addComLineArgs(args, &count);
execvp(program, args);
perror("execvp");
_exit(1);
}
W_DefineArrowCursor(metaWin);
/* allow 3 seperate wait queues for geometry specification */
waitnum ++ ; if(waitnum > 3) waitnum = 0;
#endif
}
break;
}
}
#ifdef FORKNETREK
else if (!metafile && data->y == num_servers && data->key == W_LBUTTON)
refresh();
#endif
else if (data->y == num_servers + (BUTTONS - 2) &&
data->key == W_LBUTTON) /* quit */
exit(0);
}
/* Unmap the metaWindow */
static void
metadone()
{
#ifndef FORKNETREK
/* Unmap window */
W_DestroyWindow(metaWin);
metaWin = NULL;
if(playerWin)
W_DestroyWindow(playerWin);
W_Flush();
if (serverlist)
free((void *) serverlist);
quit_meta = 1;
#endif
}
/*
* My own little input() function. I needed this so I don't have to use all
* the bull in the main input(). Plus to use it I'd have to call mapAll()
* first and the client would read in the default server and then call it up
* before I can select a server.
*/
static void
metainput(metafile)
char *metafile;
{
W_Event data;
while (!quit_meta) {
W_NextEvent(&data);
data.y --; /* skip the title */
switch ((int) data.type) {
case W_EV_KEY:
#if 0
if (data.Window == metaWin)
metaaction(metafile, &data);
#endif
break;
case W_EV_BUTTON:
if (data.Window == metaWin)
metaaction(metafile, &data);
break;
case W_EV_EXPOSE:
break;
default:
break;
}
}
}
static void
handle_metafile(metafile)
char *metafile;
{
FILE *fi;
fi = fopen(metafile, "r");
if(!fi){
perror(metafile);
return;
}
readmeta(fi, READ_OLD_CACHE);
/* fi closed by readmeta */
}
void
do_metaserver(metafile)
char *metafile;
{
char *s;
#ifdef FORKNETREK
SIGNAL(SIGCHLD, reaper);
#endif
serverlist = (struct servers *) malloc(sizeof(struct servers) * MAX_SERVERS);
MZERO((void *) serverlist, sizeof(struct servers) * MAX_SERVERS);
if(metafile){
/* tots' suggestion: use metafile as metaserver -- store INL
servers and such */
handle_metafile(metafile);
}
else {
/* get metaserver defaults */
if ((s = getdefault("metaserver")))
metaserver = s;
metaport = intDefault("metaport", metaport);
#ifdef SHOW_DEFAULTS
show_defaults("C-startup", "metaserver", metaserver,
"Metaserver host to use for -m startup option.");
show_defaults("C-startup", "metaport", metaport,
"Metaserver port to use for -m startup option.");
#endif
/* get input */
parsemeta();
}
/* put up window */
metawindow();
/* wait for input */
metainput(metafile);
}
#ifdef FORKNETREK
static void
refresh()
{
W_DefineWaitCursor(metaWin);
W_Flush();
if(serverlist)
free((char *)serverlist);
num_servers = 0;
serverlist = (struct servers *) malloc(sizeof(struct servers) * MAX_SERVERS);
MZERO((void *) serverlist, sizeof(struct servers) * MAX_SERVERS);
parsemeta();
metawindow();
W_DefineArrowCursor(metaWin);
}
static void
reaper(sig)
int sig;
{
int pid;
#ifdef SVR4
while ((pid=waitpid(-1, 0, WNOHANG)) > 0)
#else
while ((pid=wait3((union wait *) 0, WNOHANG, (struct rusage *) 0)) > 0)
#endif
;
}
#endif
static void
show_playerlist(sock)
int sock;
{
char *s;
register l;
char line[BUFSIZ];
FILE *fsock;
W_Color color;
/* let internal buffer code handle the line splitting */
fsock = fdopen(sock, "r");
if(!playerWin){
playerWin = W_MakeScrollingWindow("Player List",
0, 0,
80, 26, 0, 2);
W_DefineUpDownCursor(playerWin);
}
W_MapWindow(playerWin);
sprintf(line, "%s (%d) ", strcap(serverName), xtrekPort);
l = strlen(line);
while(l < 75)
line[l++] = '=';
line[l] = 0;
W_WriteText(playerWin, 0, 0, W_Yellow, "", 1, W_MesgFont);
W_WriteText(playerWin, (80-l)/2, 0, W_Yellow, line, l, W_MesgFont);
W_WriteText(playerWin, 0, 0, W_Yellow, "", 1, W_MesgFont);
W_FlushScrollingWindow(playerWin);
W_Flush();
while(fgets(line, BUFSIZ-1, fsock)){
if((s=strrchr(line, '\n')))
*s = 0;
/* if playerlist format changes significantly, following will fail.
assumption:
<team letter><number>:
*/
color = textColor;
s = line;
/* simple minded parsing looking for "xx:" */
while(isspace(*s)) s++;
if(s[2] == ':'){
switch(s[0]){
case 'F': color = shipCol[remap[FED]]; break;
case 'R': color = shipCol[remap[ROM]]; break;
case 'O': color = shipCol[remap[ORI]]; break;
case 'K': color = shipCol[remap[KLI]]; break;
case 'I': color = shipCol[remap[IND]]; break;
default: break;
}
}
W_WriteText(playerWin, 0, 0, color, line, strlen(line), W_MesgFont);
}
W_FlushScrollingWindow(playerWin);
fclose(fsock);
}