| 691 | } |
| 692 | |
| 693 | int CEncryptedStreamSocket::SendNegotiatingData(const void* lpBuf, uint32_t nBufLen, uint32_t nStartCryptFromByte, bool bDelaySend) |
| 694 | { |
| 695 | wxASSERT( m_StreamCryptState == ECS_NEGOTIATING || m_StreamCryptState == ECS_ENCRYPTING ); |
| 696 | wxASSERT( nStartCryptFromByte <= nBufLen ); |
| 697 | wxASSERT( m_NegotiatingState == ONS_BASIC_SERVER_DELAYEDSENDING || !bDelaySend ); |
| 698 | //printf("Send negotiation data on %s\n", (const char*) unicode2char(GetPeer())); |
| 699 | uint8_t* pBuffer = NULL; |
| 700 | bool bProcess = false; |
| 701 | if (lpBuf != NULL) { |
| 702 | pBuffer = new uint8_t[nBufLen]; |
| 703 | if (pBuffer == NULL) { |
| 704 | throw CMuleException("Memory exception", "Memory exception on TCP encrypted socket"); |
| 705 | } |
| 706 | |
| 707 | if (nStartCryptFromByte > 0) { |
| 708 | memcpy(pBuffer, lpBuf, nStartCryptFromByte); |
| 709 | } |
| 710 | |
| 711 | if (nBufLen > nStartCryptFromByte) { |
| 712 | //printf("Crypting negotiation data on %s starting on byte %i\n", (const char*) unicode2char(GetPeer()), nStartCryptFromByte); |
| 713 | //DumpMem(lpBuf, nBufLen, "Pre-encryption:"); |
| 714 | m_pfiSendBuffer.RC4Crypt((uint8*)lpBuf + nStartCryptFromByte, pBuffer + nStartCryptFromByte, nBufLen - nStartCryptFromByte); |
| 715 | //DumpMem(pBuffer, nBufLen, "Post-encryption:"); |
| 716 | } |
| 717 | |
| 718 | if (!m_pfiSendBuffer.IsEmpty()) { |
| 719 | // we already have data pending. Attach it and try to send |
| 720 | if (m_NegotiatingState == ONS_BASIC_SERVER_DELAYEDSENDING) { |
| 721 | m_NegotiatingState = ONS_COMPLETE; |
| 722 | } else { |
| 723 | wxFAIL; |
| 724 | } |
| 725 | m_pfiSendBuffer.Append(pBuffer, nBufLen); |
| 726 | delete[] pBuffer; |
| 727 | pBuffer = NULL; |
| 728 | nStartCryptFromByte = 0; |
| 729 | bProcess = true; // we want to try to send it right now |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if (lpBuf == NULL || bProcess) { |
| 734 | // this call is for processing pending data |
| 735 | if (m_pfiSendBuffer.IsEmpty() || nStartCryptFromByte != 0) { |
| 736 | wxFAIL; |
| 737 | return 0; // or not |
| 738 | } |
| 739 | nBufLen = (uint32)m_pfiSendBuffer.GetLength(); |
| 740 | pBuffer = m_pfiSendBuffer.Detach(); |
| 741 | } |
| 742 | |
| 743 | wxASSERT( m_pfiSendBuffer.IsEmpty() ); |
| 744 | |
| 745 | uint32_t result = 0; |
| 746 | if (!bDelaySend) { |
| 747 | //printf("Writing negotiation data on %s: ", (const char*) unicode2char(GetPeer())); |
| 748 | result = CSocketClientProxy::Write(pBuffer, nBufLen); |
| 749 | //printf("Wrote %i bytes\n",result); |
| 750 | } |