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
|
/* FCE Ultra Network Play Server
*
* Copyright notice for this file:
* Copyright (C) 2004 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <exception>
#include "types.h"
#include "md5.h"
#include "throttle.h"
#define VERSION "0.0.5"
#define DEFAULT_PORT 4046
#define DEFAULT_MAX 100
#define DEFAULT_TIMEOUT 5
#define DEFAULT_FRAMEDIVISOR 1
#define DEFAULT_CONFIG "/etc/fceux-server.conf"
// MSG_NOSIGNAL and SOL_TCP have been depreciated on osx
#if defined (__APPLE__) || defined(BSD)
#define MSG_NOSIGNAL SO_NOSIGPIPE
#define SOL_TCP IPPROTO_TCP
#endif
typedef struct {
uint32 id; /* mainly for faster referencing when pointed to from the Games
entries.
*/
char *nickname;
int TCPSocket;
void *game; /* Pointer to the game this player is in. */
int localplayers; /* The number of local players, 1-4 */
time_t timeconnect; /* Time the client made the connection. */
/* Variables to handle non-blocking TCP reads. */
uint8 *nbtcp;
uint32 nbtcphas, nbtcplen;
uint32 nbtcptype;
} ClientEntry;
typedef struct
{
uint8 id[16]; /* Unique 128-bit identifier for this game session. */
uint8 joybuf[5]; /* 4 player data + 1 command byte */
int MaxPlayers; /* Maximum players for this game */
ClientEntry *Players[4]; /* Pointers to player data. */
int IsUnique[4]; /* Set to 1 if player is unique client, 0
if it's a duplicated entry to handle multiple players
per client.
*/
uint8 ExtraInfo[64]; /* Expansion information to be used in future versions
of FCE Ultra.
*/
} GameEntry;
typedef struct
{
unsigned int MaxClients; /* The maximum number of clients to allow. */
/* You should expect each client to use
65-70Kbps(not including save state loads) while
connected(and logged in), when using the default
FrameDivisor value of 1.
*/
unsigned int ConnectTimeout; /* How long to wait(in seconds) for the client to provide
login data before disconnecting the client.
*/
unsigned int FrameDivisor; /* The number of updates per second are divided
by this number. 1 = 60 updates/sec(approx),
2 = 30 updates/sec, etc. */
unsigned int Port; /* The port to listen on. */
uint8 *Password; /* The server password. */
} CONFIG;
CONFIG ServerConfig;
int LoadConfigFile(const char *fn)
{
FILE *fp;
ServerConfig.Port = ServerConfig.MaxClients = ServerConfig.ConnectTimeout = ServerConfig.FrameDivisor = ~0;
if(fp=fopen(fn,"rb"))
{
char buf[256];
while(fgets(buf, 256, fp) != NULL)
{
if(!strncasecmp(buf,"maxclients",strlen("maxclients")))
sscanf(buf,"%*s %d",&ServerConfig.MaxClients);
else if(!strncasecmp(buf,"connecttimeout",strlen("connecttimeout")))
sscanf(buf,"%*s %d",&ServerConfig.ConnectTimeout);
else if(!strncasecmp(buf,"framedivisor",strlen("framedivisor")))
sscanf(buf,"%*s %d",&ServerConfig.FrameDivisor);
else if(!strncasecmp(buf,"port",strlen("port")))
sscanf(buf,"%*s %d",&ServerConfig.Port);
else if(!strncasecmp(buf,"password",strlen("password")))
{
char *pass = 0;
sscanf(buf,"%*s %as",&pass);
if(pass)
{
struct md5_context md5;
ServerConfig.Password = (uint8 *)malloc(16);
md5_starts(&md5);
md5_update(&md5,(uint8*)pass,strlen(pass));
md5_finish(&md5,ServerConfig.Password);
puts("Password required to log in.");
}
}
}
}
else
{
printf("Cannot load configuration file %s.\n", fn);
return(0);
}
if(~0 == (ServerConfig.Port & ServerConfig.MaxClients & ServerConfig.ConnectTimeout & ServerConfig.FrameDivisor))
{
puts("Incomplete configuration file");
return(0);
}
//printf("%d,%d,%d\n",ServerConfig.MaxClients,ServerConfig.ConnectTimeout,ServerConfig.FrameDivisor);
printf("Using configuration file located at %s\n", fn);
return(1);
}
static ClientEntry *Clients;
static GameEntry *Games;
static void en32(uint8 *buf, uint32 morp)
{
buf[0]=morp;
buf[1]=morp>>8;
buf[2]=morp>>16;
buf[3]=morp>>24;
}
static uint32 de32(uint8 *morp)
{
return(morp[0]|(morp[1]<<8)|(morp[2]<<16)|(morp[3]<<24));
}
static char *CleanNick(char *nick);
static int NickUnique(ClientEntry *client);
static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64]) throw();
static void SendToAll(GameEntry *game, int cmd, uint8 *data, uint32 len) throw();
static void BroadcastText(GameEntry *game, const char *fmt, ...) throw();
static void TextToClient(ClientEntry *client, const char *fmt, ...) throw();
static void KillClient(ClientEntry *client);
#define NBTCP_LOGINLEN 0x100
#define NBTCP_LOGIN 0x200
#define NBTCP_COMMANDLEN 0x300
#define NBTCP_COMMAND 0x400
#define NBTCP_UPDATEDATA 0x800
static void StartNBTCPReceive(ClientEntry *client, uint32 type, uint32 len)
{
client->nbtcp = (uint8 *)malloc(len);
client->nbtcplen = len;
client->nbtcphas = 0;
client->nbtcptype = type;
}
static void RedoNBTCPReceive(ClientEntry *client)
{
client->nbtcphas = 0;
}
static void EndNBTCPReceive(ClientEntry *client)
{
free(client->nbtcp);
client->nbtcplen = client->localplayers;
client->nbtcphas = 0;
client->nbtcptype = 0;
client->nbtcp = 0;
}
static uint8 *MakeMPS(ClientEntry *client)
{
static uint8 buf[64];
uint8 *bp = buf;
int x;
GameEntry *game = (GameEntry *)client->game;
for(x=0;x<4;x++)
{
if(game->Players[x] == client)
{
*bp = '0' + x + 1;
bp++;
*bp = ',';
bp++;
}
}
if(*(bp-1) == ',') bp--;
*bp = 0;
return(buf);
}
/* Returns 1 if we are back to normal game mode, 0 if more data is yet to arrive. */
static int CheckNBTCPReceive(ClientEntry *client) throw()
{
if(!client->nbtcplen)
throw(1); /* Should not happen. */
int l;
while(l = recv(client->TCPSocket, client->nbtcp + client->nbtcphas, client->nbtcplen - client->nbtcphas, MSG_NOSIGNAL))
{
if(l == -1)
{
if(errno == EAGAIN || errno == EWOULDBLOCK)
break;
throw(1); /* Die now. NOW. */
}
client->nbtcphas += l;
//printf("Read: %d, %04x, %d, %d\n",l,client->nbtcptype,client->nbtcphas, client->nbtcplen);
/* We're all full. Yippie. */
if(client->nbtcphas == client->nbtcplen)
{
uint32 len;
switch(client->nbtcptype & 0xF00)
{
case NBTCP_UPDATEDATA:
{
GameEntry *game = (GameEntry *)client->game;
int x, wx;
if(client->nbtcp[0] == 0xFF)
{
EndNBTCPReceive(client);
StartNBTCPReceive(client, NBTCP_COMMANDLEN, 5);
return(1);
}
for(x=0,wx=0; x < 4; x++)
{
if(game->Players[x] == client)
{
game->joybuf[x] = client->nbtcp[wx];
wx++;
}
}
RedoNBTCPReceive(client);
}
return(1);
case NBTCP_COMMANDLEN:
{
uint8 cmd = client->nbtcp[4];
len = de32(client->nbtcp);
if(len > 200000) /* Sanity check. */
throw(1);
//printf("%02x, %d\n",cmd,len);
if(!len && !(cmd&0x80))
{
SendToAll((GameEntry*)client->game, client->nbtcp[4], 0, 0);
EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
}
else if(client->nbtcp[4]&0x80)
{
EndNBTCPReceive(client);
if(len)
{
StartNBTCPReceive(client,NBTCP_COMMAND | cmd,len);
}
else
{
/* Woops. Client probably tried to send a text message of 0 length.
Or maybe a 0-length cheat file? Better be safe! */
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
}
}
else throw(1);
return(1);
}
case NBTCP_COMMAND:
{
len = client->nbtcplen;
uint32 tocmd = client->nbtcptype & 0xFF;
if(tocmd == 0x90) /* Text */
{
char *ma, *ma2;
ma = (char *) malloc(len + 1);
memcpy(ma, client->nbtcp, len);
ma[len] = 0;
asprintf(&ma2, "<%s> %s",client->nickname,ma);
free(ma);
ma = ma2;
len=strlen(ma);
SendToAll((GameEntry*)client->game, tocmd, (uint8 *)ma, len);
free(ma);
}
else
{
SendToAll((GameEntry*)client->game, tocmd, client->nbtcp, len);
}
EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
return(1);
}
case NBTCP_LOGINLEN:
len = de32(client->nbtcp);
if(len > 1024 || len < (16 + 16 + 64 + 1))
throw(1);
EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_LOGIN,len);
return(1);
case NBTCP_LOGIN:
{
uint32 len;
uint8 gameid[16];
uint8 *sexybuf;
uint8 extra[64];
len = client->nbtcplen;
sexybuf = client->nbtcp;
/* Game ID(MD5'd game MD5 and password on client side). */
memcpy(gameid, sexybuf, 16);
sexybuf += 16;
len -= 16;
if(ServerConfig.Password)
if(memcmp(ServerConfig.Password,sexybuf,16))
{
TextToClient(client,"Invalid server password.");
throw(1);
}
sexybuf += 16;
len -= 16;
memcpy(extra, sexybuf, 64);
sexybuf += 64;
len -= 64;
client->localplayers = *sexybuf;
if(client->localplayers < 1 || client->localplayers > 4)
{
TextToClient(client,"Invalid number(%d) of local players!",client->localplayers);
throw(1);
}
sexybuf++;
len -= 1;
AddClientToGame(client, gameid, extra);
/* Get the nickname */
if(len)
{
client->nickname = (char *)malloc(len + 1);
memcpy(client->nickname, sexybuf, len);
client->nickname[len] = 0;
if((client->nickname = CleanNick(client->nickname)))
if(!NickUnique(client)) /* Nickname already exists */
{
free(client->nickname);
client->nickname = 0;
}
}
uint8 *mps = MakeMPS(client);
if(!client->nickname)
asprintf(&client->nickname,"*Player %s",mps);
printf("Client %d assigned to game %d as player %s <%s>\n",client->id,(GameEntry*)client->game - Games,mps, client->nickname);
int x;
GameEntry *tg=(GameEntry *)client->game;
for(x=0; x<tg->MaxPlayers; x++)
{
if(tg->Players[x] && tg->IsUnique[x])
{
if(tg->Players[x] != client)
{
try
{
TextToClient(tg->Players[x], "* Player %s has just connected as: %s",MakeMPS(client),client->nickname);
}
catch(int i)
{
KillClient(tg->Players[x]);
}
TextToClient(client, "* Player %s is already connected as: %s",MakeMPS(tg->Players[x]),tg->Players[x]->nickname);
}
else
TextToClient(client, "* You(Player %s) have just connected as: %s",MakeMPS(client),client->nickname);
}
}
}
EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
return(1);
}
}
}
return(0);
}
int ListenSocket;
static char *CleanNick(char *nick)
{
int x;
for(x=0; x<strlen(nick); x++)
if(nick[x] == '<' || nick[x] == '>' || nick[x] == '*' || (nick[x] < 0x20))
nick[x] = 0;
if(!strlen(nick))
{
free(nick);
nick = 0;
}
return(nick);
}
static int NickUnique(ClientEntry *client)
{
int x;
GameEntry *game = (GameEntry *)client-> game;
for(x=0; x<game->MaxPlayers; x++)
if(game->Players[x] && client != game->Players[x])
if(!strcasecmp(client->nickname, game->Players[x]->nickname))
return(0);
return(1);
}
static int MakeSendTCP(ClientEntry *client, uint8 *data, uint32 len) throw()
{
if(send(client->TCPSocket, data, len, MSG_NOSIGNAL) != len)
throw(1);
return(1);
}
static void SendToAll(GameEntry *game, int cmd, uint8 *data, uint32 len) throw()
{
uint8 poo[5];
int x;
poo[4] = cmd;
for(x=0;x<game->MaxPlayers;x++)
{
if(!game->Players[x] || !game->IsUnique[x]) continue;
try
{
if(cmd & 0x80)
en32(poo, len);
MakeSendTCP(game->Players[x],poo,5);
if(cmd & 0x80)
{
MakeSendTCP(game->Players[x], data, len);
}
}
catch(int i)
{
KillClient(game->Players[x]);
}
}
}
static void TextToClient(ClientEntry *client, const char *fmt, ...) throw()
{
char *moo;
va_list ap;
va_start(ap,fmt);
vasprintf(&moo, fmt, ap);
va_end(ap);
uint8 poo[5];
uint32 len;
poo[4] = 0x90;
len = strlen(moo);
en32(poo, len);
MakeSendTCP(client, poo, 5);
MakeSendTCP(client, (uint8*)moo, len);
free(moo);
}
static void BroadcastText(GameEntry *game, const char *fmt, ...) throw()
{
char *moo;
va_list ap;
va_start(ap,fmt);
vasprintf(&moo, fmt, ap);
va_end(ap);
SendToAll(game, 0x90,(uint8 *)moo,strlen(moo));
free(moo);
}
static void KillClient(ClientEntry *client)
{
GameEntry *game;
uint8 *mps;
char *bmsg;
game = (GameEntry *)client->game;
if(game)
{
int w;
int tc = 0;
for(w=0;w<game->MaxPlayers;w++)
if(game->Players[w]) tc++;
for(w=0;w<game->MaxPlayers;w++)
if(game->Players[w])
if(game->Players[w] == client)
game->Players[w] = NULL;
time_t curtime = time(0);
printf("Player <%s> disconnected from game %d on %s",client->nickname,game-Games,ctime(&curtime));
asprintf(&bmsg, "* Player %s <%s> left.",MakeMPS(client),client->nickname);
if(tc == client->localplayers) /* If total players for this game = total local
players for this client, destroy the game.
*/
{
printf("Game %d destroyed.\n",game-Games);
memset(game, 0, sizeof(GameEntry));
game = 0;
}
}
else
{
time_t curtime = time(0);
printf("Unassigned client %d disconnected on %s",client->id, ctime(&curtime));
}
if(client->nbtcp)
free(client->nbtcp);
if(client->nickname)
free(client->nickname);
if(client->TCPSocket != -1)
close(client->TCPSocket);
memset(client, 0, sizeof(ClientEntry));
client->TCPSocket = -1;
if(game)
BroadcastText(game,"%s",bmsg);
}
static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64]) throw()
{
int wg;
GameEntry *game,*fegame;
retry:
game = NULL;
fegame = NULL;
/* First, find an available game. */
for(wg=0; wg<ServerConfig.MaxClients; wg++)
{
if(!Games[wg].MaxPlayers && !fegame)
{
if(!fegame)
fegame=&Games[wg];
}
else if(!memcmp(Games[wg].id,id,16)) /* A match was found! */
{
game = &Games[wg];
break;
}
}
if(!game) /* Hmm, no game found. Guess we'll have to create one. */
{
game=fegame;
printf("Game %d added\n",game-Games);
memset(game, 0, sizeof(GameEntry));
game->MaxPlayers = 4;
memcpy(game->id, id, 16);
memcpy(game->ExtraInfo, extra, 64);
}
int n;
for(n = 0; n < game->MaxPlayers; n++)
if(game->Players[n])
{
try
{
uint8 b[5];
b[4] = 0x81;
MakeSendTCP(game->Players[n], b, 5);
break;
}
catch(int i)
{
KillClient(game->Players[n]);
/* All right, now the client we sent the request to is killed. I HOPE YOU'RE HAPPY! */
/* Since killing a client can destroy a game, we'll need to see if the game was trashed.
If it was, then "goto retry", and try again. Ugly, yes. I LIKE UGLY.
*/
if(!game->MaxPlayers)
goto retry;
}
}
int instancecount = client->localplayers;
for(n=0; n<game->MaxPlayers; n++)
{
if(!game->Players[n])
{
game->Players[n] = client;
if(instancecount == client->localplayers)
game->IsUnique[n] = 1;
else
game->IsUnique[n] = 0;
instancecount --;
if(!instancecount)
break;
}
}
/* Game is full. */
if(n == game->MaxPlayers)
{
for(n=0; n<game->MaxPlayers; n++)
if(game->Players[n] == client)
game->Players[n] = 0;
TextToClient(client, "Sorry, game is full. %d instance(s) tried, %d available.",client->localplayers,client->localplayers - instancecount);
throw(1);
}
client->game = (void *)game;
}
int main(int argc, char *argv[])
{
struct sockaddr_in sockin;
socklen_t sockin_len;
int i;
char* pass = 0;
/* If we can't load the default config file, use some defined values */
if(!LoadConfigFile(DEFAULT_CONFIG))
{
ServerConfig.Port = DEFAULT_PORT;
ServerConfig.MaxClients = DEFAULT_MAX;
ServerConfig.ConnectTimeout = DEFAULT_TIMEOUT;
ServerConfig.FrameDivisor = DEFAULT_FRAMEDIVISOR;
}
char* configfile = 0;
for(i=1; i<argc ;i++)
{
if(!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{
printf("Usage: %s [OPTION]...\n" ,argv[0]);
printf("Begins the FCE Ultra game server with given options.\n");
printf("This server will first look in %s for options. If that \nfile does not exist, it will use the defaults given here. Any argument given \ndirectly will override any default values.\n\n", DEFAULT_CONFIG);
printf("-h\t--help\t\tDisplays this help message.\n");
printf("-v\t--version\t\tDisplays the version number and quits.\n");
printf("-p\t--port\t\tStarts server on given port. (default=%d)\n", DEFAULT_PORT);
printf("-w\t--password\tSpecifies a password for entry.\n");
printf("-m\t--maxclients\tSpecifies the maximum amount of clients allowed \n\t\t\tto access the server. (default=%d)\n", DEFAULT_MAX);
printf("-t\t--timeout\tSpecifies the amount of seconds before the server \n\t\t\ttimes out. (default=%d)\n", DEFAULT_TIMEOUT);
printf("-f\t--framedivisor\tSpecifies frame divisor.\n\t\t\t(eg: 60 / framedivisor = updates per second)(default=%d)\n", DEFAULT_FRAMEDIVISOR);
printf("-c\t--configfile\tLoads the given configuration file.\n");
return -1;
}
if(!strcmp(argv[i], "--port") || !strcmp(argv[i], "-p"))
{
i++;
if(argc == i)
{
printf("Please specify a port to use.\n");
return -1;
}
ServerConfig.Port = atoi(argv[i]);
continue;
}
if(!strcmp(argv[i], "--password") || !strcmp(argv[i], "-w"))
{
i++;
if(argc == i)
{
printf("Please specify a password.\n");
return -1;
}
pass = argv[i];
struct md5_context md5;
ServerConfig.Password = (uint8 *)malloc(16);
md5_starts(&md5);
md5_update(&md5,(uint8*)pass,strlen(pass));
md5_finish(&md5,ServerConfig.Password);
puts("Password required to log in.");
continue;
}
if(!strcmp(argv[i], "--maxclients") || !strcmp(argv[i], "-m"))
{
i++;
if(argc == i)
{
printf("Please specify the maximium amount of clients.\n");
return -1;
}
ServerConfig.MaxClients = atoi(argv[i]);
continue;
}
if(!strcmp(argv[i], "--timeout") || !strcmp(argv[i], "-t"))
{
i++;
if(argc == i)
{
printf("Please specify the connection timeout time in seconds.\n");
return -1;
}
ServerConfig.ConnectTimeout = atoi(argv[i]);
continue;
}
if(!strcmp(argv[i], "--framedivisor") || !strcmp(argv[i], "-f"))
{
i++;
if(argc == i)
{
printf("Please specify a framedivisor.\n");
return -1;
}
ServerConfig.FrameDivisor = atoi(argv[i]);
continue;
}
if(!strcmp(argv[i], "--configfile") || !strcmp(argv[i], "-c"))
{
i++;
if(argc == i)
{
printf("Please specify a configfile to load.\n");
return -1;
}
if(!LoadConfigFile(argv[i]))
{
puts("Error loading configuration file.");
exit(-1);
}
continue;
}
if(!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v"))
{
printf("FCE Ultra network server version %s\n", VERSION);
return 0;
}
/* If we've gotten here, we don't know what the argument is */
printf("Invalid parameter: %s\n", argv[i]);
return -1;
}
Games = (GameEntry *)malloc(sizeof(GameEntry) * ServerConfig.MaxClients);
Clients = (ClientEntry *)malloc(sizeof(ClientEntry) * ServerConfig.MaxClients);
memset(Games,0,sizeof(GameEntry) * ServerConfig.MaxClients);
memset(Clients,0,sizeof(ClientEntry) * ServerConfig.MaxClients);
{
int x;
for(x=0; x<ServerConfig.MaxClients; x++)
Clients[x].TCPSocket = -1;
}
RefreshThrottleFPS(ServerConfig.FrameDivisor);
/* First, we need to create a socket to listen on. */
ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
/* Set send buffer size to 262,144 bytes. */
int sndbufsize = 262144;
if(setsockopt(ListenSocket, SOL_SOCKET, SO_SNDBUF, &sndbufsize, sizeof(int)))
printf("Send buffer size set failed: %s",strerror(errno));
int tcpopt = 1;
if(setsockopt(ListenSocket, SOL_TCP, TCP_NODELAY, &tcpopt, sizeof(int)))
{
printf("Nodelay fail: %s",strerror(errno));
exit(-1);
}
memset(&sockin, 0, sizeof(sockin));
sockin.sin_family = AF_INET;
sockin.sin_addr.s_addr = INADDR_ANY;
sockin_len = sizeof(sockin);
sockin.sin_port = htons(ServerConfig.Port);
printf("Binding to port %d... ",ServerConfig.Port);
if(bind(ListenSocket, (struct sockaddr *)&sockin, sockin_len))
{
printf("Error: %s\n",strerror(errno));
exit(-1);
}
puts("Ok");
printf("Listening on socket... ");
if(listen(ListenSocket, 8))
{
printf("Error: %s",strerror(errno));
exit(-1);
}
puts("Ok");
/* We don't want to block on accept() */
fcntl(ListenSocket, F_SETFL, fcntl(ListenSocket, F_GETFL) | O_NONBLOCK);
/* Now for the BIG LOOP. */
while(1)
{
int n;
for(n=0; n<ServerConfig.MaxClients; n++)
{
if(Clients[n].TCPSocket != -1) continue;
if((Clients[n].TCPSocket = accept(ListenSocket, (struct sockaddr *)&sockin, &sockin_len)) != -1)
{
struct sockaddr_in usockin;
/* We have a new client. Yippie. */
fcntl(Clients[n].TCPSocket, F_SETFL, fcntl(Clients[n].TCPSocket, F_GETFL) | O_NONBLOCK);
Clients[n].timeconnect = time(0);
Clients[n].id = n;
printf("Client %d connecting from %s on %s",n,inet_ntoa(sockin.sin_addr),ctime(&Clients[n].timeconnect));
{
uint8 buf[1];
buf[0] = ServerConfig.FrameDivisor;
send(Clients[n].TCPSocket,buf,1,MSG_NOSIGNAL);
}
StartNBTCPReceive(&Clients[n], NBTCP_LOGINLEN, 4);
}
}
/* Check for users still in the login process(not yet assigned a game). BOING */
time_t curtime = time(0);
for(n = 0; n < ServerConfig.MaxClients; n++)
try
{
if(Clients[n].TCPSocket != -1 && !Clients[n].game)
{
if((Clients[n].timeconnect + ServerConfig.ConnectTimeout) < curtime)
{
KillClient(&Clients[n]);
}
else
while(CheckNBTCPReceive(&Clients[n])) {};
}
}
catch(int i)
{
KillClient(&Clients[n]);
}
int whichgame;
for(whichgame = 0; whichgame < ServerConfig.MaxClients; whichgame ++)
{
/* Now, the loop to get data from each client. Meep. */
for(n = 0; n < Games[whichgame].MaxPlayers; n++)
{
try
{
ClientEntry *client = Games[whichgame].Players[n];
if(!client || !Games[whichgame].IsUnique[n]) continue;
int localplayers = client->localplayers;
while(CheckNBTCPReceive(client)) {};
} // catch
catch(int i)
{
KillClient(Games[whichgame].Players[n]);
}
} // A games clients
/* Now we send the data to all the clients. */
for(n = 0; n < Games[whichgame].MaxPlayers; n++)
{
if(!Games[whichgame].Players[n] || !Games[whichgame].IsUnique[n]) continue;
try
{
MakeSendTCP(Games[whichgame].Players[n], Games[whichgame].joybuf, 5);
}
catch(int i)
{
KillClient(Games[whichgame].Players[n]);
}
} // A game's clients
} // Games
SpeedThrottle();
} // while(1)
}
|