RecieveSocket.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "StdAfx.h"
  2. #include "RecieveSocket.h"
  3. #include "Options.h"
  4. #include "Misc.h"
  5. #include "CP_Main.h"
  6. #include "shared/TextConvert.h"
  7. CRecieveSocket::CRecieveSocket(SOCKET sock)
  8. {
  9. m_pDataReturnedFromDecrypt = NULL;
  10. m_Sock = sock;
  11. m_pEncryptor = new CEncryption; //CreateEncryptionInterface("encryptdecrypt.dll");
  12. }
  13. CRecieveSocket::~CRecieveSocket()
  14. {
  15. if(m_pEncryptor)
  16. {
  17. m_pEncryptor->FreeBuffer(m_pDataReturnedFromDecrypt);
  18. delete m_pEncryptor;
  19. m_pEncryptor = NULL;
  20. }
  21. }
  22. void CRecieveSocket::FreeDecryptedData()
  23. {
  24. if(g_Opt.m_csPassword == "")
  25. {
  26. delete [] m_pDataReturnedFromDecrypt;
  27. }
  28. else
  29. {
  30. m_pEncryptor->FreeBuffer(m_pDataReturnedFromDecrypt);
  31. }
  32. m_pDataReturnedFromDecrypt = NULL;
  33. }
  34. LPVOID CRecieveSocket::ReceiveEncryptedData(long lInSize, long &lOutSize)
  35. {
  36. if(m_pEncryptor == NULL)
  37. {
  38. LogSendRecieveInfo("ReceiveEncryptedData::Encryption not initialized");
  39. return NULL;
  40. }
  41. if(m_pDataReturnedFromDecrypt)
  42. FreeDecryptedData();
  43. char *pInput = new char[lInSize];
  44. UCHAR* pOutput = NULL;
  45. if(pInput)
  46. {
  47. RecieveExactSize(pInput, lInSize);
  48. int nOut = 0;
  49. CStringA csPassword;
  50. INT_PTR count = g_Opt.m_csNetworkPasswordArray.GetSize();
  51. INT_PTR nIndex;
  52. for(int i = -2; i < count; i++)
  53. {
  54. csPassword.Empty();
  55. nIndex = i;
  56. //First time through try the last index that was valid
  57. if(i == -2)
  58. {
  59. nIndex = theApp.m_lLastGoodIndexForNextworkPassword;
  60. if(nIndex == -2)
  61. continue;
  62. }
  63. if(nIndex == -1)
  64. {
  65. csPassword = g_Opt.m_csPassword;
  66. }
  67. else
  68. {
  69. if(nIndex >= 0 && nIndex < count)
  70. {
  71. CTextConvert::ConvertToUTF8(g_Opt.m_csNetworkPasswordArray[nIndex], csPassword);
  72. }
  73. else
  74. {
  75. continue;
  76. }
  77. }
  78. if(m_pEncryptor->Decrypt((UCHAR*)pInput, lInSize, csPassword, pOutput, nOut) == FALSE)
  79. {
  80. LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to Decrypt data password = %s"), g_Opt.m_csPassword));
  81. }
  82. else
  83. {
  84. theApp.m_lLastGoodIndexForNextworkPassword = (long)nIndex;
  85. break;
  86. }
  87. }
  88. lOutSize = nOut;
  89. delete [] pInput;
  90. pInput = NULL;
  91. }
  92. else
  93. {
  94. ASSERT(FALSE);
  95. LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to create new data size = %d"), lInSize));
  96. }
  97. m_pDataReturnedFromDecrypt = pOutput;
  98. return pOutput;
  99. }
  100. BOOL CRecieveSocket::RecieveExactSize(char *pData, long lSize)
  101. {
  102. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------ Start wanted size %d"), lSize));
  103. long lReceiveCount = 0;
  104. long lWanted = lSize;
  105. long lOffset = 0;
  106. while(lWanted > 0)
  107. {
  108. lReceiveCount = recv(m_Sock, pData + lOffset, lWanted, 0);
  109. if(lReceiveCount == SOCKET_ERROR)
  110. {
  111. LogSendRecieveInfo("RecieveExactSize:: ********ERROR if(lReceiveCount == SOCKET_ERROR)*******");
  112. return FALSE;
  113. }
  114. else if(lReceiveCount == 0)
  115. {
  116. LogSendRecieveInfo("RecieveExactSize:: ********ERROR lRecieveCount == 0");
  117. return FALSE;
  118. }
  119. lWanted -= lReceiveCount;
  120. lOffset += lReceiveCount;
  121. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------Bytes Read %d Total Recieved %d"), lReceiveCount, lOffset));
  122. }
  123. // LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------END RecieveExactSize Recieved %d"), lOffset));
  124. return TRUE;
  125. }
  126. #define ENCRYPTED_SIZE_CSENDINFO 508
  127. BOOL CRecieveSocket::RecieveCSendInfo(CSendInfo *pInfo)
  128. {
  129. BOOL bRet = FALSE;
  130. long lOutSize = 0;
  131. long lRecieveSize = ENCRYPTED_SIZE_CSENDINFO;
  132. LPVOID lpData = ReceiveEncryptedData(lRecieveSize, lOutSize);
  133. if(lpData)
  134. {
  135. memcpy(pInfo, lpData, sizeof(CSendInfo));
  136. bRet = (pInfo->m_nSize == sizeof(CSendInfo));
  137. FreeDecryptedData();
  138. }
  139. return bRet;
  140. }