RecieveSocket.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. m_pProgress = NULL;
  13. }
  14. CRecieveSocket::~CRecieveSocket()
  15. {
  16. if(m_pEncryptor)
  17. {
  18. m_pEncryptor->FreeBuffer(m_pDataReturnedFromDecrypt);
  19. delete m_pEncryptor;
  20. m_pEncryptor = NULL;
  21. }
  22. }
  23. void CRecieveSocket::FreeDecryptedData()
  24. {
  25. if(g_Opt.m_csPassword == "")
  26. {
  27. delete [] m_pDataReturnedFromDecrypt;
  28. }
  29. else
  30. {
  31. m_pEncryptor->FreeBuffer(m_pDataReturnedFromDecrypt);
  32. }
  33. m_pDataReturnedFromDecrypt = NULL;
  34. }
  35. LPVOID CRecieveSocket::ReceiveEncryptedData(long lInSize, long &lOutSize)
  36. {
  37. if(m_pEncryptor == NULL)
  38. {
  39. LogSendRecieveInfo("ReceiveEncryptedData::Encryption not initialized");
  40. return NULL;
  41. }
  42. if(m_pDataReturnedFromDecrypt)
  43. FreeDecryptedData();
  44. char *pInput = new char[lInSize];
  45. UCHAR* pOutput = NULL;
  46. if(pInput)
  47. {
  48. int nOut = 0;
  49. if(RecieveExactSize(pInput, lInSize))
  50. {
  51. CStringA csPassword;
  52. INT_PTR count = g_Opt.m_csNetworkPasswordArray.GetSize();
  53. INT_PTR nIndex;
  54. for(int i = -2; i < count; i++)
  55. {
  56. csPassword.Empty();
  57. nIndex = i;
  58. //First time through try the last index that was valid
  59. if(i == -2)
  60. {
  61. nIndex = theApp.m_lLastGoodIndexForNextworkPassword;
  62. if(nIndex == -2)
  63. continue;
  64. }
  65. if(nIndex == -1)
  66. {
  67. csPassword = g_Opt.m_csPassword;
  68. }
  69. else
  70. {
  71. if(nIndex >= 0 && nIndex < count)
  72. {
  73. CTextConvert::ConvertToUTF8(g_Opt.m_csNetworkPasswordArray[nIndex], csPassword);
  74. }
  75. else
  76. {
  77. continue;
  78. }
  79. }
  80. if(m_pEncryptor->Decrypt((UCHAR*)pInput, lInSize, csPassword, pOutput, nOut) == FALSE)
  81. {
  82. LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to Decrypt data password = %s"), g_Opt.m_csPassword));
  83. }
  84. else
  85. {
  86. theApp.m_lLastGoodIndexForNextworkPassword = (long)nIndex;
  87. break;
  88. }
  89. }
  90. }
  91. else
  92. {
  93. LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: FAILED"), lInSize));
  94. }
  95. lOutSize = nOut;
  96. delete [] pInput;
  97. pInput = NULL;
  98. }
  99. else
  100. {
  101. ASSERT(FALSE);
  102. LogSendRecieveInfo(StrF(_T("ReceiveEncryptedData:: Failed to create new data size = %d"), lInSize));
  103. }
  104. m_pDataReturnedFromDecrypt = pOutput;
  105. return pOutput;
  106. }
  107. int recv_to(int fd, char *buffer, int len, int flags, int to)
  108. {
  109. fd_set readset;
  110. int result, iof = -1;
  111. struct timeval tv;
  112. // Initialize the set
  113. FD_ZERO(&readset);
  114. FD_SET(fd, &readset);
  115. // Initialize time out struct
  116. tv.tv_sec = 0;
  117. tv.tv_usec = to * 1000;
  118. // select()
  119. result = select(fd+1, &readset, NULL, NULL, &tv);
  120. // Check status
  121. if (result < 0)
  122. return -1;
  123. else if (result > 0 && FD_ISSET(fd, &readset))
  124. {
  125. // Set non-blocking mode
  126. //if ((iof = fcntl(fd, F_GETFL, 0)) != -1)
  127. // fcntl(fd, F_SETFL, iof | O_NONBLOCK);
  128. // receive
  129. result = recv(fd, buffer, len, flags);
  130. // set as before
  131. //if (iof != -1)
  132. // fcntl(fd, F_SETFL, iof);
  133. return result;
  134. }
  135. return -2;
  136. }
  137. BOOL CRecieveSocket::RecieveExactSize(char *pData, long lSize)
  138. {
  139. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------ Start wanted size %d"), lSize));
  140. long lReceiveCount = 0;
  141. long lWanted = lSize;
  142. long lOffset = 0;
  143. CString originalText = _T("");
  144. while(lWanted > 0)
  145. {
  146. fd_set readset;
  147. int res;
  148. int timeoutMs = CGetSetOptions::GetNetworkReadTimeoutMS();
  149. int loops100msEach = (timeoutMs/100);
  150. for(int i = 0; i < loops100msEach; i++)
  151. {
  152. lReceiveCount = recv_to(m_Sock, pData + lOffset, lWanted, 0, 100);
  153. if(lReceiveCount >= 0)
  154. {
  155. break;
  156. }
  157. else if(lReceiveCount == SOCKET_ERROR)
  158. {
  159. ASSERT(FALSE);
  160. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: Socket Error")));
  161. return FALSE;
  162. }
  163. if(lReceiveCount == -2 && i > 15)
  164. {
  165. if(m_pProgress != NULL)
  166. {
  167. originalText = m_pProgress->GetMessage();
  168. m_pProgress->SetMessage(StrF(_T("Requesting data from Server")));
  169. m_pProgress->PumpMessages();
  170. if(m_pProgress->Cancelled())
  171. {
  172. return FALSE;
  173. }
  174. }
  175. }
  176. }
  177. if(lReceiveCount == -2)
  178. {
  179. ASSERT(FALSE);
  180. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: Timeout waiting for server")));
  181. return FALSE;
  182. }
  183. if(lReceiveCount == SOCKET_ERROR)
  184. {
  185. LogSendRecieveInfo("RecieveExactSize:: ********ERROR if(lReceiveCount == SOCKET_ERROR)*******");
  186. return FALSE;
  187. }
  188. else if(lReceiveCount == 0)
  189. {
  190. LogSendRecieveInfo("RecieveExactSize:: ********ERROR lRecieveCount == 0");
  191. return FALSE;
  192. }
  193. if(m_pProgress != NULL &&
  194. originalText != _T(""))
  195. {
  196. m_pProgress->SetMessage(originalText);
  197. }
  198. lWanted -= lReceiveCount;
  199. lOffset += lReceiveCount;
  200. LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------Bytes Read %d Total Recieved %d"), lReceiveCount, lOffset));
  201. }
  202. // LogSendRecieveInfo(StrF(_T("RecieveExactSize:: ------END RecieveExactSize Recieved %d"), lOffset));
  203. return TRUE;
  204. }
  205. #define ENCRYPTED_SIZE_CSENDINFO 508
  206. BOOL CRecieveSocket::RecieveCSendInfo(CSendInfo *pInfo)
  207. {
  208. BOOL bRet = FALSE;
  209. long lOutSize = 0;
  210. long lRecieveSize = ENCRYPTED_SIZE_CSENDINFO;
  211. LPVOID lpData = ReceiveEncryptedData(lRecieveSize, lOutSize);
  212. if(lpData)
  213. {
  214. memcpy(pInfo, lpData, sizeof(CSendInfo));
  215. bRet = (pInfo->m_nSize == sizeof(CSendInfo));
  216. FreeDecryptedData();
  217. }
  218. return bRet;
  219. }