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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
/* Modified by Teelevision for zCatch/TeeVi, see readme.txt and license.txt. */
#include <base/math.h>
#include <base/system.h>
#include <engine/config.h>
#include <engine/console.h>
#include <engine/engine.h>
#include <engine/map.h>
#include <engine/masterserver.h>
#include <engine/server.h>
#include <engine/storage.h>
#include <engine/shared/compression.h>
#include <engine/shared/config.h>
#include <engine/shared/datafile.h>
#include <engine/shared/demo.h>
#include <engine/shared/econ.h>
#include <engine/shared/filecollection.h>
#include <engine/shared/mapchecker.h>
#include <engine/shared/netban.h>
#include <engine/shared/network.h>
#include <engine/shared/packer.h>
#include <engine/shared/protocol.h>
#include <engine/shared/snapshot.h>
#include <engine/shared/fifoconsole.h>
#include <mastersrv/mastersrv.h>
#include "register.h"
#include "server.h"
#include <string.h>
#include <string>
#include <map>
#include <game/server/gamecontext.h>
#if defined(CONF_FAMILY_WINDOWS)
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
static const char *StrUTF8Ltrim(const char *pStr)
{
while(*pStr)
{
const char *pStrOld = pStr;
int Code = str_utf8_decode(&pStr);
// check if unicode is not empty
if(Code > 0x20 && Code != 0xA0 && Code != 0x034F && Code != 0x2800 &&
(Code < 0x2000 || Code > 0x200F) && (Code < 0x2028 || Code > 0x202F) &&
(Code < 0x205F || Code > 0x2064) && (Code < 0x206A || Code > 0x206F) && (Code < 0xFE00 || Code > 0xFE0F) &&
Code != 0xFEFF && (Code < 0xFFF9 || Code > 0xFFFC))
{
return pStrOld;
}
}
return pStr;
}
static void StrUTF8Rtrim(char *pStr)
{
const char *p = pStr;
const char *pEnd = 0;
while(*p)
{
const char *pStrOld = p;
int Code = str_utf8_decode(&p);
// check if unicode is not empty
if(Code > 0x20 && Code != 0xA0 && Code != 0x034F && Code != 0x2800 &&
(Code < 0x2000 || Code > 0x200F) && (Code < 0x2028 || Code > 0x202F) &&
(Code < 0x205F || Code > 0x2064) && (Code < 0x206A || Code > 0x206F) && (Code < 0xFE00 || Code > 0xFE0F) &&
Code != 0xFEFF && (Code < 0xFFF9 || Code > 0xFFFC))
{
pEnd = 0;
}
else if(pEnd == 0)
pEnd = pStrOld;
}
if(pEnd != 0)
*(const_cast<char *>(pEnd)) = 0;
}
CSnapIDPool::CSnapIDPool()
{
Reset();
}
void CSnapIDPool::Reset()
{
for(int i = 0; i < MAX_IDS; i++)
{
m_aIDs[i].m_Next = i+1;
m_aIDs[i].m_State = 0;
}
m_aIDs[MAX_IDS-1].m_Next = -1;
m_FirstFree = 0;
m_FirstTimed = -1;
m_LastTimed = -1;
m_Usage = 0;
m_InUsage = 0;
}
void CSnapIDPool::RemoveFirstTimeout()
{
int NextTimed = m_aIDs[m_FirstTimed].m_Next;
// add it to the free list
m_aIDs[m_FirstTimed].m_Next = m_FirstFree;
m_aIDs[m_FirstTimed].m_State = 0;
m_FirstFree = m_FirstTimed;
// remove it from the timed list
m_FirstTimed = NextTimed;
if(m_FirstTimed == -1)
m_LastTimed = -1;
m_Usage--;
}
int CSnapIDPool::NewID()
{
int64 Now = time_get();
// process timed ids
while(m_FirstTimed != -1 && m_aIDs[m_FirstTimed].m_Timeout < Now)
RemoveFirstTimeout();
int ID = m_FirstFree;
dbg_assert(ID != -1, "id error");
if(ID == -1)
return ID;
m_FirstFree = m_aIDs[m_FirstFree].m_Next;
m_aIDs[ID].m_State = 1;
m_Usage++;
m_InUsage++;
return ID;
}
void CSnapIDPool::TimeoutIDs()
{
// process timed ids
while(m_FirstTimed != -1)
RemoveFirstTimeout();
}
void CSnapIDPool::FreeID(int ID)
{
if(ID < 0)
return;
dbg_assert(m_aIDs[ID].m_State == 1, "id is not alloced");
m_InUsage--;
m_aIDs[ID].m_State = 2;
m_aIDs[ID].m_Timeout = time_get()+time_freq()*5;
m_aIDs[ID].m_Next = -1;
if(m_LastTimed != -1)
{
m_aIDs[m_LastTimed].m_Next = ID;
m_LastTimed = ID;
}
else
{
m_FirstTimed = ID;
m_LastTimed = ID;
}
}
void CServerBan::InitServerBan(IConsole *pConsole, IStorage *pStorage, CServer* pServer)
{
CNetBan::Init(pConsole, pStorage);
m_pServer = pServer;
// overwrites base command, todo: improve this
Console()->Register("ban", "s?ir", CFGFLAG_SERVER|CFGFLAG_STORE, ConBanExt, this, "Ban player with ip/client id for x minutes for any reason");
}
template<class T>
int CServerBan::BanExt(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason)
{
// validate address
if(Server()->m_RconClientID >= 0 && Server()->m_RconClientID < MAX_CLIENTS &&
Server()->m_aClients[Server()->m_RconClientID].m_State != CServer::CClient::STATE_EMPTY)
{
if(NetMatch(pData, Server()->m_NetServer.ClientAddr(Server()->m_RconClientID)))
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (you can't ban yourself)");
return -1;
}
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(i == Server()->m_RconClientID || Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
continue;
if(Server()->m_aClients[i].m_Authed >= Server()->m_RconAuthLevel && NetMatch(pData, Server()->m_NetServer.ClientAddr(i)))
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (command denied)");
return -1;
}
}
}
else if(Server()->m_RconClientID == IServer::RCON_CID_VOTE)
{
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
continue;
if(Server()->m_aClients[i].m_Authed != CServer::AUTHED_NO && NetMatch(pData, Server()->m_NetServer.ClientAddr(i)))
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (command denied)");
return -1;
}
}
}
int Result = Ban(pBanPool, pData, Seconds, pReason);
if(Result != 0)
return Result;
// drop banned clients
typename T::CDataType Data = *pData;
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(Server()->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY)
continue;
if(NetMatch(&Data, Server()->m_NetServer.ClientAddr(i)))
{
CNetHash NetHash(&Data);
char aBuf[256];
MakeBanInfo(pBanPool->Find(&Data, &NetHash), aBuf, sizeof(aBuf), MSGTYPE_PLAYER);
Server()->m_NetServer.Drop(i, aBuf);
}
}
return Result;
}
int CServerBan::BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason)
{
return BanExt(&m_BanAddrPool, pAddr, Seconds, pReason);
}
int CServerBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReason)
{
if(pRange->IsValid())
return BanExt(&m_BanRangePool, pRange, Seconds, pReason);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban failed (invalid range)");
return -1;
}
void CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser)
{
CServerBan *pThis = static_cast<CServerBan *>(pUser);
const int defaultMinutes = 5;
const char *pStr = pResult->GetString(0);
int Minutes = pResult->NumArguments()>1 ? clamp(pResult->GetInteger(1), 0, 44640) : defaultMinutes;
const char *pReason = pResult->NumArguments()>2 ? pResult->GetString(2) : "No reason given";
// check if time was given or a reason instead
const char *time;
if(pResult->NumArguments() > 1)
{
// check if number given
time = pResult->GetString(1);
int i = str_length(time) - 1;
for(; i >= 0; --i)
if(time[i] < '0' || '9' < time[i])
break;
// in case that no number was given
if(!(str_length(time) && i < 0))
{
Minutes = defaultMinutes;
if(pResult->NumArguments() > 2)
{ // add to reason
char *newReason = (char*)malloc(sizeof(char) * (str_length(time) + str_length(pReason) + 2));
mem_copy(newReason, time, sizeof(char) * str_length(time));
newReason[str_length(time)] = ' ';
mem_copy(newReason + str_length(time) + 1, pReason, sizeof(char) * (str_length(pReason) + 1));
pReason = newReason;
}
else
{ // the time is the actual reason
pReason = time;
}
}
}
int CID = -1;
if(StrAllnum(pStr))
CID = str_toint(pStr);
else
{
NETADDR Addr;
if(net_addr_from_str(&Addr, pStr) == 0)
for(int i = 0; i < MAX_CLIENTS; i++)
if(pThis->NetMatch(&Addr, pThis->Server()->m_NetServer.ClientAddr(i)))
{
CID = i;
break;
}
}
if(StrAllnum(pStr))
{
int ClientID = str_toint(pStr);
if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->Server()->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "net_ban", "ban error (invalid client id)");
else
pThis->BanAddr(pThis->Server()->m_NetServer.ClientAddr(ClientID), Minutes*60, pReason);
}
else
ConBan(pResult, pUser);
}
void CServer::CClient::Reset()
{
// reset input
for(int i = 0; i < 200; i++)
m_aInputs[i].m_GameTick = -1;
m_CurrentInput = 0;
mem_zero(&m_LatestInput, sizeof(m_LatestInput));
m_Snapshots.PurgeAll();
m_LastAckedSnapshot = -1;
m_LastInputTick = -1;
m_SnapRate = CClient::SNAPRATE_INIT;
m_Score = 0;
}
CServer::CServer() : m_DemoRecorder(&m_SnapshotDelta)
{
m_TickSpeed = SERVER_TICK_SPEED;
m_pGameServer = 0;
m_CurrentGameTick = 0;
m_RunServer = 1;
m_pCurrentMapData = 0;
m_CurrentMapSize = 0;
m_MapReload = 0;
m_RconClientID = IServer::RCON_CID_SERV;
m_RconAuthLevel = AUTHED_SUBADMIN;
// when starting there are no admins
m_numLoggedInAdmins = 0;
m_Votebans = NULL;
m_InfoTexts = NULL;
m_InfoTextInterval = -1;
Init();
}
CServer::~CServer()
{
// delte votebans
while(m_Votebans != NULL)
{
CVoteban *tmp = m_Votebans->m_Next;
delete m_Votebans;
m_Votebans = tmp;
}
// delte info texts
while(m_InfoTexts != NULL)
{
CInfoText *tmp = m_InfoTexts->m_Next;
delete m_InfoTexts;
m_InfoTexts = tmp;
}
}
void CServer::UpdateLoggedInAdmins()
{
bool gotAny = (m_numLoggedInAdmins > 0);
m_numLoggedInAdmins = 0;
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(m_aClients[i].m_State == CClient::STATE_INGAME && m_aClients[i].m_Authed >= AUTHED_SUBADMIN)
{
++m_numLoggedInAdmins;
}
}
if(gotAny != (m_numLoggedInAdmins > 0))
{
UpdateServerInfo();
}
}
int CServer::TrySetClientName(int ClientID, const char *pName)
{
char aTrimmedName[64];
// trim the name
str_copy(aTrimmedName, StrUTF8Ltrim(pName), sizeof(aTrimmedName));
StrUTF8Rtrim(aTrimmedName);
// check if new and old name are the same
if(m_aClients[ClientID].m_aName[0] && str_comp(m_aClients[ClientID].m_aName, aTrimmedName) == 0)
return 0;
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "'%s' -> '%s'", pName, aTrimmedName);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf);
pName = aTrimmedName;
// check for empty names
if(!pName[0])
return -1;
// make sure that two clients doesn't have the same name
for(int i = 0; i < MAX_CLIENTS; i++)
if(i != ClientID && m_aClients[i].m_State >= CClient::STATE_READY)
{
if(str_comp(pName, m_aClients[i].m_aName) == 0)
return -1;
}
// set the client name
str_copy(m_aClients[ClientID].m_aName, pName, MAX_NAME_LENGTH);
return 0;
}
void CServer::SetClientName(int ClientID, const char *pName)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY)
return;
if(!pName)
return;
char aNameTry[MAX_NAME_LENGTH];
str_copy(aNameTry, pName, MAX_NAME_LENGTH);
if(TrySetClientName(ClientID, aNameTry))
{
// auto rename
for(int i = 1;; i++)
{
str_format(aNameTry, MAX_NAME_LENGTH, "(%d)%s", i, pName);
if(TrySetClientName(ClientID, aNameTry) == 0)
break;
}
}
}
void CServer::SetClientClan(int ClientID, const char *pClan)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY || !pClan)
return;
str_copy(m_aClients[ClientID].m_aClan, pClan, MAX_CLAN_LENGTH);
}
void CServer::SetClientCountry(int ClientID, int Country)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY)
return;
m_aClients[ClientID].m_Country = Country;
}
void CServer::SetClientScore(int ClientID, int Score)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State < CClient::STATE_READY)
return;
m_aClients[ClientID].m_Score = Score;
}
void CServer::Kick(int ClientID, const char *pReason)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CClient::STATE_EMPTY)
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "invalid client id to kick");
return;
}
else if(m_RconClientID == ClientID)
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "you can't kick yourself");
return;
}
else if(m_aClients[ClientID].m_Authed > m_RconAuthLevel)
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", "kick command denied");
return;
}
m_NetServer.Drop(ClientID, pReason);
}
/*int CServer::Tick()
{
return m_CurrentGameTick;
}*/
int64 CServer::TickStartTime(int Tick)
{
return m_GameStartTime + (time_freq()*Tick)/SERVER_TICK_SPEED;
}
/*int CServer::TickSpeed()
{
return SERVER_TICK_SPEED;
}*/
int CServer::Init()
{
for(int i = 0; i < MAX_CLIENTS; i++)
{
m_aClients[i].m_State = CClient::STATE_EMPTY;
m_aClients[i].m_aName[0] = 0;
m_aClients[i].m_aClan[0] = 0;
m_aClients[i].m_Country = -1;
m_aClients[i].m_Snapshots.Init();
}
AdjustVotebanTime(m_CurrentGameTick);
m_CurrentGameTick = 0;
return 0;
}
void CServer::SetRconCID(int ClientID)
{
m_RconClientID = ClientID;
}
bool CServer::IsAuthed(int ClientID)
{
return m_aClients[ClientID].m_Authed;
}
int CServer::GetClientInfo(int ClientID, CClientInfo *pInfo)
{
dbg_assert(ClientID >= 0 && ClientID < MAX_CLIENTS, "client_id is not valid");
dbg_assert(pInfo != 0, "info can not be null");
if(m_aClients[ClientID].m_State == CClient::STATE_INGAME)
{
pInfo->m_pName = m_aClients[ClientID].m_aName;
pInfo->m_Latency = m_aClients[ClientID].m_Latency;
return 1;
}
return 0;
}
void CServer::GetClientAddr(int ClientID, char *pAddrStr, int Size)
{
if(ClientID >= 0 && ClientID < MAX_CLIENTS && m_aClients[ClientID].m_State == CClient::STATE_INGAME)
net_addr_str(m_NetServer.ClientAddr(ClientID), pAddrStr, Size, false);
}
const char *CServer::ClientName(int ClientID)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
return "(invalid)";
if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME)
return m_aClients[ClientID].m_aName;
else
return "(connecting)";
}
const char *CServer::ClientClan(int ClientID)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
return "";
if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME)
return m_aClients[ClientID].m_aClan;
else
return "";
}
int CServer::ClientCountry(int ClientID)
{
if(ClientID < 0 || ClientID >= MAX_CLIENTS || m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
return -1;
if(m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME)
return m_aClients[ClientID].m_Country;
else
return -1;
}
bool CServer::ClientIngame(int ClientID)
{
return ClientID >= 0 && ClientID < MAX_CLIENTS && m_aClients[ClientID].m_State == CServer::CClient::STATE_INGAME;
}
int CServer::MaxClients() const
{
return m_NetServer.MaxClients();
}
int CServer::SendMsg(CMsgPacker *pMsg, int Flags, int ClientID)
{
return SendMsgEx(pMsg, Flags, ClientID, false);
}
int CServer::SendMsgEx(CMsgPacker *pMsg, int Flags, int ClientID, bool System)
{
CNetChunk Packet;
if(!pMsg)
return -1;
mem_zero(&Packet, sizeof(CNetChunk));
Packet.m_ClientID = ClientID;
Packet.m_pData = pMsg->Data();
Packet.m_DataSize = pMsg->Size();
// HACK: modify the message id in the packet and store the system flag
*((unsigned char*)Packet.m_pData) <<= 1;
if(System)
*((unsigned char*)Packet.m_pData) |= 1;
if(Flags&MSGFLAG_VITAL)
Packet.m_Flags |= NETSENDFLAG_VITAL;
if(Flags&MSGFLAG_FLUSH)
Packet.m_Flags |= NETSENDFLAG_FLUSH;
// write message to demo recorder
if(!(Flags&MSGFLAG_NORECORD))
m_DemoRecorder.RecordMessage(pMsg->Data(), pMsg->Size());
if(!(Flags&MSGFLAG_NOSEND))
{
if(ClientID == -1)
{
// broadcast
int i;
for(i = 0; i < MAX_CLIENTS; i++)
if(m_aClients[i].m_State == CClient::STATE_INGAME)
{
Packet.m_ClientID = i;
m_NetServer.Send(&Packet);
}
}
else
m_NetServer.Send(&Packet);
}
return 0;
}
void CServer::DoSnapshot()
{
GameServer()->OnPreSnap();
// create snapshot for demo recording
if(m_DemoRecorder.IsRecording())
{
char aData[CSnapshot::MAX_SIZE];
int SnapshotSize;
// build snap and possibly add some messages
m_SnapshotBuilder.Init();
GameServer()->OnSnap(-1);
SnapshotSize = m_SnapshotBuilder.Finish(aData);
// write snapshot
m_DemoRecorder.RecordSnapshot(Tick(), aData, SnapshotSize);
}
// create snapshots for all clients
for(int i = 0; i < MAX_CLIENTS; i++)
{
// client must be ingame to recive snapshots
if(m_aClients[i].m_State != CClient::STATE_INGAME)
continue;
// this client is trying to recover, don't spam snapshots
if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_RECOVER && (Tick()%50) != 0)
continue;
// this client is trying to recover, don't spam snapshots
if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_INIT && (Tick()%10) != 0)
continue;
{
char aData[CSnapshot::MAX_SIZE];
CSnapshot *pData = (CSnapshot*)aData; // Fix compiler warning for strict-aliasing
char aDeltaData[CSnapshot::MAX_SIZE];
char aCompData[CSnapshot::MAX_SIZE];
int SnapshotSize;
int Crc;
static CSnapshot EmptySnap;
CSnapshot *pDeltashot = &EmptySnap;
int DeltashotSize;
int DeltaTick = -1;
int DeltaSize;
m_SnapshotBuilder.Init();
GameServer()->OnSnap(i);
// finish snapshot
SnapshotSize = m_SnapshotBuilder.Finish(pData);
Crc = pData->Crc();
// remove old snapshos
// keep 3 seconds worth of snapshots
m_aClients[i].m_Snapshots.PurgeUntil(m_CurrentGameTick-SERVER_TICK_SPEED*3);
// save it the snapshot
m_aClients[i].m_Snapshots.Add(m_CurrentGameTick, time_get(), SnapshotSize, pData, 0);
// find snapshot that we can preform delta against
EmptySnap.Clear();
{
DeltashotSize = m_aClients[i].m_Snapshots.Get(m_aClients[i].m_LastAckedSnapshot, 0, &pDeltashot, 0);
if(DeltashotSize >= 0)
DeltaTick = m_aClients[i].m_LastAckedSnapshot;
else
{
// no acked package found, force client to recover rate
if(m_aClients[i].m_SnapRate == CClient::SNAPRATE_FULL)
m_aClients[i].m_SnapRate = CClient::SNAPRATE_RECOVER;
}
}
// create delta
DeltaSize = m_SnapshotDelta.CreateDelta(pDeltashot, pData, aDeltaData);
if(DeltaSize)
{
// compress it
int SnapshotSize;
const int MaxSize = MAX_SNAPSHOT_PACKSIZE;
int NumPackets;
SnapshotSize = CVariableInt::Compress(aDeltaData, DeltaSize, aCompData);
NumPackets = (SnapshotSize+MaxSize-1)/MaxSize;
for(int n = 0, Left = SnapshotSize; Left; n++)
{
int Chunk = Left < MaxSize ? Left : MaxSize;
Left -= Chunk;
if(NumPackets == 1)
{
CMsgPacker Msg(NETMSG_SNAPSINGLE);
Msg.AddInt(m_CurrentGameTick);
Msg.AddInt(m_CurrentGameTick-DeltaTick);
Msg.AddInt(Crc);
Msg.AddInt(Chunk);
Msg.AddRaw(&aCompData[n*MaxSize], Chunk);
SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true);
}
else
{
CMsgPacker Msg(NETMSG_SNAP);
Msg.AddInt(m_CurrentGameTick);
Msg.AddInt(m_CurrentGameTick-DeltaTick);
Msg.AddInt(NumPackets);
Msg.AddInt(n);
Msg.AddInt(Crc);
Msg.AddInt(Chunk);
Msg.AddRaw(&aCompData[n*MaxSize], Chunk);
SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true);
}
}
}
else
{
CMsgPacker Msg(NETMSG_SNAPEMPTY);
Msg.AddInt(m_CurrentGameTick);
Msg.AddInt(m_CurrentGameTick-DeltaTick);
SendMsgEx(&Msg, MSGFLAG_FLUSH, i, true);
}
}
}
GameServer()->OnPostSnap();
}
int CServer::NewClientNoAuthCallback(int ClientID, void *pUser)
{
CServer *pThis = (CServer *)pUser;
pThis->m_aClients[ClientID].m_State = CClient::STATE_CONNECTING;
pThis->m_aClients[ClientID].m_aName[0] = 0;
pThis->m_aClients[ClientID].m_aClan[0] = 0;
pThis->m_aClients[ClientID].m_Country = -1;
pThis->m_aClients[ClientID].m_Authed = AUTHED_NO;
pThis->m_aClients[ClientID].m_AuthTries = 0;
pThis->m_aClients[ClientID].m_pRconCmdToSend = 0;
pThis->m_aClients[ClientID].Reset();
pThis->SendMap(ClientID);
return 0;
}
int CServer::NewClientCallback(int ClientID, void *pUser)
{
CServer *pThis = (CServer *)pUser;
pThis->m_aClients[ClientID].m_State = CClient::STATE_AUTH;
pThis->m_aClients[ClientID].m_aName[0] = 0;
pThis->m_aClients[ClientID].m_aClan[0] = 0;
pThis->m_aClients[ClientID].m_Country = -1;
pThis->m_aClients[ClientID].m_Authed = AUTHED_NO;
pThis->m_aClients[ClientID].m_AuthTries = 0;
pThis->m_aClients[ClientID].m_pRconCmdToSend = 0;
pThis->m_aClients[ClientID].Reset();
return 0;
}
int CServer::DelClientCallback(int ClientID, const char *pReason, void *pUser)
{
CServer *pThis = (CServer *)pUser;
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(pThis->m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "client dropped. cid=%d addr=%s reason='%s'", ClientID, aAddrStr, pReason);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf);
// notify the mod about the drop
if(pThis->m_aClients[ClientID].m_State >= CClient::STATE_READY)
pThis->GameServer()->OnClientDrop(ClientID, pReason);
pThis->m_aClients[ClientID].m_State = CClient::STATE_EMPTY;
pThis->m_aClients[ClientID].m_aName[0] = 0;
pThis->m_aClients[ClientID].m_aClan[0] = 0;
pThis->m_aClients[ClientID].m_Country = -1;
pThis->m_aClients[ClientID].m_Authed = AUTHED_NO;
pThis->m_aClients[ClientID].m_AuthTries = 0;
pThis->m_aClients[ClientID].m_pRconCmdToSend = 0;
pThis->m_aClients[ClientID].m_Snapshots.PurgeAll();
// could have been an admin
pThis->UpdateLoggedInAdmins();
return 0;
}
void CServer::SendMap(int ClientID)
{
CMsgPacker Msg(NETMSG_MAP_CHANGE);
Msg.AddString(GetMapName(), 0);
Msg.AddInt(m_CurrentMapCrc);
Msg.AddInt(m_CurrentMapSize);
SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true);
}
void CServer::SendConnectionReady(int ClientID)
{
CMsgPacker Msg(NETMSG_CON_READY);
SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true);
}
void CServer::SendRconLine(int ClientID, const char *pLine)
{
CMsgPacker Msg(NETMSG_RCON_LINE);
Msg.AddString(pLine, 512);
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
}
void CServer::SendRconLineAuthed(const char *pLine, void *pUser)
{
CServer *pThis = (CServer *)pUser;
static volatile int ReentryGuard = 0;
int i;
if(ReentryGuard) return;
ReentryGuard++;
for(i = 0; i < MAX_CLIENTS; i++)
{
if(pThis->m_aClients[i].m_State != CClient::STATE_EMPTY && pThis->m_aClients[i].m_Authed >= pThis->m_RconAuthLevel)
pThis->SendRconLine(i, pLine);
}
ReentryGuard--;
}
void CServer::SendRconCmdAdd(const IConsole::CCommandInfo *pCommandInfo, int ClientID)
{
CMsgPacker Msg(NETMSG_RCON_CMD_ADD);
Msg.AddString(pCommandInfo->m_pName, IConsole::TEMPCMD_NAME_LENGTH);
Msg.AddString(pCommandInfo->m_pHelp, IConsole::TEMPCMD_HELP_LENGTH);
Msg.AddString(pCommandInfo->m_pParams, IConsole::TEMPCMD_PARAMS_LENGTH);
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
}
void CServer::SendRconCmdRem(const IConsole::CCommandInfo *pCommandInfo, int ClientID)
{
CMsgPacker Msg(NETMSG_RCON_CMD_REM);
Msg.AddString(pCommandInfo->m_pName, 256);
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
}
void CServer::UpdateClientRconCommands()
{
int ClientID = Tick() % MAX_CLIENTS;
if(m_aClients[ClientID].m_State != CClient::STATE_EMPTY && m_aClients[ClientID].m_Authed)
{
int ConsoleAccessLevel = m_aClients[ClientID].m_Authed == AUTHED_ADMIN ? IConsole::ACCESS_LEVEL_ADMIN : (AUTHED_SUBADMIN ? IConsole::ACCESS_LEVEL_SUBADMIN : IConsole::ACCESS_LEVEL_MOD);
for(int i = 0; i < MAX_RCONCMD_SEND && m_aClients[ClientID].m_pRconCmdToSend; ++i)
{
SendRconCmdAdd(m_aClients[ClientID].m_pRconCmdToSend, ClientID);
m_aClients[ClientID].m_pRconCmdToSend = m_aClients[ClientID].m_pRconCmdToSend->NextCommandInfo(ConsoleAccessLevel, CFGFLAG_SERVER);
}
}
}
void CServer::ProcessClientPacket(CNetChunk *pPacket)
{
int ClientID = pPacket->m_ClientID;
CUnpacker Unpacker;
Unpacker.Reset(pPacket->m_pData, pPacket->m_DataSize);
// unpack msgid and system flag
int Msg = Unpacker.GetInt();
int Sys = Msg&1;
Msg >>= 1;
if(Unpacker.Error())
return;
if(Sys)
{
// system message
if(Msg == NETMSG_INFO)
{
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && m_aClients[ClientID].m_State == CClient::STATE_AUTH)
{
const char *pVersion = Unpacker.GetString(CUnpacker::SANITIZE_CC);
if(str_comp(pVersion, GameServer()->NetVersion()) != 0)
{
// wrong version
char aReason[256];
str_format(aReason, sizeof(aReason), "Wrong version. Server is running '%s' and client '%s'", GameServer()->NetVersion(), pVersion);
m_NetServer.Drop(ClientID, aReason);
return;
}
const char *pPassword = Unpacker.GetString(CUnpacker::SANITIZE_CC);
if(g_Config.m_Password[0] != 0 && str_comp(g_Config.m_Password, pPassword) != 0)
{
// wrong password
m_NetServer.Drop(ClientID, "Wrong password");
return;
}
m_aClients[ClientID].m_State = CClient::STATE_CONNECTING;
SendMap(ClientID);
}
}
else if(Msg == NETMSG_REQUEST_MAP_DATA)
{
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) == 0 || m_aClients[ClientID].m_State < CClient::STATE_CONNECTING)
return;
int Chunk = Unpacker.GetInt();
int ChunkSize = 1024-128;
int Offset = Chunk * ChunkSize;
int Last = 0;
// drop faulty map data requests
if(Chunk < 0 || Offset < 0 || Offset > m_CurrentMapSize)
return;
if(Offset+ChunkSize >= m_CurrentMapSize)
{
ChunkSize = m_CurrentMapSize-Offset;
if(ChunkSize < 0)
ChunkSize = 0;
Last = 1;
}
CMsgPacker Msg(NETMSG_MAP_DATA);
Msg.AddInt(Last);
Msg.AddInt(m_CurrentMapCrc);
Msg.AddInt(Chunk);
Msg.AddInt(ChunkSize);
Msg.AddRaw(&m_pCurrentMapData[Offset], ChunkSize);
SendMsgEx(&Msg, MSGFLAG_VITAL|MSGFLAG_FLUSH, ClientID, true);
if(g_Config.m_Debug)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "sending chunk %d with size %d", Chunk, ChunkSize);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf);
}
}
else if(Msg == NETMSG_READY)
{
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && m_aClients[ClientID].m_State == CClient::STATE_CONNECTING)
{
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "player is ready. ClientID=%x addr=%s", ClientID, aAddrStr);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf);
m_aClients[ClientID].m_State = CClient::STATE_READY;
GameServer()->OnClientConnected(ClientID);
SendConnectionReady(ClientID);
}
}
else if(Msg == NETMSG_ENTERGAME)
{
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && m_aClients[ClientID].m_State == CClient::STATE_READY && GameServer()->IsClientReady(ClientID))
{
char aAddrStr[NETADDR_MAXSTRSIZE];
net_addr_str(m_NetServer.ClientAddr(ClientID), aAddrStr, sizeof(aAddrStr), true);
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "player has entered the game. ClientID=%x addr=%s", ClientID, aAddrStr);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
m_aClients[ClientID].m_State = CClient::STATE_INGAME;
GameServer()->OnClientEnter(ClientID);
}
}
else if(Msg == NETMSG_INPUT)
{
CClient::CInput *pInput;
int64 TagTime;
m_aClients[ClientID].m_LastAckedSnapshot = Unpacker.GetInt();
int IntendedTick = Unpacker.GetInt();
int Size = Unpacker.GetInt();
// check for errors
if(Unpacker.Error() || Size/4 > MAX_INPUT_SIZE)
return;
if(m_aClients[ClientID].m_LastAckedSnapshot > 0)
m_aClients[ClientID].m_SnapRate = CClient::SNAPRATE_FULL;
if(m_aClients[ClientID].m_Snapshots.Get(m_aClients[ClientID].m_LastAckedSnapshot, &TagTime, 0, 0) >= 0)
m_aClients[ClientID].m_Latency = (int)(((time_get()-TagTime)*1000)/time_freq());
// add message to report the input timing
// skip packets that are old
if(IntendedTick > m_aClients[ClientID].m_LastInputTick)
{
int TimeLeft = ((TickStartTime(IntendedTick)-time_get())*1000) / time_freq();
CMsgPacker Msg(NETMSG_INPUTTIMING);
Msg.AddInt(IntendedTick);
Msg.AddInt(TimeLeft);
SendMsgEx(&Msg, 0, ClientID, true);
}
m_aClients[ClientID].m_LastInputTick = IntendedTick;
pInput = &m_aClients[ClientID].m_aInputs[m_aClients[ClientID].m_CurrentInput];
if(IntendedTick <= Tick())
IntendedTick = Tick()+1;
pInput->m_GameTick = IntendedTick;
for(int i = 0; i < Size/4; i++)
pInput->m_aData[i] = Unpacker.GetInt();
mem_copy(m_aClients[ClientID].m_LatestInput.m_aData, pInput->m_aData, MAX_INPUT_SIZE*sizeof(int));
m_aClients[ClientID].m_CurrentInput++;
m_aClients[ClientID].m_CurrentInput %= 200;
// call the mod with the fresh input data
if(m_aClients[ClientID].m_State == CClient::STATE_INGAME)
GameServer()->OnClientDirectInput(ClientID, m_aClients[ClientID].m_LatestInput.m_aData);
}
else if(Msg == NETMSG_RCON_CMD)
{
const char *pCmd = Unpacker.GetString();
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Unpacker.Error() == 0 && m_aClients[ClientID].m_Authed)
{
// try to find first space
const char *delimiter = strchr(pCmd, ' ');
if(m_aClients[ClientID].m_Authed != AUTHED_SUBADMIN
|| (
m_aClients[ClientID].m_Authed == AUTHED_SUBADMIN
&& delimiter != NULL
&& m_aClients[ClientID].m_SubAdminAuthPass == std::string(pCmd, delimiter - pCmd)
&& (pCmd = delimiter + 1) != NULL)
)
{
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d rcon='%s'", ClientID, pCmd);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBuf);
m_RconClientID = ClientID;
m_RconAuthLevel = m_aClients[ClientID].m_Authed;
Console()->SetAccessLevel(m_aClients[ClientID].m_Authed == AUTHED_ADMIN ? IConsole::ACCESS_LEVEL_ADMIN : (m_aClients[ClientID].m_Authed == AUTHED_SUBADMIN ? IConsole::ACCESS_LEVEL_SUBADMIN : IConsole::ACCESS_LEVEL_MOD));
Console()->ExecuteLineFlag(pCmd, CFGFLAG_SERVER);
Console()->SetAccessLevel(IConsole::ACCESS_LEVEL_ADMIN);
m_RconClientID = IServer::RCON_CID_SERV;
m_RconAuthLevel = AUTHED_SUBADMIN;
}
else if(m_aClients[ClientID].m_Authed == AUTHED_SUBADMIN)
{
if(m_aClients[ClientID].m_SubAdminCommandPassFails >= 3)
{
// logout
rconLogClientOut(ClientID, "Too many wrong passwords. You were logged out.");
}
else
{
++m_aClients[ClientID].m_SubAdminCommandPassFails;
SendRconLine(ClientID, "Wrong password.");
}
}
}
}
else if(Msg == NETMSG_RCON_AUTH)
{
const char *pPw;
Unpacker.GetString(); // login name, not used
pPw = Unpacker.GetString(CUnpacker::SANITIZE_CC);
// try matching username:password login
const char *delimiter = strchr(pPw, ':');
std::string username, password;
loginiterator loginit;
bool loginWithUsername = (delimiter != NULL);
if(loginWithUsername)
{
username.assign(pPw, delimiter - pPw);
password.assign(delimiter + 1);
}
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Unpacker.Error() == 0)
{
if(g_Config.m_SvRconPassword[0] == 0 && g_Config.m_SvRconModPassword[0] == 0 && logins.empty())
{
SendRconLine(ClientID, "No rcon password set on server. Set sv_rcon_password and/or sv_rcon_mod_password or add logins using add_login to enable the remote console.");
}
else if(g_Config.m_SvRconPassword[0] && str_comp(pPw, g_Config.m_SvRconPassword) == 0)
{
CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS);
Msg.AddInt(1); //authed
Msg.AddInt(1); //cmdlist
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
m_aClients[ClientID].m_Authed = AUTHED_ADMIN;
int SendRconCmds = Unpacker.GetInt();
if(Unpacker.Error() == 0 && SendRconCmds)
m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_ADMIN, CFGFLAG_SERVER);
SendRconLine(ClientID, "Admin authentication successful. Full remote console access granted.");
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (admin)", ClientID);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
UpdateLoggedInAdmins();
}
else if(g_Config.m_SvRconModPassword[0] && str_comp(pPw, g_Config.m_SvRconModPassword) == 0)
{
CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS);
Msg.AddInt(1); //authed
Msg.AddInt(1); //cmdlist
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
m_aClients[ClientID].m_Authed = AUTHED_MOD;
int SendRconCmds = Unpacker.GetInt();
if(Unpacker.Error() == 0 && SendRconCmds)
m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_MOD, CFGFLAG_SERVER);
SendRconLine(ClientID, "Moderator authentication successful. Limited remote console access granted.");
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (moderator)", ClientID);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
else if(loginWithUsername
&& !logins.empty()
&& (loginit = logins.find(username)) != logins.end()
&& loginit->second == password)
{
CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS);
Msg.AddInt(1); //authed
Msg.AddInt(1); //cmdlist
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
m_aClients[ClientID].m_Authed = AUTHED_SUBADMIN;
m_aClients[ClientID].m_SubAdminAuthName = loginit->first;
m_aClients[ClientID].m_SubAdminAuthPass = loginit->second;
m_aClients[ClientID].m_SubAdminCommandPassFails = 0;
int SendRconCmds = Unpacker.GetInt();
if(Unpacker.Error() == 0 && SendRconCmds)
m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_SUBADMIN, CFGFLAG_SERVER);
SendRconLine(ClientID, "Subadmin authentication successful. Limited remote console access granted.");
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (subadmin:%s)", ClientID, loginit->first.c_str());
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
UpdateLoggedInAdmins();
}
else if(g_Config.m_SvRconMaxTries)
{
m_aClients[ClientID].m_AuthTries++;
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Wrong password %d/%d.", m_aClients[ClientID].m_AuthTries, g_Config.m_SvRconMaxTries);
SendRconLine(ClientID, aBuf);
if(m_aClients[ClientID].m_AuthTries >= g_Config.m_SvRconMaxTries)
{
if(!g_Config.m_SvRconBantime)
m_NetServer.Drop(ClientID, "Too many remote console authentication tries");
else
m_ServerBan.BanAddr(m_NetServer.ClientAddr(ClientID), g_Config.m_SvRconBantime*60, "Too many remote console authentication tries");
}
}
else
{
SendRconLine(ClientID, "Wrong password.");
}
}
}
else if(Msg == NETMSG_PING)
{
CMsgPacker Msg(NETMSG_PING_REPLY);
SendMsgEx(&Msg, 0, ClientID, true);
}
else
{
if(g_Config.m_Debug)
{
char aHex[] = "0123456789ABCDEF";
char aBuf[512];
for(int b = 0; b < pPacket->m_DataSize && b < 32; b++)
{
aBuf[b*3] = aHex[((const unsigned char *)pPacket->m_pData)[b]>>4];
aBuf[b*3+1] = aHex[((const unsigned char *)pPacket->m_pData)[b]&0xf];
aBuf[b*3+2] = ' ';
aBuf[b*3+3] = 0;
}
char aBufMsg[256];
str_format(aBufMsg, sizeof(aBufMsg), "strange message ClientID=%d msg=%d data_size=%d", ClientID, Msg, pPacket->m_DataSize);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBufMsg);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf);
}
}
}
else
{
// game message
if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && m_aClients[ClientID].m_State >= CClient::STATE_READY)
GameServer()->OnMessage(Msg, &Unpacker, ClientID);
}
}
void CServer::SendServerInfo(const NETADDR *pAddr, int Token)
{
CNetChunk Packet;
CPacker p;
char aBuf[128];
// count the players
int PlayerCount = 0, ClientCount = 0;
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
{
if(GameServer()->IsClientPlayer(i))
PlayerCount++;
ClientCount++;
}
}
p.Reset();
p.AddRaw(SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO));
str_format(aBuf, sizeof(aBuf), "%d", Token);
p.AddString(aBuf, 6);
p.AddString(GameServer()->Version(), 32);
// send the alternative server name when a admin is online
p.AddString((m_numLoggedInAdmins && str_length(g_Config.m_SvNameAdmin)) ? g_Config.m_SvNameAdmin : g_Config.m_SvName, 64);
p.AddString(GetMapName(), 32);
// gametype
p.AddString(GameServer()->GameType(), 16);
// flags
int i = 0;
if(g_Config.m_Password[0]) // password set
i |= SERVER_FLAG_PASSWORD;
str_format(aBuf, sizeof(aBuf), "%d", i);
p.AddString(aBuf, 2);
str_format(aBuf, sizeof(aBuf), "%d", PlayerCount); p.AddString(aBuf, 3); // num players
str_format(aBuf, sizeof(aBuf), "%d", m_NetServer.MaxClients()-g_Config.m_SvSpectatorSlots); p.AddString(aBuf, 3); // max players
str_format(aBuf, sizeof(aBuf), "%d", ClientCount); p.AddString(aBuf, 3); // num clients
str_format(aBuf, sizeof(aBuf), "%d", m_NetServer.MaxClients()); p.AddString(aBuf, 3); // max clients
for(i = 0; i < MAX_CLIENTS; i++)
{
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
{
p.AddString(ClientName(i), MAX_NAME_LENGTH); // client name
p.AddString(ClientClan(i), MAX_CLAN_LENGTH); // client clan
str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Country); p.AddString(aBuf, 6); // client country
str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Score); p.AddString(aBuf, 6); // client score
str_format(aBuf, sizeof(aBuf), "%d", GameServer()->IsClientPlayer(i)?1:0); p.AddString(aBuf, 2); // is player?
}
}
Packet.m_ClientID = -1;
Packet.m_Address = *pAddr;
Packet.m_Flags = NETSENDFLAG_CONNLESS;
Packet.m_DataSize = p.Size();
Packet.m_pData = p.Data();
m_NetServer.Send(&Packet);
}
void CServer::UpdateServerInfo()
{
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
SendServerInfo(m_NetServer.ClientAddr(i), -1);
}
}
void CServer::PumpNetwork()
{
CNetChunk Packet;
m_NetServer.Update();
// process packets
while(m_NetServer.Recv(&Packet))
{
if(Packet.m_ClientID == -1)
{
// stateless
if(!m_Register.RegisterProcessPacket(&Packet))
{
if(Packet.m_DataSize == sizeof(SERVERBROWSE_GETINFO)+1 &&
mem_comp(Packet.m_pData, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)) == 0)
{
SendServerInfo(&Packet.m_Address, ((unsigned char *)Packet.m_pData)[sizeof(SERVERBROWSE_GETINFO)]);
}
}
}
else
ProcessClientPacket(&Packet);
}
m_ServerBan.Update();
m_Econ.Update();
}
char *CServer::GetMapName()
{
// get the name of the map without his path
char *pMapShortName = &g_Config.m_SvMap[0];
for(int i = 0; i < str_length(g_Config.m_SvMap)-1; i++)
{
if(g_Config.m_SvMap[i] == '/' || g_Config.m_SvMap[i] == '\\')
pMapShortName = &g_Config.m_SvMap[i+1];
}
return pMapShortName;
}
int CServer::LoadMap(const char *pMapName)
{
//DATAFILE *df;
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "maps/%s.map", pMapName);
/*df = datafile_load(buf);
if(!df)
return 0;*/
// check for valid standard map
if(!m_MapChecker.ReadAndValidateMap(Storage(), aBuf, IStorage::TYPE_ALL))
{
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "mapchecker", "invalid standard map");
return 0;
}
if(!m_pMap->Load(aBuf))
return 0;
// stop recording when we change map
m_DemoRecorder.Stop();
// reinit snapshot ids
m_IDPool.TimeoutIDs();
// get the crc of the map
m_CurrentMapCrc = m_pMap->Crc();
char aBufMsg[256];
str_format(aBufMsg, sizeof(aBufMsg), "%s crc is %08x", aBuf, m_CurrentMapCrc);
Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "server", aBufMsg);
str_copy(m_aCurrentMap, pMapName, sizeof(m_aCurrentMap));
//map_set(df);
// load complete map into memory for download
{
IOHANDLE File = Storage()->OpenFile(aBuf, IOFLAG_READ, IStorage::TYPE_ALL);
m_CurrentMapSize = (int)io_length(File);
if(m_pCurrentMapData)
mem_free(m_pCurrentMapData);
m_pCurrentMapData = (unsigned char *)mem_alloc(m_CurrentMapSize, 1);
io_read(File, m_pCurrentMapData, m_CurrentMapSize);
io_close(File);
}
return 1;
}
void CServer::InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, IConsole *pConsole)
{
m_Register.Init(pNetServer, pMasterServer, pConsole);
}
int CServer::Run()
{
//
m_PrintCBIndex = Console()->RegisterPrintCallback(g_Config.m_ConsoleOutputLevel, SendRconLineAuthed, this);
// load map
if(!LoadMap(g_Config.m_SvMap))
{
dbg_msg("server", "failed to load map. mapname='%s'", g_Config.m_SvMap);
return -1;
}
// start server
NETADDR BindAddr;
if(g_Config.m_Bindaddr[0] && net_host_lookup(g_Config.m_Bindaddr, &BindAddr, NETTYPE_ALL) == 0)
{
// sweet!
BindAddr.type = NETTYPE_ALL;
BindAddr.port = g_Config.m_SvPort;
}
else
{
mem_zero(&BindAddr, sizeof(BindAddr));
BindAddr.type = NETTYPE_ALL;
BindAddr.port = g_Config.m_SvPort;
}
if(!m_NetServer.Open(BindAddr, &m_ServerBan, g_Config.m_SvMaxClients, g_Config.m_SvMaxClientsPerIP, 0))
{
dbg_msg("server", "couldn't open socket. port %d might already be in use", g_Config.m_SvPort);
return -1;
}
m_NetServer.SetCallbacks(NewClientCallback, NewClientNoAuthCallback, DelClientCallback, this);
m_Econ.Init(Console(), &m_ServerBan);
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "server name is '%s'", g_Config.m_SvName);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
GameServer()->OnInit();
str_format(aBuf, sizeof(aBuf), "version %s", GameServer()->NetVersion());
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
// process pending commands
m_pConsole->StoreCommands(false);
// start game
{
int64 ReportTime = time_get();
int ReportInterval = 3;
m_Lastheartbeat = 0;
m_GameStartTime = time_get();
if(g_Config.m_Debug)
{
str_format(aBuf, sizeof(aBuf), "baseline memory usage %dk", mem_stats()->allocated/1024);
Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "server", aBuf);
}
while(m_RunServer)
{
int64 t = time_get();
int NewTicks = 0;
// load new map TODO: don't poll this
if(str_comp(g_Config.m_SvMap, m_aCurrentMap) != 0 || m_MapReload)
{
m_MapReload = 0;
// load map
if(LoadMap(g_Config.m_SvMap))
{
// new map loaded
GameServer()->OnShutdown();
for(int c = 0; c < MAX_CLIENTS; c++)
{
if(m_aClients[c].m_State <= CClient::STATE_AUTH)
continue;
SendMap(c);
m_aClients[c].Reset();
m_aClients[c].m_State = CClient::STATE_CONNECTING;
}
m_GameStartTime = time_get();
AdjustVotebanTime(m_CurrentGameTick);
m_CurrentGameTick = 0;
Kernel()->ReregisterInterface(GameServer());
GameServer()->OnInit();
UpdateServerInfo();
}
else
{
str_format(aBuf, sizeof(aBuf), "failed to load map. mapname='%s'", g_Config.m_SvMap);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
str_copy(g_Config.m_SvMap, m_aCurrentMap, sizeof(g_Config.m_SvMap));
}
}
while(t > TickStartTime(m_CurrentGameTick+1))
{
m_CurrentGameTick++;
NewTicks++;
// apply new input
for(int c = 0; c < MAX_CLIENTS; c++)
{
if(m_aClients[c].m_State == CClient::STATE_EMPTY)
continue;
for(int i = 0; i < 200; i++)
{
if(m_aClients[c].m_aInputs[i].m_GameTick == Tick())
{
if(m_aClients[c].m_State == CClient::STATE_INGAME)
GameServer()->OnClientPredictedInput(c, m_aClients[c].m_aInputs[i].m_aData);
break;
}
}
}
GameServer()->OnTick();
}
// snap game
if(NewTicks)
{
if(g_Config.m_SvHighBandwidth || (m_CurrentGameTick%2) == 0)
DoSnapshot();
UpdateClientRconCommands();
}
// master server stuff
m_Register.RegisterUpdate(m_NetServer.NetType());
PumpNetwork();
if(ReportTime < time_get())
{
if(g_Config.m_Debug)
{
/*
static NETSTATS prev_stats;
NETSTATS stats;
netserver_stats(net, &stats);
perf_next();
if(config.dbg_pref)
perf_dump(&rootscope);
dbg_msg("server", "send=%8d recv=%8d",
(stats.send_bytes - prev_stats.send_bytes)/reportinterval,
(stats.recv_bytes - prev_stats.recv_bytes)/reportinterval);
prev_stats = stats;
*/
}
ReportTime += time_freq()*ReportInterval;
}
bool NonActive = true;
for(int c = 0; c < MAX_CLIENTS; c++)
if(m_aClients[c].m_State != CClient::STATE_EMPTY)
NonActive = false;
// wait for incoming data
if(NonActive && g_Config.m_SvShutdownWhenEmpty)
m_RunServer = false;
else
net_socket_read_wait(m_NetServer.Socket(), 5);
}
}
// disconnect all clients on shutdown
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(m_aClients[i].m_State != CClient::STATE_EMPTY)
m_NetServer.Drop(i, "Server shutdown");
m_Econ.Shutdown();
}
GameServer()->OnShutdown();
m_pMap->Unload();
if(m_pCurrentMapData)
mem_free(m_pCurrentMapData);
return 0;
}
// returns the time in seconds that the client is votebanned or 0 if he isn't
int CServer::ClientVotebannedTime(int ClientID)
{
CVoteban **v = IsVotebannedAddr(m_NetServer.ClientAddr(ClientID));
if(v != NULL && (*v)->m_Expire > Tick())
return ((*v)->m_Expire - Tick()) / TickSpeed();
return 0;
}
// decreases the time of all votebans by the given offset of ticks
void CServer::AdjustVotebanTime(int offset)
{
CleanVotebans();
CVoteban *v = m_Votebans;
while(v != NULL)
{
v->m_Expire -= offset;
v = v->m_Next;
}
}
// adds a new voteban for a specific address
void CServer::AddVotebanAddr(const NETADDR *addr, int expire)
{
CVoteban **v = IsVotebannedAddr(addr);
// create new
if(!v)
{
CVoteban *v = new CVoteban;
v->m_Addr = *addr;
v->m_Expire = expire;
// insert front
v->m_Next = m_Votebans;
m_Votebans = v;
}
// update existing entry
else
(*v)->m_Expire = expire;
}
// adds a new voteban for a client's address
void CServer::AddVoteban(int ClientID, int time)
{
int expire = Tick() + time * TickSpeed();
AddVotebanAddr(m_NetServer.ClientAddr(ClientID), expire);
}
// removes a voteban from a client's address
void CServer::RemoveVotebanClient(int ClientID)
{
RemoveVotebanAddr(m_NetServer.ClientAddr(ClientID));
}
// removes a voteban on an address
void CServer::RemoveVotebanAddr(const NETADDR *addr)
{
CVoteban **v = IsVotebannedAddr(addr);
if(v != NULL)
RemoveVoteban(v);
}
// removes a voteban
void CServer::RemoveVoteban(CVoteban **v)
{
CVoteban *next = (*v)->m_Next;
delete *v;
*v = next;
}
// returns the voteban with the given address if it exists
CServer::CVoteban **CServer::IsVotebannedAddr(const NETADDR *addr)
{
CVoteban **v = &m_Votebans;
while(*v != NULL)
{
// only check ip-type and ip, not port
if((*v)->m_Addr.type == addr->type && !mem_comp(&(*v)->m_Addr.ip, &addr->ip, sizeof(unsigned char[16])))
return v;
v = &(*v)->m_Next;
}
return NULL;
}
// removes expired votebans
void CServer::CleanVotebans()
{
CVoteban **v = &m_Votebans;
while(*v != NULL)
{
if((*v)->m_Expire <= Tick())
{
CVoteban *next = (*v)->m_Next;
delete *v;
*v = next;
}
else
v = &(*v)->m_Next;
}
}
void CServer::ConVoteban(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
int ClientID = pResult->GetInteger(0);
if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
{
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Invalid ClientID");
return;
}
int time = (pResult->NumArguments() > 1) ? pResult->GetInteger(1) : 300;
pThis->AddVoteban(ClientID, time);
// message to console and chat
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "'%s' has been banned from voting for %d:%02d min.", pThis->ClientName(ClientID), time/60, time%60);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
pThis->GameServer()->InformPlayers(aBuf);
}
void CServer::ConUnvoteban(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
// index to unvoteban
int index = pResult->GetInteger(0);
CVoteban **v = &pThis->m_Votebans;
for(int c = 0; *v != NULL; ++c)
{
// index found
if(index == c)
{
char aBuf[128], aAddrStr[NETADDR_MAXSTRSIZE];
// print to console
net_addr_str(&(*v)->m_Addr, aAddrStr, sizeof(aAddrStr), false);
str_format(aBuf, sizeof(aBuf), "%s has been un-votebanned.", aAddrStr);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
// remove ban
pThis->RemoveVoteban(v);
// don't look any further
return;
}
v = &(*v)->m_Next;
}
// not found
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "index was not found, please use 'votebans' to obtain an index");
}
void CServer::ConUnvotebanClient(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
int ClientID = pResult->GetInteger(0);
if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY)
{
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Invalid ClientID");
return;
}
pThis->RemoveVotebanClient(ClientID);
// message to console
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "'%s' has been un-votebanned.", pThis->ClientName(ClientID));
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
void CServer::ConVotebans(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
char aBuf[128];
char aAddrStr[NETADDR_MAXSTRSIZE];
int time;
int count = 0;
pThis->CleanVotebans();
CVoteban *v = pThis->m_Votebans;
NETADDR addr;
while(v != NULL)
{
addr.type = v->m_Addr.type;
mem_copy(addr.ip, v->m_Addr.ip, sizeof(unsigned char[16]));
net_addr_str(&addr, aAddrStr, sizeof(aAddrStr), false);
time = (v->m_Expire - pThis->Tick()) / pThis->TickSpeed();
str_format(aBuf, sizeof(aBuf), "#%d addr=%s time=%d:%02d min", count++, aAddrStr, time/60, time%60);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
v = v->m_Next;
}
str_format(aBuf, sizeof(aBuf), "%d votebanned ip(s)", count);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
void CServer::ConAddLogin(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
char aBuf[128];
std::string username(pResult->GetString(0)),
password(pResult->GetString(1));
// insert if doesn't exist
if(pThis->logins.find(username) == pThis->logins.end())
{
pThis->logins[username] = password;
str_format(aBuf, sizeof(aBuf), "Added login for '%s'.", username.c_str());
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
else
{
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Did not overwrite existing login.");
}
}
void CServer::ConRemoveLogin(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
char aBuf[128];
loginiterator loginit;
std::string username(pResult->GetString(0));
// delete if exists
if((loginit = pThis->logins.find(username)) != pThis->logins.end())
{
// check if someone is logged in with this login
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(pThis->m_aClients[i].m_State != CClient::STATE_EMPTY && pThis->m_aClients[i].m_Authed == CServer::AUTHED_SUBADMIN && pThis->m_aClients[i].m_SubAdminAuthName == loginit->first)
{
pThis->rconLogClientOut(i, "You were logged out.");
}
}
// finally delete
pThis->logins.erase(loginit);
str_format(aBuf, sizeof(aBuf), "Removed login '%s'.", username.c_str());
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
else
{
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "No login with that username.");
}
}
void CServer::ConAddInfo(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
int interval = pResult->GetInteger(0);
if(interval < 1 || interval > 1440)
{
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Interval must be between 1 and 1440.");
return;
}
// add info text
CInfoText *t = new CInfoText;
t->m_Interval = interval;
t->m_Text = std::string(pResult->GetString(1));
// insert front
t->m_Next = pThis->m_InfoTexts;
pThis->m_InfoTexts = t;
pThis->UpdateInfoTexts();
// message to console
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Added the following info text: %s", t->m_Text.c_str());
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
void CServer::ConRemoveInfo(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
int i = pResult->GetInteger(0);
int count = 0;
bool removed = false;
CInfoText **t = &(pThis->m_InfoTexts);
while(*t != NULL)
{
if(count++ == i)
{
// remove
CInfoText *next = (*t)->m_Next;
delete *t;
*t = next;
removed = true;
break;
}
t = &((*t)->m_Next);
}
pThis->UpdateInfoTexts();
if(removed)
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Info text removed");
else
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Info text not found");
}
void CServer::ConListInfo(IConsole::IResult *pResult, void *pUser)
{
CServer* pThis = static_cast<CServer *>(pUser);
char aBuf[128];
int count = 0;
CInfoText *t = pThis->m_InfoTexts;
while(t != NULL)
{
str_format(aBuf, sizeof(aBuf), "#%d interval=%d min text='%s'", count++, t->m_Interval, t->m_Text.c_str());
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
t = t->m_Next;
}
str_format(aBuf, sizeof(aBuf), "%d info text(s)", count);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
void CServer::UpdateInfoTexts()
{
// in case that there are no info texts
if(m_InfoTexts == NULL)
{
m_InfoTextInterval = -1; // no interval
return;
}
// need some random numbers later
init_rand();
// update interval, set it to LCM(all text intervals)
m_InfoTextInterval = 1; // lowest possible interval
CInfoText *t = m_InfoTexts;
while(t != NULL)
{
m_InfoTextInterval = lcm(m_InfoTextInterval, t->m_Interval);
t->m_IntervalTicks = TickSpeed() * 60 * t->m_Interval; // min to ticks
t->m_NextTick = rand() % t->m_IntervalTicks; // variance
t = t->m_Next;
}
m_InfoTextInterval *= TickSpeed() * 60; // min to ticks
// count total number of messages per interval
int numMsg = 0;
t = m_InfoTexts;
while(t != NULL)
{
numMsg += m_InfoTextInterval / t->m_IntervalTicks;
t = t->m_Next;
}
// interval between messages
m_InfoTextMsgInterval = m_InfoTextInterval / numMsg;
// additional pause to sync interval and msg interval
m_InfoTextIntervalPause = m_InfoTextInterval % numMsg;
}
std::string CServer::GetNextInfoText()
{
CInfoText *selectedText = NULL,
*t = m_InfoTexts;
// the counter r keeps track of how many equally due texts there are and actually helps so that every text has the same chance even though there are r-1 random decisions
int r = 1;
while(t != NULL)
{
// use this text if none is selected or this one is more due than the previously selected
// in the case that both are equally due, select one random
if(selectedText == NULL
|| t->m_NextTick < selectedText->m_NextTick
|| (t->m_NextTick == selectedText->m_NextTick && (rand() % ++r) == 0))
selectedText = t;
t = t->m_Next;
}
// return empty string if no text applies
if(selectedText == NULL)
return std::string();
// update tick and return string
selectedText->m_NextTick += selectedText->m_IntervalTicks;
return selectedText->m_Text;
}
void CServer::ConKick(IConsole::IResult *pResult, void *pUser)
{
if(pResult->NumArguments() > 1)
{
char aBuf[128];
str_format(aBuf, sizeof(aBuf), "Kicked (%s)", pResult->GetString(1));
((CServer *)pUser)->Kick(pResult->GetInteger(0), aBuf);
}
else
((CServer *)pUser)->Kick(pResult->GetInteger(0), "Kicked by console");
}
void CServer::ConStatus(IConsole::IResult *pResult, void *pUser)
{
char aBuf[1024];
char aAddrStr[NETADDR_MAXSTRSIZE];
CServer* pThis = static_cast<CServer *>(pUser);
for(int i = 0; i < MAX_CLIENTS; i++)
{
if(pThis->m_aClients[i].m_State != CClient::STATE_EMPTY)
{
net_addr_str(pThis->m_NetServer.ClientAddr(i), aAddrStr, sizeof(aAddrStr), true);
if(pThis->m_aClients[i].m_State == CClient::STATE_INGAME)
{
char bBuf[32];
str_format(bBuf, sizeof(bBuf), "(Subadmin:%s)", pThis->m_aClients[i].m_SubAdminAuthName.c_str());
const char *pAuthStr = pThis->m_aClients[i].m_Authed == CServer::AUTHED_ADMIN ? "(Admin)" :
pThis->m_aClients[i].m_Authed == CServer::AUTHED_SUBADMIN ? bBuf :
pThis->m_aClients[i].m_Authed == CServer::AUTHED_MOD ? "(Mod)" : "";
const char *pAimBotStr = pThis->GameServer()->IsClientAimBot(i) ? "[aimbot]" : "";
str_format(aBuf, sizeof(aBuf), "id=%d addr=%s name='%s' score=%d %s %s client=%d", i, aAddrStr,
pThis->m_aClients[i].m_aName, pThis->m_aClients[i].m_Score, pAuthStr, pAimBotStr, ((CGameContext *)(pThis->GameServer()))->m_apPlayers[i]->m_ClientVersion);
}
else
str_format(aBuf, sizeof(aBuf), "id=%d addr=%s connecting", i, aAddrStr);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
}
}
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", "Quick help: kick <id> <reason=''> | ban <id> <min=5> <reason=''> | voteban <id> <sec=300> | kill <id>");
}
void CServer::ConShutdown(IConsole::IResult *pResult, void *pUser)
{
((CServer *)pUser)->m_RunServer = 0;
}
void CServer::DemoRecorder_HandleAutoStart()
{
if(g_Config.m_SvAutoDemoRecord)
{
m_DemoRecorder.Stop();
char aFilename[128];
char aDate[20];
str_timestamp(aDate, sizeof(aDate));
str_format(aFilename, sizeof(aFilename), "demos/%s_%s.demo", "auto/autorecord", aDate);
m_DemoRecorder.Start(Storage(), m_pConsole, aFilename, GameServer()->NetVersion(), m_aCurrentMap, m_CurrentMapCrc, "server");
if(g_Config.m_SvAutoDemoMax)
{
// clean up auto recorded demos
CFileCollection AutoDemos;
AutoDemos.Init(Storage(), "demos/server", "autorecord", ".demo", g_Config.m_SvAutoDemoMax);
}
}
}
void CServer::MapReload()
{
m_MapReload = 1;
}
bool CServer::DemoRecorder_IsRecording()
{
return m_DemoRecorder.IsRecording();
}
void CServer::ConRecord(IConsole::IResult *pResult, void *pUser)
{
CServer* pServer = (CServer *)pUser;
char aFilename[128];
if(pResult->NumArguments())
str_format(aFilename, sizeof(aFilename), "demos/%s.demo", pResult->GetString(0));
else
{
char aDate[20];
str_timestamp(aDate, sizeof(aDate));
str_format(aFilename, sizeof(aFilename), "demos/demo_%s.demo", aDate);
}
pServer->m_DemoRecorder.Start(pServer->Storage(), pServer->Console(), aFilename, pServer->GameServer()->NetVersion(), pServer->m_aCurrentMap, pServer->m_CurrentMapCrc, "server");
}
void CServer::ConStopRecord(IConsole::IResult *pResult, void *pUser)
{
((CServer *)pUser)->m_DemoRecorder.Stop();
}
void CServer::ConMapReload(IConsole::IResult *pResult, void *pUser)
{
((CServer *)pUser)->m_MapReload = 1;
}
void CServer::ConLogout(IConsole::IResult *pResult, void *pUser)
{
CServer *pServer = (CServer *)pUser;
pServer->rconLogClientOut(pServer->m_RconClientID);
}
void CServer::rconLogClientOut(int ClientID, const char *msg)
{
if(ClientID >= 0 && ClientID < MAX_CLIENTS && m_aClients[ClientID].m_State != CServer::CClient::STATE_EMPTY && IsAuthed(ClientID))
{
CMsgPacker Msg(NETMSG_RCON_AUTH_STATUS);
Msg.AddInt(0); //authed
Msg.AddInt(0); //cmdlist
SendMsgEx(&Msg, MSGFLAG_VITAL, ClientID, true);
m_aClients[ClientID].m_Authed = AUTHED_NO;
m_aClients[ClientID].m_AuthTries = 0;
m_aClients[ClientID].m_pRconCmdToSend = 0;
SendRconLine(ClientID, msg);
char aBuf[32];
str_format(aBuf, sizeof(aBuf), "ClientID=%d logged out", ClientID);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
UpdateLoggedInAdmins();
}
}
void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
if(pResult->NumArguments())
((CServer *)pUserData)->UpdateServerInfo();
}
void CServer::ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
if(pResult->NumArguments())
((CServer *)pUserData)->m_NetServer.SetMaxClientsPerIP(pResult->GetInteger(0));
}
void CServer::ConchainModCommandUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
if(pResult->NumArguments() == 2)
{
CServer *pThis = static_cast<CServer *>(pUserData);
const IConsole::CCommandInfo *pInfo = pThis->Console()->GetCommandInfo(pResult->GetString(0), CFGFLAG_SERVER, false);
int OldAccessLevel = 0;
if(pInfo)
OldAccessLevel = pInfo->GetAccessLevel();
pfnCallback(pResult, pCallbackUserData);
if(pInfo && OldAccessLevel != pInfo->GetAccessLevel())
{
for(int i = 0; i < MAX_CLIENTS; ++i)
{
if(pThis->m_aClients[i].m_State == CServer::CClient::STATE_EMPTY || pThis->m_aClients[i].m_Authed != CServer::AUTHED_MOD ||
(pThis->m_aClients[i].m_pRconCmdToSend && str_comp(pResult->GetString(0), pThis->m_aClients[i].m_pRconCmdToSend->m_pName) >= 0))
continue;
if(OldAccessLevel == IConsole::ACCESS_LEVEL_ADMIN)
pThis->SendRconCmdAdd(pInfo, i);
else
pThis->SendRconCmdRem(pInfo, i);
}
}
}
else
pfnCallback(pResult, pCallbackUserData);
}
void CServer::ConchainConsoleOutputLevelUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
{
pfnCallback(pResult, pCallbackUserData);
if(pResult->NumArguments() == 1)
{
CServer *pThis = static_cast<CServer *>(pUserData);
pThis->Console()->SetPrintOutputLevel(pThis->m_PrintCBIndex, pResult->GetInteger(0));
}
}
void CServer::RegisterCommands()
{
m_pConsole = Kernel()->RequestInterface<IConsole>();
m_pGameServer = Kernel()->RequestInterface<IGameServer>();
m_pMap = Kernel()->RequestInterface<IEngineMap>();
m_pStorage = Kernel()->RequestInterface<IStorage>();
// register console commands
Console()->Register("kick", "i?r", CFGFLAG_SERVER, ConKick, this, "Kick player with specified id for any reason");
Console()->Register("status", "", CFGFLAG_SERVER, ConStatus, this, "List players");
Console()->Register("shutdown", "", CFGFLAG_SERVER, ConShutdown, this, "Shut down");
Console()->Register("logout", "", CFGFLAG_SERVER, ConLogout, this, "Logout of rcon");
Console()->Register("record", "?s", CFGFLAG_SERVER|CFGFLAG_STORE, ConRecord, this, "Record to a file");
Console()->Register("stoprecord", "", CFGFLAG_SERVER, ConStopRecord, this, "Stop recording");
Console()->Register("reload", "", CFGFLAG_SERVER, ConMapReload, this, "");
Console()->Chain("sv_name", ConchainSpecialInfoupdate, this);
Console()->Chain("sv_name_admin", ConchainSpecialInfoupdate, this);
Console()->Chain("password", ConchainSpecialInfoupdate, this);
Console()->Chain("sv_max_clients_per_ip", ConchainMaxclientsperipUpdate, this);
Console()->Chain("mod_command", ConchainModCommandUpdate, this);
Console()->Chain("console_output_level", ConchainConsoleOutputLevelUpdate, this);
Console()->Register("voteban", "i?i", CFGFLAG_SERVER, ConVoteban, this, "Voteban a player by id");
Console()->Register("unvoteban", "i", CFGFLAG_SERVER, ConUnvoteban, this, "Remove voteban by index in list votebans");
Console()->Register("unvoteban_client", "i", CFGFLAG_SERVER, ConUnvotebanClient, this, "Remove voteban by player id");
Console()->Register("votebans", "", CFGFLAG_SERVER, ConVotebans, this, "Show all votebans");
Console()->Register("add_login", "ss", CFGFLAG_SERVER, ConAddLogin, this, "Add a subadmin login. The rcon password will be user:pass with no additional spaces.", IConsole::ACCESS_LEVEL_ADMIN);
Console()->Register("remove_login", "s", CFGFLAG_SERVER, ConRemoveLogin, this, "Remove a subadmin login", IConsole::ACCESS_LEVEL_ADMIN);
Console()->Register("add_info", "is", CFGFLAG_SERVER, ConAddInfo, this, "Add a info text that is printed in the chat repeatedly in the given interval of minutes.");
Console()->Register("remove_info", "i", CFGFLAG_SERVER, ConRemoveInfo, this, "Remove a info text");
Console()->Register("list_info", "", CFGFLAG_SERVER, ConListInfo, this, "Show all info texts");
// register console commands in sub parts
m_ServerBan.InitServerBan(Console(), Storage(), this);
m_pGameServer->OnConsoleInit();
}
int CServer::SnapNewID()
{
return m_IDPool.NewID();
}
void CServer::SnapFreeID(int ID)
{
m_IDPool.FreeID(ID);
}
void *CServer::SnapNewItem(int Type, int ID, int Size)
{
dbg_assert(Type >= 0 && Type <=0xffff, "incorrect type");
dbg_assert(ID >= 0 && ID <=0xffff, "incorrect id");
return ID < 0 ? 0 : m_SnapshotBuilder.NewItem(Type, ID, Size);
}
void CServer::SnapSetStaticsize(int ItemType, int Size)
{
m_SnapshotDelta.SetStaticsize(ItemType, Size);
}
static CServer *CreateServer() { return new CServer(); }
int main(int argc, const char **argv) // ignore_convention
{
#if defined(CONF_FAMILY_WINDOWS)
for(int i = 1; i < argc; i++) // ignore_convention
{
if(str_comp("-s", argv[i]) == 0 || str_comp("--silent", argv[i]) == 0) // ignore_convention
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
break;
}
}
#endif
if(secure_random_init() != 0)
{
dbg_msg("secure", "could not initialize secure RNG");
return -1;
}
CServer *pServer = CreateServer();
IKernel *pKernel = IKernel::Create();
// create the components
IEngine *pEngine = CreateEngine("Teeworlds");
IEngineMap *pEngineMap = CreateEngineMap();
IGameServer *pGameServer = CreateGameServer();
IConsole *pConsole = CreateConsole(CFGFLAG_SERVER|CFGFLAG_ECON);
IEngineMasterServer *pEngineMasterServer = CreateEngineMasterServer();
IStorage *pStorage = CreateStorage("Teeworlds", IStorage::STORAGETYPE_SERVER, argc, argv); // ignore_convention
IConfig *pConfig = CreateConfig();
pServer->InitRegister(&pServer->m_NetServer, pEngineMasterServer, pConsole);
{
bool RegisterFail = false;
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pServer); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngine);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMap*>(pEngineMap)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMap*>(pEngineMap));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pGameServer);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMasterServer*>(pEngineMasterServer)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer*>(pEngineMasterServer));
if(RegisterFail)
return -1;
}
pEngine->Init();
pConfig->Init();
pEngineMasterServer->Init();
pEngineMasterServer->Load();
// register all console commands
pServer->RegisterCommands();
/* This banmaster is added into the source of the server that i'm able to ban players even if no banmaster.cfg is used.
* Often serverhoster doesn't add this file because they don't know what it is for and remove it, not
* because they don't want it. If so, set sv_global_bantime to 0 or use a custom banmasters.cfg with "clear_banmasters"
* in first line or in normal config.
* ## For a Teeworlds without bots \o/ ##
*/
pConsole->ExecuteLine("add_banmaster banmaster.teetw.de");
// execute autoexec file
pConsole->ExecuteFile("autoexec.cfg");
// parse the command line arguments
if(argc > 1) // ignore_convention
pConsole->ParseArguments(argc-1, &argv[1]); // ignore_convention
// restore empty config strings to their defaults
pConfig->RestoreStrings();
pEngine->InitLogfile();
#if defined(CONF_FAMILY_UNIX)
FifoConsole *fifoConsole = new FifoConsole(pConsole, g_Config.m_SvInputFifo, CFGFLAG_SERVER);
#endif
// run the server
dbg_msg("server", "starting...");
pServer->Run();
// free
#if defined(CONF_FAMILY_UNIX)
delete fifoConsole;
#endif
delete pServer;
delete pKernel;
delete pEngineMap;
delete pGameServer;
delete pConsole;
delete pEngineMasterServer;
delete pStorage;
delete pConfig;
return 0;
}
|