Client.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. }
  76. CClient::~CClient()
  77. {
  78. CloseConnection();
  79. }
  80. BOOL CClient::CloseConnection()
  81. {
  82. if(m_Connection != NULL && m_Connection != 0)
  83. {
  84. CSendInfo Info;
  85. m_SendSocket.SendCSendData(Info, MyEnums::EXIT);
  86. closesocket(m_Connection);
  87. WSACleanup();
  88. m_Connection = NULL;
  89. }
  90. return TRUE;
  91. }
  92. BOOL CClient::OpenConnection(const TCHAR* servername)
  93. {
  94. WSADATA wsaData;
  95. unsigned int addr = INADDR_NONE;
  96. struct sockaddr_in server;
  97. int wsaret=WSAStartup(0x101,&wsaData);
  98. if(wsaret)
  99. {
  100. LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)");
  101. return FALSE;
  102. }
  103. m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  104. if(m_Connection == INVALID_SOCKET)
  105. {
  106. LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
  107. m_Connection = NULL;
  108. return FALSE;
  109. }
  110. CString parsedServerName = servername;
  111. auto port = g_Opt.m_lPort;
  112. CTokenizer tokenizer(servername, ":");
  113. CString token;
  114. int pos = 0;
  115. while (tokenizer.Next(token))
  116. {
  117. if (pos == 0)
  118. {
  119. parsedServerName = token;
  120. }
  121. else if (pos == 1)
  122. {
  123. port = ATOI(token);
  124. }
  125. pos++;
  126. }
  127. CStringA csServerNameA = CTextConvert::ConvertToChar(parsedServerName);
  128. //11-5-06 Serge Baranov found that if we are passing in an ip then
  129. //don't look the name up using gethostbyname/gethostbyaddr->
  130. //on simple networks that don't use DNS these will fail.
  131. //So now only lookup the host name if they don't provide an IP.
  132. addr = inet_addr(csServerNameA);
  133. if(addr == INADDR_NONE)
  134. {
  135. struct hostent *hp = gethostbyname(csServerNameA);
  136. if(hp != NULL)
  137. {
  138. addr = *(unsigned int*)hp->h_addr;
  139. }
  140. }
  141. if(addr == NULL || addr == INADDR_NONE)
  142. {
  143. LogSendRecieveInfo("addr == NULL || addr == INADDR_NONE");
  144. closesocket(m_Connection);
  145. m_Connection = NULL;
  146. return FALSE;
  147. }
  148. server.sin_addr.s_addr = addr;
  149. server.sin_family = AF_INET;
  150. server.sin_port = htons((u_short)port);
  151. if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server)))
  152. {
  153. int nWhy = WSAGetLastError();
  154. LogSendRecieveInfo(StrF(_T("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d"), nWhy));
  155. closesocket(m_Connection);
  156. m_Connection = NULL;
  157. return FALSE;
  158. }
  159. return TRUE;
  160. }
  161. BOOL CClient::SendItem(CClip *pClip, bool manualSend)
  162. {
  163. CSendInfo Info;
  164. Info.m_manualSend = manualSend;
  165. ////only send a response port if it's different than the default
  166. if (g_Opt.m_lPort != 23443)
  167. {
  168. Info.m_respondPort = g_Opt.m_lPort;
  169. }
  170. //Send all text over as UTF-8
  171. CStringA dest;
  172. if(CTextConvert::ConvertToUTF8(GetComputerName(), dest))
  173. {
  174. strncpy(Info.m_cComputerName, dest, sizeof(Info.m_cComputerName));
  175. }
  176. if(CTextConvert::ConvertToUTF8(GetIPAddress(), dest))
  177. {
  178. strncpy(Info.m_cIP, dest, sizeof(Info.m_cIP));
  179. }
  180. if(CTextConvert::ConvertToUTF8(pClip->m_Desc, dest))
  181. {
  182. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  183. }
  184. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  185. Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
  186. Info.m_cIP[sizeof(Info.m_cIP)-1] = 0;
  187. m_SendSocket.SetSocket(m_Connection);
  188. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  189. return FALSE;
  190. CClipFormat* pCF;
  191. INT_PTR count = pClip->m_Formats.GetSize();
  192. //For each data type
  193. for(int i=0; i < count; i++)
  194. {
  195. pCF = &pClip->m_Formats.GetData()[i];
  196. SendClipFormat(pCF);
  197. }
  198. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  199. return FALSE;
  200. theApp.m_lClipsSent++;
  201. return TRUE;
  202. }
  203. BOOL CClient::SendClipFormat(CClipFormat* pCF)
  204. {
  205. CSendInfo Info;
  206. LPVOID pvData = GlobalLock(pCF->m_hgData);
  207. INT_PTR length = GlobalSize(pCF->m_hgData);
  208. UCHAR* pOutput = NULL;
  209. int nLenOutput = 0;
  210. CTextConvert Convert;
  211. BOOL bRet = FALSE;
  212. LogSendRecieveInfo(StrF(_T("BEFORE Encrypt clip data %d"), length));
  213. if(m_SendSocket.m_pEncryptor)
  214. {
  215. if(m_SendSocket.m_pEncryptor->Encrypt((UCHAR*)pvData, (int)length, g_Opt.m_csPassword, pOutput, nLenOutput))
  216. {
  217. LogSendRecieveInfo(StrF(_T("AFTER Encrypt clip data %d"), nLenOutput));
  218. Info.m_lParameter1 = nLenOutput;
  219. //Send over as UTF-8
  220. CStringA dest;
  221. if(CTextConvert::ConvertToUTF8(GetFormatName(pCF->m_cfType), dest))
  222. {
  223. strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc));
  224. Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0;
  225. }
  226. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_START) == FALSE)
  227. return FALSE;
  228. m_SendSocket.SendExactSize((char*)pOutput, nLenOutput, false);
  229. m_SendSocket.m_pEncryptor->FreeBuffer(pOutput);
  230. bRet = TRUE;
  231. }
  232. else
  233. {
  234. LogSendRecieveInfo("Failed to encrypt data");
  235. return FALSE;
  236. }
  237. }
  238. else
  239. {
  240. ASSERT(!"SendItem::Encryption not initialized");
  241. LogSendRecieveInfo("SendItem::Encryption not initialized");
  242. }
  243. GlobalUnlock(pCF->m_hgData);
  244. if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_END) == FALSE)
  245. return FALSE;
  246. return bRet;
  247. }
  248. HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CString csComputerName)
  249. {
  250. CSendInfo Info;
  251. bool bBreak = false;
  252. HGLOBAL hReturn = NULL;
  253. CString csErrorString;
  254. CFileTransferProgressDlg *pProgress = new CFileTransferProgressDlg;
  255. if(pProgress == NULL)
  256. return NULL;
  257. LogSendRecieveInfo(StrF(_T("************** START of requesting files from cpu %s, ip: %s **************"), csComputerName, csIP));
  258. pProgress->Create(IDD_DIALOG_REMOTE_FILE);
  259. pProgress->ShowWindow(SW_SHOW);
  260. pProgress->SetMessage(StrF(_T("Opening Connection to %s (%s)"), csComputerName, csIP));
  261. pProgress->PumpMessages();
  262. CString requestFrom;
  263. if(g_Opt.GetRequestFilesUsingIP())
  264. {
  265. requestFrom = csIP;
  266. }
  267. else
  268. {
  269. requestFrom = csComputerName;
  270. }
  271. do
  272. {
  273. if(OpenConnection(requestFrom) == FALSE)
  274. {
  275. csErrorString.Format(_T("Error Opening Connection to %s (%s)"), csComputerName, csIP);
  276. break;
  277. }
  278. m_SendSocket.SetSocket(m_Connection);
  279. m_SendSocket.SetProgressBar(pProgress);
  280. if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE)
  281. break;
  282. if(SendClipFormat(&HDropFormat) == FALSE)
  283. {
  284. csErrorString = _T("Error sending data request.");
  285. break;
  286. }
  287. if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE)
  288. break;
  289. pProgress->SetMessage(StrF(_T("Requesting Files from %s (%s)"), csComputerName, csIP));
  290. if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE)
  291. break;
  292. CFileRecieve Recieve;
  293. long lRet = Recieve.RecieveFiles(m_Connection, csIP, pProgress);
  294. if(lRet == TRUE)
  295. {
  296. hReturn = Recieve.CreateCF_HDROPBuffer();
  297. }
  298. else if(lRet == FALSE || lRet == MD5_MISMATCH)
  299. {
  300. if(pProgress != NULL && pProgress->Cancelled())
  301. {
  302. //Don't show an error message the user canceled things
  303. }
  304. else
  305. {
  306. csErrorString = _T("Error receiving files.");
  307. if (lRet == MD5_MISMATCH)
  308. {
  309. csErrorString += _T(" MD5 Match Error.");
  310. }
  311. }
  312. }
  313. } while(false);
  314. CloseConnection();
  315. if(hReturn == NULL && csErrorString.IsEmpty() == FALSE)
  316. {
  317. ::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)csErrorString.GetBuffer(csErrorString.GetLength()), 0);
  318. }
  319. pProgress->DestroyWindow();
  320. LogSendRecieveInfo(StrF(_T("************** END of requesting files from cpu %s, ip: %s **************************"), csComputerName, csIP));
  321. return hReturn;
  322. }