Client.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Client.cpp: implementation of the CClient class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "Client.h"
  7. #include "shared/TextConvert.h"
  8. #include "RecieveSocket.h"
  9. #include "FileRecieve.h"
  10. #include "FileTransferProgressDlg.h"
  11. #include "Shared/Tokenizer.h"
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char THIS_FILE[]=__FILE__;
  15. #define new DEBUG_NEW
  16. #endif
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. BOOL SendToFriend(CSendToFriendInfo &Info)
  21. {
  22. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend - @@@@@@@@@@@@@@@");
  23. if(Info.m_csIP == _T(""))
  24. {
  25. Info.m_csErrorText = StrF(_T("ERROR getting ip/host name position - %s"), Info.m_csIP);
  26. LogSendRecieveInfo(Info.m_csErrorText);
  27. return FALSE;
  28. }
  29. LogSendRecieveInfo(StrF(_T("Sending clip to %s"), Info.m_csIP));
  30. CClient client;
  31. if(client.OpenConnection(Info.m_csIP) == FALSE)
  32. {
  33. Info.m_csErrorText = StrF(_T("ERROR opening connection to %s"), Info.m_csIP);
  34. LogSendRecieveInfo(Info.m_csErrorText);
  35. return FALSE;
  36. }
  37. INT_PTR count = Info.m_pClipList->GetCount();
  38. int i = -1;
  39. CClip* pClip;
  40. POSITION pos;
  41. pos = Info.m_pClipList->GetHeadPosition();
  42. while(pos)
  43. {
  44. pClip = Info.m_pClipList->GetNext(pos);
  45. if(pClip == NULL)
  46. {
  47. ASSERT(FALSE);
  48. continue;
  49. }
  50. i++;
  51. if(Info.m_pPopup)
  52. {
  53. Info.m_pPopup->SendToolTipText(StrF(_T("Sending %d of %d"), i+1, count));
  54. }
  55. MSG msg;
  56. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  57. {
  58. TranslateMessage(&msg);
  59. DispatchMessage(&msg);
  60. }
  61. LogSendRecieveInfo(StrF(_T("Sending %d of %d clip to %s"), i+1, count, Info.m_csIP));
  62. if(client.SendItem(pClip, Info.m_manualSend) == FALSE)
  63. {
  64. Info.m_csErrorText = "ERROR SendItem Failed";
  65. LogSendRecieveInfo(Info.m_csErrorText);
  66. return FALSE;
  67. }
  68. }
  69. LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
  70. return TRUE;
  71. }
  72. CClient::CClient()
  73. {
  74. m_Connection = NULL;
  75. m_connectionPort = 0;
  76. }
  77. CClient::~CClient()
  78. {
  79. CloseConnection();
  80. }
  81. BOOL CClient::CloseConnection()
  82. {
  83. if(m_Connection != NULL && m_Connection != 0)
  84. {
  85. CSendInfo Info;
  86. m_SendSocket.SendCSendData(Info, MyEnums::EXIT);
  87. closesocket(m_Connection);
  88. WSACleanup();
  89. m_Connection = NULL;
  90. }
  91. return TRUE;
  92. }
  93. BOOL CClient::OpenConnection(const TCHAR* servername)
  94. {
  95. WSADATA wsaData;
  96. unsigned int addr = INADDR_NONE;
  97. struct sockaddr_in server;
  98. int wsaret=WSAStartup(0x101,&wsaData);
  99. if(wsaret)
  100. {
  101. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  102. return FALSE;
  103. }
  104. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  105. if(m_Connection == INVALID_SOCKET)
  106. {
  107. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  108. m_Connection = NULL;
  109. return FALSE;
  110. }
  111. CString parsedServerName = servername;
  112. m_connectionPort = CGetSetOptions::m_lPort;
  113. CTokenizer tokenizer(servername, ":");
  114. CString token;
  115. int pos = 0;
  116. while (tokenizer.Next(token))
  117. {
  118. if (pos == 0)
  119. {
  120. parsedServerName = token;
  121. }
  122. else if (pos == 1)
  123. {
  124. m_connectionPort = ATOI(token);
  125. }
  126. pos++;
  127. }
  128. CStringA csServerNameA = CTextConvert::UnicodeToAnsi(parsedServerName);
  129. //11-5-06 Serge Baranov found that if we are passing in an ip then
  130. //don't look the name up using gethostbyname/gethostbyaddr->
  131. //on simple networks that don't use DNS these will fail.
  132. //So now only lookup the host name if they don't provide an IP.
  133. addr = inet_addr(csServerNameA);
  134. if(addr == INADDR_NONE)
  135. {
  136. struct hostent *hp = gethostbyname(csServerNameA);
  137. if(hp != NULL)
  138. {
  139. addr = *(unsigned int*)hp->h_addr;
  140. }
  141. }
  142. if(addr == NULL || addr == INADDR_NONE)
  143. {
  144. LogSendRecieveInfo("addr == NULL || addr == INADDR_NONE");
  145. closesocket(m_Connection);
  146. m_Connection = NULL;
  147. return FALSE;
  148. }
  149. server.sin_addr.s_addr = addr;
  150. server.sin_family = AF_INET;
  151. server.sin_port = htons((u_short)m_connectionPort);
  152. if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server)))
  153. {
  154. int nWhy = WSAGetLastError();
  155. LogSendRecieveInfo(StrF(_T("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d"), nWhy));
  156. closesocket(m_Connection);
  157. m_Connection = NULL;
  158. return FALSE;
  159. }
  160. return TRUE;
  161. }
  162. BOOL CClient::SendItem(CClip *pClip, bool manualSend)
  163. {
  164. CSendInfo Info;
  165. Info.m_manualSend = manualSend;
  166. ////only send a response port if it's different than the default
  167. if (CGetSetOptions::m_lPort != 23443 || m_connectionPort != 23443)
  168. {
  169. Info.m_respondPort = CGetSetOptions::m_lPort;
  170. }
  171. //Send all text over as UTF-8
  172. CStringA dest = CTextConvert::UnicodeToUTF8(GetComputerName());
  173. strncpy(Info.m_cComputerName, dest, sizeof(Info.m_cComputerName));
  174. dest = CTextConvert::UnicodeToUTF8(GetIPAddress());
  175. strncpy(Info.m_cIP, dest, sizeof(Info.m_cIP));
  176. dest = CTextConvert::UnicodeToUTF8(pClip->m_Desc);
  177. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  178. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  179. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  180. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  181. m_SendSocket.SetSocket(m_Connection);
  182. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  183. return FALSE;
  184. CClipFormat* pCF;
  185. INT_PTR count = pClip->m_Formats.GetSize();
  186. //For each data type
  187. for(int i=0; i < count; i++)
  188. {
  189. pCF = &pClip->m_Formats.GetData()[i];
  190. SendClipFormat(pCF);
  191. }
  192. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  193. return FALSE;
  194. theApp.m_lClipsSent++;
  195. return TRUE;
  196. }
  197. BOOL CClient::SendClipFormat(CClipFormat* pCF)
  198. {
  199. CSendInfo Info;
  200. LPVOID pvData = GlobalLock(pCF->m_hgData);
  201. INT_PTR length = GlobalSize(pCF->m_hgData);
  202. UCHAR* pOutput = NULL;
  203. int nLenOutput = 0;
  204. CTextConvert Convert;
  205. BOOL bRet = FALSE;
  206. LogSendRecieveInfo(StrF(_T("BEFORE Encrypt clip data %d"), length));
  207. if(m_SendSocket.m_pEncryptor)
  208. {
  209. if(m_SendSocket.m_pEncryptor->Encrypt((UCHAR*)pvData, (int)length, CGetSetOptions::m_csPassword, pOutput, nLenOutput))
  210. {
  211. LogSendRecieveInfo(StrF(_T("AFTER Encrypt clip data %d"), nLenOutput));
  212. Info.m_lParameter1 = nLenOutput;
  213. //Send over as UTF-8
  214. CStringA dest = CTextConvert::UnicodeToUTF8(GetFormatName(pCF->m_cfType));
  215. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  216. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  217. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  218. return FALSE;
  219. m_SendSocket.SendExactSize((char*)pOutput, nLenOutput, false);
  220. m_SendSocket.m_pEncryptor->FreeBuffer(pOutput);
  221. bRet = TRUE;
  222. }
  223. else
  224. {
  225. LogSendRecieveInfo("Failed to encrypt data");
  226. return FALSE;
  227. }
  228. }
  229. else
  230. {
  231. ASSERT(!"SendItem::Encryption not initialized");
  232. LogSendRecieveInfo("SendItem::Encryption not initialized");
  233. }
  234. GlobalUnlock(pCF->m_hgData);
  235. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  236. return FALSE;
  237. return bRet;
  238. }
  239. HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CString csComputerName)
  240. {
  241. CSendInfo Info;
  242. bool bBreak = false;
  243. HGLOBAL hReturn = NULL;
  244. CString csErrorString;
  245. CFileTransferProgressDlg *pProgress = new CFileTransferProgressDlg;
  246. if(pProgress == NULL)
  247. return NULL;
  248. LogSendRecieveInfo(StrF(_T("************** START of requesting files from cpu %s, ip: %s **************"), csComputerName, csIP));
  249. pProgress->Create(IDD_DIALOG_REMOTE_FILE);
  250. pProgress->ShowWindow(SW_SHOW);
  251. pProgress->SetMessage(StrF(_T("Opening Connection to %s (%s)"), csComputerName, csIP));
  252. pProgress->PumpMessages();
  253. CString requestFrom;
  254. if(CGetSetOptions::GetRequestFilesUsingIP())
  255. {
  256. requestFrom = csIP;
  257. }
  258. else
  259. {
  260. requestFrom = csComputerName;
  261. }
  262. do
  263. {
  264. if(OpenConnection(requestFrom) == FALSE)
  265. {
  266. csErrorString.Format(_T("Error Opening Connection to %s (%s)"), csComputerName, csIP);
  267. break;
  268. }
  269. m_SendSocket.SetSocket(m_Connection);
  270. m_SendSocket.SetProgressBar(pProgress);
  271. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  272. break;
  273. if(SendClipFormat(&HDropFormat) == FALSE)
  274. {
  275. csErrorString = _T("Error sending data request.");
  276. break;
  277. }
  278. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  279. break;
  280. pProgress->SetMessage(StrF(_T("Requesting Files from %s (%s)"), csComputerName, csIP));
  281. if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE)
  282. break;
  283. CFileRecieve Recieve;
  284. long lRet = Recieve.RecieveFiles(m_Connection, csIP, pProgress);
  285. if(lRet == TRUE)
  286. {
  287. hReturn = Recieve.CreateCF_HDROPBuffer();
  288. }
  289. else if(lRet == FALSE || lRet == MD5_MISMATCH)
  290. {
  291. if(pProgress != NULL && pProgress->Cancelled())
  292. {
  293. //Don't show an error message the user canceled things
  294. }
  295. else
  296. {
  297. csErrorString = _T("Error receiving files.");
  298. if (lRet == MD5_MISMATCH)
  299. {
  300. csErrorString += _T(" MD5 Match Error.");
  301. }
  302. }
  303. }
  304. } while(false);
  305. CloseConnection();
  306. if(hReturn == NULL && csErrorString.IsEmpty() == FALSE)
  307. {
  308. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)csErrorString.GetBuffer(csErrorString.GetLength()), 0);
  309. }
  310. pProgress->DestroyWindow();
  311. LogSendRecieveInfo(StrF(_T("************** END of requesting files from cpu %s, ip: %s **************************"), csComputerName, csIP));
  312. return hReturn;
  313. }