MCommand* MCommandCommunicator::MakeCmdFromSaneTunnelingBlob(const MUID& Sender, const MUID& Receiver, const void* pBlob, size_t Size) { MCommand* pCmd = new MCommand(); if (!pCmd->SetData((char*)pBlob, &m_CommandManager, (u16)Size)) { delete pCmd; return nullptr; } pCmd->m_Sender = Sender; pCmd->m_Receiver = Receiver; return pCmd; }
MCommand* MMatchClient::MakeCmdFromTunnelingBlob(const MUID& uidSender, void* pBlob, int nBlobArrayCount) { if (nBlobArrayCount != 1) { mlog("MakeCmdFromTunnelingBlob: BlobArrayCount is not 1\n"); return NULL; } char* pPacket = (char*)MGetBlobArrayElement(pBlob, 0); int nSize = MGetBlobArraySize(pBlob) - (sizeof(int) * 2); if ((nSize <= 0) || (nSize >= MAX_BLOB_SIZE)) { mlog("MakeCmdFromTunnelingBlob: Blob Size Error(size = %d)\n", nSize); return NULL; } char* pData = new char[nSize]; if (!m_PeerPacketCrypter.Decrypt(pPacket, nSize, pData, nSize)) { delete [] pData; return NULL; } MCommand* pCmd = new MCommand(); if (!pCmd->SetData(pData, &m_CommandManager)) { delete [] pData; delete pCmd; return NULL; } delete [] pData; pCmd->m_Sender = uidSender; pCmd->m_Receiver = m_This; MMatchPeerInfo* pPeer = FindPeer(uidSender); if (pPeer == NULL) { delete pCmd; return NULL; } return pCmd; }
int MCommandBuilder::MakeCommand(char* pBuffer, int nBufferLen) { unsigned int nOffset = 0; int nLen = nBufferLen; MPacketHeader* pPacket = (MPacketHeader*)(pBuffer+nOffset); int nCmdCount = 0; int nPacketSize = 0; while (nLen >= sizeof(MPacketHeader)) { int nPacketSize = _CalcPacketSize(pPacket); if ((nPacketSize > nLen) || (nPacketSize <= 0)) break; if (pPacket->nMsg == MSGID_RAWCOMMAND) { unsigned short nCheckSum = MBuildCheckSum(pPacket, nPacketSize); if (pPacket->nCheckSum != nCheckSum) { return -1; } else if (nPacketSize > MAX_PACKET_SIZE) { return -1; } else { MCommand* pCmd = new MCommand(); int nCmdSize = nPacketSize - sizeof(MPacketHeader); if (pCmd->SetData(((MCommandMsg*)pPacket)->Buffer, m_pCommandManager, (unsigned short)nCmdSize)) { if (m_bCheckCommandSN) { if (!m_CommandSNChecker.CheckValidate(pCmd->m_nSerialNumber)) { delete pCmd; pCmd = NULL; return -1; } } pCmd->m_Sender = m_uidSender; pCmd->m_Receiver = m_uidReceiver; m_CommandList.push_back(pCmd); } else { delete pCmd; pCmd = NULL; return -1; } } } else if (pPacket->nMsg == MSGID_COMMAND) { unsigned short nCheckSum = MBuildCheckSum(pPacket, nPacketSize); if (pPacket->nCheckSum != nCheckSum) { return -1; } else if (nPacketSize > MAX_PACKET_SIZE) { return -1; } else { MCommand* pCmd = new MCommand(); int nCmdSize = nPacketSize - sizeof(MPacketHeader); if (m_pPacketCrypter) { if (!m_pPacketCrypter->Decrypt((char*)((MCommandMsg*)pPacket)->Buffer, nCmdSize)) { delete pCmd; pCmd = NULL; return -1; } } if (pCmd->SetData((char*)((MCommandMsg*)pPacket)->Buffer, m_pCommandManager, (unsigned short)nCmdSize)) { if (m_bCheckCommandSN) { if (!m_CommandSNChecker.CheckValidate(pCmd->m_nSerialNumber)) { delete pCmd; pCmd = NULL; return -1; } } pCmd->m_Sender = m_uidSender; pCmd->m_Receiver = m_uidReceiver; m_CommandList.push_back(pCmd); } else { delete pCmd; pCmd = NULL; return -1; } } } else if (pPacket->nMsg == MSGID_REPLYCONNECT) { if (nPacketSize == sizeof(MReplyConnectMsg)) { MPacketHeader* pNewPacket = (MPacketHeader*)malloc(nPacketSize); memcpy(pNewPacket, pPacket, nPacketSize); m_NetCmdList.push_back(pNewPacket); } else { return -1; } } else { return -1; } nOffset += nPacketSize; nLen -= nPacketSize; nCmdCount++; pPacket = (MPacketHeader*)(pBuffer+nOffset); } return nLen; }
void MMatchClient::ParseUDPPacket(char* pData, MPacketHeader* pPacketHeader, DWORD dwIP, unsigned int nPort) { switch (pPacketHeader->nMsg) { case MSGID_RAWCOMMAND: { unsigned short nCheckSum = MBuildCheckSum(pPacketHeader, pPacketHeader->nSize); if (pPacketHeader->nCheckSum != nCheckSum) { static int nLogCount = 0; if (nLogCount++ < 100) { // Log Flooding 방지 mlog("MMatchClient::ParseUDPPacket() -> CHECKSUM ERROR(R=%u/C=%u)\n", pPacketHeader->nCheckSum, nCheckSum); } return; } else { MCommand* pCmd = new MCommand(); if (!pCmd->SetData(pData, &m_CommandManager)) { mlog("MMatchClient::ParseUDPPacket() -> SetData Error\n"); delete pCmd; return; } MUID uidPeer = FindPeerUID(dwIP, nPort); if (uidPeer != MUID(0,0)) { pCmd->m_Sender = uidPeer; } else { // TODO: 여기 수정해야함. sockaddr_in Addr; Addr.sin_addr.S_un.S_addr = dwIP; Addr.sin_port = nPort; char* pszIP = inet_ntoa(Addr.sin_addr); if (strcmp(pszIP, GetAgentIP()) == 0) { pCmd->m_Sender = GetAgentServerUID(); } else if( (MC_RESPONSE_SERVER_LIST_INFO == pCmd->GetID()) || (MC_RESPONSE_BLOCK_COUNTRY_CODE_IP == pCmd->GetID()) ) { // 특별히 하는건 없음. // Lcator는 Peer설정이 되지 않기때문에 여기서 따로 처리함. } else if (pCmd->GetID() == MC_UDP_PONG) { // 특별히 하는건 없음. Command를 넘겨주기 위해서...(밑에 커맨드를 딜리트하기때문에) } else { delete pCmd; pCmd = NULL; return; } } pCmd->m_Receiver = m_This; if( IsUDPCommandValidationCheck(pCmd->GetID()) ) { LockRecv(); m_CommandManager.Post(pCmd); UnlockRecv(); } else { #ifdef _DEBUG mlog("%s(ID:%d) is Denied Command!\n" , pCmd->m_pCommandDesc->GetName(), pCmd->GetID()); #endif } } } break; case MSGID_COMMAND: { int nPacketSize = pPacketHeader->CalcPacketSize(&m_PeerPacketCrypter); unsigned short nCheckSum = MBuildCheckSum(pPacketHeader, nPacketSize); if (pPacketHeader->nCheckSum != nCheckSum) { static int nLogCount = 0; if (nLogCount++ < 100) { // Log Flooding 방지 mlog("MMatchClient::ParseUDPPacket() -> CHECKSUM ERROR(R=%u/C=%u)\n", pPacketHeader->nCheckSum, nCheckSum); } return; } else { MCommand* pCmd = new MCommand(); int nCmdSize = nPacketSize - sizeof(MPacketHeader); if (!m_PeerPacketCrypter.Decrypt(pData, nCmdSize)) { mlog("MMatchClient::ParseUDPPacket() -> Decrypt Error\n"); delete pCmd; pCmd = NULL; return; } if (!pCmd->SetData(pData, &m_CommandManager)) { // TODO: 여기 수정해야함. sockaddr_in Addr; Addr.sin_addr.S_un.S_addr = dwIP; Addr.sin_port = nPort; char* pszIP = inet_ntoa(Addr.sin_addr); mlog("MMatchClient::ParseUDPPacket() -> MSGID_COMMAND SetData Error(%s:%d), size=%d\n", pszIP, nPort, nCmdSize); delete pCmd; pCmd = NULL; return; } MUID uidPeer = FindPeerUID(dwIP, nPort); if (uidPeer != MUID(0,0)) { pCmd->m_Sender = uidPeer; } else { // Agent와는 암호화된 커맨드는 사용하지 않는다. delete pCmd; return; /* // TODO: 여기 수정해야함. sockaddr_in Addr; Addr.sin_addr.S_un.S_addr = dwIP; Addr.sin_port = nPort; char* pszIP = inet_ntoa(Addr.sin_addr); if (strcmp(pszIP, GetAgentIP()) == 0) { pCmd->m_Sender = GetAgentServerUID(); }else { delete pCmd; pCmd = NULL; return; } */ } pCmd->m_Receiver = m_This; if( IsUDPCommandValidationCheck(pCmd->GetID()) ) { LockRecv(); m_CommandManager.Post(pCmd); UnlockRecv(); } else { #ifdef _DEBUG mlog("%s(ID:%d) is Denied Command!\n" , pCmd->m_pCommandDesc->GetName(), pCmd->GetID()); #endif } } } break; default: { Log("MatchClient: Parse Packet Error"); } break; } }