// *************************************************************** // Function: Process.cpp // *************************************************************** #include "Main.h" #include "LocalData.h" hash_map gLoginInfo; void ProcessCreateCharacter(NetMessage* pData, LOCAL_DATA* pLir); void ProcessCharacterPlay(NetMessage* pData, LOCAL_DATA* pLir); void DispatchToGameServer(CompiledPacket* pPck, unsigned long loginAttempts, unsigned short zoneID, unsigned short port, unsigned char* pIp, unsigned long TransferId1, unsigned long TransferId2); void SendError(int errorCode, int numLogins, short networkId); #define ChopPacket(size) PacketChop(dest, &d, size) #define IS_ACTIVE_CLIENT if (BIT_TEST(lir->bitfield, LD_AUTHENTICATED)==false){if (true){NetMessage s;s.data=dest;s.dataSize=d.dataSize;s.ID=d.ID;gpLoginServer->AddIncoming(&s);}}else{ #define ISC_END } //************************************ // Name: PacketChop // // Cuts a packet out of the data. Leaving the remaining data to be processed. // // Parameters: // pDataArray - Pointer to the array of byte data // pData - Network data // size - Size of the packet. // // Notes: // o Cuts the leading [size] bytes off of the data. Packaging the remainder for processing. // // Return: // //************************************ void PacketChop(unsigned char* pDataArray, NetMessage* pData, int size) { assert(pDataArray!=NULL); if (size==-1){return;} if (size==pData->dataSize){return;} NetMessage s; s.pData=pDataArray+size; s.dataSize=pData->dataSize-size; s.networkId=pData->networkId; gpLoginServer->AddIncomingFront(&s); } //************************************ // Name: ProcessPackets // // Handles all incoming client packets. Calls registered functions for handling opcodes. // // Parameters: // // Return: // zero //************************************ int ProcessPackets() { //Create a data pack and go through and get all available messages from the server. NetMessage pckData; while (gpLoginServer->GetNextNetMessage(&pckData)==0) { assert(pckData.networkId>0); switch(pckData.dataSize) { case CLIENT_DISCONNECTED: RevokeAuthentication(pckData.networkId); DeleteLocalInfoById(pckData.networkId); break; case CLIENT_CONNECTED: if (true) { LOCAL_DATA local_data; memset(&local_data, 0, sizeof(LOCAL_DATA)); local_data.networkId=pckData.networkId; local_data.bitfield=0; typedef pair lpair; gLoginInfo.insert(lpair(pckData.networkId, local_data)); } break; default: //Make sure a login info exists for this id LOCAL_DATA* pLir=AccessLocalData(pckData.networkId); if (pLir!=NULL && pckData.dataSize>=2) { unsigned short opcode=Read_uShort(pckData.pData); hash_map::iterator itr; //Check the file server hash map if (!BIT_TEST(pLir->bitfield, LD_ESTABLISHED_ENCRYPTED_CONNECTION) && (itr=gFileServerOpcodes.find(opcode))!=gFileServerOpcodes.end()) { //No handler for this opcode if (itr->second.handler==NULL) { gCoreLog.warning("No handler!"); gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); gpLoginServer->Kick(pckData.networkId); } else { gCoreLog.debug("File Server Pck: %s", itr->second.name); int pckSize=itr->second.handler(&pckData, pLir); PacketChop(pckData.pData, &pckData, pckSize); } } else { //Check the login server hash map itr=gClientOpcodesTable.find(opcode); if (itr!=gClientOpcodesTable.end()) { //No handler for this opcode if (itr->second.handler==NULL) { gCoreLog.debug("No handler found for %s", itr->second.name); gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); } else { gCoreLog.debug("Login Server Pck: %s", itr->second.name); //gCoreLog.LogUnknownPacket(pckData.pData,pckData.dataSize); int packet_size=itr->second.handler(&pckData, pLir); PacketChop(pckData.pData, &pckData, packet_size); } } //Unknown packet else { if (pckData.pData!=NULL) { if (BIT_TEST(pLir->bitfield,LD_UPDATE_CONNECTION)) { if (false) { gCoreLog.debug("--FILE SERVER CONNECTION--"); gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); } } else { gCoreLog.LogUnknownPacket(pckData.pData, pckData.dataSize); //gpLoginServer->Kick(pckData.networkId); } } } } } } //free the memory used by the NetMessage. Deletes the original data. if (pckData.pData!=NULL) { delete [] pckData.pData; } } return 0; } //************************************ // Name: HandleOpcode_PLAYCREATE_CHARACTER // // Handles the packet that requests either character creation or play. If the request cannot be fulfilled error 24 is sent to the client. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_PLAYCREATE_CHARACTER(NetMessage* pData, LOCAL_DATA* pLir) { pLir->loginAttempts=Read_uLong(pData->pData+2); //Character creation if (pData->pData[6]==0x0b) { if (!BIT_TEST(pLir->bitfield, LD_AUTHENTICATED)) { SendError(24, pLir->loginAttempts, pLir->networkId); } else { ProcessCreateCharacter(pData, pLir); } } //Character play else { if (!BIT_TEST(pLir->bitfield, LD_AUTHENTICATED)) { SendError(24, pLir->loginAttempts, pLir->networkId); } else { ProcessCharacterPlay(pData, pLir); } } return pData->dataSize; } //************************************ // Name: HandleOpcode_ACCOUNT_LOGOUT // // Handles logging out of an account. Revokes account authentication. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_ACCOUNT_LOGOUT(NetMessage* pData, LOCAL_DATA* pLir) { RevokeAuthentication(pData->networkId); return 6; } int HandleOpcode_LS_REQUEST_RESPONSE(NetMessage* pData, LOCAL_DATA* pLir) { pLir->loginAttempts=Read_uLong(pData->pData+2); CompiledPacket pck; pck.AddShort(ServerOpcode(13)); pck.AddLong(pLir->loginAttempts); pck.AddLong(1); pck.AddShort(ServerOpcode(11)); pck.AddLong(pLir->loginAttempts); pck.AddLong(0); Util::SendCompiledPacket(&pck, pLir->networkId, gpLoginServer); return 6; } //************************************ // Name: HandleOpcode_KEEP_ALIVE // // Handles the keep alive packet sent periodially from the client. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_KEEP_ALIVE(NetMessage* pData, LOCAL_DATA* pLir) { pLir->keepAlive=Read_uLong(pData->pData+2); return 6; } //************************************ // Name: HandleOpcode_LS_COMPUTER_HARDWARE // // Does nothing with the packet except calculate the size. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_LS_COMPUTER_HARDWARE(NetMessage* pData, LOCAL_DATA* pLir) { unsigned short data_size=Read_uShort(pData->pData+2); return data_size+20; } //************************************ // Name: HandleOpcode_EXIT_GAME // // Handles the client's info informing the server that the client is either in or out of instance. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_EXIT_GAME(NetMessage* pData, LOCAL_DATA* pLir) { if (Read_uLong(pData->pData+2)==1) { //The client is trying to go to an instance } else { //The client is no longer in an instance } return 6; } //************************************ // Name: HandleOpcode_LAST_CHARACTER_INFO // // Handles the client's information informing the server of the last character played and that character's appearance data. // // Parameters: // pData - Network data // pLir - Player's data // // Return: // Size of packet //************************************ int HandleOpcode_LAST_CHARACTER_INFO(NetMessage* pData, LOCAL_DATA* pLir) { //Read the login attempts pLir->loginAttempts=Read_uLong(pData->pData+2); //Read the last character name unsigned char character_name[250]; memset(character_name, 0, 250); int name_length=Read_uShort(pData->pData+6)*2; if (name_length>240) { gpLoginServer->Kick(pData->networkId); return 0; } memcpy(character_name, pData->pData+6+2, name_length); Util::StripUnicode(character_name); //Output the name we got gCoreLog.debug("Last Character: %s", character_name); CompiledPacket pck; pck.AddShort(ServerOpcode(11)); pck.AddLong(pLir->loginAttempts); pck.AddLong(0); Util::SendCompiledPacket(&pck, pData->networkId, gpLoginServer); gCoreLog.LogKnownData("sdf", pData->pData, pData->dataSize); return Util::Limit(72+name_length, pData->dataSize); }