#include "GWServer.h" #include GwServer::GwServer(){} //Only call this once per program. int GwServer::StartSubsystem() { if(!nlInit()) { return -1; } if(!nlSelectNetwork(NL_IP)) { return -2; } return 0; } int GwServer::CloseSubsystem() { nlShutdown(); return 0; } int GwServer::Init(char* pIpString, ushort port, ulong maxClients) { //Set up a socket for listening for connections this->mListenPort=port; if(pIpString != NULL) { NLaddress na; nlStringToAddr((const NLchar*)pIpString, &na); nlSetLocalAddr(&na); } if((this->mListenSocket=nlOpen(this->mListenPort, NL_RELIABLE)) == NL_INVALID) { return -1; } if(!nlListen(this->mListenSocket)) { return -2; } this->mClientsGroupId=nlGroupCreate(); this->mMaxClients=maxClients; this->mpPacketProcessing=NULL; return 0; } int GwServer::Close() { STDEXT::hash_map::iterator itr; for(itr=mClients.begin(); itr!=mClients.end(); itr++) { itr->second.inBuffer.clear(); itr->second.outBuffer.clear(); nlClose(itr->second.mySocket); } nlClose(this->mListenSocket); nlGroupDestroy(this->mClientsGroupId); mClients.clear(); mOpcodesTable.clear(); return 0; } int GwServer::Update() { //Accept new connections if(this->mClients.size() < (unsigned int)mMaxClients) { NLsocket newSocket=nlAcceptConnection(this->mListenSocket); if(newSocket != NL_INVALID) { nlGroupAddSocket(this->mClientsGroupId, newSocket); GwClient client; client.mySocket=newSocket; client.externalId=0; client.inBuffer.clear(); client.outBuffer.clear(); client.usingEncryption=false; typedef std::pair cpair; this->mClients.insert(cpair(client.mySocket, client)); if(this->mpClientConnected != NULL) { this->mpClientConnected(AccessGwClient(client.mySocket)); } printf("Client connected\n"); } } //Check for read and writable sockets NLsocket readActiveSockets[MAX_SOCKETS]; NLsocket writeActiveSockets[MAX_SOCKETS]; int numReadSockets=nlPollGroup(this->mClientsGroupId, NL_READ_STATUS, readActiveSockets, MAX_SOCKETS, 0); int numWriteSockets=nlPollGroup(this->mClientsGroupId, NL_WRITE_STATUS, writeActiveSockets, MAX_SOCKETS, 0); //Read for readable sockets for(int i=0; i 0) { if(pClient->usingEncryption) { Crypto(buffer, buffer, numBytes, &pClient->encryptionState); } pClient->PushIn(buffer, numBytes); } else { if(this->mpClientDisconnected != NULL) { this->mpClientDisconnected(pClient); } Kick(pClient->mySocket); printf((const char*)nlGetErrorStr(nlGetError())); } } } //Write to writable sockets for(int i=0; ioutBuffer.size() > 0) { int numBytesToWrite=pClient->outBuffer.size(); if(pClient->usingEncryption) { if(pClient->eByteCounter > 0) { numBytesToWrite=pClient->eByteCounter; } else { Crypto(&pClient->outBuffer.at(0), &pClient->outBuffer.at(0), pClient->outBuffer.size(), &pClient->encryptionState); } } int numBytes=nlWrite(writeActiveSockets[i], &pClient->outBuffer.at(0), numBytesToWrite); if(numBytes > 0 && numBytes != NL_INVALID) { for(int j=0; joutBuffer.erase(pClient->outBuffer.begin()); } if(pClient->eByteCounter > 0) { pClient->eByteCounter-=numBytes; } } } } //Process a packet for each client STDEXT::hash_map::iterator itr; for(itr=mClients.begin(); itr!=mClients.end(); itr++) { GwClient* pClient=&itr->second; bool notProcessed=true; if(pClient->inBuffer.size() > 0) { int numBytesProcessed=0; //Check for custom processing if(this->mpPacketProcessing != NULL) { int retValue=0; if((retValue=mpPacketProcessing(&pClient->inBuffer.at(0), pClient->inBuffer.size(), pClient)) > 0) { notProcessed=false; numBytesProcessed=retValue; } } //Check for normal registered opcode processing if(notProcessed) { ushort opcode=0; memcpy(&opcode, &pClient->inBuffer.at(0), 2); STDEXT::hash_map::iterator oitr; oitr=mOpcodesTable.find(opcode); if (oitr!=mOpcodesTable.end()) { OPCODE* pOpcode=&oitr->second; //No handler for this opcode if (pOpcode->handler==NULL) { printf("No handler found for %s\n", pOpcode->name); numBytesProcessed=pClient->inBuffer.size(); } else { printf("Login Server Pck: %s\n", pOpcode->name); Packet p; p.Init(&pClient->inBuffer.at(0), pClient->inBuffer.size()); numBytesProcessed=pOpcode->handler(&p, pClient); } } //Unknown packet else { printf("No packet found!\n"); numBytesProcessed=pClient->inBuffer.size(); } } //Remove the proper amount of bytes from the buffer if(numBytesProcessed > 0) { if((unsigned int)numBytesProcessed > pClient->inBuffer.size()) { numBytesProcessed=pClient->inBuffer.size(); } //printf("Removed %u bytes\n", numBytesProcessed); for(int j=0; jinBuffer.erase(pClient->inBuffer.begin()); } } } } return 0; } GwClient* GwServer::AccessGwClient(NLsocket ns) { STDEXT::hash_map::iterator itr=mClients.find(ns); if(itr != mClients.end()) { return &itr->second; } return NULL; } void GwServer::RegisterCallbacks(int (*pCon)(GwClient* pClient), int (*pDisCon)(GwClient* pClient), int (*pPacketProcessing)(uchar*, ulong, GwClient*)) { this->mpClientConnected=pCon; this->mpClientDisconnected=pDisCon; this->mpPacketProcessing=pPacketProcessing; } int GwServer::Crypto(unsigned char* pSourceData, unsigned char* pDestBuffer, int length, ENCRYPTION_STATE* pEncryptionState) { unsigned char *pKey=pEncryptionState->key; //start decrypting //e->offset1;//esi //e->offset2;//edi pEncryptionState->offset1=0; pEncryptionState->offset2=0; unsigned int dl=0; for (int i=0; ioffset1++; pEncryptionState->offset1 = pEncryptionState->offset1 & 0x0FF; dl=pKey[0+pEncryptionState->offset1+8]; dl+=pEncryptionState->offset2; dl=dl & 0x0FF; pEncryptionState->offset2=dl; dl=pKey[0+pEncryptionState->offset1+8]; unsigned int bl=pKey[0+pEncryptionState->offset2+8]; //key[0+offset1+8]=bl; //No longer used. //key[0+offset2+8]=dl; bl=pKey[0+pEncryptionState->offset1+8]; dl=dl+bl; dl=dl & 0x0FF; dl=pKey[0+dl+8]; dl=dl ^ pSourceData[i]; pDestBuffer[i]=dl; } return 0; } void GwServer::RegisterOpcode(char* pName, ushort opcode, int (*pHandler)(Packet* pck, GwClient* pClient)) { OPCODE o; strncpy(o.name, pName, 100); o.opcode=opcode; o.handler=pHandler; typedef std::pair opair; mOpcodesTable.insert(opair(o.opcode, o)); } void GwServer::Send(NLsocket socket, uchar* pData, ulong dataSize) { GwClient* pClient=AccessGwClient(socket); if(pClient != NULL) { pClient->PushOut(pData, dataSize); } } void GwServer::Kick(NLsocket socket) { nlGroupDeleteSocket(this->mClientsGroupId, socket); nlClose(socket); mClients.erase(socket); } //----------------Packet-----------------// void Packet::Init(uchar* pBuffer, ulong dataSize) { this->mpBuffer=pBuffer; this->mBufPosition=2; this->mBufferSize=dataSize; } uchar* Packet::GetData() { return this->mpBuffer; } uchar Packet::ReadByte() { uchar temp=0; temp=this->mpBuffer[this->mBufPosition]; this->mBufPosition++; return temp; } ushort Packet::ReadShort() { ushort temp=0; memcpy(&temp, this->mpBuffer+this->mBufPosition, 2); this->mBufPosition+=2; return temp; } ulong Packet::ReadLong() { ulong temp=0; memcpy(&temp, this->mpBuffer+this->mBufPosition, 4); this->mBufPosition+=4; return temp; } float Packet::ReadFloat() { float temp=0; memcpy(&temp, this->mpBuffer+this->mBufPosition, 4); this->mBufPosition+=4; return temp; } void Packet::Skip(int numBytes) { this->mBufPosition+=numBytes; } void Packet::Read(void* pDestBuffer, int size) { memcpy(pDestBuffer, this->mpBuffer+this->mBufPosition, size); this->mBufPosition+=size; } ulong Packet::PacketSize() { return this->mBufPosition; } void Packet::SetBufferPosition(ulong pos) { this->mBufPosition=pos; }