#include "LoginServ.h" #include "../../src/ConfigLoader.h" #include "../../src/Item.h" int CustomNetworkProcessing(uchar* pData, ulong dataSize, GwClient* pClient); int PopulateLsOpcodeTable(); int LsClientConnected(GwClient* pClient); int LsClientDisconnected(GwClient* pClient); long GetFileSize(FILE * hFile) { long lCurPos=0; long lEndPos=0; // Check for valid file pointer if ( hFile == NULL ) { return -1; } // Store current position lCurPos = ftell(hFile); // Move to the end and get position fseek(hFile, 0, 2); lEndPos=ftell(hFile); // Restore the file pointer fseek(hFile, lCurPos, 0); return lEndPos; } int LoginServ::Init() { mpLoginServer=NULL; mpInternalServer=NULL; this->mClientIds.Init(); //Initialize the system log file mSysLog.Open("Logs/LsSysLog.txt"); mSysLog.Write("----------------------------------------------------------------\n"); mSysLog.Write("GWLP Login Server v%s\n\n", SERVER_VERSION); mSysLog.Write("[+] System Log File Opened\n"); //Initialize the network library. if(GwServer::StartSubsystem() != 0) { mSysLog.Write("[!] Failed to initialize the network subsystem.\n"); return -1; } //Variables to load some configuration options into bool lsAutomaticIp=true; char lsIp[20]; ushort lsPort=8112; ulong lsMaxPlayers=20; char dbIp[20]; ushort dbPort=3306; char dbName[25]; char dbUser[25]; char dbPass[25]; ushort dbMaxPipes=1; ushort dbMaxRetries=3; ushort isPort=7112; //Load the configuration file. ConfigLoader config; if(config.Load("Config/LsConfig.ini") != 0) { mSysLog.Write("[!] Failed to the login server configuration file.\n"); return -2; } config.GetDataAsString ("IPAddress", (char*)lsIp); config.GetDataAsShort ("Port", (short*)&lsPort); config.GetDataAsUInt ("MaxPlayers", (unsigned int*)&lsMaxPlayers); config.GetDataAsBool ("LockServerAtStart", &this->mServerLocked); config.GetDataAsBool ("ThrottleCPU", &this->mThrottleCpu); config.GetDataAsBool ("RequireValidKey", &this->mRequireValidAccessKey); config.GetDataAsString ("DatabaseIP", dbIp); config.GetDataAsShort ("DatabasePort", (short*)&dbPort); config.GetDataAsString ("DatabaseName", dbName); config.GetDataAsString ("DatabaseUser", dbUser); config.GetDataAsString ("DatabasePass", dbPass); config.GetDataAsShort ("MaxPipes", (short*)&dbMaxPipes); config.GetDataAsShort ("MaxRetryConnects", (short*)&dbMaxRetries); config.GetDataAsString ("GameServPass", (char*)this->mAuthPassword); config.GetDataAsShort ("MaxGameServers", (short*)&this->mMaxGameServers); config.GetDataAsShort ("ICPort", (short*)&isPort); config.GetDataAsBool ("DetectGameServIP", &this->mDetectGameServIp); config.GetDataAsBool ("IgnoreNoGameServer", &this->mIgnoreNoGameServer); config.GetDataAsBool ("RequireValidPassword", &this->mRequireValidPassword); if(config.ErrorOccurred()) { mSysLog.Write("[!] An error was encountered while processing the config file.\n"); return -3; } config.CleanUp(); mSysLog.Write("[+] Loaded login server configuration file.\n"); //Load the file server packet FILE* pFile=NULL; if(!(pFile=fopen("Data/Packets/NoUpdateAvailable","rb"))) { mSysLog.Write("[!] Could not load file server packet.\n"); return -8; } long fileSize=GetFileSize(pFile); this->mpNoUpdatePacket=new uchar[fileSize]; fread(this->mpNoUpdatePacket, 1, fileSize, pFile); fclose(pFile); mSysLog.Write("[+] Successfully loaded the file server packet.\n"); //Load the default interface data if(!(pFile=fopen("Data/Packets/DefaultInterface","rb"))) { mSysLog.Write("[!] Could not load default interface data.\n"); return -9; } this->mDefaultInterfaceSize=GetFileSize(pFile); this->mpDefaultInterface=new uchar[this->mDefaultInterfaceSize]; fread(this->mpDefaultInterface, 1, this->mDefaultInterfaceSize, pFile); fclose(pFile); mSysLog.Write("[+] Successfully loaded the default interface data.\n"); //Start the login server listener mpLoginServer=new GwServer; if(mpLoginServer->Init(lsIp, lsPort, lsMaxPlayers) != 0) { mSysLog.Write("[!] Could not initialize the login network server.\n"); return -4; } mpLoginServer->RegisterCallbacks(LsClientConnected, LsClientDisconnected, CustomNetworkProcessing); mSysLog.Write("[+] Login network server (%s:%u) started.\n", lsIp, lsPort); //Populate the login server opcode table if(PopulateLsOpcodeTable() != 0) { mSysLog.Write("[!] Unable to populate login server opcodes table.\n"); return -7; } mSysLog.Write("[+] Successfully populated the login server opcode table.\n"); //Start the internal server listener mpInternalServer=new GwServer; if(mpInternalServer->Init(lsIp, isPort, mMaxGameServers) != 0) { mSysLog.Write("[!] Could not initialize the internal network server.\n"); return -5; } mpInternalServer->RegisterCallbacks(NULL, NULL, NULL); mSysLog.Write("[+] Internal network server (%s:%u) started.\n", lsIp, isPort); //Connect to the database mDatabase.Init(); mSysLog.Write(" Attempting connections to the database server...\n"); bool failed=true; for(int i=0; i