// *************************************************************** // Function: ICProcess.cpp // *************************************************************** #include "Main.h" #include "../../src/IC.h" #include "AdminConsole.h" #include "../../src/SharedStructures.h" #include "Config.h" vector gLeftOverPackets; hash_map gAdmins; bool IsIDRegisteredGameSrv(long NetworkID); bool UnregisterGameSrv(long NetworkID); void ProcessGSCreateCharacter(NetMessage* pData); void GSActiveClientRequest(NetMessage* pData); void ProcessGSForwardData(NetMessage* pData); void ProcessGSRecievedWhisper(NetMessage* pData); void ProcessICConnect(NetMessage* pData); void VerifyConnectedGS(NetMessage* pData); void ProcessGSPDNotice(NetMessage* pData); void ProcessACGSResponse(NetMessage* pData); void ProcessMapChangeRequest(NetMessage* pData); /* #pragma pack(push, 1) #pragma pack(pop) ###--Packet headers and their meanings--### Note: This is only for packets sent from within this function. See the client game server cpp file for the received packet header meanings. ID# Size Meaning ----- ------ ----------------------------------------------------------------------------------------------------- 0x01 0 Ask the client for their password. It begins the authorization and registration process. 0x02 0 Response from me that the password is correct and the server has been registered 0x03 0 Response from me that the password is wrong and the client will be disconnected. 0x04 8 Response: No active client exists with this IP 0x05 12 Response: Active client exists with this IP here is the account id 0x06 5 Response: Character created successfully 0x07 18 Response: Active client exists with this IP. Here is the instance info and account id. 0x08 X Relay the command line to the game server for processing. 0x09 X Performs an action on a player on the gameserver. [action][accountid] */ //************************************ // Name: InternalCommunicationsProcess // // Processes all incoming internal communications server messages. // // Parameters: // // Return: // //************************************ void InternalCommunicationsProcess() { //Cycle through all the packets on queue NetMessage pckData; while (gpInternalComm->GetNextNetMessage(&pckData)==0) { assert(pckData.networkId!=NULL); switch(pckData.dataSize) { //Check if we have received a new connection notify packet case CLIENT_CONNECTED: ProcessICConnect(&pckData); break; //If this is a delete local client data message. (The client connection has been lost) case CLIENT_DISCONNECTED: DeleteLeftOverById(pckData.networkId); UnregisterGameSrv(pckData.networkId); //Try and delete the admin console thing if one exists with the id if (true) { hash_map::iterator itr=gAdmins.find(pckData.networkId); if (itr!=gAdmins.end()) { if (itr->second.authenticated) { gCoreLog.notice("Admin %s logged out of admin console.", itr->second.accName); } gAdmins.erase(pckData.networkId); } } break; //We have received a packet that is at least 2 bytes in length default: assert(pckData.pData!=NULL); //Verify the header and the received packet size switch(CheckHeaderAndSize(&pckData)) { //First check to see if the received packet size is //smaller than what is specified in the header. case HOLDING_LOWSIZE: #ifdef LOG_DEBUG_MESSAGES_ ProcessLowSize(&pckData, &gLeftOverPackets, &gCoreLog); #else ProcessLowSize(&pckData, &gLeftOverPackets, NULL); #endif break; //We have received more data than the packet contains. I.E. We have received 1.x packets. Split the data //into two packets and store one as incomplete. Does not need a break because it generates a proper packet. case HOLDING_HIGHSIZE: #ifdef LOG_DEBUG_MESSAGES_ ProcessHighSize(&pckData, &gLeftOverPackets, &gCoreLog); #else ProcessHighSize(&pckData, &gLeftOverPackets, NULL); #endif //There are no problems with the data we have received. case NOT_HOLDING: //-------------------------------------------------------------- //Check to see if the client is registered as a game server. //If it is then all IC commands are open to processing //New processing belongs within this. //-------------------------------------------------------------- if (IsIDRegisteredGameSrv(pckData.networkId)) { switch(pckData.pData[0]) { //If the game server is asking if a certain IP address is on the active clients list. case GS_IS_X_AUTHENTICATED: GSActiveClientRequest(&pckData); break; //We have received character creation info from a game server make a new character =) case GS_CREATE_CHARACTER: ProcessGSCreateCharacter(&pckData); break; case GS_UPDATE_PLAYER_COUNT: gGameServers[pckData.networkId].currentPlayers=Read_uShort(pckData.pData+2); break; case GS_ADMIN_CONSOLE_RESPONSE: ProcessACGSResponse(&pckData); break; case GS_PLAYER_DISCONNECTED: ProcessGSPDNotice(&pckData); break; case GS_REQUEST_MAP_CHANGE: ProcessMapChangeRequest(&pckData); break; case GS_KICK_PLAYER: gpLoginServer->Kick(Read_uShort(pckData.pData+2)); break; // client wants to logout case GS_LOGOUT: if (true) { int id; memcpy(&id, &pckData.pData[2], 4); hash_map::iterator itr; for (itr=gLoginInfo.begin(); itr!=gLoginInfo.end(); itr++) { if (BIT_TEST(itr->second.bitfield, LD_AUTHENTICATED) && (itr)->second.accountId==id) { itr->second.gameServerId=0; break; } } } break; //Output any unknown packets default: gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); } } ////////////////////////////////////////////////////////////////////////// //Check for the admin console login packet! ////////////////////////////////////////////////////////////////////////// else if (pckData.pData[0]==AC_LOGIN) { //Add this client to the admins list AdminList al; memset(&al, 0, sizeof(AdminList)); al.authenticated=false; al.networkId=pckData.networkId; //Read in the data memcpy(al.accName, pckData.pData+2, pckData.pData[1]-32); memcpy(al.passHash, pckData.pData+2+(pckData.pData[1]-32), 32); //Generate the query that gets the login info from the db char query[100]; sprintf(query, "SELECT Password, AccessLevel, AccountID FROM accounts WHERE email=\"%s\" LIMIT 1", al.accName); al.queryId=gDatabase.Query(query, (long)strlen(query)); al.QueryProcess=AuthorizeAdminCon; al.forwardToGs=0; typedef pair apair; gAdmins.insert(apair(pckData.networkId, al)); } //---------------------------------------------- //Check if we have received a password reply //We will trust the client fully if it gives //me the correct password. This cuts down on //code. //---------------------------------------------- else if (pckData.pData[0]==GS_REQUEST_AUTHORIZATION && pckData.pData[1]==38) { VerifyConnectedGS(&pckData); } //Check for the command packets from the admin console else if (pckData.pData[0]==AC_COMMAND && gAdmins.find(pckData.networkId)!=gAdmins.end()) { hash_map::iterator itr=gAdmins.find(pckData.networkId); ProcessACCommand(&pckData, &itr->second); } //We have received an authorization request from a client else if (pckData.pData[0]==AUTHORIZE) { //Make sure the payload is the correct size if (pckData.dataSize-2==pckData.pData[1]) { if (pckData.dataSize==100) { //Extract and convert the current comp hash unsigned char comp_hash[32]; memcpy(comp_hash, pckData.pData+2, 32); char comp_hash_hex[70]; Util::BinToHex(comp_hash, 32, comp_hash_hex); time_t t=time(NULL); //Compile the remaining data unsigned char buffer[70]; memcpy(buffer, &t, 4); memcpy(buffer+4, pckData.pData+32+2, 66); //Encrypt the remaining data with the compHash for (int i=0; i<70; i++) { buffer[i]=buffer[i]^comp_hash[i%32]; } //Prepare the database query char buffer_hex[(70*2)+5]; Util::BinToHex(buffer, 70, buffer_hex); char query[1000]; sprintf(query, "UPDATE accounts SET Authorization=%s WHERE CompHash=%s@NOSTATUS", buffer_hex, comp_hash_hex); gDatabase.Query(query, (long)strlen(query)); } } } //--------------------------------------------------------- //We have received an invalid packet. Close the connection. //--------------------------------------------------------- else { //tell the server to close the client connection in a random //amount of time. NetMessage m(rand()%10000, 1, pckData.networkId, 0); gpInternalComm->SendControl(&m); gCoreLog.LOCATION; gCoreLog.warning("Bad ICS packet received!"); } } } //---------------------------------------------------------------- //free the memory used by the NetMessage //---------------------------------------------------------------- if (pckData.pData!=0){delete [] pckData.pData;} RepopulatePacketQueue(gpInternalComm, &gLeftOverPackets); } //Check the admin console database queries gDatabase.ProcessResults, AdminList>(&gAdmins); } //************************************ // Name: ICC_CleanUp // // Cleans up stuff allocated for the internal communications data processing. // // Parameters: // // Return: // //************************************ void Icc_CleanUp() { vector::iterator itr; for (itr=gLeftOverPackets.begin(); itr!=gLeftOverPackets.end(); itr++) { if (itr->pData!=0) delete [] itr->pData; } if (gLeftOverPackets.size()>0){gLeftOverPackets.clear();} } //************************************ // Name: SendLSPlayerAction // // Tells the game server to perform [action] on any players with [AccountID] // // Parameters: // gameServerId - The game server id. (Game server's network id with the ICS) // accountId - Player's account id. // action - What action to perform on the player. // // Return: // //************************************ void SendLSPlayerAction(short gameServerId, unsigned long accountId, unsigned long action) { unsigned char buf[10]; STRUCT_LS_PLAYER_ACTION ls; ls.accountId=accountId; ls.action=action; NetMessage s; s.dataSize=PreparePacket(LS_PLAYER_ACTION, &ls, sizeof(STRUCT_LS_PLAYER_ACTION), buf); s.pData=buf; s.networkId=gameServerId; gpInternalComm->SendData(&s); } //************************************ // Name: DeleteLeftOverByID // // Removes any partial packets from the processor that belong to [networkId] // // Parameters: // networkId - Client's network id // // Return: // //************************************ void DeleteLeftOverById(long networkId) { assert(networkId>0); checkagain: vector::iterator itr; for (itr=gLeftOverPackets.begin(); itr!=gLeftOverPackets.end(); itr++) { if (itr->networkId==networkId) { assert(itr->networkId>0); if (itr->pData!=NULL) delete [] itr->pData; gLeftOverPackets.erase(itr); goto checkagain; } } }