Client.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. CString cs;
  93. cs.Format("Error opening connection to %s",g_Opt.m_SendClients[nClient].csIP);
  94. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  95. cs.ReleaseBuffer();
  96. continue;
  97. }
  98. CClip* pClip;
  99. POSITION pos;
  100. pos = pClipList->GetHeadPosition();
  101. while(pos)
  102. {
  103. pClip = pClipList->GetNext(pos);
  104. if(pClip == NULL)
  105. {
  106. ASSERT(FALSE);
  107. LogSendRecieveInfo("Error in GetNext");
  108. break;
  109. }
  110. LogSendRecieveInfo(StrF("Sending clip to %s", g_Opt.m_SendClients[nClient].csIP));
  111. if(client.SendItem(pClip) == FALSE)
  112. {
  113. CString cs;
  114. cs.Format("Error sending clip to %s",g_Opt.m_SendClients[nClient].csIP);
  115. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
  116. cs.ReleaseBuffer();
  117. break;
  118. }
  119. }
  120. client.CloseConnection();
  121. }
  122. }
  123. delete pClipList;
  124. pClipList = NULL;
  125. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
  126. LeaveCriticalSection(&theApp.m_CriticalSection);
  127. return TRUE;
  128. }
  129. CClient::CClient()
  130. {
  131. m_Connection = NULL;
  132. m_pEncryptor = new CEncryption; //CreateEncryptionInterface("encryptdecrypt.dll");
  133. }
  134. CClient::~CClient()
  135. {
  136. CloseConnection();
  137. delete m_pEncryptor;
  138. m_pEncryptor = NULL;
  139. // ReleaseEncryptionInterface(m_pEncryptor);
  140. }
  141. BOOL CClient::CloseConnection()
  142. {
  143. if(m_Connection != NULL && m_Connection != 0)
  144. {
  145. CSendInfo Info;
  146. SendCSendData(Info, MyEnums::EXIT);
  147. closesocket(m_Connection);
  148. WSACleanup();
  149. m_Connection = NULL;
  150. }
  151. return TRUE;
  152. }
  153. BOOL CClient::OpenConnection(const char* servername)
  154. {
  155. WSADATA wsaData;
  156. struct hostent *hp;
  157. unsigned int addr;
  158. struct sockaddr_in server;
  159. int wsaret=WSAStartup(0x101,&wsaData);
  160. if(wsaret)
  161. {
  162. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  163. return FALSE;
  164. }
  165. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  166. if(m_Connection == INVALID_SOCKET)
  167. {
  168. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  169. m_Connection = NULL;
  170. return FALSE;
  171. }
  172. if(inet_addr(servername)==INADDR_NONE)
  173. {
  174. hp=gethostbyname(servername);
  175. }
  176. else
  177. {
  178. addr=inet_addr(servername);
  179. hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
  180. }
  181. if(hp==NULL)
  182. {
  183. LogSendRecieveInfo("ERROR - if(hp==NULL)");
  184. closesocket(m_Connection);
  185. m_Connection = NULL;
  186. return FALSE;
  187. }
  188. server.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
  189. server.sin_family = AF_INET;
  190. server.sin_port = htons((u_short) g_Opt.m_lPort);
  191. if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server)))
  192. {
  193. int nWhy = WSAGetLastError();
  194. LogSendRecieveInfo(StrF("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d", nWhy));
  195. closesocket(m_Connection);
  196. m_Connection = NULL;
  197. return FALSE;
  198. }
  199. return TRUE;
  200. }
  201. BOOL CClient::SendItem(CClip *pClip)
  202. {
  203. CSendInfo Info;
  204. strncpy(Info.m_cComputerName, GetComputerName(), sizeof(Info.m_cComputerName));
  205. strncpy(Info.m_cIP, GetIPAddress(), sizeof(Info.m_cIP));
  206. strncpy(Info.m_cDesc, pClip->m_Desc, sizeof(Info.m_cDesc));
  207. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  208. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  209. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  210. if(SendCSendData(Info, MyEnums::START) == FALSE)
  211. return FALSE;
  212. CClipFormat* pCF;
  213. int nCount = pClip->m_Formats.GetSize();
  214. //For each data type
  215. for( int i=0; i < nCount; i++ )
  216. {
  217. pCF = &pClip->m_Formats.GetData()[i];
  218. LPVOID pvData = GlobalLock(pCF->m_hgData);
  219. long lLength = GlobalSize(pCF->m_hgData);
  220. UCHAR* pOutput = NULL;
  221. int nLenOutput = 0;
  222. LogSendRecieveInfo(StrF("BEFORE Encrypt clip data %d", lLength));
  223. if(m_pEncryptor)
  224. {
  225. if(m_pEncryptor->Encrypt((UCHAR*)pvData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  226. {
  227. LogSendRecieveInfo(StrF("AFTER Encrypt clip data %d", nLenOutput));
  228. Info.m_lParameter1 = nLenOutput;
  229. strncpy(Info.m_cDesc, GetFormatName(pCF->m_cfType), sizeof(Info.m_cDesc));
  230. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  231. if(SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  232. return FALSE;
  233. SendExactSize((char*)pOutput, nLenOutput, false);
  234. m_pEncryptor->FreeBuffer(pOutput);
  235. }
  236. else
  237. {
  238. LogSendRecieveInfo("Failed to encrypt data");
  239. return FALSE;
  240. }
  241. }
  242. else
  243. {
  244. ASSERT(!"SendItem::Encryption not initialized");
  245. LogSendRecieveInfo("SendItem::Encryption not initialized");
  246. }
  247. GlobalUnlock(pCF->m_hgData);
  248. if(SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  249. return FALSE;
  250. }
  251. if(SendCSendData(Info, MyEnums::END) == FALSE)
  252. return FALSE;
  253. theApp.m_lClipsSent++;
  254. return TRUE;
  255. }
  256. BOOL CClient::SendCSendData(CSendInfo &data, MyEnums::eSendType type)
  257. {
  258. data.m_Type = type;
  259. return SendExactSize((char *)&data, sizeof(CSendInfo));
  260. }
  261. BOOL CClient::SendExactSize(char *pData, long lLength, bool bEncrypt)
  262. {
  263. BOOL bRet = FALSE;
  264. if(!m_pEncryptor && bEncrypt)
  265. {
  266. ASSERT(!"Encryption not initialized");
  267. LogSendRecieveInfo("SendExactSize::Encryption not initialized");
  268. return bRet;
  269. }
  270. LogSendRecieveInfo(StrF("START SendExactSize Total %d", lLength));
  271. UCHAR* pOutput = (UCHAR*)pData;
  272. int nLenOutput = lLength;
  273. long lBytesRead = 0;
  274. if(bEncrypt == false || m_pEncryptor->Encrypt((UCHAR*)pData, lLength, g_Opt.m_csPassword, pOutput, nLenOutput))
  275. {
  276. long lExpected = nLenOutput;
  277. while(lBytesRead < lExpected)
  278. {
  279. long lSize = send(m_Connection, (char*)pOutput + lBytesRead, lExpected - lBytesRead, 0);
  280. if(lSize == SOCKET_ERROR || lSize == 0)
  281. {
  282. LogSendRecieveInfo(StrF("lSize == SOCKET_ERROR, %d", WSAGetLastError()));
  283. break;
  284. }
  285. lBytesRead += lSize;
  286. LogSendRecieveInfo(StrF("SendExactSize Last Size %d - Total %d", lSize, lBytesRead));
  287. }
  288. bRet = TRUE;
  289. if(pOutput != (UCHAR*)pData)
  290. m_pEncryptor->FreeBuffer(pOutput);
  291. }
  292. else
  293. {
  294. LogSendRecieveInfo("SendExactSize::Failed to encrypt data");
  295. }
  296. LogSendRecieveInfo(StrF("END SendExactSize Total %d", lBytesRead));
  297. return bRet;
  298. }