Guest User

Untitled

a guest
Oct 20th, 2017
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 60.54 KB | None | 0 0
  1. // nfsuserver2.cpp : Defines the entry point for the console application.
  2. //
  3. #ifdef _WIN32
  4.     #pragma comment(lib, "ws2_32.lib")
  5. #endif
  6.  
  7. #include "win_nix.h"
  8. #include "objects.h"
  9.  
  10. #ifndef _WIN32
  11.  
  12. int stricmp( const char *b, const char *a ){
  13.     while (*a != '\0' && *b != '\0' && tolower(*a) == tolower(*b)){
  14.         a++;
  15.         b++;
  16.     }
  17.  
  18.     return *a == *b ? 0 : tolower(*a)<tolower(*b) ? -1 : 1;
  19. }
  20. #endif
  21.  
  22. #ifdef NT_SERVICE
  23.  
  24. FILE * g_LogFile;
  25. char g_Msg[255];
  26.  
  27. SERVICE_STATUS ServiceStatus;
  28. SERVICE_STATUS_HANDLE ServiceStatusHandle;
  29. #endif
  30.  
  31. bool EnableLogFile;
  32. bool EnableLogScreen;
  33. bool RewriteLogFile;
  34. bool DisableTimeStamp;
  35. bool Verbose;
  36. bool RegisterGlobal;
  37. bool LogAllTraffic;
  38.  
  39. bool BanV1;
  40. bool BanV2;
  41. bool BanV3;
  42. bool BanV4;
  43.  
  44. FILE * logfil = NULL;  //file pointer for logfile
  45. char logtemp[1100];    //temp variable for logging
  46. FILE * tlogfil = NULL; //file pointer for traffic logfile
  47.  
  48. char * news = NULL;
  49. unsigned int ids = 0;
  50. time_t curtime;
  51.  
  52. char *arr[30];
  53. char arr2[30][1024];
  54.  
  55. //sockets
  56. SOCKET RedirectSocket, ListeningSocket, ReportingSocket, ClientReportingSocket, ClientReportingSocketTcp;
  57. bool running = true;
  58.  
  59. //connection queues
  60. ConnectionsClass RedirectConnections, ClientConnections, ReportingConnections;
  61.  
  62. #define NFSU_LAN_VERSION "1.0.2"
  63. #define DEFAULT_NEWS "-=-=-=-\nDefault news\nPlz tell server admin to make news file ;)\n-=-=-=-=-"
  64.  
  65. ServerClass Server; //core ;)
  66.  
  67. void Log( char *log ){
  68.     if (!DisableTimeStamp){
  69.         time_t t;
  70.         time(& t);
  71.         sprintf(logtemp, "[ %s  %s", ctime(& t), log);
  72.         logtemp[26] = 32;
  73.         logtemp[27] = 93;
  74.     }else{
  75.         strcpy(logtemp, log);
  76.     }
  77.  
  78.     if (EnableLogScreen)
  79.         printf(logtemp);
  80.  
  81.     if ((logfil != NULL) && (EnableLogFile)){
  82.         fwrite(logtemp, strlen(logtemp), 1, logfil);
  83.         fflush(logfil);
  84.     }
  85. };
  86.  
  87. void LogTraffic( char *log, int len ){
  88.     if (tlogfil != NULL){
  89.         fwrite(log, len, 1, tlogfil);
  90.         fwrite("\x0D\x0A", 2, 1, tlogfil);
  91.         fflush(tlogfil);
  92.     }
  93. };
  94.  
  95. void LoadNews(){
  96.     FILE * fil;
  97.     fil = fopen("news", "r");
  98.  
  99.     if (fil == NULL){
  100.         news = _strdup(DEFAULT_NEWS);
  101.         return;
  102.     }
  103.  
  104.     fseek(fil, 0, SEEK_END);
  105.     int siz = ftell(fil);
  106.     fseek(fil, 0, SEEK_SET);
  107.  
  108.     if(siz>10238) siz=10238;
  109.  
  110.     if (news != NULL)
  111.         free(news);
  112.  
  113.     news=(char*)calloc(siz+1, sizeof(char));
  114.     fread(news, siz, 1, fil);
  115.     fclose(fil);
  116. };
  117.  
  118. threadfunc IOThread(void * Dummy){
  119.     char log[1024];
  120.     MessageClass *msg;
  121.     ConnectionClass *temp;
  122.     ConnectionsClass *con=(ConnectionsClass*)Dummy;
  123.     sprintf(log, "Starting %s IO thread.\n", con->Name);
  124.     Log(log);
  125.     int k;
  126.     fd_set check;
  127.     timeval tim;
  128.     tim.tv_sec=0;
  129.     tim.tv_usec=100;
  130.     GameClass *Game;
  131.     RoomClass *Room;
  132.  
  133.     while(running){
  134.         temp=con->First;
  135.         while(temp!=NULL){
  136.             if(temp->Abort){
  137.                 sprintf(log, "Aborting connection @ %s. IP : %s, SessionID : %u\n", con->Name, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  138.                 Log(log);
  139.                 con->RemoveConnection(temp);               
  140.                 if(temp->user!=NULL){                  
  141.                     temp->user->Connection=NULL;
  142.                     if(temp->user->CurrentRoom!=NULL){
  143.                         if(temp->user->Game!=NULL){
  144.                             Game=temp->user->Game;
  145.                             Game->RemoveUser(temp->user, temp->Buffer);
  146.                             if(Game->Count==0){
  147.                                 temp->user->CurrentRoom->Games.RemoveGame(Game);
  148.                                 free(Game);
  149.                             }
  150.                             temp->user->CurrentRoom->RefreshUser(temp->user, temp->Buffer);
  151.                         }
  152.                         Room=temp->user->CurrentRoom;
  153.                         Room->RemoveUser(temp->user, temp->Buffer);
  154.                         if((Room->Count==0)&&(!Room->IsGlobal)){
  155.                             Server.Rooms.RemoveRoom(Room);
  156.                             free(Room);
  157.                         }
  158.                     }                  
  159.                 }              
  160.                 temp->IncomingMessages.Clear();
  161.                 temp->IncomingMessages.mut.DeInit();
  162.                 temp->OutgoingMessages.Clear();
  163.                 temp->OutgoingMessages.mut.DeInit();
  164.                 closesocket(temp->sock);
  165.                 free(temp);
  166.                 temp=NULL;
  167.                 break;
  168.             }
  169.  
  170.             msg=temp->OutgoingMessages.RemoveFirstMessage();
  171.             while(msg!=NULL){              
  172.                 if(Verbose){
  173.                     sprintf(log, "Sending outgoing message @ %s. Command: %s, IP : %s, SessionID : %u\n", con->Name, msg->Message, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  174.                     Log(log);
  175.                 }
  176.                 k=send(temp->sock, msg->Message, msg->Size, 0);
  177.                 if(k!=msg->Size){
  178.                     closesocket(temp->sock);
  179.                     temp->Abort=true;                  
  180.                     break;
  181.                 }
  182.                 free(msg->Message);
  183.                 free(msg);
  184.                 msg=temp->OutgoingMessages.RemoveFirstMessage();
  185.             }
  186.             FD_ZERO(&check);
  187.             FD_SET(temp->sock, &check);
  188.             tim.tv_sec=0;
  189.             tim.tv_usec=0;
  190.             k=select(temp->sock+1, &check, NULL, NULL, &tim);
  191.             if(k<0){
  192. #ifndef _WIN32
  193.                 if(errno != EAGAIN){
  194.                     sprintf(log, "Error on select() errno : %u\n", errno);
  195. #else
  196.                 if(WSAGetLastError()!=WSAENOTSOCK){
  197.                     sprintf(log, "Error on select() WSAError : %u\n", WSAGetLastError());
  198. #endif             
  199.                     Log(log);
  200. #ifdef _WIN32
  201.                 }else{
  202.                     temp->Abort=true;
  203. #endif
  204.                 }
  205.             }
  206.             if(k==1){
  207.                 if(temp->Received<12){
  208.                     k=recv(temp->sock, temp->Buffer+temp->Received, 12-temp->Received, 0);
  209.                     if(k<1){                       
  210.                         if(Verbose){
  211.                             sprintf(log, "Receiving command failed - connection closed @ %s. IP : %s, SessionID : %u\n", con->Name, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  212.                             Log(log);                      
  213.                         }
  214.                         temp->Abort=true;
  215.                     }else{
  216.                         //sprintf(log, "Receiving command succeed @ %s\n", con->Name);
  217.                         //Log(log);
  218.                         temp->Received+=k;
  219.                     }
  220.                 }
  221.                 if(temp->Received>11){
  222.                     //sprintf(log, "Receiving command data.\n");
  223.                     //Log(log);
  224.                     DWORD req=(temp->Buffer[11]&0xFF)|((temp->Buffer[10]<<8)&0xFF00)|((temp->Buffer[9]<<16)&0xFF0000)|((temp->Buffer[8]<<24)&0xFF000000);
  225. //                  WSASetLastError(0);
  226.                     if((req>12)&&(req<1000)){
  227.                         k=recv(temp->sock, temp->Buffer+temp->Received, req-temp->Received, 0);
  228.                         if(k<1){                           
  229.                             if(Verbose){
  230.                                 sprintf(log, "Receiving command failed - connection closed @ %s. IP : %s, SessionID : %u\n", con->Name, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  231.                                 Log(log);
  232.                             }
  233.                             closesocket(temp->sock);
  234.                             temp->Abort=true;
  235.                         }else{
  236.                         //sprintf(log, "Receiving data succeed @ %s : %s\n", con->Name, inet_ntoa(temp->remote_ip.sin_addr));
  237.                         //Log(log);
  238.                             temp->Received+=k;
  239.                         }
  240.                     }
  241.                     if(req>999){
  242.                         temp->Abort=true;
  243.                     }
  244.                     if(temp->Received==req){                       
  245.                         if(Verbose){
  246.                             sprintf(log, "Receiving data complete @ %s. Command : %s, IP : %s, SessionID  :%u\n", con->Name, temp->Buffer, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  247.                             Log(log);
  248.                         }
  249.                         msg=(MessageClass*)calloc(1, sizeof(MessageClass));
  250.                         msg->Message=(char*)calloc(temp->Received, sizeof(char));
  251.                         msg->Size=temp->Received;
  252.                         memcpy(msg->Message, temp->Buffer, temp->Received);
  253.                         temp->IncomingMessages.AddMessage(msg);
  254.                         temp->Received=0;
  255.                     }
  256.                 }
  257.             }
  258.             if((int)(difftime(curtime, temp->Idle))==50){
  259.                 sprintf(log, "Aborting connection due to timeout @ %s, IP: %s\n", con->Name, inet_ntoa(temp->remote_ip.sin_addr));
  260.                 Log(log);
  261.                 temp->Abort=true;
  262.             }          
  263.             temp=temp->Next;
  264.         }
  265.         Sleep(10);
  266.     }
  267. };
  268.  
  269. threadfunc AcceptThread(void *Dummy){
  270.     SOCKET cl;
  271.     char log[1024];
  272.  
  273.     ConAccParam *acc=(ConAccParam*)Dummy;
  274.  
  275.     sprintf(log, "Starting %s thread\n", acc->Name);
  276.     Log(log);
  277.  
  278.     unsigned long remote_sockaddr_length = sizeof(SOCKADDR_IN);
  279.  
  280.     ConnectionClass *temp;
  281.  
  282. #ifdef _WIN32
  283.     int timeo=10;
  284. #else
  285.     struct timeval timeo;
  286.     timeo.tv_sec  = 0;
  287.     timeo.tv_usec = 10000;
  288. #endif
  289.     while(running){
  290.         cl=accept(acc->sock, NULL, NULL);
  291.         if(cl!=INVALID_SOCKET){
  292.             temp=(ConnectionClass*)calloc(1, sizeof(ConnectionClass));
  293.             temp->IncomingMessages.mut.Init();
  294.             temp->OutgoingMessages.mut.Init();
  295.             temp->sock=cl;
  296. #ifndef _WIN32
  297.             getsockname(temp->sock, (SOCKADDR *)&temp->local_ip,(socklen_t*) &remote_sockaddr_length);
  298.             getpeername(temp->sock, (SOCKADDR *)&temp->remote_ip,(socklen_t*) &remote_sockaddr_length);
  299. #else
  300.             getsockname(temp->sock, (SOCKADDR *)&temp->local_ip ,(int*) &remote_sockaddr_length);
  301.             getpeername(temp->sock, (SOCKADDR *)&temp->remote_ip,(int*) &remote_sockaddr_length);
  302. #endif
  303.             time(&temp->Idle);
  304.             acc->Connections->AddConnection(temp);
  305.             temp->id=ids++;            
  306.             sprintf(log, "Accepting client @ %s. IP : %s, ID : %u\n", acc->Name, inet_ntoa(temp->remote_ip.sin_addr), temp->id);
  307.             Log(log);
  308.         }
  309.     }
  310. };
  311.  
  312. threadfunc RedirectorWorker(void *Dummy){
  313.     Log("Starting RedirectorWorker thread.\n");
  314.     char log[1024];
  315.     char buffer[1024];
  316.  
  317.     MessageClass *msg;
  318.     MessageClass *tmsg;
  319.  
  320.     ConnectionClass *temp;
  321.     while(running){
  322.         RedirectConnections.mut.Lock();
  323.         temp=RedirectConnections.First;
  324.         while(temp!=NULL){
  325.             msg=temp->IncomingMessages.RemoveFirstMessage();
  326.             while(msg!=NULL){
  327.                 free(msg->Message);
  328.                 free(msg);             
  329.                 if(Verbose){
  330.                     sprintf(log, "Adding outgoing message : server_info_to_nfsu : %s\n", inet_ntoa(temp->remote_ip.sin_addr));
  331.                     Log(log);
  332.                 }
  333.                 sprintf(arr2[0], "ADDR=%s", inet_ntoa(temp->local_ip.sin_addr));
  334.                 sprintf(arr2[1], "PORT=10901");
  335.                 sprintf(arr2[2], "SESS=1072010288");
  336.                 sprintf(arr2[3], "MASK=0295f3f70ecb1757cd7001b9a7a5eac8");
  337.                 int k=MakeCommand(buffer, "@dir", arr, 4);
  338.                 tmsg=(MessageClass*)calloc(1, sizeof(MessageClass));
  339.                 tmsg->Message=(char*)calloc(k, sizeof(char));
  340.                 memcpy(tmsg->Message, buffer, k);
  341.                 tmsg->Size=k;
  342.                 temp->OutgoingMessages.AddMessage(tmsg);
  343.  
  344.                 msg=temp->IncomingMessages.RemoveFirstMessage();
  345.             }
  346.             temp=temp->Next;
  347.         }
  348.         RedirectConnections.mut.Unlock();
  349.         Sleep(10);
  350.     }
  351. };
  352.  
  353. void Subscribe(){
  354.     struct hostent *hostInfo = gethostbyname("3priedez.net");
  355.     SOCKADDR_IN remote_sockaddr_in;
  356.     unsigned long remote_sockaddr_length = sizeof(SOCKADDR_IN);
  357.     char buf[1024];
  358.  
  359.     remote_sockaddr_in.sin_family = AF_INET;
  360.     remote_sockaddr_in.sin_port = htons(80);
  361.  
  362.     if(hostInfo!=NULL){    
  363. #ifdef _WIN32
  364.         memcpy(&remote_sockaddr_in.sin_addr.S_un.S_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
  365. #else
  366.         memcpy(&remote_sockaddr_in.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
  367. #endif
  368.     }else{
  369.         remote_sockaddr_in.sin_addr.s_addr = inet_addr("195.2.101.48");
  370.         if (remote_sockaddr_in.sin_addr.s_addr == INADDR_NONE) return;
  371.     }
  372.  
  373.     SOCKET sock=socket(AF_INET, SOCK_STREAM, 0);
  374.     if(sock==INVALID_SOCKET) return;
  375.     if(connect(sock, (const sockaddr*)&remote_sockaddr_in, remote_sockaddr_length)==0){
  376.         sprintf(buf, "GET /nfsug/submit.py HTTP/1.1\x0d\x0aHost: 3priedez.net\x0d\x0a\x0d\x0a");
  377.         send(sock, buf, strlen(buf), 0);
  378.         recv(sock, buf, 1024, 0);
  379.         closesocket(sock);
  380.     }
  381. }
  382.  
  383. threadfunc WebReport(void *dummy){
  384.     while(running){
  385.         Subscribe();
  386.         Sleep(1000*60*5);
  387.     }
  388. };
  389.  
  390. threadfunc ListenerWorker(void *Dummy){
  391.     char log[1024];
  392.     sprintf(log, "Starting ListenerWorker thread.\n");
  393.     Log(log);
  394.     char buffer[1024];
  395.     char *buf;
  396.     char *tmp, *tmp2;
  397.  
  398.     MessageClass *msg;
  399.     UserClass *user;
  400.  
  401.     int p=22;
  402.     char ttm[5];
  403.  
  404.  
  405.     bool IsNew;
  406.  
  407.     int k;
  408.  
  409.     ConnectionClass *temp;
  410.     while(running){
  411.         ClientConnections.mut.Lock();
  412.         temp=ClientConnections.First;
  413.         while(temp!=NULL){
  414.             if((int)(difftime(curtime, temp->Idle))==30){
  415.                 sprintf(log, "Sending ping IP: %s\n", inet_ntoa(temp->remote_ip.sin_addr));
  416.                 Log(log);
  417.                 ttm[3]=(char)p++;
  418.                 ttm[2]=(char)p>>8;
  419.                 ttm[1]=(char)p>>16;
  420.                 ttm[0]=(char)p>>24;
  421.                 memcpy(log, "~png", 4);
  422.                 memcpy(log+4, ttm, 4);
  423.                 memcpy(log+8, "\x00\x00\x00\x0c", 4);
  424.  
  425.                 msg=(MessageClass*)calloc(1, sizeof(MessageClass));
  426.                 msg->Message=(char*)calloc(12, sizeof(char));
  427.                 memcpy(msg->Message, log, 12);
  428.                 msg->Size=12;
  429.                 temp->OutgoingMessages.AddMessage(msg);
  430.  
  431.                 temp->Idle-=1;
  432.             }
  433.             msg=temp->IncomingMessages.RemoveFirstMessage();
  434.             while(msg!=NULL){
  435.                 buf=msg->Message;
  436.                 user=temp->user;
  437.  
  438.                 if(LogAllTraffic) LogTraffic(msg->Message, msg->Size);
  439.                 switch(buf[0]){
  440.                     case 'g':
  441.                         switch(buf[1]){
  442.                             case 'l':
  443.                                 //glea
  444.                                 if(strncmp(buf+2, "ea", 2)==0){
  445.                             tmp=strchr(buf+17, 10);
  446.                             if(tmp==NULL) tmp=strchr(buf+17, 9);
  447.                             if(tmp!=NULL) tmp[0]=0;
  448.                             GameClass *game=user->CurrentRoom->Games.GameFromName(buf+17);
  449.                             if(game!=NULL){
  450.                                 game->RemoveUser(user, buffer);
  451.                             }
  452.                         }
  453.                                 break;
  454.                             case 'g':
  455.                                 //gget
  456.                                 if(strncmp(buf+2, "et", 2)==0){
  457.                             tmp=buf+17;
  458.                             GameClass *game=user->CurrentRoom->Games.GameFromName(tmp);
  459.                             if(game!=NULL){
  460.                                 sprintf(arr2[0], "IDENT=%u", game->ID);
  461.                                 sprintf(arr2[1], "WHEN=2003.12.8 15:52:54");
  462.                                 sprintf(arr2[2], "NAME=%s", game->Name);
  463.                                 sprintf(arr2[3], "HOST=%s", game->Users.First->User->Personas[game->Users.First->User->SelectedPerson]);
  464.                                 sprintf(arr2[4], "PARAMS=%s", game->params);
  465.                                 sprintf(arr2[5], "ROOM=%s", user->CurrentRoom->Name);
  466.                                 sprintf(arr2[6], "MAXSIZE=%u", game->max);
  467.                                 sprintf(arr2[7], "MINSIZE=%u", game->min);
  468.                                 sprintf(arr2[8], "COUNT=%u", game->Count);
  469.                                 sprintf(arr2[9], "USERFLAGS=0");
  470.                                 sprintf(arr2[10], "SYSFLAGS=%u", game->sysflags);
  471.                                 sprintf(arr2[11], "OPID0=%u", game->Users.First->User->id);
  472.                                 sprintf(arr2[12], "OPPO0=%s", game->Users.First->User->Personas[game->Users.First->User->SelectedPerson]);
  473.                                 sprintf(arr2[13], "ADDR0=%s", game->Users.First->User->IP);
  474.  
  475.                                 temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "gget", arr, 14));
  476.                             }
  477.                         }
  478.                                 break;
  479.                             case 'j':
  480.                                 //gjoi
  481.                                 if(strncmp(buf+2, "oi", 2)==0){
  482.                             GameClass *game=user->CurrentRoom->Games.GameFromName(buf+17);
  483.                             if(game!=NULL){
  484.                                 game->AddUser(user, buffer);
  485.                             }
  486.                         }
  487.                                 break;
  488.                             case 'd':
  489.                                 //gdel
  490.                                 if(strncmp(buf+2, "el", 2)==0){
  491.                             tmp=strchr(buf+17, 10);
  492.                             tmp[0]=0;
  493.                             GameClass *game=user->CurrentRoom->Games.GameFromName(buf+17);
  494.                             if(game!=NULL){
  495.                                 temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "gdel", NULL, 0));
  496.  
  497.                                 while(game->Count>0){
  498.                                     game->Users.First->User->Game=NULL;
  499.                                     user->CurrentRoom->RefreshUser(game->Users.First->User, buffer);
  500.                                     game->RemoveUser(game->Users.First->User, buffer);
  501.                                 }
  502.                                 user->CurrentRoom->Games.RemoveGame(game);
  503.  
  504.                                 sprintf(arr2[0], "IDENT=%u", game->ID);
  505.                                 BroadCastCommand(user->CurrentRoom->Users, "+agm", arr, 1, buffer);
  506.                                 free(game);
  507.                             }
  508.                         }
  509.                                 break;
  510.                             case 's':
  511.                                 //gsta
  512.                                 if(strncmp(buf+2, "ta", 2)==0){
  513.                             GameClass *game=user->CurrentRoom->Games.GameFromName(buf+17);
  514.                             if(game->Users.Count<game->min){
  515.                                 temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "gstanepl", NULL, 0));
  516.                             }else{
  517.                                 arr2[0][0]=0;
  518.                                 temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "gsta", arr, 1));
  519.                                 if(game!=NULL){
  520.                                     game->StartGame(buffer);
  521.                                 }
  522.                             }
  523.                         }
  524.                                 break;
  525.                             case 'c':
  526.                                 //gcre
  527.                                 if(strncmp(buf+2, "re", 2)==0){                        
  528.                                     if(Verbose){
  529.                                         sprintf(log, "Creating challenge\n");
  530.                                         Log(log);
  531.                                     }
  532.                                     GameClass *game=(GameClass*)calloc(1, sizeof(GameClass));
  533.                                     tmp=strchr(buf+17, 10);
  534.                                     if(tmp==NULL) tmp=strchr(buf+17, 9);                                   
  535.                                     if(Verbose){
  536.                                         sprintf(log, "After getting challenge name\n");
  537.                                         Log(log);
  538.                                     }
  539.                                     tmp[0]=0;
  540.                                     tmp+=6;
  541.                                     strcpy(game->Name, buf+17);                                
  542.                                     if(Verbose){
  543.                                         sprintf(log, "After name copy\n");
  544.                                         Log(log);
  545.                                     }
  546.                                     tmp2=strchr(tmp, 10);
  547.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  548.                                     tmp2[0]=0;
  549.                                     RoomClass *room=Server.Rooms.RoomFromName(tmp);
  550.                                    
  551.                                     if(Verbose){
  552.                                         sprintf(log, "After getting room\n");
  553.                                         Log(log);
  554.                                     }
  555.  
  556.                                     tmp=tmp2+9;
  557.                                     tmp2=strchr(tmp, 10);
  558.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  559.                                     tmp2[0]=0;
  560.                                                                        
  561.                                     if(Verbose){
  562.                                         sprintf(log, "Before atoi : %u - %s\n", game->max, tmp);
  563.                                         Log(log);
  564.                                     }
  565.  
  566.                                     game->max=atoi(tmp);
  567.                                    
  568.                                     if(Verbose){
  569.                                         sprintf(log, "After getting max\n");
  570.                                         Log(log);
  571.                                     }
  572.  
  573.                                     tmp=tmp2+9;
  574.                                     tmp2=strchr(tmp, 10);
  575.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  576.                                     tmp2[0]=0;
  577.                                     game->min=atoi(tmp);
  578.                                    
  579.                                     if(Verbose){
  580.                                         sprintf(log, "After getting min\n");
  581.                                         Log(log);
  582.                                     }
  583.  
  584.                                     tmp=tmp2+10;
  585.                                     tmp2=strchr(tmp, 10);
  586.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  587.                                     tmp2[0]=0;
  588.                                     game->sysflags=atoi(tmp);
  589.                                    
  590.                                     if(Verbose){
  591.                                         sprintf(log, "After getting sysflags\n");
  592.                                         Log(log);
  593.                                     }
  594.  
  595.                                     tmp=tmp2+8;
  596.                                     strcpy(game->params, tmp);
  597.                                    
  598.                                     if(Verbose){
  599.                                         sprintf(log, "After setting all params\n");
  600.                                         Log(log);
  601.                                     }
  602.  
  603.                                     room->Games.AddGame(game);
  604.                                    
  605.                                     if(Verbose){
  606.                                         sprintf(log, "After adding game to room.games\n");
  607.                                         Log(log);
  608.                                     }
  609.  
  610.                                     game->AddUser(user, buffer);
  611.                                    
  612.                                     if(Verbose){
  613.                                         sprintf(log, "After adding user\n");
  614.                                         Log(log);
  615.                                     }
  616.  
  617.                                     sprintf(arr2[0], "IDENT=%u", game->ID);
  618.                                     sprintf(arr2[1], "WHEN=2003.12.8 15:52:54");
  619.                                     sprintf(arr2[2], "NAME=%s", game->Name);
  620.                                     sprintf(arr2[3], "HOST=%s", user->Personas[user->SelectedPerson]);
  621.                                     sprintf(arr2[4], "PARAMS=%s", game->params);
  622.                                     sprintf(arr2[5], "ROOM=%s", room->Name);
  623.                                     sprintf(arr2[6], "MAXSIZE=%u", game->max);
  624.                                     sprintf(arr2[7], "MINSIZE=%u", game->min);
  625.                                     sprintf(arr2[8], "COUNT=%u", game->Count);
  626.                                     sprintf(arr2[9], "USERFLAGS=0");
  627.                                     sprintf(arr2[10], "SYSFLAGS=%u", game->sysflags);
  628.                                     sprintf(arr2[11], "OPID0=%u", user->id);
  629.                                     sprintf(arr2[12], "OPPO0=%s", user->Personas[user->SelectedPerson]);
  630.                                     sprintf(arr2[13], "ADDR0=%s", user->IP);
  631.  
  632.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "gcre", arr, 14));
  633.  
  634.                                     room->RefreshUser(user, buffer);
  635.  
  636.                                     BroadCastCommand(room->Users, "+agm", arr, 14, buffer);
  637.                                 }
  638.                                 break;
  639.                         }
  640.                         break;
  641.                     case 'n':
  642.                         //news
  643.                         if(strncmp(buf+1, "ews", 3)==0){
  644.                             strcpy(arr2[0], news);
  645.                             temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "newsnew0", arr, 1));
  646.                         }
  647.                         break;
  648.                     case 'm':
  649.                         switch(buf[1]){
  650.                             case 'o':
  651.                                 //move
  652.                                 if(strncmp(buf+2, "ve", 2)==0){
  653.                                     tmp=strchr(buf+17, 10);
  654.                                     tmp[0]=0;
  655.  
  656.                                     RoomClass *rom=Server.Rooms.RoomFromName(buf+17);
  657.  
  658.                                     if(rom!=NULL){
  659.                                         rom->AddUser(user, buffer);
  660.  
  661.                                         sprintf(arr2[0], "Z=%u/%u", rom->ID, rom->Count);
  662.                                         arr[0]=(char*)&arr2[0];
  663.                                         BroadCastCommand(&Server.Users, "+pop", arr, 1, buffer);
  664.                                     }else{             
  665.                                         sprintf(arr2[0], "IDENT=0");
  666.                                         sprintf(arr2[1], "NAME=");
  667.                                         sprintf(arr2[2], "COUNT=0");
  668.                                         if(user->CurrentRoom==NULL){
  669.                                             sprintf(arr2[3], "LIDENT=0");
  670.                                         }else{
  671.                                             sprintf(arr2[3], "LIDENT=%u", user->CurrentRoom->ID);
  672.                                         }
  673.                                         sprintf(arr2[4], "LCOUNT=0");
  674.  
  675.                                         RoomClass *rc=NULL;
  676.  
  677.                                         if(user->CurrentRoom!=NULL){
  678.                                             rc=user->CurrentRoom;
  679.                                             user->CurrentRoom->RemoveUser(user, buffer);
  680.                                             if((rc->Users.Count==0)&&(!rc->IsGlobal)){
  681.                                                 Server.Rooms.RemoveRoom(rc);
  682.                                                 sprintf(arr2[0], "I=%u", rc->ID);
  683.                                                 arr[0]=(char*)&arr2[0];
  684.                                                 BroadCastCommand(&Server.Users, "+rom", arr, 1, buffer);
  685.                                                 free(rc);
  686.                                             }
  687.                                         }
  688.  
  689.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "move", arr, 5));
  690.  
  691.                                         if(rc!=NULL){
  692.                                             sprintf(arr2[0], "Z=%u/%u", rc->ID, rc->Count);
  693.                                             arr[0]=(char*)&arr2[0];
  694.                                             BroadCastCommand(&Server.Users, "+pop", arr, 1, buffer);
  695.                                         }
  696.                                     }
  697.                                 }
  698.                                 break;
  699.                             case 'e':
  700.                                 //mesg
  701.                                 if(strncmp(buf+2, "sg", 2)==0){
  702.                                     if(Verbose) {
  703.                                         sprintf(log, "Message received from %s\n", temp->user->IP);
  704.                                         Log(log);
  705.                                     }
  706.  
  707.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "mesg", NULL, 0));//reply that msg is recv
  708.  
  709.                                     if(strncmp(buf+12, "TEXT", 4)!=0){
  710.                                         if(Verbose) {
  711.                                             sprintf(log, "Private message\n");
  712.                                             Log(log);
  713.                                         }
  714.                                         tmp=buf+17;
  715.                                         tmp2=strchr(tmp, 10);
  716.                                         if(tmp2==NULL)tmp2=strchr(tmp, 9);
  717.                                         if(tmp2!=NULL){
  718.                                             tmp2[0]=0;
  719.                                             UserClass *us;
  720.                                             us=Server.Users.UserFromUsername(buf+17);
  721.                                             if(us!=NULL){
  722.                                                 if(us->Connection!=NULL){
  723.                                                     if(tmp2[7]=='0'){
  724.                                                         sprintf(arr2[0], "F=EP0");
  725.                                                     }else{
  726.                                                         sprintf(arr2[0], "F=EP1");
  727.                                                     }
  728.                                                     tmp2+=14;
  729.                                                     sprintf(arr2[1], "T=%s", tmp2);
  730.                                                     sprintf(arr2[2], "N=%s", user->Personas[user->SelectedPerson]);
  731.  
  732.                                                     us->Connection->OutgoingMessages.AddMessage(MakeMessage(buffer, "+msg", arr, 3));
  733.                                                 }
  734.                                             }else{
  735.                                                 if(Verbose) {
  736.                                                     sprintf(log, "Didn't find user\n");
  737.                                                     Log(log);
  738.                                                 }
  739.                                             }
  740.                                         }else{
  741.                                             sprintf(log, "Smth wrong with mesg message.\n");
  742.                                             Log(log);
  743.                                         }
  744.                                     }else{
  745.                                         if(Verbose) {
  746.                                             sprintf(log, "Global message\n");
  747.                                             Log(log);
  748.                                         }
  749.                                         if(user->CurrentRoom!=NULL){
  750.                                             sprintf(arr2[0], "F=U");
  751.                                             sprintf(arr2[1], "T=%s", buf+17);
  752.                                             sprintf(arr2[2], "N=%s", user->Personas[user->SelectedPerson]);
  753.                                             BroadCastCommand(user->CurrentRoom->Users, "+msg", arr, 3, buffer);
  754.                                         }
  755.                                     }          
  756.                                 }
  757.                                 break;
  758.                         }
  759.                         break;
  760.                     case 'r':
  761.                         switch(buf[1]){
  762.                             case 'a':
  763.                                 //rank
  764.                                 if(strncmp(buf+2, "nk", 2)==0){
  765.                                     sprintf(arr2[0], "RANK=Unranked");
  766.                                     sprintf(arr2[1], "TIME=866");
  767.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "rank", arr, 2));
  768.                                 }
  769.                                 break;
  770.                             case 'o':
  771.                                 //room
  772.                                 if(strncmp(buf+2, "om", 2)==0){                        
  773.                                     if(Verbose){
  774.                                         sprintf(log, "Create room\n");
  775.                                         Log(log);
  776.                                     }
  777.                                     RoomClass *rom;
  778.                                     rom=(RoomClass*)calloc(1, sizeof(RoomClass));
  779.                                     rom->Count=0;
  780.  
  781.                                     rom->Games.cid=1;
  782.                                     rom->Games.Count=0;
  783.                                     rom->Games.First=NULL;
  784.  
  785.                                     strcpy(rom->Name, buf+17);
  786.                                     Server.Rooms.AddRoom(rom);
  787.  
  788.                                     sprintf(arr2[0], "IDENT=%u", rom->ID);
  789.                                     sprintf(arr2[1], "NAME=%s", rom->Name);
  790.                                     sprintf(arr2[2], "HOST=%s", user->Personas[user->SelectedPerson]);
  791.                                     sprintf(arr2[3], "DESC=");
  792.                                     sprintf(arr2[4], "COUNT=1");
  793.                                     sprintf(arr2[5], "LIMIT=50");
  794.                                     sprintf(arr2[6], "FLAGS=C");
  795.  
  796.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "room", arr, 7));
  797.  
  798.                                     sprintf(arr2[0], "I=%u", user->id);
  799.                                     sprintf(arr2[1], "N=%s", user->Personas[user->SelectedPerson]);
  800.                                     sprintf(arr2[2], "M=%s", user->Username);
  801.                                     sprintf(arr2[3], "F=");
  802.                                     sprintf(arr2[4], "A=%s", user->IP);
  803.                                     sprintf(arr2[5], "S=");
  804.                                     sprintf(arr2[6], "X=%s", user->car);
  805.                                     sprintf(arr2[7], "R=%s", rom->Name);
  806.                                     sprintf(arr2[8], "RI=%u", rom->ID);
  807.                                     sprintf(arr2[9], "RF=C");
  808.                                     sprintf(arr2[10], "RT=1");
  809.  
  810.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "+who", arr, 11));
  811.  
  812.                                     rom->AddUser(user, buffer);
  813.  
  814.                                     sprintf(arr2[0], "F=CU");
  815.                                     sprintf(arr2[1], "T=\"has created the room\"");
  816.                                     sprintf(arr2[2], "N=%s", user->Personas[user->SelectedPerson]);
  817.  
  818.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "+msg", arr, 3));
  819.  
  820.  
  821.                                     sprintf(arr2[0], "I=%u", rom->ID);
  822.                                     sprintf(arr2[1], "N=%s", rom->Name);
  823.                                     sprintf(arr2[2], "H=%s", user->Personas[user->SelectedPerson]);
  824.                                     sprintf(arr2[3], "F=CH");
  825.                                     sprintf(arr2[4], "T=1");
  826.                                     sprintf(arr2[5], "L=25");
  827.                                     sprintf(arr2[6], "P=10");
  828.                                     sprintf(arr2[7], "A=%s", user->IP);
  829.  
  830.                                     BroadCastCommand(&Server.Users, "+rom", arr, 8, buffer);
  831.  
  832.                                     sprintf(arr2[0], "PI=%u", user->id);
  833.                                     sprintf(arr2[1], "N=%s", user->Personas[user->SelectedPerson]);
  834.                                     sprintf(arr2[2], "M=%s", user->Username);
  835.                                     sprintf(arr2[3], "F=HU");
  836.                                     sprintf(arr2[4], "A=%s", user->IP);
  837.                                     sprintf(arr2[5], "P=223");
  838.                                     sprintf(arr2[6], "S=");
  839.                                     sprintf(arr2[7], "X=%s", user->car);
  840.                                     sprintf(arr2[8], "G=0");
  841.                                     sprintf(arr2[9], "T=1");
  842.                                    
  843.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "+usr", arr, 10));           
  844.                                 }
  845.                                 break;
  846.                         }                                              
  847.                         break;
  848.                     case 'a':
  849.                         switch(buf[1]){
  850.                             case 'u':
  851.                                 switch(buf[2]){
  852.                                     case 'x':
  853.                                         //auxi
  854.                                         if(buf[3]=='i'){                                           
  855.                                             if(Verbose){
  856.                                                 sprintf(log, "Car received\n");
  857.                                                 Log(log);
  858.                                             }
  859.                                             strcpy(user->car, buf+17);
  860.                                             strcpy(arr2[0], buf+12);
  861.                                             temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "auxi", arr, 1));
  862.                                         }
  863.                                         break;
  864.                                     case 't':
  865.                                         //auth
  866.                                         if(buf[3]=='h'){
  867.                                             IsNew=true;                                        
  868.                                             if(Verbose){
  869.                                                 sprintf(log, "auth\n");
  870.                                                 Log(log);
  871.                                             }
  872.                                             /*
  873. +   buf+18  0x00468e8a "dlgnome
  874. PASS=~pn$P29&$Fyt6Zy`tGsfBx{hTkEJ#Ll
  875. TOS=1
  876. MID=$00e01851bf21
  877. FROM=US
  878. LANG=EN
  879. PROD=nfs-pc-2003
  880. VERS="pc/1.2-Nov 12 2003"
  881. SLUS=SLUS
  882. REGN=NA
  883. CLST=51733
  884. NETV=5
  885. "   char *
  886.                                            
  887.                                             */
  888. /*/+    buf+18  0x00468e8a "dlgnome
  889. PASS=~pn$P29&.`l`['%22%22K6?'V_I8(SjD%3dNR
  890. TOS=1
  891. MID=$00e01851bf21
  892. FROM=US
  893. LANG=EN
  894. PROD=nfs-pc-2003
  895. VERS="pc/1.1001-Oct 30 2003"
  896. SLUS=SLUS
  897. REGN=NA
  898. CLST=0
  899. NETV=5
  900. "   char *
  901. */
  902.                                             tmp=strstr(buf+17, "VERS=\"");
  903.                                             if(tmp!=NULL){
  904.                                                 k=tmp[11]-'0';
  905.                                             }else{
  906.                                                 k=1;
  907.                                             }
  908.  
  909.                                             tmp=buf+17;
  910.                                             tmp2=strchr(tmp, 10);
  911.                                             if(tmp2==NULL) tmp2=strchr(tmp, 9);
  912.                                             tmp2[0]=0;
  913.                                             RegUser *tr=Server.ru.UserFromUsername(tmp);
  914.                                             if(tr==NULL){                                              
  915.                                                 if(Verbose){
  916.                                                     sprintf(log, "No such user\n");
  917.                                                     Log(log);
  918.                                                 }
  919.                                                 temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "authimst", NULL, 0));
  920.                                             }else{
  921.                                                 user=Server.Users.UserFromUsername(tmp);
  922.                                                 if(user==NULL){                                                                
  923.                                                     if(Verbose){
  924.                                                         sprintf(log, "New user\n");
  925.                                                         Log(log);
  926.                                                     }
  927.                                                     user=(UserClass*)calloc(1, sizeof(UserClass));
  928.                                                     strcpy(user->IP, temp->IP);
  929.                                                     strcpy(user->Port, temp->Port);
  930.                                                     user->id=-1;
  931.                                                     strcpy(user->Username, tmp);
  932.                                                     int l=0;
  933.                                                     RegUser *tr=Server.ru.UserFromUsername(user->Username);
  934.                                                     while(tr->Personas[l][0]!=0){
  935.                                                         strcpy(user->Personas[l], tr->Personas[l]);                    
  936.                                                         l++;
  937.                                                         if(l==4) break;
  938.                                                     }
  939.                                                     user->Idle=0;
  940.                                                     user->Connection=temp;
  941.                                                     temp->user=user;
  942.                                                     Server.Users.AddUser(user);
  943.                                                 }else
  944.                                                 {
  945.                                                     if(user->Connection!=NULL){
  946.                                                         //in case user is already connected
  947.                                                         temp->Abort=true;
  948.                                                     }else{
  949.                                                         IsNew=false;                                                   
  950.                                                         if(Verbose){
  951.                                                             sprintf(log, "Found user\n");
  952.                                                             Log(log);
  953.                                                         }
  954.                                                         strcpy(user->IP, temp->IP);
  955.                                                         strcpy(user->Port, temp->Port);
  956.                                                         temp->user=user;
  957.                                                         temp->user->Idle=0;
  958.                                                         user->Connection=temp;
  959.                                                     }
  960.                                                 }
  961.                                                 if(!temp->Abort){
  962.                                                     sprintf(arr2[0], "TOS=%u", user->id);
  963.                                                     sprintf(arr2[1], "NAME=%s", user->Username);
  964.                                                     sprintf(arr2[2], "MAIL=vdl@3priedez.net");
  965.                                                     sprintf(arr2[3], "BORN=19800325");
  966.                                                     sprintf(arr2[4], "GEND=M");
  967.                                                     sprintf(arr2[5], "FROM=US");
  968.                                                     sprintf(arr2[6], "LANG=en");
  969.                                                     sprintf(arr2[7], "SPAM=NN");
  970.                                                     int l=0;
  971.                                                     char str[1024];
  972.                                                     memset(str, 0, 1024);
  973.                                                     while(user->Personas[l][0]!=0){
  974.                                                         if(l>0) strcat(str, ",");
  975.                                                         strcat(str, user->Personas[l]);
  976.                                                         l++;
  977.                                                         if(l==4) break;
  978.                                                     }
  979.                                                     sprintf(arr2[8], "PERSONAS=%s", str);
  980.                                                     sprintf(arr2[9], "LAST=2003.12.8 15:51:38");
  981.                                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "auth", arr, 10));
  982.                                                 }
  983.                                             }
  984.                                             if(user!=NULL){
  985.                                                 user->Version=k;
  986.                                                 switch(user->Version){
  987.                                                     case 4:
  988.                                                         if(BanV4) temp->Abort=true;
  989.                                                         break;
  990.                                                     case 3:
  991.                                                         if(BanV3) temp->Abort=true;
  992.                                                         break;
  993.                                                     case 2:
  994.                                                         if(BanV2) temp->Abort=true;
  995.                                                         break;
  996.                                                     default:
  997.                                                         if(BanV1) temp->Abort=true;
  998.                                                         break;
  999.                                                 }
  1000.                                             }
  1001.                                             if((user!=NULL)&&!IsNew)
  1002.                                                 Server.SendRoomsToUser(user, buffer);
  1003.                                         }
  1004.                                         break;
  1005.                                 }
  1006.                                 break;
  1007.                             case 'c':
  1008.                                 //acct
  1009.                                 if(strncmp(buf+2, "ct", 2)==0){
  1010.                                     tmp=strchr(buf+17, 10);
  1011.                                     tmp[0]=0;                                  
  1012.                                     if(Verbose){
  1013.                                         sprintf(log, "Try to register username\n");
  1014.                                         Log(log);
  1015.                                     }
  1016.                                     if(Server.ru.UserFromUsername(buf+17)!=NULL){
  1017.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "acctdupl", NULL, 0));
  1018.                                     }else{
  1019.                                         Server.RegisterUser(buf+17);
  1020.                                         sprintf(arr2[0], "NAME=%s", buf+17);
  1021.                                         sprintf(arr2[1], "PERSONAS=");
  1022.                                         sprintf(arr2[2], "AGE=24");
  1023.  
  1024.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "acct", arr, 3));
  1025.                                     }
  1026.                                 }
  1027.                                 break;
  1028.                             case 'd':
  1029.                                 //addr
  1030.                                 if(strncmp(buf+2, "dr", 2)==0){                                
  1031.                                     if(Verbose){
  1032.                                         sprintf(log, "addr\n");        
  1033.                                         Log(log);                                  
  1034.                                     }
  1035.                                     tmp=strchr(buf+17, 10);
  1036.                                     if(tmp==NULL) tmp=strchr(buf+17, 9);
  1037.                                     tmp[0]=0;
  1038.                                     //using only addr reported from socket
  1039.                                     strcpy(temp->IP, inet_ntoa(temp->remote_ip.sin_addr));                                 
  1040.                                     //strcpy(temp->IP, buf+17);
  1041.                                     //if(strcmp(temp->IP, "0.0.0.0")==0){
  1042.                                     //  sprintf(log, "Client sent 0.0.0.0\n");
  1043.                                     //  if(Verbose)Log(log);
  1044.                                     //  sockaddr_in so;
  1045.                                     //  k=sizeof(so);          
  1046.                                     //  strcpy(temp->IP, inet_ntoa(temp->remote_ip.sin_addr));
  1047.                                     //}                                
  1048.                                     if(Verbose){
  1049.                                         sprintf(log, "IP: %s\n", temp->IP);
  1050.                                         Log(log);
  1051.                                     }
  1052.                                     tmp+=6;
  1053.                                     tmp2=strchr(tmp, 10);
  1054.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  1055.                                     tmp2[0]=0;
  1056.                                     strcpy(temp->Port, tmp);
  1057.                                     sprintf(arr2[0], "SKEY=$37940faf2a8d1381a3b7d0d2f570e6a7");
  1058.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "skey", arr, 1));
  1059.                                 }
  1060.                                 break;
  1061.                         }
  1062.                         break;
  1063.                     case '~':
  1064.                         //~png
  1065.                         if(strncmp(buf+1, "png", 3)==0){
  1066.                             sprintf(log, "Receiving ping from IP: %s\n", inet_ntoa(temp->remote_ip.sin_addr));
  1067.                             Log(log);
  1068.                             time(&temp->Idle);
  1069.                             if(temp->user!=NULL){
  1070.                                 user->Idle=0;
  1071.                             }
  1072.                         }
  1073.                         break;
  1074.                     case 'c':
  1075.                         //cper
  1076.                         if(strncmp(buf+1, "per", 3)==0){
  1077.                             tmp=strchr(buf+17, 10);
  1078.                             if(tmp==NULL) tmp=strchr(buf+17, 9);
  1079.                             tmp[0]=0;
  1080.                             for(int l=0;l<4;l++){
  1081.                                 if(user->Personas[l][0]==0){
  1082.                                     strcpy(user->Personas[l], buf+17);
  1083.                                     RegUser *tr=Server.ru.UserFromUsername(user->Username);
  1084.                                     if(tr!=NULL){
  1085.                                         strcpy(tr->Personas[l], buf+17);
  1086.                                         Server.SaveSettings();
  1087.                                         sprintf(arr2[0], "PERS=%s", buf+17);
  1088.                                         sprintf(arr2[1], "NAME=%s", user->Username);
  1089.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "cper", arr, 2));
  1090.                                     }else{                                     
  1091.                                         if(Verbose){
  1092.                                             sprintf(buffer, "Panic - user tries to create subaccount name without having username.\n");
  1093.                                             Log(buffer);
  1094.                                         }
  1095.                                     }
  1096.                                     break;
  1097.                                 }
  1098.                             }
  1099.                         }
  1100.                         break;
  1101.                     case 'p':
  1102.                         //pers
  1103.                         if(strncmp(buf+1, "ers", 3)==0){
  1104.                             tmp=strstr(buf+12, "PERS=");
  1105.                             if(tmp!=NULL){
  1106.                                 tmp+=5;
  1107.                                 tmp2=strchr(tmp, 10);
  1108.                                 if(tmp2==0) tmp2=strchr(tmp, 9);
  1109.                                 if(tmp2!=NULL){
  1110.                                     tmp2[0]=0;
  1111.                                     user->SelectedPerson=-1;
  1112.                                     user->SelectPerson(tmp);
  1113.                                     if(user->SelectedPerson!=-1){
  1114.                                         sprintf(arr2[0], "NAME=%s", user->Username);
  1115.                                         sprintf(arr2[1], "PERS=%s", user->Personas[user->SelectedPerson]);
  1116.                                         sprintf(arr2[2], "LAST=2003.12.8 15:51:58");
  1117.                                         sprintf(arr2[3], "PLAST=2003.12.8 16:51:40");
  1118.                                         sprintf(arr2[4], "LKEY=3fcf27540c92935b0a66fd3b0000283c");
  1119.  
  1120.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "pers", arr, 5));
  1121.                                     }else{
  1122.                                         sprintf(log, "Could not select person.\n");
  1123.                                         Log(log);
  1124.                                     }
  1125.                                 }else{
  1126.                                     sprintf(log, "Wrong params in pers message\n");
  1127.                                     Log(log);
  1128.                                 }
  1129.                             }else{
  1130.                                 sprintf(log, "Wrong params in pers message\n");
  1131.                                 Log(log);
  1132.                             }
  1133.                         }
  1134.                         break;
  1135.                     case 's':
  1136.                         switch(buf[1]){
  1137.                             case 'n':
  1138.                                 //snap
  1139. /*
  1140. +snp
  1141. N=username
  1142. R=item index
  1143. P=rep points
  1144. S=1,wins_in_hex,loses_in_hex,
  1145. 0x0000   2B 73 6E 70 00 00 00 00-00 00 00 A4 4E 3D 4C 75   +snp.......¤N=Lu
  1146. 0x0010   67 6E 65 72 63 68 72 69-73 09 52 3D 31 09 50 3D   gnerchris.R=1.P=
  1147. 0x0020   32 35 30 30 30 30 37 34-09 53 3D 31 2C 66 36 61   25000074.S=1,f6a
  1148. 0x0030   2C 39 33 38 2C 31 39 36-2C 31 37 64 37 38 38 61   ,938,196,17d788a
  1149. 0x0040   2C 36 39 37 66 31 2C 61-32 63 2C 32 37 30 66 2C   ,697f1,a2c,270f,
  1150. 0x0050   2C 2C 2C 36 34 2C 36 35-2C 36 35 2C 32 37 30 66   ,,,64,65,65,270f
  1151. 0x0060   2C 2C 2C 2C 36 34 2C 36-35 2C 36 35 2C 32 37 30   ,,,,64,65,65,270
  1152. 0x0070   66 2C 2C 31 2C 2C 36 34-2C 36 61 34 38 2C 32 31   f,,1,,64,6a48,21
  1153. 0x0080   35 39 2C 31 2C 66 36 61-2C 39 33 37 2C 31 39 36   59,1,f6a,937,196
  1154. 0x0090   2C 35 66 35 65 30 66 66-2C 31 39 66 34 62 32 2C   ,5f5e0ff,19f4b2,
  1155. 0x00A0   36 38 64 00 2B 73 6E 70-00 00 00 00 00 00 00 A2   68d.+snp.......Ñž
  1156. */
  1157.                                 if(strncmp(buf+2, "ap", 2)==0){
  1158.                                     if(Verbose){
  1159.                                         sprintf(log, "Rank list\n");
  1160.                                         Log(log);
  1161.                                     }
  1162.                                     int index, chan, start, range;
  1163.                                     tmp=buf+18;
  1164.                                     tmp2=strchr(tmp, 10);
  1165.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  1166.                                     tmp2[0]=0;
  1167.                                     index=atoi(tmp);
  1168.  
  1169.                                     tmp=tmp2+6;
  1170.                                     tmp2=strchr(tmp, 10);
  1171.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  1172.                                     tmp2[0]=0;
  1173.                                     chan=atoi(tmp);
  1174.  
  1175.                                     tmp=tmp2+7;
  1176.                                     tmp2=strchr(tmp, 10);
  1177.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  1178.                                     tmp2[0]=0;
  1179.                                     start=atoi(tmp);
  1180.  
  1181.                                     tmp=tmp2+7;
  1182.                                     tmp2=strchr(tmp, 10);
  1183.                                     if(tmp2==NULL) tmp2=strchr(tmp, 9);
  1184.                                     tmp2[0]=0;
  1185.                                     range=atoi(tmp);
  1186.  
  1187.                                     sprintf(arr2[0], "INDEX=%u", index);
  1188.                                     sprintf(arr2[1], "CHAN=%u", chan);
  1189.                                     sprintf(arr2[2], "START=%u", start);
  1190.                                     sprintf(arr2[3], "RANGE=%u", range);
  1191.                                     sprintf(arr2[4], "SEQN=0");
  1192.  
  1193.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "snap", arr, 5));
  1194.  
  1195.                                     for(k=start;k<range;k++){
  1196.                                         sprintf(arr2[0], "N=VDL%u", k);
  1197.                                         sprintf(arr2[1], "R=%u", k+1);
  1198.                                         sprintf(arr2[2], "P=%u", k);
  1199.                                         sprintf(arr2[3], "S=");
  1200.                                         temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "+snp", arr, 4));
  1201.                                     }
  1202.                                 }
  1203.                                 break;
  1204.                             case 'k':
  1205.                                 //skey
  1206.                                 if(strncmp(buf+2, "ey", 2)==0){                        
  1207.                                     if(Verbose){
  1208.                                         sprintf(log, "skey\n");
  1209.                                         Log(log);
  1210.                                     }
  1211.                                     sprintf(arr2[0], "SKEY=$37940faf2a8d1381a3b7d0d2f570e6a7");
  1212.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "skey", arr, 1));
  1213.                                 }
  1214.                                 break;
  1215.                             case 'e':
  1216.                                 //sele
  1217.                                 if(strncmp(buf+2, "le", 2)==0){                        
  1218.                                     if(Verbose){
  1219.                                         sprintf(log, "sele\n");
  1220.                                         Log(log);
  1221.                                     }
  1222.                                     sprintf(arr2[0], "GAMES=1");
  1223.                                     sprintf(arr2[1], "ROOMS=1");
  1224.                                     sprintf(arr2[2], "USERS=1");
  1225.                                     sprintf(arr2[3], "MESGS=1");
  1226.                                     sprintf(arr2[4], "RANKS=0");
  1227.                                     sprintf(arr2[5], "MORE=1");
  1228.                                     sprintf(arr2[6], "SLOTS=36");
  1229.  
  1230.                                     temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "sele", arr, 7));
  1231.                                 }
  1232.                                 break;
  1233.                         }
  1234.                         break;
  1235.                     case 'u':
  1236.                         if(strncmp(buf+1, "ser", 3)==0){
  1237.                             if(Verbose){
  1238.                                 sprintf(log, "user\n");
  1239.                                 Log(log);
  1240.                             }
  1241.                             sprintf(arr2[0], "PERS=%s", temp->user->Personas[temp->user->SelectedPerson]);
  1242.                             sprintf(arr2[1], "LAST=2004.6.1 15:57:52");
  1243.                             sprintf(arr2[2], "EXPR=1072566000");
  1244.                             sprintf(arr2[3], "STAT=");
  1245.                             sprintf(arr2[4], "CHEAT=3");
  1246.                             sprintf(arr2[5], "ACK_REP=186");
  1247.                             sprintf(arr2[6], "REP=186");
  1248.                             sprintf(arr2[7], "PLAST=2004.6.1 15:57:46");
  1249.                             sprintf(arr2[8], "PSINCE=2003.11.25 07:56:09");
  1250.                             sprintf(arr2[9], "DCNT=0");
  1251.                             sprintf(arr2[10], "ADDR=%s", temp->user->IP);
  1252.                             sprintf(arr2[11], "SERV=159.153.229.239");
  1253.                             sprintf(arr2[12], "RANK=99999");
  1254.                             sprintf(arr2[13], "MESG=vdlgnome");
  1255.  
  1256.                             temp->OutgoingMessages.AddMessage(MakeMessage(buffer, "user", arr, 14));
  1257.                         }
  1258.                         break;
  1259.                     default:                       
  1260.                         if(Verbose){
  1261.                             sprintf(log, "Other recv: %s\n", buf);
  1262.                             Log(log);
  1263.                         }
  1264.                         break;
  1265.                 }  
  1266.                 free(msg->Message);
  1267.                 free(msg);
  1268.                 msg=temp->IncomingMessages.RemoveFirstMessage();
  1269.             }
  1270.             temp=temp->Next;
  1271.         }
  1272.         ClientConnections.mut.Unlock();
  1273.         Sleep(10);
  1274.     }
  1275. };
  1276.  
  1277. bool SendData(char *text, unsigned long ip) {
  1278.     SOCKADDR_IN remote_sockaddr_in;
  1279.  
  1280.     remote_sockaddr_in.sin_family = AF_INET;
  1281.     remote_sockaddr_in.sin_port = htons(10801);
  1282.     remote_sockaddr_in.sin_addr.s_addr = ip;
  1283.  
  1284.     return (sendto(ClientReportingSocket,text,strlen(text),0, (SOCKADDR *)&remote_sockaddr_in, sizeof(SOCKADDR_IN)) != SOCKET_ERROR);
  1285. };
  1286.  
  1287. threadfunc ClientReporter(void *Dummy){
  1288.     int status;
  1289.     SOCKADDR_IN remote_sockaddr_in;
  1290.     unsigned long remote_sockaddr_length = sizeof(SOCKADDR_IN);
  1291.     char tempBuff[1024];
  1292.  
  1293.     while(running){
  1294. #ifndef _WIN32
  1295.         status = recvfrom(ClientReportingSocket, tempBuff, 1024, 0, (SOCKADDR *)&remote_sockaddr_in,(socklen_t*) &remote_sockaddr_length);
  1296. #else
  1297.         status = recvfrom(ClientReportingSocket, tempBuff, 1024, 0, (SOCKADDR *)&remote_sockaddr_in,(int *) &remote_sockaddr_length);
  1298. #endif
  1299.         if (status == SOCKET_ERROR) {
  1300.             sprintf(tempBuff, "Recvfrom failed @ ClientReporter.\nExiting ClientReporter thread - AutoFind feature won't be available till server is restarted!!!.\n");
  1301.             Log(tempBuff);
  1302.             closesocket(ClientReportingSocket);
  1303.             RETURNFROMTHREAD;
  1304.         }      
  1305.         if(Verbose){
  1306.             sprintf(tempBuff, "Server finder connected from %s\n", inet_ntoa(remote_sockaddr_in.sin_addr));
  1307.             Log(tempBuff);
  1308.         }
  1309.         sprintf(tempBuff, "%s", NFSU_LAN_VERSION);
  1310. #ifdef _WIN32
  1311.         if(!SendData(tempBuff, remote_sockaddr_in.sin_addr.S_un.S_addr)){
  1312. #else
  1313.         if(!SendData(tempBuff, remote_sockaddr_in.sin_addr.s_addr)){
  1314. #endif
  1315.             sprintf(tempBuff, "Could not send data to server finder: %s\n", inet_ntoa(remote_sockaddr_in.sin_addr));
  1316.             Log(tempBuff);
  1317.         }
  1318.     }
  1319.     closesocket(ClientReportingSocket);
  1320. };
  1321.  
  1322. threadfunc ClientReporterTcp(void *Dummy){
  1323.     SOCKET cl;
  1324.     char tempBuff[1024];
  1325.  
  1326.     sprintf(tempBuff, "Starting ClientReporterTcp thread\n");
  1327.     Log(tempBuff);
  1328.  
  1329.     SOCKADDR_IN remote_sockaddr_in;
  1330.     unsigned long remote_sockaddr_length = sizeof(SOCKADDR_IN);
  1331.     int status;
  1332.  
  1333.     while(running){
  1334.         cl=accept(ClientReportingSocketTcp, NULL, NULL);
  1335.         if(cl!=INVALID_SOCKET){
  1336. #ifndef _WIN32
  1337.             getpeername(cl, (SOCKADDR *)&remote_sockaddr_in,(socklen_t*) &remote_sockaddr_length);
  1338. #else
  1339.             getpeername(cl, (SOCKADDR *)&remote_sockaddr_in,(int*) &remote_sockaddr_length);
  1340. #endif         
  1341.             if(Verbose){
  1342.                 sprintf(tempBuff, "Server finder connected from %s\n", inet_ntoa(remote_sockaddr_in.sin_addr));
  1343.                 Log(tempBuff);
  1344.             }
  1345.             status=(int)difftime(curtime, Server.Startup);
  1346.             sprintf(tempBuff, "%u|%u|%u|%s|%s|%s", ClientConnections.Count, Server.Rooms.Count, status, SERVER_PLATFORM, NFSU_LAN_VERSION, Server.Name);
  1347.             send(cl, tempBuff, strlen(tempBuff), 0);
  1348.             closesocket(cl);
  1349.         }
  1350.     }
  1351.     closesocket(ClientReportingSocketTcp);
  1352. };
  1353.  
  1354. threadfunc StatThread(void *Dummy){
  1355.     SOCKET cl;
  1356.     char tempBuff[1024];
  1357.  
  1358.     sprintf(tempBuff, "Starting StatThread\n");
  1359.     Log(tempBuff);
  1360.  
  1361.     SOCKADDR_IN remote_sockaddr_in;
  1362.     unsigned long remote_sockaddr_length = sizeof(SOCKADDR_IN);
  1363.     int status;
  1364.     RoomClass *tr;
  1365.     RUserClass *tu;
  1366.  
  1367.     while(running){
  1368.         cl=accept(ReportingSocket, NULL, NULL);
  1369.         if(cl!=INVALID_SOCKET){
  1370. #ifndef _WIN32
  1371.             getpeername(cl, (SOCKADDR *)&remote_sockaddr_in,(socklen_t*) &remote_sockaddr_length);
  1372. #else
  1373.             getpeername(cl, (SOCKADDR *)&remote_sockaddr_in,(int*) &remote_sockaddr_length);
  1374. #endif         
  1375.             if(Verbose){
  1376.                 sprintf(tempBuff, "Stat client connected from %s\n", inet_ntoa(remote_sockaddr_in.sin_addr));
  1377.                 Log(tempBuff);
  1378.             }
  1379.             status=(int)difftime(curtime, Server.Startup);
  1380.             sprintf(tempBuff, "%u|%u|%u|%s|%s|%s~~~", ClientConnections.Count, Server.Rooms.Count, status, SERVER_PLATFORM, NFSU_LAN_VERSION, Server.Name);
  1381.             send(cl, tempBuff, strlen(tempBuff), 0);
  1382.  
  1383.             tr=Server.Rooms.First;
  1384.             while(tr!=NULL){
  1385.                 sprintf(tempBuff, "%s|%u|[", tr->Name, tr->Users.Count);
  1386.                 send(cl, tempBuff, strlen(tempBuff), 0);
  1387.                 tu=tr->Users.First;
  1388.                 while(tu!=NULL){
  1389.                     sprintf(tempBuff, "%s|", tu->User->Personas[tu->User->SelectedPerson]);
  1390.                     send(cl, tempBuff, strlen(tempBuff), 0);
  1391.                     tu=tu->Next;
  1392.                 }
  1393.                 send(cl, "]", 1, 0);
  1394.                 tr=tr->Next;
  1395.             }
  1396.             closesocket(cl);
  1397.         }
  1398.     }
  1399.     closesocket(ReportingSocket);
  1400. };
  1401.  
  1402. //watches for timeouted clients
  1403. threadfunc Maintenance(void *Dummy){
  1404.     Log("Maintenance thread started\n");
  1405.     UserClass *user, *t;
  1406.     while(running){
  1407.         Sleep(1000);
  1408.         time(&curtime);
  1409.         user=Server.Users.First;
  1410.         while(user!=NULL){
  1411.             user->Idle++;
  1412.             if((user->Idle>60*45)&&(user->Connection==NULL)){
  1413.                 Log("Removing user due to 45 min timeout\n");
  1414.                 t=user->Next;
  1415.                 Server.Users.RemoveUser(user);
  1416.                 free(user);
  1417.                 user=t;
  1418.             }else{
  1419.                 user=user->Next;
  1420.             }
  1421.         }
  1422.     }
  1423.     RETURNFROMTHREAD;
  1424. };
  1425.  
  1426. #ifdef NT_SERVICE
  1427. VOID WriteInLogFile( char *text )
  1428. {
  1429.     g_LogFile = fopen( "Service.log", "a+t" );
  1430.    
  1431.     if ( g_LogFile )
  1432.     {
  1433.         fprintf( g_LogFile, text );
  1434.         fclose( g_LogFile );
  1435.     }
  1436.    
  1437.     return;
  1438. }
  1439.  
  1440. VOID WINAPI ServiceCtrlHandler(DWORD dwControl)
  1441. {
  1442.     sprintf(g_Msg, "In to service control...\n");
  1443.     WriteInLogFile(g_Msg);
  1444.  
  1445.     switch(dwControl)
  1446.     {
  1447.         case SERVICE_CONTROL_PAUSE:
  1448.             ServiceStatus.dwCurrentState = SERVICE_PAUSED;
  1449.             break;
  1450.  
  1451.         case SERVICE_CONTROL_CONTINUE:
  1452.             ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  1453.             break;
  1454.  
  1455.         case SERVICE_CONTROL_STOP:
  1456.             sprintf(g_Msg, "Stopping...\n");
  1457.             WriteInLogFile(g_Msg);
  1458.            
  1459.             ServiceStatus.dwWin32ExitCode = 0;
  1460.             ServiceStatus.dwCurrentState  = SERVICE_STOP_PENDING;
  1461.             ServiceStatus.dwCheckPoint    = 0;
  1462.             ServiceStatus.dwWaitHint      = 0;
  1463.  
  1464.             running = false;
  1465.  
  1466.             if ( !SetServiceStatus( ServiceStatusHandle, &ServiceStatus) )
  1467.             {
  1468.                 sprintf(g_Msg,"NFSU:LAN SetServiceStatus() error %ld\n",GetLastError() );
  1469.                 WriteInLogFile(g_Msg);
  1470.             }
  1471.  
  1472.             sprintf(g_Msg, "NFSU:LAN leaving handler \n", 0 );
  1473.             WriteInLogFile(g_Msg);
  1474.            
  1475.             return;
  1476.  
  1477.         case SERVICE_CONTROL_INTERROGATE:
  1478.             break;
  1479.  
  1480.         default:
  1481.             sprintf(g_Msg, "NFSU:LAN unrecognized control code %ld\n", dwControl );
  1482.             WriteInLogFile(g_Msg);
  1483.     }
  1484.  
  1485.     // Send current status.
  1486.     if ( !SetServiceStatus(ServiceStatusHandle, &ServiceStatus) )
  1487.     {
  1488.         sprintf(g_Msg,"NFSU:LAN SetServiceStatus() error %ld\n", GetLastError() );
  1489.         WriteInLogFile( g_Msg );
  1490.     }
  1491.  
  1492.     return;
  1493. }
  1494. #endif
  1495.  
  1496. bool InitServer(){
  1497. #ifdef NT_SERVICE
  1498.     EnableLogFile=GetPrivateProfileInt("NFSU:LAN", "EnableLogFile", 1, "nfsu.ini");
  1499.     EnableLogScreen=false;
  1500.     RewriteLogFile=GetPrivateProfileInt("NFSU:LAN", "RewriteLogFile", 1, "nfsu.ini");
  1501.     DisableTimeStamp=GetPrivateProfileInt("NFSU:LAN", "DisableTimeStamp", 0, "nfsu.ini");
  1502.     Verbose=GetPrivateProfileInt("NFSU:LAN", "Verbose", 0, "nfsu.ini");
  1503.     RegisterGlobal=GetPrivateProfileInt("NFSU:LAN", "RegisterGlobal", 0, "nfsu.ini");
  1504.     LogAllTraffic=GetPrivateProfileInt("NFSU:LAN", "LogAllTraffic", 0, "nfsu.ini");
  1505.     BanV1=GetPrivateProfileInt("NFSU:LAN", "BanV1", 0, "nfsu.ini");
  1506.     BanV2=GetPrivateProfileInt("NFSU:LAN", "BanV2", 0, "nfsu.ini");
  1507.     BanV3=GetPrivateProfileInt("NFSU:LAN", "BanV3", 0, "nfsu.ini");
  1508.     BanV4=GetPrivateProfileInt("NFSU:LAN", "BanV4", 0, "nfsu.ini");
  1509.     GetPrivateProfileString("NFSU:LAN", "ServerName", "LAN Service server", Server.Name, 100, "nfsu.ini");
  1510. #endif
  1511.  
  1512.     time(&curtime);
  1513.     RoomClass *room;
  1514.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1515.     room->IsGlobal=true;
  1516.     strcpy(room->Name, "A.LAN");
  1517.     Server.Rooms.AddRoom(room);
  1518.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1519.     room->IsGlobal=true;
  1520.     strcpy(room->Name, "B.LAN");
  1521.     Server.Rooms.AddRoom(room);
  1522.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1523.     room->IsGlobal=true;
  1524.     strcpy(room->Name, "C.LAN");
  1525.     Server.Rooms.AddRoom(room);
  1526.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1527.     room->IsGlobal=true;
  1528.     strcpy(room->Name, "D.LAN");
  1529.     Server.Rooms.AddRoom(room);
  1530.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1531.     room->IsGlobal=true;
  1532.     strcpy(room->Name, "E.LAN");
  1533.     Server.Rooms.AddRoom(room);
  1534.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1535.     room->IsGlobal=true;
  1536.     strcpy(room->Name, "F.LAN");
  1537.     Server.Rooms.AddRoom(room);
  1538.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1539.     room->IsGlobal=true;
  1540.     strcpy(room->Name, "G.LAN");
  1541.     Server.Rooms.AddRoom(room);
  1542.     room=(RoomClass*)calloc(1, sizeof(RoomClass));
  1543.     room->IsGlobal=true;
  1544.     strcpy(room->Name, "H.LAN");
  1545.     Server.Rooms.AddRoom(room);
  1546.  
  1547.     int k;
  1548.     char log[1024];
  1549.  
  1550.     if(Server.Name[0]==0){
  1551.         strcpy(Server.Name, "Default server name");
  1552.     }
  1553.  
  1554.     //opening logfile
  1555.     if(EnableLogFile){
  1556.         if(RewriteLogFile){
  1557.             logfil=fopen("server.log", "w");
  1558.         }else{
  1559.             logfil=fopen("server.log", "a");
  1560.         }
  1561.         if(logfil==NULL){
  1562.             EnableLogFile=false;
  1563.             sprintf(log, "Could not open logfile - logging to file will be disabled.\n");
  1564.         }
  1565.     }
  1566.    
  1567.     //opening logfile
  1568.     if(LogAllTraffic){
  1569.         tlogfil=fopen("traffic.log", "a");
  1570.         if(tlogfil==NULL){
  1571.             LogAllTraffic=false;
  1572.             sprintf(log, "Could not open traffic logfile - logging to file will be disabled.\n");
  1573.         }
  1574.     }
  1575.  
  1576.     sprintf(log, "%s NFSU:LAN server [%s] v %s starting\n", Server.Name, SERVER_PLATFORM, NFSU_LAN_VERSION);
  1577.     Log(log);
  1578.    
  1579. #ifdef _WIN32
  1580.     WSAData wda;
  1581.     if(WSAStartup(MAKEWORD( 2, 2 ), &wda)!=0){
  1582.         sprintf(log, "Could not init winsocks.\n");
  1583.         Log(log);
  1584.         return false;
  1585.     }
  1586. #endif
  1587.  
  1588.     //reading news;
  1589.     LoadNews();
  1590.    
  1591.     //making sockets
  1592.     RedirectSocket = socket(AF_INET,SOCK_STREAM,0);
  1593.     ListeningSocket = socket(AF_INET,SOCK_STREAM,0);
  1594.     ReportingSocket = socket(AF_INET,SOCK_STREAM,0);
  1595.     ClientReportingSocket = socket(AF_INET, SOCK_DGRAM, 0);
  1596.     ClientReportingSocketTcp =  socket(AF_INET, SOCK_STREAM, 0);
  1597.  
  1598.     //binding them to specific ports
  1599.     SOCKADDR_IN localsin;
  1600.     localsin.sin_family = AF_INET;
  1601.     localsin.sin_addr.s_addr = INADDR_ANY;
  1602.  
  1603.     localsin.sin_port = htons(10900);
  1604.     k=bind(RedirectSocket,(SOCKADDR *)&localsin, sizeof(SOCKADDR_IN));
  1605.     if(k==INVALID_SOCKET){
  1606.         sprintf(log, "Could not bind socket to 10900.\n");
  1607.         Log(log);
  1608.         return false;
  1609.     }
  1610.     localsin.sin_port = htons(10901);
  1611.     k=bind(ListeningSocket,(SOCKADDR *)&localsin, sizeof(SOCKADDR_IN));
  1612.     if(k==INVALID_SOCKET){
  1613.         sprintf(log, "Could not bind socket to 10901.\n");
  1614.         Log(log);
  1615.         closesocket(RedirectSocket);
  1616.         return false;
  1617.     }
  1618.     localsin.sin_port = htons(10980);
  1619.     k=bind(ReportingSocket,(SOCKADDR *)&localsin, sizeof(SOCKADDR_IN));
  1620.     if(k==INVALID_SOCKET){
  1621.         sprintf(log, "Could not bind socket to 10980.\n");
  1622.         Log(log);
  1623.         closesocket(RedirectSocket);
  1624.         closesocket(ListeningSocket);
  1625.         return false;
  1626.     }
  1627.     localsin.sin_port = htons(10800);
  1628.     k=bind(ClientReportingSocket,(SOCKADDR *)&localsin, sizeof(SOCKADDR_IN));
  1629.     if(k==INVALID_SOCKET){
  1630.         sprintf(log, "Could not bind socket to 10800.\n");
  1631.         Log(log);
  1632.         closesocket(RedirectSocket);
  1633.         closesocket(ListeningSocket);
  1634.         closesocket(ReportingSocket);
  1635.         return false;
  1636.     }
  1637.  
  1638.     localsin.sin_port = htons(10800);
  1639.     k=bind(ClientReportingSocketTcp,(SOCKADDR *)&localsin, sizeof(SOCKADDR_IN));
  1640.     if(k==INVALID_SOCKET){
  1641.         sprintf(log, "Could not bind socket to 10800.\n");
  1642.         Log(log);
  1643.         closesocket(RedirectSocket);
  1644.         closesocket(ListeningSocket);
  1645.         closesocket(ReportingSocket);
  1646.         closesocket(ClientReportingSocket);
  1647.         return false;
  1648.     }
  1649.  
  1650.     //listen to ports
  1651.     listen(RedirectSocket, 100);
  1652.     listen(ListeningSocket, 100);
  1653.     listen(ReportingSocket, 100);
  1654.     listen(ClientReportingSocket, 100);
  1655.     listen(ClientReportingSocketTcp, 100);
  1656.  
  1657.     strcpy(RedirectConnections.Name, "RedirectConnections");
  1658.     strcpy(ClientConnections.Name, "ClientConnections");
  1659.     strcpy(ReportingConnections.Name, "ReportingConnections");
  1660.  
  1661.     //starting threads
  1662.     ConAccParam Redirector, Listener, Reporter;
  1663.  
  1664.     Redirector.Connections=&RedirectConnections;
  1665.     Redirector.sock=RedirectSocket;
  1666.     strcpy(Redirector.Name, "Redirector");
  1667.     Listener.Connections=&ClientConnections;
  1668.     Listener.sock=ListeningSocket;
  1669.     strcpy(Listener.Name, "Listener");
  1670.     Reporter.Connections=&ReportingConnections;
  1671.     Reporter.sock=ReportingSocket;
  1672.     strcpy(Reporter.Name, "Reporter");
  1673.    
  1674.     _beginthread(AcceptThread, 0, (void*)&Listener);
  1675.     _beginthread(AcceptThread, 0, (void*)&Redirector);
  1676.     //_beginthread(AcceptThread, 0, (void*)&Reporter);
  1677.  
  1678.     _beginthread(IOThread, 0, (void*)&RedirectConnections);
  1679.     _beginthread(IOThread, 0, (void*)&ClientConnections);
  1680.     //_beginthread(IOThread, 0, (void*)&ReportingConnections);
  1681.  
  1682.     _beginthread(RedirectorWorker, 0, NULL);
  1683.     _beginthread(ListenerWorker, 0, NULL);
  1684.  
  1685.     _beginthread(Maintenance, 0, NULL);
  1686.  
  1687.     //responses to client find_server requests
  1688.     _beginthread(ClientReporter, 0, NULL);
  1689.     _beginthread(ClientReporterTcp, 0, NULL);
  1690.     _beginthread(StatThread, 0, NULL);
  1691.     if(RegisterGlobal)
  1692.         _beginthread(WebReport, 0, NULL);
  1693.  
  1694.     return true;
  1695. }
  1696.  
  1697. void DeInitServer(){
  1698.     char log[1024];
  1699.     //closing sockets
  1700.     closesocket(RedirectSocket);
  1701.     closesocket(ListeningSocket);
  1702.     closesocket(ReportingSocket);
  1703.     closesocket(ClientReportingSocket);
  1704.  
  1705.     free(news);
  1706.     UserClass *tuser;
  1707.     GameClass *tgame;
  1708.     RoomClass *troom;
  1709.     RegUser *treguser;
  1710.     ConnectionClass *tcon;
  1711.  
  1712.  
  1713.     while(RedirectConnections.Count>0){
  1714.         tcon=RedirectConnections.First;
  1715.         RedirectConnections.RemoveConnection(tcon);
  1716.         if(tcon->user!=NULL) tcon->user->Connection=NULL;
  1717.         tcon->IncomingMessages.Clear();
  1718.         tcon->OutgoingMessages.Clear();
  1719.         tcon->IncomingMessages.mut.DeInit();
  1720.         tcon->OutgoingMessages.mut.DeInit();
  1721.         free(tcon);
  1722.     }
  1723.  
  1724.     while(Server.Users.Count>0) {
  1725.         tuser=Server.Users.First;
  1726.         Server.Users.RemoveUser(tuser);
  1727.         if(tuser->Game!=NULL){
  1728.             tgame=tuser->Game;
  1729.             tgame->RemoveUser(tuser, log);
  1730.             if(tgame->Count==0) {
  1731.                 tuser->CurrentRoom->Games.RemoveGame(tgame);
  1732.                 free(tgame);
  1733.             }
  1734.         }
  1735.         if(tuser->CurrentRoom!=NULL){
  1736.             troom=tuser->CurrentRoom;
  1737.             troom->RemoveUser(tuser, log);
  1738.             if(troom->Count==0){
  1739.                 Server.Rooms.RemoveRoom(troom);
  1740.                 free(troom);
  1741.             }
  1742.         }
  1743.         free(tuser);
  1744.     }
  1745.     while(Server.Rooms.Count>0){
  1746.         troom=Server.Rooms.First;
  1747.         Server.Rooms.RemoveRoom(troom);
  1748.         free(troom);
  1749.     }
  1750.     while(Server.ru.Count>0){
  1751.         treguser=Server.ru.First;
  1752.         Server.ru.RemoveUser(treguser);
  1753.         free(treguser);
  1754.     }
  1755.  
  1756.     //closing logfile
  1757.     if(logfil!=NULL) fclose(logfil);
  1758.  
  1759. #ifdef _WIN32
  1760.     //unloading winsocks
  1761.     WSACleanup();
  1762. #endif
  1763. };
  1764.  
  1765. #ifdef NT_SERVICE
  1766. // Second function to implement
  1767. VOID WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
  1768. {
  1769. //    DWORD status;
  1770. //    DWORD specificError;
  1771.  
  1772.     ServiceStatus.dwServiceType        = SERVICE_WIN32_OWN_PROCESS;
  1773.     ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
  1774.     ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP;
  1775.     ServiceStatus.dwWin32ExitCode      = 0;
  1776.     ServiceStatus.dwServiceSpecificExitCode = 0;
  1777.     ServiceStatus.dwCheckPoint         = 0;
  1778.     ServiceStatus.dwWaitHint           = 0;
  1779.  
  1780.     ServiceStatusHandle = RegisterServiceCtrlHandler( "NFSU:LAN", ServiceCtrlHandler );
  1781.  
  1782.     if ( ServiceStatusHandle == (SERVICE_STATUS_HANDLE) 0 )
  1783.     {
  1784.         sprintf(g_Msg, "NFSU:LAN RegisterServiceCtrlHandler() failed %d\n", GetLastError() );
  1785.         WriteInLogFile( g_Msg );
  1786.         return;
  1787.     }
  1788.  
  1789. //    status = ServiceInitialization( argc, argv, &specificError );
  1790. //    if ( status != NO_ERROR )
  1791. //    {
  1792. //        ServiceStatus.dwCurrentState       = SERVICE_STOPPED;
  1793. //        ServiceStatus.dwCheckPoint         = 0;
  1794. //        ServiceStatus.dwWaitHint           = 0;
  1795. //        ServiceStatus.dwWin32ExitCode      = status;
  1796. //        ServiceStatus.dwServiceSpecificExitCode = specificError;
  1797. //
  1798. //        SetServiceStatus( ServiceStatusHandle, &ServiceStatus );
  1799. //        return;
  1800. //    }
  1801.  
  1802.     ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
  1803.     ServiceStatus.dwCheckPoint         = 0;
  1804.     ServiceStatus.dwWaitHint           = 0;
  1805.  
  1806.     if ( !SetServiceStatus( ServiceStatusHandle, &ServiceStatus ) )
  1807.     {
  1808.         sprintf( g_Msg,"NFSU:LAN SetServiceStatus() error %ld\n", GetLastError() );
  1809.         WriteInLogFile(g_Msg);
  1810.     }
  1811.  
  1812.     running = InitServer();
  1813.  
  1814.     if(running){
  1815.         sprintf( g_Msg, "Just before processing Loop...\n" );
  1816.         WriteInLogFile(g_Msg);
  1817.  
  1818.         ServiceStatus.dwCurrentState       = SERVICE_RUNNING;
  1819.         ServiceStatus.dwCheckPoint         = 0;
  1820.         ServiceStatus.dwWaitHint           = 0;
  1821.      
  1822.         if ( !SetServiceStatus( ServiceStatusHandle, &ServiceStatus ) )
  1823.         {
  1824.             sprintf( g_Msg,"NFSU:LAN SetServiceStatus() error %ld\n", GetLastError() );
  1825.             WriteInLogFile(g_Msg);
  1826.         }
  1827.  
  1828.  
  1829.         while (running)
  1830.         {
  1831.             Sleep(10);
  1832.         }
  1833.         DeInitServer();
  1834.     }
  1835.  
  1836.     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  1837.    
  1838.     // Send current status.
  1839.     if (!SetServiceStatus(ServiceStatusHandle, &ServiceStatus))
  1840.     {
  1841.         sprintf(g_Msg,"HelloService! SetServiceStatus error %ld\n", GetLastError() );
  1842.         WriteInLogFile( g_Msg );
  1843.     }
  1844.  
  1845.     return;
  1846. }
  1847. #endif
  1848.  
  1849.  
  1850. int main(int argc, char* argv[]){
  1851.     for(int k=0;k<30;k++) arr[k]=(char*)&arr2[k];
  1852. #ifdef NT_SERVICE  
  1853. //  The SERVICE_TABLE_ENTRY structure is used by the StartServiceCtrlDispatcher function
  1854. //  to specify the ServiceMain function for a service that can run in the calling process.
  1855.  
  1856.     SERVICE_TABLE_ENTRY DispatchTable[] = { { "NFSU:LAN", ServiceMain }, { 0, 0 } };
  1857.  
  1858.     sprintf( g_Msg, "Log opened\n" );
  1859.     WriteInLogFile( g_Msg );
  1860.  
  1861.     // Install a Service if -i switch used
  1862.     if ( argc > 1 && !( stricmp(argv[1], "-i") ) )
  1863.     {
  1864.         char szBuffer[255];
  1865.         char szPath[MAX_PATH];
  1866.  
  1867.         sprintf( g_Msg, "First Calling OpenSCManager() \n");
  1868.         WriteInLogFile( g_Msg );
  1869.  
  1870. //      The OpenSCManager function establishes a connection to the Service Control Manager(SCM)
  1871. //      on the specified computer and opens the specified service control manager database.
  1872.  
  1873.         SC_HANDLE scmHandle = OpenSCManager ( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  1874.  
  1875.         if (scmHandle == NULL) // Perform error handling.
  1876.         {
  1877.             sprintf( g_Msg, "NFSU:LAN OpenSCManager error = %d\n", GetLastError() );
  1878.             WriteInLogFile(g_Msg);
  1879.         }
  1880.  
  1881.         GetModuleFileName( GetModuleHandle(NULL), szPath, MAX_PATH );
  1882.        
  1883.         strcpy( szBuffer, "\"" );
  1884.         strcat( szBuffer, szPath );
  1885.         strcat( szBuffer, "\"" );
  1886.  
  1887.         //printf( "\n CreateService()! Installing Service %s\n", szPath );
  1888.  
  1889.         SC_HANDLE scHandle;
  1890.         scHandle = CreateService (
  1891.             scmHandle,
  1892.             "NFSU:LAN",
  1893.             "NFSU:LAN",
  1894.             SERVICE_ALL_ACCESS,
  1895.             SERVICE_WIN32_OWN_PROCESS,
  1896.             SERVICE_AUTO_START,
  1897.             SERVICE_ERROR_NORMAL,
  1898.             szBuffer, NULL, NULL, NULL, NULL, NULL );
  1899.  
  1900.         if ( scHandle == NULL ) // Process error
  1901.         {
  1902.             sprintf(g_Msg, " NFSU:LAN CreateService error = %d\n", GetLastError() );
  1903.             WriteInLogFile(g_Msg);
  1904.         }else{
  1905.             printf("NFSU:LAN service installed...");
  1906.         }
  1907.  
  1908.         CloseServiceHandle(scHandle);
  1909.         CloseServiceHandle(scmHandle);
  1910.  
  1911.     }
  1912.     else if ( argc > 1 && !( stricmp(argv[1], "-u" ) ) ) // Uninstall the Service
  1913.     {
  1914.         SC_HANDLE scmHandle = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
  1915.  
  1916.         if (scmHandle == NULL) // Perform error handling.
  1917.         {
  1918.             sprintf(g_Msg, "NFSU:LAN OpenSCManager error = %d\n", GetLastError() );
  1919.             WriteInLogFile(g_Msg);
  1920.         }
  1921.    
  1922.         SC_HANDLE scHandle;
  1923.         scHandle = OpenService( scmHandle, "NFSU:LAN", SERVICE_ALL_ACCESS );
  1924.         if(DeleteService( scHandle )!=0){
  1925.             printf("NFSU:LAN service uninstalled...");
  1926.         }else{
  1927.             switch(GetLastError()){
  1928.                 case ERROR_ACCESS_DENIED:
  1929.                     printf("Access denied\n");
  1930.                     break;
  1931.                 case ERROR_INVALID_HANDLE:
  1932.                     printf("Invalid handle\n");
  1933.                     break;
  1934.                 case ERROR_SERVICE_MARKED_FOR_DELETE:
  1935.                     printf("The specified service has already been marked for deletion.");
  1936.                     break;
  1937.                 default:
  1938.                     printf("Error\n");
  1939.                     break;
  1940.             }
  1941.         }
  1942.     }
  1943.     else
  1944.     {
  1945.         SC_HANDLE scHandle;
  1946.  
  1947.         SC_HANDLE scmHandle = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS);
  1948.         scHandle = OpenService( scmHandle, "NFSU:LAN", SERVICE_ALL_ACCESS );
  1949.  
  1950.         if(scHandle){
  1951.             CloseHandle(scHandle);
  1952.             if ( !StartServiceCtrlDispatcher(DispatchTable) )
  1953.             {
  1954.                 //ghmm smth wrong
  1955.                 printf("To install service use param -i\nTo uninstall service use param -u\n");
  1956.             }
  1957.         }else{
  1958.             //service is not installed but is started without params
  1959.             printf("To install service use param -i\nTo uninstall service use param -u\n");
  1960.         }
  1961.     }
  1962.  
  1963.     return 0;
  1964. #endif
  1965.  
  1966. #ifndef _WIN32
  1967.     signal(SIGPIPE, SIG_IGN);
  1968. #endif
  1969.  
  1970.     EnableLogFile=true;
  1971.     EnableLogScreen=true;
  1972.     RewriteLogFile=true;
  1973.     DisableTimeStamp=false;
  1974.     Verbose=false;
  1975.     RegisterGlobal=false;
  1976.     Server.Name[0]=0;
  1977.     LogAllTraffic=false;
  1978.     BanV1=false;
  1979.     BanV2=false;
  1980.     BanV3=false;
  1981.     BanV4=false;
  1982.  
  1983. #ifndef _WIN32
  1984.     bool daemon=false;
  1985.     bool writepid=false;
  1986. #endif
  1987.  
  1988.     //parse commandline
  1989.     for(int k=1;k<argc;k++){
  1990. #ifndef _WIN32
  1991.         if(stricmp(argv[k], "-d")==0){
  1992.             daemon=true;
  1993.         }
  1994.         if(stricmp(argv[k], "-p")==0){
  1995.             writepid=true;
  1996.         }
  1997. #endif
  1998.         if(stricmp(argv[k], "enablelogfile")==0){
  1999.             EnableLogFile=true;
  2000.         }
  2001.         if(stricmp(argv[k], "registerglobal")==0){
  2002.             RegisterGlobal=true;
  2003.         }
  2004.         if(stricmp(argv[k], "verbose")==0){
  2005.             Verbose=true;
  2006.         }
  2007.         if(stricmp(argv[k], "disablelogscreen")==0){
  2008.             EnableLogScreen=false;
  2009.         }
  2010.         if(stricmp(argv[k], "rewritelogfile")==0){
  2011.             RewriteLogFile=true;
  2012.         }
  2013.         if(stricmp(argv[k], "disabletimestamp")==0){
  2014.             DisableTimeStamp=true;
  2015.         }
  2016.         if(strstr(argv[k], "servername=")!=NULL){
  2017.             strncpy(Server.Name, strstr(argv[k], "servername=")+11, 99);
  2018.         }
  2019.         if(stricmp(argv[k], "logalltraffic")==0){
  2020.             LogAllTraffic=true;
  2021.         }
  2022.         if(stricmp(argv[k], "banv1")==0){
  2023.             BanV1=true;
  2024.         }
  2025.         if(stricmp(argv[k], "banv2")==0){
  2026.             BanV2=true;
  2027.         }
  2028.         if(stricmp(argv[k], "banv3")==0){
  2029.             BanV3=true;
  2030.         }
  2031.         if(stricmp(argv[k], "banv4")==0){
  2032.             BanV4=true;
  2033.         }
  2034.     }
  2035.  
  2036. #ifndef _WIN32
  2037.     if(daemon){
  2038.         //diable output to console in deamon mode
  2039.         EnableLogScreen=false;
  2040.         /* Ignore terminal I/O signals */
  2041.         signal(SIGTTOU, SIG_IGN);
  2042.             signal(SIGTTIN, SIG_IGN);
  2043.             signal(SIGTSTP, SIG_IGN);
  2044.         int pid;
  2045.         pid = fork();
  2046.         if (pid != 0)
  2047.             exit(0); /* this is the parent process here, so let's exit it for our daemon work */
  2048.         setsid(); /* child process, so let's make us a session leader, and lose control tty */
  2049.         signal(SIGHUP, SIG_IGN); /* SIGHUP will be sent to child process so ignore it */
  2050.         if (pid != 0)
  2051.             exit(0);
  2052.  
  2053.         if(writepid){
  2054.             FILE *pidfile=fopen("nfsuserver.pid", "w");
  2055.             if(pidfile!=NULL){
  2056.                 char pi[100];
  2057.                 sprintf(pi, "%u", getpid());
  2058.                 fwrite(pi, strlen(pi), sizeof(char), pidfile);
  2059.                 fclose(pidfile);
  2060.             }
  2061.         }
  2062.     }  
  2063. #endif
  2064.     if(InitServer()){
  2065.         while(running){
  2066.             Sleep(10);
  2067.         }
  2068.     }
  2069.     DeInitServer();
  2070.     return 0;
  2071. }
Add Comment
Please, Sign In to add comment