-
Notifications
You must be signed in to change notification settings - Fork 3
/
authservers.patch
841 lines (809 loc) · 26.9 KB
/
authservers.patch
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
Index: src/engine/server.cpp
===================================================================
--- src/engine/server.cpp (revision 6502)
+++ src/engine/server.cpp (working copy)
@@ -338,172 +338,20 @@
if(c) process(packet, c->num, chan);
}
-#ifdef STANDALONE
-bool resolverwait(const char *name, ENetAddress *address)
-{
- return enet_address_set_host(address, name) >= 0;
-}
+vector<masterserver *> masterlikeservers; // used for socket servicing; contains master and additional auth servers
+authserver *master;
+SVARF(mastername, server::defaultmaster(), {
+ if(!master) return;
+ master->disconnect();
+ copystring(master->hostname, mastername);
+});
+VARF(masterport, 1, server::masterport(), 0xFFFF, {
+ if(!master) return;
+ master->disconnect();
+ master->port = masterport;
+});
+hashnameset<authserver> authservers;
-int connectwithtimeout(ENetSocket sock, const char *hostname, const ENetAddress &remoteaddress)
-{
- return enet_socket_connect(sock, &remoteaddress);
-}
-#endif
-
-ENetSocket mastersock = ENET_SOCKET_NULL;
-ENetAddress masteraddress = { ENET_HOST_ANY, ENET_PORT_ANY }, serveraddress = { ENET_HOST_ANY, ENET_PORT_ANY };
-int lastupdatemaster = 0, lastconnectmaster = 0, masterconnecting = 0, masterconnected = 0;
-vector<char> masterout, masterin;
-int masteroutpos = 0, masterinpos = 0;
-VARN(updatemaster, allowupdatemaster, 0, 1, 1);
-
-void disconnectmaster()
-{
- if(mastersock != ENET_SOCKET_NULL)
- {
- server::masterdisconnected();
- enet_socket_destroy(mastersock);
- mastersock = ENET_SOCKET_NULL;
- }
-
- masterout.setsize(0);
- masterin.setsize(0);
- masteroutpos = masterinpos = 0;
-
- masteraddress.host = ENET_HOST_ANY;
- masteraddress.port = ENET_PORT_ANY;
-
- lastupdatemaster = masterconnecting = masterconnected = 0;
-}
-
-SVARF(mastername, server::defaultmaster(), disconnectmaster());
-VARF(masterport, 1, server::masterport(), 0xFFFF, disconnectmaster());
-
-ENetSocket connectmaster(bool wait)
-{
- if(!mastername[0]) return ENET_SOCKET_NULL;
- if(masteraddress.host == ENET_HOST_ANY)
- {
- if(isdedicatedserver()) logoutf("looking up %s...", mastername);
- masteraddress.port = masterport;
- if(!resolverwait(mastername, &masteraddress)) return ENET_SOCKET_NULL;
- }
- ENetSocket sock = enet_socket_create(ENET_SOCKET_TYPE_STREAM);
- if(sock == ENET_SOCKET_NULL)
- {
- if(isdedicatedserver()) logoutf("could not open master server socket");
- return ENET_SOCKET_NULL;
- }
- if(wait || serveraddress.host == ENET_HOST_ANY || !enet_socket_bind(sock, &serveraddress))
- {
- enet_socket_set_option(sock, ENET_SOCKOPT_NONBLOCK, 1);
- if(wait)
- {
- if(!connectwithtimeout(sock, mastername, masteraddress)) return sock;
- }
- else if(!enet_socket_connect(sock, &masteraddress)) return sock;
- }
- enet_socket_destroy(sock);
- if(isdedicatedserver()) logoutf("could not connect to master server");
- return ENET_SOCKET_NULL;
-}
-
-bool requestmaster(const char *req)
-{
- if(mastersock == ENET_SOCKET_NULL)
- {
- mastersock = connectmaster(false);
- if(mastersock == ENET_SOCKET_NULL) return false;
- lastconnectmaster = masterconnecting = totalmillis ? totalmillis : 1;
- }
-
- if(masterout.length() >= 4096) return false;
-
- masterout.put(req, strlen(req));
- return true;
-}
-
-bool requestmasterf(const char *fmt, ...)
-{
- defvformatstring(req, fmt, fmt);
- return requestmaster(req);
-}
-
-void processmasterinput()
-{
- if(masterinpos >= masterin.length()) return;
-
- char *input = &masterin[masterinpos], *end = (char *)memchr(input, '\n', masterin.length() - masterinpos);
- while(end)
- {
- *end = '\0';
-
- const char *args = input;
- while(args < end && !iscubespace(*args)) args++;
- int cmdlen = args - input;
- while(args < end && iscubespace(*args)) args++;
-
- if(matchstring(input, cmdlen, "failreg"))
- conoutf(CON_ERROR, "master server registration failed: %s", args);
- else if(matchstring(input, cmdlen, "succreg"))
- conoutf("master server registration succeeded");
- else server::processmasterinput(input, cmdlen, args);
-
- end++;
- masterinpos = end - masterin.getbuf();
- input = end;
- end = (char *)memchr(input, '\n', masterin.length() - masterinpos);
- }
-
- if(masterinpos >= masterin.length())
- {
- masterin.setsize(0);
- masterinpos = 0;
- }
-}
-
-void flushmasteroutput()
-{
- if(masterconnecting && totalmillis - masterconnecting >= 60000)
- {
- logoutf("could not connect to master server");
- disconnectmaster();
- }
- if(masterout.empty() || !masterconnected) return;
-
- ENetBuffer buf;
- buf.data = &masterout[masteroutpos];
- buf.dataLength = masterout.length() - masteroutpos;
- int sent = enet_socket_send(mastersock, NULL, &buf, 1);
- if(sent >= 0)
- {
- masteroutpos += sent;
- if(masteroutpos >= masterout.length())
- {
- masterout.setsize(0);
- masteroutpos = 0;
- }
- }
- else disconnectmaster();
-}
-
-void flushmasterinput()
-{
- if(masterin.length() >= masterin.capacity())
- masterin.reserve(4096);
-
- ENetBuffer buf;
- buf.data = masterin.getbuf() + masterin.length();
- buf.dataLength = masterin.capacity() - masterin.length();
- int recv = enet_socket_receive(mastersock, NULL, &buf, 1);
- if(recv > 0)
- {
- masterin.advance(recv);
- processmasterinput();
- }
- else disconnectmaster();
-}
-
static ENetAddress pongaddr;
void sendserverinforeply(ucharbuf &p)
@@ -523,12 +371,16 @@
ENET_SOCKETSET_EMPTY(writeset);
ENetSocket maxsock = pongsock;
ENET_SOCKETSET_ADD(readset, pongsock);
- if(mastersock != ENET_SOCKET_NULL)
+ loopv(masterlikeservers)
{
- maxsock = max(maxsock, mastersock);
- ENET_SOCKETSET_ADD(readset, mastersock);
- if(!masterconnected) ENET_SOCKETSET_ADD(writeset, mastersock);
+ masterserver *m = masterlikeservers[i];
+ if(m->sock != ENET_SOCKET_NULL)
+ {
+ maxsock = max(maxsock, m->sock);
+ ENET_SOCKETSET_ADD(readset, m->sock);
+ if(!m->connected) ENET_SOCKETSET_ADD(writeset, m->sock);
}
+ }
if(lansock != ENET_SOCKET_NULL)
{
maxsock = max(maxsock, lansock);
@@ -552,29 +404,33 @@
server::serverinforeply(req, p);
}
- if(mastersock != ENET_SOCKET_NULL)
+ loopv(masterlikeservers)
{
- if(!masterconnected)
+ masterserver *m = masterlikeservers[i];
+ if(m->sock != ENET_SOCKET_NULL)
{
- if(ENET_SOCKETSET_CHECK(readset, mastersock) || ENET_SOCKETSET_CHECK(writeset, mastersock))
+ if(!m->connected)
{
+ if(ENET_SOCKETSET_CHECK(readset, m->sock) || ENET_SOCKETSET_CHECK(writeset, m->sock))
+ {
int error = 0;
- if(enet_socket_get_option(mastersock, ENET_SOCKOPT_ERROR, &error) < 0 || error)
+ if(enet_socket_get_option(m->sock, ENET_SOCKOPT_ERROR, &error) < 0 || error)
{
- logoutf("could not connect to master server");
- disconnectmaster();
+ logoutf("could not connect to %s:%d (before flushinput), error: %d", m->hostname, m->port, error);
+ m->disconnect();
}
else
{
- masterconnecting = 0;
- masterconnected = totalmillis ? totalmillis : 1;
- server::masterconnected();
+ m->connecting = 0;
+ m->connected = totalmillis ? totalmillis : 1;
+ m->notifygameconnected();
}
}
}
- if(mastersock != ENET_SOCKET_NULL && ENET_SOCKETSET_CHECK(readset, mastersock)) flushmasterinput();
+ if(ENET_SOCKETSET_CHECK(readset, m->sock)) m->flushinput();
}
}
+}
VAR(serveruprate, 0, 0, INT_MAX);
SVAR(serverip, "");
@@ -584,11 +440,13 @@
int curtime = 0, lastmillis = 0, elapsedtime = 0, totalmillis = 0;
#endif
+VARN(updatemaster, allowupdatemaster, 0, 1, 1);
+
void updatemasterserver()
{
- if(!masterconnected && lastconnectmaster && totalmillis-lastconnectmaster <= 5*60*1000) return;
- if(mastername[0] && allowupdatemaster) requestmasterf("regserv %d\n", serverport);
- lastupdatemaster = totalmillis ? totalmillis : 1;
+ if(!master->connected && master->lastconnect && totalmillis-master->lastconnect <= 5*60*1000) return;
+ if(master->hostname[0] && allowupdatemaster) master->requestf("regserv %d\n", serverport);
+ master->lastupdate = totalmillis ? totalmillis : 1;
}
uint totalsecs = 0;
@@ -630,10 +488,11 @@
}
server::serverupdate();
- flushmasteroutput();
+ loopv(masterlikeservers) masterlikeservers[i]->flushoutput();
+
checkserversockets();
- if(!lastupdatemaster || totalmillis-lastupdatemaster>60*60*1000) // send alive signal to masterserver every hour of uptime
+ if(!master->lastupdate || totalmillis-master->lastupdate>60*60*1000) // send alive signal to master every hour of uptime
updatemasterserver();
if(totalmillis-laststatus>60*1000) // display bandwidth stats, useful for server ops
@@ -1033,6 +892,8 @@
return false;
}
+ENetAddress serveraddress = { ENET_HOST_ANY, ENET_PORT_ANY };
+
bool setuplistenserver(bool dedicated)
{
ENetAddress address = { ENET_HOST_ANY, enet_uint16(serverport <= 0 ? server::serverport() : serverport) };
@@ -1062,6 +923,14 @@
}
if(lansock == ENET_SOCKET_NULL) conoutf(CON_WARN, "WARNING: could not create LAN server info socket");
else enet_socket_set_option(lansock, ENET_SOCKOPT_NONBLOCK, 1);
+ if(!master && mastername && masterport)
+ {
+ master = &authservers[""];
+ copystring(master->hostname, mastername);
+ master->port = masterport;
+ master->privilege = PRIV_AUTH;
+ masterlikeservers.add(master);
+ }
return true;
}
@@ -1119,6 +988,7 @@
COMMAND(stoplistenserver, "");
#endif
+extern char *mastername;
bool serveroption(char *opt)
{
switch(opt[1])
Index: src/engine/serverbrowser.cpp
===================================================================
--- src/engine/serverbrowser.cpp (revision 6502)
+++ src/engine/serverbrowser.cpp (working copy)
@@ -617,6 +617,37 @@
#define RETRIEVELIMIT 20000
+ENetAddress masteraddress = { ENET_HOST_ANY, ENET_PORT_ANY };
+
+ENetSocket connectmaster(bool wait)
+ {
+ if(!mastername[0]) return ENET_SOCKET_NULL;
+ if(masteraddress.host == ENET_HOST_ANY)
+ {
+ if(isdedicatedserver()) logoutf("looking up %s...", mastername);
+ masteraddress.port = masterport;
+ if(!resolverwait(mastername, &masteraddress)) return ENET_SOCKET_NULL;
+ }
+ ENetSocket sock = enet_socket_create(ENET_SOCKET_TYPE_STREAM);
+ if(sock == ENET_SOCKET_NULL)
+ {
+ if(isdedicatedserver()) logoutf("could not open master server socket");
+ return ENET_SOCKET_NULL;
+ }
+ if(wait || serveraddress.host == ENET_HOST_ANY || !enet_socket_bind(sock, &serveraddress))
+ {
+ enet_socket_set_option(sock, ENET_SOCKOPT_NONBLOCK, 1);
+ if(wait)
+ {
+ if(!connectwithtimeout(sock, mastername, masteraddress)) return sock;
+ }
+ else if(!enet_socket_connect(sock, &masteraddress)) return sock;
+ }
+ enet_socket_destroy(sock);
+ if(isdedicatedserver()) logoutf("could not connect to master server");
+ return ENET_SOCKET_NULL;
+}
+
void retrieveservers(vector<char> &data)
{
ENetSocket sock = connectmaster(true);
Index: src/fpsgame/game.h
===================================================================
--- src/fpsgame/game.h (revision 6502)
+++ src/fpsgame/game.h (working copy)
@@ -214,8 +214,6 @@
// network messages codes, c2s, c2c, s2c
-enum { PRIV_NONE = 0, PRIV_MASTER, PRIV_AUTH, PRIV_ADMIN };
-
enum
{
N_CONNECT = 0, N_SERVINFO, N_WELCOME, N_INITCLIENT, N_POS, N_TEXT, N_SOUND, N_CDIS,
Index: src/fpsgame/server.cpp
===================================================================
--- src/fpsgame/server.cpp (revision 6502)
+++ src/fpsgame/server.cpp (working copy)
@@ -17,8 +17,6 @@
VAR(regenbluearmour, 0, 1, 1);
-extern ENetAddress masteraddress;
-
namespace server
{
struct server_entity // server side version of "entity" type
@@ -2712,6 +2710,22 @@
}
} ipbans, gbans;
+ bool processmasterserverinput(const char *input, int cmdlen, const char *args)
+ {
+ string val;
+ if(matchstring(input, cmdlen, "cleargbans"))
+ {
+ gbans.clear();
+ return true;
+ }
+ else if(sscanf(input, "addgban %100s", val) == 1)
+ {
+ gbans.add(val);
+ return true;
+ }
+ return false;
+ }
+
bool checkbans(uint ip)
{
loopv(bannedips) if(bannedips[i].ip==ip) return true;
@@ -2754,9 +2768,36 @@
return ci && ci->connected;
}
- clientinfo *findauth(uint id)
+ authserver *addauthserver(const char *keydomain, const char *hostname, int *port, const char *priv = "m")
{
- loopv(clients) if(clients[i]->authreq == id) return clients[i];
+ authserver &a = authservers[keydomain];
+ copystring(a.name, keydomain);
+ copystring(a.hostname, hostname);
+ a.port = *port;
+ switch(priv[0])
+ {
+ case 'a': case 'A': a.privilege = PRIV_ADMIN; break;
+ case 'm': case 'M': default: a.privilege = PRIV_AUTH; break;
+ case 'n': case 'N': a.privilege = PRIV_NONE; break;
+ }
+ masterlikeservers.add(&a);
+ return &a;
+ }
+ COMMAND(addauthserver, "ssisi");
+
+ bool requestauthserverf(const char *keydomain, const char *fmt, ...)
+ {
+ keydomain = newstring(keydomain);
+ authserver *a = authservers.access(keydomain);
+ if(!a) conoutf("no auth server for domain '%s'", keydomain);
+ if(!a) return false;
+ defvformatstring(req, fmt, fmt);
+ return a->request(req);
+ }
+
+ clientinfo *findauth(uint id, const char *desc)
+ {
+ loopv(clients) if(clients[i]->authreq == id && !strcmp(clients[i]->authdesc, desc)) return clients[i];
return NULL;
}
@@ -2768,29 +2809,38 @@
if(ci->connectauth) disconnect_client(ci->clientnum, ci->connectauth);
}
- void authfailed(uint id)
+ void authfailed(uint id, const char *desc)
{
- authfailed(findauth(id));
+ authfailed(findauth(id, desc));
}
- void authsucceeded(uint id)
+ void authsucceeded(uint id, const char *desc)
{
- clientinfo *ci = findauth(id);
+ clientinfo *ci = findauth(id, desc);
if(!ci) return;
+ authserver *a = authservers.access(desc);
+ if(!a) return;
ci->cleanauth(ci->connectauth!=0);
if(ci->connectauth) connected(ci);
if(ci->authkickvictim >= 0)
{
- if(setmaster(ci, true, "", ci->authname, NULL, PRIV_AUTH, false, true))
- trykick(ci, ci->authkickvictim, ci->authkickreason, ci->authname, NULL, PRIV_AUTH);
+ if(setmaster(ci, true, "", ci->authname, ci->authdesc, a->privilege, false, true))
+ trykick(ci, ci->authkickvictim, ci->authkickreason, ci->authname, ci->authdesc, a->privilege);
ci->cleanauthkick();
}
- else setmaster(ci, true, "", ci->authname, NULL, PRIV_AUTH);
+ else if(ci->privilege >= a->privilege)
+ {
+ string msg;
+ if(desc && desc[0]) formatstring(msg, "%s authenticated as '\fs\f5%s\fr' [\fs\f0%s\fr]", colorname(ci), ci->authname, desc);
+ else formatstring(msg, "%s authenticated as '\fs\f5%s\fr'", colorname(ci), ci->authname);
+ sendf(-1, 1, "ris", N_SERVMSG, msg);
}
+ else setmaster(ci, true, "", ci->authname, ci->authdesc, a->privilege);
+ }
- void authchallenged(uint id, const char *val, const char *desc = "")
+ void authchallenged(uint id, const char *val, const char *desc)
{
- clientinfo *ci = findauth(id);
+ clientinfo *ci = findauth(id, desc);
if(!ci) return;
sendf(ci->clientnum, 1, "risis", N_AUTHCHAL, desc, id, val);
}
@@ -2804,7 +2854,7 @@
ci->authreq = nextauthreq++;
filtertext(ci->authname, user, false, false, 100);
copystring(ci->authdesc, desc);
- if(ci->authdesc[0])
+ if(desc[0] && !strcmp(desc, serverauth))
{
userinfo *u = users.access(userkey(ci->authname, ci->authdesc));
if(u)
@@ -2816,7 +2866,7 @@
}
else ci->cleanauth();
}
- else if(!requestmasterf("reqauth %u %s\n", ci->authreq, ci->authname))
+ else if(!requestauthserverf(desc, "reqauth %u %s\n", ci->authreq, ci->authname))
{
ci->cleanauth();
sendf(ci->clientnum, 1, "ris", N_SERVMSG, "not connected to authentication server");
@@ -2837,7 +2887,7 @@
{
if(!isxdigit(*s)) { *s = '\0'; break; }
}
- if(desc[0])
+ if(desc[0] && !strcmp(desc, serverauth))
{
if(ci->authchallenge && checkchallenge(val, ci->authchallenge))
{
@@ -2855,7 +2905,7 @@
}
ci->cleanauth();
}
- else if(!requestmasterf("confauth %u %s\n", id, val))
+ else if(!requestauthserverf(desc, "confauth %u %s\n", id, val))
{
ci->cleanauth();
sendf(ci->clientnum, 1, "ris", N_SERVMSG, "not connected to authentication server");
@@ -2863,33 +2913,27 @@
return ci->authreq || !ci->connectauth;
}
- void masterconnected()
- {
- }
+ void authserverconnected(const char *keydomain) {}
- void masterdisconnected()
+ void authserverdisconnected(const char *keydomain)
{
loopvrev(clients)
{
clientinfo *ci = clients[i];
- if(ci->authreq) authfailed(ci);
+ if(ci->authreq && !strcmp(ci->authdesc, keydomain)) authfailed(ci);
}
}
- void processmasterinput(const char *cmd, int cmdlen, const char *args)
+ void processauthserverinput(const char *desc, const char *cmd, int cmdlen, const char *args)
{
uint id;
string val;
if(sscanf(cmd, "failauth %u", &id) == 1)
- authfailed(id);
+ authfailed(id, desc);
else if(sscanf(cmd, "succauth %u", &id) == 1)
- authsucceeded(id);
+ authsucceeded(id, desc);
else if(sscanf(cmd, "chalauth %u %255s", &id, val) == 2)
- authchallenged(id, val);
- else if(matchstring(cmd, cmdlen, "cleargbans"))
- gbans.clear();
- else if(sscanf(cmd, "addgban %100s", val) == 1)
- gbans.add(val);
+ authchallenged(id, val, desc);
}
void receivefile(int sender, uchar *data, int len)
Index: src/p1xbraten/authservers.h
===================================================================
--- src/p1xbraten/authservers.h (nonexistent)
+++ src/p1xbraten/authservers.h (working copy)
@@ -0,0 +1,28 @@
+#include "masterserver.h"
+
+enum { PRIV_NONE = 0, PRIV_MASTER, PRIV_AUTH, PRIV_ADMIN };
+
+struct authserver : masterserver {
+ string name; // = key domain
+ int privilege = PRIV_NONE;
+
+ void notifygameconnected()
+ {
+ server::authserverconnected(name);
+ }
+
+ void notifygamedisconnected()
+ {
+ server::authserverdisconnected(name);
+ }
+
+ bool notifygameinput(const char *input, int cmdlen, const char *args)
+ {
+ if(!name[0] && masterserver::notifygameinput(input, cmdlen, args)) return true; // master registration, gbans
+ server::processauthserverinput(name, input, cmdlen, args);
+ return true;
+ }
+};
+
+extern authserver *master;
+extern hashnameset<authserver> authservers;
\ No newline at end of file
Index: src/shared/cube.h
===================================================================
--- src/shared/cube.h (revision 6502)
+++ src/shared/cube.h (working copy)
@@ -64,5 +64,7 @@
#include "iengine.h"
#include "igame.h"
+#include "authservers.h"
+
#endif
Index: src/shared/iengine.h
===================================================================
--- src/shared/iengine.h (revision 6502)
+++ src/shared/iengine.h (working copy)
@@ -473,8 +473,6 @@
extern bool hasnonlocalclients();
extern bool haslocalclients();
extern void sendserverinforeply(ucharbuf &p);
-extern bool requestmaster(const char *req);
-extern bool requestmasterf(const char *fmt, ...) PRINTFARGS(1, 2);
extern bool isdedicatedserver();
// client
Index: src/shared/igame.h
===================================================================
--- src/shared/igame.h (revision 6502)
+++ src/shared/igame.h (working copy)
@@ -121,9 +121,10 @@
extern int serverport(int infoport = -1);
extern const char *defaultmaster();
extern int masterport();
- extern void processmasterinput(const char *cmd, int cmdlen, const char *args);
- extern void masterconnected();
- extern void masterdisconnected();
+ extern bool processmasterserverinput(const char *input, int cmdlen, const char *args);
+ extern void authserverconnected(const char *keydomain);
+ extern void processauthserverinput(const char *keydomain, const char *input, int cmdlen, const char *args);
+ extern void authserverdisconnected(const char *keydomain);
extern bool ispaused();
extern int scaletime(int t);
}
Index: src/p1xbraten/masterserver.h
===================================================================
--- src/p1xbraten/masterserver.h (nonexistent)
+++ src/p1xbraten/masterserver.h (working copy)
@@ -0,0 +1,186 @@
+#include "cube.h"
+
+extern ENetAddress serveraddress;
+
+#ifndef STANDALONE
+extern bool resolverwait(const char *name, ENetAddress *address);
+#else
+static inline bool resolverwait(const char *name, ENetAddress *address)
+{
+ return enet_address_set_host(address, name) >= 0;
+}
+#endif
+
+struct masterserver {
+ string hostname;
+ int port;
+ ENetAddress address;
+ ENetSocket sock;
+ int connecting, connected, lastconnect, lastupdate;
+ vector<char> in, out;
+ int inpos, outpos;
+
+ masterserver() : port(0),
+#ifdef __clang__
+ address((ENetAddress){ENET_HOST_ANY, ENET_PORT_ANY}),
+#else
+ address({ENET_HOST_ANY, ENET_PORT_ANY}),
+#endif
+ sock(ENET_SOCKET_NULL), connecting(0), connected(0), lastconnect(0), lastupdate(0), inpos(0), outpos(0)
+ {
+ hostname[0] = 0;
+ }
+
+ void disconnect()
+ {
+ if(sock != ENET_SOCKET_NULL)
+ {
+ notifygamedisconnected();
+ enet_socket_destroy(sock);
+ sock = ENET_SOCKET_NULL;
+ }
+
+ out.setsize(0);
+ in.setsize(0);
+ outpos = inpos = 0;
+
+ address.host = ENET_HOST_ANY;
+ address.port = ENET_PORT_ANY;
+
+ lastupdate = connecting = connected = 0;
+ }
+
+ void connect()
+ {
+ if(sock!=ENET_SOCKET_NULL) return;
+ if(!hostname[0]) return;
+ if(address.host == ENET_HOST_ANY)
+ {
+ if(isdedicatedserver()) logoutf("looking up %s...", hostname);
+ address.port = port;
+ if(!resolverwait(hostname, &address)) return;
+ }
+ sock = enet_socket_create(ENET_SOCKET_TYPE_STREAM);
+ if(sock == ENET_SOCKET_NULL)
+ {
+ if(isdedicatedserver()) logoutf("could not open socket for %s:%d", hostname, port);
+ return;
+ }
+ if(serveraddress.host == ENET_HOST_ANY || !enet_socket_bind(sock, &serveraddress))
+ {
+ enet_socket_set_option(sock, ENET_SOCKOPT_NONBLOCK, 1);
+ if(!enet_socket_connect(sock, &address)) return;
+ }
+ enet_socket_destroy(sock);
+ if(isdedicatedserver()) logoutf("could not connect to %s:%d", hostname, port);
+ sock = ENET_SOCKET_NULL;
+ return;
+ }
+
+ bool request(const char *req)
+ {
+ if(sock == ENET_SOCKET_NULL)
+ {
+ connect();
+ if(sock == ENET_SOCKET_NULL) return false;
+ lastconnect = connecting = totalmillis ? totalmillis : 1;
+ }
+
+ if(out.length() >= 4096) return false;
+
+ out.put(req, strlen(req));
+ return true;
+ }
+
+ bool requestf(const char *fmt, ...)
+ {
+ defvformatstring(req, fmt, fmt);
+ return request(req);
+ }
+
+ void processinput()
+ {
+ if(inpos >= in.length()) return;
+
+ char *input = &in[inpos], *end = (char *)memchr(input, '\n', in.length() - inpos);
+ while(end)
+ {
+ *end = '\0';
+
+ const char *args = input;
+ while(args < end && !iscubespace(*args)) args++;
+ int cmdlen = args - input;
+ while(args < end && iscubespace(*args)) args++;
+
+ if(matchstring(input, cmdlen, "failreg"))
+ conoutf(CON_ERROR, "master server registration failed: %s", args);
+ else if(matchstring(input, cmdlen, "succreg"))
+ conoutf("master server registration succeeded");
+ else notifygameinput(input, cmdlen, args);
+
+ end++;
+ inpos = end - in.getbuf();
+ input = end;
+ end = (char *)memchr(input, '\n', in.length() - inpos);
+ }
+
+ if(inpos >= in.length())
+ {
+ in.setsize(0);
+ inpos = 0;
+ }
+ }
+
+ void flushoutput()
+ {
+ if(connecting && totalmillis - connecting >= 60000)
+ {
+ logoutf("could not connect to %s:%d", hostname, port);
+ disconnect();
+ }
+ if(out.empty() || !connected) return;
+
+ ENetBuffer buf;
+ buf.data = &out[outpos];
+ buf.dataLength = out.length() - outpos;
+ int sent = enet_socket_send(sock, NULL, &buf, 1);
+ if(sent >= 0)
+ {
+ outpos += sent;
+ if(outpos >= out.length())
+ {
+ out.setsize(0);
+ outpos = 0;
+ }
+ }
+ else disconnect();
+ }
+
+ void flushinput()
+ {
+ if(in.length() >= in.capacity())
+ in.reserve(4096);
+
+ ENetBuffer buf;
+ buf.data = in.getbuf() + in.length();
+ buf.dataLength = in.capacity() - in.length();
+ int recv = enet_socket_receive(sock, NULL, &buf, 1);
+ if(recv > 0)
+ {
+ in.advance(recv);
+ processinput();
+ }
+ else disconnect();
+ }
+
+ virtual void notifygameconnected() {}
+ virtual void notifygamedisconnected() {}
+ virtual bool notifygameinput(const char *input, int cmdlen, const char *args)
+ {
+ return server::processmasterserverinput(input, cmdlen, args);
+ }
+};
+
+extern char *mastername;
+extern int masterport;
+extern vector<masterserver *> masterlikeservers;