Client.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Client.cpp: implementation of the CClient class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "Client.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. BOOL SendToFriend(CSendToFriendInfo &Info)
  16. {
  17. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend - @@@@@@@@@@@@@@@");
  18. if(Info.m_lPos > -1 && Info.m_lPos < MAX_SEND_CLIENTS)
  19. {
  20. Info.m_csIP = g_Opt.m_SendClients[Info.m_lPos].csIP;
  21. }
  22. else
  23. {
  24. Info.m_csErrorText = StrF("ERROR getting ip position - %d", Info.m_lPos);
  25. LogSendRecieveInfo(Info.m_csErrorText);
  26. return FALSE;
  27. }
  28. LogSendRecieveInfo(StrF("Sending clip to %s", Info.m_csIP));
  29. CClient client;
  30. if(client.OpenConnection(Info.m_csIP) == FALSE)
  31. {
  32. Info.m_csErrorText = StrF("ERROR opening connection to %s", Info.m_csIP);
  33. LogSendRecieveInfo(Info.m_csErrorText);
  34. return FALSE;
  35. }
  36. long lCount = Info.m_pClipList->GetCount();
  37. int i = -1;
  38. CClip* pClip;
  39. POSITION pos;
  40. pos = Info.m_pClipList->GetHeadPosition();
  41. while(pos)
  42. {
  43. pClip = Info.m_pClipList->GetNext(pos);
  44. if(pClip == NULL)
  45. {
  46. ASSERT(FALSE);
  47. continue;
  48. }
  49. i++;
  50. if(Info.m_pPopup)
  51. {
  52. Info.m_pPopup->SendToolTipText(StrF("Sending %d of %d", i+1, lCount));
  53. }
  54. MSG msg;
  55. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  56. {
  57. TranslateMessage(&msg);
  58. DispatchMessage(&msg);
  59. }
  60. LogSendRecieveInfo(StrF("Sending %d of %d clip to %s", i+1, lCount, Info.m_csIP));
  61. if(client.SendItem(pClip) == FALSE)
  62. {
  63. Info.m_csErrorText = "ERROR SendItem Failed";
  64. LogSendRecieveInfo(Info.m_csErrorText);
  65. return FALSE;
  66. }
  67. }
  68. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
  69. return TRUE;
  70. }
  71. UINT SendClientThread(LPVOID pParam)
  72. {
  73. EnterCriticalSection(&theApp.m_CriticalSection);
  74. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
  75. CClipList *pClipList = (CClipList*)pParam;
  76. if(pClipList == NULL)
  77. {
  78. LogSendRecieveInfo("ERROR if(pClipList == NULL)");
  79. return FALSE;
  80. }
  81. long lCount = pClipList->GetCount();
  82. LogSendRecieveInfo(StrF("Start of Send ClientThread Count - %d", lCount));
  83. for(int nClient = 0; nClient < MAX_SEND_CLIENTS; nClient++)
  84. {
  85. if(g_Opt.m_SendClients[nClient].bSendAll &&
  86. g_Opt.m_SendClients[nClient].csIP.GetLength() > 0)
  87. {
  88. CClient client;
  89. if(client.OpenConnection(g_Opt.m_SendClients[nClient].csIP) == FALSE)
  90. {
  91. LogSendRecieveInfo(StrF("ERROR opening connection to %s", g_Opt.m_SendClients[nClient].csIP));
  92. if(g_Opt.m_SendClients[nClient].bShownFirstError == FALSE)
  93. {
  94. CString cs;
  95. cs.Format("Error opening connection to %s",g_Opt.m_SendClients[nClient].csIP);
  96. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  97. cs.ReleaseBuffer();
  98. g_Opt.m_SendClients[nClient].bShownFirstError = TRUE;
  99. }
  100. continue;
  101. }
  102. //We were connected successfully show an error next time we can't connect
  103. g_Opt.m_SendClients[nClient].bShownFirstError = FALSE;
  104. CClip* pClip;
  105. POSITION pos;
  106. pos = pClipList->GetHeadPosition();
  107. while(pos)
  108. {
  109. pClip = pClipList->GetNext(pos);
  110. if(pClip == NULL)
  111. {
  112. ASSERT(FALSE);
  113. LogSendRecieveInfo("Error in GetNext");
  114. break;
  115. }
  116. LogSendRecieveInfo(StrF("Sending clip to %s", g_Opt.m_SendClients[nClient].csIP));
  117. if(client.SendItem(pClip) == FALSE)
  118. {
  119. CString cs;
  120. cs.Format("Error sending clip to %s",g_Opt.m_SendClients[nClient].csIP);
  121. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  122. cs.ReleaseBuffer();
  123. break;
  124. }
  125. }
  126. client.CloseConnection();
  127. }
  128. }
  129. delete pClipList;
  130. pClipList = NULL;
  131. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
  132. LeaveCriticalSection(&theApp.m_CriticalSection);
  133. return TRUE;
  134. }
  135. CClient::CClient()
  136. {
  137. m_Connection = NULL;
  138. m_pEncryptor = new CEncryption; //CreateEncryptionInterface("encryptdecrypt.dll");
  139. }
  140. CClient::~CClient()
  141. {
  142. CloseConnection();
  143. delete m_pEncryptor;
  144. m_pEncryptor = NULL;
  145. // ReleaseEncryptionInterface(m_pEncryptor);
  146. }
  147. BOOL CClient::CloseConnection()
  148. {
  149. if(m_Connection != NULL && m_Connection != 0)
  150. {
  151. CSendInfo Info;
  152. SendCSendData(Info, MyEnums::EXIT);
  153. closesocket(m_Connection);
  154. WSACleanup();
  155. m_Connection = NULL;
  156. }
  157. return TRUE;
  158. }
  159. BOOL CClient::OpenConnection(const char* servername)
  160. {
  161. WSADATA wsaData;
  162. struct hostent *hp;
  163. unsigned int addr;
  164. struct sockaddr_in server;
  165. int wsaret=WSAStartup(0x101,&wsaData);
  166. if(wsaret)
  167. {
  168. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  169. return FALSE;
  170. }
  171. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  172. if(m_Connection == INVALID_SOCKET)
  173. {
  174. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  175. m_Connection = NULL;
  176. return FALSE;
  177. }
  178. if(inet_addr(servername)==INADDR_NONE)
  179. {
  180. hp=gethostbyname(servername);
  181. }
  182. else
  183. {
  184. addr=inet_addr(servername);
  185. hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
  186. }
  187. if(hp==NULL)
  188. {
  189. LogSendRecieveInfo("ERROR - if(hp==NULL)");
  190. closesocket(m_Connection);
  191. m_Connection = NULL;
  192. return FALSE;
  193. }
  194. server.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
  195. server.sin_family = AF_INET;
  196. server.sin_port = htons((u_short) g_Opt.m_lPort);
  197. if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server)))
  198. {
  199. int nWhy = WSAGetLastError();
  200. LogSendRecieveInfo(StrF("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d", nWhy));
  201. closesocket(m_Connection);
  202. m_Connection = NULL;
  203. return FALSE;
  204. }
  205. return TRUE;
  206. }
  207. BOOL CClient::SendItem(CClip *pClip)
  208. {
  209. CSendInfo Info;
  210. strncpy(Info.m_cComputerName, GetComputerName(), sizeof(Info.m_cComputerName));
  211. strncpy(Info.m_cIP, GetIPAddress(), sizeof(Info.m_cIP));
  212. strncpy(Info.m_cDesc, pClip->m_Desc, sizeof(Info.m_cDesc));
  213. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  214. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  215. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  216. if(SendCSendData(Info, MyEnums::START) == FALSE)
  217. return FALSE;
  218. CClipFormat* pCF;
  219. int nCount = pClip->m_Formats.GetSize();
  220. //For each data type
  221. for( int i=0; i < nCount; i++ )
  222. {
  223. pCF = &pClip->m_Formats.GetData()[i];
  224. LPVOID pvData = GlobalLock(pCF->m_hgData);
  225. long lLength = GlobalSize(pCF->m_hgData);
  226. UCHAR* pOutput = NULL;
  227. int nLenOutput = 0;
  228. LogSendRecieveInfo(StrF("BEFORE Encrypt clip data %d", lLength));
  229. if(m_pEncryptor)
  230. {
  231. if(m_pEncryptor->Encrypt((UCHAR*)pvData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  232. {
  233. LogSendRecieveInfo(StrF("AFTER Encrypt clip data %d", nLenOutput));
  234. Info.m_lParameter1 = nLenOutput;
  235. strncpy(Info.m_cDesc, GetFormatName(pCF->m_cfType), sizeof(Info.m_cDesc));
  236. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  237. if(SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  238. return FALSE;
  239. SendExactSize((char*)pOutput, nLenOutput, false);
  240. m_pEncryptor->FreeBuffer(pOutput);
  241. }
  242. else
  243. {
  244. LogSendRecieveInfo("Failed to encrypt data");
  245. return FALSE;
  246. }
  247. }
  248. else
  249. {
  250. ASSERT(!"SendItem::Encryption not initialized");
  251. LogSendRecieveInfo("SendItem::Encryption not initialized");
  252. }
  253. GlobalUnlock(pCF->m_hgData);
  254. if(SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  255. return FALSE;
  256. }
  257. if(SendCSendData(Info, MyEnums::END) == FALSE)
  258. return FALSE;
  259. theApp.m_lClipsSent++;
  260. return TRUE;
  261. }
  262. BOOL CClient::SendCSendData(CSendInfo &data, MyEnums::eSendType type)
  263. {
  264. data.m_Type = type;
  265. return SendExactSize((char *)&data, sizeof(CSendInfo));
  266. }
  267. BOOL CClient::SendExactSize(char *pData, long lLength, bool bEncrypt)
  268. {
  269. BOOL bRet = FALSE;
  270. if(!m_pEncryptor && bEncrypt)
  271. {
  272. ASSERT(!"Encryption not initialized");
  273. LogSendRecieveInfo("SendExactSize::Encryption not initialized");
  274. return bRet;
  275. }
  276. LogSendRecieveInfo(StrF("START SendExactSize Total %d", lLength));
  277. UCHAR* pOutput = (UCHAR*)pData;
  278. int nLenOutput = lLength;
  279. long lBytesRead = 0;
  280. if(bEncrypt == false || m_pEncryptor->Encrypt((UCHAR*)pData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  281. {
  282. long lExpected = nLenOutput;
  283. while(lBytesRead < lExpected)
  284. {
  285. long lSize = send(m_Connection, (char*)pOutput + lBytesRead, lExpected - lBytesRead, 0);
  286. if(lSize == SOCKET_ERROR || lSize == 0)
  287. {
  288. LogSendRecieveInfo(StrF("lSize == SOCKET_ERROR, %d", WSAGetLastError()));
  289. break;
  290. }
  291. lBytesRead += lSize;
  292. LogSendRecieveInfo(StrF("SendExactSize Last Size %d - Total %d", lSize, lBytesRead));
  293. }
  294. bRet = TRUE;
  295. if(pOutput != (UCHAR*)pData)
  296. m_pEncryptor->FreeBuffer(pOutput);
  297. }
  298. else
  299. {
  300. LogSendRecieveInfo("SendExactSize::Failed to encrypt data");
  301. }
  302. LogSendRecieveInfo(StrF("END SendExactSize Total %d", lBytesRead));
  303. return bRet;
  304. }