BOOL SecureChatIOCP::SendTextMessageTo(ClientContext* pContext, CString sMsg) { if ( !pContext->m_bGotSessionKey ) { //AppendLog("Client is not authorized"); return FALSE; } UINT nBufLen = sMsg.GetLength(); // Add one to the size header for the null termination byte. nBufLen++; // Add one for the Payload type (text) nBufLen++; if ( nBufLen>=MaxEncyptedPayloadSize(MAXIMUMPAYLOADSIZE-2) ) { AppendLog("SendMessageTo FAILED Message to long for encryption.."); return FALSE; } if ( nBufLen>=MAXIMUMPAYLOADSIZE || nBufLen<=0 ) { AppendLog("SendMessageTo FAILED Message to long or zero.."); return FALSE; } CIOCPBuffer *pBuff=AllocateBuffer(IOWrite); if ( !pBuff ) { AppendLog("SendMessageTo FAILED pOverlapBuff=NULL"); return FALSE; } pBuff->EmptyUsed(); // Size Header pBuff->AddData(nBufLen); // Payload Header pBuff->AddData((BYTE)PKG_TEXT_TO_ALL); // Add the string. int length=sMsg.GetLength(); pBuff->AddData((PBYTE) sMsg.GetBuffer(length),length); //Extra Null Teriminate (for Strings) pBuff->AddData((BYTE)'\0'); // Encrypt the buffer pBuff=EnCryptBuffer(pBuff,pContext); // Send it. ASend(pContext,pBuff); return TRUE; }
void SecureChatIOCP::BuildAndSend(ClientContext *pContext, BYTE _pkgtype, UINT nBufferSize1,const BYTE *_pBuff1, UINT nBufferSize2, const BYTE *_pBuff2) { if ( !pContext->m_bGotSessionKey ) { AppendLog("BuildAndSend FAILED, no Session key.."); return; } UINT nPayLoadLen=sizeof(BYTE)+sizeof(UINT)+nBufferSize1+sizeof(UINT)+nBufferSize2+2; // two null termination. if ( nPayLoadLen > MAXIMUMPAYLOADSIZE) { AppendLog("BuildAndSend FAILED, nPayLoadLen > MAXIMUMPAYLOADSIZE"); return; } if ( nPayLoadLen > MaxEncyptedPayloadSize(MAXIMUMPAYLOADSIZE-1) ) { AppendLog("BuildAndSend FAILED, nPayLoadLen > MaxEncyptedPayloadSize"); return; } CIOCPBuffer *pBuff=AllocateBuffer(IOWrite); if ( !pBuff ) { AppendLog("BuildAndSend FAILED pBuff=NULL"); return; } pBuff->EmptyUsed(); // Size Header pBuff->AddData(nPayLoadLen); // Payload type pBuff->AddData((BYTE)_pkgtype); // The size of the buffer pBuff->AddData(nBufferSize1); // add the buffer. pBuff->AddData(_pBuff1,nBufferSize1); pBuff->AddData((BYTE)0); // The size of the buffer pBuff->AddData(nBufferSize2); // add the buffer. pBuff->AddData(_pBuff2,nBufferSize2); pBuff->AddData((BYTE)0); // Encrypt the data.. pBuff=EnCryptBuffer(pBuff,pContext); ASend(pContext,pBuff); }
/* * Send a "Start the file transfer package" to the remote connection. * * */ BOOL MyIOCP::BuildStartFileTransferPackageAndSend(ClientContext *pContext) { CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_StartFileTransfer)) return ASend(pContext,pOverlapBuff); }else { CString msg; msg.Format(_T("Could not allocate memory ASend: %s"),ErrorCode2Text(WSAGetLastError())); AppendLog(msg); DisconnectClient(pContext->m_Socket); return FALSE; } return TRUE; }
BOOL SecureChatIOCP::SendErrorMessageTo(int iClientID, CString sMsg) { UINT nBufLen = sMsg.GetLength(); // Add one to the size header for the null termination byte. nBufLen++; // Add one for the message type (encrypted/SessionExchange/ERROR_MESSAGE).. nBufLen++; // Add one for the Payload type (text) nBufLen++; if ( nBufLen>=MAXIMUMPAYLOADSIZE || nBufLen<=0 ) { AppendLog("SendMessageTo FAILED Message to long or zero.."); return FALSE; } CIOCPBuffer *pBuff=AllocateBuffer(IOWrite); if ( !pBuff ) { AppendLog("SendMessageTo FAILED pOverlapBuff=NULL"); return FALSE; } // Make sure that buffer is empty pBuff->EmptyUsed(); // Size Header pBuff->AddData(nBufLen); // Message Header pBuff->AddData((BYTE)PKG_ERRORMSG); // Payload Header pBuff->AddData((BYTE)PKG_ERRORMSG); // Add the string. int length=sMsg.GetLength(); pBuff->AddData((PBYTE) sMsg.GetBuffer(length),length); //Extra Null Teriminate (for Strings) pBuff->AddData((BYTE)'\0'); ASend(iClientID,pBuff); return TRUE; }
// Build the a text message message and send it.. BOOL MyIOCP::BuildPackageAndSend(ClientContext *pContext, CString sText) { CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_SendText2Client,sText)) return ASend(pContext,pOverlapBuff); else { AppendLog(_T("CreatePackage(0,sText) Failed in BuildPackageAndSend")); ReleaseBuffer(pOverlapBuff); return FALSE; } }else { CString msg; msg.Format(_T("Could not allocate memory ASend: %s"),ErrorCode2Text(WSAGetLastError())); AppendLog(msg); DisconnectClient(pContext->m_Socket); return FALSE; } return FALSE; }
/* * This function Build a package of type * * [obligatory headersize|Headertype (_keytype)| public key size (nPublicKeySize) | Publickey ( * obligatory headersize|Headertype (_keytype)| public key size (nPublicKeySize) | Publickey] * * and sends it to client. */ void SecureChatIOCP::SendPublicKey(ClientContext *pContext, BYTE _keytype, DWORD *_pPublicKey, UINT nPublicKeySize) { CIOCPBuffer *pBuff=AllocateBuffer(IOWrite); if( !pBuff ) { AppendLog("SendPublicKey FAILED pBuff=NULL"); return; } UINT nPayLoadLen=sizeof(DWORD)+sizeof(BYTE)+nPublicKeySize*sizeof(DWORD); pBuff->EmptyUsed(); // Size Header pBuff->AddData(nPayLoadLen); // Payload type pBuff->AddData((BYTE)_keytype); // The size of nPublicKey pBuff->AddData(nPublicKeySize); // add the public key. pBuff->AddData((PBYTE)_pPublicKey,nPublicKeySize*4); ASend(pContext,pBuff); }
/* * This function is called when the remote connection, whant to send a file. * */ void MyIOCP::PackageFileTransfer(CIOCPBuffer *pOverlapBuff,int nSize,ClientContext *pContext) { #ifdef TRANSFERFILEFUNCTIONALITY CString sFileName=""; UINT iMaxFileSize=0; BYTE dummy=0; if(pOverlapBuff->GetPackageInfo(dummy,iMaxFileSize,sFileName)) { // Get The Current Path name and application name. CString sFilePath=""; TCHAR drive[_MAX_DRIVE]; TCHAR dir[_MAX_DIR]; TCHAR fname[_MAX_FNAME]; TCHAR ext[_MAX_EXT]; CString strTemp; GetModuleFileName(NULL,strTemp.GetBuffer(MAX_PATH),MAX_PATH); strTemp.ReleaseBuffer(); #ifdef _UNICODE _wsplitpath_s(strTemp,drive, _MAX_DRIVE,dir,_MAX_DIR,fname,_MAX_FNAME,ext,_MAX_EXT); #else _splitpath( strTemp, drive, dir, fname, ext ); #endif sFilePath=drive; //Drive sFilePath+=dir; //dir sFilePath+=sFileName; //name.. TRACE("Incoming File >%s.\r\n",sFilePath); // Perpare for Receive if(PrepareReceiveFile(pContext->m_Socket,sFilePath,iMaxFileSize)) { // Send start file transfer.. CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_StartFileTransfer)) ASend(pContext,pOverlapBuff); } }else { // Abort Transfer. CIOCPBuffer *pOverlapBuff=AllocateBuffer(IOWrite); if(pOverlapBuff) { if(pOverlapBuff->CreatePackage(Job_AbortFileTransfer)) ASend(pContext,pOverlapBuff); } } // to be sure that pcontext Suddenly does not dissapear.. m_ContextMapLock.Lock(); pContext->m_ContextLock.Lock(); pContext->m_sReceived=sFilePath; // Update that we have data pContext->m_iNumberOfReceivedMsg++; pContext->m_ContextLock.Unlock(); m_ContextMapLock.Unlock(); // Update Statistics. m_StatusLock.Lock(); m_iNumberOfMsg++; m_StatusLock.Unlock(); } #endif }