void StagingLogic::SendReadyRequest(bool isReady) { WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_ReadyRequest); aMsg.AppendBool(isReady); SendGameMessageToCaptain(aMsg.ToByteBuffer()); }
void AddServiceOp::SetNetAddrPort(unsigned short thePort) { WriteBuffer theBuf; theBuf.AppendShort( htons(thePort) ); SetNetAddr(theBuf.ToByteBuffer()); }
ByteBufferPtr LobbyGame::GetJoinGameRequest() { WriteBuffer aBuf; aBuf.AppendByte(LobbyGameMsg_JoinRequest); aBuf.AppendShort(mPing); GetJoinGameRequestHook(aBuf); return aBuf.ToByteBuffer(); }
WONStatus AuthSession::Decrypt(ByteBufferPtr &theMsg) { mLastUseTime = time(NULL); try { if(mAuthType==AUTH_TYPE_NONE || mAuthType==AUTH_TYPE_PERSISTENT_NOCRYPT) return WS_Success; ReadBuffer anIn(theMsg->data(),theMsg->length()); WriteBuffer anOut; unsigned char headerType = anIn.ReadByte(); switch (headerType) { case 2: break; //WONMsg::EncryptedService: case 4: anOut.AppendByte(3); break; //WONMsg::MiniEncryptedService: case 6: anOut.AppendByte(5); break; //WONMsg::SmallEncryptedService: case 8: anOut.AppendByte(7); break; //WONMsg::LargeEncryptedService: case 12: break; //WONMsg::HeaderEncryptedService: default: return WS_Success; } bool sessioned = mAuthType==AUTH_TYPE_SESSION; if(sessioned) { unsigned short aSessionId = anIn.ReadShort(); if(aSessionId!=mId) return WS_AuthSession_DecryptSessionIdMismatch; } ByteBufferPtr aDecrypt = mKey.Decrypt(anIn.data() + anIn.pos(), anIn.length() - anIn.pos()); if(aDecrypt.get()==NULL) return WS_AuthSession_DecryptFailure; if(sessioned) { if(aDecrypt->length()<2) return WS_AuthSession_DecryptBadLen; if(++mInSeq!=ShortFromLittleEndian(*(unsigned short*)aDecrypt->data())) // sequence mismatch return WS_AuthSession_DecryptInvalidSequenceNum; anOut.AppendBytes(aDecrypt->data()+2,aDecrypt->length()-2); } else anOut.AppendBytes(aDecrypt->data(),aDecrypt->length()); theMsg = anOut.ToByteBuffer(); return WS_Success; } catch(ReadBufferException&) { return WS_AuthSession_DecryptUnpackFailure; } }
void StagingLogic::SendDissolveGame() { if(!IAmCaptain()) return; WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_DissolveGame); BroadcastGameMessage(aMsg.ToByteBuffer()); }
ByteBufferPtr LobbyGame::HandleJoinGameRequest(ReadBuffer &theMsg, LobbyClient *theClient) { WriteBuffer aBuf; aBuf.AppendByte(LobbyGameMsg_JoinReply); aBuf.AppendShort(0); // reserve space for status short aStatus = HandleJoinGameRequest(theMsg,theClient,aBuf); aBuf.SetShort(1,aStatus); return aBuf.ToByteBuffer(); }
ByteBufferPtr ElGamal::Sign(const void* theMessage, int theMessageLen) const { if(!IsPrivate()) return NULL; MD5Digest anMD5; anMD5.update(theMessage, theMessageLen); RawBuffer aDigest = anMD5.digest(); BigInteger M; if(!EncodeDigest(aDigest,M)) return NULL; BigInteger ab[2]; if(!BogusSign(M,ab)) return NULL; WriteBuffer aSignature; int qLen = q.byteLength(); RawBuffer a,b; ab[0].toBinary(a); ab[1].toBinary(b); if(a.length()==qLen) aSignature.AppendBytes(a.data(),a.length()); else if(a.length()>qLen) aSignature.AppendBytes(a.data()+a.length()-qLen,qLen); else { for(int i=a.length(); i<qLen; i++) aSignature.AppendByte(0); aSignature.AppendBytes(a.data(),a.length()); } if(b.length()==qLen) aSignature.AppendBytes(b.data(),b.length()); else if(b.length()>qLen) aSignature.AppendBytes(b.data()+b.length()-qLen,qLen); else { for(int i=b.length(); i<qLen; i++) aSignature.AppendByte(0); aSignature.AppendBytes(b.data(),b.length()); } return aSignature.ToByteBuffer(); }
ByteBufferPtr MultiPingOp::GetRequest(MultiPingStruct *theStruct) { theStruct->mPingId = rand(); theStruct->mStartPingTick = GetTickCount(); WriteBuffer aBuf; aBuf.AppendByte(3); aBuf.AppendByte(1); aBuf.AppendByte(5); aBuf.AppendLong(theStruct->mPingId); aBuf.AppendBool(false); return aBuf.ToByteBuffer(); }
void StagingLogic::NotifyPingChange(LobbyGame *theGame) { if(mGame.get()==NULL || IAmCaptain()) return; if(!theGame->IsSameGame(mGame)) return; WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_PingChangedRequest); aMsg.AppendShort(theGame->GetPing()); SendGameMessageToCaptain(aMsg.ToByteBuffer()); }
void StagingLogic::KickClient(LobbyClient *theClient, bool isBan) { if(theClient==NULL || mGame.get()==NULL || !IAmCaptain()) return; WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_ClientKicked); aMsg.AppendShort(theClient->GetClientId()); aMsg.AppendBool(isBan); BroadcastGameMessage(aMsg.ToByteBuffer()); LobbyStagingPrv::NetKickClient(theClient,isBan); }
void StagingLogic::HandleReadyRequest(ReadBuffer &theMsg, LobbyClient *theSender) { if(!IAmCaptain() || !theSender->IsPlayer()) return; bool isReady = theMsg.ReadBool(); if(theSender->IsPlayerReady()==isReady) // no change needed return; WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_PlayerReady); aMsg.AppendShort(theSender->GetClientId()); aMsg.AppendBool(isReady); BroadcastGameMessage(aMsg.ToByteBuffer()); }
void UpdateVersionOp::RunHook() { SetMessageType(DBProxyMsgType::DBProxyPatchServer); SetSubMessageType(mMsgType); // Pack the message data WriteBuffer requestData; requestData.AppendString(mProductName); mVersionData->WriteToBuffer(requestData, VersionData::WriteFlag_AppendDescripURL | VersionData::WriteFlag_AppendState); // Pack and call base class implementation SetProxyRequestData(requestData.ToByteBuffer()); DBProxyOp::RunHook(); }
void QueryBannedKeyOp::RunHook() { SetMessageType(DBProxyAccount); SetSubMessageType(MSGTYPE); mKeyList.clear(); // Stores the message data WriteBuffer requestData; requestData.AppendWString(mCommunityName); requestData.AppendString(mProductName); // Copy buffer into the class SetProxyRequestData(requestData.ToByteBuffer()); DBProxyOp::RunHook(); }
ByteBufferPtr WIMDecoder::EncodeToBuffer(RawImagePtr theImage) { if(theImage->GetType()!=RawImageType_32) return NULL; RawImage32 *anImage = (RawImage32*)theImage.get(); WriteBuffer aBuf; aBuf.AppendBytes("WIM",3); // file format identifier aBuf.AppendLong(32); // file subtype (32-bit raw) aBuf.AppendByte(anImage->GetDoTransparency()?1:0); // apply transparency bit? aBuf.AppendLong(anImage->GetWidth()); aBuf.AppendLong(anImage->GetHeight()); aBuf.AppendBytes(anImage->GetImageData(), 4*anImage->GetWidth()*anImage->GetHeight()); return aBuf.ToByteBuffer(); }
void StagingLogic::HandlePingChangedRequest(ReadBuffer &theMsg, LobbyClient *theSender) { if(!IAmCaptain()) return; unsigned short aPing = theMsg.ReadShort(); LobbyPlayer *aPlayer = theSender->GetPlayer(); if(aPlayer==NULL) return; WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_PingChanged); aMsg.AppendShort(theSender->GetClientId()); aMsg.AppendShort(aPing); BroadcastGameMessage(aMsg.ToByteBuffer()); }
void RemoveBannedKeyOp::RunHook() { SetMessageType(DBProxyAccount); SetSubMessageType(MSGTYPE); mStatusList.clear(); // Stores the message data WriteBuffer requestData; requestData.AppendWString(mCommunityName); requestData.AppendString(mProductName); requestData.AppendShort(mKeyList.size()); for(std::list<std::string>::const_iterator anItr = mKeyList.begin(); anItr != mKeyList.end(); ++anItr) requestData.AppendString(*anItr,1); // Copy buffer into the class SetProxyRequestData(requestData.ToByteBuffer()); DBProxyOp::RunHook(); }
void ReportPatchStatusOp::RunHook() { SetMessageType(DBProxyPatchServer); SetSubMessageType(mMsgType); // Pack the message data WriteBuffer requestData; requestData.AppendString(mProductName); requestData.AppendString(mConfigName); requestData.AppendString(mFromVersion); requestData.AppendString(mToVersion); requestData.AppendString(mNetAddress); // patch url requestData.AppendByte(mPatchStatus); // Pack and call base class implementation SetProxyRequestData(requestData.ToByteBuffer()); if(mUDPSocket.get()==NULL) // just do TCP ServerRequest { DBProxyOp::RunHook(); return; } // Do UDP server request IPAddr anAddr = GetAddr(); if(!anAddr.IsValid()) { Finish(WS_ServerReq_NoServersSpecified); return; } Reset(); unsigned char aLengthFieldSize = GetLengthFieldSize(); SetLengthFieldSize(0); GetNextRequest(); SetLengthFieldSize(aLengthFieldSize); SendBytesToOpPtr anOp = new SendBytesToOp(mRequest, anAddr, mUDPSocket); anOp->Run(GetMode(),GetTimeout()); Finish(WS_Success); }
ByteBufferPtr GetHTTPDocumentOp::GetDocument(HTTPDocOwner theOwner) const { FILE *aFile = NULL; if(!mDocumentPath[theOwner].empty()) // fopen will assert if the path is empty in MS crt debug library aFile = fopen(mDocumentPath[theOwner].c_str(),"r"); if(aFile==NULL) { return new ByteBuffer(""); // return NULL; } const int SIZE = 1024; char aBuf[SIZE]; WriteBuffer anOverallBuf; // Skip the first character in MOTD documents if (mDocType == HTTPDocType_MOTD) { int aChar = fgetc(aFile); if(aChar=='<') // optional HTML ungetc(aChar,aFile); } while(!feof(aFile)) { int aNumRead = fread(aBuf,1,SIZE,aFile); if(aNumRead>0) anOverallBuf.AppendBytes(aBuf,aNumRead); } anOverallBuf.AppendByte(0); fclose(aFile); return anOverallBuf.ToByteBuffer(); }
void StagingLogic::HandleJoinGameRequest(ReadBuffer &theMsg, LobbyClient *theSender) { if(theSender->IsPlayer()) return; ByteBufferPtr aReply = mGame->HandleJoinGameRequest(theMsg,theSender); if(aReply.get()==NULL) return; SendGameMessageToClient(theSender,aReply); LobbyPlayer *aPlayer = theSender->GetPlayer(); if(aPlayer!=NULL) // client successfully joined the game, tell everyone else { WriteBuffer aPlayerJoined; aPlayerJoined.AppendByte(LobbyGameMsg_PlayerJoined); aPlayerJoined.AppendShort(theSender->GetClientId()); aPlayer->WriteData(aPlayerJoined); BroadcastGameMessage(aPlayerJoined.ToByteBuffer()); LobbyStaging::UpdateGameSummary(); return; } }
ByteBufferPtr ElGamal::Encrypt(const void *thePlainText, int thePlainTextLen) const { if(!IsPublic()) return NULL; const unsigned char *aPlainText = (const unsigned char*)thePlainText; int aBlockLen = modulusLen - 3; int aNumBlock = thePlainTextLen / aBlockLen; if ((thePlainTextLen % aBlockLen) != 0) aNumBlock++; WriteBuffer anEncrypt; anEncrypt.Reserve(4+modulusLen*2*aNumBlock); int anOffset = 0; anEncrypt.AppendLong(aNumBlock); while(anOffset < thePlainTextLen) { int thisBlockLen = aBlockLen; if(thePlainTextLen - anOffset < aBlockLen) thisBlockLen = thePlainTextLen - anOffset; RawBuffer anEncryptBlock(modulusLen-1,(unsigned char)0); for(int k=0,j=modulusLen-2-thisBlockLen; j<modulusLen-2; j++,k++) anEncryptBlock[j] = aPlainText[anOffset+k]; anEncryptBlock[modulusLen - 2] = (unsigned char)thisBlockLen; BigInteger ab[2]; if(!encrypt(BigInteger(anEncryptBlock),ab)) return NULL; RawBuffer aa,bb; ab[0].toBinary(aa); ab[1].toBinary(bb); if(aa.length()==modulusLen) anEncrypt.AppendBytes(aa.data(),aa.length()); else if(aa.length()>modulusLen) anEncrypt.AppendBytes(aa.data()+aa.length()-modulusLen,modulusLen); else { for(int i=aa.length(); i<modulusLen; i++) anEncrypt.AppendByte(0); anEncrypt.AppendBytes(aa.data(),aa.length()); } if(bb.length()==modulusLen) anEncrypt.AppendBytes(bb.data(),bb.length()); else if(bb.length()>modulusLen) anEncrypt.AppendBytes(bb.data()+bb.length()-modulusLen,modulusLen); else { for(int i=bb.length(); i<modulusLen; i++) anEncrypt.AppendByte(0); anEncrypt.AppendBytes(bb.data(),bb.length()); } anOffset+=thisBlockLen; } return anEncrypt.ToByteBuffer(); }
void StagingLogic::SendStartGame() { WriteBuffer aMsg; aMsg.AppendByte(LobbyGameMsg_StartGame); BroadcastGameMessage(aMsg.ToByteBuffer()); }
ByteBufferPtr ElGamal::Decrypt(const void *theCipherText, int theCipherTextLen) const { if(!IsPrivate()) return NULL; const unsigned char *in = (const unsigned char*)theCipherText; int inOffset = 0; if(theCipherTextLen-inOffset<4) return NULL; int aNumBlocks = LongFromLittleEndian(*(int*)in); inOffset+=4; if(theCipherTextLen-inOffset < aNumBlocks*modulusLen*2-inOffset) return NULL; RawBuffer aBuf(modulusLen,(unsigned char)0); RawBuffer bBuf(modulusLen,(unsigned char)0); WriteBuffer aDecrypt; BigInteger a;; BigInteger b; BigInteger aPlainText; for(int i=0; i<aNumBlocks; i++) { aBuf.assign(in+inOffset,modulusLen); inOffset+=modulusLen; bBuf.assign(in+inOffset,modulusLen); inOffset+=modulusLen; a.fromBinary(aBuf); b.fromBinary(bBuf); if(!decrypt(a,b,aPlainText)) return NULL; RawBuffer aBigIntArray; aPlainText.toBinary(aBigIntArray); if(aBigIntArray.length()==0) return NULL; int aPlainLen = aBigIntArray[aBigIntArray.length() - 1]; if(aPlainLen>modulusLen - 3) return NULL; if(aBigIntArray.length() - 1 - aPlainLen < 0) { int extra = aPlainLen - (aBigIntArray.length() - 1); for(int j=0; j<extra; j++) aDecrypt.AppendByte(0); aDecrypt.AppendBytes(aBigIntArray.data(),aBigIntArray.length()); } else aDecrypt.AppendBytes(aBigIntArray.data()+aBigIntArray.length()-1-aPlainLen,aPlainLen); } return aDecrypt.ToByteBuffer(); }